Midnight Cow / ThingerIO

Dependencies:   DHT WIZnetInterface mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thinger_encoder.hpp Source File

thinger_encoder.hpp

00001 // The MIT License (MIT)
00002 //
00003 // Copyright (c) 2015 THINGER LTD
00004 // Author: alvarolb@gmail.com (Alvaro Luis Bustamante)
00005 //
00006 // Permission is hereby granted, free of charge, to any person obtaining a copy
00007 // of this software and associated documentation files (the "Software"), to deal
00008 // in the Software without restriction, including without limitation the rights
00009 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 // copies of the Software, and to permit persons to whom the Software is
00011 // furnished to do so, subject to the following conditions:
00012 //
00013 // The above copyright notice and this permission notice shall be included in
00014 // all copies or substantial portions of the Software.
00015 //
00016 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022 // THE SOFTWARE.
00023 
00024 #ifndef THINGER_ENCODER_HPP
00025 #define THINGER_ENCODER_HPP
00026 
00027 #include "thinger_message.hpp"
00028 #include "thinger_io.hpp"
00029 
00030 namespace thinger{
00031 
00032 class thinger_encoder : public protoson::pson_encoder{
00033 
00034 protected:
00035     virtual void write(const void *buffer, size_t size){
00036         protoson::pson_encoder::write(buffer, size);
00037     }
00038 
00039 public:
00040     void encode(thinger_message& message){
00041         if(message.get_stream_id()!=0){
00042             pb_encode_varint(thinger_message::STREAM_ID, message.get_stream_id());
00043         }
00044         if(message.get_signal_flag()!=thinger_message::NONE){
00045             pb_encode_varint(thinger_message::SIGNAL_FLAG, message.get_signal_flag());
00046         }
00047         if(message.get_thing_id()!=NULL){
00048             pb_encode_string(message.get_thing_id(), thinger_message::THING_ID);
00049         }
00050         if(message.has_resource()){
00051             pb_encode_tag(protoson::pson_type, thinger_message::RESOURCE);
00052             protoson::pson_encoder::encode(message.get_resources());
00053         }
00054         if(message.has_data()){
00055             pb_encode_tag(protoson::pson_type, thinger_message::PSON);
00056             protoson::pson_encoder::encode((protoson::pson&) message);
00057         }
00058     }
00059 };
00060 
00061 class thinger_write_encoder : public thinger_encoder{
00062     public:
00063         thinger_write_encoder(thinger_io& io) : io_(io)
00064         {}
00065 
00066     protected:
00067         virtual void write(const void *buffer, size_t size){
00068             io_.write((const char*)buffer, size);
00069             protoson::pson_encoder::write(buffer, size);
00070         }
00071 
00072     private:
00073         thinger_io& io_;
00074 };
00075 
00076 class thinger_memory_encoder : public thinger_encoder{
00077     public:
00078         thinger_memory_encoder(uint8_t* buffer, size_t size) : buffer_(buffer), size_(size){}
00079 
00080     protected:
00081         virtual void write(const void *buffer, size_t size){
00082             memcpy(buffer_ + written_, buffer, size);
00083             protoson::pson_encoder::write(buffer, size);
00084         }
00085 
00086     private:
00087         uint8_t* buffer_;
00088         size_t size_;
00089 };
00090 
00091 }
00092 
00093 #endif