Cumulocity Official / MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MbedClient.h Source File

MbedClient.h

00001 /*
00002  * MbedClient.h
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 #ifndef MBEDCLIENT_H
00030 #define MBEDCLIENT_H
00031 
00032 #include <stdint.h>
00033 #include "AbstractClient.h"
00034 #include "TCPSocketConnection.h"
00035 #include "MbedDataSource.h"
00036 #include "MbedDataSink.h"
00037 #include "HTTPResponseFilter.h"
00038 
00039 #define MBCL_DBG(...) do { printf("\033[32mMbedClient:\033[39m "); printf(__VA_ARGS__); printf("\r\n"); } while (0);
00040 //#define MBCL_DBG(fmt, ...)
00041 
00042 class MbedClient : public AbstractClient {
00043 public:
00044     MbedClient(const char*, uint16_t, uint8_t);
00045     ~MbedClient();
00046 
00047     uint8_t setAuthorization(const char*, const char*);
00048     uint8_t beginRequest();
00049     uint8_t beginStream(const char*);
00050     uint8_t sendIdentifier(const char*);
00051     uint8_t sendData(const DataGenerator& generator);
00052     uint8_t endRequest();
00053     uint8_t awaitResponse();
00054     AbstractDataSource& receiveData();
00055     void stop();
00056 
00057 protected:
00058     bool connect();
00059     bool send(const char *str);
00060     bool sendRequestHeader(const char*);
00061     bool sendBasicAuth();
00062 
00063 private:
00064     uint8_t internalError();
00065     uint8_t connectionError();
00066 
00067 private:
00068     const char *_host, *_username, *_password;
00069     uint16_t _port, _tries, _state;
00070     bool _isStreamRequest;
00071     TCPSocketConnection _sock;
00072     MbedDataSource _source;
00073     MbedDataSink _sink;
00074     HTTPResponseFilter _filter;
00075 };
00076 
00077 #endif