Skip to content

Commit

Permalink
Darwin compile under gn (project-chip#5403)
Browse files Browse the repository at this point in the history
* src/darwin/Framework: error: implicit conversion loses integer precision

* Add src/examples/chip-tool-darwin in order to compile src/darwin/Framework files more often and avoid breakages

* Address some review comments and update some .gn files style
  • Loading branch information
vivien-apple authored and jimlyall-q committed Mar 26, 2021
1 parent e970b2e commit 50b1f62
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 16 deletions.
14 changes: 14 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)" ]
Expand Down Expand Up @@ -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" ]
}
Expand Down
25 changes: 25 additions & 0 deletions examples/chip-tool-darwin/.gn
Original file line number Diff line number Diff line change
@@ -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")
}
79 changes: 79 additions & 0 deletions examples/chip-tool-darwin/BUILD.gn
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions examples/chip-tool-darwin/args.gni
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions examples/chip-tool-darwin/build_overrides
30 changes: 30 additions & 0 deletions examples/chip-tool-darwin/main.m
Original file line number Diff line number Diff line change
@@ -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 <CHIP/CHIPDevice.h>
#import <CHIP/CHIPDeviceController.h>
#import <CHIP/CHIPDevicePairingDelegate.h>
#import <CHIP/CHIPError.h>
#import <CHIP/CHIPManualSetupPayloadParser.h>
#import <CHIP/CHIPPersistentStorageDelegate.h>
#import <CHIP/CHIPQRCodeSetupPayloadParser.h>
#import <CHIP/CHIPSetupPayload.h>
#import <CHIP/gen/CHIPClustersObjc.h>
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) { return EXIT_SUCCESS; }
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 20 additions & 4 deletions src/darwin/Framework/CHIP/CHIPDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 ()

Expand Down
20 changes: 12 additions & 8 deletions src/darwin/Framework/CHIP/CHIPPersistentStorageDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 50b1f62

Please sign in to comment.