Exemple #1
0
void ICACHE_FLASH_ATTR
start_fota(fota_client_t *fota_client, uint16_t interval, char *host, uint16_t port, char *id, char* token)
{
  if (is_running) {
    REPORT("FOTA is called only one time, exit\n");
    return;
  }
  else
    is_running = 1;

  // get current firmware version
  if (convert_version(VERSION, os_strlen(VERSION), &version_fwr) < 0) {
    REPORT("FOTA: Version configuration [%s] is wrong\n", VERSION);
    return;
  }

  // setup fota_client
  os_memset(fota_client, '\0', sizeof(fota_client_t));
  fota_client->interval = interval;
  fota_client->host = (char*)os_zalloc(os_strlen(host)+1);
  os_memcpy(fota_client->host, host, os_strlen(host));
  // port
  fota_client->port = port;
  // uuid
  fota_client->uuid = (char*)os_zalloc(os_strlen(id)+1);
  os_memcpy(fota_client->uuid, id, os_strlen(id));
  // token
  fota_client->token = (char*)os_zalloc(os_strlen(token)+1);
  os_memcpy(fota_client->token, token, os_strlen(token));

  os_timer_disarm(&fota_client->periodic);
  os_timer_setfn(&fota_client->periodic, (os_timer_func_t *)fota_ticktock, fota_client);
  os_timer_arm(&fota_client->periodic, fota_client->interval, 0);       // do not repeat
}
Exemple #2
0
void ICACHE_FLASH_ATTR
handle_response(fota_client_t *fota_client)
{

  INFO("Final response: %s\n", get_version_body);
  /* parse json, get version */  
  char *n_host,
       *n_path,
       *n_version,
       *n_protocol;

  if (parse_fota(get_version_body, get_version_body_len, &n_version, &n_host, &n_path, &n_protocol) < 0) {
    INFO("FOTA Client: Invalid response\n");
    goto CLEAN_MEM;
  }
  INFO("\tVersion %s\n", n_version);
  INFO("\tHost %s\n", n_host);
  INFO("\tPath %s\n", n_path);
  INFO("\tProtocol %s\n", n_protocol);

  /* then, we have valide JSON response */  

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

  /* if we still have lastest version */
  if (version <= version_fwr) {
    INFO("FOTA Client: 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("FOTA Client: Preparing to get firmware\n");
  fota_client->status = FOTA_GETTING_FIRMWARE;
  start_cdn(&fota_client->fw_server, n_version, n_host, n_path, n_protocol);

CLEAN_MEM:
  FREE(n_host);
  FREE(n_path);
  FREE(n_version);
  FREE(n_protocol);
}
Exemple #3
0
/**
  * @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("FOTA Client: Invalid response\n");
    goto CLEAN_MEM;
  }
  INFO("\tVersion %s\n", n_version);
  INFO("\tHost %s\n", n_host);
  INFO("\tURL %s\n", n_url);
  INFO("\tProtocol %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);
#if (FOTA_SECURE)
  espconn_secure_disconnect(pespconn);
#else
  espconn_disconnect(pespconn);
#endif

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

  /* if we still have lastest version */
  if (version <= version_fwr) {
    INFO("FOTA Client: 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("FOTA Client: Preparing to get firmware\n");

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

CLEAN_MEM:
  FREE(n_host);
  FREE(n_url);
  FREE(n_version);
  FREE(n_protocol);
}