The BERG Cloud connection library for mbed. This works in conjunction with the BERG Cloud Devshield available via http://bergcloud.com/platform/devkit/

Dependents:   LittleCounter-Example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BERGCloudMessageBase.h Source File

BERGCloudMessageBase.h

00001 /*
00002 
00003 BERGCloud message pack/unpack
00004 
00005 Based on MessagePack http://msgpack.org/
00006 
00007 Copyright (c) 2013 BERG Cloud Ltd. http://bergcloud.com/
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 
00027 */
00028 
00029 #ifndef BERGCLOUDMESSAGEBASE_H
00030 #define BERGCLOUDMESSAGEBASE_H
00031 
00032 #include "BERGCloudConfig.h"
00033 #include "BERGCloudMessageBuffer.h"
00034 #include "BERGCloudLogPrint.h"
00035 
00036 #define _LOG_PACK_ERROR_NO_SPACE    _LOG("Pack: Out of space.\r\n")
00037 #define _LOG_UNPACK_ERROR_TYPE      _LOG("Unpack: Can't convert to this variable type.\r\n")
00038 #define _LOG_UNPACK_ERROR_RANGE     _LOG("Unpack: Value out of range for this variable type.\r\n")
00039 #define _LOG_UNPACK_ERROR_NO_DATA   _LOG("Unpack: No more data.\r\n")
00040 
00041 #define IN_RANGE(value, min, max) ((value >= min) && (value <= max))
00042 
00043 #define MAX_MAP_KEY_STRING_LENGTH (16)
00044 
00045 class BERGCloudMessageBase : public BERGCloudMessageBuffer
00046 {
00047 public:
00048   BERGCloudMessageBase(void);
00049   ~BERGCloudMessageBase(void);
00050 
00051   /*
00052    *  Pack methods
00053    */
00054 
00055   /* Pack an unsigned integer */
00056   bool pack(uint8_t n);
00057   bool pack(uint16_t n);
00058   bool pack(uint32_t n);
00059   /* Pack a signed integer */
00060   bool pack(int8_t n);
00061   bool pack(int16_t n);
00062   bool pack(int32_t n);
00063   /* Pack a float */
00064   bool pack(float n);
00065   /* Pack a boolean */
00066   bool pack(bool n);
00067 
00068   /* Pack a nil type */
00069   bool pack_nil(void);
00070   /* Pack an array header, giving the number of items that will follow */
00071   bool pack_array(uint16_t items);
00072   /* Pack a map header, giving the number of key-value pairs that will follow */
00073   bool pack_map(uint16_t items);
00074 
00075   /* Pack an array of data */
00076   bool pack(uint8_t *data, uint16_t sizeInBytes);
00077   /* Pack a null-terminated C string */
00078   bool pack(const char *string);
00079 
00080   /*
00081    *  Unpack methods
00082    */
00083 
00084   /* Unpack an unsigned integer */
00085   bool unpack(uint8_t& n);
00086   bool unpack(uint16_t& n);
00087   bool unpack(uint32_t& n);
00088   /* Unpack a signed integer */
00089   bool unpack(int8_t& n);
00090   bool unpack(int16_t& n);
00091   bool unpack(int32_t& n);
00092   /* Unpack a float */
00093   bool unpack(float& n);
00094   /* Unpack a boolean */
00095   bool unpack(bool& n);
00096 
00097   /* Unpack a nil type */
00098   bool unpack_nil(void);
00099 
00100   /* Unpack an array header, this gives the number of items that follow */
00101   bool unpack_array(uint16_t& items);
00102   /* Unpack a map header, this gives the number of key-value pairs that follow */
00103   bool unpack_map(uint16_t& items);
00104 
00105   /* Get the messagePack type of the next item without unpacking it, */
00106   /* returns false if there are no more items */
00107   bool unpack_peek(uint8_t& messagePackType);
00108 
00109 #ifdef BERGCLOUD_LOG
00110   /* Prints the type of the next item without unpacking it, */
00111   /* returns false if there are no more items */
00112   bool unpack_peek(void);
00113   /* Prints the type of all items without unpacking them */
00114   void print(void);
00115   /* Print the raw MessagePack bytes of a message */
00116   void print_bytes(void);
00117 #endif
00118 
00119   /* Skip the next item */
00120   bool unpack_skip(void);
00121   /* Restart unpacking from the beginning */
00122   bool unpack_restart();
00123   /* Moves to the value associated with the map key 'key' */
00124   bool unpack_find(const char *key);
00125   /* Moves to the value associated with array index 'i' */
00126   bool unpack_find(uint16_t i);
00127 
00128   /* Unpack a null-terminated C string */
00129   bool unpack(char *string, uint32_t maxSizeInBytes);
00130   /* Unpack an array of data */
00131   bool unpack(uint8_t *data, uint32_t maxSizeInBytes, uint32_t *sizeInBytes = NULL);
00132 
00133 protected:
00134   /* Internal methods */
00135   uint16_t strlen(const char *string);
00136   bool strcompare(const char *s1, const char *s2);
00137   bool pack_raw_header(uint16_t sizeInBytes);
00138   bool pack_raw_data(uint8_t *data, uint16_t sizeInBytes);
00139   bool unpack_raw_header(uint16_t *sizeInBytes);
00140   bool unpack_raw_data(uint8_t *data, uint16_t packedSizeInBytes, uint16_t bufferSizeInBytes);
00141   bool getInteger(void *value, bool valueIsSigned, int32_t min, uint32_t max);
00142 };
00143 
00144 #endif // #ifndef BERGCLOUDMESSAGEBASE_H
00145