diff --git a/BUILD.gn b/BUILD.gn index b835b9ac26f881..5d15e381223b03 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -168,6 +168,10 @@ if (current_toolchain != "${dir_pw_toolchain}/dummy:dummy") { enable_standalone_chip_tool_build = enable_default_builds && host_os != "win" + # Build the chip-tool-darwin example. + enable_standalone_chip_tool_darwin_build = + enable_default_builds && host_os == "mac" + # Build the shell example. enable_standalone_shell_build = enable_default_builds && host_os != "win" @@ -244,6 +248,13 @@ if (current_toolchain != "${dir_pw_toolchain}/dummy:dummy") { } } + if (enable_standalone_chip_tool_darwin_build) { + group("standalone_chip_tool_darwin") { + deps = + [ "${chip_root}/examples/chip-tool-darwin(${standalone_toolchain})" ] + } + } + if (enable_cc13x2x7_26x2x7_lock_app_build) { group("cc13x2x7_26x2x7_lock_app") { deps = [ "${chip_root}/examples/lock-app/cc13x2x7_26x2x7(${chip_root}/config/cc13x2_26x2/toolchain:cc13x2x7_26x2x7_lock_app)" ] @@ -326,6 +337,9 @@ if (current_toolchain != "${dir_pw_toolchain}/dummy:dummy") { if (enable_standalone_chip_tool_build) { deps += [ ":standalone_chip_tool" ] } + if (enable_standalone_chip_tool_darwin_build) { + deps += [ ":standalone_chip_tool_darwin" ] + } if (enable_standalone_shell_build) { deps += [ ":standalone_shell" ] } diff --git a/examples/chip-tool-darwin/.gn b/examples/chip-tool-darwin/.gn new file mode 100644 index 00000000000000..5d1ce757507582 --- /dev/null +++ b/examples/chip-tool-darwin/.gn @@ -0,0 +1,25 @@ +# Copyright (c) 2020 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build_overrides/build.gni") + +# The location of the build configuration file. +buildconfig = "${build_root}/config/BUILDCONFIG.gn" + +# CHIP uses angle bracket includes. +check_system_includes = true + +default_args = { + import("//args.gni") +} diff --git a/examples/chip-tool-darwin/BUILD.gn b/examples/chip-tool-darwin/BUILD.gn new file mode 100644 index 00000000000000..6bb03ed0d69c7a --- /dev/null +++ b/examples/chip-tool-darwin/BUILD.gn @@ -0,0 +1,79 @@ +# Copyright (c) 2020 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build_overrides/build.gni") +import("//build_overrides/chip.gni") + +import("${chip_root}/build/chip/tools.gni") + +assert(chip_build_tools) + +config("includes") { + include_dirs = [ + ".", + "${chip_root}/src/darwin/Framework", + "${chip_root}/src/darwin/Framework/CHIP", + ] +} + +executable("chip-tool-darwin") { + sources = [ + "${chip_root}/src/app/clusters/media-playback-client/media-playback-client.cpp", + "${chip_root}/src/app/reporting/reporting-default-configuration.cpp", + "${chip_root}/src/app/reporting/reporting.cpp", + "${chip_root}/src/app/server/DataModelHandler.cpp", + "${chip_root}/src/app/util/af-event.cpp", + "${chip_root}/src/app/util/af-main-common.cpp", + "${chip_root}/src/app/util/attribute-size.cpp", + "${chip_root}/src/app/util/attribute-storage.cpp", + "${chip_root}/src/app/util/attribute-table.cpp", + "${chip_root}/src/app/util/binding-table.cpp", + "${chip_root}/src/app/util/chip-message-send.cpp", + "${chip_root}/src/app/util/client-api.cpp", + "${chip_root}/src/app/util/ember-print.cpp", + "${chip_root}/src/app/util/message.cpp", + "${chip_root}/src/app/util/process-cluster-message.cpp", + "${chip_root}/src/app/util/process-global-message.cpp", + "${chip_root}/src/app/util/util.cpp", + "${chip_root}/src/darwin/Framework/CHIP/CHIPDevice.mm", + "${chip_root}/src/darwin/Framework/CHIP/CHIPDeviceController.mm", + "${chip_root}/src/darwin/Framework/CHIP/CHIPDevicePairingDelegateBridge.mm", + "${chip_root}/src/darwin/Framework/CHIP/CHIPError.mm", + "${chip_root}/src/darwin/Framework/CHIP/CHIPManualSetupPayloadParser.mm", + "${chip_root}/src/darwin/Framework/CHIP/CHIPPersistentStorageDelegateBridge.mm", + "${chip_root}/src/darwin/Framework/CHIP/CHIPQRCodeSetupPayloadParser.mm", + "${chip_root}/src/darwin/Framework/CHIP/CHIPSetupPayload.mm", + "${chip_root}/src/darwin/Framework/CHIP/gen/CHIPClientCallbacks.cpp", + "${chip_root}/src/darwin/Framework/CHIP/gen/CHIPClustersObjc.mm", + "${chip_root}/src/darwin/Framework/CHIP/gen/call-command-handler.cpp", + "${chip_root}/src/darwin/Framework/CHIP/gen/callback-stub.cpp", + "main.m", + ] + + cflags = [ + "-Wconversion", + "-fobjc-arc", + ] + + public_deps = [ + "${chip_root}/src/app/server", + "${chip_root}/src/lib", + "${chip_root}/src/platform", + "${chip_root}/third_party/inipp", + ] + + public_configs = [ ":includes" ] + + output_dir = root_out_dir +} diff --git a/examples/chip-tool-darwin/args.gni b/examples/chip-tool-darwin/args.gni new file mode 100644 index 00000000000000..311ddab32d5fe5 --- /dev/null +++ b/examples/chip-tool-darwin/args.gni @@ -0,0 +1,17 @@ +# Copyright (c) 2020 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build_overrides/chip.gni") + +import("${chip_root}/config/standalone/args.gni") diff --git a/examples/chip-tool-darwin/build_overrides b/examples/chip-tool-darwin/build_overrides new file mode 120000 index 00000000000000..b430cf6a2e6391 --- /dev/null +++ b/examples/chip-tool-darwin/build_overrides @@ -0,0 +1 @@ +../build_overrides \ No newline at end of file diff --git a/examples/chip-tool-darwin/main.m b/examples/chip-tool-darwin/main.m new file mode 100644 index 00000000000000..41057656779108 --- /dev/null +++ b/examples/chip-tool-darwin/main.m @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import + +int main(int argc, const char * argv[]) { return EXIT_SUCCESS; } diff --git a/src/darwin/Framework/CHIP/CHIPDevice.h b/src/darwin/Framework/CHIP/CHIPDevice.h index b3a1dba6c27796..63cb2d423fe319 100644 --- a/src/darwin/Framework/CHIP/CHIPDevice.h +++ b/src/darwin/Framework/CHIP/CHIPDevice.h @@ -24,8 +24,8 @@ NS_ASSUME_NONNULL_BEGIN @interface CHIPDevice : NSObject -- (BOOL)openPairingWindow:(NSTimeInterval)duration error:(NSError * __autoreleasing *)error; -- (NSString *)openPairingWindowWithPIN:(NSTimeInterval)duration +- (BOOL)openPairingWindow:(NSUInteger)duration error:(NSError * __autoreleasing *)error; +- (NSString *)openPairingWindowWithPIN:(NSUInteger)duration discriminator:(NSUInteger)discriminator setupPIN:(NSUInteger)setupPIN error:(NSError * __autoreleasing *)error; diff --git a/src/darwin/Framework/CHIP/CHIPDevice.mm b/src/darwin/Framework/CHIP/CHIPDevice.mm index 2881694ff83a3a..f101ab0f172717 100644 --- a/src/darwin/Framework/CHIP/CHIPDevice.mm +++ b/src/darwin/Framework/CHIP/CHIPDevice.mm @@ -51,15 +51,23 @@ - (instancetype)initWithDevice:(chip::Controller::Device *)device return _cppDevice; } -- (BOOL)openPairingWindow:(NSTimeInterval)duration error:(NSError * __autoreleasing *)error +- (BOOL)openPairingWindow:(NSUInteger)duration error:(NSError * __autoreleasing *)error { CHIP_ERROR err = CHIP_NO_ERROR; chip::SetupPayload setupPayload; + if (duration > UINT32_MAX) { + CHIP_LOG_ERROR("Error: Duration %tu is too large. Max value %d", duration, UINT32_MAX); + if (error) { + *error = [CHIPError errorForCHIPErrorCode:CHIP_ERROR_INVALID_INTEGER_VALUE]; + } + return NO; + } + [self.lock lock]; err = self.cppDevice->OpenPairingWindow( - duration, chip::Controller::Device::PairingWindowOption::kOriginalSetupCode, setupPayload); + (uint32_t) duration, chip::Controller::Device::PairingWindowOption::kOriginalSetupCode, setupPayload); [self.lock unlock]; if (err != CHIP_NO_ERROR) { @@ -73,7 +81,7 @@ - (BOOL)openPairingWindow:(NSTimeInterval)duration error:(NSError * __autoreleas return YES; } -- (NSString *)openPairingWindowWithPIN:(NSTimeInterval)duration +- (NSString *)openPairingWindowWithPIN:(NSUInteger)duration discriminator:(NSUInteger)discriminator setupPIN:(NSUInteger)setupPIN error:(NSError * __autoreleasing *)error @@ -82,6 +90,14 @@ - (NSString *)openPairingWindowWithPIN:(NSTimeInterval)duration chip::SetupPayload setupPayload; + if (duration > UINT32_MAX) { + CHIP_LOG_ERROR("Error: Duration %tu is too large. Max value %d", duration, UINT32_MAX); + if (error) { + *error = [CHIPError errorForCHIPErrorCode:CHIP_ERROR_INVALID_INTEGER_VALUE]; + } + return nil; + } + if (discriminator > 0xfff) { CHIP_LOG_ERROR("Error: Discriminator %tu is too large. Max value %d", discriminator, 0xfff); if (error) { @@ -97,7 +113,7 @@ - (NSString *)openPairingWindowWithPIN:(NSTimeInterval)duration [self.lock lock]; err = self.cppDevice->OpenPairingWindow( - duration, chip::Controller::Device::PairingWindowOption::kTokenWithProvidedPIN, setupPayload); + (uint32_t) duration, chip::Controller::Device::PairingWindowOption::kTokenWithProvidedPIN, setupPayload); [self.lock unlock]; if (err != CHIP_NO_ERROR) { diff --git a/src/darwin/Framework/CHIP/CHIPDeviceController.mm b/src/darwin/Framework/CHIP/CHIPDeviceController.mm index 469d7b41064686..63c08824590ec1 100644 --- a/src/darwin/Framework/CHIP/CHIPDeviceController.mm +++ b/src/darwin/Framework/CHIP/CHIPDeviceController.mm @@ -29,7 +29,6 @@ static const char * const CHIP_COMMISSIONER_DEVICE_ID_KEY = "com.zigbee.chip.commissioner.device.id"; static NSString * const kErrorMemoryInit = @"Init Memory failure"; -static NSString * const kErrorCommissionerCreate = @"Init failure while creating a commissioner"; static NSString * const kErrorCommissionerInit = @"Init failure while initializing a commissioner"; static NSString * const kErrorPairingInit = @"Init failure while creating a pairing delegate"; static NSString * const kErrorPersistentStorageInit = @"Init failure while creating a persistent storage delegate"; @@ -38,7 +37,6 @@ static NSString * const kErrorUnpairDevice = @"Failure while unpairing the device"; static NSString * const kErrorStopPairing = @"Failure while trying to stop the pairing process"; static NSString * const kErrorGetPairedDevice = @"Failure while trying to retrieve a paired device"; -static NSString * const kInfoStackShutdown = @"Shutting down the CHIP Stack"; @interface CHIPDeviceController () diff --git a/src/darwin/Framework/CHIP/CHIPPersistentStorageDelegateBridge.mm b/src/darwin/Framework/CHIP/CHIPPersistentStorageDelegateBridge.mm index aa235928acb398..71a2814f4a4a97 100644 --- a/src/darwin/Framework/CHIP/CHIPPersistentStorageDelegateBridge.mm +++ b/src/darwin/Framework/CHIP/CHIPPersistentStorageDelegateBridge.mm @@ -120,17 +120,21 @@ } if (valueString != nil) { - if (value != nullptr) { - size = strlcpy(value, [valueString UTF8String], size); - if (size < [valueString length]) { + if (([valueString lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1) > UINT16_MAX) { + error = CHIP_ERROR_BUFFER_TOO_SMALL; + } else { + if (value != nullptr) { + size = (uint16_t) strlcpy(value, [valueString UTF8String], size); + if (size < [valueString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]) { + error = CHIP_ERROR_NO_MEMORY; + } + } else { + size = (uint16_t) [valueString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; error = CHIP_ERROR_NO_MEMORY; } - } else { - size = [valueString length]; - error = CHIP_ERROR_NO_MEMORY; + // Increment size to account for null termination + size += 1; } - // Increment size to account for null termination - size += 1; } else { error = CHIP_ERROR_KEY_NOT_FOUND; }