mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.

Dependents:   MbedSmartRestMain MbedSmartRestMain

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MbedDataSink.cpp Source File

MbedDataSink.cpp

00001 /*
00002  * MbedDataSink.cpp
00003  *
00004  * Created on: Feb 1, 2013
00005  * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
00006  *
00007  * Copyright (c) 2013 Cumulocity GmbH
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining
00010  * a copy of this software and associated documentation files (the
00011  * "Software"), to deal in the Software without restriction, including
00012  * without limitation the rights to use, copy, modify, merge, publish,
00013  * distribute, sublicense, and/or sell copies of the Software, and to
00014  * permit persons to whom the Software is furnished to do so, subject to
00015  * the following conditions:
00016  *
00017  * The above copyright notice and this permission notice shall be
00018  * included in all copies or substantial portions of the Software.
00019  *
00020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00022  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00023  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00024  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00025  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00026  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 #include "MbedDataSink.h"
00030 #include "MbedClient.h"
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #include "mbed.h"
00035 
00036 MbedDataSink::MbedDataSink(TCPSocketConnection& sock) : _len(0), _sock(sock)
00037 {
00038 }
00039 
00040 MbedDataSink::~MbedDataSink()
00041 {
00042 }
00043 
00044 size_t MbedDataSink::write(char c)
00045 {
00046     while (MBED_SINK_BUFFER_SIZE - _len < 1) {
00047         if (!send())
00048             return 0;
00049     }
00050     
00051     _buf[_len++] = c;
00052     return 1;
00053 }
00054 
00055 size_t MbedDataSink::write(void *buf, size_t length)
00056 {
00057     size_t sent = 0, len;
00058     
00059     while (sent < length) {
00060         while (MBED_SINK_BUFFER_SIZE - _len < 1) {
00061             if (!send())
00062                 return 0;
00063         }
00064         
00065         if (MBED_SINK_BUFFER_SIZE - _len >= length-sent)
00066             len = length-sent;
00067         else
00068             len = MBED_SINK_BUFFER_SIZE - _len;
00069         
00070         memcpy(_buf+_len, (char*)buf+sent, len);
00071         _len += len;
00072         sent += len;
00073     }
00074     
00075     return length;
00076 }
00077 
00078 size_t MbedDataSink::write(const char *str)
00079 {
00080     return write((void*)str, strlen(str));
00081 }
00082 
00083 size_t MbedDataSink::write(unsigned long number)
00084 {
00085     char str[24];
00086     
00087     snprintf(str, 24, "%lu", number);
00088     return write(str);
00089 }
00090 
00091 bool MbedDataSink::flush()
00092 {
00093     while (_len > 0) {
00094         if (!send())
00095             return false;
00096     }
00097     
00098     return true;
00099 }
00100 
00101 bool MbedDataSink::send()
00102 {
00103     int ret;
00104     
00105     if (!_sock.is_connected())
00106         return false;
00107 
00108     _sock.set_blocking(true);
00109     ret = _sock.send(_buf, _len);
00110     
00111     //TODO: fix bug in u-blox so no longer wait is needed
00112     wait(0.5);
00113     
00114     if (ret < 0){
00115         puts("Send failed.");
00116         return false;
00117     }
00118     
00119     // move rest of buffer
00120     if ((ret > 0) && (ret < _len))
00121         memmove(_buf, _buf+ret, _len-ret);
00122     
00123     _len -= ret;
00124     return true;
00125 }
00126 
00127 void MbedDataSink::reset()
00128 {
00129     _len = 0;
00130 }