Skip to content

Commit

Permalink
Add dynamic dispatcher and access control for Android controller
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Sep 15, 2023
1 parent f5e6962 commit d43ee4a
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 196 deletions.
1 change: 1 addition & 0 deletions examples/java-matter-controller/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ chip_project_config_include_dirs =
[ "${chip_root}/examples/java-matter-controller/include" ]
chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]
chip_stack_lock_tracking = "fatal"
chip_build_controler_dynamic_server = true
1 change: 1 addition & 0 deletions src/app/common_flags.gni
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ declare_args() {
# Temporary flag for interaction model and echo protocols, set it to true to enable
chip_app_use_echo = false
chip_enable_read_client = true
chip_build_controler_dynamic_server = false
}
42 changes: 23 additions & 19 deletions src/app/dynamic_server/AccessControl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Project CHIP Authors
* Copyright (c) 2022-2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

#import "MTRControllerAccessControl.h"
#include "AccessControl.h"

#include <access/AccessControl.h>
#include <access/Privilege.h>
Expand All @@ -30,10 +30,11 @@ using namespace chip::app::Clusters;

namespace {
// TODO: Maybe consider making this configurable? See also
// CHIPIMDispatch.mm.
// DynamicDispatch.cpp.
constexpr EndpointId kSupportedEndpoint = 0;

class DeviceTypeResolver : public Access::AccessControl::DeviceTypeResolver {
class DeviceTypeResolver : public Access::AccessControl::DeviceTypeResolver
{
public:
bool IsDeviceTypeOnEndpoint(DeviceTypeId deviceType, EndpointId endpoint) override
{
Expand All @@ -42,22 +43,26 @@ class DeviceTypeResolver : public Access::AccessControl::DeviceTypeResolver {
} gDeviceTypeResolver;

// TODO: Make the policy more configurable by consumers.
class AccessControlDelegate : public Access::AccessControl::Delegate {
CHIP_ERROR Check(
const SubjectDescriptor & subjectDescriptor, const RequestPath & requestPath, Privilege requestPrivilege) override
class AccessControlDelegate : public Access::AccessControl::Delegate
{
CHIP_ERROR Check(const SubjectDescriptor & subjectDescriptor, const RequestPath & requestPath,
Privilege requestPrivilege) override
{
if (requestPath.endpoint != kSupportedEndpoint || requestPath.cluster != OtaSoftwareUpdateProvider::Id) {
if (requestPath.endpoint != kSupportedEndpoint || requestPath.cluster != OtaSoftwareUpdateProvider::Id)
{
// We only allow access to OTA software update provider.
return CHIP_ERROR_ACCESS_DENIED;
}

if (requestPrivilege != Privilege::kOperate) {
if (requestPrivilege != Privilege::kOperate)
{
// The commands on OtaSoftwareUpdateProvider all require
// Operate; we should not be asked for anything else.
return CHIP_ERROR_ACCESS_DENIED;
}

if (subjectDescriptor.authMode != AuthMode::kCase && subjectDescriptor.authMode != AuthMode::kPase) {
if (subjectDescriptor.authMode != AuthMode::kCase && subjectDescriptor.authMode != AuthMode::kPase)
{
// No idea who is asking; deny for now.
return CHIP_ERROR_ACCESS_DENIED;
}
Expand All @@ -71,14 +76,13 @@ class AccessControlDelegate : public Access::AccessControl::Delegate {
AccessControlDelegate gDelegate;
} // anonymous namespace

@implementation MTRControllerAccessControl

+ (void)init
namespace chip {
namespace app {
namespace dynamic_server {
void InitAccessControl()
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
GetAccessControl().Init(&gDelegate, gDeviceTypeResolver);
});
GetAccessControl().Init(&gDelegate, gDeviceTypeResolver);
}

@end
} // namespace dynamic_server
} // namespace app
} // namespace chip
26 changes: 8 additions & 18 deletions src/app/dynamic_server/AccessControl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 Project CHIP Authors
* Copyright (c) 2022-2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,24 +15,14 @@
*/
#pragma once

#import <Foundation/Foundation.h>
#import <Matter/MTRDefines.h>

NS_ASSUME_NONNULL_BEGIN

MTR_HIDDEN
@interface MTRControllerAccessControl : NSObject

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
+ (instancetype)alloc NS_UNAVAILABLE;

namespace chip {
namespace app {
namespace dynamic_server {
/**
* Initialize the access control module. Must be called on the Matter task
* queue.
*/
+ (void)init;

@end

NS_ASSUME_NONNULL_END
void InitAccessControl();
} // namespace dynamic_server
} // namespace app
} // namespace chip
Loading

0 comments on commit d43ee4a

Please sign in to comment.