Skip to content

Commit

Permalink
fix bug of parsing fota version response
Browse files Browse the repository at this point in the history
  • Loading branch information
nqd committed May 17, 2015
1 parent fc85704 commit 2ae6f3e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
35 changes: 19 additions & 16 deletions apps/fota/fota-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ jsoneq(const char *json, jsmntok_t *tok, const char *s) {
return -1;
}
LOCAL int8_t ICACHE_FLASH_ATTR
json_get_value(const char *json, jsmntok_t *tok, const char *key, char *value) {
json_get_value(const char *json, jsmntok_t *tok, const char *key, char **value) {
if (jsoneq(json, tok, key) == 0 && tok[1].type == JSMN_STRING) {
uint32_t len = tok[1].end-tok[1].start;
value = (char*) os_zalloc(len+1);
os_strncpy(value, (char*)(json+ tok[1].start), len);
*value = (char*) os_zalloc(len+1);
os_strncpy(*value, (char*)(json+ tok[1].start), len);
// INFO("%s: %s\n", key, *value);
return 0;
}
return -1;
}

int8_t ICACHE_FLASH_ATTR
parse_fota(const char *json, uint32_t len, char *version, char *host, char *url, char *protocol)
parse_fota(const char *json, uint32_t len, char **version, char **host, char **url, char **protocol)
{
int count = 0;
int i;
Expand All @@ -42,10 +43,10 @@ parse_fota(const char *json, uint32_t len, char *version, char *host, char *url,
jsmntok_t tok[128]; /* We expect no more than 128 tokens */

// prepare pointer
version = NULL;
host = NULL;
url = NULL;
protocol = NULL;
*version = NULL;
*host = NULL;
*url = NULL;
*protocol = NULL;

jsmn_init(&par);
r = jsmn_parse(&par, json, len, tok, sizeof(tok)/sizeof(tok[0]));
Expand All @@ -67,14 +68,16 @@ parse_fota(const char *json, uint32_t len, char *version, char *host, char *url,

int j;
int z=0;
char value[100];
for (j = 0; j < tok[i+1].size; j++) {
if (json_get_value(json, &tok[i+z+2], "version", version) ==0)
if (json_get_value(json, &tok[i+z+2], "version", version) == 0) {
count += 1;
else if (json_get_value(json, &tok[i+z+2], "host", version) == 0)
}
else if (json_get_value(json, &tok[i+z+2], "host", host) == 0)
count += 1;
else if (json_get_value(json, &tok[i+z+2], "url", version) == 0)
else if (json_get_value(json, &tok[i+z+2], "url", url) == 0)
count += 1;
else if (json_get_value(json, &tok[i+z+2], "protocol", version) == 0)
else if (json_get_value(json, &tok[i+z+2], "protocol", protocol) == 0)
count += 1;

z += 2; // we expect key: value under last object
Expand All @@ -83,10 +86,10 @@ parse_fota(const char *json, uint32_t len, char *version, char *host, char *url,
}
// clean up malloc when parsing failed
if (count < 4) {
if (version) os_free(version);
if (host) os_free(host);
if (url) os_free(url);
if (protocol) os_free(protocol);
if (*version!=NULL) os_free(*version);
if (*host!=NULL) os_free(*host);
if (*url!=NULL) os_free(*url);
if (*protocol!=NULL) os_free(*protocol);
return FAILED;
}
else
Expand Down
2 changes: 1 addition & 1 deletion apps/fota/fota-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@


int8_t convert_version(const char *version_str, uint32_t len, uint32_t *version_bin);
int8_t parse_fota(const char *json, uint32_t len, char *version, char *host, char *url, char *protocol);
int8_t parse_fota(const char *json, uint32_t len, char **version, char **host, char **url, char **protocol);

#endif
38 changes: 23 additions & 15 deletions apps/fota/fota.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ get_version_recv(void *arg, char *pusrdata, unsigned short len)
*n_version,
*n_protocol;

if (parse_fota(body, bodylen, n_version, n_host, n_url, n_protocol) < 0) {
if (parse_fota(body, bodylen, &n_version, &n_host, &n_url, &n_protocol) < 0) {
INFO("Invalid response\n");
goto CLEAN_MEM;
}
INFO("Version %s\n", n_version);
INFO("Host %s\n", n_host);
INFO("URL %s\n", n_url);
INFO("Protocol %s\n", n_protocol);

/* then, we have valide JSON response */
// disable data receiving timeout handing
// and close connection
os_timer_disarm(&(fota_client->request_timeout));
os_timer_disarm(&fota_client->request_timeout);
clear_tcp_of_espconn(fota_client->conn);

uint32_t version;
Expand All @@ -111,6 +115,8 @@ get_version_recv(void *arg, char *pusrdata, unsigned short len)
goto CLEAN_MEM;
}

INFO("Prepare to get firmware\n");

if (os_strncmp(n_protocol, "https", os_strlen("https")))
fota_client->fw_server.secure = 1;
else
Expand All @@ -126,10 +132,10 @@ get_version_recv(void *arg, char *pusrdata, unsigned short len)
return;

CLEAN_MEM:
if (n_host) os_free(n_host);
if (n_url) os_free(n_url);
if (n_version) os_free(n_version);
if (n_protocol) os_free(n_protocol);
if (n_host!=NULL) os_free(n_host);
if (n_url!=NULL) os_free(n_url);
if (n_version!=NULL) os_free(n_version);
if (n_protocol!=NULL) os_free(n_protocol);
}

/**
Expand Down Expand Up @@ -164,7 +170,7 @@ get_version_sent_cb(void *arg)
os_timer_disarm(&fota_client->request_timeout);
os_timer_setfn(&fota_client->request_timeout, (os_timer_func_t *)get_version_timeout, pespconn);
os_timer_arm(&fota_client->request_timeout, 5000, 0);
INFO("get version sent cb\n");
INFO("FOTA Client: sent request\n");
}

/**
Expand All @@ -175,7 +181,7 @@ get_version_sent_cb(void *arg)
LOCAL void ICACHE_FLASH_ATTR
get_version_disconnect_cb(void *arg)
{
INFO("get version disconnect tcp\n");
INFO("FOTA Client: disconnect\n");
clear_tcp_of_espconn((struct espconn *)arg);
}

Expand All @@ -195,10 +201,10 @@ get_version_connect_cb(void *arg)

uint8_t user_bin[12] = {0};
if(system_upgrade_userbin_check() == UPGRADE_FW_BIN1) {
os_memcpy(user_bin, "user2.bin", 10);
os_memcpy(user_bin, "user1.bin", 10);
}
else if(system_upgrade_userbin_check() == UPGRADE_FW_BIN2) {
os_memcpy(user_bin, "user1.bin", 10);
os_memcpy(user_bin, "user2.bin", 10);
}

char *temp = NULL;
Expand All @@ -215,9 +221,9 @@ get_version_connect_cb(void *arg)
);

#if (FOTA_SECURE)
espconn_sent(pespconn, temp, os_strlen(temp));
#else
espconn_secure_sent(pespconn, temp, os_strlen(temp));
#else
espconn_sent(pespconn, temp, os_strlen(temp));
#endif
os_free(temp);
}
Expand Down Expand Up @@ -269,8 +275,8 @@ upDate_discon_cb(void *arg)
// clear url and host
if (fota_cdn->host) os_free(fota_cdn->host);
if (fota_cdn->url) os_free(fota_cdn->url);
INFO("update disconnect\n");

INFO("FOTA Client: disconnect\n");
}

/**
Expand All @@ -286,6 +292,8 @@ upDate_connect_cb(void *arg)
char temp[32] = {0};
uint8_t i = 0;

INFO("FOTA Client: connected\n");

system_upgrade_init();

fota_cdn->up_server = (struct upgrade_server_info *)os_zalloc(sizeof(struct upgrade_server_info));
Expand Down Expand Up @@ -363,7 +371,7 @@ fota_ticktock(fota_client_t *fota_client)

// if ip address is provided, go ahead
if (UTILS_StrToIP(fota_client->host, &fota_client->ip)) {
INFO("FOTA client: Connect to ip %s:%d\r\n", fota_client->host, fota_client->port);
INFO("FOTA client: Connect to ip %s:%d\r\n", fota_client->host, fota_client->port);
// check for new version
start_session(fota_client, get_version_connect_cb, get_version_disconnect_cb);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ota-update/user/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define UPDATE_SERVER_PORT 8888

#define OTA_UUID "16699293389619"
#define OTA_TOKEN "fbbadbdea3d84294a02ce66b3aaf26d19c68909c0bccea64f544e35a5f68948fdeb374850adade60a12828614835f35aea0f840c7f8033f07ef32a661936d167d7705522924ccf8ec5e528668e858020"
#define OTA_TOKEN "fbbadbdea3d84294a02ce66b"
#define OTA_CLIENT "ESP"
#define OTA_VERSION VERSION

Expand Down
5 changes: 4 additions & 1 deletion tools/ota-server-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ http.createServer(function(request, response) {
{
version: "1.0.2",
updated: 1430135590467,
url: "/firmware/otaupdate/versions/1.0.2"
url: "/firmware/otaupdate/versions/1.0.2",
protocol: "http",
host: "192.168.1.178",
url: "/user2"
}
}
response.write(JSON.stringify(res));
Expand Down

0 comments on commit 2ae6f3e

Please sign in to comment.