Skip to content

Commit

Permalink
Switch iOS CHIPTool to using the new On/Off implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Jul 17, 2020
1 parent 9c79b6c commit b83d489
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ typedef void (^ControllerOnErrorBlock)(NSError * error);
error:(NSError * __autoreleasing *)error;
- (nullable AddressInfo *)getAddressInfo;
- (BOOL)sendMessage:(NSData *)message error:(NSError * __autoreleasing *)error;
// We can't include definitions of ChipZclClusterId_t and ChipZclCommandId_t
// here, but they're just integers, so pass them that way.
- (BOOL)sendCHIPCommand:(uint16_t)cluster command:(uint16_t)command;
- (BOOL)sendOnCommand;
- (BOOL)sendOffCommand;
- (BOOL)sendToggleCommand;
- (BOOL)disconnect:(NSError * __autoreleasing *)error;
- (BOOL)isConnected;

Expand Down
60 changes: 28 additions & 32 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@

#import <Foundation/Foundation.h>

extern "C" {
#include "chip-zcl/chip-zcl-buffer.h"
#include "chip-zcl/chip-zcl.h"
#include "gen/gen-command-id.h"
} // extern "C"
#include "chip-zcl/chip-zcl-zpro-codec.h"

#import "CHIPDeviceController.h"
#import "CHIPError.h"
Expand Down Expand Up @@ -261,32 +257,16 @@ - (BOOL)sendMessage:(NSData *)message error:(NSError * __autoreleasing *)error
return YES;
}

- (BOOL)sendCHIPCommand:(ChipZclClusterId_t)cluster command:(ChipZclCommandId_t)command
- (BOOL)sendCHIPCommand:(uint32_t (^)(chip::System::PacketBuffer *, uint16_t))encodeCommandBlock
{
CHIP_ERROR err = CHIP_NO_ERROR;
[self.lock lock];
// FIXME: This needs a better buffersizing setup!
static const size_t bufferSize = 1024;
chip::System::PacketBuffer * buffer = chip::System::PacketBuffer::NewWithAvailableSize(bufferSize);

ChipZclBuffer_t * zcl_buffer = (ChipZclBuffer_t *) buffer;
ChipZclCommandContext_t ctx = {
1, // endpointId
cluster, // clusterId
true, // clusterSpecific
false, // mfgSpecific
0, // mfgCode
command, // commandId
ZCL_DIRECTION_CLIENT_TO_SERVER, // direction
0, // payloadStartIndex
nullptr, // request
nullptr // response
};
chipZclEncodeZclHeader(zcl_buffer, &ctx);

const size_t data_len = chipZclBufferDataLength(zcl_buffer);

buffer->SetDataLength(data_len);
uint32_t dataLength = encodeCommandBlock(buffer, (uint16_t) bufferSize);
buffer->SetDataLength(dataLength);

err = self.cppController->SendMessage((__bridge void *) self, buffer);
[self.lock unlock];
Expand All @@ -297,6 +277,30 @@ - (BOOL)sendCHIPCommand:(ChipZclClusterId_t)cluster command:(ChipZclCommandId_t)
return YES;
}

- (BOOL)sendOnCommand
{
return [self sendCHIPCommand:^(chip::System::PacketBuffer * buffer, uint16_t bufferSize) {
// Hardcode endpoint to 1 for now
return encodeOnCommand(buffer->Start(), bufferSize, 1);
}];
}

- (BOOL)sendOffCommand
{
return [self sendCHIPCommand:^(chip::System::PacketBuffer * buffer, uint16_t bufferSize) {
// Hardcode endpoint to 1 for now
return encodeOffCommand(buffer->Start(), bufferSize, 1);
}];
}

- (BOOL)sendToggleCommand
{
return [self sendCHIPCommand:^(chip::System::PacketBuffer * buffer, uint16_t bufferSize) {
// Hardcode endpoint to 1 for now
return encodeToggleCommand(buffer->Start(), bufferSize, 1);
}];
}

- (BOOL)disconnect:(NSError * __autoreleasing *)error
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -360,11 +364,3 @@ - (void)registerCallbacks:appCallbackQueue onMessage:(ControllerOnMessageBlock)o
}

@end

extern "C" {
// We have to have this empty callback, because the ZCL code links against it.
void chipZclPostAttributeChangeCallback(uint8_t endpoint, ChipZclClusterId clusterId, ChipZclAttributeId attributeId, uint8_t mask,
uint16_t manufacturerCode, uint8_t type, uint8_t size, uint8_t * value)
{
}
} // extern "C"
12 changes: 3 additions & 9 deletions src/darwin/Framework/CHIP/CHIPOnOff.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@

#import "CHIPOnOff.h"

extern "C" {
#include "chip-zcl/chip-zcl.h"
#include "gen/gen-cluster-id.h"
#include "gen/gen-types.h"
}

@interface CHIPOnOff ()

@property (readonly) CHIPDeviceController * deviceController;
Expand All @@ -43,17 +37,17 @@ - (instancetype)initWithDeviceController:(CHIPDeviceController *)deviceControlle

- (BOOL)lightOn
{
return [self.deviceController sendCHIPCommand:CHIP_ZCL_CLUSTER_ON_OFF command:CHIP_ZCL_CLUSTER_ON_OFF_SERVER_COMMAND_ON];
return [self.deviceController sendOnCommand];
}

- (BOOL)lightOff
{
return [self.deviceController sendCHIPCommand:CHIP_ZCL_CLUSTER_ON_OFF command:CHIP_ZCL_CLUSTER_ON_OFF_SERVER_COMMAND_OFF];
return [self.deviceController sendOffCommand];
}

- (BOOL)toggleLight
{
return [self.deviceController sendCHIPCommand:CHIP_ZCL_CLUSTER_ON_OFF command:CHIP_ZCL_CLUSTER_ON_OFF_SERVER_COMMAND_TOGGLE];
return [self.deviceController sendToggleCommand];
}

@end

0 comments on commit b83d489

Please sign in to comment.