Skip to content

Commit 586f962

Browse files
kkasperczyk-nonordicjm
authored andcommitted
samples: matter: Used DFUsync module in the DFU over SMP
Updated Matter repository to pull the change introducing DFUSync module and use in the DFU over SMP implementation in Matter. Signed-off-by: Kamil Kasperczyk <[email protected]>
1 parent 73e77b3 commit 586f962

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

samples/matter/common/src/dfu/smp/dfu_over_smp.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "dfu/ota/ota_util.h"
1818

1919
#include <platform/CHIPDeviceLayer.h>
20+
#include <platform/nrfconnect/DFUSync.h>
2021

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

@@ -37,25 +38,38 @@ constexpr static uint16_t kAdvertisingIntervalMin = 400;
3738
constexpr static uint16_t kAdvertisingIntervalMax = 500;
3839
constexpr static uint8_t kAdvertisingFlags = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR;
3940

41+
static bool sDfuInProgress = false;
42+
static uint32_t sDfuSyncMutexId;
43+
4044
namespace
4145
{
42-
enum mgmt_cb_return UploadConfirmHandler(uint32_t event, enum mgmt_cb_return prev_status, int32_t *rc, uint16_t *group,
43-
bool *abort_more, void *data, size_t data_size)
46+
enum mgmt_cb_return UploadConfirmHandler(uint32_t, enum mgmt_cb_return, int32_t *rc, uint16_t *,
47+
bool *, void *data, size_t)
4448
{
4549
/* Currently img_mgmt hooks are not supported by SUIT */
4650
#ifndef CONFIG_SUIT
4751
const img_mgmt_upload_check &imgData = *static_cast<img_mgmt_upload_check *>(data);
4852
IgnoreUnusedVariable(imgData);
4953

54+
if (!sDfuInProgress) {
55+
if (DFUSync::GetInstance().Take(sDfuSyncMutexId) == CHIP_NO_ERROR) {
56+
sDfuInProgress = true;
57+
} else {
58+
ChipLogError(SoftwareUpdate, "Cannot start DFU over SMP, another DFU in progress.");
59+
*rc = MGMT_ERR_EBUSY;
60+
return MGMT_CB_ERROR_RC;
61+
}
62+
}
63+
5064
ChipLogProgress(SoftwareUpdate, "DFU over SMP progress: %u/%u B of image %u",
5165
static_cast<unsigned>(imgData.req->off), static_cast<unsigned>(imgData.action->size),
5266
static_cast<unsigned>(imgData.req->image));
5367
#endif
5468
return MGMT_CB_OK;
5569
}
5670

57-
enum mgmt_cb_return CommandHandler(uint32_t event, enum mgmt_cb_return prev_status, int32_t *rc, uint16_t *group,
58-
bool *abort_more, void *data, size_t data_size)
71+
enum mgmt_cb_return CommandHandler(uint32_t event, enum mgmt_cb_return, int32_t *, uint16_t *,
72+
bool *, void *, size_t)
5973
{
6074
if (event == MGMT_EVT_OP_CMD_RECV) {
6175
Nrf::Matter::GetFlashHandler().DoAction(ExternalFlashManager::Action::WAKE_UP);
@@ -66,6 +80,14 @@ enum mgmt_cb_return CommandHandler(uint32_t event, enum mgmt_cb_return prev_stat
6680
return MGMT_CB_OK;
6781
}
6882

83+
enum mgmt_cb_return DfuStoppedHandler(uint32_t, enum mgmt_cb_return, int32_t *, uint16_t *,
84+
bool *, void *, size_t)
85+
{
86+
DFUSync::GetInstance().Free(sDfuSyncMutexId);
87+
88+
return MGMT_CB_OK;
89+
}
90+
6991
mgmt_callback sUploadCallback = {
7092
.callback = UploadConfirmHandler,
7193
.event_id = MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK,
@@ -76,11 +98,20 @@ mgmt_callback sCommandCallback = {
7698
.event_id = (MGMT_EVT_OP_CMD_RECV | MGMT_EVT_OP_CMD_DONE),
7799
};
78100

101+
mgmt_callback sDfuStopped = {
102+
.callback = DfuStoppedHandler,
103+
.event_id = (MGMT_EVT_OP_IMG_MGMT_DFU_STOPPED | MGMT_EVT_OP_IMG_MGMT_DFU_PENDING),
104+
};
105+
79106
} /* namespace */
80107

81108
namespace Nrf
82109
{
83110

111+
BT_CONN_CB_DEFINE(conn_callbacks) = {
112+
.disconnected = DFUOverSMP::Disconnected,
113+
};
114+
84115
DFUOverSMP DFUOverSMP::sDFUOverSMP;
85116

86117
void DFUOverSMP::Init()
@@ -106,6 +137,7 @@ void DFUOverSMP::Init()
106137

107138
mgmt_callback_register(&sUploadCallback);
108139
mgmt_callback_register(&sCommandCallback);
140+
mgmt_callback_register(&sDfuStopped);
109141
}
110142

111143
void DFUOverSMP::ConfirmNewImage()
@@ -136,4 +168,19 @@ void DFUOverSMP::StartServer()
136168
ChipLogProgress(DeviceLayer, "DFU over SMP started");
137169
}
138170

171+
void DFUOverSMP::Disconnected(bt_conn *conn, uint8_t reason)
172+
{
173+
bt_conn_info btInfo;
174+
175+
VerifyOrReturn(sDfuInProgress);
176+
VerifyOrReturn(bt_conn_get_info(conn, &btInfo) == 0);
177+
178+
// Drop all callbacks incoming for the role other than peripheral, required by the Matter accessory
179+
VerifyOrReturn(btInfo.role == BT_CONN_ROLE_PERIPHERAL);
180+
181+
sDfuInProgress = false;
182+
183+
DFUSync::GetInstance().Free(sDfuSyncMutexId);
184+
}
185+
139186
} /* namespace Nrf */

samples/matter/common/src/dfu/smp/dfu_over_smp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class DFUOverSMP {
5050
*/
5151
void StartServer();
5252

53+
static void Disconnected(bt_conn *conn, uint8_t reason);
54+
5355
private:
5456
bool mIsStarted = false;
5557
chip::DeviceLayer::BLEAdvertisingArbiter::Request mAdvertisingRequest = {};

west.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ manifest:
160160
- name: matter
161161
repo-path: sdk-connectedhomeip
162162
path: modules/lib/matter
163-
revision: fd8e621006950f56c6cb54f670872d709d95b64f
163+
revision: 4d8e75cf312083c39be9523f2ff92bc8359bbf02
164164
west-commands: scripts/west/west-commands.yml
165165
submodules:
166166
- name: nlio

0 commit comments

Comments
 (0)