Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

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 <stdlib.h>
00030 #include <string.h>
00031 #include "mbed.h"
00032 #include "MbedDataSink.h"
00033 #include "MbedClient.h"
00034 #include "logging.h"
00035 
00036 
00037 MbedDataSink::MbedDataSink(TCPSocketConnection& sock) : _sock(sock), _len(0)
00038 {
00039 }
00040 
00041 MbedDataSink::~MbedDataSink()
00042 {
00043 }
00044 
00045 size_t MbedDataSink::write(char c)
00046 {
00047     while (MBED_SINK_BUFFER_SIZE - _len < 1) {
00048         if (!send())
00049             return 0;
00050     }
00051     
00052     _buf[_len++] = c;
00053     return 1;
00054 }
00055 
00056 size_t MbedDataSink::write(void *buf, size_t length)
00057 {
00058     size_t sent = 0, len;
00059     
00060     while (sent < length) {
00061         while (MBED_SINK_BUFFER_SIZE - _len < 1) {
00062             if (!send())
00063                 return 0;
00064         }
00065         
00066         if (MBED_SINK_BUFFER_SIZE - _len >= length-sent)
00067             len = length-sent;
00068         else
00069             len = MBED_SINK_BUFFER_SIZE - _len;
00070         
00071         memcpy(_buf+_len, (char*)buf+sent, len);
00072         _len += len;
00073         sent += len;
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     snprintf(str, 24, "%lu", number);
00087     return write(str);
00088 }
00089 
00090 bool MbedDataSink::flush()
00091 {
00092     while (_len > 0) {
00093         if (!send())
00094             return false;
00095     }
00096     
00097     return true;
00098 }
00099 
00100 bool MbedDataSink::send()
00101 {
00102     int ret;
00103     
00104     if (!_sock.is_connected())
00105         return false;
00106 
00107     _sock.set_blocking(true);
00108     ret = _sock.send(_buf, _len);
00109     
00110     //TODO: fix bug in u-blox so no longer wait is needed
00111 //    wait(0.5);
00112     
00113     if (ret < 0){
00114         aError("Sink: Send failed.\n");
00115         return false;
00116     }
00117     
00118     // move rest of buffer
00119     if ((ret > 0) && (ret < _len))
00120         memmove(_buf, _buf+ret, _len-ret);
00121     
00122     _len -= ret;
00123     return true;
00124 }
00125 
00126 void MbedDataSink::reset()
00127 {
00128     _len = 0;
00129 }