WizziLab / modem_ref_v5_3_217

Dependents:   modem_ref_helper_for_v5_3_217

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers kal_codec.h Source File

kal_codec.h

Go to the documentation of this file.
00001 /// @copyright
00002 /// ========================================================================={{{
00003 /// Copyright (c) 20XX                                                         /
00004 /// All rights reserved                                                        /
00005 ///                                                                            /
00006 /// IMPORTANT: This Software may not be modified, copied or distributed unless /
00007 /// embedded on a WizziLab product. Other than for the foregoing purpose, this /
00008 /// Software and/or its documentation may not be used, reproduced, copied,     /
00009 /// prepared derivative works of, modified, performed, distributed, displayed  /
00010 /// or sold for any purpose. For the sole purpose of embedding this Software   /
00011 /// on a WizziLab product, copy, modification and distribution of this         /
00012 /// Software is granted provided that the following conditions are respected:  /
00013 ///                                                                            /
00014 /// *  Redistributions of source code must retain the above copyright notice,  /
00015 ///    this list of conditions and the following disclaimer                    /
00016 ///                                                                            /
00017 /// *  Redistributions in binary form must reproduce the above copyright       /
00018 ///    notice, this list of conditions and the following disclaimer in the     /
00019 ///    documentation and/or other materials provided with the distribution.    /
00020 ///                                                                            /
00021 /// *  The name of WizziLab can not be used to endorse or promote products     /
00022 ///    derived from this software without specific prior written permission.   /
00023 ///                                                                            /
00024 /// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS        /
00025 /// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED  /
00026 /// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR /
00027 /// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR          /
00028 /// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,      /
00029 /// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,        /
00030 /// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,            /
00031 /// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     /
00032 /// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING    /
00033 /// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS         /
00034 /// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.               /
00035 /// WIZZILAB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,       /
00036 /// ENHANCEMENTS OR MODIFICATIONS.                                             /
00037 ///                                                                            /
00038 /// Should you have any questions regarding your right to use this Software,   /
00039 /// contact WizziLab at www.wizzilab.com.                                      /
00040 ///                                                                            /
00041 /// =========================================================================}}}
00042 /// @endcopyright
00043 
00044 //  =======================================================================
00045 /// @file           kal_codec.h
00046 /// @brief          KAL utilities s
00047 //  =======================================================================
00048 
00049 #ifndef _KAL_CODEC_H_
00050 #define _KAL_CODEC_H_
00051 
00052 #include "hal_types.h"
00053 
00054 // ======================================================================
00055 //
00056 //
00057 //                  ASCII to Binary Codec Toolkits 
00058 //
00059 //
00060 // ======================================================================
00061 
00062 //======================================================================
00063 // hex2ascii
00064 //----------------------------------------------------------------------
00065 /// @brief  Convert hexadecimal number to ascii
00066 /// @param  char* in        input buffer (hex)
00067 /// @param  char* out      output buffer (ascii)
00068 /// @param  u16 len         length of inoput buffer in bytes
00069 //======================================================================
00070 void hex2ascii( char* in, char* out, u16 length);
00071 
00072 //======================================================================
00073 // itoa
00074 //----------------------------------------------------------------------
00075 /// @brief  Converts an integer value to a null-terminated string using the specified base and stores the result
00076 /// in the array given by result parameter.
00077 /// @param  int value       integer to convert
00078 /// @param  char* result    converted string
00079 /// @param  int base        base used for conversion 2, 8, 10, 16
00080 /// @return A pointer to the resulting null-terminated string, same as parameter result
00081 //======================================================================
00082 char* itoa(int value, char* result, int base);
00083 
00084 //======================================================================
00085 // atoitok
00086 //----------------------------------------------------------------------
00087 /// @brief  Extracts from a ASCII string a value (decimal or hex) and
00088 ///         convert it to an integer value.
00089 ///         - Blank characters are skipped
00090 ///         - hexa values must begin with '0x' or '0X'
00091 ///         - '-' sign is handled
00092 ///         - returns after one value has been parsed or null character
00093 ///           or CR character has been found.
00094 /// @param  p pointer to pointer to the string to be parsed. The pointer
00095 ///         to string is modified: at the end of the call it points to
00096 ///         the character following the last parsed one.
00097 //======================================================================
00098 int atoitok(u8 **p);
00099 
00100 //======================================================================
00101 // kal_atoi
00102 //----------------------------------------------------------------------
00103 /// @brief  Extracts from a ASCII string a decimal value and
00104 ///         convert it to an integer value.
00105 ///         '-' sign is handled
00106 /// @param  s               char*                   string to convert
00107 /// @retval                 s32                     integer value
00108 //======================================================================
00109 s32 kal_atoi(u8* s);
00110 
00111 //======================================================================
00112 // kal_atoi_float
00113 //----------------------------------------------------------------------
00114 /// @brief  Extracts from a ASCII string a float value and
00115 ///         convert it to an integer value * 10^number_of_decimals.
00116 ///         '-' and '+' signs are handled
00117 /// @param  s               char*                   string to convert
00118 /// @param  d               u8                      Number of decimals
00119 /// @retval                 s32                     integer value
00120 //======================================================================
00121 s32 kal_atoi_float(u8* s, u8 d);
00122 
00123 //======================================================================
00124 // kal_atoi_hex
00125 //----------------------------------------------------------------------
00126 /// @brief  Extracts from a ASCII string a hex value and
00127 ///         convert it to an integer value.
00128 /// @param  s               char*                   string to convert
00129 /// @retval                 u32                     integer value
00130 //======================================================================
00131 u32 kal_atoi_hex(u8* s);
00132 
00133 /// Length of the Base64-encoded string based on the input binary length.
00134 #define KAL_BASE64_ENCODE_LEN(l)        (((((l) + 2) / 3) * 4) + 1)
00135 
00136 /// Length of the binary based on the real Base64-encoded string length.
00137 #define KAL_BASE64_DECODE_LEN(l)        ((l) * 3 / 4)
00138 
00139 //======================================================================
00140 // kal_base64_strlen
00141 //----------------------------------------------------------------------
00142 /// @brief  Real length (excluding padding) of Base64-encoded string
00143 ///         Note : the input string always has 4x chars, if not return 0
00144 /// @param  in              char*                   string to decode
00145 /// @retval                 u16                     string length
00146 //======================================================================
00147 u16 kal_base64_strlen(const char* in);
00148 
00149 //======================================================================
00150 // kal_base64_encode
00151 //----------------------------------------------------------------------
00152 /// @brief  Encode Base64-encoded buffer. The output buffer is supposed to have enough space
00153 ///         Beware, the stream encoding is Big Endian.
00154 /// @param  out             char*                   string result
00155 /// @param  in              u8*                     binary buffer to encode
00156 /// @param  len             u16                     length of the binary buffer in bytes
00157 /// @retval                 void 
00158 //======================================================================
00159 void kal_base64_encode(char *out, u8* in, u16 len);
00160 
00161 //======================================================================
00162 // kal_base64_decode
00163 //----------------------------------------------------------------------
00164 /// @brief  Decode Base64-encoded buffer. The output buffer is supposed to have enough space
00165 /// @param  out             u8*                     binary buffer result
00166 /// @param  in              char*                   string to decode
00167 /// @param  len             u16                     string length, excluding padding
00168 /// @retval                 void 
00169 //======================================================================
00170 void kal_base64_decode(u8 *out, const char* in, u16 len);
00171 
00172 //======================================================================
00173 // kal_tolower
00174 //----------------------------------------------------------------------
00175 /// @brief  Changes inplace to lower character a non-constant string
00176 /// @param  s               u8*                     String to transform
00177 /// @retval                 void
00178 //======================================================================
00179 void kal_tolower(u8* s);
00180 
00181 //======================================================================
00182 // kal_toupper
00183 //----------------------------------------------------------------------
00184 /// @brief  Changes inplace to upper character a non-constant string
00185 /// @param  s               u8*                     String to transform
00186 /// @retval                 void
00187 //======================================================================
00188 void kal_toupper(u8* s);
00189 
00190 //======================================================================
00191 // kal_crc8
00192 //----------------------------------------------------------------------
00193 /// @brief  Generate crc8
00194 /// @param  in          u8*                     input buffer
00195 /// @param  len         u32                     input buffer length in bytes
00196 /// @retval             void
00197 //======================================================================
00198 u8 kal_crc8(u8* in, u32 len);
00199 
00200 // =======================================================================
00201 // kal_ctf_t
00202 // -----------------------------------------------------------------------
00203 /// D7A compressed time format
00204 // =======================================================================
00205 typedef union
00206 {
00207     // bit access fields
00208     struct {
00209         /// Mantissa
00210         u8 mant : 5;
00211         /// Exponent
00212         u8 exp  : 3;
00213     } bf;
00214 
00215     // byte access
00216     u8 byte;
00217 
00218 } kal_ctf_t;
00219 
00220 //======================================================================
00221 // kal_ctf_encode
00222 //----------------------------------------------------------------------
00223 /// @brief  Compress u32 to D7A Compressed Time format (CTF).
00224 ///         The ceil flag is used to define rounding, so that 
00225 ///         kal_ctf_decode(kal_ctf_encode(val, FALSE) <= val (floor)
00226 ///         kal_ctf_decode(kal_ctf_encode(val, TRUE)  >= val (ceiling)
00227 /// @param  val         u32                     value to encode
00228 /// @param  ceil        u8                      ceil value when TRUE
00229 /// @retval             kal_ctf_t               compressed value 
00230 //======================================================================
00231 kal_ctf_t kal_ctf_encode(u32 val, u8 ceil);
00232 
00233 // =======================================================================
00234 // kal_ctf_decode
00235 // -----------------------------------------------------------------------
00236 /// @brief  Decompress from Compressed Time Format to u32
00237 /// @param  ctf         kal_ctf_t           compressed value in CTF
00238 /// @retval             u32                 decode result
00239 // =======================================================================
00240 u32 kal_ctf_decode(kal_ctf_t ctf);
00241 
00242 #endif // _KAL_CODEC_H_
00243