Simple interface for Mbed Cloud Client

Dependents:  

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_utilities.h Source File

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 "sotp.h"
00025 #include <string.h>
00026 #include <stdbool.h>
00027 
00028 #ifdef __cplusplus
00029 extern "C" {
00030 #endif
00031 
00032 #define ARM_UC_util_min(A,B)\
00033     ((A) < (B) ? (A) : (B))
00034 
00035 /* lookup table for printing hexadecimal characters. */
00036 extern const uint8_t arm_uc_hex_table[16];
00037 
00038 /**
00039  * @brief Parse a uri string to populate a arm_uc_uri_t struct
00040  * @detail Format of uri scheme:[//]host[:port]/path
00041  *         [] means optional, path will always start with a '/'
00042  *
00043  * @param str Pointer to string containing URI.
00044  * @param size String length.
00045  * @param uri The arm_uc_uri_t struct to be populated
00046  */
00047 arm_uc_error_t arm_uc_str2uri(const uint8_t* str, uint32_t size, arm_uc_uri_t* uri);
00048 
00049 /**
00050  * @brief Find substring inside string.
00051  * @details The size of both string and substring is explicitly passed so no
00052  *          assumptions are made about NULL termination.
00053  *
00054  * @param big_buffer Pointer to the string to be searched.
00055  * @param big_length Length of the string to be searched.
00056  * @param little_buffer Pointer to the substring being searched for.
00057  * @param little_length Length of the substring being searched for.
00058  * @return Index to where the substring was found inside the string. If the
00059  *         string doesn't contain the subtring, UINT32_MAX is returned.
00060  */
00061 uint32_t arm_uc_strnstrn(const uint8_t* big_buffer,
00062                          uint32_t big_length,
00063                          const uint8_t* little_buffer,
00064                          uint32_t little_length);
00065 
00066 /**
00067  * @brief Find string length.
00068  * @details Custom implementation of strnlen which is a GNU extension.
00069  *          Returns either the string length or max_length.
00070  *
00071  * @param buffer Pointer to string.
00072  * @param max_length Maximum buffer length.
00073  *
00074  * @return String length or max_length.
00075  */
00076 uint32_t arm_uc_strnlen(const uint8_t* buffer, uint32_t max_length);
00077 
00078 /**
00079  * @brief Convert string to unsigned 32 bit integer.
00080  * @details Function tries to parse string as an unsigned 32 bit integer
00081  *          and return the value. The function will set the third parameter
00082  *          to true if the parsing was successful.
00083  *
00084  * @param buffer Pointer to string.
00085  * @param max_length Maximum buffer length.
00086  * @param success Pointer to boolean indicating whether the parsing was successful.
00087  * @return Parsed value. Only valid if success it true.
00088  */
00089 uint32_t arm_uc_str2uint32(const uint8_t* buffer,
00090                            uint32_t max_length,
00091                            bool* success);
00092 
00093 /**
00094  * @brief Calculate CRC 32
00095  *
00096  * @param buffer Input array.
00097  * @param length Length of array in bytes.
00098  *
00099  * @return 32 bit CRC.
00100  */
00101 uint32_t arm_uc_crc32(const uint8_t* buffer, uint32_t length);
00102 
00103 /**
00104  * @brief Parse 4 byte array into uint32_t
00105  *
00106  * @param input 4 byte array.
00107  * @return uint32_t
00108  */
00109 uint32_t arm_uc_parse_uint32(const uint8_t* input);
00110 
00111 /**
00112  * @brief Parse 8 byte array into uint64_t
00113  *
00114  * @param input 8 byte array.
00115  * @return uint64_t
00116  */
00117 uint64_t arm_uc_parse_uint64(const uint8_t* input);
00118 
00119 /**
00120  * @brief Write uint32_t to array.
00121  *
00122  * @param buffer Pointer to buffer.
00123  * @param value Value to be written.
00124  */
00125 void arm_uc_write_uint32(uint8_t* buffer, uint32_t value);
00126 
00127 /**
00128  * @brief Write uint64_t to array.
00129  *
00130  * @param buffer Pointer to buffer.
00131  * @param value Value to be written.
00132  */
00133 void arm_uc_write_uint64(uint8_t* buffer, uint64_t value);
00134 
00135 /**
00136  * @brief Do a shallow copy of a buffer.
00137  * @details Copies each field of a buffer from `src` to `dest`. This creates another reference to the buffer that
00138  *          backs `src` and drops any reference to a buffer that backs `dest`.
00139  *
00140  * @param[out] dest Pointer to a buffer structure that will receive a reference to the buffer that backs `src`
00141  * @param[in] src Pointer to a buffer to copy into dest
00142  */
00143 static inline void ARM_UC_buffer_shallow_copy(arm_uc_buffer_t* dest, arm_uc_buffer_t* src) {
00144     dest->size_max = src->size_max;
00145     dest->size     = src->size;
00146     dest->ptr      = src->ptr;
00147 }
00148 
00149 /**
00150  * @brief Do a deep copy of a buffer.
00151  * @details Copies the content of `src->ptr` to `dest->ptr`
00152  * If the space used in the source memory, referenced by the source buffer, is less than the maximum space available in
00153  * the destination memory, referenced by the destination buffer, copies the source into the destination.
00154  *
00155  * @param[out] dest Pointer to a buffer structure that references destination memory
00156  * @param[in] src Pointer to a buffer that references the data to copy into the destination memory
00157  * @retval MFST_ERR_SIZE when the source size is larger than the destination's maximum size
00158  * @retval MFST_ERR_NULL_PTR when any expected pointer is NULL
00159  * @retval MFST_ERR_NONE on success
00160  */
00161 static inline arm_uc_error_t ARM_UC_buffer_deep_copy(arm_uc_buffer_t* dest, arm_uc_buffer_t* src)
00162 {
00163     arm_uc_error_t retval = { .code = MFST_ERR_NULL_PTR };
00164 
00165     /* NULL pointer check */
00166     if (dest &&
00167         dest->ptr &&
00168         src &&
00169         src->ptr)
00170     {
00171         /* destination buffer is large enough */
00172         if (src->size <= dest->size_max)
00173         {
00174             /* copy content and set new size */
00175             memcpy(dest->ptr, src->ptr, src->size);
00176             dest->size = src->size;
00177 
00178             retval.code = MFST_ERR_NONE;
00179         }
00180         else
00181         {
00182             retval.code = MFST_ERR_SIZE;
00183         }
00184     }
00185     else
00186     {
00187         retval.code = MFST_ERR_NULL_PTR;
00188     }
00189 
00190     return retval;
00191 }
00192 
00193 uint32_t ARM_UC_BinCompareCT(const arm_uc_buffer_t* a, const arm_uc_buffer_t* b);
00194 uint8_t * ARM_UC_Base64Enc(uint8_t *buf, const uint32_t size, const arm_uc_buffer_t* bin);
00195 void ARM_UC_Base64Dec(arm_uc_buffer_t* bin, const uint32_t size, const uint8_t* buf);
00196 
00197 /**
00198  * Reads an integer up to 8 bytes in size from SOTP.
00199  * @param type SOTP item type.
00200  * @param size SOTP item size in bytes (1, 2, 4 or 8).
00201  * @param out Location where the integer value is written.
00202  * @return ERR_NONE if the read succeeded, ERR_INVALID_PARAMETER otherwise.
00203  */
00204 arm_uc_error_t arm_uc_read_sotp_uint(uint32_t type, uint16_t size, void *out);
00205 
00206 /**
00207  * Writes an integer up to 8 bytes in size to SOTP.
00208  * @param type SOTP item type.
00209  * @param size SOTP item size in bytes (1, 2, 4 or 8).
00210  * @param in Location where the integer value is read.
00211  * @return ERR_NONE if the read succeeded, ERR_INVALID_PARAMETER otherwise.
00212  */
00213 arm_uc_error_t arm_uc_write_sotp_uint(uint32_t type, uint16_t size, void *in);
00214 
00215 #ifdef __cplusplus
00216 }
00217 #endif
00218 
00219 #endif // ARM_UPDATE_COMMON_UTILITIES_H