Skip to content

Commit

Permalink
[BL602] 1.add matter ota support, 2. use matter example DeviceInfoPro… (
Browse files Browse the repository at this point in the history
#19877)

* [BL602] 1.add matter ota support, 2. use matter example DeviceInfoProviderImpl.cpp for adaption, 3.usd matter default dnssd, 4. add compile compile parameters, 5.change product id.

* Restyled by clang-format

* Restyled by gn

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Aug 19, 2023
1 parent e25be0f commit 1155512
Show file tree
Hide file tree
Showing 17 changed files with 551 additions and 50 deletions.
1 change: 1 addition & 0 deletions examples/lighting-app/bouffalolab/bl602/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ bl602_executable("lighting_app") {
":sdk",
"${chip_root}/examples/common/QRCode",
"${chip_root}/examples/lighting-app/lighting-common",
"${chip_root}/examples/providers:device_info_provider",
"${chip_root}/src/lib",
"${chip_root}/src/setup_payload",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,38 @@
* TODO: 3R
*/
//#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0xF001
#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x8005
#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID 0x8001

/**
* CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION
*
* The hardware version number assigned to device or product by the device vendor. This
* number is scoped to the device product id, and typically corresponds to a revision of the
* physical device, a change to its packaging, and/or a change to its marketing presentation.
* This value is generally *not* incremented for device software versions.
*/
#define CHIP_DEVICE_CONFIG_DEVICE_HARDWARE_VERSION 1

/**
* CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING
*
* A string identifying the software version running on the device.
* CHIP service currently expects the software version to be in the format
* {MAJOR_VERSION}.0d{MINOR_VERSION}
*/
#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING
#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING "0.1ALPHA"

/**
* CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION
*
* A uint32_t identifying the software version running on the device.
*/
/* The SoftwareVersion attribute of the Basic cluster. */
#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION
#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION 0x0001
#endif
#endif

/**
* CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_REVISION
Expand Down
20 changes: 19 additions & 1 deletion examples/lighting-app/bouffalolab/bl602/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
#include "LEDWidget.h"
#include <DeviceInfoProviderImpl.h>
#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/cluster-id.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/DefaultOTARequestor.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorDriver.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <app/util/af-enums.h>
Expand All @@ -34,6 +39,7 @@
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/bouffalolab/BL602/NetworkCommissioningDriver.h>
#include <platform/bouffalolab/BL602/OTAImageProcessorImpl.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <bl_sys_ota.h>
Expand Down Expand Up @@ -72,6 +78,12 @@ chip::app::Clusters::NetworkCommissioning::Instance

using namespace ::chip::System;

DefaultOTARequestor gRequestorCore;
DefaultOTARequestorStorage gRequestorStorage;
DefaultOTARequestorDriver gRequestorUser;
BDXDownloader gDownloader;
OTAImageProcessorImpl gImageProcessor;

AppTask AppTask::sAppTask;
static DeviceCallbacks EchoCallbacks;

Expand Down Expand Up @@ -121,7 +133,13 @@ CHIP_ERROR AppTask::Init()

LightMgr().SetCallbacks(ActionInitiated, ActionCompleted);

UpdateClusterState();
SetRequestorInstance(&gRequestorCore);
gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage());
gRequestorCore.Init(chip::Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader);
gImageProcessor.SetOTADownloader(&gDownloader);
gDownloader.SetImageProcessorDelegate(&gImageProcessor);
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);
SetDeviceInfoProvider(&DeviceInfoProviderImpl::GetDefaultInstance());

ConfigurationMgr().LogDeviceConfig();

Expand Down
36 changes: 36 additions & 0 deletions examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/cluster-id.h>
#include <app/CommandHandler.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/server/Dnssd.h>
#include <app/util/basic-types.h>
#include <app/util/util.h>
Expand Down Expand Up @@ -250,3 +251,38 @@ bool emberAfBasicClusterMfgSpecificPingCallback(chip::app::CommandHandler * comm
emberAfSendDefaultResponse(emberAfCurrentCommand(), EMBER_ZCL_STATUS_SUCCESS);
return true;
}

void OnIdentifyStart(Identify *)
{
ChipLogProgress(Zcl, "OnIdentifyStart");
}

void OnIdentifyStop(Identify *)
{
ChipLogProgress(Zcl, "OnIdentifyStop");
}

void OnTriggerEffect(Identify * identify)
{
switch (identify->mCurrentEffectIdentifier)
{
case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK:
ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK");
break;
case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE:
ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE");
break;
case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY:
ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY");
break;
case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_CHANNEL_CHANGE:
break;
default:
ChipLogProgress(Zcl, "No identifier effect");
return;
}
}

static Identify gIdentify1 = {
chip::EndpointId{ 1 }, OnIdentifyStart, OnIdentifyStop, EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_VISIBLE_LED, OnTriggerEffect,
};
7 changes: 5 additions & 2 deletions src/platform/bouffalolab/BL602/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ static_library("BL602") {
"DeviceNetworkProvisioningDelegateImpl.h",
"DiagnosticDataProviderImpl.cpp",
"DiagnosticDataProviderImpl.h",
"DnssdImpl.cpp",
"InetPlatformConfig.h",
"Logging.cpp",
"NetworkCommissioningDriver.cpp",
"OTAImageProcessorImpl.cpp",
"PlatformManagerImpl.cpp",
"PlatformManagerImpl.h",
"SystemPlatformConfig.h",
]

deps = [ "${chip_root}/src/lib/dnssd:platform_header" ]
deps = [
"${chip_root}/src/lib/dnssd:platform_header",
"${chip_root}/src/setup_payload",
]

public_deps = [ "${chip_root}/src/platform:platform_base" ]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@

#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <DiagnosticDataProviderImpl.h>
#include <crypto/CHIPCryptoPAL.h>
#include <platform/DiagnosticDataProvider.h>
#include <platform/nxp/k32w/k32w0/DiagnosticDataProviderImpl.h>

#include <lwip/tcpip.h>

Expand Down
89 changes: 58 additions & 31 deletions src/platform/bouffalolab/BL602/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ namespace chip {
namespace Dnssd {

#define MDNS_MAX_PACKET_SIZE 64
typedef struct
{
const char * key; /*!< item key name */
const char * value; /*!< item value string */
size_t value_len;
} mdns_txt_item_t;

typedef struct mdns
{
struct netif * netif;
uint8_t slot[10];
uint8_t slot_idx;
int txt_cnt;
} mdns_t;

#define MDNS_TXT_MAX_LEN 128
static mdns_t mdns = { NULL, 0, 0, 0, 0 };
mdns_txt_item_t * items = nullptr;
uint8_t packet[MDNS_TXT_MAX_LEN];

static const DnssdService * glservice;

Expand All @@ -50,6 +69,7 @@ CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturn
CHIP_ERROR error = CHIP_NO_ERROR;

mdns_resp_init();
mdns.slot_idx = 0;
initCallback(context, error);

glservice = static_cast<DnssdService *>(chip::Platform::MemoryCalloc(1, sizeof(DnssdService)));
Expand All @@ -67,24 +87,6 @@ static const char * GetProtocolString(DnssdServiceProtocol protocol)
return protocol == DnssdServiceProtocol::kDnssdProtocolTcp ? "_tcp" : "_udp";
}

typedef struct
{
const char * key; /*!< item key name */
const char * value; /*!< item value string */
size_t value_len;
} mdns_txt_item_t;

typedef struct mdns
{
struct netif * netif;
int slot;
} mdns_t;

#define MDNS_TXT_MAX_LEN 128
static mdns_t mdns = { NULL, -1 };
mdns_txt_item_t * items = nullptr;
uint8_t packet[MDNS_TXT_MAX_LEN];

static inline uint8_t _mdns_append_u8(uint8_t * packet, uint16_t * index, uint8_t value)
{
if (*index >= MDNS_MAX_PACKET_SIZE)
Expand Down Expand Up @@ -139,7 +141,7 @@ static void srv_txt(struct mdns_service * service, void * txt_userdata)
int i, ret;
int index = 0;

for (i = 0; i < 3; i++)
for (i = 0; i < mdns.txt_cnt; i++)
{
ret = mdns_resp_add_service_txtitem(service, &(packet[index + 1]), packet[index]);
if (ret)
Expand Down Expand Up @@ -179,18 +181,21 @@ int mdns_responder_ops(struct netif * netif)
return -1;
}

mdns.netif = netif;

ret = mdns_resp_add_netif(netif, glservice->mHostName, 10);
if (ret != 0)
if (!(mdns.netif))
{
mdns_resp_deinit();
log_info("add netif failed:%d\r\n", ret);
return -1;
}
mdns.netif = netif;

items = static_cast<mdns_txt_item_t *>(chip::Platform::MemoryCalloc(glservice->mTextEntrySize, sizeof(mdns_txt_item_t)));
ret = mdns_resp_add_netif(netif, glservice->mHostName, 10);
if (ret != 0)
{
mdns_resp_deinit();
log_info("add netif failed:%d\r\n", ret);
return -1;
}
}

items = static_cast<mdns_txt_item_t *>(chip::Platform::MemoryCalloc(glservice->mTextEntrySize, sizeof(mdns_txt_item_t)));
mdns.txt_cnt = glservice->mTextEntrySize;
for (size_t i = 0; i < glservice->mTextEntrySize; i++)
{
items[i].key = glservice->mTextEntries[i].mKey;
Expand All @@ -199,8 +204,6 @@ int mdns_responder_ops(struct netif * netif)
packet_len = packet_len + strlen(items[i].key) + items[i].value_len + 1;
}

// todo:use malloc?
// packet = static_cast<uint8_t*>(chip::Platform::MemoryCalloc(packet_len, sizeof(uint8_t)));
if (MDNS_TXT_MAX_LEN < packet_len)
{
return -1;
Expand All @@ -220,6 +223,11 @@ int mdns_responder_ops(struct netif * netif)
return -1;
}

mdns.slot[mdns.slot_idx] = slot;
mdns.slot_idx++;
mdns_resp_announce(netif);

#if 0
// for ota
slot =
mdns_resp_add_service(netif, "MATTER OTA", "_ota", static_cast<uint8_t>(glservice->mProtocol), 3333, 1000, ota_txt, NULL);
Expand All @@ -229,6 +237,7 @@ int mdns_responder_ops(struct netif * netif)
mdns_resp_deinit();
log_info("ota mdns fail.\r\n");
}
#endif

return slot;
}
Expand Down Expand Up @@ -262,6 +271,8 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCal
return CHIP_ERROR_INTERNAL;
}

// mdns_responder_ops(netif);

slot = netifapi_netif_common(netif, NULL, mdns_responder_start_netifapi_errt_fn);
if (slot < 0)
{
Expand All @@ -274,7 +285,23 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCal

CHIP_ERROR ChipDnssdRemoveServices()
{
// netifapi_netif_common(mdns.netif, NULL, mdns_responder_stop_netifapi_errt_fn);
struct netif * netif;
int i = 0;

netif = wifi_mgmr_sta_netif_get();
if (netif == NULL)
{
log_info("find failed\r\n");
return CHIP_ERROR_INTERNAL;
}

for (i = 0; i < mdns.slot_idx; i++)
{
mdns_resp_del_service(netif, mdns.slot[i]);
}

mdns.slot_idx = 0;

return CHIP_NO_ERROR;
}

Expand Down
4 changes: 0 additions & 4 deletions src/platform/bouffalolab/BL602/InetPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@
#define INET_CONFIG_ERROR_MIN 1000000
#define INET_CONFIG_ERROR_MAX 1000999

#if LWIP_CONFIG_ENABLE_IPV4
#define INET_CONFIG_ENABLE_IPV4 1
#else
#define INET_CONFIG_ENABLE_IPV4 0
#endif

// ========== Platform-specific Configuration Overrides =========

Expand Down
Loading

0 comments on commit 1155512

Please sign in to comment.