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

[IM] Decouple CommandSender with CHIPDevice #6802

Merged
merged 4 commits into from
May 17, 2021

Conversation

erjiaqing
Copy link
Contributor

@erjiaqing erjiaqing commented May 14, 2021

Problem

Current IM command interaction can send multiple command in one interaction, but device controller has limited this capabilit with one command, actually the ember needs to send multiple commands in one invoke command interaction in some user scenario,

Summary of Changes

  • After this PR, the CHIPDevice nolonger holds the commandSender object, for backward compatibility, the ClusterObjects will accept an optional CommandSender object, if not provided, it will request one from IMDelegate.

  • This also uses a pool of CommandSender with two objects (previous one), this will allow sending multiple commands at the same time.

  • This also fixes the broken chip-device-ctrl after Integrate commissioner and device clusters with operational credentials #6705 introduced OpCred provisioning.

Note: The API for sending multiple commands at the same time should be ready after this PR, it is used by OpCred provisioning routine now, will add Python api support for more commands later.

Fixes #6560
Also Fixes #6797, fixes #6803.

@erjiaqing
Copy link
Contributor Author

erjiaqing commented May 14, 2021

@cjandhyala Please verify if this PR fixes #6797, when testing against all-cluster-apps, thanks.

Copy link
Contributor

@woody-apple woody-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cecille
Copy link
Contributor

cecille commented May 14, 2021

Confirmed that this works with Pankaj's Op-cert change using chip-device-tool and BLE connection. Also works for the upcoming IP commissioning stuff.

Thanks - much cleaner than my previous hacks.

@yunhanw-google yunhanw-google force-pushed the im-multiple-commands branch from bdfe43f to 573a5ad Compare May 14, 2021 18:00

return mDevice->SendCommands();
return mDevice->SendCommands(mpCommandSender);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this isn't actually batching up multiple commands into a single InvokeInteraction right? This is still very much, 1 command PER invoke interaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when talking about "sending multiple commands", we need to

  1. Make it possible to have multiple transactions between controller and device.
  2. The transactions can carry more than one commands.

This PR is focus on the first one, and the second will be implemented in another PR.

Copy link
Contributor

@mrjerryjohns mrjerryjohns May 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest holding off on 2, since as part of the proposal/prototype I'm writing up, I'm combining CommandSender/CommandHandler/Command into a single 'InvokeInteraction' class that will handle:

  • Permitting multiple commands to be bundled in a single invoke
  • Allow for async responses to be sent
  • Permit multiple invoke interactions over a single exchange AND InvokeInteraction object instance, to reduce allocation/de-allocation transients.

That being said, I'm also not certain what the real use-case for 2) is right now, since everything I've seen so far would only necessitate single-command-in-invoke interactions. Is there an example of it that is needed in the upcoming TEs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block transfer for OTA comes to mind.

{
chip::app::CommandSender * commandSenderObj = nullptr;
VerifyOrReturnError(commandSender != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
ReturnErrorOnFailure(chip::app::InteractionModelEngine::GetInstance()->NewCommandSender(&commandSenderObj));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it even have to be allocated out of a pool? Why can't the allocation of the CommandSender be managed by the application here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still prefer to make im engine manage the command sender.
According to the encoding spec, command is a one time transaction, so the application can dispose the command sender once they finished the sending routine.

Another concern is that we should avoid the use of heap if possible and we still need to pass the reference (and maybe ownership) of command sender to im to avoid use after free issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still prefer to make im engine manage the command sender.

Haven't provided a reason.

According to the encoding spec, command is a one time transaction, so the application can dispose the command sender once they finished the sending routine.

Doesn't mean it has to be allocated by the IM...application can still dispose of the command if they own the allocation for it (in fact, it's even easier...).

Another concern is that we should avoid the use of heap if possible and we still need to pass the reference (and maybe ownership) of command sender to im to avoid use after free issue.

It doesn't have to allocate out of heap. It however, has more flexibility to decide between heap OR pool-based.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for now, I think we can hold-off on conversion to any kind of 'application-allocated' model for CommandSender, till we review the upcoming proposal I'll put out on InvokeInteraction as a concept. If we think that makes sense, we can then re-assess how exactly the allocation and ownership model should be for that.

@cjandhyala
Copy link
Contributor

@erjiaqing all-clusters-app on your branch also failed with 'Error occurred calling SyncSetKeyValue.' when connecting to the device

Device log

$ sudo ./out/host/chip-all-clusters-app --wifi
CHIP:DL: Device Configuration:
CHIP:DL: Serial Number: TEST_SN
CHIP:DL: Vendor Id: 9050 (0x235A)
CHIP:DL: Product Id: 65279 (0xFEFF)
CHIP:DL: Product Revision: 1
CHIP:DL: Setup Pin Code: 20202021
CHIP:DL: Setup Discriminator: 3840 (0xF00)
CHIP:DL: Manufacturing Date: (not set)
CHIP:DL: Device Type: 65535 (0xFFFF)
CHIP:SVR: SetupQRCode: [CH:YNJV793700A50324800]
CHIP:SVR: Copy/paste the below URL in a browser to see the QR Code:
CHIP:SVR: https://dhrishi.github.io/connectedhomeip/qrcode.html?data=CH%3AYNJV793700A50324800
CHIP:SVR: Manual pairing code: [34970112332]
CHIP:ZCL: Using ZAP configuration...
CHIP:ZCL: deactivate report event
CHIP:ZCL: 0xaaaaaf537018ep 1 clus 101 attr 3 not supported
CHIP:ZCL: Failed to write DoorState attribute: 0x86
CHIP:ZCL: 0xaaaaaf537018ep 1 clus 101 attr 21 not supported
CHIP:ZCL: Failed to write Language attribute: 0x86
CHIP:ZCL: OpCreds: Initiating OpCreds cluster by writing fabrics list from admin pairing table.
CHIP:DIS: Set the admin pairing table delegate
CHIP:ZCL: OpCreds: Call to writeAdminsIntoFabricsListAttribute
CHIP:ZCL: OpCreds: Stored 0 admins in fabrics list attribute.
CHIP:ZCL: Start identify callback on endpoint 1 time 0
CHIP:ZCL: Stop identify callback on endpoint 1
CHIP:ZCL: WARNING: ATTRIBUTES ARE NOT BEING STORED IN FLASH!
CHIP:ZCL: DEVICE WILL NOT FUNCTION PROPERLY AFTER REBOOTING!!
CHIP:ZCL: Failed to send IAS Zone update. Err 0x70
CHIP:DIS: Init admin pairing table with server storage.
CHIP:IN: TransportMgr initialized
CHIP:DL: writing settings to file (/tmp/chip_counters.ini.tmp)
CHIP:DL: renamed tmp file to file (/tmp/chip_counters.ini)
CHIP:DL: NVS set: chip-counters/GlobalMCTR = 4000 (0xFA0)
CHIP:IN: local node id is 0x0000000000bc5c01
CHIP:DL: wpa_supplicant: _IsWiFiStationProvisioned: interface not connected
CHIP:BLE: Assigned local session key ID 0
CHIP:BLE: Waiting for PBKDF param request
CHIP:DIS: Start dns-sd server
CHIP:DL: MDNS failed to join multicast group on wlan0 for address type IPv4: Inet Error 1016 (0x000003F8): Address not found
CHIP:DL: wpa_supplicant: connected to wpa_supplicant proxy
CHIP:DL: wpa_supplicant: WiFi interface: /fi/w1/wpa_supplicant1/Interfaces/0
CHIP:DL: MDNS failed to join multicast group on veth6216850 for address type IPv4: Inet Error 1016 (0x000003F8): Address not found
CHIP:DL: MDNS failed to join multicast group on vethc7bdc55 for address type IPv4: Inet Error 1016 (0x000003F8): Address not found
CHIP:DL: MDNS failed to join multicast group on veth4d36d6a for address type IPv4: Inet Error 1016 (0x000003F8): Address not found
CHIP:DL: MDNS failed to join multicast group on vetha36a6dc for address type IPv4: Inet Error 1016 (0x000003F8): Address not found
CHIP:DL: MDNS failed to join multicast group on vethbee108f for address type IPv4: Inet Error 1016 (0x000003F8): Address not found
CHIP:DIS: CHIP minimal mDNS started advertising.
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DL: wpa_supplicant: connected to wpa_supplicant interface proxy
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Replying to DNS-SD service listing request
CHIP:DIS: Using wifi MAC for hostname
CHIP:DL: rotatingDeviceId: 00007C5F6E176CD40F68685D100A1CF8A98B
CHIP:DIS: Advertise commission parameter vendorID=74586 productID=130815 discriminator=3840/00
CHIP:DIS: CHIP minimal mDNS configured as 'Commissionable node device'.
CHIP:SVR: Server Listening...
CHIP:DL: TRACE: Bluez mainloop starting Thread
CHIP:DL: TRACE: Bus acquired for name CHIP-3840
CHIP:DL: CREATE service object at /chipoble/8110/service
CHIP:DL: Create characteristic object at /chipoble/8110/service/c1
CHIP:DL: Create characteristic object at /chipoble/8110/service/c2
CHIP:DL: CHIP BTP C1 /chipoble/8110/service
CHIP:DL: CHIP BTP C2 /chipoble/8110/service
CHIP:DL: CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING is TRUE
CHIP:DL: Create characteristic object at /chipoble/8110/service/c3
CHIP:DL: rotatingDeviceId: 00007C5F6E176CD40F68685D100A1CF8A98B
CHIP:DL: CHIP BTP C3 /chipoble/8110/service
CHIP:DL: PlatformBlueZInit init success
CHIP:DL: BluezPeripheralRegisterAppDone done
CHIP:DL: HandlePlatformSpecificBLEEvent 16392
CHIP:DL: Create adv object at /chipoble/8110/advertising
CHIP:DL: SET service data to {'0xFFF6': <[byte 0x00, 0x00, 0x0f, 0x5a, 0x23, 0xff, 0xfe]>}
CHIP:DL: HandlePlatformSpecificBLEEvent 16393
CHIP:DL: CHIPoBLE advertising config complete
CHIP:DL: RegisterAdvertisement complete
CHIP:DL: HandlePlatformSpecificBLEEvent 16394
CHIP:DL: Device DC:A6:32:7E:5C:77 (Path: /org/bluez/hci0/dev_DC_A6_32_7E_5C_77) Connected
CHIP:DL: BluezCharacteristicAcquireWrite is called, conn: 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: local and remote recv window sizes = 3
CHIP:BLE: selected BTP version 3
CHIP:BLE: using BTP fragment sizes rx 128 / tx 20.
CHIP:DL: CHIPoBLE subscribe received
CHIP:IN: BLE EndPoint 0xaaaaaf56e2d8 Connection Complete
CHIP:DL: HandlePlatformSpecificBLEEvent 32777
CHIP:DL: Receive kCHIPoBLEConnectionEstablished
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:EM: Received message of type 32 and protocolId 0
CHIP:EM: ec pos: 0, id: 17767, Delegate: 0xaf571028
CHIP:BLE: Received PBKDF param request
CHIP:BLE: Sent PBKDF param response
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:EM: Received message of type 34 and protocolId 0
CHIP:BLE: Received spake2p msg1
CHIP:BLE: Peer assigned session key ID 0
CHIP:BLE: Sent spake2p msg2
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:EM: Received message of type 36 and protocolId 0
CHIP:BLE: Received spake2p msg3
CHIP:IN: New pairing for device 0x0000000000000000, key 0!!
CHIP:SVR: Device completed SPAKE2+ handshake
CHIP:SVR: Persisting admin ID 0, next available 1
CHIP:SVR: Stored value in server storage
CHIP:DIS: Error occurred calling SyncSetKeyValue.
CHIP:SVR: Device completed Rendezvous process
CHIP:SVR: Stored value in server storage
CHIP:SVR: Failed to store the connection state
CHIP:DL: UnregisterAdvertisement complete
CHIP:DL: HandlePlatformSpecificBLEEvent 16395
CHIP:DL: CHIPoBLE advertising stopped
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:IN: Secure transport received message destined to node ID (12344321)
CHIP:IN: Secure transport received message, but failed to decode it, discarding
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80
CHIP:BLE: Indication confirmation, 0xffff9c041b80
CHIP:DL: c1 BluezCharacteristicWriteFD mtu, 517
CHIP:BLE: Write request received debug 0xffff9c041b80

@erjiaqing erjiaqing force-pushed the im-multiple-commands branch from 573a5ad to 2905d94 Compare May 14, 2021 22:48
@todo
Copy link

todo bot commented May 14, 2021

Make number of command handler and command sender configurable

// TODO: Make number of command handler and command sender configurable
#define CHIP_MAX_NUM_COMMAND_HANDLER 2
#define CHIP_MAX_NUM_COMMAND_SENDER 2
#define CHIP_MAX_NUM_READ_CLIENT 1
#define CHIP_MAX_NUM_READ_HANDLER 1
#define CHIP_MAX_REPORTS_IN_FLIGHT 1


This comment was generated by todo based on a TODO comment in 2905d94 in #6802. cc @erjiaqing.

@erjiaqing
Copy link
Contributor Author

erjiaqing commented May 14, 2021

@erjiaqing all-clusters-app on your branch also failed with 'Error occurred calling SyncSetKeyValue.' when connecting to the device

Can you try cherry-pick #6840 on server side to see if this issue persists?

@yunhanw-google yunhanw-google added the hotfix urgent fix needed, can bypass review label May 15, 2021
@erjiaqing
Copy link
Contributor Author

erjiaqing commented May 15, 2021

Copy link
Contributor

@woody-apple woody-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holding based on comments from @mrjerryjohns and review from @pan-apple @vivien-apple

Copy link
Contributor

@mrjerryjohns mrjerryjohns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved for now to unblock the breakage, but this stuff will likely go through some churn later.

@msandstedt msandstedt self-requested a review May 15, 2021 15:55
@erjiaqing erjiaqing force-pushed the im-multiple-commands branch from 39f3346 to 16de55b Compare May 17, 2021 01:48
@erjiaqing erjiaqing force-pushed the im-multiple-commands branch from 16de55b to 3980654 Compare May 17, 2021 02:53
@erjiaqing
Copy link
Contributor Author

@vivien-apple By decouple CommandSender with CHIPDevice, the CHIPDevice no longer holds the CommandSender object, which allows sending multiple commands (having multiple command transactions) to the same device in parallel, the IMEngine will hold the ownership of CommandSedner and maintains its lifespan.

@pan-apple This PR also updated some API used in controller to send commands during pairing. Tested locally and @cecille reports this PR works.

@github-actions
Copy link

Size increase report for "nrfconnect-example-build" from 8a51de2

File Section File VM
chip-lighting.elf bss 0 915
chip-lighting.elf text 108 108
chip-lighting.elf device_handles 4 4
chip-lighting.elf [LOAD #3 [RW]] 0 -19
chip-lock.elf bss 0 919
chip-lock.elf text 108 108
chip-lock.elf device_handles 4 4
chip-lock.elf [LOAD #3 [RW]] 0 -23
Full report output
BLOAT REPORT

Files found only in the build output:
    report.csv

Comparing ./master_artifact/chip-shell.elf and ./pull_artifact/chip-shell.elf:

sections,vmsize,filesize

Comparing ./master_artifact/chip-lighting.elf and ./pull_artifact/chip-lighting.elf:

sections,vmsize,filesize
bss,915,0
.debug_line,0,268
text,108,108
.debug_ranges,0,64
.debug_abbrev,0,16
.debug_frame,0,4
device_handles,4,4
[LOAD #3 [RW]],-19,0
.debug_info,0,-20
.debug_loc,0,-476

Comparing ./master_artifact/chip-lock.elf and ./pull_artifact/chip-lock.elf:

sections,vmsize,filesize
bss,919,0
.debug_line,0,273
text,108,108
.debug_ranges,0,64
.debug_abbrev,0,16
.debug_frame,0,4
device_handles,4,4
.debug_info,0,-21
[LOAD #3 [RW]],-23,0
.debug_loc,0,-472


@github-actions
Copy link

Size increase report for "esp32-example-build" from 8a51de2

File Section File VM
chip-all-clusters-app.elf .dram0.bss 0 936
chip-all-clusters-app.elf .flash.text 80 80
Full report output
BLOAT REPORT

Files found only in the build output:
    report.csv

Comparing ./master_artifact/chip-all-clusters-app.elf and ./pull_artifact/chip-all-clusters-app.elf:

sections,vmsize,filesize
.dram0.bss,936,0
.debug_line,0,411
.flash.text,80,80
.debug_loc,0,66
.xt.prop._ZN4chip3app22InteractionModelEngineD2Ev,0,48
.debug_abbrev,0,40
.xt.prop._ZTVN4chip11DeviceLayer37DeviceNetworkProvisioningDelegateImplE,0,1
.debug_info,0,-114
.debug_ranges,0,-224

Comparing ./master_artifact/chip-pigweed-app.elf and ./pull_artifact/chip-pigweed-app.elf:

sections,vmsize,filesize


Copy link
Contributor

@vivien-apple vivien-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes looks fine to me. I would still like @pan-apple to look at the changes to fix the operational credentials things.

Also, in my understanding this only partially fix #6560 right ? One can now have one in-flight commands per cluster, but can not send multiple commands for the same cluster right ? This is still much better than the previous situation but I just want to make sure I did understand correctly what's going on.

@pan-apple pan-apple requested a review from woody-apple May 17, 2021 15:09
@andy31415
Copy link
Contributor

andy31415 commented May 17, 2021

@woody-apple - your "change requested" was on pending from Jerry, Vivien and Pankaj, all of which gave checkmarks here. Assuming your change requested is now "ok".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app controller darwin examples hotfix urgent fix needed, can bypass review
Projects
None yet