R1 code for micro:bit based train controller code, requires second micro:bit running rx code to operate - see https://meanderingpi.wordpress.com/ for more information
Fork of nrf51-sdk by
peer_data.c
00001 /* 00002 * Copyright (c) Nordic Semiconductor ASA 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without modification, 00006 * are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, this 00009 * list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, this 00012 * list of conditions and the following disclaimer in the documentation and/or 00013 * other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other 00016 * contributors to this software may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00021 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00023 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00024 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00027 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 * 00031 */ 00032 00033 00034 #include "peer_data.h" 00035 00036 #include <stdint.h> 00037 #include <string.h> 00038 #include "peer_manager_types.h " 00039 #include "fds.h" 00040 00041 00042 00043 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) 00044 { 00045 if (p_n_chunks == NULL) 00046 { 00047 } 00048 else if ((p_peer_data == NULL) || (p_chunks == NULL)) 00049 { 00050 *p_n_chunks = 0; 00051 } 00052 else 00053 { 00054 switch (p_peer_data->data_type) 00055 { 00056 case PM_PEER_DATA_ID_BONDING: 00057 p_chunks[0].p_data = p_peer_data->data.p_bonding_data; 00058 p_chunks[0].length_words = p_peer_data->length_words; 00059 *p_n_chunks = 1; 00060 break; 00061 case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING: 00062 p_chunks[0].p_data = p_peer_data->data.p_service_changed_pending; 00063 p_chunks[0].length_words = p_peer_data->length_words; 00064 *p_n_chunks = 1; 00065 break; 00066 case PM_PEER_DATA_ID_GATT_LOCAL: 00067 p_chunks[0].p_data = p_peer_data->data.p_local_gatt_db; 00068 p_chunks[0].length_words = PM_N_WORDS(PM_LOCAL_DB_LEN_OVERHEAD_BYTES); 00069 p_chunks[1].p_data = p_peer_data->data.p_local_gatt_db->p_data; 00070 p_chunks[1].length_words = p_peer_data->length_words - p_chunks[0].length_words; 00071 *p_n_chunks = 2; 00072 break; 00073 case PM_PEER_DATA_ID_GATT_REMOTE: 00074 p_chunks[0].p_data = p_peer_data->data.p_remote_gatt_db; 00075 p_chunks[0].length_words = PM_N_WORDS(PM_REMOTE_DB_LEN_OVERHEAD_BYTES); 00076 p_chunks[1].p_data = p_peer_data->data.p_remote_gatt_db->p_data; 00077 p_chunks[1].length_words = p_peer_data->length_words - p_chunks[0].length_words; 00078 *p_n_chunks = 2; 00079 break; 00080 case PM_PEER_DATA_ID_APPLICATION: 00081 p_chunks[0].p_data = p_peer_data->data.p_application_data; 00082 p_chunks[0].length_words = p_peer_data->length_words; 00083 *p_n_chunks = 1; 00084 break; 00085 default: 00086 *p_n_chunks = 0; 00087 break; 00088 } 00089 } 00090 } 00091 00092 00093 ret_code_t peer_data_deserialize(pm_peer_data_flash_t const * p_in_data, pm_peer_data_t * p_out_data) 00094 { 00095 if ((p_in_data == NULL) || (p_out_data == NULL)) 00096 { 00097 return NRF_ERROR_NULL; 00098 } 00099 else 00100 { 00101 if (p_out_data->length_words < p_in_data->length_words) 00102 { 00103 p_out_data->length_words = p_in_data->length_words; 00104 return NRF_ERROR_NO_MEM; 00105 } 00106 p_out_data->length_words = p_in_data->length_words; 00107 p_out_data->data_type = p_in_data->data_type; 00108 00109 switch (p_in_data->data_type) 00110 { 00111 case PM_PEER_DATA_ID_BONDING: 00112 *p_out_data->data.p_bonding_data = *p_in_data->data.p_bonding_data; 00113 break; 00114 case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING: 00115 *p_out_data->data.p_service_changed_pending = *p_in_data->data.p_service_changed_pending; 00116 break; 00117 case PM_PEER_DATA_ID_GATT_LOCAL: 00118 if (p_out_data->data.p_local_gatt_db->p_data == NULL) 00119 { 00120 return NRF_ERROR_NULL; 00121 } 00122 if (p_out_data->data.p_local_gatt_db->len < p_in_data->data.p_local_gatt_db->len) 00123 { 00124 p_out_data->data.p_local_gatt_db->len = p_in_data->data.p_local_gatt_db->len; 00125 return NRF_ERROR_NO_MEM; 00126 } 00127 else 00128 { 00129 p_out_data->data.p_local_gatt_db->flags = p_in_data->data.p_local_gatt_db->flags; 00130 p_out_data->data.p_local_gatt_db->len = p_in_data->data.p_local_gatt_db->len; 00131 memcpy(p_out_data->data.p_local_gatt_db->p_data, 00132 p_in_data->data.p_local_gatt_db->p_data, 00133 p_in_data->data.p_local_gatt_db->len); 00134 } 00135 break; 00136 case PM_PEER_DATA_ID_GATT_REMOTE: 00137 if (p_out_data->data.p_remote_gatt_db->p_data == NULL) 00138 { 00139 return NRF_ERROR_NULL; 00140 } 00141 if (p_out_data->data.p_remote_gatt_db->service_count < p_in_data->data.p_remote_gatt_db->service_count) 00142 { 00143 p_out_data->data.p_remote_gatt_db->service_count = p_in_data->data.p_remote_gatt_db->service_count; 00144 return NRF_ERROR_NO_MEM; 00145 } 00146 else 00147 { 00148 p_out_data->data.p_remote_gatt_db->service_count = p_in_data->data.p_remote_gatt_db->service_count; 00149 memcpy(p_out_data->data.p_remote_gatt_db->p_data, 00150 p_in_data->data.p_remote_gatt_db->p_data, 00151 p_in_data->data.p_remote_gatt_db->service_count * sizeof(ble_gatt_db_srv_t)); 00152 } 00153 break; 00154 case PM_PEER_DATA_ID_APPLICATION: 00155 memcpy(p_out_data->data.p_application_data, 00156 p_in_data->data.p_application_data, 00157 p_in_data->length_words * 4); 00158 break; 00159 default: 00160 break; 00161 } 00162 } 00163 return NRF_SUCCESS; 00164 } 00165 00166
Generated on Tue Jul 12 2022 19:00:12 by
![doxygen](doxygen.png)