FluentLogger: fluent-logger-mbed A structured logger for Fluentd (mbed)

Dependents:   FluentLogger_Hello SNIC-FluentLogger-example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers uMP.h Source File

uMP.h

00001 /* uMP - micro MessagePack class
00002  * Copyright (c) 2014 Yuuichi Akagawa
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef MBED_UMP_H
00018 #define MBED_UMP_H
00019 
00020 #include <stdint.h>
00021 #include <string.h>
00022 #include <string>
00023 
00024 /** Subset of MessagePack implementation.
00025  *
00026  */
00027 class uMP {
00028 public:
00029     /** uMP
00030      */
00031     uMP();
00032     /** uMP
00033      *
00034      * @param size buffer size
00035      */
00036     explicit uMP(uint32_t size);
00037     ~uMP();
00038 
00039     /** Initialize buffer pointer
00040      */
00041     void init(){ _ptr = 0; }
00042 
00043     /** Get message size
00044      *
00045      * @return message size(bytes)
00046      */
00047     inline uint32_t get_size(){ return _ptr; }
00048 
00049     /** Get message buffer pointer
00050      *
00051      * @return Pointer of message buffer
00052      */
00053     inline uint8_t *get_buffer(){ return _buf; }
00054 
00055     /** Start array format
00056      *
00057      * @param size Number of array elements
00058      * @retval true Success
00059      * @retval false Failure
00060      */
00061     bool start_array(uint32_t size);
00062 
00063     /** Start map format
00064      *
00065      * @param size Number of map pairs
00066      * @retval true Success
00067      * @retval false Failure
00068      */
00069     bool start_map(uint32_t size);
00070 
00071     /** Set NIL message
00072      *
00073      * @retval true Success
00074      * @retval false Failure
00075      */
00076     bool set_nil();
00077 
00078     /** Set TRUE message (bool family)
00079      *
00080      * @retval true Success
00081      * @retval false Failure
00082      */
00083     bool set_true();
00084 
00085     /** Set FALSE message (bool family)
00086      *
00087      * @retval true Success
00088      * @retval false Failure
00089      */
00090     bool set_false();
00091 
00092     /** Set unsigned int message
00093      *
00094      * Auto route the optimal function.
00095      *
00096      * @param u unsigned int value(max 32bit)
00097      * @retval true Success
00098      * @retval false Failure
00099      */
00100     bool set_uint(uint32_t u); //max 32bit
00101 
00102     /** Set uint8 message
00103      *
00104      * @param u uint8_t value
00105      * @retval true Success
00106      * @retval false Failure
00107      */
00108     bool set_u8(uint8_t u);
00109 
00110     /** Set uint16 message
00111      *
00112      * @param u uint16_t value
00113      * @retval true Success
00114      * @retval false Failure
00115      */
00116     bool set_u16(uint16_t u);
00117 
00118     /** Set uint32 message
00119      *
00120      * @param u uint32_t value
00121      * @retval true Success
00122      * @retval false Failure
00123      */
00124     bool set_u32(uint32_t u);
00125 
00126     /** Set uint64 message
00127      *
00128      * @param u uint64_t value
00129      * @retval true Success
00130      * @retval false Failure
00131      */
00132     bool set_u64(uint64_t u);
00133 
00134     /** Set signed int message
00135      *
00136      * Auto route the optimal function.
00137      *
00138      * @param s signed int value(max 32bit)
00139      * @retval true Success
00140      * @retval false Failure
00141      */
00142     bool set_sint(int32_t i);  //max 32bit
00143 
00144     /** Set int8 message
00145      *
00146      * @param s int8_t value
00147      * @retval true Success
00148      * @retval false Failure
00149      */
00150     bool set_s8(int8_t i);
00151 
00152     /** Set int16 message
00153      *
00154      * @param s int16_t value
00155      * @retval true Success
00156      * @retval false Failure
00157      */
00158     bool set_s16(int16_t i);
00159 
00160     /** Set int32 message
00161      *
00162      * @param s int32_t value
00163      * @retval true Success
00164      * @retval false Failure
00165      */
00166     bool set_s32(int32_t i);
00167 
00168     /** Set int64 message
00169      *
00170      * @param s int64_t value
00171      * @retval true Success
00172      * @retval false Failure
00173      */
00174     bool set_s64(int64_t i);
00175 
00176     /** Set float(32bit) message
00177      *
00178      * @param f float value
00179      * @retval true Success
00180      * @retval false Failure
00181      */
00182     bool set_float(float f);
00183 
00184     /** Set double(64bit) message
00185      *
00186      * @param d double value
00187      * @retval true Success
00188      * @retval false Failure
00189      */
00190     bool set_double(double d);
00191 
00192     /** Set string message
00193      *
00194      * Auto route the optimal function.
00195      *
00196      * @param data Pointer of message string
00197      * @param size Size of message string (max 255 bytes)
00198      * @retval true Success
00199      * @retval false Failure
00200      */
00201     bool set_str(const char *data, uint32_t size);
00202 
00203     /** Set string message
00204      *
00205      * Auto route the optimal function.
00206      *
00207      * @param str string of message string
00208      * @retval true Success
00209      * @retval false Failure
00210      */
00211     bool set_str(const std::string& str);
00212 
00213     /** Set string message (upto 31 bytes)
00214      *
00215      * @param data Pointer of message string
00216      * @param size Size of message string (max 31 bytes)
00217      * @retval true Success
00218      * @retval false Failure
00219      */
00220     bool set_fixstr(const char *data, uint8_t size);
00221 
00222     /** Set string message (upto 256 bytes)
00223      *
00224      * @param data Pointer of message string
00225      * @param size Size of message string (max 255 bytes)
00226      * @retval true Success
00227      * @retval false Failure
00228      */
00229     bool set_str8(const char *data, uint8_t size);
00230 
00231     /** Set raw message
00232      *
00233      * Insert the pre build message into buffer.
00234      * This function is not MessagePack standard.
00235      *
00236      * @param data Pointer of message string
00237      * @param size Size of message string
00238      * @retval true Success
00239      * @retval false Failure
00240      */
00241     bool set_raw(const char *data, uint8_t size);
00242 
00243     /** associate a key with value (bool)
00244      *
00245      * @param k key string
00246      * @param v bool value(true/false)
00247      * @retval true Success
00248      * @retval false Failure
00249      */
00250     bool map(const std::string& k, bool v);
00251 
00252     /** associate a key with value (uint8_t)
00253      *
00254      * @param k key string
00255      * @param v value
00256      * @retval true Success
00257      * @retval false Failure
00258      */
00259     bool map(const std::string& k, uint8_t v);
00260 
00261     /** associate a key with value (uint16_t)
00262      *
00263      * @param k key string
00264      * @param v value
00265      * @retval true Success
00266      * @retval false Failure
00267      */
00268     bool map(const std::string& k, uint16_t v);
00269 
00270     /** associate a key with value (uint32_t)
00271      *
00272      * @param k key string
00273      * @param v value
00274      * @retval true Success
00275      * @retval false Failure
00276      */
00277     bool map(const std::string& k, uint32_t v);
00278 
00279     /** associate a key with value (int8_t)
00280      *
00281      * @param k key string
00282      * @param v value
00283      * @retval true Success
00284      * @retval false Failure
00285      */
00286     bool map(const std::string& k, int8_t v);
00287 
00288     /** associate a key with value (int16_t)
00289      *
00290      * @param k key string
00291      * @param v value
00292      * @retval true Success
00293      * @retval false Failure
00294      */
00295     bool map(const std::string& k, int16_t v);
00296 
00297     /** associate a key with value (int32_t)
00298      *
00299      * @param k key string
00300      * @param v value
00301      * @retval true Success
00302      * @retval false Failure
00303      */
00304     bool map(const std::string& k, int32_t v);
00305 
00306     /** associate a key with value (float)
00307      *
00308      * @param k key string
00309      * @param v value
00310      * @retval true Success
00311      * @retval false Failure
00312      */
00313     bool map(const std::string& k, float v);
00314 
00315     /** associate a key with value (double)
00316      *
00317      * @param k key string
00318      * @param v value
00319      * @retval true Success
00320      * @retval false Failure
00321      */
00322     bool map(const std::string& k, double v);
00323 
00324     /** associate a key with value (char * string)
00325      *
00326      * @param k key string
00327      * @param v value
00328      * @retval true Success
00329      * @retval false Failure
00330      */
00331     bool map(const std::string& k, const char *v);
00332 
00333     /** associate a key with value (string)
00334      *
00335      * @param k key string
00336      * @param v value
00337      * @retval true Success
00338      * @retval false Failure
00339      */
00340     bool map(const std::string& k, const std::string& v);
00341 
00342 private:
00343     enum MpTag{
00344         TAG_POSITIVE_FIXNUM = 0x00,
00345         TAG_FIXMAP          = 0x80,
00346         TAG_FIXARRAY        = 0x90,
00347         TAG_FIXSTR          = 0xa0,
00348         TAG_NIL             = 0xc0,
00349         TAG_FALSE           = 0xc2,
00350         TAG_TRUE            = 0xc3,
00351 //      TAG_BIN8            = 0xc4,
00352 //      TAG_BIN16           = 0xc5,
00353 //      TAG_BIN32           = 0xc6,
00354 //      TAG_EXT8            = 0xc7,
00355 //      TAG_EXT16           = 0xc8,
00356 //      TAG_EXT32           = 0xc9,
00357         TAG_FLOAT32         = 0xca,
00358         TAG_FLOAT64         = 0xcb,
00359         TAG_U8              = 0xcc,
00360         TAG_U16             = 0xcd,
00361         TAG_U32             = 0xce,
00362         TAG_U64             = 0xcf,
00363         TAG_S8              = 0xd0,
00364         TAG_S16             = 0xd1,
00365         TAG_S32             = 0xd2,
00366         TAG_S64             = 0xd3,
00367 //      TAG_FIXEXT1         = 0xd4,
00368 //      TAG_FIXEXT2         = 0xd5,
00369 //      TAG_FIXEXT16        = 0xd8,
00370         TAG_STR8            = 0xd9,
00371 //      TAG_STR16           = 0xda,
00372 //      TAG_STR32           = 0xdb,
00373 //      TAG_ARRAY16         = 0xdc,
00374 //      TAG_ARRAY32         = 0xdd,
00375 //      TAG_MAP16           = 0xde,
00376 //      TAG_MAP32           = 0xdf,
00377         TAG_NEGATIVE_FIXNUM = 0xe0
00378     };
00379 
00380     static const uint16_t DEFAULT_BUFFSIZE = 128;
00381     uint8_t   *_buf;
00382     uint32_t  _ptr;
00383     uint32_t  _nbuf;
00384 
00385     /** Insert sigle byte fomrat message
00386      *
00387      * @param c single byte message format data
00388      * @retval true Success
00389      * @retval false Failure
00390      */
00391     bool set_buffer(const uint8_t c);
00392 
00393     /** Insert multi byte fomrat message
00394      *
00395      * @param c Pointer of message data
00396      * @param sise Size of message data
00397      * @retval true Success
00398      * @retval false Failure
00399      */
00400     bool set_buffer(const uint8_t *c, size_t size);
00401 
00402     /** Endian converter - 16bit data
00403      *
00404      * @param t 16bit data
00405      * @return converted 16bit data
00406      */
00407     template<typename T> T to_be16(T t);
00408 
00409     /** Endian converter - 32bit data
00410      *
00411      * @param t 32bit data
00412      * @return converted 32bit data
00413      */
00414     template<typename T> T to_be32(T t);
00415 
00416     /** Endian converter - 64bit data
00417      *
00418      * @param t 64bit data
00419      * @return converted 64bit data
00420      */
00421     template<typename T> T to_be64(T t);
00422 };
00423 
00424 #endif