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 MbedDataSource.cpp Source File

MbedDataSource.cpp

00001 /*
00002  * MbedDataSource.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 "MbedDataSource.h"
00030 #include "stdio.h"
00031 
00032 MbedDataSource::MbedDataSource(TCPSocketConnection& sock) :
00033     _sock(sock),
00034     _timeout(0),
00035     _isTimedOut(false)
00036 {
00037     _offset = _len = 0;
00038 }
00039 
00040 MbedDataSource::~MbedDataSource()
00041 {
00042 }
00043 
00044 char MbedDataSource::read()
00045 {
00046     while (_offset == _len) {
00047         if (!receive())
00048             return 0;
00049     }
00050     
00051     return _buf[_offset++];
00052 }
00053 
00054 uint8_t MbedDataSource::status()
00055 {
00056     if (!_sock.is_connected())
00057         return DS_STATUS_CLOSED;
00058     
00059     if (_isTimedOut)
00060         return DS_STATUS_TIMEOUT;
00061     
00062     return DS_STATUS_OK;
00063 }
00064 
00065 void MbedDataSource::setTimeout(size_t timeout)
00066 {
00067     _timeout = timeout;
00068 }
00069 
00070 bool MbedDataSource::receive()
00071 {
00072     int ret;
00073 
00074     if (status() != DS_STATUS_OK)
00075         return false;
00076     
00077     _sock.set_blocking(true, _timeout);
00078     ret = _sock.receive(_buf, MBED_SOURCE_BUFFER_SIZE);
00079     
00080     if (ret < 0) {
00081         _isTimedOut = true;
00082         return false;
00083     }
00084     
00085     _len = (size_t)ret;
00086     _offset = 0;
00087     
00088     return true;
00089 }
00090 
00091 void MbedDataSource::reset()
00092 {
00093     _len = _offset = 0;
00094     _isTimedOut = false;
00095 }