Skip to content

Commit a8ea618

Browse files
committed
Merge branch 'develop' into release/1.3.3
2 parents 1e47963 + 4e2135e commit a8ea618

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

src/http_client.cpp

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define SECURITY_PROFILE_PREFIX_LENGTH 11
2626
#define HTTPS_SECURITY_PROFILE_NUMBER '3'
2727

28-
#define HTTP_SEND "AT+SQNHTTPSND=0,%u,\"%s\",%lu,\"\",\"%s\""
28+
#define HTTP_SEND "AT+SQNHTTPSND=0,%u,\"%s\",%lu,\"%s\",\"%s\""
2929
#define HTTP_RECEIVE "AT+SQNHTTPRCV=0,%lu"
3030
#define HTTP_QUERY "AT+SQNHTTPQRY=0,%u,\"%s\",\"%s\""
3131

@@ -37,6 +37,13 @@
3737
#define HTTP_HEAD_METHOD 1
3838
#define HTTP_DELETE_METHOD 2
3939

40+
// Content type specifiers for POST requests for the AT+SQNHTTPSND command
41+
#define HTTP_CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED "0"
42+
#define HTTP_CONTENT_TYPE_TEXT_PLAIN "1"
43+
#define HTTP_CONTENT_TYPE_APPLICATION_OCTET_STREAM "2"
44+
#define HTTP_CONTENT_TYPE_APPLICATION_MULTIPART_FORM_DATA "3"
45+
#define HTTP_CONTENT_TYPE_APPLICATION_APPLICATION_JSON "4"
46+
4047
#define HTTP_RECEIVE_LENGTH 32
4148
#define HTTP_RECEIVE_START_CHARACTER '<'
4249
#define HTTP_SEND_START_CHARACTER '>'
@@ -74,7 +81,12 @@ static HttpResponse sendData(const char* endpoint,
7481
const uint32_t data_length,
7582
const uint8_t method,
7683
const uint8_t* header = NULL,
77-
const uint32_t header_length = 0) {
84+
const uint32_t header_length = 0,
85+
const char* content_type = "") {
86+
87+
// The modem could hang if several HTTP requests are done quickly after each
88+
// other, this alleviates this
89+
SequansController.writeCommand("AT");
7890

7991
HttpResponse http_response = {0, 0};
8092

@@ -94,6 +106,7 @@ static HttpResponse sendData(const char* endpoint,
94106
method,
95107
endpoint,
96108
(unsigned long)data_length,
109+
content_type,
97110
header == NULL ? "" : (const char*)header);
98111

99112
SequansController.writeBytes((uint8_t*)command, command_length, true);
@@ -170,6 +183,10 @@ static HttpResponse queryData(const char* endpoint,
170183
const uint8_t* header,
171184
const uint32_t header_length) {
172185

186+
// The modem could hang if several HTTP requests are done quickly after each
187+
// other, this alleviates this
188+
SequansController.writeCommand("AT");
189+
173190
HttpResponse http_response = {0, 0};
174191

175192
// Set up and send the query
@@ -279,23 +296,64 @@ HttpResponse HttpClientClass::post(const char* endpoint,
279296
const uint8_t* data_buffer,
280297
const uint32_t data_length,
281298
const uint8_t* header_buffer,
282-
const uint32_t header_length) {
299+
const uint32_t header_length,
300+
const ContentType content_type) {
301+
302+
// The content type within the Sequans modem is classified by a single
303+
// character (+1 for NULL termination)
304+
char content_type_buffer[2] = "";
305+
306+
switch (content_type) {
307+
case CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED:
308+
strncpy(content_type_buffer,
309+
HTTP_CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED,
310+
sizeof(content_type_buffer));
311+
break;
312+
313+
case CONTENT_TYPE_APPLICATION_OCTET_STREAM:
314+
strncpy(content_type_buffer,
315+
HTTP_CONTENT_TYPE_APPLICATION_OCTET_STREAM,
316+
sizeof(content_type_buffer));
317+
break;
318+
319+
case CONTENT_TYPE_MULTIPART_FORM_DATA:
320+
strncpy(content_type_buffer,
321+
HTTP_CONTENT_TYPE_APPLICATION_MULTIPART_FORM_DATA,
322+
sizeof(content_type_buffer));
323+
break;
324+
325+
case CONTENT_TYPE_APPLICATION_JSON:
326+
strncpy(content_type_buffer,
327+
HTTP_CONTENT_TYPE_APPLICATION_APPLICATION_JSON,
328+
sizeof(content_type_buffer));
329+
break;
330+
331+
default:
332+
strncpy(content_type_buffer,
333+
HTTP_CONTENT_TYPE_TEXT_PLAIN,
334+
sizeof(content_type_buffer));
335+
break;
336+
}
337+
283338
return sendData(endpoint,
284339
data_buffer,
285340
data_length,
286341
HTTP_POST_METHOD,
287342
header_buffer,
288-
header_length);
343+
header_length,
344+
content_type_buffer);
289345
}
290346

291347
HttpResponse HttpClientClass::post(const char* endpoint,
292348
const char* data,
293-
const char* header) {
349+
const char* header,
350+
const ContentType content_type) {
294351
return post(endpoint,
295352
(uint8_t*)data,
296353
strlen(data),
297354
(uint8_t*)header,
298-
strlen(header));
355+
header == NULL ? 0 : strlen(header),
356+
content_type);
299357
}
300358

301359
HttpResponse HttpClientClass::put(const char* endpoint,
@@ -318,7 +376,7 @@ HttpResponse HttpClientClass::put(const char* endpoint,
318376
(uint8_t*)message,
319377
strlen(message),
320378
(uint8_t*)header,
321-
strlen(header));
379+
header == NULL ? 0 : strlen(header));
322380
}
323381

324382
HttpResponse HttpClientClass::get(const char* endpoint, const char* header) {

src/http_client.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ class HttpClientClass {
3030
STATUS_INTERNAL_SERVER_ERROR = 500,
3131
};
3232

33+
enum ContentType {
34+
CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED,
35+
CONTENT_TYPE_TEXT_PLAIN,
36+
CONTENT_TYPE_APPLICATION_OCTET_STREAM,
37+
CONTENT_TYPE_MULTIPART_FORM_DATA,
38+
CONTENT_TYPE_APPLICATION_JSON
39+
};
40+
3341
/**
3442
* @brief Sets up the HTTP client with a host and port.
3543
*
@@ -54,12 +62,14 @@ class HttpClientClass {
5462
* @param header_buffer Optional header line (e.g. for authorization
5563
* bearers)
5664
* @param header_length Length of the optinal header line.
65+
* @param content_type HTTP content type of the post request.
5766
*/
5867
HttpResponse post(const char* endpoint,
5968
const uint8_t* data_buffer,
6069
const uint32_t data_length,
61-
const uint8_t* header_buffer = NULL,
62-
const uint32_t header_length = 0);
70+
const uint8_t* header_buffer = NULL,
71+
const uint32_t header_length = 0,
72+
const ContentType content_type = CONTENT_TYPE_TEXT_PLAIN);
6373

6474
/**
6575
* @brief Issues a post to the host configured. Will block until operation
@@ -70,9 +80,12 @@ class HttpClientClass {
7080
* @param data The data payload.
7181
* @param header Optional header line (e.g. for authorization
7282
* bearers).
83+
* @param content_type HTTP content type of the post request.
7384
*/
74-
HttpResponse
75-
post(const char* endpoint, const char* data, const char* header = NULL);
85+
HttpResponse post(const char* endpoint,
86+
const char* data,
87+
const char* header = NULL,
88+
const ContentType content_type = CONTENT_TYPE_TEXT_PLAIN);
7689

7790
/**
7891
* @brief Issues a put to the host configured. Will block until operation is

0 commit comments

Comments
 (0)