Skip to content

Commit

Permalink
Add ZCL API Objc Wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Jan 11, 2021
1 parent bc4f841 commit 936dbbc
Show file tree
Hide file tree
Showing 10 changed files with 3,983 additions and 151 deletions.
10 changes: 10 additions & 0 deletions src/app/zap-templates/chip-templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
"name": "C++ ZCL API",
"output": "src/controller/CHIPClusters.cpp"
},
{
"path": "templates/chip/CHIPClustersObjc.zapt",
"name": "Objc ZCL API Header",
"output": "/src/darwin/Framework/CHIP/CHIPClustersObjc.h"
},
{
"path": "templates/chip/CHIPClustersObjc-src.zapt",
"name": "Objc ZCL API",
"output": "/src/darwin/Framework/CHIP/CHIPClustersObjc.mm"
},
{
"path": "templates/chip/chip-zcl-zpro-codec-api.zapt",
"name": "CHIP ZCL API Header",
Expand Down
118 changes: 118 additions & 0 deletions src/app/zap-templates/templates/chip/CHIPClustersObjc-src.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{{> header}}

#import <Foundation/Foundation.h>

#import "CHIPCallbackBridge.h"
#import "CHIPDevice.h"
#import "CHIPDevice_Internal.h"
#import "CHIPClustersObjc.h"

#include <controller/CHIPClusters.h>

{{#chip_server_clusters}}

@interface CHIP{{asCamelCased name false}} ()

@property (readonly) chip::Controller::{{asCamelCased name false}}Cluster cppCluster;
@property (readonly, nonatomic) dispatch_queue_t callbackQueue;
@end

@implementation CHIP{{asCamelCased name false}}

- (instancetype)initWithDevice:(CHIPDevice *)device endpoint:(chip::EndpointId)endpoint queue:(dispatch_queue_t)queue
{
CHIP_ERROR err = _cppCluster.Associate([device internalDevice], endpoint);

if (err != CHIP_NO_ERROR) {
return nil;
}

if (self = [super init]) {
_callbackQueue = queue;
}
return self;
}

{{#chip_server_cluster_commands}}
- (BOOL){{asCamelCased name}}:(CHIPDeviceCallback)onCompletion{{#chip_server_cluster_command_arguments}} {{asCamelCased label}}:({{chipType}}){{asCamelCased label}}{{/chip_server_cluster_command_arguments}}
{
CHIPCallbackBridge * callback = new CHIPCallbackBridge(onCompletion, _callbackQueue);
if (!callback) {
return NO;
}

CHIP_ERROR err = self.cppCluster.{{asCamelCased name false}}(callback{{#chip_server_cluster_command_arguments}}, {{asCamelCased label}}{{/chip_server_cluster_command_arguments}});
if (err != CHIP_NO_ERROR) {
callback->Cancel();
delete callback;
return NO;
}
return YES;
}
{{/chip_server_cluster_commands}}

{{#chip_server_cluster_attributes}}
- (BOOL)readAttribute{{asCamelCased name false}}:(CHIPDeviceCallback)onCompletion
{
CHIPCallbackBridge * callback = new CHIPCallbackBridge(onCompletion, _callbackQueue);
if (!callback) {
return NO;
}

CHIP_ERROR err = self.cppCluster.ReadAttribute{{asCamelCased name false}}(callback);
if (err != CHIP_NO_ERROR) {
callback->Cancel();
delete callback;
return NO;
}
return YES;
}

{{#if (isWritableAttribute)}}
- (BOOL)writeAttribute{{asCamelCased name false}}:(CHIPDeviceCallback)onCompletion value:({{asUnderlyingZclType type}})value
{
CHIPCallbackBridge * callback = new CHIPCallbackBridge(onCompletion, _callbackQueue);
if (!callback) {
return NO;
}

CHIP_ERROR err = self.cppCluster.WriteAttribute{{asCamelCased name false}}(callback, value);
if (err != CHIP_NO_ERROR) {
callback->Cancel();
delete callback;
return NO;
}
return YES;
}

{{/if}}
{{#if (isReportableAttribute)}}
- (BOOL) reportAttribute{{asCamelCased name false}}:(CHIPDeviceCallback)onCompletion onChange:(CHIPDeviceCallback)onChange minInterval:(uint16_t)minInterval maxInterval:(uint16_t)maxInterval{{#unless (isDiscreteType)}} change:({{chipType}})change{{/unless}}
{
CHIPCallbackBridge * completionCallback = new CHIPCallbackBridge(onCompletion, _callbackQueue);
if (!completionCallback) {
return NO;
}

CHIPCallbackBridge * changeCallback = new CHIPCallbackBridge(onChange, _callbackQueue);
if (!changeCallback) {
return NO;
}

CHIP_ERROR err = self.cppCluster.ReportAttribute{{asCamelCased name false}}(completionCallback, changeCallback, minInterval, maxInterval{{#unless (isDiscreteType)}}, change{{/unless}});
if (err != CHIP_NO_ERROR) {
completionCallback->Cancel();
changeCallback->Cancel();
delete completionCallback;
delete changeCallback;
return NO;
}
return YES;
}

{{/if}}
{{/chip_server_cluster_attributes}}

@end

{{/chip_server_clusters}}
40 changes: 40 additions & 0 deletions src/app/zap-templates/templates/chip/CHIPClustersObjc.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{> header}}

#ifndef CHIP_CLUSTERS_H
#define CHIP_CLUSTERS_H

#import <CHIP/CHIPDeviceCallback.h>
#import <Foundation/Foundation.h>

@class CHIPDevice;

{{#chip_server_clusters}}

NS_ASSUME_NONNULL_BEGIN

@interface CHIP{{asCamelCased name false}} : NSObject

- (nullable instancetype)initWithDevice:(CHIPDevice *)device endpoint:(uint8_t)endpoint queue:(dispatch_queue_t)queue;
{{#chip_server_cluster_commands}}
- (BOOL){{asCamelCased name}}:(CHIPDeviceCallback)onCompletion{{#chip_server_cluster_command_arguments}} {{asCamelCased label}}:({{#if (isStrEqual chipType "chip::ClusterId")}}uint16_t{{else}}{{chipType}}{{/if}}){{asCamelCased label}}{{/chip_server_cluster_command_arguments}};
{{/chip_server_cluster_commands}}

{{#chip_server_cluster_attributes}}
- (BOOL)readAttribute{{asCamelCased name false}}:(CHIPDeviceCallback)onCompletion;
{{#if (isWritableAttribute)}}
- (BOOL)writeAttribute{{asCamelCased name false}}:(CHIPDeviceCallback)onCompletion value:({{asUnderlyingZclType type}})value;
{{/if}}
{{#if (isReportableAttribute)}}
- (BOOL) reportAttribute{{asCamelCased name false}}:(CHIPDeviceCallback)onCompletion onChange:(CHIPDeviceCallback)onChange minInterval:(uint16_t)minInterval maxInterval:(uint16_t)maxInterval{{#unless (isDiscreteType)}} change:({{chipType}})change{{/unless}};
{{/if}}
{{/chip_server_cluster_attributes}}

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

@end

NS_ASSUME_NONNULL_END
{{/chip_server_clusters}}

#endif /* CHIP_CLUSTERS_H */
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,23 @@ - (IBAction)onButtonTapped:(id)sender
CHIPDeviceCallback completionHandler = ^(NSError * error) {
NSLog(@"Status: On command completed with error %@", [error description]);
};
[self.onOff lightOn:completionHandler];
[self.onOff on:completionHandler];
}

- (IBAction)offButtonTapped:(id)sender
{
CHIPDeviceCallback completionHandler = ^(NSError * error) {
NSLog(@"Status: Off command completed with error %@", [error description]);
};
[self.onOff lightOff:completionHandler];
[self.onOff off:completionHandler];
}

- (IBAction)toggleButtonTapped:(id)sender
{
CHIPDeviceCallback completionHandler = ^(NSError * error) {
NSLog(@"Status: Toggle command completed with error %@", [error description]);
};
[self.onOff toggleLight:completionHandler];
[self.onOff toggle:completionHandler];
}

// MARK: CHIPDeviceControllerDelegate
Expand Down
16 changes: 8 additions & 8 deletions src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
1EF8B75425782A9E009AA59C /* CHIPClustersObjc.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EF8B75325782A9E009AA59C /* CHIPClustersObjc.h */; settings = {ATTRIBUTES = (Public, ); }; };
1EF8B75625782AAC009AA59C /* CHIPClustersObjc.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1EF8B75525782AAC009AA59C /* CHIPClustersObjc.mm */; };
2C222AD0255C620600E446B9 /* CHIPDevice.h in Headers */ = {isa = PBXBuildFile; fileRef = 2C222ACE255C620600E446B9 /* CHIPDevice.h */; settings = {ATTRIBUTES = (Public, ); }; };
2C222AD1255C620600E446B9 /* CHIPDevice.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2C222ACF255C620600E446B9 /* CHIPDevice.mm */; };
2C222ADB255C74D100E446B9 /* CHIPDeviceStatusDelegateBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2C222AD8255C74D100E446B9 /* CHIPDeviceStatusDelegateBridge.mm */; };
Expand All @@ -23,8 +25,6 @@
2CB7163B252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 2CB71638252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.h */; };
2CB7163C252E8A7C0026E2BB /* CHIPDevicePairingDelegateBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2CB71639252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.mm */; };
2CB7163F252F731E0026E2BB /* CHIPDevicePairingDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 2CB7163E252F731E0026E2BB /* CHIPDevicePairingDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
515C401C2486BF43004C4DB3 /* CHIPOnOff.mm in Sources */ = {isa = PBXBuildFile; fileRef = 515C401A2486BF43004C4DB3 /* CHIPOnOff.mm */; };
515C401D2486BF43004C4DB3 /* CHIPOnOff.h in Headers */ = {isa = PBXBuildFile; fileRef = 515C401B2486BF43004C4DB3 /* CHIPOnOff.h */; settings = {ATTRIBUTES = (Public, ); }; };
991DC0842475F45400C13860 /* CHIPDeviceController.h in Headers */ = {isa = PBXBuildFile; fileRef = 991DC0822475F45400C13860 /* CHIPDeviceController.h */; settings = {ATTRIBUTES = (Public, ); }; };
991DC0892475F47D00C13860 /* CHIPDeviceController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 991DC0872475F47D00C13860 /* CHIPDeviceController.mm */; };
991DC08B247704DC00C13860 /* CHIPLogging.h in Headers */ = {isa = PBXBuildFile; fileRef = 991DC08A247704DC00C13860 /* CHIPLogging.h */; };
Expand Down Expand Up @@ -53,6 +53,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1EF8B75325782A9E009AA59C /* CHIPClustersObjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPClustersObjc.h; sourceTree = "<group>"; };
1EF8B75525782AAC009AA59C /* CHIPClustersObjc.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPClustersObjc.mm; sourceTree = "<group>"; };
2C222ACE255C620600E446B9 /* CHIPDevice.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPDevice.h; sourceTree = "<group>"; };
2C222ACF255C620600E446B9 /* CHIPDevice.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPDevice.mm; sourceTree = "<group>"; };
2C222AD8255C74D100E446B9 /* CHIPDeviceStatusDelegateBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPDeviceStatusDelegateBridge.mm; sourceTree = "<group>"; };
Expand All @@ -69,8 +71,6 @@
2CB71638252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPDevicePairingDelegateBridge.h; sourceTree = "<group>"; };
2CB71639252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPDevicePairingDelegateBridge.mm; sourceTree = "<group>"; };
2CB7163E252F731E0026E2BB /* CHIPDevicePairingDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPDevicePairingDelegate.h; sourceTree = "<group>"; };
515C401A2486BF43004C4DB3 /* CHIPOnOff.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPOnOff.mm; sourceTree = "<group>"; };
515C401B2486BF43004C4DB3 /* CHIPOnOff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPOnOff.h; sourceTree = "<group>"; };
991DC0822475F45400C13860 /* CHIPDeviceController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CHIPDeviceController.h; sourceTree = "<group>"; };
991DC0872475F47D00C13860 /* CHIPDeviceController.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPDeviceController.mm; sourceTree = "<group>"; };
991DC08A247704DC00C13860 /* CHIPLogging.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CHIPLogging.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -136,6 +136,8 @@
B202528F2459E34F00F97062 /* CHIP */ = {
isa = PBXGroup;
children = (
1EF8B75525782AAC009AA59C /* CHIPClustersObjc.mm */,
1EF8B75325782A9E009AA59C /* CHIPClustersObjc.h */,
2C222AEF25646BB100E446B9 /* CHIPCallbackBridge.h */,
2C222AEE25646BB100E446B9 /* CHIPCallbackBridge.mm */,
2C222AEC25634E5300E446B9 /* CHIPDeviceCallback.h */,
Expand Down Expand Up @@ -163,8 +165,6 @@
B2E0D7B0245B0B5C003C5B48 /* CHIPSetupPayload.mm */,
991DC0822475F45400C13860 /* CHIPDeviceController.h */,
991DC0872475F47D00C13860 /* CHIPDeviceController.mm */,
515C401B2486BF43004C4DB3 /* CHIPOnOff.h */,
515C401A2486BF43004C4DB3 /* CHIPOnOff.mm */,
B20252912459E34F00F97062 /* Info.plist */,
);
path = CHIP;
Expand Down Expand Up @@ -209,8 +209,8 @@
B2E0D7B5245B0B5C003C5B48 /* CHIPQRCodeSetupPayloadParser.h in Headers */,
2C222AF125646BB100E446B9 /* CHIPCallbackBridge.h in Headers */,
2C8C8FC0253E0C2100797F05 /* CHIPPersistentStorageDelegateBridge.h in Headers */,
1EF8B75425782A9E009AA59C /* CHIPClustersObjc.h in Headers */,
2C222ADF255C811800E446B9 /* CHIPDevice_Internal.h in Headers */,
515C401D2486BF43004C4DB3 /* CHIPOnOff.h in Headers */,
991DC08B247704DC00C13860 /* CHIPLogging.h in Headers */,
B2E0D7B4245B0B5C003C5B48 /* CHIPError.h in Headers */,
);
Expand Down Expand Up @@ -338,10 +338,10 @@
files = (
2C8C8FC2253E0C2100797F05 /* CHIPPersistentStorageDelegateBridge.mm in Sources */,
2CB7163C252E8A7C0026E2BB /* CHIPDevicePairingDelegateBridge.mm in Sources */,
515C401C2486BF43004C4DB3 /* CHIPOnOff.mm in Sources */,
2C222AD1255C620600E446B9 /* CHIPDevice.mm in Sources */,
991DC0892475F47D00C13860 /* CHIPDeviceController.mm in Sources */,
2C222AF025646BB100E446B9 /* CHIPCallbackBridge.mm in Sources */,
1EF8B75625782AAC009AA59C /* CHIPClustersObjc.mm in Sources */,
B2E0D7B7245B0B5C003C5B48 /* CHIPQRCodeSetupPayloadParser.mm in Sources */,
B2E0D7B3245B0B5C003C5B48 /* CHIPError.mm in Sources */,
2C222ADB255C74D100E446B9 /* CHIPDeviceStatusDelegateBridge.mm in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/CHIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#import <CHIP/CHIPDevicePairingDelegate.h>
#import <CHIP/CHIPError.h>
#import <CHIP/CHIPManualSetupPayloadParser.h>
#import <CHIP/CHIPOnOff.h>
#import <CHIP/CHIPClustersObjc.h>
#import <CHIP/CHIPPersistentStorageDelegate.h>
#import <CHIP/CHIPQRCodeSetupPayloadParser.h>
#import <CHIP/CHIPSetupPayload.h>
Expand Down
Loading

0 comments on commit 936dbbc

Please sign in to comment.