Skip to content

Commit

Permalink
adding reconnect to fota
Browse files Browse the repository at this point in the history
  • Loading branch information
nqd committed May 22, 2015
1 parent 7ae7312 commit 22390cf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
25 changes: 23 additions & 2 deletions apps/fota/fota-firmware.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ else
espconn_disconnect(pespconn);
}

LOCAL void ICACHE_FLASH_ATTR
upgrade_recon_cb(void *arg, sint8 err)
{
//error occured , tcp connection broke. user can try to reconnect here.
INFO("reconnect callback, error code %d !!!\n",err);
struct espconn *pespconn = (struct espconn *)arg;
fota_cdn_t *fota_cdn = (fota_cdn_t *)pespconn->reverse;
if (fota_cdn->secure)
espconn_secure_disconnect(pespconn);
else
espconn_disconnect(pespconn);
}

/**
* @brief Tcp client disconnect success callback function.
* @param arg: contain the ip link information
Expand Down Expand Up @@ -136,7 +149,11 @@ upgrade_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr, 4);
}
// check for new version
start_esp_connect(fota_cdn->conn, fota_cdn->secure, upgrade_connect_cb, upgrade_discon_cb);
start_esp_connect(fota_cdn->conn,
fota_cdn->secure,
upgrade_connect_cb,
upgrade_discon_cb,
upgrade_recon_cb);
}

void ICACHE_FLASH_ATTR
Expand Down Expand Up @@ -173,7 +190,11 @@ start_cdn(fota_cdn_t *fota_cdn, char *version, char *host, char *url, char *prot
if (UTILS_StrToIP(fota_cdn->host, &fota_cdn->conn->proto.tcp->remote_ip)) {
INFO("Firmware client: Connect to %s:%d\r\n", fota_cdn->host, fota_cdn->port);
// check for new version
start_esp_connect(fota_cdn->conn, fota_cdn->secure, upgrade_connect_cb, upgrade_discon_cb);
start_esp_connect(fota_cdn->conn,
fota_cdn->secure,
upgrade_connect_cb,
upgrade_discon_cb,
upgrade_recon_cb);
}
// else, use dns query to get ip address
else {
Expand Down
12 changes: 12 additions & 0 deletions apps/fota/fota-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ get_version_disconnect_cb(void *arg)
clear_espconn((struct espconn *)arg);
}

void ICACHE_FLASH_ATTR
get_version_recon_cb(void *arg, sint8 err)
{
//error occured , tcp connection broke. user can try to reconnect here.
INFO("reconnect callback, error code %d !!!\n",err);
#if (FOTA_SECURE)
espconn_secure_disconnect((struct espconn *)arg);
#else
espconn_disconnect((struct espconn *)arg);
#endif
}

/**
* @brief Get version connection version
* @param arg: contain the ip link information
Expand Down
1 change: 1 addition & 0 deletions apps/fota/fota-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

void get_version_disconnect_cb(void *arg);
void get_version_connect_cb(void *arg);
void get_version_recon_cb(void *arg, sint8 err);

#endif
4 changes: 3 additions & 1 deletion apps/fota/fota-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ convert_version(const char *version_str, uint32_t len, uint32_t *version_bin)
}

void ICACHE_FLASH_ATTR
start_esp_connect(struct espconn *conn, uint8_t secure, void *connect_cb, void *disconnect_cb)
start_esp_connect(struct espconn *conn, uint8_t secure, void *connect_cb, void *disconnect_cb, void *reconn_cb)
{
espconn_regist_connectcb(conn, connect_cb);
espconn_regist_disconcb(conn, disconnect_cb);
espconn_regist_reconcb(conn, reconn_cb);

// INFO("Dest ip: %d.%d.%d.%d:%d\n",
// conn->proto.tcp->remote_ip[0],
// conn->proto.tcp->remote_ip[1],
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 @@ -8,7 +8,7 @@

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);
void start_esp_connect(struct espconn *conn, uint8_t secure, void *conn_cb, void *disconn_cb);
void start_esp_connect(struct espconn *conn, uint8_t secure, void *conn_cb, void *disconn_cb, void *reconn_cb);
void clear_espconn(struct espconn *conn);

#endif
12 changes: 10 additions & 2 deletions apps/fota/fota.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ fota_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
}

// check for new version
start_esp_connect(pespconn, FOTA_SECURE, get_version_connect_cb, get_version_disconnect_cb);
start_esp_connect(pespconn,
FOTA_SECURE,
get_version_connect_cb,
get_version_disconnect_cb,
get_version_recon_cb);
}

// is called periodically to check for next version
Expand All @@ -61,7 +65,11 @@ fota_ticktock(fota_client_t *fota_client)
if (UTILS_StrToIP(fota_client->host, &fota_client->conn->proto.tcp->remote_ip)) {
INFO("FOTA client: Connect to ip %s:%d\r\n", fota_client->host, fota_client->port);
// check for new version
start_esp_connect(fota_client->conn, FOTA_SECURE, get_version_connect_cb, get_version_disconnect_cb);
start_esp_connect(fota_client->conn,
FOTA_SECURE,
get_version_connect_cb,
get_version_disconnect_cb,
get_version_recon_cb);
}
// else, use dns query to get ip address
else {
Expand Down

0 comments on commit 22390cf

Please sign in to comment.