Generic SmartRest library
Dependents: SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming
SmartRest.cpp
00001 /* 00002 * SmartRest.cpp 00003 * 00004 * Created on: Nov 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 "SmartRest.h" 00030 #include <stdlib.h> 00031 #include <stdio.h> 00032 #include <string.h> 00033 00034 SmartRest::SmartRest(AbstractClient& client, const char *identifier) : _client(client), _identifier(identifier) 00035 { 00036 _source = NULL; 00037 _mogid[0] = 0; 00038 } 00039 00040 int8_t SmartRest::send(DataGenerator& generator) 00041 { 00042 uint8_t res; 00043 00044 res = beginRequest(); 00045 if (res != SMARTREST_SUCCESS) 00046 return res; 00047 00048 _client.sendData(generator); 00049 return awaitResponse(); 00050 } 00051 00052 int8_t SmartRest::receive(ParsedRecord& record) 00053 { 00054 uint8_t res; 00055 00056 if (_source == NULL) 00057 return SMARTREST_INTERNAL_ERROR; 00058 00059 res = _parser.readFrom(*_source, record); 00060 00061 switch (res) { 00062 case PARSER_SUCCESS: 00063 return SMARTREST_SUCCESS; 00064 case PARSER_END_OF_RESPONSE: 00065 return SMARTREST_END_OF_RESPONSE; 00066 case PARSER_TIMEOUT_ERROR: 00067 return SMARTREST_TIMEOUT_ERROR; 00068 } 00069 return SMARTREST_INTERNAL_ERROR; 00070 } 00071 00072 int8_t SmartRest::bootstrap(DataGenerator& generator) 00073 { 00074 ParsedRecord record; 00075 int8_t ret; 00076 00077 ret = beginRequest(); 00078 if (ret != SMARTREST_SUCCESS) 00079 return ret; 00080 ret = awaitResponse(); 00081 if (ret != SMARTREST_SUCCESS) 00082 return ret; 00083 ret = receive(record); 00084 if (ret != SMARTREST_SUCCESS) 00085 return ret; 00086 if (!record) { 00087 return SMARTREST_INTERNAL_ERROR; 00088 } 00089 stop(); 00090 00091 if (setMoGid(record)) 00092 return SMARTREST_SUCCESS; 00093 00094 if (record.value(0).integerValue() != 40) 00095 return SMARTREST_INTERNAL_ERROR; 00096 00097 ret = send(generator); 00098 if (ret != SMARTREST_SUCCESS) 00099 return ret; 00100 ret = receive(record); 00101 if (ret != SMARTREST_SUCCESS) 00102 return ret; 00103 stop(); 00104 00105 if (!setMoGid(record)) 00106 return SMARTREST_INTERNAL_ERROR; 00107 00108 return SMARTREST_SUCCESS; 00109 } 00110 00111 void SmartRest::stop() 00112 { 00113 _source = NULL; 00114 _client.stop(); 00115 } 00116 00117 uint8_t SmartRest::beginRequest() 00118 { 00119 int res; 00120 00121 res = _client.beginRequest(); 00122 if (res == CLIENT_CONNECTION_ERROR) { 00123 return SMARTREST_CONNECTION_FAILED; 00124 } else if (res != CLIENT_OK) { 00125 return SMARTREST_INTERNAL_ERROR; 00126 } 00127 if (strlen(_mogid)) { 00128 if (_client.sendIdentifier(_mogid) != CLIENT_OK) 00129 return SMARTREST_INTERNAL_ERROR; 00130 } else { 00131 if (_client.sendIdentifier(_identifier) != CLIENT_OK) 00132 return SMARTREST_INTERNAL_ERROR; 00133 } 00134 return SMARTREST_SUCCESS; 00135 } 00136 00137 uint8_t SmartRest::awaitResponse() 00138 { 00139 if ((_client.endRequest() != CLIENT_OK) || 00140 (_client.awaitResponse() != CLIENT_OK)) 00141 return SMARTREST_INTERNAL_ERROR; 00142 _source = &_client.receiveData(); 00143 return SMARTREST_SUCCESS; 00144 } 00145 00146 bool SmartRest::setMoGid(Record& record) 00147 { 00148 long mogid; 00149 00150 *_mogid = 0; 00151 00152 if ((record.values() < 2) || (record.value(0).integerValue() != 20)) 00153 return false; 00154 00155 if (record.value(1).integerValue() == 0) 00156 return false; 00157 00158 mogid = record.value(1).integerValue(); 00159 00160 if (!snprintf(_mogid, SMARTREST_MOGID_BUFFER_SIZE, "%ld", mogid)) 00161 *_mogid = 0; 00162 00163 return true; 00164 }
Generated on Wed Jul 13 2022 09:53:06 by 1.7.2