|
2 | 2 | #include "log.h" |
3 | 3 | #include "sequans_controller.h" |
4 | 4 |
|
| 5 | +#include <Arduino.h> |
5 | 6 | #include <math.h> |
6 | 7 | #include <stdio.h> |
7 | 8 | #include <stdlib.h> |
|
46 | 47 | #define HTTP_BODY_BUFFER_MIN_SIZE 64 |
47 | 48 | #define HTTP_BODY_BUFFER_MAX_SIZE 1500 |
48 | 49 |
|
49 | | -#define DEFAULT_RETRIES 5 |
| 50 | +#define HTTP_TIMEOUT 10000 |
50 | 51 |
|
51 | 52 | HttpClientClass HttpClient = HttpClientClass::instance(); |
52 | 53 |
|
@@ -104,19 +105,38 @@ static HttpResponse sendData(const char *endpoint, |
104 | 105 |
|
105 | 106 | // We receive one start bytes of the character '>', so we wait for |
106 | 107 | // it |
107 | | - while (SequansController.readByte() != HTTP_SEND_START_CHARACTER) {} |
| 108 | + |
| 109 | + uint64_t start = millis(); |
| 110 | + int16_t start_character = 0; |
| 111 | + |
| 112 | + do { |
| 113 | + start_character = SequansController.readByte(); |
| 114 | + } while (millis() - start < HTTP_TIMEOUT && |
| 115 | + start_character != HTTP_SEND_START_CHARACTER); |
| 116 | + |
| 117 | + if (start_character != HTTP_SEND_START_CHARACTER) { |
| 118 | + Log.error("Timed out waiting for modem to be ready for HTTP payload"); |
| 119 | + return httpResponse; |
| 120 | + } |
| 121 | + |
| 122 | + delay(100); |
108 | 123 |
|
109 | 124 | // Now we deliver the payload |
110 | 125 | SequansController.writeBytes(buffer, buffer_size); |
111 | 126 |
|
112 | 127 | // Wait until we get some valid response and we don't reach the timeout for |
113 | 128 | // the interface |
| 129 | + |
| 130 | + start = millis(); |
114 | 131 | ResponseResult response_result; |
| 132 | + |
115 | 133 | do { |
116 | 134 | response_result = SequansController.readResponse(); |
117 | | - } while (response_result == ResponseResult::TIMEOUT); |
| 135 | + } while (millis() - start < HTTP_TIMEOUT && |
| 136 | + response_result != ResponseResult::OK); |
118 | 137 |
|
119 | 138 | if (response_result != ResponseResult::OK) { |
| 139 | + Log.error("Modem timed out whilst delivering HTTP payload"); |
120 | 140 | return httpResponse; |
121 | 141 | } |
122 | 142 |
|
@@ -218,7 +238,7 @@ bool HttpClientClass::configure(const char *host, |
218 | 238 | char command[HTTP_CONFIGURE_SIZE] = ""; |
219 | 239 | sprintf(command, HTTP_CONFIGURE, host, port, enable_tls ? 1 : 0); |
220 | 240 |
|
221 | | - return SequansController.retryCommand(command, DEFAULT_RETRIES); |
| 241 | + return SequansController.retryCommand(command); |
222 | 242 | } |
223 | 243 |
|
224 | 244 | HttpResponse HttpClientClass::post(const char *endpoint, |
@@ -293,12 +313,7 @@ int16_t HttpClientClass::readBody(char *buffer, const uint32_t buffer_size) { |
293 | 313 | return 0; |
294 | 314 | } |
295 | 315 |
|
296 | | - size_t response_length = strlen(buffer); |
297 | | - |
298 | | - // Remove extra <CR> from command response |
299 | | - buffer[response_length - 1] = '\0'; |
300 | | - |
301 | | - return response_length - 1; |
| 316 | + return strlen(buffer); |
302 | 317 | } |
303 | 318 |
|
304 | 319 | String HttpClientClass::readBody(const uint32_t size) { |
|
0 commit comments