test
Fork of nrf51-sdk by
Embed:
(wiki syntax)
Show/hide line numbers
sdk_mapped_flags.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 #include "sdk_mapped_flags.h " 00034 #include <stdint.h> 00035 #include <stdbool.h> 00036 #include <stddef.h> 00037 #include "compiler_abstraction.h" 00038 00039 00040 /**@brief Function for setting the state of a flag to true. 00041 * 00042 * @note This function does not check whether the index is valid. 00043 * 00044 * @param[in] p_flags The collection of flags to modify. 00045 * @param[in] index The index of the flag to modify. 00046 */ 00047 static __INLINE void sdk_mapped_flags_set_by_index(sdk_mapped_flags_t * p_flags, uint16_t index) 00048 { 00049 *p_flags |= (1U << index); 00050 } 00051 00052 00053 /**@brief Function for setting the state of a flag to false. 00054 * 00055 * @note This function does not check whether the index is valid. 00056 * 00057 * @param[in] p_flags The collection of flags to modify. 00058 * @param[in] index The index of the flag to modify. 00059 */ 00060 static __INLINE void sdk_mapped_flags_clear_by_index(sdk_mapped_flags_t * p_flags, uint16_t index) 00061 { 00062 *p_flags &= ~(1U << index); 00063 } 00064 00065 00066 /**@brief Function for getting the state of a flag. 00067 * 00068 * @note This function does not check whether the index is valid. 00069 * 00070 * @param[in] p_flags The collection of flags to read. 00071 * @param[in] index The index of the flag to get. 00072 */ 00073 static __INLINE bool sdk_mapped_flags_get_by_index(sdk_mapped_flags_t flags, uint16_t index) 00074 { 00075 return ((flags & (1 << index)) != 0); 00076 } 00077 00078 00079 00080 uint16_t sdk_mapped_flags_first_key_index_get(sdk_mapped_flags_t flags) 00081 { 00082 for (uint16_t i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++) 00083 { 00084 if (sdk_mapped_flags_get_by_index(flags, i)) 00085 { 00086 return i; 00087 } 00088 } 00089 return SDK_MAPPED_FLAGS_INVALID_INDEX; 00090 } 00091 00092 00093 void sdk_mapped_flags_update_by_key(uint16_t * p_keys, 00094 sdk_mapped_flags_t * p_flags, 00095 uint16_t key, 00096 bool value) 00097 { 00098 sdk_mapped_flags_bulk_update_by_key(p_keys, p_flags, 1, key, value); 00099 } 00100 00101 00102 void sdk_mapped_flags_bulk_update_by_key(uint16_t * p_keys, 00103 sdk_mapped_flags_t * p_flags, 00104 uint32_t n_flag_collections, 00105 uint16_t key, 00106 bool value) 00107 { 00108 if ((p_keys != NULL) && (p_flags != NULL) && (n_flag_collections > 0)) 00109 { 00110 for (int i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++) 00111 { 00112 if (p_keys[i] == key) 00113 { 00114 for (int j = 0; j < n_flag_collections; j++) 00115 { 00116 if (value) 00117 { 00118 sdk_mapped_flags_set_by_index(&p_flags[j], i); 00119 } 00120 else 00121 { 00122 sdk_mapped_flags_clear_by_index(&p_flags[j], i); 00123 } 00124 } 00125 return; 00126 } 00127 } 00128 } 00129 } 00130 00131 00132 bool sdk_mapped_flags_get_by_key(uint16_t * p_keys, sdk_mapped_flags_t flags, uint16_t key) 00133 { 00134 if (p_keys != NULL) 00135 { 00136 for (int i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++) 00137 { 00138 if (p_keys[i] == key) 00139 { 00140 return sdk_mapped_flags_get_by_index(flags, i); 00141 } 00142 } 00143 } 00144 return false; 00145 } 00146 00147 00148 sdk_mapped_flags_key_list_t sdk_mapped_flags_key_list_get(uint16_t * p_keys, 00149 sdk_mapped_flags_t flags) 00150 { 00151 sdk_mapped_flags_key_list_t key_list; 00152 key_list.len = 0; 00153 00154 if (p_keys != NULL) 00155 { 00156 for (int i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++) 00157 { 00158 if (sdk_mapped_flags_get_by_index(flags, i)) 00159 { 00160 key_list.flag_keys[key_list.len++] = p_keys[i]; 00161 } 00162 } 00163 } 00164 00165 return key_list; 00166 } 00167 00168 00169 uint32_t sdk_mapped_flags_n_flags_set(sdk_mapped_flags_t flags) 00170 { 00171 uint32_t n_flags_set = 0; 00172 00173 for (int i = 0; i < SDK_MAPPED_FLAGS_N_KEYS; i++) 00174 { 00175 if (sdk_mapped_flags_get_by_index(flags, i)) 00176 { 00177 n_flags_set += 1; 00178 } 00179 } 00180 return n_flags_set; 00181 }
Generated on Tue Jul 12 2022 15:51:29 by 1.7.2