Skip to content

Commit

Permalink
Dft xpc2 (project-chip#36719)
Browse files Browse the repository at this point in the history
* [darwin-framework-tool][XPC] Add a XPC server registry.

* [darwin-framework-tool][XPC] Add a XPC server for MTRDeviceControllerServerProtocol.

* [darwin-framework-tool][XPC] Add a XPC server for MTRXPCServerProtocol
  • Loading branch information
vivien-apple authored and silabs-olivierb committed Feb 19, 2025
1 parent c6a6537 commit 3880312
Show file tree
Hide file tree
Showing 9 changed files with 867 additions and 7 deletions.
3 changes: 3 additions & 0 deletions examples/darwin-framework-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ executable("darwin-framework-tool") {
"commands/common/PreferencesStorage.mm",
"commands/common/RemoteDataModelLogger.h",
"commands/common/RemoteDataModelLogger.mm",
"commands/common/xpc/DeviceControllerServer.mm",
"commands/common/xpc/XPCServer.mm",
"commands/common/xpc/XPCServerRegistry.mm",
"commands/configuration/Commands.h",
"commands/configuration/ResetMRPParametersCommand.h",
"commands/configuration/ResetMRPParametersCommand.mm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class CHIPCommandBridge : public Command {
AddArgument("commissioner-vendor-id", 0, UINT16_MAX, &mCommissionerVendorId,
"The vendor id to use for darwin-framework-tool. If not provided, chip::VendorId::TestVendor1 (65521, 0xFFF1) will be "
"used.");
AddArgument("use-xpc", &mUseXPC, "Use a controller that will connect to an XPC endpoint instead of talking to devices directly. If a string argument is provided, it must identify a Mach service name that can be used to connect to a remote endpoint. If no argument is provided, a local endpoint will be used.");
}

/////////// Command Interface /////////
Expand Down Expand Up @@ -168,4 +169,5 @@ class CHIPCommandBridge : public Command {
chip::Optional<char *> mPaaTrustStorePath;
chip::Optional<chip::VendorId> mCommissionerVendorId;
std::string mCurrentIdentity;
chip::Optional<chip::app::DataModel::Nullable<char *>> mUseXPC;
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#import "DeviceDelegate.h"
#include "MTRError_Utils.h"

#include "xpc/XPCServerRegistry.h"

#include <map>
#include <string>

Expand All @@ -45,6 +47,17 @@
bool CHIPCommandBridge::sUseSharedStorage = true;
constexpr char kTrustStorePathVariable[] = "PAA_TRUST_STORE_PATH";

namespace {
NSString * ToNSString(const chip::Optional<chip::app::DataModel::Nullable<char *>> & string)
{
if (!string.HasValue() && string.Value().IsNull()) {
return nil;
}

return @(string.Value().Value());
}
}

CHIP_ERROR CHIPCommandBridge::Run()
{
// In interactive mode, we want to avoid memory accumulating in the main autorelease pool,
Expand Down Expand Up @@ -143,6 +156,8 @@
productAttestationAuthorityCertificates = nil;
}

[[XPCServerRegistry sharedInstance] start];

sUseSharedStorage = mCommissionerSharedStorage.ValueOr(false);
if (sUseSharedStorage) {
return SetUpStackWithSharedStorage(productAttestationAuthorityCertificates);
Expand Down Expand Up @@ -202,7 +217,13 @@

params.productAttestationAuthorityCertificates = productAttestationAuthorityCertificates;

__auto_type * controller = [[MTRDeviceController alloc] initWithParameters:params error:&error];
MTRDeviceController * controller = nil;
if (mUseXPC.HasValue()) {
__auto_type * identifier = uuidString;
controller = [[XPCServerRegistry sharedInstance] createController:identifier serviceName:ToNSString(mUseXPC) params:params error:&error];
} else {
controller = [[MTRDeviceController alloc] initWithParameters:params error:&error];
}
VerifyOrReturnError(nil != controller, MTRErrorToCHIPErrorCode(error), ChipLogError(chipTool, "Controller startup failure: %@", error));
mControllers[identities[i]] = controller;
}
Expand Down Expand Up @@ -237,12 +258,18 @@
params.nodeId = @(mCommissionerNodeId.Value());
}

// We're not sure whether we're creating a new fabric or using an existing one, so just try both.
auto controller = [factory createControllerOnExistingFabric:params error:&error];
if (controller == nil) {
// Maybe we didn't have this fabric yet.
params.vendorID = @(mCommissionerVendorId.ValueOr(chip::VendorId::TestVendor1));
controller = [factory createControllerOnNewFabric:params error:&error];
MTRDeviceController * controller = nil;
if (mUseXPC.HasValue()) {
__auto_type * identifier = @(identities[i]);
controller = [[XPCServerRegistry sharedInstance] createController:identifier serviceName:ToNSString(mUseXPC) params:params error:&error];
} else {
// We're not sure whether we're creating a new fabric or using an existing one, so just try both.
controller = [factory createControllerOnExistingFabric:params error:&error];
if (controller == nil) {
// Maybe we didn't have this fabric yet.
params.vendorID = @(mCommissionerVendorId.ValueOr(chip::VendorId::TestVendor1));
controller = [factory createControllerOnNewFabric:params error:&error];
}
}
VerifyOrReturnError(nil != controller, MTRErrorToCHIPErrorCode(error), ChipLogError(chipTool, "Controller startup failure: %@", error));
mControllers[identities[i]] = controller;
Expand All @@ -256,6 +283,8 @@
if (IsInteractive()) {
return;
}

[[XPCServerRegistry sharedInstance] stop];
ShutdownCommissioner();
}

Expand Down
Loading

0 comments on commit 3880312

Please sign in to comment.