Skip to content

Commit

Permalink
[K32W0] Add OTA for the lighting application (project-chip#13331)
Browse files Browse the repository at this point in the history
* [K32W0] Add OTA for the lighting application

Signed-off-by: Doru Gucea <[email protected]>

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
doru91 and restyled-commits authored Jan 7, 2022
1 parent 5cd7eee commit ec032bf
Show file tree
Hide file tree
Showing 12 changed files with 948 additions and 43 deletions.
2 changes: 1 addition & 1 deletion examples/lighting-app/nxp/k32w/k32w0/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ k32w0_executable("light_app") {
deps = [
":sdk",
"${chip_root}/examples/common/QRCode",
"${chip_root}/examples/lighting-app/lighting-common",
"${chip_root}/examples/ota-requestor-app/ota-requestor-common",
"${chip_root}/src/lib",
"${chip_root}/third_party/mbedtls:mbedtls",
"${k32w0_platform_dir}/app/support:freertos_mbedtls_utils",
Expand Down
4 changes: 4 additions & 0 deletions examples/lighting-app/nxp/k32w/k32w0/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ import("${chip_root}/examples/platform/nxp/k32w/k32w0/args.gni")

# SDK target. This is overridden to add our SDK app_config.h & defines.
k32w0_sdk_target = get_label_info(":sdk", "label_no_toolchain")

declare_args() {
chip_enable_ota_requestor = true
}
98 changes: 65 additions & 33 deletions examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
#include <app-common/zap-generated/cluster-id.h>
#include <app/util/attribute-storage.h>

/* OTA related includes */
#include "OTAImageProcessorImpl.h"
#include "OtaSupport.h"
#include "platform/GenericOTARequestorDriver.h"
#include "src/app/clusters/ota-requestor/BDXDownloader.h"
#include "src/app/clusters/ota-requestor/OTARequestor.h"

#include "Keyboard.h"
#include "LED.h"
#include "LEDWidget.h"
Expand Down Expand Up @@ -71,9 +78,20 @@ extern "C" void K32WUartProcess(void);

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

AppTask AppTask::sAppTask;

/* OTA related variables */
static OTARequestor gRequestorCore;
DeviceLayer::GenericOTARequestorDriver gRequestorUser;
static BDXDownloader gDownloader;
static OTAImageProcessorImpl gImageProcessor;

static NodeId providerNodeId = 2;
static FabricIndex providerFabricIndex = 1;

CHIP_ERROR AppTask::StartAppTask()
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -103,6 +121,32 @@ CHIP_ERROR AppTask::Init()
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
#endif

// Initialize and interconnect the Requestor and Image Processor objects -- START
SetRequestorInstance(&gRequestorCore);

// Set server instance used for session establishment
chip::Server * server = &(chip::Server::GetInstance());
gRequestorCore.SetServerInstance(server);

// Connect the Requestor and Requestor Driver objects
gRequestorCore.SetOtaRequestorDriver(&gRequestorUser);
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);

// WARNING: this is probably not realistic to know such details of the image or to even have an OTADownloader instantiated at
// the beginning of program execution. We're using hardcoded values here for now since this is a reference application.
// TODO: instatiate and initialize these values when QueryImageResponse tells us an image is available
// TODO: add API for OTARequestor to pass QueryImageResponse info to the application to use for OTADownloader init
OTAImageProcessorParams ipParams;
ipParams.imageFile = CharSpan("test.txt");
gImageProcessor.SetOTAImageProcessorParams(ipParams);
gImageProcessor.SetOTADownloader(&gDownloader);

// Connect the gDownloader and Image Processor objects
gDownloader.SetImageProcessorDelegate(&gImageProcessor);

gRequestorCore.SetBDXDownloader(&gDownloader);
// Initialize and interconnect the Requestor and Image Processor objects -- END

// QR code will be used with CHIP Tool
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));

Expand Down Expand Up @@ -230,12 +274,22 @@ void AppTask::AppTaskMain(void * pvParameter)
sLightLED.Animate();

HandleKeyboard();

if (gDownloader.GetState() == OTADownloader::State::kInProgress)
{
OTA_TransactionResume();

if (!EEPROM_isBusy())
{
gDownloader.FetchNextData();
}
}
}
}

void AppTask::ButtonEventHandler(uint8_t pin_no, uint8_t button_action)
{
if ((pin_no != RESET_BUTTON) && (pin_no != LIGHT_BUTTON) && (pin_no != JOIN_BUTTON) && (pin_no != BLE_BUTTON))
if ((pin_no != RESET_BUTTON) && (pin_no != LIGHT_BUTTON) && (pin_no != OTA_BUTTON) && (pin_no != BLE_BUTTON))
{
return;
}
Expand All @@ -253,9 +307,9 @@ void AppTask::ButtonEventHandler(uint8_t pin_no, uint8_t button_action)
{
button_event.Handler = LightActionEventHandler;
}
else if (pin_no == JOIN_BUTTON)
else if (pin_no == OTA_BUTTON)
{
button_event.Handler = JoinHandler;
button_event.Handler = OTAHandler;
}
else if (pin_no == BLE_BUTTON)
{
Expand Down Expand Up @@ -307,7 +361,7 @@ void AppTask::HandleKeyboard(void)
ButtonEventHandler(LIGHT_BUTTON, LIGHT_BUTTON_PUSH);
break;
case gKBD_EventPB3_c:
ButtonEventHandler(JOIN_BUTTON, JOIN_BUTTON_PUSH);
ButtonEventHandler(OTA_BUTTON, OTA_BUTTON_PUSH);
break;
case gKBD_EventPB4_c:
ButtonEventHandler(BLE_BUTTON, BLE_BUTTON_PUSH);
Expand Down Expand Up @@ -435,43 +489,21 @@ void AppTask::LightActionEventHandler(AppEvent * aEvent)
}
}

void AppTask::ThreadStart()
void AppTask::OTAHandler(AppEvent * aEvent)
{
chip::Thread::OperationalDataset dataset{};

constexpr uint8_t xpanid[] = { 0xde, 0xad, 0x00, 0xbe, 0xef, 0x00, 0xca, 0xfe };
constexpr uint8_t masterkey[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
constexpr uint16_t panid = 0xabcd;
constexpr uint16_t channel = 15;

dataset.SetNetworkName("OpenThread");
dataset.SetExtendedPanId(xpanid);
dataset.SetMasterKey(masterkey);
dataset.SetPanId(panid);
dataset.SetChannel(channel);

ThreadStackMgr().SetThreadEnabled(false);
ThreadStackMgr().SetThreadProvision(dataset.AsByteSpan());
ThreadStackMgr().SetThreadEnabled(true);
}

void AppTask::JoinHandler(AppEvent * aEvent)
{
if (aEvent->ButtonEvent.PinNo != JOIN_BUTTON)
if (aEvent->ButtonEvent.PinNo != OTA_BUTTON)
return;

if (sAppTask.mFunction != kFunction_NoneSelected)
{
K32W_LOG("Another function is scheduled. Could not initiate Thread Join!");
K32W_LOG("Another function is scheduled. Could not initiate OTA!");
return;
}

/* hard-code Thread Commissioning Parameters for the moment.
* In a future PR, these parameters will be sent via BLE.
*/
ThreadStart();
// In this mode Provider node ID and fabric idx must be supplied explicitly from program args
gRequestorCore.TestModeSetProviderParameters(providerNodeId, providerFabricIndex, chip::kRootEndpointId);

static_cast<OTARequestor *>(GetRequestorInstance())->TriggerImmediateQuery();
}

void AppTask::BleHandler(AppEvent * aEvent)
Expand Down
4 changes: 1 addition & 3 deletions examples/lighting-app/nxp/k32w/k32w0/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AppTask
static void FunctionTimerEventHandler(AppEvent * aEvent);
static void KBD_Callback(uint8_t events);
static void HandleKeyboard(void);
static void JoinHandler(AppEvent * aEvent);
static void OTAHandler(AppEvent * aEvent);
static void BleHandler(AppEvent * aEvent);
static void LightActionEventHandler(AppEvent * aEvent);
static void ResetActionEventHandler(AppEvent * aEvent);
Expand All @@ -73,8 +73,6 @@ class AppTask
static void TimerEventHandler(TimerHandle_t xTimer);

static void ThreadProvisioningHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);

static void ThreadStart();
void StartTimer(uint32_t aTimeoutInMs);

enum Function_t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

#define RESET_BUTTON 1
#define LIGHT_BUTTON 2
#define JOIN_BUTTON 3
#define OTA_BUTTON 3
#define BLE_BUTTON 4

#define RESET_BUTTON_PUSH 1
#define LIGHT_BUTTON_PUSH 2
#define JOIN_BUTTON_PUSH 3
#define OTA_BUTTON_PUSH 3
#define BLE_BUTTON_PUSH 4

#define APP_BUTTON_PUSH 1
Expand Down
7 changes: 7 additions & 0 deletions src/platform/nxp/k32w/k32w0/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ static_library("k32w0") {
"ble_function_mux.c",
]

if (chip_enable_ota_requestor) {
sources += [
"OTAImageProcessorImpl.cpp",
"OTAImageProcessorImpl.h",
]
}

deps = []

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

0 comments on commit ec032bf

Please sign in to comment.