Thinger.io Client Library for ARM mbed platform. This is a generic library that provides a base class that can be used to other develop hardware specific libraries.
Fork of ThingerClient by
thinger/thinger_encoder.hpp@4:de51256455f7, 2015-12-26 (annotated)
- Committer:
- alvarolb
- Date:
- Sat Dec 26 13:18:01 2015 +0000
- Revision:
- 4:de51256455f7
- Parent:
- 0:b75d784c7c1a
Adapter pson to properly work in ARM Mbed old compiler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alvarolb | 0:b75d784c7c1a | 1 | // The MIT License (MIT) |
alvarolb | 0:b75d784c7c1a | 2 | // |
alvarolb | 0:b75d784c7c1a | 3 | // Copyright (c) 2015 THINGER LTD |
alvarolb | 0:b75d784c7c1a | 4 | // Author: alvarolb@gmail.com (Alvaro Luis Bustamante) |
alvarolb | 0:b75d784c7c1a | 5 | // |
alvarolb | 0:b75d784c7c1a | 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
alvarolb | 0:b75d784c7c1a | 7 | // of this software and associated documentation files (the "Software"), to deal |
alvarolb | 0:b75d784c7c1a | 8 | // in the Software without restriction, including without limitation the rights |
alvarolb | 0:b75d784c7c1a | 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
alvarolb | 0:b75d784c7c1a | 10 | // copies of the Software, and to permit persons to whom the Software is |
alvarolb | 0:b75d784c7c1a | 11 | // furnished to do so, subject to the following conditions: |
alvarolb | 0:b75d784c7c1a | 12 | // |
alvarolb | 0:b75d784c7c1a | 13 | // The above copyright notice and this permission notice shall be included in |
alvarolb | 0:b75d784c7c1a | 14 | // all copies or substantial portions of the Software. |
alvarolb | 0:b75d784c7c1a | 15 | // |
alvarolb | 0:b75d784c7c1a | 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
alvarolb | 0:b75d784c7c1a | 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
alvarolb | 0:b75d784c7c1a | 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
alvarolb | 0:b75d784c7c1a | 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
alvarolb | 0:b75d784c7c1a | 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
alvarolb | 0:b75d784c7c1a | 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
alvarolb | 0:b75d784c7c1a | 22 | // THE SOFTWARE. |
alvarolb | 0:b75d784c7c1a | 23 | |
alvarolb | 0:b75d784c7c1a | 24 | #ifndef THINGER_ENCODER_HPP |
alvarolb | 0:b75d784c7c1a | 25 | #define THINGER_ENCODER_HPP |
alvarolb | 0:b75d784c7c1a | 26 | |
alvarolb | 0:b75d784c7c1a | 27 | #include "thinger_message.hpp" |
alvarolb | 0:b75d784c7c1a | 28 | #include "thinger_io.hpp" |
alvarolb | 0:b75d784c7c1a | 29 | |
alvarolb | 0:b75d784c7c1a | 30 | namespace thinger{ |
alvarolb | 0:b75d784c7c1a | 31 | |
alvarolb | 0:b75d784c7c1a | 32 | class thinger_encoder : public protoson::pson_encoder{ |
alvarolb | 0:b75d784c7c1a | 33 | |
alvarolb | 0:b75d784c7c1a | 34 | protected: |
alvarolb | 0:b75d784c7c1a | 35 | virtual void write(const void *buffer, size_t size){ |
alvarolb | 0:b75d784c7c1a | 36 | protoson::pson_encoder::write(buffer, size); |
alvarolb | 0:b75d784c7c1a | 37 | } |
alvarolb | 0:b75d784c7c1a | 38 | |
alvarolb | 0:b75d784c7c1a | 39 | public: |
alvarolb | 0:b75d784c7c1a | 40 | void encode(thinger_message& message){ |
alvarolb | 0:b75d784c7c1a | 41 | if(message.get_stream_id()!=0){ |
alvarolb | 0:b75d784c7c1a | 42 | pb_encode_varint(thinger_message::STREAM_ID, message.get_stream_id()); |
alvarolb | 0:b75d784c7c1a | 43 | } |
alvarolb | 0:b75d784c7c1a | 44 | if(message.get_signal_flag()!=thinger_message::NONE){ |
alvarolb | 0:b75d784c7c1a | 45 | pb_encode_varint(thinger_message::SIGNAL_FLAG, message.get_signal_flag()); |
alvarolb | 0:b75d784c7c1a | 46 | } |
alvarolb | 0:b75d784c7c1a | 47 | if(message.has_resource()){ |
alvarolb | 0:b75d784c7c1a | 48 | pb_encode_tag(protoson::pson_type, thinger_message::RESOURCE); |
alvarolb | 0:b75d784c7c1a | 49 | protoson::pson_encoder::encode(message.get_resources()); |
alvarolb | 0:b75d784c7c1a | 50 | } |
alvarolb | 0:b75d784c7c1a | 51 | if(message.has_data()){ |
alvarolb | 0:b75d784c7c1a | 52 | pb_encode_tag(protoson::pson_type, thinger_message::PSON_PAYLOAD); |
alvarolb | 0:b75d784c7c1a | 53 | protoson::pson_encoder::encode((protoson::pson&) message); |
alvarolb | 0:b75d784c7c1a | 54 | } |
alvarolb | 0:b75d784c7c1a | 55 | } |
alvarolb | 0:b75d784c7c1a | 56 | }; |
alvarolb | 0:b75d784c7c1a | 57 | |
alvarolb | 0:b75d784c7c1a | 58 | class thinger_write_encoder : public thinger_encoder{ |
alvarolb | 0:b75d784c7c1a | 59 | public: |
alvarolb | 0:b75d784c7c1a | 60 | thinger_write_encoder(thinger_io& io) : io_(io) |
alvarolb | 0:b75d784c7c1a | 61 | {} |
alvarolb | 0:b75d784c7c1a | 62 | |
alvarolb | 0:b75d784c7c1a | 63 | protected: |
alvarolb | 0:b75d784c7c1a | 64 | virtual void write(const void *buffer, size_t size){ |
alvarolb | 0:b75d784c7c1a | 65 | io_.write((const char*)buffer, size); |
alvarolb | 0:b75d784c7c1a | 66 | protoson::pson_encoder::write(buffer, size); |
alvarolb | 0:b75d784c7c1a | 67 | } |
alvarolb | 0:b75d784c7c1a | 68 | |
alvarolb | 0:b75d784c7c1a | 69 | private: |
alvarolb | 0:b75d784c7c1a | 70 | thinger_io& io_; |
alvarolb | 0:b75d784c7c1a | 71 | }; |
alvarolb | 0:b75d784c7c1a | 72 | |
alvarolb | 0:b75d784c7c1a | 73 | class thinger_memory_encoder : public thinger_encoder{ |
alvarolb | 0:b75d784c7c1a | 74 | public: |
alvarolb | 0:b75d784c7c1a | 75 | thinger_memory_encoder(uint8_t* buffer, size_t size) : buffer_(buffer), size_(size){} |
alvarolb | 0:b75d784c7c1a | 76 | |
alvarolb | 0:b75d784c7c1a | 77 | protected: |
alvarolb | 0:b75d784c7c1a | 78 | virtual void write(const void *buffer, size_t size){ |
alvarolb | 0:b75d784c7c1a | 79 | memcpy(buffer_ + written_, buffer, size); |
alvarolb | 0:b75d784c7c1a | 80 | protoson::pson_encoder::write(buffer, size); |
alvarolb | 0:b75d784c7c1a | 81 | } |
alvarolb | 0:b75d784c7c1a | 82 | |
alvarolb | 0:b75d784c7c1a | 83 | private: |
alvarolb | 0:b75d784c7c1a | 84 | uint8_t* buffer_; |
alvarolb | 0:b75d784c7c1a | 85 | size_t size_; |
alvarolb | 0:b75d784c7c1a | 86 | }; |
alvarolb | 0:b75d784c7c1a | 87 | |
alvarolb | 0:b75d784c7c1a | 88 | } |
alvarolb | 0:b75d784c7c1a | 89 | |
alvarolb | 0:b75d784c7c1a | 90 | #endif |