Skip to content

Commit

Permalink
Mbed OTA-requestor-app cleanup
Browse files Browse the repository at this point in the history
Move common cmake code to mbed-platform-specific file
  • Loading branch information
ATmobica committed Dec 9, 2021
1 parent 6c75afc commit 827662e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 225 deletions.
23 changes: 22 additions & 1 deletion config/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ if (CONFIG_CHIP_PW_RPC)
chip_gn_arg_bool ("chip_build_pw_rpc_lighting_proto" CONFIG_CHIP_PW_RPC_LIGHTING_PROTO)
chip_gn_arg_bool ("chip_build_pw_rpc_locking_proto" CONFIG_CHIP_PW_RPC_LOCKING_PROTO)
endif(CONFIG_CHIP_PW_RPC)
chip_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR)
if (CONFIG_CHIP_OTA_REQUESTOR)
chip_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR)
endif(CONFIG_CHIP_OTA_REQUESTOR)


file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/args.gn CONTENT ${CHIP_GN_ARGS})
Expand Down Expand Up @@ -444,6 +446,25 @@ endif(CONFIG_CHIP_PW_RPC_LOCKING_PROTO)

endif(CONFIG_CHIP_PW_RPC)

if (CONFIG_CHIP_OTA_REQUESTOR)
target_include_directories(${APP_TARGET} PRIVATE
${CHIP_ROOT}/zzz_generated/ota-requestor-app
${CHIP_ROOT}/src/app/clusters/ota-requestor
${CHIP_ROOT}/src/platform/mbed
)

target_sources(${APP_TARGET} PRIVATE
${CHIP_ROOT}/zzz_generated/ota-requestor-app/zap-generated/attribute-size.cpp
${CHIP_ROOT}/zzz_generated/ota-requestor-app/zap-generated/callback-stub.cpp
${CHIP_ROOT}/zzz_generated/ota-requestor-app/zap-generated/CHIPClusters.cpp
${CHIP_ROOT}/zzz_generated/ota-requestor-app/zap-generated/IMClusterCommandHandler.cpp
)

list(APPEND CHIP_DEFINES
CHIP_OTA_REQUESTOR=1
)
endif(CONFIG_CHIP_OTA_REQUESTOR)


target_include_directories(${APP_TARGET} PRIVATE
${CHIP_INCLUDES}
Expand Down
17 changes: 3 additions & 14 deletions examples/ota-requestor-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
cmake_minimum_required(VERSION 3.19.0)

get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH)
get_filename_component(APP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/.. REALPATH)
get_filename_component(MBED_COMMON ${CHIP_ROOT}/examples/platform/mbed REALPATH)
get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH)
get_filename_component(NLIO_DIR ${CHIP_ROOT}/third_party/nlio/repo/include REALPATH)
Expand Down Expand Up @@ -37,13 +36,8 @@ target_include_directories(${APP_TARGET} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/main/include/
${NLIO_DIR}
${GEN_DIR}
${GEN_DIR}/app-common
${GEN_DIR}/ota-requestor-app
${APP_ROOT}/ota-requestor-common
${MBED_COMMON}/util/include
${CHIP_ROOT}/src/app/clusters/ota-requestor
${CHIP_ROOT}/src/platform/mbed
)

target_sources(${APP_TARGET} PRIVATE
Expand All @@ -52,18 +46,13 @@ target_sources(${APP_TARGET} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/main/AppTask.cpp
${CMAKE_CURRENT_SOURCE_DIR}/main/MbedOTARequestorDriver.cpp
${MBED_COMMON}/util/LEDWidget.cpp

${GEN_DIR}/ota-requestor-app/zap-generated/attribute-size.cpp
${GEN_DIR}/ota-requestor-app/zap-generated/callback-stub.cpp
${GEN_DIR}/ota-requestor-app/zap-generated/CHIPClusters.cpp
${GEN_DIR}/ota-requestor-app/zap-generated/IMClusterCommandHandler.cpp
)

chip_configure_data_model(${APP_TARGET}
INCLUDE_CLIENT_CALLBACKS
INCLUDE_SERVER
ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../ota-requestor-common/ota-requestor-app.zap
GEN_DIR ${GEN_DIR}/ota-requestor-app/zap-generated
INCLUDE_CLIENT_CALLBACKS
ZAP_FILE ${CHIP_ROOT}/examples/ota-requestor-app/ota-requestor-common/ota-requestor-app.zap
GEN_DIR ${CHIP_ROOT}/zzz_generated/ota-requestor-app/zap-generated
)

target_link_libraries(${APP_TARGET} mbed-os-posix-socket mbed-os mbed-ble mbed-events mbed-netsocket mbed-storage mbed-storage-kv-global-api mbed-mbedtls mbed-emac chip)
Expand Down
97 changes: 54 additions & 43 deletions examples/ota-requestor-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,25 @@
*/

#include "AppTask.h"
#include "LEDWidget.h"
#include <app/server/OnboardingCodesUtil.h>
#include <LEDWidget.h>

// FIXME: Undefine the `sleep()` function included by the CHIPDeviceLayer.h
// from unistd.h to avoid a conflicting declaration with the `sleep()` provided
// by Mbed-OS in mbed_power_mgmt.h.
#define sleep unistd_sleep
#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 <platform/CHIPDeviceLayer.h>
#undef sleep

#include <lib/support/logging/CHIPLogging.h>

// ZAP -- ZCL Advanced Platform
#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/util/attribute-storage.h>
#include <platform/CHIPDeviceLayer.h>

// mbed-os headers
#include "drivers/InterruptIn.h"
#include "drivers/Timeout.h"
#include "events/EventQueue.h"
#include "platform/Callback.h"

#ifdef CHIP_OTA_REQUESTOR
#include "MbedOTARequestorDriver.h"
#include <MbedOTADownloader.h>
#include <MbedOTAImageProcessor.h>
#include <MbedOTARequestor.h>
#include <MbedOTARequestorDriver.h>
#endif // CHIP_OTA_REQUESTOR

static bool sIsWiFiStationProvisioned = false;
static bool sIsWiFiStationEnabled = false;
Expand All @@ -68,6 +55,7 @@ AppTask AppTask::sAppTask;

int AppTask::Init()
{
CHIP_ERROR error;
// Register the callback to init the MDNS server when connectivity is available
PlatformMgr().AddEventHandler(
[](const ChipDeviceEvent * event, intptr_t arg) {
Expand All @@ -83,13 +71,28 @@ int AppTask::Init()
},
0);

#ifdef MBED_CONF_APP_BLE_DEVICE_NAME
error = ConnectivityMgr().SetBLEDeviceName(MBED_CONF_APP_BLE_DEVICE_NAME);
if (error != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Set BLE device name failed: %s", error.AsString());
return EXIT_FAILURE;
}
#endif

// Start BLE advertising if needed
if (!CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART)
{
ChipLogProgress(NotSpecified, "Enabling BLE advertising.");
ConnectivityMgr().SetBLEAdvertisingEnabled(true);
error = ConnectivityMgr().SetBLEAdvertisingEnabled(true);
if (error != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Set BLE advertising enabled failed: %s", error.AsString());
return EXIT_FAILURE;
}
}

#ifdef CHIP_OTA_REQUESTOR
// Initialize and interconnect the Requestor and Image Processor objects -- START
// Initialize the instance of the main Requestor Class
MbedOTARequestor * requestorCore = new MbedOTARequestor(OnAnnounceProviderCallback, OnProviderResponseCallback);
Expand All @@ -111,11 +114,17 @@ int AppTask::Init()
// Connect the Downloader and Image Processor objects
downloaderCore->SetImageProcessorDelegate(downloaderUser);
// Initialize and interconnect the Requestor and Image Processor objects -- END
#endif // CHIP_OTA_REQUESTOR

chip::DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement();
ConnectivityMgrImpl().StartWiFiManagement();

// Init ZCL Data Model and start server
chip::Server::GetInstance().Init();
error = Server::GetInstance().Init();
if (error != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Server initalization failed: %s", error.AsString());
return EXIT_FAILURE;
}

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
Expand Down Expand Up @@ -186,6 +195,28 @@ int AppTask::StartApp()
}
}

void AppTask::PostEvent(AppEvent * aEvent)
{
auto handle = sAppEventQueue.call([event = *aEvent, this] { DispatchEvent(&event); });
if (!handle)
{
ChipLogError(NotSpecified, "Failed to post event to app task event queue: Not enough memory");
}
}

void AppTask::DispatchEvent(const AppEvent * aEvent)
{
if (aEvent->Handler)
{
aEvent->Handler(const_cast<AppEvent *>(aEvent));
}
else
{
ChipLogError(NotSpecified, "Event received with no handler. Dropping event.");
}
}

#ifdef CHIP_OTA_REQUESTOR
void AppTask::OnOtaEventHandler(AppEvent * aEvent)
{
switch (aEvent->Type)
Expand Down Expand Up @@ -241,24 +272,4 @@ void AppTask::OnDownloadCompletedCallback(MbedOTADownloader::ImageInfo * imageIn
ota_download_completed_event.OTADownloadCompletedEvent.imageInfo = imageInfo;
sAppTask.PostEvent(&ota_download_completed_event);
}

void AppTask::PostEvent(AppEvent * aEvent)
{
auto handle = sAppEventQueue.call([event = *aEvent, this] { DispatchEvent(&event); });
if (!handle)
{
ChipLogError(NotSpecified, "Failed to post event to app task event queue: Not enough memory");
}
}

void AppTask::DispatchEvent(const AppEvent * aEvent)
{
if (aEvent->Handler)
{
aEvent->Handler(const_cast<AppEvent *>(aEvent));
}
else
{
ChipLogError(NotSpecified, "Event received with no handler. Dropping event.");
}
}
#endif
6 changes: 1 addition & 5 deletions examples/ota-requestor-app/mbed/main/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
#include <app/ConcreteAttributePath.h>
#include <lib/support/logging/CHIPLogging.h>

#include <app-common/zap-generated/cluster-objects.h>
#include <app/CommandHandler.h>
#include <app/util/af.h>

using namespace chip;
using namespace ::chip;
using namespace ::chip::app::Clusters;

void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t mask, uint8_t type,
Expand Down
8 changes: 7 additions & 1 deletion examples/ota-requestor-app/mbed/main/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

#pragma once

#include <app-common/zap-generated/cluster-objects.h>
#include <cstdint>

#ifdef CHIP_OTA_REQUESTOR
#include <platform/mbed/MbedOTADownloader.h>
#include <platform/mbed/MbedOTARequestor.h>
#endif // CHIP_OTA_REQUESTOR

struct AppEvent;
typedef void (*EventHandler)(AppEvent *);
Expand All @@ -32,12 +34,15 @@ struct AppEvent
enum AppEventTypes
{
kEventType_ota_provider_announce = 0,
kEventType_ota_provider_connect,
kEventType_ota_provider_response,
kEventType_ota_download_completed,
kEventType_ota_download_failed,
};

uint16_t Type;

#ifdef CHIP_OTA_REQUESTOR
union
{
struct
Expand All @@ -50,6 +55,7 @@ struct AppEvent
chip::MbedOTADownloader::ImageInfo * imageInfo;
} OTADownloadCompletedEvent;
};
#endif // CHIP_OTA_REQUESTOR

EventHandler Handler;
};
7 changes: 5 additions & 2 deletions examples/ota-requestor-app/mbed/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
#pragma once

#include "AppEvent.h"

#ifdef CHIP_OTA_REQUESTOR
#include <MbedOTADownloader.h>
#include <MbedOTARequestor.h>
#endif

class AppTask
{
Expand All @@ -36,13 +39,13 @@ class AppTask
void PostEvent(AppEvent * aEvent);
void DispatchEvent(const AppEvent * event);

#ifdef CHIP_OTA_REQUESTOR
static void OnOtaEventHandler(AppEvent * aEvent);

static void OnAnnounceProviderCallback();

static void OnProviderResponseCallback(MbedOTARequestor::OTAUpdateDetails * updateDetails);

static void OnDownloadCompletedCallback(chip::MbedOTADownloader::ImageInfo * imageInfo);
#endif

static AppTask sAppTask;
};
Expand Down
30 changes: 3 additions & 27 deletions examples/ota-requestor-app/mbed/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@

#include "AppTask.h"

#ifdef CHIP_PW_RPC
#include "Rpc.h"
#endif

#include "mbedtls/platform.h"
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
#include <mbedtls/platform.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/mbed/Logging.h>

using namespace ::chip;
using namespace ::chip::Inet;
using namespace ::chip::DeviceLayer;
using namespace ::chip::Platform;
using namespace ::chip::Logging::Platform;

int main()
Expand All @@ -39,16 +35,6 @@ int main()

mbed_logging_init();

#if CHIP_PW_RPC
auto rpcThread = chip::rpc::Init();
if (rpcThread == NULL)
{
ChipLogError(NotSpecified, "RPC service initialization and run failed");
ret = EXIT_FAILURE;
goto exit;
}
#endif

ChipLogProgress(SoftwareUpdate, "Mbed OTA Requestor example application start");

ret = mbedtls_platform_setup(NULL);
Expand All @@ -58,7 +44,7 @@ int main()
goto exit;
}

err = chip::Platform::MemoryInit();
err = MemoryInit();
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Memory initalization failed: %s", err.AsString());
Expand All @@ -74,16 +60,6 @@ int main()
goto exit;
}

#ifdef MBED_CONF_APP_BLE_DEVICE_NAME
err = ConnectivityMgr().SetBLEDeviceName(MBED_CONF_APP_BLE_DEVICE_NAME);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Set BLE device name failed: %s", err.AsString());
ret = EXIT_FAILURE;
goto exit;
}
#endif

err = PlatformMgr().StartEventLoopTask();
if (err != CHIP_NO_ERROR)
{
Expand Down
Loading

0 comments on commit 827662e

Please sign in to comment.