-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[nrf toup][nrfconnect] Added module to synchronize DFU processes
Introduced a module that can be used to synchronize DFU processes and make it mutual exclusive (prevent concurrent DFU runs using different protocols, e.g Matter OTA and DFU over BT SMP). Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no>
- Loading branch information
1 parent
fd8e621
commit 21e6e78
Showing
6 changed files
with
115 additions
and
1 deletion.
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
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
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,33 @@ | ||
#include "DFUSync.h" | ||
|
||
CHIP_ERROR DFUSync::Take(uint32_t & id) | ||
{ | ||
if (mIsTaken) | ||
{ | ||
if (id == mOwnerId) | ||
{ | ||
return CHIP_NO_ERROR; | ||
} | ||
return CHIP_ERROR_BUSY; | ||
} | ||
|
||
mIsTaken = true; | ||
/* Increment owner id to make sure that every allocation is unique. */ | ||
mOwnerId++; | ||
id = mOwnerId; | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
CHIP_ERROR DFUSync::Free(uint32_t id) | ||
{ | ||
/* Prevent free operation from the threads that do not own mutex. */ | ||
if (id != mOwnerId) | ||
{ | ||
return CHIP_ERROR_ACCESS_DENIED; | ||
} | ||
|
||
mIsTaken = false; | ||
|
||
return CHIP_NO_ERROR; | ||
} |
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,60 @@ | ||
/* | ||
* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <lib/core/CHIPError.h> | ||
#include <atomic> | ||
|
||
class DFUSync | ||
{ | ||
public: | ||
/** | ||
* @brief Tries to take a mutex allowing to perform the DFU process. | ||
* | ||
* @param id reference to the mutex owner id that is assigned with random id, if the mutex was taken successfully | ||
* | ||
* @return CHIP_NO_ERROR on success, the other error code on failure. | ||
*/ | ||
CHIP_ERROR Take(uint32_t & id); | ||
|
||
/** | ||
* @brief Tries to free a mutex allowing to perform the DFU process. | ||
* | ||
* @param id mutex owner id that has to be equal to the id assigned by Take method to prevent free attempts from the other | ||
* threads that do not own the mutex. | ||
* | ||
* @return CHIP_NO_ERROR on success, the other error code on failure. | ||
*/ | ||
CHIP_ERROR Free(uint32_t id); | ||
|
||
/** | ||
* @brief Get the DFUSync instance | ||
* | ||
* @return DFUSync object | ||
*/ | ||
static inline DFUSync & GetInstance() | ||
{ | ||
static DFUSync sInstance; | ||
return sInstance; | ||
} | ||
|
||
private: | ||
/* Mutex to synchronize the DFU operations. */ | ||
std::atomic<bool> mIsTaken = false; | ||
uint32_t mOwnerId = 0; | ||
}; |
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
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