Skip to content

Commit

Permalink
new fota-info files, get firmware information
Browse files Browse the repository at this point in the history
  • Loading branch information
nqd committed May 17, 2015
1 parent 9d5a5c7 commit 027a3dd
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 176 deletions.
24 changes: 12 additions & 12 deletions apps/fota/fota-cdn.c → apps/fota/fota-firmware.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include "upgrade.h"

#include "fota.h"
#include "fota-cdn.h"
#include "fota-firmware.h"
#include "fota-util.h"

LOCAL void upDate_discon_cb(void *arg);
LOCAL void upDate_connect_cb(void *arg);
LOCAL void upgrade_discon_cb(void *arg);
LOCAL void upgrade_connect_cb(void *arg);

/************************************************************************************
* GET FIRMWARE
Expand All @@ -30,7 +30,7 @@ clear_upgradeconn(struct upgrade_server_info *server)
}

LOCAL void ICACHE_FLASH_ATTR
cdn_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
upgrade_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{
if(ipaddr == NULL) {
INFO("DNS: Found, but got no ip\n");
Expand All @@ -50,7 +50,7 @@ cdn_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
}

LOCAL void ICACHE_FLASH_ATTR
upDate_rsp(void *arg)
upgrade_response(void *arg)
{
struct upgrade_server_info *server = arg;
struct espconn *pespconn = server->pespconn;
Expand Down Expand Up @@ -80,7 +80,7 @@ upDate_rsp(void *arg)
* @retval None
*/
LOCAL void ICACHE_FLASH_ATTR
upDate_discon_cb(void *arg)
upgrade_discon_cb(void *arg)
{
struct espconn *pespconn = (struct espconn *)arg;
fota_cdn_t *fota_cdn = (fota_cdn_t *)pespconn->reverse;
Expand All @@ -102,7 +102,7 @@ upDate_discon_cb(void *arg)
* @retval None
*/
LOCAL void ICACHE_FLASH_ATTR
upDate_connect_cb(void *arg)
upgrade_connect_cb(void *arg)
{
struct espconn *pespconn = (struct espconn *)arg;
fota_cdn_t *fota_cdn = (fota_cdn_t *)pespconn->reverse;
Expand All @@ -119,7 +119,7 @@ upDate_connect_cb(void *arg)
fota_cdn->up_server->pespconn = pespconn;
os_memcpy(fota_cdn->up_server->ip, pespconn->proto.tcp->remote_ip, 4);
fota_cdn->up_server->port = fota_cdn->port;
fota_cdn->up_server->check_cb = upDate_rsp;
fota_cdn->up_server->check_cb = upgrade_response;
fota_cdn->up_server->check_times = 60000;

if(fota_cdn->up_server->url == NULL) {
Expand Down Expand Up @@ -169,9 +169,9 @@ start_cdn(fota_cdn_t *fota_cdn, char *version, char *host, char *url, char *prot
fota_cdn->conn->proto.tcp->local_port = espconn_port();
fota_cdn->conn->proto.tcp->remote_port = fota_cdn->port;

espconn_regist_connectcb(fota_cdn->conn, upDate_connect_cb);
// espconn_regist_reconcb(fota_client->conn, upDate_discon_cb);
espconn_regist_disconcb(fota_cdn->conn, upDate_discon_cb);
espconn_regist_connectcb(fota_cdn->conn, upgrade_connect_cb);
// espconn_regist_reconcb(fota_client->conn, upgrade_discon_cb);
espconn_regist_disconcb(fota_cdn->conn, upgrade_discon_cb);

// if ip address is provided, go ahead
if (UTILS_StrToIP(fota_cdn->host, &fota_cdn->conn->proto.tcp->remote_ip)) {
Expand All @@ -185,6 +185,6 @@ start_cdn(fota_cdn_t *fota_cdn, char *version, char *host, char *url, char *prot
espconn_gethostbyname(fota_cdn->conn,
fota_cdn->host,
(ip_addr_t *) &fota_cdn->conn->proto.tcp->remote_ip,
cdn_dns_found);
upgrade_dns_found);
}
}
File renamed without changes.
174 changes: 174 additions & 0 deletions apps/fota/fota-info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include "osapi.h"
#include "c_types.h"
#include "user_interface.h"
#include "espconn.h"
#include "mem.h"
#include "upgrade.h"

#include "jsmn.h" // json parsing
#include "utils.h"

#include "fota.h"
#include "fota-info.h"
#include "fota-firmware.h"
#include "fota-util.h"

extern uint32_t version_fwr;
/**
* @brief response for get version request.
* parse answer, save online version to flash.
* @param arg: contain the ip link information
* pusrdata: data
* len: len of data (strlen)
* @retval None
*/
LOCAL void ICACHE_FLASH_ATTR
get_version_recv(void *arg, char *pusrdata, unsigned short len)
{
struct espconn *pespconn = arg;
fota_client_t *fota_client = (fota_client_t *)pespconn->reverse;

/* get body */
char *body = (char*)os_strstr(pusrdata, "\r\n\r\n");
if (body == NULL) {
INFO("Invalide response\n");
return;
}
INFO("Body: %s\n", body+4);
uint32_t bodylen = os_strlen(body);

/* parse json, get version */
char *n_host,
*n_url,
*n_version,
*n_protocol;

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);
clear_tcp_of_espconn(fota_client->conn);

uint32_t version;
if (convert_version(n_version, os_strlen(n_version), &version) < 0) {
REPORT("Invalide version return %s\n", n_version);
goto CLEAN_MEM;
}

/* if we still have lastest version */
if (version <= version_fwr) {
INFO("We have lastest firmware (current %u.%u.%u vs online %u.%u.%u)\n",
(version_fwr/256/256)%256, (version_fwr/256)%256, version_fwr%256,
(version/256/256)%256, (version/256)%256, version%256);
goto CLEAN_MEM;
}

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

start_cdn(&fota_client->fw_server, n_version, n_host, n_url, n_protocol);

CLEAN_MEM:
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);
}

/**
* @brief after sending (version) request, wait for reply timeout
* we do not received right answer, close connection
* @param arg: contain the ip link information
* @retval None
*/
LOCAL void ICACHE_FLASH_ATTR
get_version_timeout(void *arg)
{
struct espconn *pespconn = arg;
fota_client_t *fota_client = (fota_client_t *)pespconn->reverse;
os_timer_disarm(&fota_client->request_timeout);

INFO("get version timeout, close connection\n");
clear_tcp_of_espconn(pespconn);
}

/**
* @brief sent callback, data has been set successfully, and ack by the
* remote host. Set timmer to wait for reply
* @param arg: contain the ip link information
* @retval None
*/
LOCAL void ICACHE_FLASH_ATTR
get_version_sent_cb(void *arg)
{
struct espconn *pespconn = arg;
fota_client_t *fota_client = (fota_client_t *)pespconn->reverse;

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("FOTA Client: sent request\n");
}

/**
* @brief Tcp client disconnect success callback function.
* @param arg: contain the ip link information
* @retval None
*/
void ICACHE_FLASH_ATTR
get_version_disconnect_cb(void *arg)
{
INFO("FOTA Client: disconnect\n");
clear_tcp_of_espconn((struct espconn *)arg);
}

/**
* @brief Get version connection version
* @param arg: contain the ip link information
* @retval None
*/
void ICACHE_FLASH_ATTR
get_version_connect_cb(void *arg)
{
struct espconn *pespconn = (struct espconn *)arg;
fota_client_t *fota_client = (fota_client_t *)pespconn->reverse;

espconn_regist_recvcb(pespconn, get_version_recv);
espconn_regist_sentcb(pespconn, get_version_sent_cb);

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

char *temp = NULL;
temp = (uint8 *) os_zalloc(512);

os_sprintf(temp, "GET /firmware/%s/versions HTTP/1.0\r\nHost: %s\r\n"pHeadStatic""pHeadAuthen"\r\n",
PROJECT,
fota_client->host,
fota_client->uuid, //pHeaderAuthen
fota_client->token,
FOTA_CLIENT,
user_bin,
VERSION
);

#if (FOTA_SECURE)
espconn_secure_sent(pespconn, temp, os_strlen(temp));
#else
espconn_sent(pespconn, temp, os_strlen(temp));
#endif
os_free(temp);
}
7 changes: 7 additions & 0 deletions apps/fota/fota-info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _FOTA_INFO_H_
#define _FOTA_INFO_H_

void get_version_disconnect_cb(void *arg);
void get_version_connect_cb(void *arg);

#endif
Loading

0 comments on commit 027a3dd

Please sign in to comment.