project-chip / connectedhomeip

Matter (formerly Project CHIP) creates more connections between more objects, simplifying development for manufacturers and increasing compatibility for consumers, guided by the Connectivity Standards Alliance.
https://buildwithmatter.com
Apache License 2.0
7.56k stars 2.04k forks source link

[BUG] Unable to commission the device #34317

Open QuentinCaldeira-eaton opened 4 months ago

QuentinCaldeira-eaton commented 4 months ago

Reproduction steps

Use the zephyr standalone platform and write a simple application for an ethernet device. I attached my application. I don't know if I have to OpenBasicCommissioningWindow(). I put a static ipv6 adress, it might be working because I tested another application which send a message through UDP and I see it on Wireshark. I have left in comment some functions I'm not sure about. I don't have any Ethernet Driver. I don't need one because zephyr already implement that I think. I don't know if I miss some sort of FLAGS, or functions in the sample. Any clue would be nice ! Thanks :) In order to try commissioning, I do :

./chip-tool pairing onnetwork 42 20202021
OR
./chip-tool pairing code 42 MT:Y.K90AFN00KA0648G00

Bug prevalence

Whenever I do this

GitHub hash of the SDK that was being used

latest

Platform

other

Platform Version(s)

No response

Anything else?

/* App include */
#include "AppTask.h"
#include "AppEvent.h"
//TODO #include "FabricTableDelegate.h"

/* Zephyr includes */
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/linker/sections.h>
#include <errno.h>
#include <stdio.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_core.h>
#include <zephyr/net/net_context.h>
#include <zephyr/net/net_mgmt.h>

/*Matter includes*/
#include <platform/Zephyr/DeviceInstanceInfoProviderImpl.h>
#include <platform/DeviceInfoProvider.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/clusters/ota-requestor/OTATestEventTriggerHandler.h>
#include <app/server/Dnssd.h>
#include <app/server/Server.h>
#include <app/util/attribute-storage.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/NetworkCommissioning.h>
#include <lib/core/ErrorStr.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <system/SystemClock.h>
#include <app/clusters/network-commissioning/network-commissioning.h> //TODO
#include <app/TestEventTriggerDelegate.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/clusters/ota-requestor/OTATestEventTriggerHandler.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <app/util/attribute-storage.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>

constexpr int kAppEventQueueSize               = 10;

LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);

K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent));

#define MY_STATIC_IPV6_ADDR "2001:db8::1"

struct sockaddr_in6 my_addr;
struct net_if *iface;
int sock;
int ret;

using namespace ::chip;
using namespace ::chip::app;
using namespace ::chip::TLV;
using namespace ::chip::Credentials;
using namespace ::chip::DeviceLayer;
using namespace ::chip::Platform;
using namespace ::chip::Credentials;
using namespace ::chip::app::Clusters;

uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                                                                                   0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
bool isConnected = false;

constexpr EndpointId kNetworkCommissioningEndpointMain = 0;
//Clusters::NetworkCommissioning::NullNetworkDriver sEthernetDriver;
//Clusters::NetworkCommissioning::Instance sEthernetNetworkCommissioningInstance(kNetworkCommissioningEndpointMain, &sEthernetDriver);

CHIP_ERROR AppTask::Init()
{
    // Initialize CHIP stack
    LOG_INF("Init CHIP stack");

    CHIP_ERROR err = chip::Platform::MemoryInit();
    if (err != CHIP_NO_ERROR)
    {
        LOG_ERR("Platform::MemoryInit() failed");
        return err;
    }

    // TODO >
    iface = net_if_get_default();
    if (!iface) {
        printk("No default network interface found.\n");
    }
    ret = net_addr_pton(AF_INET6, MY_STATIC_IPV6_ADDR, &my_addr.sin6_addr);
    if (ret < 0) {
        printk("Invalid IPv6 address.\n");
    }

    net_if_ipv6_addr_add(iface, &my_addr.sin6_addr, NET_ADDR_MANUAL, 0);
    // TODO <

    //sEthernetNetworkCommissioningInstance.Init();

    err = PlatformMgr().InitChipStack();
    if (err != CHIP_NO_ERROR)
    {
        LOG_ERR("PlatformMgr().InitChipStack() failed");
        return err;
    }

    // Initialize device attestation config
    SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());

    // Init ZCL Data Model and start server
    static chip::CommonCaseDeviceServerInitParams initParams;
    static SimpleTestEventTriggerDelegate sTestEventTriggerDelegate{};
    VerifyOrDie(sTestEventTriggerDelegate.Init(ByteSpan(sTestEventTriggerEnableKey)) == CHIP_NO_ERROR);
    (void) initParams.InitializeStaticResourcesBeforeServerInit();
    initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
    ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));

    AppEvent aEvent;
    ConfigurationMgr().LogDeviceConfig();
    PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kOnNetwork));
    // Add CHIP event handler and start CHIP thread.
    // Note that all the initialization code should happen prior to this point to avoid data races
    // between the main and the CHIP threads.

    PlatformMgr().AddEventHandler(ChipEventHandler, 0);
    //Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
    err = PlatformMgr().StartEventLoopTask();
    if (err != CHIP_NO_ERROR)
    {
        LOG_ERR("PlatformMgr().StartEventLoopTask() failed");
    }

    return CHIP_NO_ERROR;
}

CHIP_ERROR AppTask::StartApp()
{
    ReturnErrorOnFailure(Init());

    AppEvent event = {};

    while (true)
    {
        k_msgq_get(&sAppEventQueue, &event, K_FOREVER);
        DispatchEvent(event);
    }

    return CHIP_NO_ERROR;
}

void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */)
{
    LOG_INF("ChipEventHandler");
}

void AppTask::PostEvent(const AppEvent & event)
{
    if (k_msgq_put(&sAppEventQueue, &event, K_NO_WAIT) != 0)
    {
        LOG_INF("Failed to post event to app task event queue");
    }
}

void AppTask::DispatchEvent(const AppEvent & event)
{
    if (event.Handler)
    {
        event.Handler(event);
    }
    else
    {
        LOG_INF("Event received with no handler. Dropping event.");
    }
}

This is my log so far

I: Init CHIP stack
I: 90 [SVR]Subscription persistence not supported
I: 90 [SVR]Server initializing...
D: 100 [FP]Initializing FabricTable from persistent storage
I: 100 [TS]Last Known Good Time: 2023-10-14T01:16:48
I: 110 [DMG]AccessControl: initializing
I: 110 [DMG]Examples::AccessControlDelegate::Init
I: 110 [DMG]AccessControl: setting
I: 120 [DMG]DefaultAclStorage: initializing
I: 120 [DMG]DefaultAclStorage: 0 entries loaded
D: 130 [IN]UDP::Init bind&listen port=5540
E: 130 [IN]IPV6_PKTINFO failed: 109
D: 130 [IN]UDP::Init bound to port=5540
D: 140 [IN]TransportMgr initialized
I: 140 [ZCL]Using ZAP configuration...
I: 140 [DMG]AccessControlCluster: initializing
I: 150 [ZCL]Initiating Admin Commissioning cluster.
D: 150 [DL]Boot reason: 1
D: 150 [ZCL]GeneralDiagnostics: OnDeviceReboot
D: 160 [DMG]Endpoint 0, Cluster 0x0000_0033 update version to 4c06d632
D: 160 [EVL]LogEvent event number: 0x0000000000070000 priority: 2, endpoint id:  0x0 cluster id: 0x0000_0033 event id: 0x3 Sys timestamp: 0x000000000000003C
E: 180 [ZCL]DeviceInfoProvider is not registered
D: 180 [DMG]Endpoint 1, Cluster 0x0000_0004 update version to 5c439d02
D: 190 [DMG]Endpoint 1, Cluster 0x0000_0004 update version to 5c439d03
I: 190 [ZCL]0x806f000 ep 1 clus 0x0000_0062 attr 0x0000_0000 not supported
D: 200 [ZCL]ERR: setting LastConfiguredBy on Endpoint 1 Status: 86
D: 210 [IN]SecureSession[0x20001700]: Allocated Type:1 LSID:48179
D: 210 [SC]Assigned local session key ID 48179
D: 220 [SC]Waiting for PBKDF param request
I: 220 [DIS]Updating services using commissioning mode 1
I: 230 [DIS]CHIP minimal mDNS started advertising.
E: 230 [DIS]Failed to get primary mac address of device. Generating a random one.
I: 240 [DIS]Advertise commission parameter vendorID=65521 productID=32768 discriminator=3840/15 cm=1 cp=0
D: 250 [DIS]Responding with _matterc._udp.local
D: 250 [DIS]Responding with 78539C3005373652._matterc._udp.local
D: 260 [DIS]Responding with 882EF001F36D.local
D: 260 [DIS]Responding with 882EF001F36D.local
D: 270 [DIS]Responding with _V65521._sub._matterc._udp.local
D: 270 [DIS]Responding with _S15._sub._matterc._udp.local
D: 280 [DIS]Responding with _L3840._sub._matterc._udp.local
D: 280 [DIS]Responding with _CM._sub._matterc._udp.local
D: 290 [DIS]Responding with 78539C3005373652._matterc._udp.local
I: 290 [DIS]CHIP minimal mDNS configured as 'Commissionable node device'; instance name: 78539C3005373652.
I: 300 [DIS]mDNS service published: _matterc._udp
I: 310 [DIS]Updating services using commissioning mode 1
I: 310 [DIS]CHIP minimal mDNS started advertising.
I: 320 [DIS]Advertise commission parameter vendorID=65521 productID=32768 discriminator=3840/15 cm=1 cp=0
D: 320 [DIS]Responding with _matterc._udp.local
D: 330 [DIS]Responding with 78539C3005373652._matterc._udp.local
D: 330 [DIS]Responding with 882EF001F36D.local
D: 340 [DIS]Responding with 882EF001F36D.local
D: 340 [DIS]Responding with _V65521._sub._matterc._udp.local
D: 350 [DIS]Responding with _S15._sub._matterc._udp.local
D: 350 [DIS]Responding with _L3840._sub._matterc._udp.local
D: 360 [DIS]Responding with _CM._sub._matterc._udp.local
D: 360 [DIS]Responding with 78539C3005373652._matterc._udp.local
I: 370 [DIS]CHIP minimal mDNS configured as 'Commissionable node device'; instance name: 78539C3005373652.
I: 380 [DIS]mDNS service published: _matterc._udp
I: 380 [IN]CASE Server enabling CASE session setups
D: 390 [IN]SecureSession[0x200017d0]: Allocated Type:2 LSID:48180
D: 390 [SC]Allocated SecureSession (0x200017d0) - waiting for Sigma1 msg
I: 400 [SVR]Joining Multicast groups
D: 400 [ZCL]Emitting StartUp event
D: 410 [EVL]LogEvent event number: 0x0000000000070001 priority: 2, endpoint id:  0x0 cluster id: 0x0000_0028 event id: 0x0 Sys timestamp: 0x0000000000000136
I: 420 [SVR]Server Listening...
I: 420 [DL]Device Configuration:
I: 430 [DL]  Serial Number: TEST_SN
I: 430 [DL]  Vendor Id: 65521 (0xFFF1)
I: 430 [DL]  Product Id: 32768 (0x8000)
I: 440 [DL]  Product Name: TEST_PRODUCT
I: 440 [DL]  Hardware Version: 0
I: 440 [DL]  Setup Pin Code (0 for UNKNOWN/ERROR): 20202021
I: 450 [DL]  Setup Discriminator (0xFFFF for UNKNOWN/ERROR): 3840 (0xF00)
I: 450 [DL]  Manufacturing Date: (not set)
I: 460 [DL]  Device Type: 65535 (0xFFFF)
I: 460 [SVR]SetupQRCode: [MT:Y.K90AFN00KA0648G00]
I: 470 [SVR]Copy/paste the below URL in a browser to see the QR Code:
I: 470 [SVR]https://project-chip.github.io/connectedhomeip/qrcode.html?data=MT%3AY.K90AFN00KA0648G00
I: 480 [SVR]Manual pairing code: [34970112332]
D: 490 [IN]SecureSession[0x20001700]: Released - Type:1 LSID:48179
D: 490 [IN]SecureSession[0x20001700]: Allocated Type:1 LSID:48181
D: 500 [SC]Assigned local session key ID 48181
D: 500 [SC]Waiting for PBKDF param request
I: 510 [DIS]Updating services using commissioning mode 1
I: 510 [DIS]CHIP minimal mDNS started advertising.
I: 520 [DIS]Advertise commission parameter vendorID=65521 productID=32768 discriminator=3840/15 cm=1 cp=0
D: 520 [DIS]Responding with _matterc._udp.local
D: 530 [DIS]Responding with 3EEE8AAC8953FD67._matterc._udp.local
D: 530 [DIS]Responding with 882EF001F36D.local
D: 540 [DIS]Responding with 882EF001F36D.local
D: 540 [DIS]Responding with _V65521._sub._matterc._udp.local
D: 550 [DIS]Responding with _S15._sub._matterc._udp.local
D: 550 [DIS]Responding with _L3840._sub._matterc._udp.local
D: 560 [DIS]Responding with _CM._sub._matterc._udp.local
D: 560 [DIS]Responding with 3EEE8AAC8953FD67._matterc._udp.local
I: 570 [DIS]CHIP minimal mDNS configured as 'Commissionable node device'; instance name: 3EEE8AAC8953FD67.
I: 580 [DIS]mDNS service published: _matterc._udp
I: 580 [DL]CHIP task running
QuentinCaldeira-eaton commented 4 months ago

After debugging, seems like in UDPEndPointImplSockets.cpp, the ip address is always full of 0. Any way to pass a static ipv6 address ?