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

[Ameba] Use common API to print QR code URL #14234

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
128 changes: 2 additions & 126 deletions examples/all-clusters-app/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ Identify gIdentify1 = {
EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_VISIBLE_LED,
};

#define QRCODE_BASE_URL "https://dhrishi.github.io/connectedhomeip/qrcode.html"
#define EXAMPLE_VENDOR_TAG_IP 1

#ifdef CONFIG_PLATFORM_8721D
#define STATUS_LED_GPIO_NUM PB_5
#elif defined(CONFIG_PLATFORM_8710C)
Expand All @@ -99,119 +96,6 @@ BDXDownloader gDownloader;
AmebaOTAImageProcessor gImageProcessor;
#endif

void GetGatewayIP(char * ip_buf, size_t ip_len)
{
uint8_t * gateway = LwIP_GetGW(&xnetif[0]);
sprintf(ip_buf, "%d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]);
ChipLogProgress(DeviceLayer, "Got gateway ip: %s\r\n", ip_buf);
}

// need to check CONFIG_RENDEZVOUS_MODE
bool isRendezvousBLE()
{
RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE);
return flags.Has(RendezvousInformationFlag::kBLE);
}

std::string createSetupPayload()
{
CHIP_ERROR err = CHIP_NO_ERROR;
std::string result;

uint16_t discriminator;
err = ConfigurationMgr().GetSetupDiscriminator(discriminator);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Couldn't get discriminator: %s\r\n", ErrorStr(err));
return result;
}
ChipLogProgress(DeviceLayer, "Setup discriminator: %d (0x%x)\r\n", discriminator, discriminator);

uint32_t setupPINCode;
err = ConfigurationMgr().GetSetupPinCode(setupPINCode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Couldn't get setupPINCode: %s\r\n", ErrorStr(err));
return result;
}
ChipLogProgress(DeviceLayer, "Setup PIN code: %lu (0x%lx)\r\n", setupPINCode, setupPINCode);

uint16_t vendorId;
err = ConfigurationMgr().GetVendorId(vendorId);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Couldn't get vendorId: %s\r\n", ErrorStr(err));
return result;
}

uint16_t productId;
err = ConfigurationMgr().GetProductId(productId);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Couldn't get productId: %s\r\n", ErrorStr(err));
return result;
}

SetupPayload payload;
payload.version = 0;
payload.discriminator = discriminator;
payload.setUpPINCode = setupPINCode;
payload.rendezvousInformation = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE);
payload.vendorID = vendorId;
payload.productID = productId;

if (!isRendezvousBLE())
{
char gw_ip[INET6_ADDRSTRLEN];
GetGatewayIP(gw_ip, sizeof(gw_ip));
payload.addOptionalVendorData(EXAMPLE_VENDOR_TAG_IP, gw_ip);

QRCodeSetupPayloadGenerator generator(payload);

size_t tlvDataLen = sizeof(gw_ip);
uint8_t tlvDataStart[tlvDataLen];
err = generator.payloadBase38Representation(result, tlvDataStart, tlvDataLen);
}
else
{
QRCodeSetupPayloadGenerator generator(payload);
err = generator.payloadBase38Representation(result);
}

{
ManualSetupPayloadGenerator generator(payload);
std::string outCode;

if (generator.payloadDecimalStringRepresentation(outCode) == CHIP_NO_ERROR)
{
ChipLogProgress(DeviceLayer, "Short Manual(decimal) setup code: %s\r\n", outCode.c_str());
}
else
{
ChipLogError(DeviceLayer, "Failed to get decimal setup code\r\n");
}

payload.commissioningFlow = CommissioningFlow::kCustom;
generator = ManualSetupPayloadGenerator(payload);

if (generator.payloadDecimalStringRepresentation(outCode) == CHIP_NO_ERROR)
{
// intentional extra space here to align the log with the short code
ChipLogProgress(DeviceLayer, "Long Manual(decimal) setup code: %s\r\n", outCode.c_str());
}
else
{
ChipLogError(DeviceLayer, "Failed to get decimal setup code\r\n");
}
}

if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Couldn't get payload string %lu\r\n" CHIP_ERROR_FORMAT, err.Format());
}
return result;
};

#if CONFIG_ENABLE_OTA_REQUESTOR
extern "C" void amebaQueryImageCmdHandler(uint32_t nodeId, uint32_t fabricId)
{
Expand Down Expand Up @@ -284,16 +168,8 @@ extern "C" void ChipTest(void)

if (RTW_SUCCESS != wifi_is_connected_to_ap())
{
std::string qrCodeText = createSetupPayload();
ChipLogProgress(DeviceLayer, "QR CODE Text: '%s'\r\n", qrCodeText.c_str());

std::vector<char> qrCode(3 * qrCodeText.size() + 1);
err = EncodeQRCodeToUrl(qrCodeText.c_str(), qrCodeText.size(), qrCode.data(), qrCode.max_size());
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(DeviceLayer, "Copy/paste the below URL in a browser to see the QR CODE:\n\t%s?data=%s \r\n",
QRCODE_BASE_URL, qrCode.data());
}
// QR code will be used with CHIP Tool
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
}

statusLED1.Init(STATUS_LED_GPIO_NUM);
Expand Down
131 changes: 2 additions & 129 deletions examples/lighting-app/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;
using namespace ::chip::System;

#define QRCODE_BASE_URL "https://dhrishi.github.io/connectedhomeip/qrcode.html"
#define EXAMPLE_VENDOR_TAG_IP 1

#ifdef CONFIG_PLATFORM_8721D
#define STATUS_LED_GPIO_NUM PB_5
#elif defined(CONFIG_PLATFORM_8710C)
Expand All @@ -71,119 +68,6 @@ BDXDownloader gDownloader;
AmebaOTAImageProcessor gImageProcessor;
#endif

void GetGatewayIP(char * ip_buf, size_t ip_len)
{
uint8_t * gateway = LwIP_GetGW(&xnetif[0]);
sprintf(ip_buf, "%d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]);
printf("Got gateway ip: %s\r\n", ip_buf);
}

// need to check CONFIG_RENDEZVOUS_MODE
bool isRendezvousBLE()
{
RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE);
return flags.Has(RendezvousInformationFlag::kBLE);
}

std::string createSetupPayload()
{
CHIP_ERROR err = CHIP_NO_ERROR;
std::string result;

uint16_t discriminator;
err = ConfigurationMgr().GetSetupDiscriminator(discriminator);
if (err != CHIP_NO_ERROR)
{
printf("Couldn't get discriminator: %s\r\n", ErrorStr(err));
return result;
}
printf("Setup discriminator: %u (0x%x)\r\n", discriminator, discriminator);

uint32_t setupPINCode;
err = ConfigurationMgr().GetSetupPinCode(setupPINCode);
if (err != CHIP_NO_ERROR)
{
printf("Couldn't get setupPINCode: %s\r\n", ErrorStr(err));
return result;
}
printf("Setup PIN code: %u (0x%x)\r\n", setupPINCode, setupPINCode);

uint16_t vendorId;
err = ConfigurationMgr().GetVendorId(vendorId);
if (err != CHIP_NO_ERROR)
{
printf("Couldn't get vendorId: %s\r\n", ErrorStr(err));
return result;
}

uint16_t productId;
err = ConfigurationMgr().GetProductId(productId);
if (err != CHIP_NO_ERROR)
{
printf("Couldn't get productId: %s\r\n", ErrorStr(err));
return result;
}

SetupPayload payload;
payload.version = 0;
payload.discriminator = discriminator;
payload.setUpPINCode = setupPINCode;
payload.rendezvousInformation = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE);
payload.vendorID = vendorId;
payload.productID = productId;

if (!isRendezvousBLE())
{
char gw_ip[INET6_ADDRSTRLEN];
GetGatewayIP(gw_ip, sizeof(gw_ip));
payload.addOptionalVendorData(EXAMPLE_VENDOR_TAG_IP, gw_ip);

QRCodeSetupPayloadGenerator generator(payload);

size_t tlvDataLen = sizeof(gw_ip);
uint8_t tlvDataStart[tlvDataLen];
err = generator.payloadBase38Representation(result, tlvDataStart, tlvDataLen);
}
else
{
QRCodeSetupPayloadGenerator generator(payload);
err = generator.payloadBase38Representation(result);
}

{
ManualSetupPayloadGenerator generator(payload);
std::string outCode;

if (generator.payloadDecimalStringRepresentation(outCode) == CHIP_NO_ERROR)
{
printf("Short Manual(decimal) setup code: %s\r\n", outCode.c_str());
}
else
{
printf("Failed to get decimal setup code\r\n");
}

payload.commissioningFlow = CommissioningFlow::kCustom;
generator = ManualSetupPayloadGenerator(payload);

if (generator.payloadDecimalStringRepresentation(outCode) == CHIP_NO_ERROR)
{
// intentional extra space here to align the log with the short code
printf("Long Manual(decimal) setup code: %s\r\n", outCode.c_str());
}
else
{
printf("Failed to get decimal setup code\r\n");
}
}

if (err != CHIP_NO_ERROR)
{
printf("Couldn't get payload string %\r\n" CHIP_ERROR_FORMAT, err.Format());
}
return result;
};

#if CONFIG_ENABLE_OTA_REQUESTOR
extern "C" void amebaQueryImageCmdHandler(uint32_t nodeId, uint32_t fabricId)
{
Expand Down Expand Up @@ -286,19 +170,8 @@ extern "C" void ChipTest(void)
// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());

std::string qrCodeText = createSetupPayload();

printf("QR CODE Text: '%s'\r\n", qrCodeText.c_str());

{
std::vector<char> qrCode(3 * qrCodeText.size() + 1);
err = EncodeQRCodeToUrl(qrCodeText.c_str(), qrCodeText.size(), qrCode.data(), qrCode.max_size());
if (err == CHIP_NO_ERROR)
{
printf("Copy/paste the below URL in a browser to see the QR CODE:\n\t%s?data=%s", QRCODE_BASE_URL, qrCode.data());
}
}
printf("\n\n");
// QR code will be used with CHIP Tool
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));

statusLED1.Init(STATUS_LED_GPIO_NUM);

Expand Down