Skip to content

Commit

Permalink
Add qrcode url for lighting-app
Browse files Browse the repository at this point in the history
  • Loading branch information
pankore committed Oct 21, 2021
1 parent 0e6e3a7 commit e76b0b6
Showing 1 changed file with 136 additions and 3 deletions.
139 changes: 136 additions & 3 deletions examples/lighting-app/ambd/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@
#include "chip_porting.h"

#include <platform/AMBD/AMBDConfig.h>
#include <app/server/OnboardingCodesUtil.h>
#include <lib/support/ErrorStr.h>
#include <setup_payload/ManualSetupPayloadGenerator.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>

#include <lwip_netconf.h>

extern "C"{ void * __dso_handle = 0 ;}

using namespace ::chip;
using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;

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

namespace
{
void SetupPretendDevices(void)
Expand Down Expand Up @@ -47,6 +56,118 @@ namespace

static DeviceCallbacks EchoCallbacks;

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;
};
extern "C" void ChipTest(void)
{
printf("In ChipTest()\r\n");
Expand All @@ -56,7 +177,6 @@ extern "C" void ChipTest(void)
initPref();

CHIPDeviceManager &deviceMgr = CHIPDeviceManager::GetInstance();

err = deviceMgr.Init(&EchoCallbacks);

if (err != CHIP_NO_ERROR)
Expand All @@ -68,11 +188,24 @@ extern "C" void ChipTest(void)
printf("DeviceManagerInit() - OK\r\n");
}

SetupPretendDevices();

AppCallbacks callbacks;
chip::Server::GetInstance().Init(&callbacks);

SetupPretendDevices();

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");

statusLED1.Init(STATUS_LED_GPIO_NUM);

while(true)
Expand Down

0 comments on commit e76b0b6

Please sign in to comment.