-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[portinglayer] Add the porting layer for matter
- restructure the files - create matter api - create new light example using the porting layer
- Loading branch information
Showing
56 changed files
with
1,838 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
162 changes: 162 additions & 0 deletions
162
component/common/application/matter/core/matter_core.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <platform/CHIPDeviceLayer.h> | ||
|
||
CHIP_ERROR matter_core_start(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.