Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
xinlei
Date:
Mon Feb 16 09:15:55 2015 +0000
Revision:
14:56478403e340
Parent:
0:099f76422485
Child:
17:b3a4b4bdfc59
Various minor bug fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * MbedDataSink.cpp
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Feb 1, 2013
Cumulocity 0:099f76422485 5 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
Cumulocity 0:099f76422485 6 *
Cumulocity 0:099f76422485 7 * Copyright (c) 2013 Cumulocity GmbH
Cumulocity 0:099f76422485 8 *
Cumulocity 0:099f76422485 9 * Permission is hereby granted, free of charge, to any person obtaining
Cumulocity 0:099f76422485 10 * a copy of this software and associated documentation files (the
Cumulocity 0:099f76422485 11 * "Software"), to deal in the Software without restriction, including
Cumulocity 0:099f76422485 12 * without limitation the rights to use, copy, modify, merge, publish,
Cumulocity 0:099f76422485 13 * distribute, sublicense, and/or sell copies of the Software, and to
Cumulocity 0:099f76422485 14 * permit persons to whom the Software is furnished to do so, subject to
Cumulocity 0:099f76422485 15 * the following conditions:
Cumulocity 0:099f76422485 16 *
Cumulocity 0:099f76422485 17 * The above copyright notice and this permission notice shall be
Cumulocity 0:099f76422485 18 * included in all copies or substantial portions of the Software.
Cumulocity 0:099f76422485 19 *
Cumulocity 0:099f76422485 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Cumulocity 0:099f76422485 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Cumulocity 0:099f76422485 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Cumulocity 0:099f76422485 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Cumulocity 0:099f76422485 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Cumulocity 0:099f76422485 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Cumulocity 0:099f76422485 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Cumulocity 0:099f76422485 27 */
Cumulocity 0:099f76422485 28
Cumulocity 0:099f76422485 29 #include "MbedDataSink.h"
Cumulocity 0:099f76422485 30 #include "MbedClient.h"
Cumulocity 0:099f76422485 31 #include <stdlib.h>
Cumulocity 0:099f76422485 32 #include <string.h>
Cumulocity 0:099f76422485 33
Cumulocity 0:099f76422485 34 #include "mbed.h"
xinlei 14:56478403e340 35 size_t packetSize = 0;
Cumulocity 0:099f76422485 36
Cumulocity 0:099f76422485 37 MbedDataSink::MbedDataSink(TCPSocketConnection& sock) : _len(0), _sock(sock)
Cumulocity 0:099f76422485 38 {
Cumulocity 0:099f76422485 39 }
Cumulocity 0:099f76422485 40
Cumulocity 0:099f76422485 41 MbedDataSink::~MbedDataSink()
Cumulocity 0:099f76422485 42 {
Cumulocity 0:099f76422485 43 }
Cumulocity 0:099f76422485 44
Cumulocity 0:099f76422485 45 size_t MbedDataSink::write(char c)
Cumulocity 0:099f76422485 46 {
Cumulocity 0:099f76422485 47 while (MBED_SINK_BUFFER_SIZE - _len < 1) {
Cumulocity 0:099f76422485 48 if (!send())
Cumulocity 0:099f76422485 49 return 0;
Cumulocity 0:099f76422485 50 }
Cumulocity 0:099f76422485 51
Cumulocity 0:099f76422485 52 _buf[_len++] = c;
xinlei 14:56478403e340 53 ++packetSize;
Cumulocity 0:099f76422485 54 return 1;
Cumulocity 0:099f76422485 55 }
Cumulocity 0:099f76422485 56
Cumulocity 0:099f76422485 57 size_t MbedDataSink::write(void *buf, size_t length)
Cumulocity 0:099f76422485 58 {
Cumulocity 0:099f76422485 59 size_t sent = 0, len;
Cumulocity 0:099f76422485 60
Cumulocity 0:099f76422485 61 while (sent < length) {
Cumulocity 0:099f76422485 62 while (MBED_SINK_BUFFER_SIZE - _len < 1) {
Cumulocity 0:099f76422485 63 if (!send())
Cumulocity 0:099f76422485 64 return 0;
Cumulocity 0:099f76422485 65 }
Cumulocity 0:099f76422485 66
Cumulocity 0:099f76422485 67 if (MBED_SINK_BUFFER_SIZE - _len >= length-sent)
Cumulocity 0:099f76422485 68 len = length-sent;
Cumulocity 0:099f76422485 69 else
Cumulocity 0:099f76422485 70 len = MBED_SINK_BUFFER_SIZE - _len;
Cumulocity 0:099f76422485 71
Cumulocity 0:099f76422485 72 memcpy(_buf+_len, (char*)buf+sent, len);
Cumulocity 0:099f76422485 73 _len += len;
Cumulocity 0:099f76422485 74 sent += len;
Cumulocity 0:099f76422485 75 }
xinlei 14:56478403e340 76 packetSize += length;
Cumulocity 0:099f76422485 77 return length;
Cumulocity 0:099f76422485 78 }
Cumulocity 0:099f76422485 79
Cumulocity 0:099f76422485 80 size_t MbedDataSink::write(const char *str)
Cumulocity 0:099f76422485 81 {
Cumulocity 0:099f76422485 82 return write((void*)str, strlen(str));
Cumulocity 0:099f76422485 83 }
Cumulocity 0:099f76422485 84
Cumulocity 0:099f76422485 85 size_t MbedDataSink::write(unsigned long number)
Cumulocity 0:099f76422485 86 {
Cumulocity 0:099f76422485 87 char str[24];
Cumulocity 0:099f76422485 88
xinlei 14:56478403e340 89 packetSize += snprintf(str, 24, "%lu", number);
Cumulocity 0:099f76422485 90 return write(str);
Cumulocity 0:099f76422485 91 }
Cumulocity 0:099f76422485 92
Cumulocity 0:099f76422485 93 bool MbedDataSink::flush()
Cumulocity 0:099f76422485 94 {
Cumulocity 0:099f76422485 95 while (_len > 0) {
Cumulocity 0:099f76422485 96 if (!send())
Cumulocity 0:099f76422485 97 return false;
Cumulocity 0:099f76422485 98 }
Cumulocity 0:099f76422485 99
Cumulocity 0:099f76422485 100 return true;
Cumulocity 0:099f76422485 101 }
Cumulocity 0:099f76422485 102
Cumulocity 0:099f76422485 103 bool MbedDataSink::send()
Cumulocity 0:099f76422485 104 {
Cumulocity 0:099f76422485 105 int ret;
Cumulocity 0:099f76422485 106
Cumulocity 0:099f76422485 107 if (!_sock.is_connected())
Cumulocity 0:099f76422485 108 return false;
Cumulocity 0:099f76422485 109
Cumulocity 0:099f76422485 110 _sock.set_blocking(true);
Cumulocity 0:099f76422485 111 ret = _sock.send(_buf, _len);
Cumulocity 0:099f76422485 112
Cumulocity 0:099f76422485 113 //TODO: fix bug in u-blox so no longer wait is needed
Cumulocity 0:099f76422485 114 wait(0.5);
Cumulocity 0:099f76422485 115
Cumulocity 0:099f76422485 116 if (ret < 0){
Cumulocity 0:099f76422485 117 puts("Send failed.");
Cumulocity 0:099f76422485 118 return false;
Cumulocity 0:099f76422485 119 }
Cumulocity 0:099f76422485 120
Cumulocity 0:099f76422485 121 // move rest of buffer
Cumulocity 0:099f76422485 122 if ((ret > 0) && (ret < _len))
Cumulocity 0:099f76422485 123 memmove(_buf, _buf+ret, _len-ret);
Cumulocity 0:099f76422485 124
Cumulocity 0:099f76422485 125 _len -= ret;
Cumulocity 0:099f76422485 126 return true;
Cumulocity 0:099f76422485 127 }
Cumulocity 0:099f76422485 128
Cumulocity 0:099f76422485 129 void MbedDataSink::reset()
Cumulocity 0:099f76422485 130 {
Cumulocity 0:099f76422485 131 _len = 0;
Cumulocity 0:099f76422485 132 }