Exemplo n.º 1
0
uint32_t ICACHE_FLASH_ATTR REST_Setup(PACKET_CMD *cmd)
{
	REQUEST req;
	REST_CLIENT *client;
	uint8_t *rest_host;
	uint16_t len;
	uint32_t port, security;

	CMD_Request(&req, cmd);
	if(CMD_GetArgc(&req) != 3)
		return 0;

	len = CMD_ArgLen(&req);
	rest_host = (uint8_t*)os_zalloc(len + 1);
	CMD_PopArgs(&req, rest_host);
	rest_host[len] = 0;

	client = (REST_CLIENT*)os_zalloc(sizeof(REST_CLIENT));
	os_memset(client, 0, sizeof(REST_CLIENT));
	if(client == NULL)
		return 0;

	CMD_PopArgs(&req, (uint8_t*)&port);

	CMD_PopArgs(&req, (uint8_t*)&security);

	client->resp_cb = cmd->callback;

	client->host = rest_host;
	client->port = port;
	client->security = security;
	client->ip.addr = 0;

	client->data = (uint8_t*)os_zalloc(1024);

	client->header = (uint8_t*)os_zalloc(4);
	client->header[0] = 0;

	client->content_type = (uint8_t*)os_zalloc(22);
	os_sprintf(client->content_type, "x-www-form-urlencoded");
	client->content_type[21] = 0;

	client->user_agent = (uint8_t*)os_zalloc(17);
	os_sprintf(client->user_agent, "ESPDRUINO@tuanpmt");
	client->user_agent[16] = 0;

	client->pCon = (struct espconn *)os_zalloc(sizeof(struct espconn));
	client->pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));

	client->pCon->type = ESPCONN_TCP;
	client->pCon->state = ESPCONN_NONE;
	client->pCon->proto.tcp->local_port = espconn_port();
	client->pCon->proto.tcp->remote_port = client->port;

	client->pCon->reverse = client;

	return (uint32_t)client;
}
Exemplo n.º 2
0
uint32_t ICACHE_FLASH_ATTR REST_SetHeader(PACKET_CMD *cmd)
{
	REQUEST req;
	REST_CLIENT *client;
	uint16_t len;
	uint32_t header_index, client_ptr = 0;

	CMD_Request(&req, cmd);

	if(CMD_GetArgc(&req) != 3)
		return 0;

	/* Get client*/
	CMD_PopArgs(&req, (uint8_t*)&client_ptr);
	client = (REST_CLIENT*)client_ptr;

	CMD_PopArgs(&req, (uint8_t*)&header_index);
	len = CMD_ArgLen(&req);

	switch(header_index) {
	case HEADER_GENERIC:
		if(client->header)
			os_free(client->header);
		client->header = (uint8_t*)os_zalloc(len + 1);
		CMD_PopArgs(&req, (uint8_t*)client->header);
		client->header[len] = 0;
		INFO("Set header: %s\r\n", client->header);
		break;
	case HEADER_CONTENT_TYPE:
		if(client->content_type)
			os_free(client->content_type);
		client->content_type = (uint8_t*)os_zalloc(len + 1);
		CMD_PopArgs(&req, (uint8_t*)client->content_type);
		client->content_type[len] = 0;
		INFO("Set content_type: %s\r\n", client->content_type);
		break;
	case HEADER_USER_AGENT:
		if(client->user_agent)
			os_free(client->user_agent);
		client->user_agent = (uint8_t*)os_zalloc(len + 1);
		CMD_PopArgs(&req, (uint8_t*)client->user_agent);
		client->user_agent[len] = 0;
		INFO("Set user_agent: %s\r\n", client->user_agent);
		break;
	}
	return 1;

}
Exemplo n.º 3
0
uint32_t ICACHE_FLASH_ATTR NTP_SetTimeZone(PACKET_CMD *cmd)
{
  REQUEST           req;
  int               timezone;

  CMD_Request(&req, cmd);
  if (CMD_GetArgc(&req) != 1)
  {
    return 0;
  }

  CMD_PopArgs(&req, (uint8_t*)&timezone);

  if ((timezone > 13) || (timezone < -11))
  {
    return -1;
  }
  
  if (timezone != sntp_get_timezone())
  {
    sntp_stop();
    if (true == sntp_set_timezone(timezone))
    {
      sntp_init();
    }
  }

  return 0;
}
Exemplo n.º 4
0
uint32_t ICACHE_FLASH_ATTR SYSTEM_Handler(PACKET_CMD *cmd)
{
  enum system_event event;
  REQUEST           req;
  uint8_t           mac[6];
  uint16_t          crc;


  CMD_Request(&req, cmd);
  if (CMD_GetArgc(&req) != 1)
  {
    return 0;
  }
  
  CMD_PopArgs(&req, (uint8_t*)&event);

  switch (event)
  {
    case GET_STATION_MAC:
    case GET_AP_MAC:
      if (event == GET_STATION_MAC)
      {
        wifi_get_macaddr(STATION_IF, mac);
      }
      else
      {
        wifi_get_macaddr(SOFTAP_IF, mac);
      }
      crc = CMD_ResponseStart(CMD_SYSTEM, cmd->callback, 0, 2);
      crc = CMD_ResponseBody(crc, (uint8_t*)&event, sizeof(event));
      crc = CMD_ResponseBody(crc, mac, 6);
      CMD_ResponseEnd(crc);
      break;

    default:
      break;
  }

  return 0;
}
Exemplo n.º 5
0
uint32_t ICACHE_FLASH_ATTR REST_Request(PACKET_CMD *cmd)
{

	REQUEST req;
	REST_CLIENT *client;
	uint16_t len, realLen = 0;
	uint32_t client_ptr;
	uint8_t *method, *path, *body = NULL;

	CMD_Request(&req, cmd);

	if(CMD_GetArgc(&req) <3)
		return 0;

	/* Get client*/
	CMD_PopArgs(&req, (uint8_t*)&client_ptr);
	client = (REST_CLIENT*)client_ptr;

	//method
	len = CMD_ArgLen(&req);
	method = (uint8_t*)os_zalloc(len + 1);
	CMD_PopArgs(&req, method);
	method[len] = 0;


	//path
	len = CMD_ArgLen(&req);
	path = (uint8_t*)os_zalloc(len + 1);
	CMD_PopArgs(&req, path);
	path[len] = 0;

	//body
	if(CMD_GetArgc(&req) == 3){
		realLen = 0;
		len = 0;
	} else {
		CMD_PopArgs(&req, (uint8_t*)&realLen);

		len = CMD_ArgLen(&req);
		body = (uint8_t*)os_zalloc(len + 1);
		CMD_PopArgs(&req, body);
		body[len] = 0;
	}

	client->pCon->state = ESPCONN_NONE;

	INFO("REQ: method: %s, path: %s\r\n", method, path);

	client->data_len = os_sprintf(client->data, "%s %s HTTP/1.1\r\n"
												"Host: %s\r\n"
												"%s"
												"Content-Length: %d\r\n"
												"Connection: close\r\n"
												"Content-Type: %s\r\n"
												"User-Agent: %s\r\n\r\n",
												method, path,
												client->host,
												client->header,
												realLen,
												client->content_type,
												client->user_agent);

	if(realLen > 0){
		os_memcpy(client->data + client->data_len, body, realLen);
		client->data_len += realLen;
		os_sprintf(client->data + client->data_len, "\r\n\r\n");
		client->data_len += 4;
	}

	client->pCon->state = ESPCONN_NONE;
	espconn_regist_connectcb(client->pCon, tcpclient_connect_cb);
	espconn_regist_reconcb(client->pCon, tcpclient_recon_cb);

	if(UTILS_StrToIP(client->host, &client->pCon->proto.tcp->remote_ip)) {
		INFO("REST: Connect to ip  %s:%d\r\n",client->host, client->port);
		if(client->security){
			espconn_secure_connect(client->pCon);
		}
		else {
			espconn_connect(client->pCon);
		}
	}
	else {
		INFO("REST: Connect to domain %s:%d\r\n", client->host, client->port);
		espconn_gethostbyname(client->pCon, client->host, &client->ip, rest_dns_found);
	}
	os_free(method);
	os_free(path);
	if(body) os_free(body);
	return 1;
}