Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
arm_uc_utilities.h
00001 // ---------------------------------------------------------------------------- 00002 // Copyright 2016-2017 ARM Ltd. 00003 // 00004 // SPDX-License-Identifier: Apache-2.0 00005 // 00006 // Licensed under the Apache License, Version 2.0 (the "License"); 00007 // you may not use this file except in compliance with the License. 00008 // You may obtain a copy of the License at 00009 // 00010 // http://www.apache.org/licenses/LICENSE-2.0 00011 // 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // ---------------------------------------------------------------------------- 00018 00019 #ifndef ARM_UPDATE_COMMON_UTILITIES_H 00020 #define ARM_UPDATE_COMMON_UTILITIES_H 00021 00022 #include "update-client-common/arm_uc_types.h" 00023 #include "update-client-common/arm_uc_error.h" 00024 #include <string.h> 00025 #include <stdbool.h> 00026 00027 #ifdef __cplusplus 00028 extern "C" { 00029 #endif 00030 00031 #define ARM_UC_util_min(A,B)\ 00032 ((A) < (B) ? (A) : (B)) 00033 00034 /* lookup table for printing hexadecimal characters. */ 00035 extern const uint8_t arm_uc_hex_table[16]; 00036 00037 /** 00038 * @brief Parse a uri string to populate a arm_uc_uri_t struct 00039 * @detail Format of uri scheme:[//]host[:port]/path 00040 * [] means optional, path will always start with a '/' 00041 * 00042 * @param str Pointer to string containing URI. 00043 * @param size String length. 00044 * @param uri The arm_uc_uri_t struct to be populated 00045 */ 00046 arm_uc_error_t arm_uc_str2uri(const uint8_t* str, uint32_t size, arm_uc_uri_t* uri); 00047 00048 /** 00049 * @brief Find substring inside string. 00050 * @details The size of both string and substring is explicitly passed so no 00051 * assumptions are made about NULL termination. 00052 * 00053 * @param big_buffer Pointer to the string to be searched. 00054 * @param big_length Length of the string to be searched. 00055 * @param little_buffer Pointer to the substring being searched for. 00056 * @param little_length Length of the substring being searched for. 00057 * @return Index to where the substring was found inside the string. If the 00058 * string doesn't contain the subtring, UINT32_MAX is returned. 00059 */ 00060 uint32_t arm_uc_strnstrn(const uint8_t* big_buffer, 00061 uint32_t big_length, 00062 const uint8_t* little_buffer, 00063 uint32_t little_length); 00064 00065 /** 00066 * @brief Find string length. 00067 * @details Custom implementation of strnlen which is a GNU extension. 00068 * Returns either the string length or max_length. 00069 * 00070 * @param buffer Pointer to string. 00071 * @param max_length Maximum buffer length. 00072 * 00073 * @return String length or max_length. 00074 */ 00075 uint32_t arm_uc_strnlen(const uint8_t* buffer, uint32_t max_length); 00076 00077 /** 00078 * @brief Convert string to unsigned 32 bit integer. 00079 * @details Function tries to parse string as an unsigned 32 bit integer 00080 * and return the value. The function will set the third parameter 00081 * to true if the parsing was successful. 00082 * 00083 * @param buffer Pointer to string. 00084 * @param max_length Maximum buffer length. 00085 * @param success Pointer to boolean indicating whether the parsing was successful. 00086 * @return Parsed value. Only valid if success it true. 00087 */ 00088 uint32_t arm_uc_str2uint32(const uint8_t* buffer, 00089 uint32_t max_length, 00090 bool* success); 00091 00092 /** 00093 * @brief Calculate CRC 32 00094 * 00095 * @param buffer Input array. 00096 * @param length Length of array in bytes. 00097 * 00098 * @return 32 bit CRC. 00099 */ 00100 uint32_t arm_uc_crc32(const uint8_t* buffer, uint32_t length); 00101 00102 /** 00103 * @brief Parse 4 byte array into uint32_t 00104 * 00105 * @param input 4 byte array. 00106 * @return uint32_t 00107 */ 00108 uint32_t arm_uc_parse_uint32(const uint8_t* input); 00109 00110 /** 00111 * @brief Parse 8 byte array into uint64_t 00112 * 00113 * @param input 8 byte array. 00114 * @return uint64_t 00115 */ 00116 uint64_t arm_uc_parse_uint64(const uint8_t* input); 00117 00118 /** 00119 * @brief Write uint32_t to array. 00120 * 00121 * @param buffer Pointer to buffer. 00122 * @param value Value to be written. 00123 */ 00124 void arm_uc_write_uint32(uint8_t* buffer, uint32_t value); 00125 00126 /** 00127 * @brief Write uint64_t to array. 00128 * 00129 * @param buffer Pointer to buffer. 00130 * @param value Value to be written. 00131 */ 00132 void arm_uc_write_uint64(uint8_t* buffer, uint64_t value); 00133 00134 /** 00135 * @brief Do a shallow copy of a buffer. 00136 * @details Copies each field of a buffer from `src` to `dest`. This creates another reference to the buffer that 00137 * backs `src` and drops any reference to a buffer that backs `dest`. 00138 * 00139 * @param[out] dest Pointer to a buffer structure that will receive a reference to the buffer that backs `src` 00140 * @param[in] src Pointer to a buffer to copy into dest 00141 */ 00142 static inline void ARM_UC_buffer_shallow_copy(arm_uc_buffer_t* dest, arm_uc_buffer_t* src) { 00143 dest->size_max = src->size_max; 00144 dest->size = src->size; 00145 dest->ptr = src->ptr; 00146 } 00147 00148 /** 00149 * @brief Do a deep copy of a buffer. 00150 * @details Copies the content of `src->ptr` to `dest->ptr` 00151 * If the space used in the source memory, referenced by the source buffer, is less than the maximum space available in 00152 * the destination memory, referenced by the destination buffer, copies the source into the destination. 00153 * 00154 * @param[out] dest Pointer to a buffer structure that references destination memory 00155 * @param[in] src Pointer to a buffer that references the data to copy into the destination memory 00156 * @retval MFST_ERR_SIZE when the source size is larger than the destination's maximum size 00157 * @retval MFST_ERR_NULL_PTR when any expected pointer is NULL 00158 * @retval MFST_ERR_NONE on success 00159 */ 00160 static inline arm_uc_error_t ARM_UC_buffer_deep_copy(arm_uc_buffer_t* dest, arm_uc_buffer_t* src) 00161 { 00162 ARM_UC_INIT_ERROR(retval, MFST_ERR_NULL_PTR); 00163 00164 /* NULL pointer check */ 00165 if (dest && 00166 dest->ptr && 00167 src && 00168 src->ptr) 00169 { 00170 /* destination buffer is large enough */ 00171 if (src->size <= dest->size_max) 00172 { 00173 /* copy content and set new size */ 00174 memcpy(dest->ptr, src->ptr, src->size); 00175 dest->size = src->size; 00176 00177 ARM_UC_CLEAR_ERROR(retval); 00178 } 00179 else 00180 { 00181 ARM_UC_SET_ERROR(retval, MFST_ERR_SIZE); 00182 } 00183 } 00184 else 00185 { 00186 ARM_UC_SET_ERROR(retval, MFST_ERR_NULL_PTR); 00187 } 00188 00189 return retval; 00190 } 00191 00192 uint32_t ARM_UC_BinCompareCT(const arm_uc_buffer_t* a, const arm_uc_buffer_t* b); 00193 uint8_t * ARM_UC_Base64Enc(uint8_t *buf, const uint32_t size, const arm_uc_buffer_t* bin); 00194 void ARM_UC_Base64Dec(arm_uc_buffer_t* bin, const uint32_t size, const uint8_t* buf); 00195 00196 #ifdef __cplusplus 00197 } 00198 #endif 00199 00200 #endif // ARM_UPDATE_COMMON_UTILITIES_H
Generated on Tue Jul 12 2022 19:12:11 by 1.7.2