Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read/subscribe event function for darwin #24057

Merged
merged 33 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
befba5f
Add functions for darwin-framework-tool
seokhee-lee Dec 13, 2022
4c46a1a
Merge branch 'master' into master
seokhee-lee Dec 13, 2022
9a931de
Remove discovery and ethernet pairing changes
seokhee-lee Dec 16, 2022
076941d
Update PairingCommandBridge.mm
seokhee-lee Dec 16, 2022
0e1ddfa
Update ReportCommandBridge.h
seokhee-lee Dec 16, 2022
e1f7eae
Delete Commands.h
seokhee-lee Dec 19, 2022
f260d1d
Rollback Commands.h
seokhee-lee Dec 19, 2022
c2a5a17
Merge branch 'master' of github.com:seokhee-lee/connectedhomeip
seokhee-lee Dec 19, 2022
151808c
Update MTRDeviceController.h
seokhee-lee Dec 19, 2022
0ee5ea4
Update commands.zapt
seokhee-lee Dec 20, 2022
fecea8c
Add Commands.h to fix zap build error
seokhee-lee Dec 20, 2022
1e8ee98
Merge remote-tracking branch 'upstream/master'
seokhee-lee Dec 22, 2022
8b80dd2
Update Commands.h based on the latest version in upstream
seokhee-lee Dec 22, 2022
e3720fe
Remove fabric-filtered option in read-event-by-id
seokhee-lee Dec 22, 2022
2dc8cc5
Revert "Update Commands.h based on the latest version in upstream"
seokhee-lee Dec 22, 2022
22846b5
Restyled by whitespace
restyled-commits Dec 22, 2022
d812cbd
Restyled by clang-format
restyled-commits Dec 22, 2022
d7388b5
Restyle zzz_generated/darwin-framework-tool/zap-generated/cluster/Com…
seokhee-lee Dec 27, 2022
f3ba981
Update Commands.h to fix zap-related error
seokhee-lee Dec 27, 2022
923e8a8
Merge branch 'project-chip:master' into master
seokhee-lee Jan 8, 2023
9b7b82f
Added subscribeToEventsWithEndpointID
ready2die4u Jan 6, 2023
1233799
Restyled by clang-format
restyled-commits Jan 9, 2023
3112bc7
Added Read/SubscribeEvent to PowerSourceCluster
ready2die4u Jan 9, 2023
8e747b7
Merge branch 'project-chip:master' into master
ready2die4u Jan 12, 2023
0734574
Merge branch 'project-chip:master' into master
ready2die4u Jan 12, 2023
cf68435
Added BufferedReadClientCallback for both attribte and event
ready2die4u Jan 11, 2023
5586960
Modified to support 'any subscribe-event-by-id'
ready2die4u Jan 25, 2023
0d9b4ef
Restyled by clang-format
restyled-commits Jan 25, 2023
b66a9e1
Merge remote-tracking branch 'upstream/master'
ready2die4u Jan 25, 2023
8f5d5fa
Merge branch 'project-chip:master' into master
ready2die4u Jan 26, 2023
4957f01
Removed the hardcoded true on subscribeWithQeuue
ready2die4u Jan 26, 2023
345b0d9
Revert "Removed the casts on descriptions"
ready2die4u Jan 26, 2023
4857114
Restore the mIsUrgentEvent in subscribeWithQueue.
bzbarsky-apple Jan 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,24 @@ class SubscribeEvent : public ModelCommand {
public:
SubscribeEvent()
: ModelCommand("subscribe-all-events")
{
AddCommonArguments();
}

SubscribeEvent(chip::ClusterId clusterId, bool isClusterAny = false)
: ModelCommand("subscribe-event-by-id")
, mClusterId(clusterId)
{
if (isClusterAny == true) {
AddArgument("cluster-id", 0, UINT32_MAX, &mClusterId);
}
AddArgument("event-id", 0, UINT32_MAX, &mEventId);
AddArgument("event-min", 0, UINT64_MAX, &mEventNumber);
AddArgument("is-urgent", 0, 1, &mIsUrgent);
AddCommonArguments();
}

void AddCommonArguments()
{
AddArgument("min-interval", 0, UINT16_MAX, &mMinInterval);
AddArgument("max-interval", 0, UINT16_MAX, &mMaxInterval);
Expand All @@ -199,34 +217,61 @@ class SubscribeEvent : public ModelCommand {
dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.command", DISPATCH_QUEUE_SERIAL);

MTRSubscribeParams * params = [[MTRSubscribeParams alloc] initWithMinInterval:@(mMinInterval) maxInterval:@(mMaxInterval)];
if (mEventNumber.HasValue()) {
params.minimumEventNumber = [NSNumber numberWithUnsignedLongLong:mEventNumber.Value()];
}
if (mKeepSubscriptions.HasValue()) {
params.replaceExistingSubscriptions = !mKeepSubscriptions.Value();
}
if (mIsUrgent.HasValue()) {
params.reportEventsUrgently = mIsUrgent.Value();
}
if (mAutoResubscribe.HasValue()) {
params.resubscribeIfLost = mAutoResubscribe.Value();
}

[device subscribeWithQueue:callbackQueue
params:params
clusterStateCacheContainer:nil
attributeReportHandler:^(NSArray * value) {
SetCommandExitStatus(CHIP_NO_ERROR);
}
eventReportHandler:^(NSArray * value) {
for (id item in value) {
NSLog(@"Response Item: %@", [item description]);
if (strcmp(GetName(), "subscribe-event-by-id") == 0) {
[device subscribeToEventsWithEndpointID:(endpointId == chip::kInvalidEndpointId)
? nil
: [NSNumber numberWithUnsignedShort:endpointId]
clusterID:(mClusterId == chip::kInvalidClusterId) ? nil : [NSNumber numberWithUnsignedInteger:mClusterId]
eventID:(mEventId == chip::kInvalidEventId) ? nil : [NSNumber numberWithUnsignedInteger:mEventId]
params:params
queue:callbackQueue
reportHandler:^(NSArray<NSDictionary<NSString *, id> *> * _Nullable values, NSError * _Nullable error) {
if (values) {
for (id item in values) {
NSLog(@"Response Item: %@", [item description]);
}
}
SetCommandExitStatus(error);
}
SetCommandExitStatus(CHIP_NO_ERROR);
}
errorHandler:^(NSError * error) {
SetCommandExitStatus(error);
}
subscriptionEstablished:^() {
mSubscriptionEstablished = YES;
}
resubscriptionScheduled:^(NSError * error, NSNumber * resubscriptionDelay) {
NSLog(@"Subscription dropped with error %@. Resubscription in %@ms", error, resubscriptionDelay);
}];
subscriptionEstablished:^() {
mSubscriptionEstablished = YES;
}];
} else {
[device subscribeWithQueue:callbackQueue
params:params
clusterStateCacheContainer:nil
attributeReportHandler:^(NSArray * value) {
SetCommandExitStatus(CHIP_NO_ERROR);
}
eventReportHandler:^(NSArray * value) {
for (id item in value) {
NSLog(@"Response Item: %@", [item description]);
}
SetCommandExitStatus(CHIP_NO_ERROR);
}
errorHandler:^(NSError * error) {
SetCommandExitStatus(error);
}
subscriptionEstablished:^() {
mSubscriptionEstablished = YES;
}
resubscriptionScheduled:^(NSError * error, NSNumber * resubscriptionDelay) {
NSLog(@"Subscription dropped with error %@. Resubscription in %@ms", error, resubscriptionDelay);
}];
}

return CHIP_NO_ERROR;
}
Expand All @@ -237,7 +282,82 @@ class SubscribeEvent : public ModelCommand {
chip::Optional<bool> mKeepSubscriptions;
chip::Optional<bool> mAutoResubscribe;
chip::Optional<chip::EventNumber> mEventNumber;
chip::Optional<bool> mIsUrgent;
bool mSubscriptionEstablished = NO;
uint16_t mMinInterval;
uint16_t mMaxInterval;

void Shutdown() override
{
mSubscriptionEstablished = NO;
ModelCommand::Shutdown();
}

bool DeferInteractiveCleanup() override { return mSubscriptionEstablished; }

private:
chip::ClusterId mClusterId;
chip::EventId mEventId;
};

class ReadEvent : public ModelCommand {
public:
ReadEvent()
: ModelCommand("read-event-by-id")
{
AddArgument("cluster-id", 0, UINT32_MAX, &mClusterId);
AddArgument("event-id", 0, UINT32_MAX, &mEventId);
AddArgument("event-min", 0, UINT64_MAX, &mEventNumber);
ModelCommand::AddArguments();
}

ReadEvent(chip::ClusterId clusterId)
: ModelCommand("read-event-by-id")
, mClusterId(clusterId)
{
AddArgument("event-id", 0, UINT32_MAX, &mEventId);
AddArgument("event-min", 0, UINT64_MAX, &mEventNumber);
ModelCommand::AddArguments();
}

~ReadEvent() {}

CHIP_ERROR SendCommand(MTRBaseDevice * _Nonnull device, chip::EndpointId endpointId) override
{
dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.command", DISPATCH_QUEUE_SERIAL);
MTRReadParams * params = [[MTRReadParams alloc] init];
if (mFabricFiltered.HasValue()) {
params.filterByFabric = mFabricFiltered.Value();
}
if (mEventNumber.HasValue()) {
params.minimumEventNumber = [NSNumber numberWithUnsignedLongLong:mEventNumber.Value()];
}

[device
readEventsWithEndpointID:(endpointId == chip::kInvalidEndpointId) ? nil : [NSNumber numberWithUnsignedShort:endpointId]
clusterID:(mClusterId == chip::kInvalidClusterId) ? nil : [NSNumber numberWithUnsignedInteger:mClusterId]
eventID:(mEventId == chip::kInvalidEventId) ? nil : [NSNumber numberWithUnsignedInteger:mEventId]
params:params
queue:callbackQueue
completion:^(NSArray<NSDictionary<NSString *, id> *> * _Nullable values, NSError * _Nullable error) {
if (error != nil) {
LogNSError("Error reading event", error);
}
if (values) {
for (id item in values) {
NSLog(@"Response Item: %@", [item description]);
}
}
SetCommandExitStatus(error);
}];
return CHIP_NO_ERROR;
}

protected:
chip::Optional<bool> mFabricFiltered;
chip::Optional<chip::EventNumber> mEventNumber;

private:
chip::ClusterId mClusterId;
chip::AttributeId mEventId;
};
8 changes: 8 additions & 0 deletions examples/darwin-framework-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ void registerCluster{{asUpperCamelCase name}}(Commands & commands)
{{/if}}
{{/unless}}
{{/chip_server_cluster_attributes}}
{{#zcl_events}}
{{#first}}
make_unique<ReadEvent>(Id), //
make_unique<SubscribeEvent>(Id), //
{{/first}}
{{/zcl_events}}
};

commands.Register(clusterName, clusterCommands);
Expand All @@ -308,6 +314,8 @@ void registerClusterAny(Commands & commands)
make_unique<ReadAttribute>(), //
make_unique<WriteAttribute>(), //
make_unique<SubscribeAttribute>(), //
make_unique<ReadEvent>(), //
make_unique<SubscribeEvent>(chip::kInvalidClusterId, true), //
make_unique<SubscribeEvent>(), //
};

Expand Down
41 changes: 41 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,47 @@ typedef NS_ENUM(uint8_t, MTRTransportType) {
completion:(MTRDeviceOpenCommissioningWindowHandler)completion
API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2));

/**
* Reads events from the device.
*
* Nil values for endpointID, clusterID, eventID indicate wildcards
* (e.g. nil eventID means "read all the events from the endpoint(s) and
* cluster(s) that match endpointID/clusterID").
*
* If all of endpointID, clusterID, eventID are non-nil, all the matching instances of a single
* event will be read.
*
* If all of endpointID, clusterID, eventID are nil, all events on the
* device will be read.
*/

- (void)readEventsWithEndpointID:(NSNumber * _Nullable)endpointID
seokhee-lee marked this conversation as resolved.
Show resolved Hide resolved
clusterID:(NSNumber * _Nullable)clusterID
eventID:(NSNumber * _Nullable)eventID
params:(MTRReadParams * _Nullable)params
queue:(dispatch_queue_t)queue
completion:(MTRDeviceResponseHandler)completion MTR_NEWLY_AVAILABLE;

/**
* Subscribes to the specified events on the device.
*
* Nil values for endpointID, clusterID, eventID indicate wildcards
* (e.g. nil eventID means "subscribe to all the events from the
* endpoint(s) and cluster(s) that match endpointID/clusterID").
*
* If all of endpointID, clusterID, eventID are non-nil, a single event
* will be subscribed to.
*
* If all of endpointID, clusterID, eventID are nil, all events on the
* device will be subscribed to.
*/
- (void)subscribeToEventsWithEndpointID:(NSNumber * _Nullable)endpointID
clusterID:(NSNumber * _Nullable)clusterID
eventID:(NSNumber * _Nullable)eventID
params:(MTRSubscribeParams * _Nullable)params
queue:(dispatch_queue_t)queue
reportHandler:(MTRDeviceResponseHandler)reportHandler
subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptionEstablished MTR_NEWLY_AVAILABLE;
@end

/**
Expand Down
Loading