Skip to content

Commit

Permalink
onboardingcodes now requires rendezvous type (#12963)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CodeChronos928 authored Dec 14, 2021
1 parent 726785b commit 65347c9
Showing 1 changed file with 68 additions and 20 deletions.
88 changes: 68 additions & 20 deletions src/lib/shell/commands/OnboardingCodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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];

Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -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);
Expand Down

0 comments on commit 65347c9

Please sign in to comment.