cvb

Fork of nrf51-sdk by Lancaster University

Committer:
Jonathan Austin
Date:
Wed Apr 06 23:55:04 2016 +0100
Revision:
0:bc2961fa1ef0
Synchronized with git rev 90647e3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 0:bc2961fa1ef0 1 /*
Jonathan Austin 0:bc2961fa1ef0 2 * Copyright (c) Nordic Semiconductor ASA
Jonathan Austin 0:bc2961fa1ef0 3 * All rights reserved.
Jonathan Austin 0:bc2961fa1ef0 4 *
Jonathan Austin 0:bc2961fa1ef0 5 * Redistribution and use in source and binary forms, with or without modification,
Jonathan Austin 0:bc2961fa1ef0 6 * are permitted provided that the following conditions are met:
Jonathan Austin 0:bc2961fa1ef0 7 *
Jonathan Austin 0:bc2961fa1ef0 8 * 1. Redistributions of source code must retain the above copyright notice, this
Jonathan Austin 0:bc2961fa1ef0 9 * list of conditions and the following disclaimer.
Jonathan Austin 0:bc2961fa1ef0 10 *
Jonathan Austin 0:bc2961fa1ef0 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this
Jonathan Austin 0:bc2961fa1ef0 12 * list of conditions and the following disclaimer in the documentation and/or
Jonathan Austin 0:bc2961fa1ef0 13 * other materials provided with the distribution.
Jonathan Austin 0:bc2961fa1ef0 14 *
Jonathan Austin 0:bc2961fa1ef0 15 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
Jonathan Austin 0:bc2961fa1ef0 16 * contributors to this software may be used to endorse or promote products
Jonathan Austin 0:bc2961fa1ef0 17 * derived from this software without specific prior written permission.
Jonathan Austin 0:bc2961fa1ef0 18 *
Jonathan Austin 0:bc2961fa1ef0 19 *
Jonathan Austin 0:bc2961fa1ef0 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Jonathan Austin 0:bc2961fa1ef0 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Jonathan Austin 0:bc2961fa1ef0 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Jonathan Austin 0:bc2961fa1ef0 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Jonathan Austin 0:bc2961fa1ef0 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Jonathan Austin 0:bc2961fa1ef0 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Jonathan Austin 0:bc2961fa1ef0 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Jonathan Austin 0:bc2961fa1ef0 27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Jonathan Austin 0:bc2961fa1ef0 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Jonathan Austin 0:bc2961fa1ef0 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jonathan Austin 0:bc2961fa1ef0 30 *
Jonathan Austin 0:bc2961fa1ef0 31 */
Jonathan Austin 0:bc2961fa1ef0 32
Jonathan Austin 0:bc2961fa1ef0 33
Jonathan Austin 0:bc2961fa1ef0 34 #include "peer_data.h"
Jonathan Austin 0:bc2961fa1ef0 35
Jonathan Austin 0:bc2961fa1ef0 36 #include <stdint.h>
Jonathan Austin 0:bc2961fa1ef0 37 #include <string.h>
Jonathan Austin 0:bc2961fa1ef0 38 #include "peer_manager_types.h"
Jonathan Austin 0:bc2961fa1ef0 39 #include "fds.h"
Jonathan Austin 0:bc2961fa1ef0 40
Jonathan Austin 0:bc2961fa1ef0 41
Jonathan Austin 0:bc2961fa1ef0 42
Jonathan Austin 0:bc2961fa1ef0 43 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)
Jonathan Austin 0:bc2961fa1ef0 44 {
Jonathan Austin 0:bc2961fa1ef0 45 if (p_n_chunks == NULL)
Jonathan Austin 0:bc2961fa1ef0 46 {
Jonathan Austin 0:bc2961fa1ef0 47 }
Jonathan Austin 0:bc2961fa1ef0 48 else if ((p_peer_data == NULL) || (p_chunks == NULL))
Jonathan Austin 0:bc2961fa1ef0 49 {
Jonathan Austin 0:bc2961fa1ef0 50 *p_n_chunks = 0;
Jonathan Austin 0:bc2961fa1ef0 51 }
Jonathan Austin 0:bc2961fa1ef0 52 else
Jonathan Austin 0:bc2961fa1ef0 53 {
Jonathan Austin 0:bc2961fa1ef0 54 switch (p_peer_data->data_type)
Jonathan Austin 0:bc2961fa1ef0 55 {
Jonathan Austin 0:bc2961fa1ef0 56 case PM_PEER_DATA_ID_BONDING:
Jonathan Austin 0:bc2961fa1ef0 57 p_chunks[0].p_data = p_peer_data->data.p_bonding_data;
Jonathan Austin 0:bc2961fa1ef0 58 p_chunks[0].length_words = p_peer_data->length_words;
Jonathan Austin 0:bc2961fa1ef0 59 *p_n_chunks = 1;
Jonathan Austin 0:bc2961fa1ef0 60 break;
Jonathan Austin 0:bc2961fa1ef0 61 case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING:
Jonathan Austin 0:bc2961fa1ef0 62 p_chunks[0].p_data = p_peer_data->data.p_service_changed_pending;
Jonathan Austin 0:bc2961fa1ef0 63 p_chunks[0].length_words = p_peer_data->length_words;
Jonathan Austin 0:bc2961fa1ef0 64 *p_n_chunks = 1;
Jonathan Austin 0:bc2961fa1ef0 65 break;
Jonathan Austin 0:bc2961fa1ef0 66 case PM_PEER_DATA_ID_GATT_LOCAL:
Jonathan Austin 0:bc2961fa1ef0 67 p_chunks[0].p_data = p_peer_data->data.p_local_gatt_db;
Jonathan Austin 0:bc2961fa1ef0 68 p_chunks[0].length_words = PM_N_WORDS(PM_LOCAL_DB_LEN_OVERHEAD_BYTES);
Jonathan Austin 0:bc2961fa1ef0 69 p_chunks[1].p_data = p_peer_data->data.p_local_gatt_db->p_data;
Jonathan Austin 0:bc2961fa1ef0 70 p_chunks[1].length_words = p_peer_data->length_words - p_chunks[0].length_words;
Jonathan Austin 0:bc2961fa1ef0 71 *p_n_chunks = 2;
Jonathan Austin 0:bc2961fa1ef0 72 break;
Jonathan Austin 0:bc2961fa1ef0 73 case PM_PEER_DATA_ID_GATT_REMOTE:
Jonathan Austin 0:bc2961fa1ef0 74 p_chunks[0].p_data = p_peer_data->data.p_remote_gatt_db;
Jonathan Austin 0:bc2961fa1ef0 75 p_chunks[0].length_words = PM_N_WORDS(PM_REMOTE_DB_LEN_OVERHEAD_BYTES);
Jonathan Austin 0:bc2961fa1ef0 76 p_chunks[1].p_data = p_peer_data->data.p_remote_gatt_db->p_data;
Jonathan Austin 0:bc2961fa1ef0 77 p_chunks[1].length_words = p_peer_data->length_words - p_chunks[0].length_words;
Jonathan Austin 0:bc2961fa1ef0 78 *p_n_chunks = 2;
Jonathan Austin 0:bc2961fa1ef0 79 break;
Jonathan Austin 0:bc2961fa1ef0 80 case PM_PEER_DATA_ID_APPLICATION:
Jonathan Austin 0:bc2961fa1ef0 81 p_chunks[0].p_data = p_peer_data->data.p_application_data;
Jonathan Austin 0:bc2961fa1ef0 82 p_chunks[0].length_words = p_peer_data->length_words;
Jonathan Austin 0:bc2961fa1ef0 83 *p_n_chunks = 1;
Jonathan Austin 0:bc2961fa1ef0 84 break;
Jonathan Austin 0:bc2961fa1ef0 85 default:
Jonathan Austin 0:bc2961fa1ef0 86 *p_n_chunks = 0;
Jonathan Austin 0:bc2961fa1ef0 87 break;
Jonathan Austin 0:bc2961fa1ef0 88 }
Jonathan Austin 0:bc2961fa1ef0 89 }
Jonathan Austin 0:bc2961fa1ef0 90 }
Jonathan Austin 0:bc2961fa1ef0 91
Jonathan Austin 0:bc2961fa1ef0 92
Jonathan Austin 0:bc2961fa1ef0 93 ret_code_t peer_data_deserialize(pm_peer_data_flash_t const * p_in_data, pm_peer_data_t * p_out_data)
Jonathan Austin 0:bc2961fa1ef0 94 {
Jonathan Austin 0:bc2961fa1ef0 95 if ((p_in_data == NULL) || (p_out_data == NULL))
Jonathan Austin 0:bc2961fa1ef0 96 {
Jonathan Austin 0:bc2961fa1ef0 97 return NRF_ERROR_NULL;
Jonathan Austin 0:bc2961fa1ef0 98 }
Jonathan Austin 0:bc2961fa1ef0 99 else
Jonathan Austin 0:bc2961fa1ef0 100 {
Jonathan Austin 0:bc2961fa1ef0 101 if (p_out_data->length_words < p_in_data->length_words)
Jonathan Austin 0:bc2961fa1ef0 102 {
Jonathan Austin 0:bc2961fa1ef0 103 p_out_data->length_words = p_in_data->length_words;
Jonathan Austin 0:bc2961fa1ef0 104 return NRF_ERROR_NO_MEM;
Jonathan Austin 0:bc2961fa1ef0 105 }
Jonathan Austin 0:bc2961fa1ef0 106 p_out_data->length_words = p_in_data->length_words;
Jonathan Austin 0:bc2961fa1ef0 107 p_out_data->data_type = p_in_data->data_type;
Jonathan Austin 0:bc2961fa1ef0 108
Jonathan Austin 0:bc2961fa1ef0 109 switch (p_in_data->data_type)
Jonathan Austin 0:bc2961fa1ef0 110 {
Jonathan Austin 0:bc2961fa1ef0 111 case PM_PEER_DATA_ID_BONDING:
Jonathan Austin 0:bc2961fa1ef0 112 *p_out_data->data.p_bonding_data = *p_in_data->data.p_bonding_data;
Jonathan Austin 0:bc2961fa1ef0 113 break;
Jonathan Austin 0:bc2961fa1ef0 114 case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING:
Jonathan Austin 0:bc2961fa1ef0 115 *p_out_data->data.p_service_changed_pending = *p_in_data->data.p_service_changed_pending;
Jonathan Austin 0:bc2961fa1ef0 116 break;
Jonathan Austin 0:bc2961fa1ef0 117 case PM_PEER_DATA_ID_GATT_LOCAL:
Jonathan Austin 0:bc2961fa1ef0 118 if (p_out_data->data.p_local_gatt_db->p_data == NULL)
Jonathan Austin 0:bc2961fa1ef0 119 {
Jonathan Austin 0:bc2961fa1ef0 120 return NRF_ERROR_NULL;
Jonathan Austin 0:bc2961fa1ef0 121 }
Jonathan Austin 0:bc2961fa1ef0 122 if (p_out_data->data.p_local_gatt_db->len < p_in_data->data.p_local_gatt_db->len)
Jonathan Austin 0:bc2961fa1ef0 123 {
Jonathan Austin 0:bc2961fa1ef0 124 p_out_data->data.p_local_gatt_db->len = p_in_data->data.p_local_gatt_db->len;
Jonathan Austin 0:bc2961fa1ef0 125 return NRF_ERROR_NO_MEM;
Jonathan Austin 0:bc2961fa1ef0 126 }
Jonathan Austin 0:bc2961fa1ef0 127 else
Jonathan Austin 0:bc2961fa1ef0 128 {
Jonathan Austin 0:bc2961fa1ef0 129 p_out_data->data.p_local_gatt_db->flags = p_in_data->data.p_local_gatt_db->flags;
Jonathan Austin 0:bc2961fa1ef0 130 p_out_data->data.p_local_gatt_db->len = p_in_data->data.p_local_gatt_db->len;
Jonathan Austin 0:bc2961fa1ef0 131 memcpy(p_out_data->data.p_local_gatt_db->p_data,
Jonathan Austin 0:bc2961fa1ef0 132 p_in_data->data.p_local_gatt_db->p_data,
Jonathan Austin 0:bc2961fa1ef0 133 p_in_data->data.p_local_gatt_db->len);
Jonathan Austin 0:bc2961fa1ef0 134 }
Jonathan Austin 0:bc2961fa1ef0 135 break;
Jonathan Austin 0:bc2961fa1ef0 136 case PM_PEER_DATA_ID_GATT_REMOTE:
Jonathan Austin 0:bc2961fa1ef0 137 if (p_out_data->data.p_remote_gatt_db->p_data == NULL)
Jonathan Austin 0:bc2961fa1ef0 138 {
Jonathan Austin 0:bc2961fa1ef0 139 return NRF_ERROR_NULL;
Jonathan Austin 0:bc2961fa1ef0 140 }
Jonathan Austin 0:bc2961fa1ef0 141 if (p_out_data->data.p_remote_gatt_db->service_count < p_in_data->data.p_remote_gatt_db->service_count)
Jonathan Austin 0:bc2961fa1ef0 142 {
Jonathan Austin 0:bc2961fa1ef0 143 p_out_data->data.p_remote_gatt_db->service_count = p_in_data->data.p_remote_gatt_db->service_count;
Jonathan Austin 0:bc2961fa1ef0 144 return NRF_ERROR_NO_MEM;
Jonathan Austin 0:bc2961fa1ef0 145 }
Jonathan Austin 0:bc2961fa1ef0 146 else
Jonathan Austin 0:bc2961fa1ef0 147 {
Jonathan Austin 0:bc2961fa1ef0 148 p_out_data->data.p_remote_gatt_db->service_count = p_in_data->data.p_remote_gatt_db->service_count;
Jonathan Austin 0:bc2961fa1ef0 149 memcpy(p_out_data->data.p_remote_gatt_db->p_data,
Jonathan Austin 0:bc2961fa1ef0 150 p_in_data->data.p_remote_gatt_db->p_data,
Jonathan Austin 0:bc2961fa1ef0 151 p_in_data->data.p_remote_gatt_db->service_count * sizeof(ble_gatt_db_srv_t));
Jonathan Austin 0:bc2961fa1ef0 152 }
Jonathan Austin 0:bc2961fa1ef0 153 break;
Jonathan Austin 0:bc2961fa1ef0 154 case PM_PEER_DATA_ID_APPLICATION:
Jonathan Austin 0:bc2961fa1ef0 155 memcpy(p_out_data->data.p_application_data,
Jonathan Austin 0:bc2961fa1ef0 156 p_in_data->data.p_application_data,
Jonathan Austin 0:bc2961fa1ef0 157 p_in_data->length_words * 4);
Jonathan Austin 0:bc2961fa1ef0 158 break;
Jonathan Austin 0:bc2961fa1ef0 159 default:
Jonathan Austin 0:bc2961fa1ef0 160 break;
Jonathan Austin 0:bc2961fa1ef0 161 }
Jonathan Austin 0:bc2961fa1ef0 162 }
Jonathan Austin 0:bc2961fa1ef0 163 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 164 }
Jonathan Austin 0:bc2961fa1ef0 165
Jonathan Austin 0:bc2961fa1ef0 166