From 65347c95af58a53c3668a1e798ac4766979c4906 Mon Sep 17 00:00:00 2001 From: CodeChronos <1197535+CodeChronos928@users.noreply.github.com> Date: Tue, 14 Dec 2021 05:55:52 -0800 Subject: [PATCH] onboardingcodes now requires rendezvous type (#12963) The shell's "onboardingcodes" could only generate codes with the BLE rendezvous flag set. It was hardcoded. This commit adds a required first parameter to the "onboardingcodes" command: none|softap|ble|onnetwork. The help screen was also updated. The motivation for this change was so I could generate a correct QR code for IP commissioning. --- src/lib/shell/commands/OnboardingCodes.cpp | 88 +++++++++++++++++----- 1 file changed, 68 insertions(+), 20 deletions(-) diff --git a/src/lib/shell/commands/OnboardingCodes.cpp b/src/lib/shell/commands/OnboardingCodes.cpp index cac2325a1072a7..83978e502519dd 100644 --- a/src/lib/shell/commands/OnboardingCodes.cpp +++ b/src/lib/shell/commands/OnboardingCodes.cpp @@ -33,7 +33,7 @@ using chip::DeviceLayer::ConfigurationMgr; namespace chip { namespace Shell { -static CHIP_ERROR GetOnboardingQRCode(bool printHeader) +static CHIP_ERROR GetOnboardingQRCode(bool printHeader, chip::RendezvousInformationFlags aRendezvousFlags) { streamer_t * sout = streamer_get(); std::string QRCode; @@ -42,12 +42,12 @@ static CHIP_ERROR GetOnboardingQRCode(bool printHeader) { streamer_printf(sout, "QRCode: "); } - ReturnErrorOnFailure(GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE))); + ReturnErrorOnFailure(GetQRCode(QRCode, aRendezvousFlags)); streamer_printf(sout, "%s\r\n", QRCode.c_str()); return CHIP_NO_ERROR; } -static CHIP_ERROR GetOnboardingQRCodeUrl(bool printHeader) +static CHIP_ERROR GetOnboardingQRCodeUrl(bool printHeader, chip::RendezvousInformationFlags aRendezvousFlags) { streamer_t * sout = streamer_get(); std::string QRCode; @@ -56,7 +56,7 @@ static CHIP_ERROR GetOnboardingQRCodeUrl(bool printHeader) { streamer_printf(sout, "QRCodeUrl: "); } - ReturnErrorOnFailure(GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE))); + ReturnErrorOnFailure(GetQRCode(QRCode, aRendezvousFlags)); char qrCodeBuffer[CHIP_SHELL_MAX_BUFFER_SIZE]; @@ -65,7 +65,7 @@ static CHIP_ERROR GetOnboardingQRCodeUrl(bool printHeader) return CHIP_NO_ERROR; } -static CHIP_ERROR GetOnboardingManualPairingCode(bool printHeader) +static CHIP_ERROR GetOnboardingManualPairingCode(bool printHeader, chip::RendezvousInformationFlags aRendezvousFlags) { streamer_t * sout = streamer_get(); std::string manualPairingCode; @@ -74,39 +74,85 @@ static CHIP_ERROR GetOnboardingManualPairingCode(bool printHeader) { streamer_printf(sout, "ManualPairingCode: "); } - ReturnErrorOnFailure( - GetManualPairingCode(manualPairingCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE))); + ReturnErrorOnFailure(GetManualPairingCode(manualPairingCode, aRendezvousFlags)); streamer_printf(sout, "%s\r\n", manualPairingCode.c_str()); return CHIP_NO_ERROR; } -static CHIP_ERROR PrintAllOnboardingCodes() +static CHIP_ERROR PrintAllOnboardingCodes(chip::RendezvousInformationFlags aRendezvousFlags) { - ReturnErrorOnFailure(GetOnboardingQRCode(true)); - ReturnErrorOnFailure(GetOnboardingQRCodeUrl(true)); - ReturnErrorOnFailure(GetOnboardingManualPairingCode(true)); + ReturnErrorOnFailure(GetOnboardingQRCode(true, aRendezvousFlags)); + ReturnErrorOnFailure(GetOnboardingQRCodeUrl(true, aRendezvousFlags)); + ReturnErrorOnFailure(GetOnboardingManualPairingCode(true, aRendezvousFlags)); return CHIP_NO_ERROR; } +static CHIP_ERROR RendezvousStringToFlag(char * str, chip::RendezvousInformationFlags * aRendezvousFlags) +{ + if (str == nullptr) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + if (aRendezvousFlags == nullptr) + { + return CHIP_NO_ERROR; + } + + if (strcmp(str, "none") == 0) + { + *aRendezvousFlags = chip::RendezvousInformationFlag::kNone; + return CHIP_NO_ERROR; + } + else if (strcmp(str, "softap") == 0) + { + *aRendezvousFlags = chip::RendezvousInformationFlag::kSoftAP; + return CHIP_NO_ERROR; + } + else if (strcmp(str, "ble") == 0) + { + *aRendezvousFlags = chip::RendezvousInformationFlag::kBLE; + return CHIP_NO_ERROR; + } + else if (strcmp(str, "onnetwork") == 0) + { + *aRendezvousFlags = chip::RendezvousInformationFlag::kOnNetwork; + return CHIP_NO_ERROR; + } + return CHIP_ERROR_INVALID_ARGUMENT; +} + static CHIP_ERROR OnboardingHandler(int argc, char ** argv) { + chip::RendezvousInformationFlags aRendezvousFlags; + if (argc == 0) { - return PrintAllOnboardingCodes(); + return CHIP_ERROR_INVALID_ARGUMENT; + } + + CHIP_ERROR err = RendezvousStringToFlag(argv[0], &aRendezvousFlags); + if (err != CHIP_NO_ERROR) + { + return err; + } + + if (argc == 1) + { + return PrintAllOnboardingCodes(aRendezvousFlags); } - if (strcmp(argv[0], "qrcode") == 0) + if (strcmp(argv[1], "qrcode") == 0) { - return GetOnboardingQRCode(false); + return GetOnboardingQRCode(false, aRendezvousFlags); } - else if (strcmp(argv[0], "qrcodeurl") == 0) + else if (strcmp(argv[1], "qrcodeurl") == 0) { - return GetOnboardingQRCodeUrl(false); + return GetOnboardingQRCodeUrl(false, aRendezvousFlags); } - else if (strcmp(argv[0], "manualpairingcode") == 0) + else if (strcmp(argv[1], "manualpairingcode") == 0) { - return GetOnboardingManualPairingCode(false); + return GetOnboardingManualPairingCode(false, aRendezvousFlags); } else { @@ -117,8 +163,10 @@ static CHIP_ERROR OnboardingHandler(int argc, char ** argv) void RegisterOnboardingCodesCommands() { - static const shell_command_t sDeviceComand = { &OnboardingHandler, "onboardingcodes", - "Dump device onboarding codes. Usage: onboardingcodes [param_name]" }; + static const shell_command_t sDeviceComand = { + &OnboardingHandler, "onboardingcodes", + "Dump device onboarding codes. Usage: onboardingcodes none|softap|ble|onnetwork [qrcode|qrcodeurl|manualpairingcode]" + }; // Register the root `device` command with the top-level shell. Engine::Root().RegisterCommands(&sDeviceComand, 1);