Skip to content

Commit

Permalink
[portinglayer] Add the porting layer for matter
Browse files Browse the repository at this point in the history
- restructure the files
- create matter api
- create new light example using the porting layer
  • Loading branch information
step0035 committed Jan 5, 2023
1 parent e6a2c22 commit 9258310
Show file tree
Hide file tree
Showing 56 changed files with 1,845 additions and 46 deletions.
21 changes: 21 additions & 0 deletions component/common/application/matter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Matter Application Porting Layer

The goal of this porting layer is to make it easier for users to design their own Matter applications. The porting layer provides core Matter APIs for users and also makes it easier to add their own codes, such as their own peripheral drivers.

## Structure
| Directory | Description |
| ----------- | ----------- |
| common | contains common files and utilities to support Matter, used by all types of Matter applications |
| core | contains files that provides core Matter APIs |
| driver | peripheral drivers to be used by Matter application callbacks |
| example | Matter application examples for reference, according to device types |

## How to design your own custom Matter application with the porting layer
1. You do not need to modify/add files under `common` or `core`
2. Under `driver`, place your peripheral driver code, see existing driver files for reference
3. Under `example`, you may create your new custom example directory, or modify an existing one
- Your example should at least have the following files:
- Main task (see `light/example_matter_light.cpp`)
- Driver interface (see `light/matter_drivers.cpp`)
- Makefiles to build the Matter libraries
4. More details on how it works will be explained in the examples themselves
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
extern void ChipTest(void);
extern u32 deinitPref(void);
#if CONFIG_ENABLE_OTA_REQUESTOR
#if CONFIG_EXAMPLE_MATTER_CHIPTEST
extern void amebaQueryImageCmdHandler();
extern void amebaApplyUpdateCmdHandler();
#endif
#endif

void fATchipapp(void *arg)
{
Expand All @@ -34,19 +36,23 @@ void fATchipapp(void *arg)
void fATchipapp1(void *arg)
{
#if CONFIG_ENABLE_OTA_REQUESTOR
#if CONFIG_EXAMPLE_MATTER_CHIPTEST
printf("Chip Test: amebaQueryImageCmdHandler\r\n");
amebaQueryImageCmdHandler();
#endif
#endif
}

void fATchipapp2(void *arg)
{
#if CONFIG_ENABLE_OTA_REQUESTOR
#if CONFIG_EXAMPLE_MATTER_CHIPTEST
(void) arg;
printf("Chip Test: amebaApplyUpdateCmdHandler\r\n");

amebaApplyUpdateCmdHandler();
#endif
#endif
}

log_item_t at_matter_items[] = {
Expand All @@ -68,4 +74,4 @@ void at_matter_init(void)
log_module_init(at_matter_init);
#endif

#endif /* CHIP_PROJECT */
#endif /* CHIP_PROJECT */
162 changes: 162 additions & 0 deletions component/common/application/matter/core/matter_core.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <stdlib.h>
#include <stdint.h>

#include "matter_core.h"
#include "matter_ota.h"
#include <DeviceInfoProviderImpl.h>

#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/command-id.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/util/af-types.h>
#include <app/util/attribute-storage.h>
#include <app/util/basic-types.h>
#include <app/util/util.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/server/Dnssd.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>

#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>

#include <lib/dnssd/Advertiser.h>

#include <platform/Ameba/AmebaConfig.h>
#include <platform/Ameba/FactoryDataProvider.h>
#include <platform/Ameba/NetworkCommissioningDriver.h>

#include <route_hook/ameba_route_hook.h>

#include <support/CHIPMem.h>
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>

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

app::Clusters::NetworkCommissioning::Instance
sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::AmebaWiFiDriver::GetInstance()));

chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
chip::DeviceLayer::FactoryDataProvider mFactoryDataProvider;

void matter_core_device_callback_internal(const ChipDeviceEvent * event, intptr_t arg)
{
switch (event->Type)
{
case DeviceEventType::kInternetConnectivityChange:
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
static bool isOTAInitialized = false;
#endif
if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established)
{
ChipLogProgress(DeviceLayer, "IPv4 Server ready...");
chip::app::DnssdServer::Instance().StartServer();
}
else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost)
{
ChipLogProgress(DeviceLayer, "Lost IPv4 connectivity...");
}
if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established)
{
ChipLogProgress(DeviceLayer, "IPv6 Server ready...");
chip::app::DnssdServer::Instance().StartServer();

ChipLogProgress(DeviceLayer, "Initializing route hook...");
ameba_route_hook_init();
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
// Init OTA requestor only when we have gotten IPv6 address
if (!isOTAInitialized)
{
matter_ota_init();
isOTAInitialized = true;
}
#endif
}
else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost)
{
ChipLogProgress(DeviceLayer, "Lost IPv6 connectivity...");
}
break;

case DeviceEventType::kInterfaceIpAddressChanged:
if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) ||
(event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned))
{
// MDNS server restart on any ip assignment: if link local ipv6 is configured, that
// will not trigger a 'internet connectivity change' as there is no internet
// connectivity. MDNS still wants to refresh its listening interfaces to include the
// newly selected address.
chip::app::DnssdServer::Instance().StartServer();
}
break;
}
}

void matter_core_init_server(intptr_t context)
{
xTaskHandle task_to_notify = reinterpret_cast<xTaskHandle>(context);
// Init ZCL Data Model and CHIP App Server
static chip::CommonCaseDeviceServerInitParams initParams;
initParams.InitializeStaticResourcesBeforeServerInit();
chip::Server::GetInstance().Init(initParams);
gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);

sWiFiNetworkCommissioningInstance.Init();

// We only have network commissioning on endpoint 0.
// TODO: configure the endpoint
emberAfEndpointEnableDisable(0xFFFE, false);

if (RTW_SUCCESS != wifi_is_connected_to_ap())
{
// QR code will be used with CHIP Tool
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
}
xTaskNotifyGive(task_to_notify);
}

CHIP_ERROR matter_core_init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
err = Platform::MemoryInit();
SuccessOrExit(err);

// Initialize the CHIP stack.
err = PlatformMgr().InitChipStack();
SuccessOrExit(err);

// TODO: update this when upstream PR merged
SetCommissionableDataProvider(&mFactoryDataProvider);
SetDeviceAttestationCredentialsProvider(&mFactoryDataProvider);

if (CONFIG_NETWORK_LAYER_BLE)
{
ConnectivityMgr().SetBLEAdvertisingEnabled(true);
}

// Start a task to run the CHIP Device event loop.
err = PlatformMgr().StartEventLoopTask();
SuccessOrExit(err);

// Register a function to receive events from the CHIP device layer. Note that calls to
// this function will happen on the CHIP event loop thread, not the app_main thread.
PlatformMgr().AddEventHandler(matter_core_device_callback_internal, reinterpret_cast<intptr_t>(NULL));

// PlatformMgr().ScheduleWork(matter_core_init_server, 0);
PlatformMgr().ScheduleWork(matter_core_init_server, reinterpret_cast<intptr_t>(xTaskGetCurrentTaskHandle()));
xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);

exit:
return err;
}

CHIP_ERROR matter_core_start()
{
return matter_core_init();
// matter_core_init_server();
}
12 changes: 12 additions & 0 deletions component/common/application/matter/core/matter_core.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <platform/CHIPDeviceLayer.h>

// #include <lib/support/DLLUtil.h>
//
// #include <stdarg.h>
// #include <stdlib.h>
//
// #include <app/util/af-types.h>

CHIP_ERROR matter_core_start(void);
21 changes: 21 additions & 0 deletions component/common/application/matter/core/matter_events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <platform/CHIPDeviceLayer.h>

struct AppEvent;
typedef void (*EventHandler)(AppEvent *);

struct AppEvent
{
enum AppEventTypes
{
kEventType_Uplink = 0,
kEventType_Downlink_OnOff,
kEventType_Downlink_Identify,
};

uint16_t Type;
chip::app::ConcreteAttributePath path;
uint8_t value;
EventHandler mHandler;
};
Loading

0 comments on commit 9258310

Please sign in to comment.