Nordic stack and drivers for the mbed BLE API

Fork of nRF51822 by Nordic Semiconductor

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
640:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

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