Midnight Cow / ThingerIO

Dependencies:   DHT WIZnetInterface mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thinger_decoder.hpp Source File

thinger_decoder.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_DECODER_HPP
00025 #define THINGER_DECODER_HPP
00026 
00027 #include "pson.h"
00028 #include "thinger_io.hpp"
00029 #include "thinger_message.hpp"
00030 
00031 namespace thinger{
00032 
00033     class thinger_decoder : public protoson::pson_decoder{
00034     public:
00035         void decode(thinger_message&  message, size_t size){
00036             size_t start_read = bytes_read();
00037             while(size-(bytes_read()-start_read)>0) {
00038                 protoson::pb_wire_type wire_type;
00039                 uint32_t field_number;
00040                 pb_decode_tag(wire_type, field_number);
00041                 switch (wire_type) {
00042                     case protoson::length_delimited:{
00043                         uint32_t size = pb_decode_varint32();
00044                         void *data = NULL;
00045                         switch (field_number) {
00046                             case thinger_message::THING_ID:
00047                                 data = protoson::pool.allocate(size + 1);
00048                                 pb_read_string((char *) data, size);
00049                                 message.set_thing_id((const char *) data);
00050                                 break;
00051                             default:
00052                                 pb_skip(size);
00053                                 break;
00054                         }
00055                     }
00056                         break;
00057                     case protoson::varint: {
00058                         switch (field_number) {
00059                             case thinger_message::SIGNAL_FLAG:
00060                                 message.set_signal_flag((thinger_message::signal_flag)pb_decode_varint32());
00061                                 break;
00062                             case thinger_message::STREAM_ID:
00063                                 message.set_stream_id(pb_decode_varint32());
00064                                 break;
00065                             default:
00066                                 pb_skip_varint();
00067                                 break;
00068                         }
00069                         break;
00070                     }
00071                     case protoson::pson_type:
00072                         switch(field_number){
00073                             case thinger_message::RESOURCE:
00074                                 protoson::pson_decoder::decode(message.get_resources());
00075                                 break;
00076                             case thinger_message::PSON:
00077                                 protoson::pson_decoder::decode(((protoson::pson&) message));
00078                                 break;
00079                             default:
00080                                 break;
00081                         }
00082                         break;
00083                     case protoson::fixed_32:
00084                         pb_skip(4);
00085                         break;
00086                     case protoson::fixed_64:
00087                         pb_skip(8);
00088                         break;
00089                     default:
00090                         break;
00091                 }
00092             }
00093         }
00094     };
00095 
00096     class thinger_read_decoder : public thinger_decoder{
00097     public:
00098         thinger_read_decoder(thinger_io& io) : io_(io)
00099         {}
00100 
00101     protected:
00102         virtual bool read(void* buffer, size_t size){
00103             io_.read((char*)buffer, size);
00104             protoson::pson_decoder::read(buffer, size);
00105             return true;
00106         }
00107 
00108     private:
00109         thinger_io& io_;
00110     };
00111 
00112     class thinger_memory_decoder : public thinger_decoder{
00113 
00114     public:
00115         thinger_memory_decoder(uint8_t* buffer, size_t size) : buffer_(buffer), size_(size){}
00116 
00117     protected:
00118         virtual bool read(void* buffer, size_t size){
00119             memcpy(buffer, buffer_ + read_, size);
00120             return protoson::pson_decoder::read(buffer, size);
00121         }
00122 
00123     private:
00124         uint8_t* buffer_;
00125         size_t size_;
00126 
00127     };
00128 
00129 }
00130 
00131 #endif