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 #include "sdk_mapped_flags.h"
Vincent Coubard 640:c90ae1400bf2 34 #include <stdint.h>
Vincent Coubard 640:c90ae1400bf2 35 #include <stdbool.h>
Vincent Coubard 640:c90ae1400bf2 36 #include <stddef.h>
Vincent Coubard 640:c90ae1400bf2 37 #include "compiler_abstraction.h"
Vincent Coubard 640:c90ae1400bf2 38
Vincent Coubard 640:c90ae1400bf2 39
Vincent Coubard 640:c90ae1400bf2 40 /**@brief Function for setting the state of a flag to true.
Vincent Coubard 640:c90ae1400bf2 41 *
Vincent Coubard 640:c90ae1400bf2 42 * @note This function does not check whether the index is valid.
Vincent Coubard 640:c90ae1400bf2 43 *
Vincent Coubard 640:c90ae1400bf2 44 * @param[in] p_flags The collection of flags to modify.
Vincent Coubard 640:c90ae1400bf2 45 * @param[in] index The index of the flag to modify.
Vincent Coubard 640:c90ae1400bf2 46 */
Vincent Coubard 640:c90ae1400bf2 47 static __INLINE void sdk_mapped_flags_set_by_index(sdk_mapped_flags_t * p_flags, uint16_t index)
Vincent Coubard 640:c90ae1400bf2 48 {
Vincent Coubard 640:c90ae1400bf2 49 *p_flags |= (1U << index);
Vincent Coubard 640:c90ae1400bf2 50 }
Vincent Coubard 640:c90ae1400bf2 51
Vincent Coubard 640:c90ae1400bf2 52
Vincent Coubard 640:c90ae1400bf2 53 /**@brief Function for setting the state of a flag to false.
Vincent Coubard 640:c90ae1400bf2 54 *
Vincent Coubard 640:c90ae1400bf2 55 * @note This function does not check whether the index is valid.
Vincent Coubard 640:c90ae1400bf2 56 *
Vincent Coubard 640:c90ae1400bf2 57 * @param[in] p_flags The collection of flags to modify.
Vincent Coubard 640:c90ae1400bf2 58 * @param[in] index The index of the flag to modify.
Vincent Coubard 640:c90ae1400bf2 59 */
Vincent Coubard 640:c90ae1400bf2 60 static __INLINE void sdk_mapped_flags_clear_by_index(sdk_mapped_flags_t * p_flags, uint16_t index)
Vincent Coubard 640:c90ae1400bf2 61 {
Vincent Coubard 640:c90ae1400bf2 62 *p_flags &= ~(1U << index);
Vincent Coubard 640:c90ae1400bf2 63 }
Vincent Coubard 640:c90ae1400bf2 64
Vincent Coubard 640:c90ae1400bf2 65
Vincent Coubard 640:c90ae1400bf2 66 /**@brief Function for getting the state of a flag.
Vincent Coubard 640:c90ae1400bf2 67 *
Vincent Coubard 640:c90ae1400bf2 68 * @note This function does not check whether the index is valid.
Vincent Coubard 640:c90ae1400bf2 69 *
Vincent Coubard 640:c90ae1400bf2 70 * @param[in] p_flags The collection of flags to read.
Vincent Coubard 640:c90ae1400bf2 71 * @param[in] index The index of the flag to get.
Vincent Coubard 640:c90ae1400bf2 72 */
Vincent Coubard 640:c90ae1400bf2 73 static __INLINE bool sdk_mapped_flags_get_by_index(sdk_mapped_flags_t flags, uint16_t index)
Vincent Coubard 640:c90ae1400bf2 74 {
Vincent Coubard 640:c90ae1400bf2 75 return ((flags & (1 << index)) != 0);
Vincent Coubard 640:c90ae1400bf2 76 }
Vincent Coubard 640:c90ae1400bf2 77
Vincent Coubard 640:c90ae1400bf2 78
Vincent Coubard 640:c90ae1400bf2 79
Vincent Coubard 640:c90ae1400bf2 80 uint16_t sdk_mapped_flags_first_key_index_get(sdk_mapped_flags_t flags)
Vincent Coubard 640:c90ae1400bf2 81 {
Vincent Coubard 640:c90ae1400bf2 82 for (uint16_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
Vincent Coubard 640:c90ae1400bf2 83 {
Vincent Coubard 640:c90ae1400bf2 84 if (sdk_mapped_flags_get_by_index(flags, i))
Vincent Coubard 640:c90ae1400bf2 85 {
Vincent Coubard 640:c90ae1400bf2 86 return i;
Vincent Coubard 640:c90ae1400bf2 87 }
Vincent Coubard 640:c90ae1400bf2 88 }
Vincent Coubard 640:c90ae1400bf2 89 return SDK_MAPPED_FLAGS_INVALID_INDEX;
Vincent Coubard 640:c90ae1400bf2 90 }
Vincent Coubard 640:c90ae1400bf2 91
Vincent Coubard 640:c90ae1400bf2 92
Vincent Coubard 640:c90ae1400bf2 93 void sdk_mapped_flags_update_by_key(uint16_t * p_keys,
Vincent Coubard 640:c90ae1400bf2 94 sdk_mapped_flags_t * p_flags,
Vincent Coubard 640:c90ae1400bf2 95 uint16_t key,
Vincent Coubard 640:c90ae1400bf2 96 bool value)
Vincent Coubard 640:c90ae1400bf2 97 {
Vincent Coubard 640:c90ae1400bf2 98 sdk_mapped_flags_bulk_update_by_key(p_keys, p_flags, 1, key, value);
Vincent Coubard 640:c90ae1400bf2 99 }
Vincent Coubard 640:c90ae1400bf2 100
Vincent Coubard 640:c90ae1400bf2 101
Vincent Coubard 640:c90ae1400bf2 102 void sdk_mapped_flags_bulk_update_by_key(uint16_t * p_keys,
Vincent Coubard 640:c90ae1400bf2 103 sdk_mapped_flags_t * p_flags,
Vincent Coubard 640:c90ae1400bf2 104 uint32_t n_flag_collections,
Vincent Coubard 640:c90ae1400bf2 105 uint16_t key,
Vincent Coubard 640:c90ae1400bf2 106 bool value)
Vincent Coubard 640:c90ae1400bf2 107 {
Vincent Coubard 640:c90ae1400bf2 108 if ((p_keys != NULL) && (p_flags != NULL) && (n_flag_collections > 0))
Vincent Coubard 640:c90ae1400bf2 109 {
Vincent Coubard 640:c90ae1400bf2 110 for (int i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
Vincent Coubard 640:c90ae1400bf2 111 {
Vincent Coubard 640:c90ae1400bf2 112 if (p_keys[i] == key)
Vincent Coubard 640:c90ae1400bf2 113 {
Vincent Coubard 640:c90ae1400bf2 114 for (int j = 0; j < n_flag_collections; j++)
Vincent Coubard 640:c90ae1400bf2 115 {
Vincent Coubard 640:c90ae1400bf2 116 if (value)
Vincent Coubard 640:c90ae1400bf2 117 {
Vincent Coubard 640:c90ae1400bf2 118 sdk_mapped_flags_set_by_index(&p_flags[j], i);
Vincent Coubard 640:c90ae1400bf2 119 }
Vincent Coubard 640:c90ae1400bf2 120 else
Vincent Coubard 640:c90ae1400bf2 121 {
Vincent Coubard 640:c90ae1400bf2 122 sdk_mapped_flags_clear_by_index(&p_flags[j], i);
Vincent Coubard 640:c90ae1400bf2 123 }
Vincent Coubard 640:c90ae1400bf2 124 }
Vincent Coubard 640:c90ae1400bf2 125 return;
Vincent Coubard 640:c90ae1400bf2 126 }
Vincent Coubard 640:c90ae1400bf2 127 }
Vincent Coubard 640:c90ae1400bf2 128 }
Vincent Coubard 640:c90ae1400bf2 129 }
Vincent Coubard 640:c90ae1400bf2 130
Vincent Coubard 640:c90ae1400bf2 131
Vincent Coubard 640:c90ae1400bf2 132 bool sdk_mapped_flags_get_by_key(uint16_t * p_keys, sdk_mapped_flags_t flags, uint16_t key)
Vincent Coubard 640:c90ae1400bf2 133 {
Vincent Coubard 640:c90ae1400bf2 134 if (p_keys != NULL)
Vincent Coubard 640:c90ae1400bf2 135 {
Vincent Coubard 640:c90ae1400bf2 136 for (int i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
Vincent Coubard 640:c90ae1400bf2 137 {
Vincent Coubard 640:c90ae1400bf2 138 if (p_keys[i] == key)
Vincent Coubard 640:c90ae1400bf2 139 {
Vincent Coubard 640:c90ae1400bf2 140 return sdk_mapped_flags_get_by_index(flags, i);
Vincent Coubard 640:c90ae1400bf2 141 }
Vincent Coubard 640:c90ae1400bf2 142 }
Vincent Coubard 640:c90ae1400bf2 143 }
Vincent Coubard 640:c90ae1400bf2 144 return false;
Vincent Coubard 640:c90ae1400bf2 145 }
Vincent Coubard 640:c90ae1400bf2 146
Vincent Coubard 640:c90ae1400bf2 147
Vincent Coubard 640:c90ae1400bf2 148 sdk_mapped_flags_key_list_t sdk_mapped_flags_key_list_get(uint16_t * p_keys,
Vincent Coubard 640:c90ae1400bf2 149 sdk_mapped_flags_t flags)
Vincent Coubard 640:c90ae1400bf2 150 {
Vincent Coubard 640:c90ae1400bf2 151 sdk_mapped_flags_key_list_t key_list;
Vincent Coubard 640:c90ae1400bf2 152 key_list.len = 0;
Vincent Coubard 640:c90ae1400bf2 153
Vincent Coubard 640:c90ae1400bf2 154 if (p_keys != NULL)
Vincent Coubard 640:c90ae1400bf2 155 {
Vincent Coubard 640:c90ae1400bf2 156 for (int i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
Vincent Coubard 640:c90ae1400bf2 157 {
Vincent Coubard 640:c90ae1400bf2 158 if (sdk_mapped_flags_get_by_index(flags, i))
Vincent Coubard 640:c90ae1400bf2 159 {
Vincent Coubard 640:c90ae1400bf2 160 key_list.flag_keys[key_list.len++] = p_keys[i];
Vincent Coubard 640:c90ae1400bf2 161 }
Vincent Coubard 640:c90ae1400bf2 162 }
Vincent Coubard 640:c90ae1400bf2 163 }
Vincent Coubard 640:c90ae1400bf2 164
Vincent Coubard 640:c90ae1400bf2 165 return key_list;
Vincent Coubard 640:c90ae1400bf2 166 }
Vincent Coubard 640:c90ae1400bf2 167
Vincent Coubard 640:c90ae1400bf2 168
Vincent Coubard 640:c90ae1400bf2 169 uint32_t sdk_mapped_flags_n_flags_set(sdk_mapped_flags_t flags)
Vincent Coubard 640:c90ae1400bf2 170 {
Vincent Coubard 640:c90ae1400bf2 171 uint32_t n_flags_set = 0;
Vincent Coubard 640:c90ae1400bf2 172
Vincent Coubard 640:c90ae1400bf2 173 for (int i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++)
Vincent Coubard 640:c90ae1400bf2 174 {
Vincent Coubard 640:c90ae1400bf2 175 if (sdk_mapped_flags_get_by_index(flags, i))
Vincent Coubard 640:c90ae1400bf2 176 {
Vincent Coubard 640:c90ae1400bf2 177 n_flags_set += 1;
Vincent Coubard 640:c90ae1400bf2 178 }
Vincent Coubard 640:c90ae1400bf2 179 }
Vincent Coubard 640:c90ae1400bf2 180 return n_flags_set;
Vincent Coubard 640:c90ae1400bf2 181 }