Skip to content

Commit 80df41c

Browse files
committed
Update GATT cache manager
Updates GATT cache manager. Signed-off-by: Mirko Covizzi <[email protected]>
1 parent c3c1648 commit 80df41c

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

lib/peer_manager/modules/gatt_cache_manager.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <stdint.h>
88
#include <zephyr/logging/log.h>
9+
#include <zephyr/sys/atomic.h>
910
#include <nrf_error.h>
1011
#include <ble_gap.h>
1112
#include <ble_err.h>
@@ -20,6 +21,58 @@
2021

2122
LOG_MODULE_REGISTER(gatt_cache_manager, CONFIG_PEER_MANAGER_LOG_LEVEL);
2223

24+
#define NRF_MTX_LOCKED 1
25+
#define NRF_MTX_UNLOCKED 0
26+
27+
typedef atomic_t nrf_mtx_t;
28+
29+
/**
30+
* @brief Initialize mutex.
31+
*
32+
* This function _must_ be called before nrf_mtx_trylock() and nrf_mtx_unlock() functions.
33+
*
34+
* @param[in, out] p_mtx The mutex to be initialized.
35+
*/
36+
__STATIC_INLINE void nrf_mtx_init(nrf_mtx_t *p_mtx)
37+
{
38+
ASSERT(p_mtx != NULL);
39+
40+
atomic_set(p_mtx, NRF_MTX_UNLOCKED);
41+
}
42+
43+
/**
44+
* @brief Try to lock a mutex.
45+
*
46+
* If the mutex is already held by another context, this function will return immediately.
47+
*
48+
* @param[in, out] p_mtx The mutex to be locked.
49+
* @return true if lock was acquired, false if not
50+
*/
51+
__STATIC_INLINE bool nrf_mtx_trylock(nrf_mtx_t *p_mtx)
52+
{
53+
ASSERT(p_mtx != NULL);
54+
55+
return atomic_cas(p_mtx, NRF_MTX_UNLOCKED, NRF_MTX_LOCKED);
56+
}
57+
58+
/**
59+
* @brief Unlock a mutex.
60+
*
61+
* This function _must_ only be called when holding the lock. Unlocking a mutex which you do not
62+
* hold will give undefined behavior.
63+
*
64+
* @note Unlock must happen from the same context as the one used to lock the mutex.
65+
*
66+
* @param[in, out] p_mtx The mutex to be unlocked.
67+
*/
68+
__STATIC_INLINE void nrf_mtx_unlock(nrf_mtx_t *p_mtx)
69+
{
70+
ASSERT(p_mtx != NULL);
71+
ASSERT(*p_mtx == NRF_MTX_LOCKED);
72+
73+
atomic_set(p_mtx, NRF_MTX_UNLOCKED);
74+
}
75+
2376
/* The number of registered event handlers. */
2477
#define GCM_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers)
2578

@@ -72,7 +125,8 @@ static int m_flag_car_handle_queried;
72125
static int m_flag_car_value_queried;
73126

74127
#ifdef CONFIG_PM_SERVICE_CHANGED_ENABLED
75-
STATIC_ASSERT(CONFIG_PM_SERVICE_CHANGED_ENABLED || !NRF_SDH_BLE_SERVICE_CHANGED,
128+
BUILD_ASSERT(IS_ENABLED(CONFIG_PM_SERVICE_CHANGED_ENABLED) ||
129+
!IS_ENABLED(CONFIG_NRF_SDH_BLE_SERVICE_CHANGED),
76130
"CONFIG_PM_SERVICE_CHANGED_ENABLED should be enabled "
77131
"if NRF_SDH_BLE_SERVICE_CHANGED is enabled.");
78132
#else

0 commit comments

Comments
 (0)