From 4603997528c5e46967f2dce569c90e482f769381 Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Sat, 28 Aug 2021 09:07:43 -0700 Subject: [PATCH] Get peer's node ID from session instead of packet header for response handling (#9287) * Get peer's node ID from session instead of packet header for response handling * Fix typo --- src/controller/CHIPDeviceController.cpp | 6 +- .../CHIP/templates/clusters-tests.zapt | 73 +++++++++++++++++++ .../Framework/CHIPTests/CHIPClustersTests.m | 73 +++++++++++++++++++ 3 files changed, 148 insertions(+), 4 deletions(-) diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index d7cac96dfb58d6..3843247f8971c7 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -476,11 +476,9 @@ CHIP_ERROR DeviceController::OnMessageReceived(Messaging::ExchangeContext * ec, uint16_t index; VerifyOrExit(mState == State::Initialized, ChipLogError(Controller, "OnMessageReceived was called in incorrect state")); + VerifyOrExit(ec != nullptr, ChipLogError(Controller, "OnMessageReceived was called with null exchange")); - VerifyOrExit(packetHeader.GetSourceNodeId().HasValue(), - ChipLogError(Controller, "OnMessageReceived was called for unknown source node")); - - index = FindDeviceIndex(packetHeader.GetSourceNodeId().Value()); + index = FindDeviceIndex(ec->GetSecureSession().GetPeerNodeId()); VerifyOrExit(index < kNumMaxActiveDevices, ChipLogError(Controller, "OnMessageReceived was called for unknown device object")); mActiveDevices[index].OnMessageReceived(ec, packetHeader, payloadHeader, std::move(msgBuf)); diff --git a/src/darwin/Framework/CHIP/templates/clusters-tests.zapt b/src/darwin/Framework/CHIP/templates/clusters-tests.zapt index c44bd0459c6adc..d7bbd8676dc7b3 100644 --- a/src/darwin/Framework/CHIP/templates/clusters-tests.zapt +++ b/src/darwin/Framework/CHIP/templates/clusters-tests.zapt @@ -134,6 +134,79 @@ CHIPDevice * GetPairedDevice(uint64_t deviceId) [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; } +- (void)testSendClusterTestCluster_Reporting_0000_BindOnOff_Test +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"Binding to OnOff Cluster complete"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPBinding * bindingCluster = [[CHIPBinding alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(bindingCluster); + [bindingCluster bind:kDeviceId groupId:0 + endpointId:1 + clusterId:6 + responseHandler:^(NSError * err, NSDictionary * values) { + NSLog(@"Reporting Test Binding status : %@", err); + XCTAssertEqual(err.code, 0); + [expectation fulfill]; + }]; + + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} + +- (void)testSendClusterTestCluster_Reporting_0001_ConfigureOnOff_Test +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"Reporting OnOff configured"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPOnOff * onOffCluster = [[CHIPOnOff alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(onOffCluster); + [onOffCluster configureAttributeOnOffWithMinInterval:0 + maxInterval:1 + responseHandler:^(NSError * err, NSDictionary * values) { + NSLog(@"Reporting Test Configure status: %@", err); + + XCTAssertEqual(err.code, 0); + [expectation fulfill]; + }]; + + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} + +- (void)testSendClusterTestCluster_Reporting_0002_ReportOnOff_Test +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"First report received"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPOnOff * onOffCluster = [[CHIPOnOff alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(onOffCluster); + [onOffCluster reportAttributeOnOffWithResponseHandler:^(NSError * err, NSDictionary * values) + { + NSLog(@"Reporting Test Report first report: %@", err); + [expectation fulfill]; + XCTAssertEqual(err.code, 0); + }]; + + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} + +- (void)testSendClusterTestCluster_Reporting_0003_StopReportOnOff_Test +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"Reporting OnOff cancelled"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPOnOff * onOffCluster = [[CHIPOnOff alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(onOffCluster); + [onOffCluster configureAttributeOnOffWithMinInterval:0 + maxInterval:0xffff + responseHandler:^(NSError * err, NSDictionary * values) { + NSLog(@"Reporting Test Cancel Reports status: %@", err); + + XCTAssertEqual(err.code, 0); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} + {{>test_cluster tests="TestCluster, Test_TC_OO_1_1, Test_TC_OO_2_1, Test_TC_OO_2_2, Test_TC_DM_1_1, Test_TC_DM_3_1, Test_TC_CC_3_4, Test_TC_CC_5, Test_TC_CC_6, Test_TC_CC_7, Test_TC_CC_8, Test_TC_WNCV_1_1, Test_TC_WNCV_2_1, Test_TC_BI_1_1, Test_TC_FLW_1_1, Test_TC_TM_1_1, Test_TC_OCC_1_1"}} {{#chip_client_clusters}} diff --git a/src/darwin/Framework/CHIPTests/CHIPClustersTests.m b/src/darwin/Framework/CHIPTests/CHIPClustersTests.m index 074aa95ac1e860..918e2c22db71b5 100644 --- a/src/darwin/Framework/CHIPTests/CHIPClustersTests.m +++ b/src/darwin/Framework/CHIPTests/CHIPClustersTests.m @@ -152,6 +152,79 @@ - (void)testReuseChipClusterObject [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; } +- (void)testSendClusterTestCluster_Reporting_0000_BindOnOff_Test +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"Binding to OnOff Cluster complete"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPBinding * bindingCluster = [[CHIPBinding alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(bindingCluster); + [bindingCluster bind:kDeviceId + groupId:0 + endpointId:1 + clusterId:6 + responseHandler:^(NSError * err, NSDictionary * values) { + NSLog(@"Reporting Test Binding status : %@", err); + XCTAssertEqual(err.code, 0); + [expectation fulfill]; + }]; + + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} + +- (void)testSendClusterTestCluster_Reporting_0001_ConfigureOnOff_Test +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"Reporting OnOff configured"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPOnOff * onOffCluster = [[CHIPOnOff alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(onOffCluster); + [onOffCluster configureAttributeOnOffWithMinInterval:0 + maxInterval:1 + responseHandler:^(NSError * err, NSDictionary * values) { + NSLog(@"Reporting Test Configure status: %@", err); + + XCTAssertEqual(err.code, 0); + [expectation fulfill]; + }]; + + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} + +- (void)testSendClusterTestCluster_Reporting_0002_ReportOnOff_Test +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"First report received"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPOnOff * onOffCluster = [[CHIPOnOff alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(onOffCluster); + [onOffCluster reportAttributeOnOffWithResponseHandler:^(NSError * err, NSDictionary * values) { + NSLog(@"Reporting Test Report first report: %@", err); + [expectation fulfill]; + XCTAssertEqual(err.code, 0); + }]; + + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} + +- (void)testSendClusterTestCluster_Reporting_0003_StopReportOnOff_Test +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"Reporting OnOff cancelled"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPOnOff * onOffCluster = [[CHIPOnOff alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(onOffCluster); + [onOffCluster configureAttributeOnOffWithMinInterval:0 + maxInterval:0xffff + responseHandler:^(NSError * err, NSDictionary * values) { + NSLog(@"Reporting Test Cancel Reports status: %@", err); + + XCTAssertEqual(err.code, 0); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} + - (void)testSendClusterTestCluster_000000_Test { XCTestExpectation * expectation = [self expectationWithDescription:@"Send Test Command"];