update qcloud sdk
1. iot-hub sdk update to 3.2.0 2. iot-explorer update to 3.1.1
This commit is contained in:
@@ -18,7 +18,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
@@ -41,13 +41,13 @@ extern "C" {
|
||||
|
||||
#define HTTP_CLIENT_MAX_HOST_LEN 64
|
||||
#define HTTP_CLIENT_MAX_URL_LEN 1024
|
||||
|
||||
|
||||
#define HTTP_RETRIEVE_MORE_DATA (1)
|
||||
|
||||
|
||||
#if defined(MBEDTLS_DEBUG_C)
|
||||
#define DEBUG_LEVEL 2
|
||||
#define DEBUG_LEVEL 2
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
static void _http_client_base64enc(char *out, const char *in)
|
||||
{
|
||||
@@ -71,13 +71,13 @@ static void _http_client_base64enc(char *out, const char *in)
|
||||
}
|
||||
|
||||
static int _http_client_parse_url(const char *url, char *scheme, uint32_t max_scheme_len, char *host, uint32_t maxhost_len,
|
||||
int *port, char *path, uint32_t max_path_len)
|
||||
int *port, char *path, uint32_t max_path_len)
|
||||
{
|
||||
char *scheme_ptr = (char *) url;
|
||||
char *host_ptr = (char *) strstr(url, "://");
|
||||
uint32_t host_len = 0;
|
||||
uint32_t path_len;
|
||||
|
||||
|
||||
char *path_ptr;
|
||||
char *fragment_ptr;
|
||||
|
||||
@@ -103,38 +103,38 @@ static int _http_client_parse_url(const char *url, char *scheme, uint32_t max_sc
|
||||
host_len = path_ptr - host_ptr;
|
||||
memcpy(host, host_ptr, host_len);
|
||||
host[host_len] = '\0';
|
||||
|
||||
|
||||
memcpy(path, "/", 1);
|
||||
path[1] = '\0';
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
if (host_len == 0) {
|
||||
host_len = path_ptr - host_ptr;
|
||||
}
|
||||
|
||||
|
||||
if (maxhost_len < host_len + 1) {
|
||||
Log_e("Host str is too long (host_len(%d) >= max_len(%d))", host_len + 1, maxhost_len);
|
||||
return QCLOUD_ERR_HTTP_PARSE;
|
||||
}
|
||||
memcpy(host, host_ptr, host_len);
|
||||
host[host_len] = '\0';
|
||||
|
||||
|
||||
fragment_ptr = strchr(host_ptr, '#');
|
||||
if (fragment_ptr != NULL) {
|
||||
path_len = fragment_ptr - path_ptr;
|
||||
} else {
|
||||
path_len = strlen(path_ptr);
|
||||
}
|
||||
|
||||
|
||||
if (max_path_len < path_len + 1) {
|
||||
Log_e("Path str is too small (%d >= %d)", max_path_len, path_len + 1);
|
||||
return QCLOUD_ERR_HTTP_PARSE;
|
||||
}
|
||||
|
||||
|
||||
memcpy(path, path_ptr, path_len);
|
||||
|
||||
|
||||
path[path_len] = '\0';
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
@@ -151,7 +151,7 @@ static int _http_client_parse_host(const char *url, char *host, uint32_t host_ma
|
||||
return QCLOUD_ERR_HTTP_PARSE;
|
||||
}
|
||||
host_ptr += 3;
|
||||
|
||||
|
||||
uint32_t pro_len = 0;
|
||||
pro_len = host_ptr - url;
|
||||
|
||||
@@ -222,7 +222,7 @@ static int _http_client_send_auth(HTTPClient *client, unsigned char *send_buf, i
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int _http_client_send_header(HTTPClient *client, const char *url, HttpMethod method, HTTPClientData *client_data)
|
||||
{
|
||||
@@ -237,49 +237,49 @@ static int _http_client_send_header(HTTPClient *client, const char *url, HttpMet
|
||||
(method == HTTP_HEAD) ? "HEAD" : "";
|
||||
int rc;
|
||||
int port;
|
||||
|
||||
|
||||
int res = _http_client_parse_url(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path));
|
||||
if (res != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("httpclient_parse_url returned %d", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
if (strcmp(scheme, "http") == 0) {
|
||||
|
||||
|
||||
} else if (strcmp(scheme, "https") == 0) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
memset(send_buf, 0, HTTP_CLIENT_SEND_BUF_SIZE);
|
||||
len = 0;
|
||||
|
||||
|
||||
HAL_Snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host);
|
||||
rc = _http_client_get_info(client, send_buf, &len, buf, strlen(buf));
|
||||
if (rc) {
|
||||
Log_e("Could not write request");
|
||||
return QCLOUD_ERR_HTTP_CONN;
|
||||
}
|
||||
|
||||
|
||||
if (client->auth_user) {
|
||||
_http_client_send_auth(client, send_buf, &len);
|
||||
}
|
||||
|
||||
|
||||
if (client->header) {
|
||||
_http_client_get_info(client, send_buf, &len, (char *) client->header, strlen(client->header));
|
||||
}
|
||||
|
||||
|
||||
if (client_data->post_buf != NULL) {
|
||||
HAL_Snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", client_data->post_buf_len);
|
||||
_http_client_get_info(client, send_buf, &len, buf, strlen(buf));
|
||||
|
||||
|
||||
if (client_data->post_content_type != NULL) {
|
||||
HAL_Snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", client_data->post_content_type);
|
||||
_http_client_get_info(client, send_buf, &len, buf, strlen(buf));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_http_client_get_info(client, send_buf, &len, "\r\n", 0);
|
||||
|
||||
|
||||
//Log_d("REQUEST:\n%s", send_buf);
|
||||
|
||||
size_t written_len = 0;
|
||||
@@ -293,13 +293,13 @@ static int _http_client_send_header(HTTPClient *client, const char *url, HttpMet
|
||||
Log_e("Connection error (send returned %d)", rc);
|
||||
return QCLOUD_ERR_HTTP_CONN;
|
||||
}
|
||||
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int _http_client_send_userdata(HTTPClient *client, HTTPClientData *client_data)
|
||||
{
|
||||
{
|
||||
if (client_data->post_buf && client_data->post_buf_len) {
|
||||
//Log_d("client_data->post_buf: %s", client_data->post_buf);
|
||||
{
|
||||
@@ -316,10 +316,10 @@ static int _http_client_send_userdata(HTTPClient *client, HTTPClientData *client
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int _http_client_recv(HTTPClient *client, char *buf, int min_len, int max_len, int *p_read_len, uint32_t timeout_ms, HTTPClientData *client_data)
|
||||
{
|
||||
@@ -327,39 +327,37 @@ static int _http_client_recv(HTTPClient *client, char *buf, int min_len, int max
|
||||
|
||||
int rc = 0;
|
||||
Timer timer;
|
||||
|
||||
size_t recv_size = 0;
|
||||
|
||||
InitTimer(&timer);
|
||||
countdown_ms(&timer, (unsigned int)timeout_ms);
|
||||
|
||||
|
||||
*p_read_len = 0;
|
||||
|
||||
rc = client->network_stack.read(&client->network_stack, (unsigned char *)buf, max_len, (uint32_t)left_ms(&timer), (size_t *)p_read_len);
|
||||
|
||||
rc = client->network_stack.read(&client->network_stack, (unsigned char *)buf, max_len, (uint32_t)left_ms(&timer), &recv_size);
|
||||
*p_read_len = (int)recv_size;
|
||||
if (rc == QCLOUD_ERR_SSL_NOTHING_TO_READ || rc == QCLOUD_ERR_TCP_NOTHING_TO_READ) {
|
||||
Log_d("HTTP read nothing and timeout");
|
||||
rc = QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
else if (rc == QCLOUD_ERR_SSL_READ_TIMEOUT || rc == QCLOUD_ERR_TCP_READ_TIMEOUT) {
|
||||
} else if (rc == QCLOUD_ERR_SSL_READ_TIMEOUT || rc == QCLOUD_ERR_TCP_READ_TIMEOUT) {
|
||||
if (*p_read_len == client_data->retrieve_len || client_data->retrieve_len == 0)
|
||||
rc = QCLOUD_RET_SUCCESS;
|
||||
else
|
||||
Log_e("network_stack read timeout");
|
||||
}
|
||||
else if (rc == QCLOUD_ERR_TCP_PEER_SHUTDOWN && *p_read_len > 0) {
|
||||
} else if (rc == QCLOUD_ERR_TCP_PEER_SHUTDOWN && *p_read_len > 0) {
|
||||
/* HTTP server give response and close this connection */
|
||||
client->network_stack.disconnect(&client->network_stack);
|
||||
rc = QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
else if (rc != QCLOUD_RET_SUCCESS) {
|
||||
} else if (rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("Connection error rc = %d (recv returned %d)", rc, *p_read_len);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
static int _http_client_retrieve_content(HTTPClient *client, char *data, int len, uint32_t timeout_ms,
|
||||
HTTPClientData *client_data)
|
||||
HTTPClientData *client_data)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
@@ -367,12 +365,12 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
int templen = 0;
|
||||
int crlf_pos;
|
||||
Timer timer;
|
||||
|
||||
|
||||
InitTimer(&timer);
|
||||
countdown_ms(&timer, (unsigned int)timeout_ms);
|
||||
|
||||
|
||||
client_data->is_more = IOT_TRUE;
|
||||
|
||||
|
||||
if (client_data->response_content_len == -1 && client_data->is_chunked == IOT_FALSE) {
|
||||
while (1) {
|
||||
int rc, max_len;
|
||||
@@ -385,13 +383,13 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
client_data->response_buf[client_data->response_buf_len - 1] = '\0';
|
||||
return HTTP_RETRIEVE_MORE_DATA;
|
||||
}
|
||||
|
||||
|
||||
max_len = HTTP_CLIENT_MIN(HTTP_CLIENT_CHUNK_SIZE - 1, client_data->response_buf_len - 1 - count);
|
||||
rc = _http_client_recv(client, data, 1, max_len, &len, (uint32_t)left_ms(&timer), client_data);
|
||||
|
||||
|
||||
/* Receive data */
|
||||
//Log_d("data len: %d %d", len, count);
|
||||
|
||||
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
@@ -399,7 +397,7 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
Log_e("HTTP read timeout!");
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
|
||||
}
|
||||
|
||||
|
||||
if (len == 0) {
|
||||
/* read no more data */
|
||||
Log_d("no more data, len == 0");
|
||||
@@ -408,7 +406,7 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
while (1) {
|
||||
uint32_t readLen = 0;
|
||||
if (client_data->is_chunked && client_data->retrieve_len <= 0) {
|
||||
@@ -432,12 +430,12 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
if (len < HTTP_CLIENT_CHUNK_SIZE) {
|
||||
int new_trf_len, rc;
|
||||
rc = _http_client_recv(client,
|
||||
data + len,
|
||||
0,
|
||||
HTTP_CLIENT_CHUNK_SIZE - len - 1,
|
||||
&new_trf_len,
|
||||
left_ms(&timer),
|
||||
client_data);
|
||||
data + len,
|
||||
0,
|
||||
HTTP_CLIENT_CHUNK_SIZE - len - 1,
|
||||
&new_trf_len,
|
||||
left_ms(&timer),
|
||||
client_data);
|
||||
len += new_trf_len;
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
@@ -465,14 +463,14 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
Log_e("Could not read chunk length");
|
||||
return QCLOUD_ERR_HTTP_UNRESOLVED_DNS;
|
||||
}
|
||||
|
||||
|
||||
memmove(data, &data[crlf_pos + 2], len - (crlf_pos + 2));
|
||||
len -= (crlf_pos + 2);
|
||||
|
||||
|
||||
} else {
|
||||
readLen = client_data->retrieve_len;
|
||||
}
|
||||
|
||||
|
||||
do {
|
||||
templen = HTTP_CLIENT_MIN(len, readLen);
|
||||
if (count + templen < client_data->response_buf_len - 1) {
|
||||
@@ -486,7 +484,7 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
client_data->retrieve_len -= (client_data->response_buf_len - 1 - count);
|
||||
IOT_FUNC_EXIT_RC(HTTP_RETRIEVE_MORE_DATA);
|
||||
}
|
||||
|
||||
|
||||
if (len > readLen) {
|
||||
Log_d("memmove %d %d %d\n", readLen, len, client_data->retrieve_len);
|
||||
memmove(data, &data[readLen], len - readLen); /* chunk case, read between two chunks */
|
||||
@@ -496,7 +494,7 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
} else {
|
||||
readLen -= len;
|
||||
}
|
||||
|
||||
|
||||
if (readLen) {
|
||||
int rc;
|
||||
int max_len = HTTP_CLIENT_MIN(HTTP_CLIENT_CHUNK_SIZE - 1, client_data->response_buf_len - 1 - count);
|
||||
@@ -511,19 +509,19 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
}
|
||||
}
|
||||
} while (readLen);
|
||||
|
||||
|
||||
if (client_data->is_chunked) {
|
||||
if (len < 2) {
|
||||
int new_trf_len, rc;
|
||||
/* Read missing chars to find end of chunk */
|
||||
rc = _http_client_recv(client, data + len, 2 - len, HTTP_CLIENT_CHUNK_SIZE - len - 1, &new_trf_len,
|
||||
left_ms(&timer), client_data);
|
||||
if ((rc != QCLOUD_RET_SUCCESS )|| (0 == left_ms(&timer))) {
|
||||
left_ms(&timer), client_data);
|
||||
if ((rc != QCLOUD_RET_SUCCESS ) || (0 == left_ms(&timer))) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
len += new_trf_len;
|
||||
}
|
||||
|
||||
|
||||
if ((data[0] != '\r') || (data[1] != '\n')) {
|
||||
Log_e("Format error, %s", data); /* after memmove, the beginning of next chunk */
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_UNRESOLVED_DNS);
|
||||
@@ -535,36 +533,36 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
|
||||
client_data->is_more = IOT_FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
static int _http_client_response_parse(HTTPClient *client, char *data, int len, uint32_t timeout_ms,
|
||||
|
||||
static int _http_client_response_parse(HTTPClient *client, char *data, int len, uint32_t timeout_ms,
|
||||
HTTPClientData *client_data)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
|
||||
int crlf_pos;
|
||||
Timer timer;
|
||||
char *tmp_ptr, *ptr_body_end;
|
||||
|
||||
|
||||
InitTimer(&timer);
|
||||
countdown_ms(&timer, timeout_ms);
|
||||
|
||||
|
||||
client_data->response_content_len = -1;
|
||||
|
||||
|
||||
char *crlf_ptr = strstr(data, "\r\n");
|
||||
if (crlf_ptr == NULL) {
|
||||
Log_e("\\r\\n not found");
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_UNRESOLVED_DNS);
|
||||
}
|
||||
|
||||
|
||||
crlf_pos = crlf_ptr - data;
|
||||
data[crlf_pos] = '\0';
|
||||
|
||||
|
||||
#if 0
|
||||
if (sscanf(data, "HTTP/%*d.%*d %d %*[^\r\n]", &(client->response_code)) != 1) {
|
||||
Log_e("Not a correct HTTP answer : %s\n", data);
|
||||
@@ -573,23 +571,23 @@ static int _http_client_response_parse(HTTPClient *client, char *data, int len,
|
||||
#endif
|
||||
|
||||
client->response_code = atoi(data + 9);
|
||||
|
||||
|
||||
if ((client->response_code < 200) || (client->response_code >= 400)) {
|
||||
Log_w("Response code %d", client->response_code);
|
||||
|
||||
|
||||
if (client->response_code == 403)
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_AUTH);
|
||||
|
||||
|
||||
if (client->response_code == 404)
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
//Log_d("Reading headers : %s", data);
|
||||
|
||||
|
||||
// remove null character
|
||||
memmove(data, &data[crlf_pos + 2], len - (crlf_pos + 2) + 1);
|
||||
len -= (crlf_pos + 2);
|
||||
|
||||
|
||||
client_data->is_chunked = IOT_FALSE;
|
||||
|
||||
if (NULL == (ptr_body_end = strstr(data, "\r\n\r\n"))) {
|
||||
@@ -623,7 +621,7 @@ static int _http_client_response_parse(HTTPClient *client, char *data, int len,
|
||||
Log_e("Could not parse header");
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP);
|
||||
}
|
||||
|
||||
|
||||
len = len - (ptr_body_end + 4 - data);
|
||||
memmove(data, ptr_body_end + 4, len + 1);
|
||||
int rc = _http_client_retrieve_content(client, data, len, left_ms(&timer), client_data);
|
||||
@@ -632,7 +630,7 @@ static int _http_client_response_parse(HTTPClient *client, char *data, int len,
|
||||
|
||||
static int _http_client_connect(HTTPClient *client)
|
||||
{
|
||||
if (QCLOUD_RET_SUCCESS != client->network_stack.connect(&client->network_stack)) {
|
||||
if (QCLOUD_RET_SUCCESS != client->network_stack.connect(&client->network_stack)) {
|
||||
return QCLOUD_ERR_HTTP_CONN;
|
||||
}
|
||||
|
||||
@@ -640,7 +638,7 @@ static int _http_client_connect(HTTPClient *client)
|
||||
}
|
||||
|
||||
static int _http_client_send_request(HTTPClient *client, const char *url, HttpMethod method, HTTPClientData *client_data)
|
||||
{
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = _http_client_send_header(client, url, method, client_data);
|
||||
@@ -655,7 +653,7 @@ static int _http_client_send_request(HTTPClient *client, const char *url, HttpMe
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int _http_client_recv_response(HTTPClient *client, uint32_t timeout_ms, HTTPClientData *client_data)
|
||||
{
|
||||
@@ -664,37 +662,37 @@ static int _http_client_recv_response(HTTPClient *client, uint32_t timeout_ms, H
|
||||
int reclen = 0, rc = QCLOUD_ERR_HTTP_CONN;
|
||||
char buf[HTTP_CLIENT_CHUNK_SIZE] = { 0 };
|
||||
Timer timer;
|
||||
|
||||
|
||||
InitTimer(&timer);
|
||||
countdown_ms(&timer, timeout_ms);
|
||||
|
||||
|
||||
if (0 == client->network_stack.handle) {
|
||||
Log_e("Connection has not been established");
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
|
||||
if (client_data->is_more) {
|
||||
client_data->response_buf[0] = '\0';
|
||||
rc = _http_client_retrieve_content(client, buf, reclen, left_ms(&timer), client_data);
|
||||
} else {
|
||||
client_data->is_more = IOT_TRUE;
|
||||
rc = _http_client_recv(client, buf, 1, HTTP_CLIENT_CHUNK_SIZE - 1, &reclen, left_ms(&timer), client_data);
|
||||
|
||||
rc = _http_client_recv(client, buf, 1, HTTP_CLIENT_CHUNK_SIZE - 1, &reclen, left_ms(&timer), client_data);
|
||||
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
//else if(0 == left_ms(&timer)){
|
||||
// IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
|
||||
//}
|
||||
|
||||
|
||||
buf[reclen] = '\0';
|
||||
|
||||
|
||||
if (reclen) {
|
||||
//HAL_Printf("RESPONSE:\n%s", buf);
|
||||
rc = _http_client_response_parse(client, buf, reclen, left_ms(&timer), client_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
@@ -717,7 +715,7 @@ static int _http_network_init(Network *pNetwork, const char *host, int port, con
|
||||
pNetwork->port = port;
|
||||
|
||||
rc = network_init(pNetwork);
|
||||
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -765,7 +763,7 @@ int qcloud_http_client_common(HTTPClient *client, const char *url, int port, con
|
||||
if (client->network_stack.handle == 0) {
|
||||
rc = qcloud_http_client_connect(client, url, port, ca_crt);
|
||||
if (rc != QCLOUD_RET_SUCCESS) return rc;
|
||||
}
|
||||
}
|
||||
|
||||
rc = _http_client_send_request(client, url, method, client_data);
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
@@ -773,7 +771,7 @@ int qcloud_http_client_common(HTTPClient *client, const char *url, int port, con
|
||||
qcloud_http_client_close(client);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -786,7 +784,7 @@ int qcloud_http_recv_data(HTTPClient *client, uint32_t timeout_ms, HTTPClientDat
|
||||
|
||||
InitTimer(&timer);
|
||||
countdown_ms(&timer, (unsigned int) timeout_ms);
|
||||
|
||||
|
||||
if ((NULL != client_data->response_buf)
|
||||
&& (0 != client_data->response_buf_len)) {
|
||||
rc = _http_client_recv_response(client, left_ms(&timer), client_data);
|
||||
|
@@ -66,73 +66,72 @@ int IOT_MQTT_GetErrCode(void)
|
||||
void* IOT_MQTT_Construct(MQTTInitParams *pParams)
|
||||
{
|
||||
g_last_err_code = QCLOUD_ERR_INVAL;
|
||||
POINTER_SANITY_CHECK(pParams, NULL);
|
||||
STRING_PTR_SANITY_CHECK(pParams->product_id, NULL);
|
||||
STRING_PTR_SANITY_CHECK(pParams->device_name, NULL);
|
||||
POINTER_SANITY_CHECK(pParams, NULL);
|
||||
STRING_PTR_SANITY_CHECK(pParams->product_id, NULL);
|
||||
STRING_PTR_SANITY_CHECK(pParams->device_name, NULL);
|
||||
|
||||
int rc = iot_device_info_set(pParams->product_id, pParams->device_name);
|
||||
if ( rc != QCLOUD_RET_SUCCESS) {
|
||||
if ( rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("failed to set device info: %d", rc);
|
||||
g_last_err_code = rc;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Qcloud_IoT_Client* mqtt_client = NULL;
|
||||
Qcloud_IoT_Client* mqtt_client = NULL;
|
||||
|
||||
// create and init MQTTClient
|
||||
if ((mqtt_client = (Qcloud_IoT_Client*) HAL_Malloc (sizeof(Qcloud_IoT_Client))) == NULL) {
|
||||
Log_e("malloc MQTTClient failed");
|
||||
g_last_err_code = QCLOUD_ERR_MALLOC;
|
||||
return NULL;
|
||||
}
|
||||
// create and init MQTTClient
|
||||
if ((mqtt_client = (Qcloud_IoT_Client*) HAL_Malloc (sizeof(Qcloud_IoT_Client))) == NULL) {
|
||||
Log_e("malloc MQTTClient failed");
|
||||
g_last_err_code = QCLOUD_ERR_MALLOC;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rc = qcloud_iot_mqtt_init(mqtt_client, pParams);
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("mqtt init failed: %d", rc);
|
||||
HAL_Free(mqtt_client);
|
||||
g_last_err_code = rc;
|
||||
return NULL;
|
||||
}
|
||||
rc = qcloud_iot_mqtt_init(mqtt_client, pParams);
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("mqtt init failed: %d", rc);
|
||||
HAL_Free(mqtt_client);
|
||||
g_last_err_code = rc;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
MQTTConnectParams connect_params = DEFAULT_MQTTCONNECT_PARAMS;
|
||||
connect_params.client_id = iot_device_info_get()->client_id;
|
||||
MQTTConnectParams connect_params = DEFAULT_MQTTCONNECT_PARAMS;
|
||||
connect_params.client_id = iot_device_info_get()->client_id;
|
||||
// Upper limit of keep alive interval is (11.5 * 60) seconds
|
||||
connect_params.keep_alive_interval = Min(pParams->keep_alive_interval_ms / 1000, 690);
|
||||
connect_params.clean_session = pParams->clean_session;
|
||||
connect_params.auto_connect_enable = pParams->auto_connect_enable;
|
||||
connect_params.keep_alive_interval = Min(pParams->keep_alive_interval_ms / 1000, 690);
|
||||
connect_params.clean_session = pParams->clean_session;
|
||||
connect_params.auto_connect_enable = pParams->auto_connect_enable;
|
||||
#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)
|
||||
size_t src_len = strlen(pParams->device_secret);
|
||||
size_t len;
|
||||
memset(sg_psk_str, 0x00, DECODE_PSK_LENGTH);
|
||||
rc = qcloud_iot_utils_base64decode(sg_psk_str, sizeof( sg_psk_str ), &len, (unsigned char *)pParams->device_secret, src_len );
|
||||
connect_params.device_secret = (char *)sg_psk_str;
|
||||
connect_params.device_secret_len = len;
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("Device secret decode err, secret:%s", pParams->device_secret);
|
||||
qcloud_iot_mqtt_deinit(mqtt_client);
|
||||
HAL_Free(mqtt_client);
|
||||
g_last_err_code = rc;
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
rc = qcloud_iot_mqtt_connect(mqtt_client, &connect_params);
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("mqtt connect with id: %s failed: %d", mqtt_client->options.conn_id, rc);
|
||||
size_t src_len = strlen(pParams->device_secret);
|
||||
size_t len;
|
||||
memset(sg_psk_str, 0x00, DECODE_PSK_LENGTH);
|
||||
rc = qcloud_iot_utils_base64decode(sg_psk_str, sizeof( sg_psk_str ), &len, (unsigned char *)pParams->device_secret, src_len );
|
||||
connect_params.device_secret = (char *)sg_psk_str;
|
||||
connect_params.device_secret_len = len;
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("Device secret decode err, secret:%s", pParams->device_secret);
|
||||
qcloud_iot_mqtt_deinit(mqtt_client);
|
||||
HAL_Free(mqtt_client);
|
||||
g_last_err_code = rc;
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
Log_i("mqtt connect with id: %s success", mqtt_client->options.conn_id);
|
||||
g_last_err_code = QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
g_last_err_code = rc;
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef LOG_UPLOAD
|
||||
//log subscribe topics
|
||||
if (is_log_uploader_init()) {
|
||||
int log_level;
|
||||
rc = qcloud_iot_mqtt_connect(mqtt_client, &connect_params);
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("mqtt connect with id: %s failed: %d", mqtt_client->options.conn_id, rc);
|
||||
qcloud_iot_mqtt_deinit(mqtt_client);
|
||||
HAL_Free(mqtt_client);
|
||||
g_last_err_code = rc;
|
||||
return NULL;
|
||||
} else {
|
||||
Log_i("mqtt connect with id: %s success", mqtt_client->options.conn_id);
|
||||
g_last_err_code = QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
#ifdef LOG_UPLOAD
|
||||
//log subscribe topics
|
||||
if (is_log_uploader_init()) {
|
||||
int log_level;
|
||||
rc = qcloud_get_log_level(mqtt_client, &log_level);
|
||||
//rc = qcloud_log_topic_subscribe(mqtt_client);
|
||||
if (rc < 0) {
|
||||
@@ -144,22 +143,23 @@ void* IOT_MQTT_Construct(MQTTInitParams *pParams)
|
||||
}
|
||||
#endif
|
||||
|
||||
return mqtt_client;
|
||||
return mqtt_client;
|
||||
}
|
||||
|
||||
int IOT_MQTT_Destroy(void **pClient) {
|
||||
POINTER_SANITY_CHECK(*pClient, QCLOUD_ERR_INVAL);
|
||||
int IOT_MQTT_Destroy(void **pClient)
|
||||
{
|
||||
POINTER_SANITY_CHECK(*pClient, QCLOUD_ERR_INVAL);
|
||||
|
||||
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)(*pClient);
|
||||
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)(*pClient);
|
||||
|
||||
int rc = qcloud_iot_mqtt_disconnect(mqtt_client);
|
||||
int rc = qcloud_iot_mqtt_disconnect(mqtt_client);
|
||||
|
||||
int i = 0;
|
||||
int i = 0;
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
|
||||
/* notify this event to topic subscriber */
|
||||
if (NULL != mqtt_client->sub_handles[i].sub_event_handler)
|
||||
mqtt_client->sub_handles[i].sub_event_handler(mqtt_client,
|
||||
MQTT_EVENT_CLIENT_DESTROY, mqtt_client->sub_handles[i].handler_user_data);
|
||||
mqtt_client->sub_handles[i].sub_event_handler(mqtt_client,
|
||||
MQTT_EVENT_CLIENT_DESTROY, mqtt_client->sub_handles[i].handler_user_data);
|
||||
|
||||
if (NULL != mqtt_client->sub_handles[i].topic_filter) {
|
||||
HAL_Free((void *)mqtt_client->sub_handles[i].topic_filter);
|
||||
@@ -173,7 +173,7 @@ int IOT_MQTT_Destroy(void **pClient) {
|
||||
#endif
|
||||
|
||||
HAL_MutexDestroy(mqtt_client->lock_generic);
|
||||
HAL_MutexDestroy(mqtt_client->lock_write_buf);
|
||||
HAL_MutexDestroy(mqtt_client->lock_write_buf);
|
||||
|
||||
HAL_MutexDestroy(mqtt_client->lock_list_sub);
|
||||
HAL_MutexDestroy(mqtt_client->lock_list_pub);
|
||||
@@ -183,15 +183,16 @@ int IOT_MQTT_Destroy(void **pClient) {
|
||||
|
||||
HAL_Free(*pClient);
|
||||
*pClient = NULL;
|
||||
#ifdef LOG_UPLOAD
|
||||
#ifdef LOG_UPLOAD
|
||||
set_log_mqtt_client(NULL);
|
||||
#endif
|
||||
#endif
|
||||
Log_i("mqtt release!");
|
||||
|
||||
return rc;
|
||||
return rc;
|
||||
}
|
||||
|
||||
int IOT_MQTT_Yield(void *pClient, uint32_t timeout_ms) {
|
||||
int IOT_MQTT_Yield(void *pClient, uint32_t timeout_ms)
|
||||
{
|
||||
|
||||
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
|
||||
int rc = qcloud_iot_mqtt_yield(mqtt_client, timeout_ms);
|
||||
@@ -204,7 +205,7 @@ int IOT_MQTT_Yield(void *pClient, uint32_t timeout_ms) {
|
||||
IOT_Log_Upload(true);
|
||||
#endif
|
||||
|
||||
return rc;
|
||||
return rc;
|
||||
}
|
||||
|
||||
int IOT_MQTT_Publish(void *pClient, char *topicName, PublishParams *pParams)
|
||||
@@ -214,21 +215,24 @@ int IOT_MQTT_Publish(void *pClient, char *topicName, PublishParams *pParams)
|
||||
return qcloud_iot_mqtt_publish(mqtt_client, topicName, pParams);
|
||||
}
|
||||
|
||||
int IOT_MQTT_Subscribe(void *pClient, char *topicFilter, SubscribeParams *pParams) {
|
||||
int IOT_MQTT_Subscribe(void *pClient, char *topicFilter, SubscribeParams *pParams)
|
||||
{
|
||||
|
||||
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
|
||||
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
|
||||
|
||||
return qcloud_iot_mqtt_subscribe(mqtt_client, topicFilter, pParams);
|
||||
return qcloud_iot_mqtt_subscribe(mqtt_client, topicFilter, pParams);
|
||||
}
|
||||
|
||||
int IOT_MQTT_Unsubscribe(void *pClient, char *topicFilter) {
|
||||
int IOT_MQTT_Unsubscribe(void *pClient, char *topicFilter)
|
||||
{
|
||||
|
||||
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
|
||||
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
|
||||
|
||||
return qcloud_iot_mqtt_unsubscribe(mqtt_client, topicFilter);
|
||||
return qcloud_iot_mqtt_unsubscribe(mqtt_client, topicFilter);
|
||||
}
|
||||
|
||||
bool IOT_MQTT_IsConnected(void *pClient) {
|
||||
bool IOT_MQTT_IsConnected(void *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -239,27 +243,27 @@ bool IOT_MQTT_IsConnected(void *pClient) {
|
||||
}
|
||||
|
||||
static inline void _strlowr(char *str)
|
||||
{
|
||||
while(*str != '\0')
|
||||
{
|
||||
*str = tolower(*str);
|
||||
{
|
||||
while (*str != '\0') {
|
||||
*str = tolower(*str);
|
||||
str++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams) {
|
||||
int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
POINTER_SANITY_CHECK(pParams, QCLOUD_ERR_INVAL);
|
||||
|
||||
memset(pClient, 0x0, sizeof(Qcloud_IoT_Client));
|
||||
|
||||
memset(pClient, 0x0, sizeof(Qcloud_IoT_Client));
|
||||
|
||||
int size = HAL_Snprintf(s_qcloud_iot_host, HOST_STR_LENGTH, "%s.%s", pParams->product_id, QCLOUD_IOT_MQTT_DIRECT_DOMAIN);
|
||||
if (size < 0 || size > HOST_STR_LENGTH - 1) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
|
||||
_strlowr(s_qcloud_iot_host);
|
||||
|
||||
int i = 0;
|
||||
@@ -272,9 +276,9 @@ int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams) {
|
||||
}
|
||||
|
||||
if (pParams->command_timeout < MIN_COMMAND_TIMEOUT)
|
||||
pParams->command_timeout = MIN_COMMAND_TIMEOUT;
|
||||
pParams->command_timeout = MIN_COMMAND_TIMEOUT;
|
||||
if (pParams->command_timeout > MAX_COMMAND_TIMEOUT)
|
||||
pParams->command_timeout = MAX_COMMAND_TIMEOUT;
|
||||
pParams->command_timeout = MAX_COMMAND_TIMEOUT;
|
||||
pClient->command_timeout_ms = pParams->command_timeout;
|
||||
|
||||
// packet id, random from [1 - 65536]
|
||||
@@ -284,7 +288,7 @@ int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams) {
|
||||
pClient->is_ping_outstanding = 0;
|
||||
pClient->was_manually_disconnected = 0;
|
||||
pClient->counter_network_disconnected = 0;
|
||||
|
||||
|
||||
pClient->event_handle = pParams->event_handle;
|
||||
|
||||
pClient->lock_generic = HAL_MutexCreate();
|
||||
@@ -295,29 +299,29 @@ int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams) {
|
||||
set_client_conn_state(pClient, NOTCONNECTED);
|
||||
|
||||
if ((pClient->lock_write_buf = HAL_MutexCreate()) == NULL) {
|
||||
Log_e("create write buf lock failed.");
|
||||
goto error;
|
||||
Log_e("create write buf lock failed.");
|
||||
goto error;
|
||||
}
|
||||
if ((pClient->lock_list_sub = HAL_MutexCreate()) == NULL) {
|
||||
Log_e("create sub list lock failed.");
|
||||
goto error;
|
||||
Log_e("create sub list lock failed.");
|
||||
goto error;
|
||||
}
|
||||
if ((pClient->lock_list_pub = HAL_MutexCreate()) == NULL) {
|
||||
Log_e("create pub list lock failed.");
|
||||
goto error;
|
||||
Log_e("create pub list lock failed.");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((pClient->list_pub_wait_ack = list_new()) == NULL) {
|
||||
Log_e("create pub wait list failed.");
|
||||
goto error;
|
||||
}
|
||||
pClient->list_pub_wait_ack->free = HAL_Free;
|
||||
|
||||
if ((pClient->list_sub_wait_ack = list_new()) == NULL) {
|
||||
Log_e("create sub wait list failed.");
|
||||
Log_e("create pub wait list failed.");
|
||||
goto error;
|
||||
}
|
||||
pClient->list_sub_wait_ack->free = HAL_Free;
|
||||
pClient->list_pub_wait_ack->free = HAL_Free;
|
||||
|
||||
if ((pClient->list_sub_wait_ack = list_new()) == NULL) {
|
||||
Log_e("create sub wait list failed.");
|
||||
goto error;
|
||||
}
|
||||
pClient->list_sub_wait_ack->free = HAL_Free;
|
||||
|
||||
#ifndef AUTH_WITH_NOTLS
|
||||
// device param for TLS connection
|
||||
@@ -357,8 +361,8 @@ int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams) {
|
||||
|
||||
pClient->network_stack.host = s_qcloud_iot_host;
|
||||
pClient->network_stack.port = s_qcloud_iot_port;
|
||||
pClient->network_stack.ssl_connect_params.timeout_ms =
|
||||
pClient->command_timeout_ms > QCLOUD_IOT_TLS_HANDSHAKE_TIMEOUT ? pClient->command_timeout_ms:QCLOUD_IOT_TLS_HANDSHAKE_TIMEOUT;
|
||||
pClient->network_stack.ssl_connect_params.timeout_ms =
|
||||
pClient->command_timeout_ms > QCLOUD_IOT_TLS_HANDSHAKE_TIMEOUT ? pClient->command_timeout_ms : QCLOUD_IOT_TLS_HANDSHAKE_TIMEOUT;
|
||||
|
||||
#else
|
||||
pClient->network_stack.host = s_qcloud_iot_host;
|
||||
@@ -375,56 +379,57 @@ int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
|
||||
error:
|
||||
if (pClient->list_pub_wait_ack) {
|
||||
pClient->list_pub_wait_ack->free(pClient->list_pub_wait_ack);
|
||||
pClient->list_pub_wait_ack = NULL;
|
||||
}
|
||||
if (pClient->list_sub_wait_ack) {
|
||||
pClient->list_sub_wait_ack->free(pClient->list_sub_wait_ack);
|
||||
pClient->list_sub_wait_ack = NULL;
|
||||
}
|
||||
if (pClient->lock_generic) {
|
||||
HAL_MutexDestroy(pClient->lock_generic);
|
||||
pClient->lock_generic = NULL;
|
||||
}
|
||||
if (pClient->lock_list_sub) {
|
||||
HAL_MutexDestroy(pClient->lock_list_sub);
|
||||
pClient->lock_list_sub = NULL;
|
||||
}
|
||||
if (pClient->lock_list_pub) {
|
||||
HAL_MutexDestroy(pClient->lock_list_pub);
|
||||
pClient->lock_list_pub = NULL;
|
||||
}
|
||||
if (pClient->lock_write_buf) {
|
||||
HAL_MutexDestroy(pClient->lock_write_buf);
|
||||
pClient->lock_write_buf = NULL;
|
||||
}
|
||||
if (pClient->list_pub_wait_ack) {
|
||||
pClient->list_pub_wait_ack->free(pClient->list_pub_wait_ack);
|
||||
pClient->list_pub_wait_ack = NULL;
|
||||
}
|
||||
if (pClient->list_sub_wait_ack) {
|
||||
pClient->list_sub_wait_ack->free(pClient->list_sub_wait_ack);
|
||||
pClient->list_sub_wait_ack = NULL;
|
||||
}
|
||||
if (pClient->lock_generic) {
|
||||
HAL_MutexDestroy(pClient->lock_generic);
|
||||
pClient->lock_generic = NULL;
|
||||
}
|
||||
if (pClient->lock_list_sub) {
|
||||
HAL_MutexDestroy(pClient->lock_list_sub);
|
||||
pClient->lock_list_sub = NULL;
|
||||
}
|
||||
if (pClient->lock_list_pub) {
|
||||
HAL_MutexDestroy(pClient->lock_list_pub);
|
||||
pClient->lock_list_pub = NULL;
|
||||
}
|
||||
if (pClient->lock_write_buf) {
|
||||
HAL_MutexDestroy(pClient->lock_write_buf);
|
||||
pClient->lock_write_buf = NULL;
|
||||
}
|
||||
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE)
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE)
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_deinit(Qcloud_IoT_Client *mqtt_client)
|
||||
int qcloud_iot_mqtt_deinit(Qcloud_IoT_Client *mqtt_client)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(mqtt_client, QCLOUD_ERR_INVAL);
|
||||
|
||||
HAL_MutexDestroy(mqtt_client->lock_generic);
|
||||
HAL_MutexDestroy(mqtt_client->lock_write_buf);
|
||||
HAL_MutexDestroy(mqtt_client->lock_write_buf);
|
||||
|
||||
HAL_MutexDestroy(mqtt_client->lock_list_sub);
|
||||
HAL_MutexDestroy(mqtt_client->lock_list_pub);
|
||||
|
||||
list_destroy(mqtt_client->list_pub_wait_ack);
|
||||
list_destroy(mqtt_client->list_sub_wait_ack);
|
||||
|
||||
|
||||
Log_i("release mqtt client resources");
|
||||
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
int qcloud_iot_mqtt_set_autoreconnect(Qcloud_IoT_Client *pClient, bool value) {
|
||||
int qcloud_iot_mqtt_set_autoreconnect(Qcloud_IoT_Client *pClient, bool value)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -434,7 +439,8 @@ int qcloud_iot_mqtt_set_autoreconnect(Qcloud_IoT_Client *pClient, bool value) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
bool qcloud_iot_mqtt_is_autoreconnect_enabled(Qcloud_IoT_Client *pClient) {
|
||||
bool qcloud_iot_mqtt_is_autoreconnect_enabled(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -447,7 +453,8 @@ bool qcloud_iot_mqtt_is_autoreconnect_enabled(Qcloud_IoT_Client *pClient) {
|
||||
IOT_FUNC_EXIT_RC(is_enabled);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_get_network_disconnected_count(Qcloud_IoT_Client *pClient) {
|
||||
int qcloud_iot_mqtt_get_network_disconnected_count(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -455,7 +462,8 @@ int qcloud_iot_mqtt_get_network_disconnected_count(Qcloud_IoT_Client *pClient) {
|
||||
IOT_FUNC_EXIT_RC(pClient->counter_network_disconnected);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_reset_network_disconnected_count(Qcloud_IoT_Client *pClient) {
|
||||
int qcloud_iot_mqtt_reset_network_disconnected_count(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
|
@@ -27,7 +27,7 @@ extern "C" {
|
||||
#include "utils_list.h"
|
||||
|
||||
/* remain waiting time after MQTT header is received (unit: ms) */
|
||||
#define QCLOUD_IOT_MQTT_MAX_REMAIN_WAIT_MS (2000)
|
||||
#define QCLOUD_IOT_MQTT_MAX_REMAIN_WAIT_MS (2000)
|
||||
|
||||
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
|
||||
|
||||
@@ -59,7 +59,8 @@ static int _check_handle_is_identical(SubTopicHandle *sub_handle1, SubTopicHandl
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t get_next_packet_id(Qcloud_IoT_Client *pClient) {
|
||||
uint16_t get_next_packet_id(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -71,25 +72,26 @@ uint16_t get_next_packet_id(Qcloud_IoT_Client *pClient) {
|
||||
IOT_FUNC_EXIT_RC(pClient->next_packet_id);
|
||||
}
|
||||
|
||||
void get_next_conn_id(char *conn_id) {
|
||||
int i;
|
||||
srand((unsigned)HAL_GetTimeMs());
|
||||
for (i = 0; i < MAX_CONN_ID_LEN - 1; i++) {
|
||||
int flag = rand() % 3;
|
||||
switch(flag) {
|
||||
case 0:
|
||||
conn_id[i] = (rand() % 26) + 'a';
|
||||
break;
|
||||
case 1:
|
||||
conn_id[i] = (rand() % 26) + 'A';
|
||||
break;
|
||||
case 2:
|
||||
conn_id[i] = (rand() % 10) + '0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
void get_next_conn_id(char *conn_id)
|
||||
{
|
||||
int i;
|
||||
srand((unsigned)HAL_GetTimeMs());
|
||||
for (i = 0; i < MAX_CONN_ID_LEN - 1; i++) {
|
||||
int flag = rand() % 3;
|
||||
switch (flag) {
|
||||
case 0:
|
||||
conn_id[i] = (rand() % 26) + 'a';
|
||||
break;
|
||||
case 1:
|
||||
conn_id[i] = (rand() % 26) + 'A';
|
||||
break;
|
||||
case 2:
|
||||
conn_id[i] = (rand() % 10) + '0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
conn_id[MAX_CONN_ID_LEN - 1] = '\0';
|
||||
conn_id[MAX_CONN_ID_LEN - 1] = '\0';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +100,8 @@ void get_next_conn_id(char *conn_id) {
|
||||
* @param length the length to be encoded
|
||||
* @return the number of bytes written to buffer
|
||||
*/
|
||||
size_t mqtt_write_packet_rem_len(unsigned char *buf, uint32_t length) {
|
||||
size_t mqtt_write_packet_rem_len(unsigned char *buf, uint32_t length)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
size_t outLen = 0;
|
||||
@@ -117,7 +120,8 @@ size_t mqtt_write_packet_rem_len(unsigned char *buf, uint32_t length) {
|
||||
IOT_FUNC_EXIT_RC((int)outLen);
|
||||
}
|
||||
|
||||
size_t get_mqtt_packet_len(size_t rem_len) {
|
||||
size_t get_mqtt_packet_len(size_t rem_len)
|
||||
{
|
||||
rem_len += 1; /* header byte */
|
||||
|
||||
/* now remaining_length field */
|
||||
@@ -141,7 +145,8 @@ size_t get_mqtt_packet_len(size_t rem_len) {
|
||||
* @return the number of bytes read from the socket
|
||||
*/
|
||||
static int _decode_packet_rem_len_from_buf_read(uint32_t (*getcharfn)(unsigned char *, uint32_t), uint32_t *value,
|
||||
uint32_t *readBytesLen) {
|
||||
uint32_t *readBytesLen)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
unsigned char c;
|
||||
@@ -168,7 +173,8 @@ static int _decode_packet_rem_len_from_buf_read(uint32_t (*getcharfn)(unsigned c
|
||||
}
|
||||
|
||||
static unsigned char *bufptr;
|
||||
uint32_t bufchar(unsigned char *c, uint32_t count) {
|
||||
uint32_t bufchar(unsigned char *c, uint32_t count)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < count; ++i) {
|
||||
@@ -178,7 +184,8 @@ uint32_t bufchar(unsigned char *c, uint32_t count) {
|
||||
return count;
|
||||
}
|
||||
|
||||
int mqtt_read_packet_rem_len_form_buf(unsigned char *buf, uint32_t *value, uint32_t *readBytesLen) {
|
||||
int mqtt_read_packet_rem_len_form_buf(unsigned char *buf, uint32_t *value, uint32_t *readBytesLen)
|
||||
{
|
||||
bufptr = buf;
|
||||
return _decode_packet_rem_len_from_buf_read(bufchar, value, readBytesLen);
|
||||
}
|
||||
@@ -188,7 +195,8 @@ int mqtt_read_packet_rem_len_form_buf(unsigned char *buf, uint32_t *value, uint3
|
||||
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
|
||||
* @return the value calculated
|
||||
*/
|
||||
uint16_t mqtt_read_uint16_t(unsigned char **pptr) {
|
||||
uint16_t mqtt_read_uint16_t(unsigned char **pptr)
|
||||
{
|
||||
unsigned char *ptr = *pptr;
|
||||
uint8_t firstByte = (uint8_t) (*ptr);
|
||||
uint8_t secondByte = (uint8_t) (*(ptr + 1));
|
||||
@@ -202,7 +210,8 @@ uint16_t mqtt_read_uint16_t(unsigned char **pptr) {
|
||||
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
|
||||
* @return the character read
|
||||
*/
|
||||
unsigned char mqtt_read_char(unsigned char **pptr) {
|
||||
unsigned char mqtt_read_char(unsigned char **pptr)
|
||||
{
|
||||
unsigned char c = **pptr;
|
||||
(*pptr)++;
|
||||
return c;
|
||||
@@ -213,7 +222,8 @@ unsigned char mqtt_read_char(unsigned char **pptr) {
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param c the character to write
|
||||
*/
|
||||
void mqtt_write_char(unsigned char **pptr, unsigned char c) {
|
||||
void mqtt_write_char(unsigned char **pptr, unsigned char c)
|
||||
{
|
||||
**pptr = c;
|
||||
(*pptr)++;
|
||||
}
|
||||
@@ -223,7 +233,8 @@ void mqtt_write_char(unsigned char **pptr, unsigned char c) {
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param anInt the integer to write
|
||||
*/
|
||||
void mqtt_write_uint_16(unsigned char **pptr, uint16_t anInt) {
|
||||
void mqtt_write_uint_16(unsigned char **pptr, uint16_t anInt)
|
||||
{
|
||||
**pptr = (unsigned char) (anInt / 256);
|
||||
(*pptr)++;
|
||||
**pptr = (unsigned char) (anInt % 256);
|
||||
@@ -235,7 +246,8 @@ void mqtt_write_uint_16(unsigned char **pptr, uint16_t anInt) {
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param string the C string to write
|
||||
*/
|
||||
void mqtt_write_utf8_string(unsigned char **pptr, const char *string) {
|
||||
void mqtt_write_utf8_string(unsigned char **pptr, const char *string)
|
||||
{
|
||||
size_t len = strlen(string);
|
||||
mqtt_write_uint_16(pptr, (uint16_t) len);
|
||||
memcpy(*pptr, string, len);
|
||||
@@ -319,10 +331,10 @@ int mqtt_init_packet_header(unsigned char *header, MessageTypes message_type,
|
||||
}
|
||||
|
||||
/* Generate the final protocol header by using bitwise operator */
|
||||
*header = ((type<<MQTT_HEADER_TYPE_SHIFT)&MQTT_HEADER_TYPE_MASK)
|
||||
| ((dup<<MQTT_HEADER_DUP_SHIFT)&MQTT_HEADER_DUP_MASK)
|
||||
| ((qos<<MQTT_HEADER_QOS_SHIFT)&MQTT_HEADER_QOS_MASK)
|
||||
| (retained&MQTT_HEADER_RETAIN_MASK);
|
||||
*header = ((type << MQTT_HEADER_TYPE_SHIFT)&MQTT_HEADER_TYPE_MASK)
|
||||
| ((dup << MQTT_HEADER_DUP_SHIFT)&MQTT_HEADER_DUP_MASK)
|
||||
| ((qos << MQTT_HEADER_QOS_SHIFT)&MQTT_HEADER_QOS_MASK)
|
||||
| (retained & MQTT_HEADER_RETAIN_MASK);
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
@@ -336,7 +348,8 @@ int mqtt_init_packet_header(unsigned char *header, MessageTypes message_type,
|
||||
* @param buf_len the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int deserialize_ack_packet(uint8_t *packet_type, uint8_t *dup, uint16_t *packet_id, unsigned char *buf, size_t buf_len) {
|
||||
int deserialize_ack_packet(uint8_t *packet_type, uint8_t *dup, uint16_t *packet_id, unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(packet_type, QCLOUD_ERR_INVAL);
|
||||
@@ -355,9 +368,9 @@ int deserialize_ack_packet(uint8_t *packet_type, uint8_t *dup, uint16_t *packet_
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_BUF_TOO_SHORT);
|
||||
}
|
||||
|
||||
header = mqtt_read_char(&curdata);
|
||||
*packet_type = ((header&MQTT_HEADER_TYPE_MASK)>>MQTT_HEADER_TYPE_SHIFT);
|
||||
*dup = ((header&MQTT_HEADER_DUP_MASK)>>MQTT_HEADER_DUP_SHIFT);
|
||||
header = mqtt_read_char(&curdata);
|
||||
*packet_type = ((header & MQTT_HEADER_TYPE_MASK) >> MQTT_HEADER_TYPE_SHIFT);
|
||||
*dup = ((header & MQTT_HEADER_DUP_MASK) >> MQTT_HEADER_DUP_SHIFT);
|
||||
|
||||
/* read remaining length */
|
||||
rc = mqtt_read_packet_rem_len_form_buf(curdata, &decodedLen, &readBytesLen);
|
||||
@@ -372,7 +385,7 @@ int deserialize_ack_packet(uint8_t *packet_type, uint8_t *dup, uint16_t *packet_
|
||||
}
|
||||
|
||||
*packet_id = mqtt_read_uint16_t(&curdata);
|
||||
|
||||
|
||||
if (enddata - curdata >= 1) {
|
||||
unsigned char ack_code = mqtt_read_char(&curdata);
|
||||
if (ack_code != 0) {
|
||||
@@ -395,7 +408,7 @@ int deserialize_ack_packet(uint8_t *packet_type, uint8_t *dup, uint16_t *packet_
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int deserialize_suback_packet(uint16_t *packet_id, uint32_t max_count, uint32_t *count,
|
||||
QoS *grantedQoSs, unsigned char *buf, size_t buf_len)
|
||||
QoS *grantedQoSs, unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
@@ -416,12 +429,12 @@ int deserialize_suback_packet(uint16_t *packet_id, uint32_t max_count, uint32_t
|
||||
}
|
||||
// read 1st byte in fixed header
|
||||
header = mqtt_read_char(&curdata);
|
||||
type = (header&MQTT_HEADER_TYPE_MASK)>>MQTT_HEADER_TYPE_SHIFT;
|
||||
type = (header & MQTT_HEADER_TYPE_MASK) >> MQTT_HEADER_TYPE_SHIFT;
|
||||
if (type != SUBACK) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
|
||||
// read remain part in fixed header
|
||||
// read remain part in fixed header
|
||||
decodeRc = mqtt_read_packet_rem_len_form_buf(curdata, &decodedLen, &readBytesLen);
|
||||
if (decodeRc != QCLOUD_RET_SUCCESS) {
|
||||
IOT_FUNC_EXIT_RC(decodeRc);
|
||||
@@ -455,7 +468,7 @@ int deserialize_suback_packet(uint16_t *packet_id, uint32_t max_count, uint32_t
|
||||
* @param buf_len the length in bytes of the data in the supplied buffer
|
||||
* @return int indicating function execution status
|
||||
*/
|
||||
int deserialize_unsuback_packet(uint16_t *packet_id, unsigned char *buf, size_t buf_len)
|
||||
int deserialize_unsuback_packet(uint16_t *packet_id, unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
@@ -482,7 +495,8 @@ int deserialize_unsuback_packet(uint16_t *packet_id, unsigned char *buf, size_t
|
||||
* @param serialized length
|
||||
* @return int indicating function execution status
|
||||
*/
|
||||
int serialize_packet_with_zero_payload(unsigned char *buf, size_t buf_len, MessageTypes packetType, uint32_t *serialized_len) {
|
||||
int serialize_packet_with_zero_payload(unsigned char *buf, size_t buf_len, MessageTypes packetType, uint32_t *serialized_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
|
||||
@@ -512,7 +526,8 @@ int serialize_packet_with_zero_payload(unsigned char *buf, size_t buf_len, Messa
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
int send_mqtt_packet(Qcloud_IoT_Client *pClient, size_t length, Timer *timer) {
|
||||
int send_mqtt_packet(Qcloud_IoT_Client *pClient, size_t length, Timer *timer)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -544,7 +559,8 @@ int send_mqtt_packet(Qcloud_IoT_Client *pClient, size_t length, Timer *timer) {
|
||||
}
|
||||
|
||||
|
||||
static int _decode_packet_rem_len_with_net_read(Qcloud_IoT_Client *pClient, uint32_t *value, uint32_t timeout) {
|
||||
static int _decode_packet_rem_len_with_net_read(Qcloud_IoT_Client *pClient, uint32_t *value, uint32_t timeout)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -591,7 +607,8 @@ static int _decode_packet_rem_len_with_net_read(Qcloud_IoT_Client *pClient, uint
|
||||
* @param packet_type MQTT packet type
|
||||
* @return QCLOUD_RET_SUCCESS for success, or err code for failure
|
||||
*/
|
||||
static int _read_mqtt_packet(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_type) {
|
||||
static int _read_mqtt_packet(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_type)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -601,9 +618,9 @@ static int _read_mqtt_packet(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *
|
||||
uint32_t rem_len = 0;
|
||||
size_t read_len = 0;
|
||||
int rc;
|
||||
int timer_left_ms = left_ms(timer);
|
||||
|
||||
if (timer_left_ms <= 0) {
|
||||
int timer_left_ms = left_ms(timer);
|
||||
|
||||
if (timer_left_ms <= 0) {
|
||||
timer_left_ms = 1;
|
||||
}
|
||||
|
||||
@@ -618,12 +635,12 @@ static int _read_mqtt_packet(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *
|
||||
len = 1;
|
||||
|
||||
// 2. read the remaining length
|
||||
timer_left_ms = left_ms(timer);
|
||||
timer_left_ms = left_ms(timer);
|
||||
if (timer_left_ms <= 0) {
|
||||
timer_left_ms = 1;
|
||||
}
|
||||
timer_left_ms += QCLOUD_IOT_MQTT_MAX_REMAIN_WAIT_MS;
|
||||
|
||||
|
||||
rc = _decode_packet_rem_len_with_net_read(pClient, &rem_len, timer_left_ms);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
@@ -635,7 +652,7 @@ static int _read_mqtt_packet(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *
|
||||
size_t bytes_to_be_read;
|
||||
int32_t ret_val = 0;
|
||||
|
||||
timer_left_ms = left_ms(timer);
|
||||
timer_left_ms = left_ms(timer);
|
||||
if (timer_left_ms <= 0) {
|
||||
timer_left_ms = 1;
|
||||
}
|
||||
@@ -644,7 +661,7 @@ static int _read_mqtt_packet(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *
|
||||
bytes_to_be_read = pClient->read_buf_size;
|
||||
do {
|
||||
ret_val = pClient->network_stack.read(&(pClient->network_stack), pClient->read_buf, bytes_to_be_read, timer_left_ms,
|
||||
&read_len);
|
||||
&read_len);
|
||||
if (ret_val == QCLOUD_RET_SUCCESS) {
|
||||
total_bytes_read += read_len;
|
||||
if ((rem_len - total_bytes_read) >= pClient->read_buf_size) {
|
||||
@@ -663,33 +680,32 @@ static int _read_mqtt_packet(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *
|
||||
|
||||
// 3. read payload according to remaining length
|
||||
if (rem_len > 0 && ((len + rem_len) > pClient->read_buf_size)) {
|
||||
|
||||
timer_left_ms = left_ms(timer);
|
||||
|
||||
timer_left_ms = left_ms(timer);
|
||||
if (timer_left_ms <= 0) {
|
||||
timer_left_ms = 1;
|
||||
}
|
||||
timer_left_ms += QCLOUD_IOT_MQTT_MAX_REMAIN_WAIT_MS;
|
||||
|
||||
pClient->network_stack.read(&(pClient->network_stack), pClient->read_buf, rem_len, timer_left_ms, &read_len);
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_BUF_TOO_SHORT);
|
||||
}
|
||||
else {
|
||||
|
||||
pClient->network_stack.read(&(pClient->network_stack), pClient->read_buf, rem_len, timer_left_ms, &read_len);
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_BUF_TOO_SHORT);
|
||||
} else {
|
||||
if (rem_len > 0) {
|
||||
|
||||
timer_left_ms = left_ms(timer);
|
||||
if (timer_left_ms <= 0) {
|
||||
timer_left_ms = 1;
|
||||
}
|
||||
timer_left_ms += QCLOUD_IOT_MQTT_MAX_REMAIN_WAIT_MS;
|
||||
rc = pClient->network_stack.read(&(pClient->network_stack), pClient->read_buf + len, rem_len, timer_left_ms, &read_len);
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
timer_left_ms = left_ms(timer);
|
||||
if (timer_left_ms <= 0) {
|
||||
timer_left_ms = 1;
|
||||
}
|
||||
timer_left_ms += QCLOUD_IOT_MQTT_MAX_REMAIN_WAIT_MS;
|
||||
rc = pClient->network_stack.read(&(pClient->network_stack), pClient->read_buf + len, rem_len, timer_left_ms, &read_len);
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*packet_type = (pClient->read_buf[0]&MQTT_HEADER_TYPE_MASK)>>MQTT_HEADER_TYPE_SHIFT;
|
||||
|
||||
*packet_type = (pClient->read_buf[0] & MQTT_HEADER_TYPE_MASK) >> MQTT_HEADER_TYPE_SHIFT;
|
||||
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -700,7 +716,8 @@ static int _read_mqtt_packet(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *
|
||||
* @param topicName
|
||||
* @return
|
||||
*/
|
||||
static uint8_t _is_topic_equals(char *topic_filter, char *topicName) {
|
||||
static uint8_t _is_topic_equals(char *topic_filter, char *topicName)
|
||||
{
|
||||
return (uint8_t) (strlen(topic_filter) == strlen(topicName) && !strcmp(topic_filter, topicName));
|
||||
}
|
||||
|
||||
@@ -716,7 +733,8 @@ static uint8_t _is_topic_equals(char *topic_filter, char *topicName) {
|
||||
* @param topicNameLen length of topic name
|
||||
* @return
|
||||
*/
|
||||
static uint8_t _is_topic_matched(char *topic_filter, char *topicName, uint16_t topicNameLen) {
|
||||
static uint8_t _is_topic_matched(char *topic_filter, char *topicName, uint16_t topicNameLen)
|
||||
{
|
||||
char *curf;
|
||||
char *curn;
|
||||
char *curn_end;
|
||||
@@ -769,7 +787,8 @@ static uint8_t _is_topic_matched(char *topic_filter, char *topicName, uint16_t t
|
||||
* @param message
|
||||
* @return
|
||||
*/
|
||||
static int _deliver_message(Qcloud_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, MQTTMessage *message) {
|
||||
static int _deliver_message(Qcloud_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, MQTTMessage *message)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -781,13 +800,12 @@ static int _deliver_message(Qcloud_IoT_Client *pClient, char *topicName, uint16_
|
||||
|
||||
uint32_t i;
|
||||
int flag_matched = 0;
|
||||
|
||||
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
|
||||
if ((pClient->sub_handles[i].topic_filter != NULL)
|
||||
&& (_is_topic_equals(topicName, (char *) pClient->sub_handles[i].topic_filter) ||
|
||||
_is_topic_matched((char *) pClient->sub_handles[i].topic_filter, topicName, topicNameLen)))
|
||||
{
|
||||
_is_topic_matched((char *) pClient->sub_handles[i].topic_filter, topicName, topicNameLen))) {
|
||||
HAL_MutexUnlock(pClient->lock_generic);
|
||||
if (pClient->sub_handles[i].message_handler != NULL) {
|
||||
pClient->sub_handles[i].message_handler(pClient, message, pClient->sub_handles[i].handler_user_data);
|
||||
@@ -904,7 +922,7 @@ static int _mask_sub_info_from(Qcloud_IoT_Client *c, unsigned int msgId, SubTopi
|
||||
if (sub_info->msg_id == msgId) {
|
||||
*messageHandler = sub_info->handler; /* return handle */
|
||||
sub_info->node_state = MQTT_NODE_STATE_INVALID; /* mark as invalid node */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list_iterator_destroy(iter);
|
||||
@@ -956,7 +974,7 @@ static int _handle_suback_packet(Qcloud_IoT_Client *pClient, Timer *timer, QoS q
|
||||
QoS grantedQoS[3] = {QOS0, QOS0, QOS0};
|
||||
int rc;
|
||||
bool sub_nack = false;
|
||||
|
||||
|
||||
rc = deserialize_suback_packet(&packet_id, 1, &count, grantedQoS, pClient->read_buf, pClient->read_buf_size);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
@@ -965,7 +983,7 @@ static int _handle_suback_packet(Qcloud_IoT_Client *pClient, Timer *timer, QoS q
|
||||
int flag_dup = 0, i_free = -1;
|
||||
// check return code in SUBACK packet: 0x00(QOS0, SUCCESS),0x01(QOS1, SUCCESS),0x02(QOS2, SUCCESS),0x80(Failure)
|
||||
if (grantedQoS[0] == 0x80) {
|
||||
MQTTEventMsg msg;
|
||||
MQTTEventMsg msg;
|
||||
|
||||
msg.event_type = MQTT_EVENT_SUBCRIBE_NACK;
|
||||
msg.msg = (void *)(uintptr_t)packet_id;
|
||||
@@ -975,7 +993,7 @@ static int _handle_suback_packet(Qcloud_IoT_Client *pClient, Timer *timer, QoS q
|
||||
}
|
||||
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
|
||||
|
||||
SubTopicHandle sub_handle;
|
||||
memset(&sub_handle, 0, sizeof(SubTopicHandle));
|
||||
(void)_mask_sub_info_from(pClient, (unsigned int)packet_id, &sub_handle);
|
||||
@@ -992,25 +1010,25 @@ static int _handle_suback_packet(Qcloud_IoT_Client *pClient, Timer *timer, QoS q
|
||||
/* notify this event to topic subscriber */
|
||||
if (NULL != sub_handle.sub_event_handler)
|
||||
sub_handle.sub_event_handler(pClient, MQTT_EVENT_SUBCRIBE_NACK, sub_handle.handler_user_data);
|
||||
|
||||
|
||||
HAL_Free((void *)sub_handle.topic_filter);
|
||||
sub_handle.topic_filter = NULL;
|
||||
sub_handle.topic_filter = NULL;
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_MQTT_SUB);
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
|
||||
if ((NULL != pClient->sub_handles[i].topic_filter)) {
|
||||
if (0 == _check_handle_is_identical(&pClient->sub_handles[i], &sub_handle)) {
|
||||
if (0 == _check_handle_is_identical(&pClient->sub_handles[i], &sub_handle)) {
|
||||
flag_dup = 1;
|
||||
Log_w("Identical topic found: %s", sub_handle.topic_filter);
|
||||
if (pClient->sub_handles[i].handler_user_data != sub_handle.handler_user_data) {
|
||||
Log_w("Update handler_user_data %p -> %p!",
|
||||
pClient->sub_handles[i].handler_user_data, sub_handle.handler_user_data);
|
||||
Log_w("Update handler_user_data %p -> %p!",
|
||||
pClient->sub_handles[i].handler_user_data, sub_handle.handler_user_data);
|
||||
pClient->sub_handles[i].handler_user_data = sub_handle.handler_user_data;
|
||||
}
|
||||
HAL_Free((void *)sub_handle.topic_filter);
|
||||
sub_handle.topic_filter = NULL;
|
||||
sub_handle.topic_filter = NULL;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
@@ -1033,7 +1051,7 @@ static int _handle_suback_packet(Qcloud_IoT_Client *pClient, Timer *timer, QoS q
|
||||
pClient->sub_handles[i_free].handler_user_data = sub_handle.handler_user_data;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
HAL_MutexUnlock(pClient->lock_generic);
|
||||
|
||||
/* notify this event to user callback */
|
||||
@@ -1074,18 +1092,18 @@ static int _handle_unsuback_packet(Qcloud_IoT_Client *pClient, Timer *timer)
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
|
||||
/* actually below code is nonsense as unsub handle is different with sub handle even the same topic_filter*/
|
||||
#if 0
|
||||
#if 0
|
||||
int i;
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
|
||||
if ((pClient->sub_handles[i].topic_filter != NULL)
|
||||
&& (0 == _check_handle_is_identical(&pClient->sub_handles[i], &messageHandler))) {
|
||||
&& (0 == _check_handle_is_identical(&pClient->sub_handles[i], &messageHandler))) {
|
||||
memset(&pClient->sub_handles[i], 0, sizeof(SubTopicHandle));
|
||||
|
||||
/* NOTE: in case of more than one register(subscribe) with different callback function,
|
||||
* so we must keep continuously searching related message handle. */
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Free the topic filter malloced in qcloud_iot_mqtt_unsubscribe */
|
||||
if (messageHandler.topic_filter) {
|
||||
@@ -1115,10 +1133,8 @@ static uint16_t sg_repeat_packet_id_buf[MQTT_MAX_REPEAT_BUF_LEN];
|
||||
static int _get_packet_id_in_repeat_buf(uint16_t packet_id)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MQTT_MAX_REPEAT_BUF_LEN; ++i)
|
||||
{
|
||||
if (packet_id == sg_repeat_packet_id_buf[i])
|
||||
{
|
||||
for (i = 0; i < MQTT_MAX_REPEAT_BUF_LEN; ++i) {
|
||||
if (packet_id == sg_repeat_packet_id_buf[i]) {
|
||||
return packet_id;
|
||||
}
|
||||
}
|
||||
@@ -1140,8 +1156,7 @@ static void _add_packet_id_to_repeat_buf(uint16_t packet_id)
|
||||
void reset_repeat_packet_id_buffer(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MQTT_MAX_REPEAT_BUF_LEN; ++i)
|
||||
{
|
||||
for (i = 0; i < MQTT_MAX_REPEAT_BUF_LEN; ++i) {
|
||||
sg_repeat_packet_id_buf[i] = 0;
|
||||
}
|
||||
}
|
||||
@@ -1149,7 +1164,8 @@ void reset_repeat_packet_id_buffer(void)
|
||||
#endif
|
||||
|
||||
|
||||
static int _handle_publish_packet(Qcloud_IoT_Client *pClient, Timer *timer) {
|
||||
static int _handle_publish_packet(Qcloud_IoT_Client *pClient, Timer *timer)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
char *topic_name;
|
||||
uint16_t topic_len;
|
||||
@@ -1162,18 +1178,17 @@ static int _handle_publish_packet(Qcloud_IoT_Client *pClient, Timer *timer) {
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
|
||||
// topicName from packet is NOT null terminated
|
||||
char fix_topic[MAX_SIZE_OF_CLOUD_TOPIC] = {0};
|
||||
|
||||
if(topic_len > MAX_SIZE_OF_CLOUD_TOPIC){
|
||||
topic_len = MAX_SIZE_OF_CLOUD_TOPIC - 1;
|
||||
Log_e("topic len exceed buffer len");
|
||||
}
|
||||
|
||||
if (topic_len > MAX_SIZE_OF_CLOUD_TOPIC) {
|
||||
topic_len = MAX_SIZE_OF_CLOUD_TOPIC - 1;
|
||||
Log_e("topic len exceed buffer len");
|
||||
}
|
||||
memcpy(fix_topic, topic_name, topic_len);
|
||||
|
||||
if (QOS0 == msg.qos)
|
||||
{
|
||||
if (QOS0 == msg.qos) {
|
||||
rc = _deliver_message(pClient, fix_topic, topic_len, &msg);
|
||||
if (QCLOUD_RET_SUCCESS != rc)
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
@@ -1187,8 +1202,7 @@ static int _handle_publish_packet(Qcloud_IoT_Client *pClient, Timer *timer) {
|
||||
int repeat_id = _get_packet_id_in_repeat_buf(msg.id);
|
||||
|
||||
// deliver to msg callback
|
||||
if (repeat_id < 0)
|
||||
{
|
||||
if (repeat_id < 0) {
|
||||
#endif
|
||||
rc = _deliver_message(pClient, fix_topic, topic_len, &msg);
|
||||
if (QCLOUD_RET_SUCCESS != rc)
|
||||
@@ -1198,7 +1212,7 @@ static int _handle_publish_packet(Qcloud_IoT_Client *pClient, Timer *timer) {
|
||||
_add_packet_id_to_repeat_buf(msg.id);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
HAL_MutexLock(pClient->lock_write_buf);
|
||||
if (QOS1 == msg.qos) {
|
||||
rc = serialize_pub_ack_packet(pClient->write_buf, pClient->write_buf_size, PUBACK, 0, msg.id, &len);
|
||||
@@ -1222,7 +1236,8 @@ static int _handle_publish_packet(Qcloud_IoT_Client *pClient, Timer *timer) {
|
||||
}
|
||||
|
||||
|
||||
static int _handle_pubrec_packet(Qcloud_IoT_Client *pClient, Timer *timer) {
|
||||
static int _handle_pubrec_packet(Qcloud_IoT_Client *pClient, Timer *timer)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
uint16_t packet_id;
|
||||
unsigned char dup, type;
|
||||
@@ -1254,7 +1269,8 @@ static int _handle_pubrec_packet(Qcloud_IoT_Client *pClient, Timer *timer) {
|
||||
}
|
||||
|
||||
|
||||
static void _handle_pingresp_packet(Qcloud_IoT_Client *pClient) {
|
||||
static void _handle_pingresp_packet(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
@@ -1265,7 +1281,8 @@ static void _handle_pingresp_packet(Qcloud_IoT_Client *pClient) {
|
||||
IOT_FUNC_EXIT;
|
||||
}
|
||||
|
||||
int cycle_for_read(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_type, QoS qos) {
|
||||
int cycle_for_read(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_type, QoS qos)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -1309,29 +1326,29 @@ int cycle_for_read(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_typ
|
||||
}
|
||||
case PUBCOMP:
|
||||
break;
|
||||
case PINGRESP:
|
||||
case PINGRESP:
|
||||
break;
|
||||
default: {
|
||||
/* Either unknown packet type or Failure occurred
|
||||
* Should not happen */
|
||||
|
||||
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_RX_MESSAGE_INVAL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (*packet_type) {
|
||||
switch (*packet_type) {
|
||||
/* Recv below msgs are all considered as PING OK */
|
||||
case PUBACK:
|
||||
case SUBACK:
|
||||
case UNSUBACK:
|
||||
case UNSUBACK:
|
||||
case PINGRESP: {
|
||||
_handle_pingresp_packet(pClient);
|
||||
break;
|
||||
}
|
||||
/* Recv downlink pub means link is OK but we still need to send PING request */
|
||||
case PUBLISH: {
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
pClient->is_ping_outstanding = 0;
|
||||
HAL_MutexUnlock(pClient->lock_generic);
|
||||
break;
|
||||
@@ -1341,37 +1358,40 @@ int cycle_for_read(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_typ
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
int wait_for_read(Qcloud_IoT_Client *pClient, uint8_t packet_type, Timer *timer, QoS qos) {
|
||||
IOT_FUNC_ENTRY;
|
||||
int rc;
|
||||
uint8_t read_packet_type = 0;
|
||||
int wait_for_read(Qcloud_IoT_Client *pClient, uint8_t packet_type, Timer *timer, QoS qos)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
int rc;
|
||||
uint8_t read_packet_type = 0;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
POINTER_SANITY_CHECK(timer, QCLOUD_ERR_INVAL);
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
POINTER_SANITY_CHECK(timer, QCLOUD_ERR_INVAL);
|
||||
|
||||
do {
|
||||
if (expired(timer)) {
|
||||
rc = QCLOUD_ERR_MQTT_REQUEST_TIMEOUT;
|
||||
break;
|
||||
}
|
||||
rc = cycle_for_read(pClient, timer, &read_packet_type, qos);
|
||||
} while (QCLOUD_RET_SUCCESS == rc && read_packet_type != packet_type );
|
||||
do {
|
||||
if (expired(timer)) {
|
||||
rc = QCLOUD_ERR_MQTT_REQUEST_TIMEOUT;
|
||||
break;
|
||||
}
|
||||
rc = cycle_for_read(pClient, timer, &read_packet_type, qos);
|
||||
} while (QCLOUD_RET_SUCCESS == rc && read_packet_type != packet_type );
|
||||
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
void set_client_conn_state(Qcloud_IoT_Client *pClient, uint8_t connected) {
|
||||
void set_client_conn_state(Qcloud_IoT_Client *pClient, uint8_t connected)
|
||||
{
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
pClient->is_connected = connected;
|
||||
HAL_MutexUnlock(pClient->lock_generic);
|
||||
}
|
||||
|
||||
uint8_t get_client_conn_state(Qcloud_IoT_Client *pClient) {
|
||||
uint8_t get_client_conn_state(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
uint8_t is_connected = 0;
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
is_connected = pClient->is_connected;
|
||||
HAL_MutexUnlock(pClient->lock_generic);
|
||||
uint8_t is_connected = 0;
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
is_connected = pClient->is_connected;
|
||||
HAL_MutexUnlock(pClient->lock_generic);
|
||||
IOT_FUNC_EXIT_RC(is_connected);
|
||||
}
|
||||
|
||||
@@ -1381,7 +1401,7 @@ uint8_t get_client_conn_state(Qcloud_IoT_Client *pClient) {
|
||||
* return: 0, success; NOT 0, fail;
|
||||
*/
|
||||
int push_sub_info_to(Qcloud_IoT_Client *c, int len, unsigned short msgId, MessageTypes type,
|
||||
SubTopicHandle *handler, ListNode **node)
|
||||
SubTopicHandle *handler, ListNode **node)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
if (!c || !handler || !node) {
|
||||
@@ -1398,13 +1418,13 @@ int push_sub_info_to(Qcloud_IoT_Client *c, int len, unsigned short msgId, Messag
|
||||
}
|
||||
|
||||
QcloudIotSubInfo *sub_info = (QcloudIotSubInfo *)HAL_Malloc(sizeof(
|
||||
QcloudIotSubInfo) + len);
|
||||
QcloudIotSubInfo) + len);
|
||||
if (NULL == sub_info) {
|
||||
HAL_MutexUnlock(c->lock_list_sub);
|
||||
Log_e("malloc failed!");
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
sub_info->node_state = MQTT_NODE_STATE_NORMANL;
|
||||
sub_info->msg_id = msgId;
|
||||
sub_info->len = len;
|
||||
|
@@ -55,7 +55,8 @@ typedef enum {
|
||||
* @param the length of buffer needed to contain the serialized version of the packet
|
||||
* @return int indicating function execution status
|
||||
*/
|
||||
static uint32_t _get_packet_connect_rem_len(MQTTConnectParams *options) {
|
||||
static uint32_t _get_packet_connect_rem_len(MQTTConnectParams *options)
|
||||
{
|
||||
size_t len = 0;
|
||||
/* variable depending on MQTT or MQIsdp */
|
||||
if (3 == options->mqtt_version) {
|
||||
@@ -77,10 +78,16 @@ static uint32_t _get_packet_connect_rem_len(MQTTConnectParams *options) {
|
||||
return (uint32_t) len;
|
||||
}
|
||||
|
||||
static void _copy_connect_params(MQTTConnectParams *destination, MQTTConnectParams *source) {
|
||||
static void _copy_connect_params(MQTTConnectParams *destination, MQTTConnectParams *source)
|
||||
{
|
||||
|
||||
POINTER_SANITY_CHECK_RTN(destination);
|
||||
POINTER_SANITY_CHECK_RTN(source);
|
||||
POINTER_SANITY_CHECK_RTN(destination);
|
||||
POINTER_SANITY_CHECK_RTN(source);
|
||||
|
||||
/* In case of reconnecting, source == destination */
|
||||
if (source == destination) {
|
||||
return;
|
||||
}
|
||||
|
||||
destination->mqtt_version = source->mqtt_version;
|
||||
destination->client_id = source->client_id;
|
||||
@@ -102,7 +109,8 @@ static void _copy_connect_params(MQTTConnectParams *destination, MQTTConnectPara
|
||||
* @param serialized length
|
||||
* @return int indicating function execution status
|
||||
*/
|
||||
static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTConnectParams *options, uint32_t *serialized_len) {
|
||||
static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTConnectParams *options, uint32_t *serialized_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
|
||||
@@ -118,28 +126,28 @@ static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTCon
|
||||
|
||||
long cur_timesec = HAL_Timer_current_sec() + MAX_ACCESS_EXPIRE_TIMEOUT / 1000;
|
||||
if (cur_timesec <= 0 || MAX_ACCESS_EXPIRE_TIMEOUT <= 0) {
|
||||
cur_timesec = LONG_MAX;
|
||||
cur_timesec = LONG_MAX;
|
||||
}
|
||||
long cur_timesec_bak = cur_timesec;
|
||||
int cur_timesec_len = 0;
|
||||
while(cur_timesec_bak != 0) {
|
||||
cur_timesec_bak /= 10;
|
||||
++cur_timesec_len;
|
||||
}
|
||||
while (cur_timesec_bak != 0) {
|
||||
cur_timesec_bak /= 10;
|
||||
++cur_timesec_len;
|
||||
}
|
||||
|
||||
int username_len = strlen(options->client_id) + strlen(QCLOUD_IOT_DEVICE_SDK_APPID) + MAX_CONN_ID_LEN + cur_timesec_len + 4;
|
||||
options->username = (char*)HAL_Malloc(username_len);
|
||||
get_next_conn_id(options->conn_id);
|
||||
HAL_Snprintf(options->username, username_len, "%s;%s;%s;%ld", options->client_id, QCLOUD_IOT_DEVICE_SDK_APPID, options->conn_id, cur_timesec);
|
||||
HAL_Snprintf(options->username, username_len, "%s;%s;%s;%ld", options->client_id, QCLOUD_IOT_DEVICE_SDK_APPID, options->conn_id, cur_timesec);
|
||||
|
||||
#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)
|
||||
if (options->device_secret != NULL && options->username != NULL) {
|
||||
char sign[41] = {0};
|
||||
utils_hmac_sha1(options->username, strlen(options->username), sign, options->device_secret, options->device_secret_len);
|
||||
options->password = (char*) HAL_Malloc (51);
|
||||
if (options->password == NULL) IOT_FUNC_EXIT_RC(QCLOUD_ERR_INVAL);
|
||||
HAL_Snprintf(options->password, 51, "%s;hmacsha1", sign);
|
||||
}
|
||||
if (options->device_secret != NULL && options->username != NULL) {
|
||||
char sign[41] = {0};
|
||||
utils_hmac_sha1(options->username, strlen(options->username), sign, options->device_secret, options->device_secret_len);
|
||||
options->password = (char*) HAL_Malloc (51);
|
||||
if (options->password == NULL) IOT_FUNC_EXIT_RC(QCLOUD_ERR_INVAL);
|
||||
HAL_Snprintf(options->password, 51, "%s;hmacsha1", sign);
|
||||
}
|
||||
#endif
|
||||
|
||||
rem_len = _get_packet_connect_rem_len(options);
|
||||
@@ -172,7 +180,7 @@ static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTCon
|
||||
flags |= (options->username != NULL) ? MQTT_CONNECT_FLAG_USERNAME : 0;
|
||||
|
||||
#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)
|
||||
flags |= MQTT_CONNECT_FLAG_PASSWORD;
|
||||
flags |= MQTT_CONNECT_FLAG_PASSWORD;
|
||||
#endif
|
||||
|
||||
mqtt_write_char(&ptr, flags);
|
||||
@@ -190,9 +198,9 @@ static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTCon
|
||||
}
|
||||
|
||||
if ((flags & MQTT_CONNECT_FLAG_PASSWORD) && options->password != NULL) {
|
||||
mqtt_write_utf8_string(&ptr, options->password);
|
||||
HAL_Free(options->password);
|
||||
options->password = NULL;
|
||||
mqtt_write_utf8_string(&ptr, options->password);
|
||||
HAL_Free(options->password);
|
||||
options->password = NULL;
|
||||
}
|
||||
|
||||
*serialized_len = (uint32_t) (ptr - buf);
|
||||
@@ -208,7 +216,8 @@ static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTCon
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return int indicating function execution status
|
||||
*/
|
||||
static int _deserialize_connack_packet(uint8_t *sessionPresent, int *connack_rc, unsigned char *buf, size_t buflen) {
|
||||
static int _deserialize_connack_packet(uint8_t *sessionPresent, int *connack_rc, unsigned char *buf, size_t buflen)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(sessionPresent, QCLOUD_ERR_INVAL);
|
||||
@@ -229,7 +238,7 @@ static int _deserialize_connack_packet(uint8_t *sessionPresent, int *connack_rc,
|
||||
}
|
||||
|
||||
header = mqtt_read_char(&curdata);
|
||||
type = (header&MQTT_HEADER_TYPE_MASK)>>MQTT_HEADER_TYPE_SHIFT;
|
||||
type = (header & MQTT_HEADER_TYPE_MASK) >> MQTT_HEADER_TYPE_SHIFT;
|
||||
if (CONNACK != type) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
@@ -284,7 +293,8 @@ static int _deserialize_connack_packet(uint8_t *sessionPresent, int *connack_rc,
|
||||
* @param options
|
||||
* @return
|
||||
*/
|
||||
static int _mqtt_connect(Qcloud_IoT_Client *pClient, MQTTConnectParams *options) {
|
||||
static int _mqtt_connect(Qcloud_IoT_Client *pClient, MQTTConnectParams *options)
|
||||
{
|
||||
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
@@ -294,8 +304,7 @@ static int _mqtt_connect(Qcloud_IoT_Client *pClient, MQTTConnectParams *options)
|
||||
uint32_t len = 0;
|
||||
|
||||
InitTimer(&connect_timer);
|
||||
// if we have log in console, we need a longer timeout
|
||||
countdown_ms(&connect_timer, pClient->command_timeout_ms + 10 * 1000);
|
||||
countdown_ms(&connect_timer, pClient->command_timeout_ms);
|
||||
|
||||
if (NULL != options) {
|
||||
_copy_connect_params(&(pClient->options), options);
|
||||
@@ -311,14 +320,14 @@ static int _mqtt_connect(Qcloud_IoT_Client *pClient, MQTTConnectParams *options)
|
||||
// serialize CONNECT packet
|
||||
rc = _serialize_connect_packet(pClient->write_buf, pClient->write_buf_size, &(pClient->options), &len);
|
||||
if (QCLOUD_RET_SUCCESS != rc || 0 == len) {
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
// send CONNECT packet
|
||||
rc = send_mqtt_packet(pClient, len, &connect_timer);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
@@ -349,7 +358,8 @@ static int _mqtt_connect(Qcloud_IoT_Client *pClient, MQTTConnectParams *options)
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_connect(Qcloud_IoT_Client *pClient, MQTTConnectParams *pParams) {
|
||||
int qcloud_iot_mqtt_connect(Qcloud_IoT_Client *pClient, MQTTConnectParams *pParams)
|
||||
{
|
||||
|
||||
IOT_FUNC_ENTRY;
|
||||
int rc;
|
||||
@@ -371,7 +381,8 @@ int qcloud_iot_mqtt_connect(Qcloud_IoT_Client *pClient, MQTTConnectParams *pPara
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_attempt_reconnect(Qcloud_IoT_Client *pClient) {
|
||||
int qcloud_iot_mqtt_attempt_reconnect(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
@@ -398,7 +409,8 @@ int qcloud_iot_mqtt_attempt_reconnect(Qcloud_IoT_Client *pClient) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_MQTT_RECONNECTED);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_disconnect(Qcloud_IoT_Client *pClient) {
|
||||
int qcloud_iot_mqtt_disconnect(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
|
@@ -19,14 +19,15 @@ extern "C" {
|
||||
|
||||
#include "mqtt_client_net.h"
|
||||
|
||||
//TODO: how to implement
|
||||
//TODO: how to implement
|
||||
/**
|
||||
* @brief Check if TLS connection is valid
|
||||
*
|
||||
* @param pNetwork
|
||||
* @return
|
||||
*/
|
||||
int qcloud_iot_mqtt_tls_is_connected(Network *pNetwork) {
|
||||
int qcloud_iot_mqtt_tls_is_connected(Network *pNetwork)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -37,13 +38,14 @@ int qcloud_iot_mqtt_tls_is_connected(Network *pNetwork) {
|
||||
* @param pConnectParams
|
||||
* @return
|
||||
*/
|
||||
int qcloud_iot_mqtt_network_init(Network *pNetwork) {
|
||||
int qcloud_iot_mqtt_network_init(Network *pNetwork)
|
||||
{
|
||||
int rc;
|
||||
|
||||
/* first choice: TLS */
|
||||
pNetwork->type = NETWORK_TLS;
|
||||
|
||||
#ifdef AUTH_WITH_NOTLS
|
||||
#ifdef AUTH_WITH_NOTLS
|
||||
pNetwork->type = NETWORK_TCP;
|
||||
#endif
|
||||
|
||||
|
@@ -31,19 +31,20 @@ extern "C" {
|
||||
* @param enddata pointer to the end of the data: do not read beyond
|
||||
* @return SUCCESS if successful, FAILURE if not
|
||||
*/
|
||||
static int _read_string_with_len(char **string, uint16_t *stringLen, unsigned char **pptr, unsigned char *enddata) {
|
||||
static int _read_string_with_len(char **string, uint16_t *stringLen, unsigned char **pptr, unsigned char *enddata)
|
||||
{
|
||||
int rc = QCLOUD_ERR_FAILURE;
|
||||
|
||||
/* the first two bytes are the length of the string */
|
||||
/* enough length to read the integer? */
|
||||
if (enddata - (*pptr) > 1) {
|
||||
*stringLen = mqtt_read_uint16_t(pptr); /* increments pptr to point past length */
|
||||
|
||||
if(*stringLen > QCLOUD_IOT_MQTT_RX_BUF_LEN){
|
||||
Log_e("stringLen exceed QCLOUD_IOT_MQTT_RX_BUF_LEN");
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
if (*stringLen > QCLOUD_IOT_MQTT_RX_BUF_LEN) {
|
||||
Log_e("stringLen exceed QCLOUD_IOT_MQTT_RX_BUF_LEN");
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
|
||||
if (&(*pptr)[*stringLen] <= enddata) {
|
||||
*string = (char *) *pptr;
|
||||
*pptr += *stringLen;
|
||||
@@ -57,11 +58,12 @@ static int _read_string_with_len(char **string, uint16_t *stringLen, unsigned ch
|
||||
/**
|
||||
* Determines the length of the MQTT publish packet that would be produced using the supplied parameters
|
||||
* @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
|
||||
* @param topicName the topic name to be used in the publish
|
||||
* @param topicName the topic name to be used in the publish
|
||||
* @param payload_len the length of the payload to be sent
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
static uint32_t _get_publish_packet_len(uint8_t qos, char *topicName, size_t payload_len) {
|
||||
static uint32_t _get_publish_packet_len(uint8_t qos, char *topicName, size_t payload_len)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
||||
len += 2 + strlen(topicName) + payload_len;
|
||||
@@ -138,7 +140,7 @@ static int _mask_push_pubInfo_to(Qcloud_IoT_Client *c, int len, unsigned short m
|
||||
* @return error code. 1 is success
|
||||
*/
|
||||
int deserialize_publish_packet(uint8_t *dup, QoS *qos, uint8_t *retained, uint16_t *packet_id, char **topicName,
|
||||
uint16_t *topicNameLen,unsigned char **payload, size_t *payload_len, unsigned char *buf, size_t buf_len)
|
||||
uint16_t *topicNameLen, unsigned char **payload, size_t *payload_len, unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
@@ -165,11 +167,11 @@ int deserialize_publish_packet(uint8_t *dup, QoS *qos, uint8_t *retained, uint16
|
||||
}
|
||||
|
||||
header = mqtt_read_char(&curdata);
|
||||
type = (header&MQTT_HEADER_TYPE_MASK)>>MQTT_HEADER_TYPE_SHIFT;
|
||||
*dup = (header&MQTT_HEADER_DUP_MASK)>>MQTT_HEADER_DUP_SHIFT;
|
||||
*qos = (QoS)((header&MQTT_HEADER_QOS_MASK)>>MQTT_HEADER_QOS_SHIFT);
|
||||
*retained = header&MQTT_HEADER_RETAIN_MASK;
|
||||
|
||||
type = (header & MQTT_HEADER_TYPE_MASK) >> MQTT_HEADER_TYPE_SHIFT;
|
||||
*dup = (header & MQTT_HEADER_DUP_MASK) >> MQTT_HEADER_DUP_SHIFT;
|
||||
*qos = (QoS)((header & MQTT_HEADER_QOS_MASK) >> MQTT_HEADER_QOS_SHIFT);
|
||||
*retained = header & MQTT_HEADER_RETAIN_MASK;
|
||||
|
||||
if (PUBLISH != type) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
@@ -208,7 +210,8 @@ int deserialize_publish_packet(uint8_t *dup, QoS *qos, uint8_t *retained, uint16
|
||||
*/
|
||||
int serialize_pub_ack_packet(unsigned char *buf, size_t buf_len, MessageTypes packet_type, uint8_t dup,
|
||||
uint16_t packet_id,
|
||||
uint32_t *serialized_len) {
|
||||
uint32_t *serialized_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
|
||||
POINTER_SANITY_CHECK(serialized_len, QCLOUD_ERR_INVAL);
|
||||
@@ -253,7 +256,8 @@ int serialize_pub_ack_packet(unsigned char *buf, size_t buf_len, MessageTypes pa
|
||||
static int _serialize_publish_packet(unsigned char *buf, size_t buf_len, uint8_t dup, QoS qos, uint8_t retained,
|
||||
uint16_t packet_id,
|
||||
char *topicName, unsigned char *payload, size_t payload_len,
|
||||
uint32_t *serialized_len) {
|
||||
uint32_t *serialized_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
|
||||
POINTER_SANITY_CHECK(serialized_len, QCLOUD_ERR_INVAL);
|
||||
@@ -292,7 +296,8 @@ static int _serialize_publish_packet(unsigned char *buf, size_t buf_len, uint8_t
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_publish(Qcloud_IoT_Client *pClient, char *topicName, PublishParams *pParams) {
|
||||
int qcloud_iot_mqtt_publish(Qcloud_IoT_Client *pClient, char *topicName, PublishParams *pParams)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
|
||||
@@ -304,7 +309,7 @@ int qcloud_iot_mqtt_publish(Qcloud_IoT_Client *pClient, char *topicName, Publish
|
||||
int rc;
|
||||
|
||||
ListNode *node = NULL;
|
||||
|
||||
|
||||
size_t topicLen = strlen(topicName);
|
||||
if (topicLen > MAX_SIZE_OF_CLOUD_TOPIC) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_MAX_TOPIC_LENGTH);
|
||||
@@ -326,25 +331,22 @@ int qcloud_iot_mqtt_publish(Qcloud_IoT_Client *pClient, char *topicName, Publish
|
||||
if (pParams->qos == QOS1) {
|
||||
pParams->id = get_next_packet_id(pClient);
|
||||
if (IOT_Log_Get_Level() <= eLOG_DEBUG) {
|
||||
Log_d("publish topic seq=%d|topicName=%s|payload=%s", pParams->id, topicName, (char *)pParams->payload);
|
||||
Log_d("publish topic seq=%d|topicName=%s|payload=%s", pParams->id, topicName, (char *)pParams->payload);
|
||||
} else {
|
||||
Log_i("publish topic seq=%d|topicName=%s", pParams->id, topicName);
|
||||
}
|
||||
else {
|
||||
Log_i("publish topic seq=%d|topicName=%s", pParams->id, topicName);
|
||||
} else {
|
||||
if (IOT_Log_Get_Level() <= eLOG_DEBUG) {
|
||||
Log_d("publish packetID=%d|topicName=%s|payload=%s", pParams->id, topicName, (char *)pParams->payload);
|
||||
} else {
|
||||
Log_i("publish packetID=%d|topicName=%s", pParams->id, topicName);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (IOT_Log_Get_Level() <= eLOG_DEBUG) {
|
||||
Log_d("publish packetID=%d|topicName=%s|payload=%s", pParams->id, topicName, (char *)pParams->payload);
|
||||
}
|
||||
else {
|
||||
Log_i("publish packetID=%d|topicName=%s", pParams->id, topicName);
|
||||
}
|
||||
}
|
||||
|
||||
rc = _serialize_publish_packet(pClient->write_buf, pClient->write_buf_size, 0, pParams->qos, pParams->retained, pParams->id,
|
||||
topicName, (unsigned char *) pParams->payload, pParams->payload_len, &len);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
@@ -359,20 +361,20 @@ int qcloud_iot_mqtt_publish(Qcloud_IoT_Client *pClient, char *topicName, Publish
|
||||
|
||||
/* send the publish packet */
|
||||
rc = send_mqtt_packet(pClient, len, &timer);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
if (pParams->qos > QOS0) {
|
||||
HAL_MutexLock(pClient->lock_list_pub);
|
||||
list_remove(pClient->list_pub_wait_ack, node);
|
||||
HAL_MutexUnlock(pClient->lock_list_pub);
|
||||
}
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
if (pParams->qos > QOS0) {
|
||||
HAL_MutexLock(pClient->lock_list_pub);
|
||||
list_remove(pClient->list_pub_wait_ack, node);
|
||||
HAL_MutexUnlock(pClient->lock_list_pub);
|
||||
}
|
||||
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
|
||||
IOT_FUNC_EXIT_RC(pParams->id);
|
||||
IOT_FUNC_EXIT_RC(pParams->id);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@@ -28,7 +28,8 @@ extern "C" {
|
||||
* @param topicFilters the array of topic filter strings to be used in the publish
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
static uint32_t _get_subscribe_packet_rem_len(uint32_t count, char **topicFilters) {
|
||||
static uint32_t _get_subscribe_packet_rem_len(uint32_t count, char **topicFilters)
|
||||
{
|
||||
size_t i;
|
||||
size_t len = 2; /* packetid */
|
||||
|
||||
@@ -51,7 +52,8 @@ static uint32_t _get_subscribe_packet_rem_len(uint32_t count, char **topicFilter
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
static int _serialize_subscribe_packet(unsigned char *buf, size_t buf_len, uint8_t dup, uint16_t packet_id, uint32_t count,
|
||||
char **topicFilters, QoS *requestedQoSs, uint32_t *serialized_len) {
|
||||
char **topicFilters, QoS *requestedQoSs, uint32_t *serialized_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
|
||||
@@ -90,7 +92,8 @@ static int _serialize_subscribe_packet(unsigned char *buf, size_t buf_len, uint8
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, SubscribeParams *pParams) {
|
||||
int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, SubscribeParams *pParams)
|
||||
{
|
||||
|
||||
IOT_FUNC_ENTRY;
|
||||
int rc;
|
||||
@@ -105,7 +108,7 @@ int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, Sub
|
||||
uint16_t packet_id = 0;
|
||||
|
||||
ListNode *node = NULL;
|
||||
|
||||
|
||||
size_t topicLen = strlen(topicFilter);
|
||||
if (topicLen > MAX_SIZE_OF_CLOUD_TOPIC) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_MAX_TOPIC_LENGTH);
|
||||
@@ -115,7 +118,7 @@ int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, Sub
|
||||
Log_e("QoS2 is not supported currently");
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_MQTT_QOS_NOT_SUPPORT);
|
||||
}
|
||||
|
||||
|
||||
if (!get_client_conn_state(pClient)) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_MQTT_NO_CONN)
|
||||
}
|
||||
@@ -126,10 +129,10 @@ int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, Sub
|
||||
Log_e("malloc failed");
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
strcpy(topic_filter_stored, topicFilter);
|
||||
topic_filter_stored[topicLen] = 0;
|
||||
|
||||
|
||||
InitTimer(&timer);
|
||||
countdown_ms(&timer, pClient->command_timeout_ms);
|
||||
|
||||
@@ -140,8 +143,8 @@ int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, Sub
|
||||
rc = _serialize_subscribe_packet(pClient->write_buf, pClient->write_buf_size, 0, packet_id, 1, &topic_filter_stored,
|
||||
&pParams->qos, &len);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_Free(topic_filter_stored);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_Free(topic_filter_stored);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
@@ -160,7 +163,7 @@ int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, Sub
|
||||
HAL_Free(topic_filter_stored);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
|
||||
// send SUBSCRIBE packet
|
||||
rc = send_mqtt_packet(pClient, len, &timer);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
@@ -168,8 +171,8 @@ int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, Sub
|
||||
list_remove(pClient->list_sub_wait_ack, node);
|
||||
HAL_MutexUnlock(pClient->lock_list_sub);
|
||||
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_Free(topic_filter_stored);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_Free(topic_filter_stored);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
@@ -178,7 +181,8 @@ int qcloud_iot_mqtt_subscribe(Qcloud_IoT_Client *pClient, char *topicFilter, Sub
|
||||
IOT_FUNC_EXIT_RC(packet_id);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_resubscribe(Qcloud_IoT_Client *pClient) {
|
||||
int qcloud_iot_mqtt_resubscribe(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
int rc;
|
||||
|
||||
@@ -208,7 +212,7 @@ int qcloud_iot_mqtt_resubscribe(Qcloud_IoT_Client *pClient) {
|
||||
|
||||
rc = qcloud_iot_mqtt_subscribe(pClient, topic, &temp_param);
|
||||
if (rc < 0) {
|
||||
Log_e("resubscribe failed %d, topic: %s", rc, topic);
|
||||
Log_e("resubscribe failed %d, topic: %s", rc, topic);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
}
|
||||
|
@@ -28,7 +28,8 @@ extern "C" {
|
||||
* @param topicFilters the array of topic filter strings to be used in the publish
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
static uint32_t _get_unsubscribe_packet_rem_len(uint32_t count, char **topicFilters) {
|
||||
static uint32_t _get_unsubscribe_packet_rem_len(uint32_t count, char **topicFilters)
|
||||
{
|
||||
size_t i;
|
||||
size_t len = 2; /* packetid */
|
||||
|
||||
@@ -51,9 +52,10 @@ static uint32_t _get_unsubscribe_packet_rem_len(uint32_t count, char **topicFilt
|
||||
* @return int indicating function execution status
|
||||
*/
|
||||
static int _serialize_unsubscribe_packet(unsigned char *buf, size_t buf_len,
|
||||
uint8_t dup, uint16_t packet_id,
|
||||
uint32_t count, char **topicFilters,
|
||||
uint32_t *serialized_len) {
|
||||
uint8_t dup, uint16_t packet_id,
|
||||
uint32_t count, char **topicFilters,
|
||||
uint32_t *serialized_len)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
|
||||
@@ -89,7 +91,8 @@ static int _serialize_unsubscribe_packet(unsigned char *buf, size_t buf_len,
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
|
||||
}
|
||||
|
||||
int qcloud_iot_mqtt_unsubscribe(Qcloud_IoT_Client *pClient, char *topicFilter) {
|
||||
int qcloud_iot_mqtt_unsubscribe(Qcloud_IoT_Client *pClient, char *topicFilter)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
int rc;
|
||||
|
||||
@@ -108,12 +111,12 @@ int qcloud_iot_mqtt_unsubscribe(Qcloud_IoT_Client *pClient, char *topicFilter) {
|
||||
if (topicLen > MAX_SIZE_OF_CLOUD_TOPIC) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_MAX_TOPIC_LENGTH);
|
||||
}
|
||||
|
||||
|
||||
/* Remove from message handler array */
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
|
||||
if ((pClient->sub_handles[i].topic_filter != NULL && !strcmp(pClient->sub_handles[i].topic_filter, topicFilter))
|
||||
|| strstr(topicFilter,"/#") != NULL || strstr(topicFilter,"/+") != NULL) {
|
||||
|| strstr(topicFilter, "/#") != NULL || strstr(topicFilter, "/+") != NULL) {
|
||||
/* notify this event to topic subscriber */
|
||||
if (NULL != pClient->sub_handles[i].sub_event_handler)
|
||||
pClient->sub_handles[i].sub_event_handler(
|
||||
@@ -156,8 +159,8 @@ int qcloud_iot_mqtt_unsubscribe(Qcloud_IoT_Client *pClient, char *topicFilter) {
|
||||
rc = _serialize_unsubscribe_packet(pClient->write_buf, pClient->write_buf_size, 0, packet_id, 1, &topic_filter_stored,
|
||||
&len);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_Free(topic_filter_stored);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_Free(topic_filter_stored);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
@@ -182,8 +185,8 @@ int qcloud_iot_mqtt_unsubscribe(Qcloud_IoT_Client *pClient, char *topicFilter) {
|
||||
list_remove(pClient->list_sub_wait_ack, node);
|
||||
HAL_MutexUnlock(pClient->lock_list_sub);
|
||||
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_Free(topic_filter_stored);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_Free(topic_filter_stored);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
|
@@ -27,7 +27,7 @@ static uint32_t _get_random_interval(void)
|
||||
{
|
||||
srand((unsigned)HAL_GetTimeMs());
|
||||
/* range: 1000 - 2000 ms, in 10ms unit */
|
||||
return (rand() % 100 + 100)*10;
|
||||
return (rand() % 100 + 100) * 10;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ static void _iot_disconnect_callback(Qcloud_IoT_Client *pClient)
|
||||
}
|
||||
}
|
||||
|
||||
static void _reconnect_callback(Qcloud_IoT_Client* pClient)
|
||||
static void _reconnect_callback(Qcloud_IoT_Client* pClient)
|
||||
{
|
||||
if (NULL != pClient->event_handle.h_fp) {
|
||||
MQTTEventMsg msg;
|
||||
@@ -60,10 +60,11 @@ static void _reconnect_callback(Qcloud_IoT_Client* pClient)
|
||||
* @param pClient
|
||||
* @return
|
||||
*/
|
||||
static int _handle_disconnect(Qcloud_IoT_Client *pClient) {
|
||||
static int _handle_disconnect(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
int rc;
|
||||
|
||||
|
||||
if (0 == get_client_conn_state(pClient)) {
|
||||
IOT_FUNC_EXIT_RC(QCLOUD_ERR_MQTT_NO_CONN);
|
||||
}
|
||||
@@ -76,7 +77,7 @@ static int _handle_disconnect(Qcloud_IoT_Client *pClient) {
|
||||
}
|
||||
|
||||
Log_e("disconnect MQTT for some reasons..");
|
||||
|
||||
|
||||
_iot_disconnect_callback(pClient);
|
||||
|
||||
// exceptional disconnection
|
||||
@@ -90,7 +91,8 @@ static int _handle_disconnect(Qcloud_IoT_Client *pClient) {
|
||||
* @param pClient
|
||||
* @return
|
||||
*/
|
||||
static int _handle_reconnect(Qcloud_IoT_Client *pClient) {
|
||||
static int _handle_reconnect(Qcloud_IoT_Client *pClient)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
int8_t isPhysicalLayerConnected = 1;
|
||||
@@ -102,7 +104,7 @@ static int _handle_reconnect(Qcloud_IoT_Client *pClient) {
|
||||
}
|
||||
|
||||
if (NULL != pClient->network_stack.is_connected) {
|
||||
isPhysicalLayerConnected = (int8_t) pClient->network_stack.is_connected(&(pClient->network_stack)); // always return 1
|
||||
isPhysicalLayerConnected = (int8_t) pClient->network_stack.is_connected(&(pClient->network_stack)); // always return 1
|
||||
}
|
||||
|
||||
if (isPhysicalLayerConnected) {
|
||||
@@ -112,15 +114,14 @@ static int _handle_reconnect(Qcloud_IoT_Client *pClient) {
|
||||
_reconnect_callback(pClient);
|
||||
#ifdef LOG_UPLOAD
|
||||
if (is_log_uploader_init()) {
|
||||
int log_level;
|
||||
int log_level;
|
||||
if (qcloud_get_log_level(pClient, &log_level) < 0) {
|
||||
Log_e("client get log topic failed: %d", rc);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Log_e("attempt to reconnect failed, errCode: %d", rc);
|
||||
rc = QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT;
|
||||
}
|
||||
@@ -167,7 +168,7 @@ static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient)
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
|
||||
/* there is no ping outstanding - send one */
|
||||
/* there is no ping outstanding - send one */
|
||||
HAL_MutexLock(pClient->lock_write_buf);
|
||||
rc = serialize_packet_with_zero_payload(pClient->write_buf, pClient->write_buf_size, PINGREQ, &serialized_len);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
@@ -177,25 +178,25 @@ static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient)
|
||||
|
||||
/* send the ping packet */
|
||||
int i = 0;
|
||||
InitTimer(&timer);
|
||||
InitTimer(&timer);
|
||||
do {
|
||||
countdown_ms(&timer, pClient->command_timeout_ms);
|
||||
rc = send_mqtt_packet(pClient, serialized_len, &timer);
|
||||
} while (QCLOUD_RET_SUCCESS != rc && (i++ < 3));
|
||||
|
||||
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
//If sending a PING fails, propably the connection is not OK and we decide to disconnect and begin reconnection attempts
|
||||
Log_e("Fail to send PING request. Something wrong with the connection.");
|
||||
rc = _handle_disconnect(pClient);
|
||||
IOT_FUNC_EXIT_RC(rc);
|
||||
}
|
||||
HAL_MutexUnlock(pClient->lock_write_buf);
|
||||
|
||||
|
||||
HAL_MutexLock(pClient->lock_generic);
|
||||
pClient->is_ping_outstanding++;
|
||||
/* start a timer to wait for PINGRESP from server */
|
||||
countdown(&pClient->ping_timer, Min(5, pClient->options.keep_alive_interval/2));
|
||||
countdown(&pClient->ping_timer, Min(5, pClient->options.keep_alive_interval / 2));
|
||||
HAL_MutexUnlock(pClient->lock_generic);
|
||||
Log_d("PING request %u has been sent...", pClient->is_ping_outstanding);
|
||||
|
||||
@@ -210,7 +211,8 @@ static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient)
|
||||
*
|
||||
* @return QCLOUD_RET_SUCCESS when success, QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT when try reconnecing, or err code for failure
|
||||
*/
|
||||
int qcloud_iot_mqtt_yield(Qcloud_IoT_Client *pClient, uint32_t timeout_ms) {
|
||||
int qcloud_iot_mqtt_yield(Qcloud_IoT_Client *pClient, uint32_t timeout_ms)
|
||||
{
|
||||
IOT_FUNC_ENTRY;
|
||||
|
||||
int rc = QCLOUD_RET_SUCCESS;
|
||||
@@ -234,8 +236,8 @@ int qcloud_iot_mqtt_yield(Qcloud_IoT_Client *pClient, uint32_t timeout_ms) {
|
||||
countdown_ms(&timer, timeout_ms);
|
||||
|
||||
// 3. main loop for packet reading/handling and keep alive maintainance
|
||||
while (!expired(&timer)) {
|
||||
|
||||
while (!expired(&timer)) {
|
||||
|
||||
if (!get_client_conn_state(pClient)) {
|
||||
if (pClient->current_reconnect_wait_interval > MAX_RECONNECT_WAIT_INTERVAL) {
|
||||
rc = QCLOUD_ERR_MQTT_RECONNECT_TIMEOUT;
|
||||
@@ -244,7 +246,7 @@ int qcloud_iot_mqtt_yield(Qcloud_IoT_Client *pClient, uint32_t timeout_ms) {
|
||||
rc = _handle_reconnect(pClient);
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
rc = cycle_for_read(pClient, &timer, &packet_type, QOS0);
|
||||
|
||||
@@ -256,11 +258,10 @@ int qcloud_iot_mqtt_yield(Qcloud_IoT_Client *pClient, uint32_t timeout_ms) {
|
||||
qcloud_iot_mqtt_sub_info_proc(pClient);
|
||||
|
||||
rc = _mqtt_keep_alive(pClient);
|
||||
}
|
||||
else if (rc == QCLOUD_ERR_SSL_READ_TIMEOUT || rc == QCLOUD_ERR_SSL_READ ||
|
||||
rc == QCLOUD_ERR_TCP_PEER_SHUTDOWN || rc == QCLOUD_ERR_TCP_READ_FAIL){
|
||||
Log_e("network read failed, rc: %d. MQTT Disconnect.", rc);
|
||||
rc = _handle_disconnect(pClient);
|
||||
} else if (rc == QCLOUD_ERR_SSL_READ_TIMEOUT || rc == QCLOUD_ERR_SSL_READ ||
|
||||
rc == QCLOUD_ERR_TCP_PEER_SHUTDOWN || rc == QCLOUD_ERR_TCP_READ_FAIL) {
|
||||
Log_e("network read failed, rc: %d. MQTT Disconnect.", rc);
|
||||
rc = _handle_disconnect(pClient);
|
||||
}
|
||||
|
||||
if (rc == QCLOUD_ERR_MQTT_NO_CONN) {
|
||||
@@ -453,10 +454,10 @@ int qcloud_iot_mqtt_sub_info_proc(Qcloud_IoT_Client *pClient)
|
||||
|
||||
/* notify this event to topic subscriber */
|
||||
if (NULL != sub_info->handler.sub_event_handler)
|
||||
sub_info->handler.sub_event_handler(pClient,
|
||||
MQTT_EVENT_SUBCRIBE_TIMEOUT, sub_info->handler.handler_user_data);
|
||||
|
||||
} else {
|
||||
sub_info->handler.sub_event_handler(pClient,
|
||||
MQTT_EVENT_SUBCRIBE_TIMEOUT, sub_info->handler.handler_user_data);
|
||||
|
||||
} else {
|
||||
/* unsubscribe timeout */
|
||||
msg.event_type = MQTT_EVENT_UNSUBCRIBE_TIMEOUT;
|
||||
msg.msg = (void *)(uintptr_t)packet_id;
|
||||
@@ -467,7 +468,7 @@ int qcloud_iot_mqtt_sub_info_proc(Qcloud_IoT_Client *pClient)
|
||||
|
||||
if (NULL != sub_info->handler.topic_filter)
|
||||
HAL_Free((void *)(sub_info->handler.topic_filter));
|
||||
|
||||
|
||||
temp_node = node;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user