even if implementations doesn't makes it into final binarymaster
parent
a0faa63aae
commit
51c333fe69
@ -0,0 +1,414 @@ |
||||
/*
|
||||
* Copyright (c) Nordic Semiconductor ASA |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without modification, |
||||
* are permitted provided that the following conditions are met: |
||||
* |
||||
* 1. Redistributions of source code must retain the above copyright notice, this |
||||
* list of conditions and the following disclaimer. |
||||
* |
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this |
||||
* list of conditions and the following disclaimer in the documentation and/or |
||||
* other materials provided with the distribution. |
||||
* |
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
||||
* contributors to this software may be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "ble_conn_state.h" |
||||
#include <stdbool.h> |
||||
#include <stdint.h> |
||||
#include <string.h> |
||||
#include "ble.h" |
||||
#include "sdk_mapped_flags.h" |
||||
#include "app_error.h" |
||||
|
||||
|
||||
#if defined(__CC_ARM) |
||||
#pragma push |
||||
#pragma anon_unions |
||||
#elif defined(__ICCARM__) |
||||
#pragma language=extended |
||||
#elif defined(__GNUC__) |
||||
/* anonymous unions are enabled by default */ |
||||
#endif |
||||
|
||||
|
||||
#define BLE_CONN_STATE_N_DEFAULT_FLAGS 5 /**< The number of flags kept for each connection, excluding user flags. */ |
||||
#define BLE_CONN_STATE_N_FLAGS (BLE_CONN_STATE_N_DEFAULT_FLAGS + BLE_CONN_STATE_N_USER_FLAGS) /**< The number of flags kept for each connection, including user flags. */ |
||||
|
||||
|
||||
/**@brief Structure containing all the flag collections maintained by the Connection State module.
|
||||
*/ |
||||
typedef struct |
||||
{ |
||||
sdk_mapped_flags_t valid_flags; /**< Flags indicating which connection handles are valid. */ |
||||
sdk_mapped_flags_t connected_flags; /**< Flags indicating which connections are connected, since disconnected connection handles will not immediately be invalidated. */ |
||||
sdk_mapped_flags_t central_flags; /**< Flags indicating in which connections the local device is the central. */ |
||||
sdk_mapped_flags_t encrypted_flags; /**< Flags indicating which connections are encrypted. */ |
||||
sdk_mapped_flags_t mitm_protected_flags; /**< Flags indicating which connections have encryption with protection from man-in-the-middle attacks. */ |
||||
sdk_mapped_flags_t user_flags[BLE_CONN_STATE_N_USER_FLAGS]; /**< Flags that can be reserved by the user. The flags will be cleared when a connection is invalidated, otherwise, the user is wholly responsible for the flag states. */ |
||||
} ble_conn_state_flag_collections_t; |
||||
|
||||
|
||||
/**@brief Structure containing the internal state of the Connection State module.
|
||||
*/ |
||||
typedef struct |
||||
{ |
||||
uint16_t acquired_flags; /**< Bitmap for keeping track of which user flags have been acquired. */ |
||||
uint16_t valid_conn_handles[SDK_MAPPED_FLAGS_N_KEYS]; /**< List of connection handles used as keys for the sdk_mapped_flags module. */ |
||||
union |
||||
{ |
||||
ble_conn_state_flag_collections_t flags; /**< Flag collections kept by the Connection State module. */ |
||||
sdk_mapped_flags_t flag_array[BLE_CONN_STATE_N_FLAGS]; /**< Flag collections as array to allow use of @ref sdk_mapped_flags_bulk_update_by_key() when setting all flags. */ |
||||
}; |
||||
} ble_conn_state_t; |
||||
|
||||
|
||||
#if defined(__CC_ARM) |
||||
#pragma pop |
||||
#elif defined(__ICCARM__) |
||||
/* leave anonymous unions enabled */ |
||||
#elif defined(__GNUC__) |
||||
/* anonymous unions are enabled by default */ |
||||
#endif |
||||
|
||||
|
||||
static ble_conn_state_t m_bcs = {0}; /**< Instantiation of the internal state. */ |
||||
|
||||
|
||||
/**@brief Function for resetting all internal memory to the values it had at initialization.
|
||||
*/ |
||||
void bcs_internal_state_reset(void) |
||||
{ |
||||
memset( &m_bcs, 0, sizeof(ble_conn_state_t) ); |
||||
} |
||||
|
||||
|
||||
/**@brief Function for activating a connection record.
|
||||
* |
||||
* @param p_record The record to activate. |
||||
* @param conn_handle The connection handle to copy into the record. |
||||
* @param role The role of the connection. |
||||
* |
||||
* @return whether the record was activated successfully. |
||||
*/ |
||||
static bool record_activate(uint16_t conn_handle) |
||||
{ |
||||
uint16_t available_index = sdk_mapped_flags_first_key_index_get(~m_bcs.flags.valid_flags); |
||||
|
||||
if (available_index != SDK_MAPPED_FLAGS_INVALID_INDEX) |
||||
{ |
||||
m_bcs.valid_conn_handles[available_index] = conn_handle; |
||||
sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
||||
&m_bcs.flags.connected_flags, |
||||
conn_handle, |
||||
1); |
||||
sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
||||
&m_bcs.flags.valid_flags, |
||||
conn_handle, |
||||
1); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
|
||||
/**@brief Function for marking a connection record as invalid and resetting the values.
|
||||
* |
||||
* @param p_record The record to invalidate. |
||||
*/ |
||||
static void record_invalidate(uint16_t conn_handle) |
||||
{ |
||||
sdk_mapped_flags_bulk_update_by_key(m_bcs.valid_conn_handles, |
||||
m_bcs.flag_array, |
||||
BLE_CONN_STATE_N_FLAGS, |
||||
conn_handle, |
||||
0); |
||||
} |
||||
|
||||
|
||||
/**@brief Function for marking a connection as disconnected. See @ref BLE_CONN_STATUS_DISCONNECTED.
|
||||
* |
||||
* @param p_record The record of the connection to set as disconnected. |
||||
*/ |
||||
static void record_set_disconnected(uint16_t conn_handle) |
||||
{ |
||||
sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
||||
&m_bcs.flags.connected_flags, |
||||
conn_handle, |
||||
0); |
||||
} |
||||
|
||||
|
||||
/**@brief Function for invalidating records with a @ref BLE_CONN_STATUS_DISCONNECTED
|
||||
* connection status |
||||
*/ |
||||
static void record_purge_disconnected() |
||||
{ |
||||
sdk_mapped_flags_key_list_t disconnected_list; |
||||
|
||||
disconnected_list = sdk_mapped_flags_key_list_get( |
||||
m_bcs.valid_conn_handles, |
||||
(~m_bcs.flags.connected_flags) & (m_bcs.flags.valid_flags)); |
||||
|
||||
for (int i = 0; i < disconnected_list.len; i++) |
||||
{ |
||||
record_invalidate(disconnected_list.flag_keys[i]); |
||||
} |
||||
} |
||||
|
||||
|
||||
/**@brief Function for checking if a user flag has been acquired.
|
||||
* |
||||
* @param[in] flag_id Which flag to check. |
||||
* |
||||
* @return Whether the flag has been acquired. |
||||
*/ |
||||
static bool user_flag_is_acquired(ble_conn_state_user_flag_id_t flag_id) |
||||
{ |
||||
return ((m_bcs.acquired_flags & (1 << flag_id)) != 0); |
||||
} |
||||
|
||||
|
||||
/**@brief Function for marking a user flag as acquired.
|
||||
* |
||||
* @param[in] flag_id Which flag to mark. |
||||
*/ |
||||
static void user_flag_acquire(ble_conn_state_user_flag_id_t flag_id) |
||||
{ |
||||
m_bcs.acquired_flags |= (1 << flag_id); |
||||
} |
||||
|
||||
|
||||
void ble_conn_state_init(void) |
||||
{ |
||||
bcs_internal_state_reset(); |
||||
} |
||||
|
||||
|
||||
void ble_conn_state_on_ble_evt(ble_evt_t * p_ble_evt) |
||||
{ |
||||
switch (p_ble_evt->header.evt_id) |
||||
{ |
||||
case BLE_GAP_EVT_CONNECTED: |
||||
record_purge_disconnected(); |
||||
|
||||
if ( !record_activate(p_ble_evt->evt.gap_evt.conn_handle) ) |
||||
{ |
||||
// No more records available. Should not happen.
|
||||
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM); |
||||
} |
||||
else |
||||
{ |
||||
#if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110) |
||||
bool is_central = false; |
||||
#elif defined(TARGET_MCU_NRF51_16K_S120) || defined(TARGET_MCU_NRF51_32K_S120) |
||||
bool is_central = true; |
||||
#else |
||||
bool is_central = |
||||
(p_ble_evt->evt.gap_evt.params.connected.role == BLE_GAP_ROLE_CENTRAL); |
||||
#endif |
||||
|
||||
sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
||||
&m_bcs.flags.central_flags, |
||||
p_ble_evt->evt.gap_evt.conn_handle, |
||||
is_central); |
||||
} |
||||
|
||||
break; |
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED: |
||||
record_set_disconnected(p_ble_evt->evt.gap_evt.conn_handle); |
||||
break; |
||||
|
||||
case BLE_GAP_EVT_CONN_SEC_UPDATE: |
||||
sdk_mapped_flags_update_by_key( |
||||
m_bcs.valid_conn_handles, |
||||
&m_bcs.flags.encrypted_flags, |
||||
p_ble_evt->evt.gap_evt.conn_handle, |
||||
(p_ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv > 1)); |
||||
sdk_mapped_flags_update_by_key( |
||||
m_bcs.valid_conn_handles, |
||||
&m_bcs.flags.mitm_protected_flags, |
||||
p_ble_evt->evt.gap_evt.conn_handle, |
||||
(p_ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv > 2)); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
|
||||
bool ble_conn_state_valid(uint16_t conn_handle) |
||||
{ |
||||
return sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
||||
m_bcs.flags.valid_flags, |
||||
conn_handle); |
||||
} |
||||
|
||||
|
||||
uint8_t ble_conn_state_role(uint16_t conn_handle) |
||||
{ |
||||
uint8_t role = BLE_GAP_ROLE_INVALID; |
||||
|
||||
if ( sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, m_bcs.flags.valid_flags, conn_handle) ) |
||||
{ |
||||
bool central = sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
||||
m_bcs.flags.central_flags, |
||||
conn_handle); |
||||
|
||||
role = central ? BLE_GAP_ROLE_CENTRAL : BLE_GAP_ROLE_PERIPH; |
||||
} |
||||
|
||||
return role; |
||||
} |
||||
|
||||
|
||||
ble_conn_state_status_t ble_conn_state_status(uint16_t conn_handle) |
||||
{ |
||||
ble_conn_state_status_t conn_status = BLE_CONN_STATUS_INVALID; |
||||
bool valid = sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
||||
m_bcs.flags.valid_flags, |
||||
conn_handle); |
||||
|
||||
if (valid) |
||||
{ |
||||
bool connected = sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
||||
m_bcs.flags.connected_flags, |
||||
conn_handle); |
||||
|
||||
conn_status = connected ? BLE_CONN_STATUS_CONNECTED : BLE_CONN_STATUS_DISCONNECTED; |
||||
} |
||||
|
||||
return conn_status; |
||||
} |
||||
|
||||
|
||||
bool ble_conn_state_encrypted(uint16_t conn_handle) |
||||
{ |
||||
return sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
||||
m_bcs.flags.encrypted_flags, |
||||
conn_handle); |
||||
} |
||||
|
||||
|
||||
bool ble_conn_state_mitm_protected(uint16_t conn_handle) |
||||
{ |
||||
return sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
||||
m_bcs.flags.mitm_protected_flags, |
||||
conn_handle); |
||||
} |
||||
|
||||
|
||||
uint32_t ble_conn_state_n_connections(void) |
||||
{ |
||||
return sdk_mapped_flags_n_flags_set(m_bcs.flags.connected_flags); |
||||
} |
||||
|
||||
|
||||
uint32_t ble_conn_state_n_centrals(void) |
||||
{ |
||||
return sdk_mapped_flags_n_flags_set((m_bcs.flags.central_flags) & (m_bcs.flags.connected_flags)); |
||||
} |
||||
|
||||
|
||||
uint32_t ble_conn_state_n_peripherals(void) |
||||
{ |
||||
return sdk_mapped_flags_n_flags_set((~m_bcs.flags.central_flags) & (m_bcs.flags.connected_flags)); |
||||
} |
||||
|
||||
|
||||
sdk_mapped_flags_key_list_t ble_conn_state_conn_handles(void) |
||||
{ |
||||
return sdk_mapped_flags_key_list_get(m_bcs.valid_conn_handles, m_bcs.flags.valid_flags); |
||||
} |
||||
|
||||
|
||||
sdk_mapped_flags_key_list_t ble_conn_state_central_handles(void) |
||||
{ |
||||
return sdk_mapped_flags_key_list_get(m_bcs.valid_conn_handles, |
||||
(m_bcs.flags.central_flags) & (m_bcs.flags.connected_flags)); |
||||
} |
||||
|
||||
|
||||
sdk_mapped_flags_key_list_t ble_conn_state_periph_handles(void) |
||||
{ |
||||
return sdk_mapped_flags_key_list_get(m_bcs.valid_conn_handles, |
||||
(~m_bcs.flags.central_flags) & (m_bcs.flags.connected_flags)); |
||||
} |
||||
|
||||
|
||||
ble_conn_state_user_flag_id_t ble_conn_state_user_flag_acquire(void) |
||||
{ |
||||
for (ble_conn_state_user_flag_id_t i = BLE_CONN_STATE_USER_FLAG0; |
||||
i < BLE_CONN_STATE_N_USER_FLAGS; |
||||
i++) |
||||
{ |
||||
if ( !user_flag_is_acquired(i) ) |
||||
{ |
||||
user_flag_acquire(i); |
||||
return i; |
||||
} |
||||
} |
||||
|
||||
return BLE_CONN_STATE_USER_FLAG_INVALID; |
||||
} |
||||
|
||||
|
||||
bool ble_conn_state_user_flag_get(uint16_t conn_handle, ble_conn_state_user_flag_id_t flag_id) |
||||
{ |
||||
if (user_flag_is_acquired(flag_id)) |
||||
{ |
||||
return sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
||||
m_bcs.flags.user_flags[flag_id], |
||||
conn_handle); |
||||
} |
||||
else |
||||
{ |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
|
||||
void ble_conn_state_user_flag_set(uint16_t conn_handle, |
||||
ble_conn_state_user_flag_id_t flag_id, |
||||
bool value) |
||||
{ |
||||
if (user_flag_is_acquired(flag_id)) |
||||
{ |
||||
sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
||||
&m_bcs.flags.user_flags[flag_id], |
||||
conn_handle, |
||||
value); |
||||
} |
||||
} |
||||
|
||||
|
||||
sdk_mapped_flags_t ble_conn_state_user_flag_collection(ble_conn_state_user_flag_id_t flag_id) |
||||
{ |
||||
if ( user_flag_is_acquired(flag_id) ) |
||||
{ |
||||
return m_bcs.flags.user_flags[flag_id]; |
||||
} |
||||
else |
||||
{ |
||||
return 0; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,166 @@ |
||||
/*
|
||||
* Copyright (c) Nordic Semiconductor ASA |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without modification, |
||||
* are permitted provided that the following conditions are met: |
||||
* |
||||
* 1. Redistributions of source code must retain the above copyright notice, this |
||||
* list of conditions and the following disclaimer. |
||||
* |
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this |
||||
* list of conditions and the following disclaimer in the documentation and/or |
||||
* other materials provided with the distribution. |
||||
* |
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
||||
* contributors to this software may be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
|
||||
#include "peer_data.h" |
||||
|
||||
#include <stdint.h> |
||||
#include <string.h> |
||||
#include "peer_manager_types.h" |
||||
#include "fds.h" |
||||
|
||||
|
||||
|
||||
void peer_data_parts_get(pm_peer_data_const_t const * p_peer_data, fds_record_chunk_t * p_chunks, uint16_t * p_n_chunks) |
||||
{ |
||||
if (p_n_chunks == NULL) |
||||
{ |
||||
} |
||||
else if ((p_peer_data == NULL) || (p_chunks == NULL)) |
||||
{ |
||||
*p_n_chunks = 0; |
||||
} |
||||
else |
||||
{ |
||||
switch (p_peer_data->data_type) |
||||
{ |
||||
case PM_PEER_DATA_ID_BONDING: |
||||
p_chunks[0].p_data = p_peer_data->data.p_bonding_data; |
||||
p_chunks[0].length_words = p_peer_data->length_words; |
||||
*p_n_chunks = 1; |
||||
break; |
||||
case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING: |
||||
p_chunks[0].p_data = p_peer_data->data.p_service_changed_pending; |
||||
p_chunks[0].length_words = p_peer_data->length_words; |
||||
*p_n_chunks = 1; |
||||
break; |
||||
case PM_PEER_DATA_ID_GATT_LOCAL: |
||||
p_chunks[0].p_data = p_peer_data->data.p_local_gatt_db; |
||||
p_chunks[0].length_words = PM_N_WORDS(PM_LOCAL_DB_LEN_OVERHEAD_BYTES); |
||||
p_chunks[1].p_data = p_peer_data->data.p_local_gatt_db->p_data; |
||||
p_chunks[1].length_words = p_peer_data->length_words - p_chunks[0].length_words; |
||||
*p_n_chunks = 2; |
||||
break; |
||||
case PM_PEER_DATA_ID_GATT_REMOTE: |
||||
p_chunks[0].p_data = p_peer_data->data.p_remote_gatt_db; |
||||
p_chunks[0].length_words = PM_N_WORDS(PM_REMOTE_DB_LEN_OVERHEAD_BYTES); |
||||
p_chunks[1].p_data = p_peer_data->data.p_remote_gatt_db->p_data; |
||||
p_chunks[1].length_words = p_peer_data->length_words - p_chunks[0].length_words; |
||||
*p_n_chunks = 2; |
||||
break; |
||||
case PM_PEER_DATA_ID_APPLICATION: |
||||
p_chunks[0].p_data = p_peer_data->data.p_application_data; |
||||
p_chunks[0].length_words = p_peer_data->length_words; |
||||
*p_n_chunks = 1; |
||||
break; |
||||
default: |
||||
*p_n_chunks = 0; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
ret_code_t peer_data_deserialize(pm_peer_data_flash_t const * p_in_data, pm_peer_data_t * p_out_data) |
||||
{ |
||||
if ((p_in_data == NULL) || (p_out_data == NULL)) |
||||
{ |
||||
return NRF_ERROR_NULL; |
||||
} |
||||
else |
||||
{ |
||||
if (p_out_data->length_words < p_in_data->length_words) |
||||
{ |
||||
p_out_data->length_words = p_in_data->length_words; |
||||
return NRF_ERROR_NO_MEM; |
||||
} |
||||
p_out_data->length_words = p_in_data->length_words; |
||||
p_out_data->data_type = p_in_data->data_type; |
||||
|
||||
switch (p_in_data->data_type) |
||||
{ |
||||
case PM_PEER_DATA_ID_BONDING: |
||||
*p_out_data->data.p_bonding_data = *p_in_data->data.p_bonding_data; |
||||
break; |
||||
case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING: |
||||
*p_out_data->data.p_service_changed_pending = *p_in_data->data.p_service_changed_pending; |
||||
break; |
||||
case PM_PEER_DATA_ID_GATT_LOCAL: |
||||
if (p_out_data->data.p_local_gatt_db->p_data == NULL) |
||||
{ |
||||
return NRF_ERROR_NULL; |
||||
} |
||||
if (p_out_data->data.p_local_gatt_db->len < p_in_data->data.p_local_gatt_db->len) |
||||
{ |
||||
p_out_data->data.p_local_gatt_db->len = p_in_data->data.p_local_gatt_db->len; |
||||
return NRF_ERROR_NO_MEM; |
||||
} |
||||
else |
||||
{ |
||||
p_out_data->data.p_local_gatt_db->flags = p_in_data->data.p_local_gatt_db->flags; |
||||
p_out_data->data.p_local_gatt_db->len = p_in_data->data.p_local_gatt_db->len; |
||||
memcpy(p_out_data->data.p_local_gatt_db->p_data, |
||||
p_in_data->data.p_local_gatt_db->p_data, |
||||
p_in_data->data.p_local_gatt_db->len); |
||||
} |
||||
break; |
||||
case PM_PEER_DATA_ID_GATT_REMOTE: |
||||
if (p_out_data->data.p_remote_gatt_db->p_data == NULL) |
||||
{ |
||||
return NRF_ERROR_NULL; |
||||
} |
||||
if (p_out_data->data.p_remote_gatt_db->service_count < p_in_data->data.p_remote_gatt_db->service_count) |
||||
{ |
||||
p_out_data->data.p_remote_gatt_db->service_count = p_in_data->data.p_remote_gatt_db->service_count; |
||||
return NRF_ERROR_NO_MEM; |
||||
} |
||||
else |
||||
{ |
||||
p_out_data->data.p_remote_gatt_db->service_count = p_in_data->data.p_remote_gatt_db->service_count; |
||||
memcpy(p_out_data->data.p_remote_gatt_db->p_data, |
||||
p_in_data->data.p_remote_gatt_db->p_data, |
||||
p_in_data->data.p_remote_gatt_db->service_count * sizeof(ble_gatt_db_srv_t)); |
||||
} |
||||
break; |
||||
case PM_PEER_DATA_ID_APPLICATION: |
||||
memcpy(p_out_data->data.p_application_data, |
||||
p_in_data->data.p_application_data, |
||||
p_in_data->length_words * 4); |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
} |
||||
return NRF_SUCCESS; |
||||
} |
||||
|
||||
|
@ -0,0 +1,73 @@ |
||||
/*
|
||||
* Copyright (c) Nordic Semiconductor ASA |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without modification, |
||||
* are permitted provided that the following conditions are met: |
||||
* |
||||
* 1. Redistributions of source code must retain the above copyright notice, this |
||||
* list of conditions and the following disclaimer. |
||||
* |
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this |
||||
* list of conditions and the following disclaimer in the documentation and/or |
||||
* other materials provided with the distribution. |
||||
* |
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
||||
* contributors to this software may be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
|
||||
#ifndef PEER_DATA_H__ |
||||
#define PEER_DATA_H__ |
||||
|
||||
#include <stdint.h> |
||||
#include "peer_manager_types.h" |
||||
#include "fds.h" |
||||
|
||||
|
||||
/**
|
||||
* @defgroup peer_data Peer Data |
||||
* @ingroup peer_manager |
||||
* @{ |
||||
* @brief An internal module of @ref peer_manager. This module defines the structure of the data |
||||
* that is managed by the @ref peer_manager. It also provides functions for parsing the data. |
||||
*/ |
||||
|
||||
|
||||
/**@brief Function for enumerating the separate (non-contiguous) parts of the peer data.
|
||||
* |
||||
* @param[in] p_peer_data The peer data to enumerate. |
||||
* @param[out] p_chunks The resulting chunks. This must be an array of at least 2 elements. |
||||
* @param[out] p_n_chunks The number of chunks. If this is 0, something went wrong. |
||||
*/ |
||||
void peer_data_parts_get(pm_peer_data_const_t const * p_peer_data, fds_record_chunk_t * p_chunks, uint16_t * p_n_chunks); |
||||
|
||||
|
||||
/**@brief Function for converting @ref pm_peer_data_flash_t into @ref pm_peer_data_t.
|
||||
* |
||||
* @param[in] p_in_data The source data. |
||||
* @param[out] p_out_data The target data structure. |
||||
* |
||||
* @retval NRF_SUCCESS Successful conversion. |
||||
* @retval NRF_ERROR_NULL A parameter was NULL. |
||||
* @retval NRF_ERROR_NO_MEM A buffer was not large enough. |
||||
*/ |
||||
ret_code_t peer_data_deserialize(pm_peer_data_flash_t const * p_in_data, pm_peer_data_t * p_out_data); |
||||
|
||||
/** @} */ |
||||
|
||||
#endif /* PEER_DATA_H__ */ |
@ -0,0 +1,688 @@ |
||||
/*
|
||||
* Copyright (c) Nordic Semiconductor ASA |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without modification, |
||||
* are permitted provided that the following conditions are met: |
||||
* |
||||
* 1. Redistributions of source code must retain the above copyright notice, this |
||||
* list of conditions and the following disclaimer. |
||||
* |
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this |
||||
* list of conditions and the following disclaimer in the documentation and/or |
||||
* other materials provided with the distribution. |
||||
* |
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
||||
* contributors to this software may be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
|
||||
#include "peer_data_storage.h" |
||||
|
||||
#include <stdint.h> |
||||
#include <string.h> |
||||
#include "sdk_errors.h" |
||||
#include "peer_manager_types.h" |
||||
#include "peer_id.h" |
||||
#include "peer_data.h" |
||||
#include "fds.h" |
||||
|
||||
#define MAX_REGISTRANTS 6 /**< The number of user that can register with the module. */ |
||||
|
||||
#define MODULE_INITIALIZED (m_pds.n_registrants > 0) /**< Expression which is true when the module is initialized. */ |
||||
|
||||
/**@brief Macro for verifying that the module is initialized. It will cause the function to return
|
||||
* @ref NRF_ERROR_INVALID_STATE if not. |
||||
*/ |
||||
#define VERIFY_MODULE_INITIALIZED() \ |
||||
do \
|
||||
{ \
|
||||
if (!MODULE_INITIALIZED) \
|
||||
{ \
|
||||
return NRF_ERROR_INVALID_STATE; \
|
||||
} \
|
||||
} while(0) |
||||
|
||||
|
||||
/**@brief Macro for verifying that the module is initialized. It will cause the function to return
|
||||
* if not. |
||||
*/ |
||||
#define VERIFY_MODULE_INITIALIZED_VOID() \ |
||||
do \
|
||||
{ \
|
||||
if (!MODULE_INITIALIZED) \
|
||||
{ \
|
||||
return; \
|
||||
} \
|
||||
} while(0) |
||||
|
||||
|
||||
/**@brief Macro for verifying that the param is not NULL. It will cause the function to return
|
||||
* if not. |
||||
* |
||||
* @param[in] param The variable to check if is NULL. |
||||
*/ |
||||
#define VERIFY_PARAM_NOT_NULL(param) \ |
||||
do \
|
||||
{ \
|
||||
if (param == NULL) \
|
||||
{ \
|
||||
return NRF_ERROR_NULL; \
|
||||
} \
|
||||
} while(0) |
||||
|
||||
|
||||
/**@brief Macro for verifying that param is not zero. It will cause the function to return
|
||||
* if not. |
||||
* |
||||
* @param[in] param The variable to check if is zero. |
||||
*/ |
||||
#define VERIFY_PARAM_NOT_ZERO(param) \ |
||||
do \
|
||||
{ \
|
||||
if (param == 0) \
|
||||
{ \
|
||||
return NRF_ERROR_NULL; \
|
||||
} \
|
||||
} while(0) |
||||
|
||||
|
||||
/**@brief Macro for verifying that the peer id is within a valid range
|
||||
* |
||||
* @param[in] id The peer data id to check. |
||||
*/ |
||||
#define VERIFY_PEER_ID_IN_RANGE(id) \ |
||||
do \
|
||||
{ \
|
||||
if ((id >= PM_PEER_ID_N_AVAILABLE_IDS)) \
|
||||
{ \
|
||||
return NRF_ERROR_INVALID_PARAM; \
|
||||
} \
|
||||
} while (0) |
||||
|
||||
|
||||
/**@brief Macro for verifying that the peer data id is withing a valid range
|
||||
* |
||||
* @param[in] id The peer data id to check. |
||||
*/ |
||||
#define VERIFY_PEER_DATA_ID_IN_RANGE(id) \ |
||||
do \
|
||||
{ \
|
||||
if (!PM_PEER_DATA_ID_IS_VALID(id)) \
|
||||
{ \
|
||||
return NRF_ERROR_INVALID_PARAM; \
|
||||
} \
|
||||
} while (0) |
||||
|
||||
|
||||
#define PEER_IDS_INITIALIZE() \ |
||||
do \
|
||||
{ \
|
||||
if (!m_pds.peer_ids_initialized) \
|
||||
{ \
|
||||
peer_ids_init(); \
|
||||
} \
|
||||
} while (0) |
||||
|
||||
|
||||
typedef struct |
||||
{ |
||||
bool peer_ids_initialized; |
||||
pds_evt_handler_t evt_handlers[MAX_REGISTRANTS]; |
||||
uint8_t n_registrants; |
||||
} pds_t; |
||||
|
||||
static pds_t m_pds = {.n_registrants = 0}; |
||||
|
||||
static void internal_state_reset(pds_t * p_pds) |
||||
{ |
||||
memset(p_pds, 0, sizeof(pds_t)); |
||||
} |
||||
|
||||
/**@brief Function for dispatching outbound events to all registered event handlers.
|
||||
* |
||||
* @param[in] p_event The event to dispatch. |
||||
*/ |
||||
static void pds_evt_send(pds_evt_t * p_event) |
||||
{ |
||||
for (int i = 0; i < m_pds.n_registrants; i++) |
||||
{ |
||||
m_pds.evt_handlers[i](p_event); |
||||
} |
||||
} |
||||
|
||||
/**@brief Function to convert peer id to instance id
|
||||
* |
||||
* @param[in] peer_id Peer id to convert to instance id |
||||
* |
||||
* @return Value as instance id |
||||
*/ |
||||
static fds_instance_id_t convert_peer_id_to_instance_id(pm_peer_id_t peer_id) |
||||
{ |
||||
return (fds_instance_id_t)(peer_id + peer_id_to_instance_id); |
||||
} |
||||
|
||||
/**@brief Function to convert peer data id to type id
|
||||
* |
||||
* @param[in] peer_data_id Peer data id to convert to type_id |
||||
* |
||||
* @return Value as type id |
||||
*/ |
||||
static fds_type_id_t convert_peer_data_id_to_type_id(pm_peer_data_id_t peer_data_id) |
||||
{ |
||||
return (fds_type_id_t)peer_data_id + (fds_type_id_t)peer_data_id_to_type_id; |
||||
} |
||||
|
||||
|
||||
/**@brief Function to convert peer data id to type id
|
||||
* |
||||
* @param[in] peer_data_id Peer data id to convert to type_id |
||||
* |
||||
* @return Value as type id |
||||
*/ |
||||
static pm_peer_id_t convert_instance_id_to_peer_id(fds_instance_id_t instance_id) |
||||
{ |
||||
return (pm_peer_id_t)(instance_id + instance_id_to_peer_id); |
||||
} |
||||
|
||||
|
||||
/**@brief Function to type id to peer data id
|
||||
* |
||||
* @param[in] type_id Type id to convert to peer data id |
||||
* |
||||
* @return Value as peer data id |
||||
*/ |
||||
static pm_peer_data_id_t convert_type_id_to_peer_data_id(fds_type_id_t type_id) |
||||
{ |
||||
return (pm_peer_data_id_t)(type_id + instance_id_to_peer_id); |
||||
} |
||||
|
||||
|
||||
static ret_code_t find_fds_item(pm_peer_id_t peer_id, |
||||
pm_peer_data_id_t data_id, |
||||
fds_record_desc_t * const p_desc) |
||||
{ |
||||
fds_find_token_t find_tok; |
||||
|
||||
VERIFY_PEER_ID_IN_RANGE(peer_id); |
||||
VERIFY_PEER_DATA_ID_IN_RANGE(data_id); |
||||
// pp_record verified outside
|
||||
|
||||
fds_type_id_t type_id = convert_peer_data_id_to_type_id(data_id); |
||||
fds_instance_id_t instance_id = convert_peer_id_to_instance_id(peer_id); |
||||
|
||||
return fds_find(type_id, instance_id, p_desc, &find_tok); |
||||
} |
||||
|
||||
|
||||
static void peer_ids_init() |
||||
{ |
||||
fds_record_t record; |
||||
fds_record_desc_t record_desc; |
||||
fds_find_token_t find_tok; |
||||
fds_type_id_t const type_id = convert_peer_data_id_to_type_id(PM_PEER_DATA_ID_BONDING); |
||||
pm_peer_id_t peer_id; |
||||
|
||||
if (!m_pds.peer_ids_initialized) |
||||
{ |
||||
while(fds_find_by_type(type_id, &record_desc, &find_tok) == NRF_SUCCESS) |
||||
{ |
||||
fds_open(&record_desc, &record); |
||||
fds_close(&record_desc); |
||||
peer_id = convert_instance_id_to_peer_id(record.header.ic.instance); |
||||
peer_id_allocate(peer_id); |
||||
} |
||||
|
||||
m_pds.peer_ids_initialized = true; |
||||
} |
||||
} |
||||
|
||||
//uint32_t size_pad_to_mult_of_four(uint32_t unpadded_size)
|
||||
//{
|
||||
// return (unpadded_size + 3) & 3;
|
||||
//}
|
||||
|
||||
static void fds_evt_handler(ret_code_t result, |
||||
fds_cmd_id_t cmd, |
||||
fds_record_id_t record_id, |
||||
fds_record_key_t record_key |
||||
/*fds_record_t const * const p_record*/) |
||||
{ |
||||
pds_evt_t evt; |
||||
switch(cmd) |
||||
{ |
||||
case FDS_CMD_INIT: |
||||
|
||||
break; |
||||
|
||||
case FDS_CMD_UPDATE: |
||||
case FDS_CMD_WRITE: |
||||
evt.peer_id = convert_instance_id_to_peer_id(record_key.instance); |
||||
evt.evt_id = (result == NRF_SUCCESS) ? PDS_EVT_STORED : PDS_EVT_ERROR_STORE; |
||||
evt.data_id = convert_type_id_to_peer_data_id(record_key.type); |
||||
evt.store_token = record_id; |
||||
pds_evt_send(&evt); |
||||
break; |
||||
|
||||
case FDS_CMD_CLEAR: |
||||
evt.peer_id = convert_instance_id_to_peer_id(record_key.instance); |
||||
evt.evt_id = (result == NRF_SUCCESS) ? PDS_EVT_CLEARED : PDS_EVT_ERROR_CLEAR; |
||||
evt.data_id = convert_type_id_to_peer_data_id(record_key.type); |
||||
evt.store_token = record_id; |
||||
pds_evt_send(&evt); |
||||
break; |
||||
|
||||
case FDS_CMD_CLEAR_INST: |
||||
{ |
||||
if ((record_key.type == FDS_TYPE_ID_INVALID) && |
||||
(record_key.instance != FDS_TYPE_ID_INVALID)) |
||||
{ |
||||
pm_peer_id_t peer_id = convert_instance_id_to_peer_id(record_key.instance); |
||||
|
||||
evt.peer_id = peer_id; |
||||
evt.data_id = PM_PEER_DATA_ID_INVALID; |
||||
if (result == NRF_SUCCESS) |
||||
{ |
||||
evt.evt_id = PDS_EVT_PEER_ID_CLEAR; |
||||
peer_id_free(peer_id); |
||||
} |
||||
else |
||||
{ |
||||
evt.evt_id = PDS_EVT_ERROR_PEER_ID_CLEAR; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
// TODO: Not supported yet (clear many without clearing peer_id)
|
||||
} |
||||
|
||||
pds_evt_send(&evt); |
||||
} |
||||
break; |
||||
|
||||
case FDS_CMD_GC: |
||||
evt.peer_id = convert_instance_id_to_peer_id(record_key.instance); |
||||
evt.evt_id = PDS_EVT_COMPRESSED; |
||||
evt.data_id = convert_type_id_to_peer_data_id(record_key.type); |
||||
evt.store_token = record_id; |
||||
pds_evt_send(&evt); |
||||
break; |
||||
|
||||
default: |
||||
|
||||
break; |
||||
} |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_register(pds_evt_handler_t evt_handler) |
||||
{ |
||||
if (m_pds.n_registrants >= MAX_REGISTRANTS) |
||||
{ |
||||
return NRF_ERROR_NO_MEM; |
||||
} |
||||
|
||||
VERIFY_PARAM_NOT_NULL(evt_handler); |
||||
|
||||
if (!MODULE_INITIALIZED) |
||||
{ |
||||
ret_code_t retval; |
||||
internal_state_reset(&m_pds); |
||||
peer_id_init(); |
||||
|
||||
fds_cb_t cb = fds_evt_handler; |
||||
retval = fds_register(cb); |
||||
if(retval != NRF_SUCCESS) |
||||
{ |
||||
return retval; |
||||
} |
||||
|
||||
retval = fds_init(); |
||||
if(retval != NRF_SUCCESS) |
||||
{ |
||||
return retval; |
||||
} |
||||
} |
||||
|
||||
m_pds.evt_handlers[m_pds.n_registrants] = evt_handler; |
||||
m_pds.n_registrants += 1; |
||||
|
||||
return NRF_SUCCESS; |
||||
|
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_read_ptr_get(pm_peer_id_t peer_id, |
||||
pm_peer_data_id_t data_id, |
||||
pm_peer_data_flash_t * p_data, |
||||
pm_store_token_t * p_token) |
||||
{ |
||||
ret_code_t retval; |
||||
|
||||
fds_record_t record; |
||||
fds_record_desc_t record_desc; |
||||
|
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PEER_ID_IN_RANGE(peer_id); |
||||
VERIFY_PEER_DATA_ID_IN_RANGE(data_id); |
||||
|
||||
retval = find_fds_item(peer_id, data_id, &record_desc); |
||||
if (retval != NRF_SUCCESS) |
||||
{ |
||||
return retval; |
||||
} |
||||
|
||||
// Shouldn't fail, unless record is cleared.
|
||||
fds_open(&record_desc, &record); |
||||
// No need to keep it open, since we are not reading.
|
||||
fds_close(&record_desc); |
||||
|
||||
//NRF_LOG_PRINTF("Found item with peer_id: %d, data_id: %d, Address: %p\r\n", record.p_data);
|
||||
|
||||
if (p_data != NULL) |
||||
{ |
||||
p_data->data_type = data_id; |
||||
p_data->length_words = record.header.tl.length_words; |
||||
|
||||
p_data->data.p_application_data = (uint8_t const*)record.p_data; |
||||
} |
||||
|
||||
if (p_token != NULL) |
||||
{ |
||||
*p_token = (uint32_t)record.header.id; |
||||
} |
||||
|
||||
return retval; |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_lock(pm_store_token_t store_token) |
||||
{ |
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PARAM_NOT_ZERO(store_token); |
||||
|
||||
// TODO: Not implemented yet in fds
|
||||
|
||||
return NRF_SUCCESS; |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_verify(pm_store_token_t store_token) |
||||
{ |
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PARAM_NOT_ZERO(store_token); |
||||
|
||||
// TODO: Not implemented yet in fds
|
||||
|
||||
return NRF_SUCCESS; |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_read(pm_peer_id_t peer_id, |
||||
pm_peer_data_id_t data_id, |
||||
pm_peer_data_t * p_data, |
||||
fds_length_t * p_len_words) |
||||
{ |
||||
VERIFY_PEER_ID_IN_RANGE(peer_id); |
||||
VERIFY_PEER_DATA_ID_IN_RANGE(data_id); |
||||
VERIFY_PARAM_NOT_NULL(p_len_words); |
||||
VERIFY_PARAM_NOT_NULL(p_data); |
||||
|
||||
ret_code_t err_code; |
||||
pm_peer_data_flash_t peer_data_flash; |
||||
|
||||
err_code = pds_peer_data_read_ptr_get(peer_id, data_id, &peer_data_flash, NULL); |
||||
|
||||
if (err_code != NRF_SUCCESS) |
||||
{ |
||||
return err_code; |
||||
} |
||||
|
||||
if ((*p_len_words) == 0) |
||||
{ |
||||
(*p_len_words) = peer_data_flash.length_words; |
||||
return NRF_SUCCESS; |
||||
} |
||||
else if ((*p_len_words) < peer_data_flash.length_words) |
||||
{ |
||||
return NRF_ERROR_NO_MEM; |
||||
} |
||||
|
||||
VERIFY_PARAM_NOT_NULL(p_data->data.p_application_data); |
||||
|
||||
err_code = peer_data_deserialize(&peer_data_flash, p_data); |
||||
|
||||
return err_code; |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_write_prepare(pm_peer_data_const_t const * p_peer_data, |
||||
pm_prepare_token_t * p_prepare_token) |
||||
{ |
||||
ret_code_t retval; |
||||
|
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PARAM_NOT_NULL(p_peer_data); |
||||
VERIFY_PARAM_NOT_NULL(p_prepare_token); |
||||
VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type); |
||||
|
||||
retval = fds_reserve((fds_write_token_t*)p_prepare_token, p_peer_data->length_words); |
||||
return retval; |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_write_prepare_cancel(pm_prepare_token_t prepare_token) |
||||
{ |
||||
ret_code_t retval; |
||||
|
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PARAM_NOT_ZERO(prepare_token); |
||||
|
||||
retval = fds_reserve_cancel((fds_write_token_t*)&prepare_token); |
||||
return retval; |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_write_prepared(pm_peer_id_t peer_id, |
||||
pm_peer_data_const_t const * p_peer_data, |
||||
pm_prepare_token_t prepare_token, |
||||
pm_store_token_t * p_store_token) |
||||
{ |
||||
ret_code_t retval; |
||||
fds_record_desc_t record_desc; |
||||
fds_record_key_t record_key; |
||||
fds_record_chunk_t chunks[2]; |
||||
uint16_t n_chunks; |
||||
|
||||
VERIFY_MODULE_INITIALIZED(); |
||||
//VERIFY_PARAM_NOT_ZERO(prepare_token);
|
||||
VERIFY_PARAM_NOT_NULL(p_peer_data); |
||||
VERIFY_PEER_ID_IN_RANGE(peer_id); |
||||
VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type); |
||||
|
||||
// Fill in the keys.
|
||||
record_key.type = convert_peer_data_id_to_type_id(p_peer_data->data_type); |
||||
record_key.instance = convert_peer_id_to_instance_id(peer_id); |
||||
|
||||
// Create chunks.
|
||||
peer_data_parts_get(p_peer_data, chunks, &n_chunks); |
||||
|
||||
retval = fds_write_reserved((fds_write_token_t*)&prepare_token, &record_desc, |
||||
record_key, n_chunks, chunks); |
||||
|
||||
if ((retval == NRF_SUCCESS) && (p_store_token != NULL)) |
||||
{ |
||||
fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token); |
||||
} |
||||
|
||||
return retval; |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_write(pm_peer_id_t peer_id, |
||||
pm_peer_data_const_t const * p_peer_data, |
||||
pm_store_token_t * p_store_token) |
||||
{ |
||||
ret_code_t retval; |
||||
fds_record_desc_t record_desc; |
||||
fds_record_key_t record_key; |
||||
fds_record_chunk_t chunks[2]; |
||||
uint16_t n_chunks; |
||||
|
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PEER_ID_IN_RANGE(peer_id); |
||||
VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type); |
||||
|
||||
// Fill in the keys.
|
||||
record_key.type = convert_peer_data_id_to_type_id(p_peer_data->data_type); |
||||
record_key.instance = convert_peer_id_to_instance_id(peer_id); |
||||
|
||||
// Create chunks
|
||||
peer_data_parts_get(p_peer_data, chunks, &n_chunks); |
||||
|
||||
// Request write
|
||||
retval = fds_write(&record_desc, record_key, n_chunks, chunks); |
||||
|
||||
if ((retval == NRF_SUCCESS) && (p_store_token != NULL)) |
||||
{ |
||||
fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token); |
||||
} |
||||
|
||||
return retval; |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_data_update(pm_peer_id_t peer_id, |
||||
pm_peer_data_const_t const * p_peer_data, |
||||
pm_store_token_t old_token, |
||||
pm_store_token_t * p_store_token) |
||||
{ |
||||
ret_code_t retval; |
||||
fds_record_desc_t record_desc; |
||||
fds_record_key_t record_key; |
||||
fds_record_chunk_t chunks[2]; |
||||
uint16_t n_chunks; |
||||
|
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_type); |
||||
VERIFY_PARAM_NOT_NULL(p_peer_data); |
||||
|
||||
record_key.type = convert_peer_data_id_to_type_id(p_peer_data->data_type); |
||||
record_key.instance = convert_peer_id_to_instance_id(peer_id); |
||||
|
||||
// Create chunks
|
||||
peer_data_parts_get(p_peer_data, chunks, &n_chunks); |
||||
|
||||
fds_descriptor_from_rec_id(&record_desc, (fds_record_id_t)old_token); |
||||
|
||||
retval = fds_update(&record_desc, record_key, n_chunks, chunks); |
||||
|
||||
if ((retval == NRF_SUCCESS) && (p_store_token != NULL)) |
||||
{ |
||||
fds_record_id_from_desc(&record_desc, (fds_record_id_t*)p_store_token); |
||||
} |
||||
|
||||
return retval; |
||||
} |
||||
|
||||
ret_code_t pds_peer_data_clear(pm_peer_id_t peer_id, pm_peer_data_id_t data_id) |
||||
{ |
||||
ret_code_t retval; |
||||
fds_type_id_t type_id; |
||||
fds_instance_id_t instance_id; |
||||
fds_record_desc_t record_desc; |
||||
fds_find_token_t find_tok; |
||||
|
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PEER_ID_IN_RANGE(peer_id); |
||||
VERIFY_PEER_DATA_ID_IN_RANGE(data_id); |
||||
|
||||
type_id = convert_peer_data_id_to_type_id(data_id); |
||||
instance_id = convert_peer_id_to_instance_id(peer_id); |
||||
|
||||
retval = fds_find(type_id, instance_id, &record_desc, &find_tok); |
||||
if(retval != NRF_SUCCESS) |
||||
{ |
||||
return retval; |
||||
} |
||||
|
||||
retval = fds_clear(&record_desc); |
||||
return retval; |
||||
} |
||||
|
||||
|
||||
pm_peer_id_t pds_peer_id_allocate(void) |
||||
{ |
||||
if (!MODULE_INITIALIZED) |
||||
{ |
||||
return PM_PEER_ID_INVALID; |
||||
} |
||||
PEER_IDS_INITIALIZE(); |
||||
return peer_id_allocate(PM_PEER_ID_INVALID); |
||||
} |
||||
|
||||
|
||||
ret_code_t pds_peer_id_free(pm_peer_id_t peer_id) |
||||
{ |
||||
ret_code_t retval; |
||||
fds_instance_id_t instance_id; |
||||
|
||||
VERIFY_MODULE_INITIALIZED(); |
||||
VERIFY_PEER_ID_IN_RANGE(peer_id); |
||||
PEER_IDS_INITIALIZE(); |
||||
|
||||
instance_id = convert_peer_id_to_instance_id(peer_id); |
||||
|
||||
retval = fds_clear_by_instance(instance_id); |
||||
return retval; |
||||
} |
||||
|
||||
|
||||
bool pds_peer_id_is_allocated(pm_peer_id_t peer_id) |
||||
{ |
||||
if (!MODULE_INITIALIZED) |
||||
{ |
||||
return false; |
||||
} |
||||
PEER_IDS_INITIALIZE(); |
||||
|
||||
return peer_id_is_allocated(peer_id); |
||||
} |
||||
|
||||
|
||||
pm_peer_id_t pds_next_peer_id_get(pm_peer_id_t prev_peer_id) |
||||
{ |
||||
if (!MODULE_INITIALIZED) |
||||
{ |
||||
return PM_PEER_ID_INVALID; |
||||
} |
||||
PEER_IDS_INITIALIZE(); |
||||
|
||||
return peer_id_next_id_get(prev_peer_id); |
||||
} |
||||
|
||||
|
||||
uint32_t pds_n_peers(void) |
||||
{ |
||||
if (!MODULE_INITIALIZED) |
||||
{ |
||||
return 0; |
||||
} |
||||
PEER_IDS_INITIALIZE(); |
||||
return peer_id_n_ids(); |
||||
} |
||||
|
@ -0,0 +1,370 @@ |
||||
/*
|
||||
* Copyright (c) Nordic Semiconductor ASA |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without modification, |
||||
* are permitted provided that the following conditions are met: |
||||
* |
||||
* 1. Redistributions of source code must retain the above copyright notice, this |
||||
* list of conditions and the following disclaimer. |
||||
* |
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this |
||||
* list of conditions and the following disclaimer in the documentation and/or |
||||
* other materials provided with the distribution. |
||||
* |
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
||||
* contributors to this software may be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
|
||||
#ifndef PEER_DATA_STORAGE_H__ |
||||
#define PEER_DATA_STORAGE_H__ |
||||
|
||||
|
||||
#include "stdint.h" |
||||
#include "sdk_errors.h" |
||||
#include "ble_gap.h" |
||||
#include "peer_manager_types.h" |
||||
#include "fds.h" |
||||
|
||||
|
||||
/**
|
||||
* |