33#
44# Temboo Arduino library
55#
6- # Copyright 2014 , Temboo Inc.
6+ # Copyright 2015 , Temboo Inc.
77#
88# Licensed under the Apache License, Version 2.0 (the "License");
99# you may not use this file except in compliance with the License.
@@ -92,6 +92,8 @@ void TembooChoreo::setSettingsFileToRead(const String& filePath) {
9292#include " utility/TembooSession.h"
9393
9494static const char HTTP_CODE[] PROGMEM = " HTTP_CODE\x0A\x1F " ;
95+ static char HTTP_EOL[] = " \r\n " ;
96+ static char HTTP_EOH[] = " \r\n\r\n " ;
9597
9698TembooChoreo::TembooChoreo (Client& client) : m_client(client) {
9799 m_accountName = NULL ;
@@ -171,7 +173,6 @@ void TembooChoreo::setProfile(const char* profileName) {
171173}
172174
173175
174-
175176void TembooChoreo::addInput (const String& inputName, const String& inputValue) {
176177 m_inputs.put (inputName.c_str (), inputValue.c_str ());
177178}
@@ -233,66 +234,76 @@ void TembooChoreo::addOutputFilter(const String& outputName, const String& filte
233234
234235
235236int TembooChoreo::run () {
236- return run (INADDR_NONE, 80 );
237+ return run (INADDR_NONE, 80 , TEMBOO_CHOREO_DEFAULT_TIMEOUT_SECS );
237238}
238239
240+ int TembooChoreo::run (uint16_t timeoutSecs) {
241+ return run (INADDR_NONE, 80 , timeoutSecs);
242+ }
239243
240- int TembooChoreo::run (IPAddress addr, uint16_t port) {
241-
244+ int TembooChoreo::run (IPAddress addr, uint16_t port, uint16_t timeoutSecs ) {
245+
242246 m_nextChar = NULL ;
243-
247+
244248 if (m_accountName == NULL || *m_accountName == ' \0 ' ) {
245249 return TEMBOO_ERROR_ACCOUNT_MISSING;
246250 }
247-
251+
248252 if (m_path == NULL || *m_path == ' \0 ' ) {
249253 return TEMBOO_ERROR_CHOREO_MISSING;
250254 }
251-
255+
252256 if (m_appKeyName == NULL || *m_appKeyName == ' \0 ' ) {
253257 return TEMBOO_ERROR_APPKEY_NAME_MISSING;
254258 }
255-
259+
256260 if (m_appKeyValue == NULL || *m_appKeyValue == ' \0 ' ) {
257261 return TEMBOO_ERROR_APPKEY_MISSING;
258262 }
259-
260-
263+
261264 TembooSession session (m_client, addr, port);
262265 uint16_t httpCode = 0 ;
263266
264267 for (int i = 0 ; i < 2 ; i++) {
268+ unsigned long timeoutBeginSecs = session.getTime ();
265269 if (0 != session.executeChoreo (m_accountName, m_appKeyName, m_appKeyValue, m_path, m_inputs, m_outputs, m_preset)) {
266270 httpCode = 0 ;
267271 break ;
268272 }
269-
273+
270274 while (!m_client.available ()) {
275+ if ((session.getTime () - timeoutBeginSecs) >= timeoutSecs) {
276+ TEMBOO_TRACELN (" Receive time out" );
277+ m_client.stop ();
278+ return TEMBOO_ERROR_STREAM_TIMEOUT;
279+ }
271280 if (!m_client.connected ()) {
272281 TEMBOO_TRACELN (" Disconnected" );
273282 return TEMBOO_ERROR_HTTP_ERROR;
274283 }
275284 delay (10 );
276285 }
277-
278- if (!m_client.find (" HTTP/1.1 " )) {
286+ if (!m_client.findUntil (" HTTP/1." , HTTP_EOL)) {
279287 TEMBOO_TRACELN (" No HTTP" );
280288 return TEMBOO_ERROR_HTTP_ERROR;
281289 }
282-
290+ // Don't care if the next byte is a '1' or a '0'
291+ m_client.read ();
292+
293+ // Read the HTTP status code
283294 httpCode = (uint16_t )m_client.parseInt ();
284-
285- // We expect HTTP response codes to be <= 599, but
295+
296+ // We expect HTTP response codes to be <= 599, but
286297 // we need to be prepared for anything.
287298 if (httpCode >= 600 ) {
288299 TEMBOO_TRACELN (" Invalid HTTP" );
289300 httpCode = 0 ;
290301 }
291-
302+
292303 // if we get an auth error AND there was an x-temboo-time header,
293304 // update the session timeOffset
294305 if ((httpCode == 401 ) && (i == 0 )) {
295- if (m_client.find (" x-temboo-time:" )) {
306+ if (m_client.findUntil (" x-temboo-time:" , HTTP_EOH )) {
296307 TembooSession::setTime ((unsigned long )m_client.parseInt ());
297308 while (m_client.available ()) {
298309 m_client.read ();
@@ -303,20 +314,20 @@ int TembooChoreo::run(IPAddress addr, uint16_t port) {
303314 break ;
304315 }
305316 }
306-
317+
307318 uint16toa (httpCode, m_httpCodeStr);
308319 strcat_P (m_httpCodeStr, PSTR (" \x0A\x1E " ));
309320 m_nextState = START;
310321 m_nextChar = HTTP_CODE;
311-
322+
312323 if (httpCode < 200 || httpCode >= 300 ) {
313324 return TEMBOO_ERROR_HTTP_ERROR;
314325 }
315-
316- if (!m_client.find (" \x0D\x0A\x0D\x0A " )) {
326+
327+ if (!m_client.find (HTTP_EOH )) {
317328 return TEMBOO_ERROR_HTTP_ERROR;
318329 }
319-
330+
320331 return TEMBOO_ERROR_OK;
321332}
322333
0 commit comments