Beispiel #1
0
// Command handler to add a callback to the named-callbacks list, this is for a callback to the uC
static uint32_t ICACHE_FLASH_ATTR
CMD_AddCallback(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);
#ifdef CMD_DBG
  os_printf("CMD_AddCallback: setup argc=%ld\n", CMD_GetArgc(&req));
#endif
  if (cmd->argc != 1 || cmd->callback == 0)
    return 0;

  char name[16];
  uint16_t len;

  // get the sensor name
  len = CMD_ArgLen(&req);
#ifdef CMD_DBG
  os_printf("CMD_AddCallback: name len=%d\n", len);
#endif
  if (len > 15) return 0; // max size of name is 15 characters
  if (CMD_PopArg(&req, (uint8_t *)name, len)) return 0;
  name[len] = 0;
#ifdef CMD_DBG
  os_printf("CMD_AddCallback: name=%s\n", name);
#endif

  return CMD_AddCb(name, (uint32_t)cmd->callback); // save the sensor callback
}
Beispiel #2
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;
}
Beispiel #3
0
uint32_t ICACHE_FLASH_ATTR
REST_SetHeader(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);

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

  // Get client
  uint32_t clientNum;
  if (CMD_PopArg(&req, (uint8_t*)&clientNum, 4)) return 0;
  if ((clientNum & 0xffff0000) != REST_CB) return 0;
  RestClient *client = restClient + ((clientNum & 0xffff) % MAX_REST);

  // Get header selector
  uint32_t header_index;
  if (CMD_PopArg(&req, (uint8_t*)&header_index, 4)) return 0;

  // Get header value
  uint16_t len = CMD_ArgLen(&req);
  if (len > 256) return 0; //safety check
  switch(header_index) {
  case HEADER_GENERIC:
    if(client->header) os_free(client->header);
    client->header = (char*)os_zalloc(len + 3);
    CMD_PopArg(&req, (uint8_t*)client->header, len);
    client->header[len] = '\r';
    client->header[len+1] = '\n';
    client->header[len+2] = 0;
#ifdef CMDREST_DBG
    os_printf("REST: Set header: %s\r\n", client->header);
#endif
    break;
  case HEADER_CONTENT_TYPE:
    if(client->content_type) os_free(client->content_type);
    client->content_type = (char*)os_zalloc(len + 3);
    CMD_PopArg(&req, (uint8_t*)client->content_type, len);
    client->content_type[len] = '\r';
    client->content_type[len+1] = '\n';
    client->content_type[len+2] = 0;
#ifdef CMDREST_DBG
    os_printf("REST: Set content_type: %s\r\n", client->content_type);
#endif
    break;
  case HEADER_USER_AGENT:
    if(client->user_agent) os_free(client->user_agent);
    client->user_agent = (char*)os_zalloc(len + 3);
    CMD_PopArg(&req, (uint8_t*)client->user_agent, len);
    client->user_agent[len] = '\r';
    client->user_agent[len+1] = '\n';
    client->user_agent[len+2] = 0;
#ifdef CMDREST_DBG
    os_printf("REST: Set user_agent: %s\r\n", client->user_agent);
#endif
    break;
  }
  return 1;
}
Beispiel #4
0
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Publish(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);

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

  // get mqtt client
  uint32_t client_ptr;
  CMD_PopArg(&req, (uint8_t*)&client_ptr, 4);
  MQTT_Client* client = (MQTT_Client*)client_ptr;
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Publish client ptr=%p\n", (void*)client_ptr);
#endif

  uint16_t len;
  uint8_t *topic, *data;
  uint32_t qos = 0, retain = 0, data_len;

  // get topic
  len = CMD_ArgLen(&req);
  if (len > 128) return 0; // safety check
  topic = (uint8_t*)os_zalloc(len + 1);
  CMD_PopArg(&req, topic, len);
  topic[len] = 0;

  // get data
  len = CMD_ArgLen(&req);
  // TODO: Safety check
  // TODO: this was orignially zalloc len not len+1
  data = (uint8_t*)os_zalloc(len+1);
  CMD_PopArg(&req, data, len);
  // TODO: next line not originally present
  data[len] = 0;

  // get data length
  // TODO: this isn't used but we have to pull it off the stack
  CMD_PopArg(&req, (uint8_t*)&data_len, 4);

  // get qos
  CMD_PopArg(&req, (uint8_t*)&qos, 4);

  // get retain
  CMD_PopArg(&req, (uint8_t*)&retain, 4);
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Publish topic=%s, data_len=%d, qos=%ld, retain=%ld\n",
    topic,
    os_strlen((char*)data),
    qos,
    retain);
#endif

  MQTT_Publish(client, (char*)topic, (char*)data, (uint8_t)qos, (uint8_t)retain);
  os_free(topic);
  os_free(data);
  return 1;
}
Beispiel #5
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;
}
Beispiel #6
0
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Lwt(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);

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

  // get mqtt client
  uint32_t client_ptr;
  CMD_PopArg(&req, (uint8_t*)&client_ptr, 4);
  MQTT_Client* client = (MQTT_Client*)client_ptr;
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Lwt client ptr=%p\n", (void*)client_ptr);
#endif

  uint16_t len;

  // get topic
  if (client->connect_info.will_topic)
  os_free(client->connect_info.will_topic);
  len = CMD_ArgLen(&req);
  if (len > 128) return 0; // safety check
  client->connect_info.will_topic = (char*)os_zalloc(len + 1);
  CMD_PopArg(&req, client->connect_info.will_topic, len);
  client->connect_info.will_topic[len] = 0;

  // get message
  if (client->connect_info.will_message)
  os_free(client->connect_info.will_message);
  len = CMD_ArgLen(&req);
  // TODO: safety check
  client->connect_info.will_message = (char*)os_zalloc(len + 1);
  CMD_PopArg(&req, client->connect_info.will_message, len);
  client->connect_info.will_message[len] = 0;

  // get qos
  CMD_PopArg(&req, (uint8_t*)&client->connect_info.will_qos, 4);

  // get retain
  CMD_PopArg(&req, (uint8_t*)&client->connect_info.will_retain, 4);
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Lwt topic=%s, message=%s, qos=%d, retain=%d\n",
       client->connect_info.will_topic,
       client->connect_info.will_message,
       client->connect_info.will_qos,
       client->connect_info.will_retain);
#endif
  return 1;
}
Beispiel #7
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;

}
Beispiel #8
0
// Command handler for Wifi connect command
static uint32_t ICACHE_FLASH_ATTR
CMD_WifiConnect(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);
	if(cmd->argc != 2 || cmd->callback == 0)
		return 0;

  if (!wifiCbAdded) {
    wifiAddStateChangeCb(CMD_WifiCb);    // register our callback with wifi subsystem
    wifiCbAdded = true;
  }
  CMD_AddCb("wifiCb", (uint32_t)cmd->callback); // save the MCU's callback
  lastWifiStatus = 0xff; // set to invalid value so we immediately send status cb in all cases
  CMD_WifiCb(wifiState);

  return 1;
}
Beispiel #9
0
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Connect(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);

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

  // get mqtt client
  uint32_t client_ptr;
  CMD_PopArg(&req, (uint8_t*)&client_ptr, 4);
  MQTT_Client* client = (MQTT_Client*)client_ptr;
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Connect client ptr=%p\n", (void*)client_ptr);
#endif

  uint16_t len;

  // get host
  if (client->host)
  os_free(client->host);
  len = CMD_ArgLen(&req);
  if (len > 128) return 0; // safety check
  client->host = (char*)os_zalloc(len + 1);
  CMD_PopArg(&req, client->host, len);
  client->host[len] = 0;

  // get port
  CMD_PopArg(&req, (uint8_t*)&client->port, 4);

  // get security
  CMD_PopArg(&req, (uint8_t*)&client->security, 4);
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Connect host=%s, port=%d, security=%d\n",
    client->host,
    client->port,
    client->security);
#endif

  MQTT_Connect(client);
  return 1;
}
Beispiel #10
0
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Disconnect(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);

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

  // get mqtt client
  uint32_t client_ptr;
  CMD_PopArg(&req, (uint8_t*)&client_ptr, 4);
  MQTT_Client* client = (MQTT_Client*)client_ptr;
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Disconnect client ptr=%p\n", (void*)client_ptr);
#endif

  // disconnect
  MQTT_Disconnect(client);
  return 1;
}
Beispiel #11
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;
}
Beispiel #12
0
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Subscribe(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);

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

  // get mqtt client
  uint32_t client_ptr;
  CMD_PopArg(&req, (uint8_t*)&client_ptr, 4);
  MQTT_Client* client = (MQTT_Client*)client_ptr;
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Subscribe client ptr=%p\n", (void*)client_ptr);
#endif

  uint16_t len;
  uint8_t* topic;
  uint32_t qos = 0;

  // get topic
  len = CMD_ArgLen(&req);
  if (len > 128) return 0; // safety check
  topic = (uint8_t*)os_zalloc(len + 1);
  CMD_PopArg(&req, topic, len);
  topic[len] = 0;

  // get qos
  CMD_PopArg(&req, (uint8_t*)&qos, 4);
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Subscribe topic=%s, qos=%ld\n", topic, qos);
#endif
  MQTT_Subscribe(client, (char*)topic, (uint8_t)qos);
  os_free(topic);
  return 1;
}
Beispiel #13
0
uint32_t ICACHE_FLASH_ATTR
REST_Request(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);
#ifdef CMDREST_DBG
  os_printf("REST: request");
#endif
  // Get client
  uint32_t clientNum;
  if (CMD_PopArg(&req, (uint8_t*)&clientNum, 4)) goto fail;
  if ((clientNum & 0xffff0000) != REST_CB) goto fail;
  clientNum &= 0xffff;
  RestClient *client = restClient + clientNum % MAX_REST;
#ifdef CMDREST_DBG
  os_printf(" #%ld", clientNum);
#endif
  // Get HTTP method
  uint16_t len = CMD_ArgLen(&req);
  if (len > 15) goto fail;
  char method[16];
  CMD_PopArg(&req, method, len);
  method[len] = 0;
#ifdef CMDREST_DBG
  os_printf(" method=%s", method);
#endif
  // Get HTTP path
  len = CMD_ArgLen(&req);
  if (len > 1023) goto fail;
  char path[1024];
  CMD_PopArg(&req, path, len);
  path[len] = 0;
#ifdef CMDREST_DBG
  os_printf(" path=%s", path);
#endif
  // Get HTTP body
  uint32_t realLen = 0;
  if (CMD_GetArgc(&req) == 3) {
    realLen = 0;
    len = 0;
  } else {
    CMD_PopArg(&req, (uint8_t*)&realLen, 4);

    len = CMD_ArgLen(&req);
    if (len > 2048 || realLen > len) goto fail;
  }
#ifdef CMDREST_DBG
  os_printf(" bodyLen=%ld", realLen);
#endif

  // we need to allocate memory for the header plus the body. First we count the length of the
  // header (including some extra counted "%s" and then we add the body length. We allocate the
  // whole shebang and copy everything into it.
  // BTW, use http/1.0 to avoid responses with transfer-encoding: chunked
  char *headerFmt = "%s %s HTTP/1.0\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";
  uint16_t headerLen = strlen(headerFmt) + strlen(method) + strlen(path) + strlen(client->host) +
      strlen(client->header) + strlen(client->content_type) + strlen(client->user_agent);
#ifdef CMDREST_DBG
  os_printf(" hdrLen=%d", headerLen);
#endif
  if (client->data) os_free(client->data);
  client->data = (char*)os_zalloc(headerLen + realLen);
  if (client->data == NULL) goto fail;
#ifdef CMDREST_DBG
  os_printf(" totLen=%ld data=%p", headerLen + realLen, client->data);
#endif
  client->data_len = os_sprintf((char*)client->data, headerFmt, method, path, client->host,
      client->header, realLen, client->content_type, client->user_agent);
#ifdef CMDREST_DBG
  os_printf(" hdrLen=%d", client->data_len);
#endif

  if (realLen > 0) {
    CMD_PopArg(&req, client->data + client->data_len, realLen);
    client->data_len += realLen;
  }
#ifdef CMDREST_DBG
  os_printf("\n");

  //os_printf("REST request: %s", (char*)client->data);

  os_printf("REST: pCon state=%d\n", client->pCon->state);
#endif
  client->pCon->state = ESPCONN_NONE;
  espconn_regist_connectcb(client->pCon, tcpclient_connect_cb);
  espconn_regist_reconcb(client->pCon, tcpclient_recon_cb);

  if(UTILS_StrToIP((char *)client->host, &client->pCon->proto.tcp->remote_ip)) {
#ifdef CMDREST_DBG
    os_printf("REST: Connect to ip %s:%ld\n",client->host, client->port);
#endif
    //if(client->security){
    //  espconn_secure_connect(client->pCon);
    //}
    //else {
      espconn_connect(client->pCon);
    //}
  } else {
#ifdef CMDREST_DBG
    os_printf("REST: Connect to host %s:%ld\n", client->host, client->port);
#endif
    espconn_gethostbyname(client->pCon, (char *)client->host, &client->ip, rest_dns_found);
  }

  return 1;

fail:
#ifdef CMDREST_DBG
  os_printf("\n");
#endif
  return 0;
}
Beispiel #14
0
uint32_t ICACHE_FLASH_ATTR
REST_Setup(CmdPacket *cmd) {
  CmdRequest req;
  uint32_t port, security;

  // start parsing the command
  CMD_Request(&req, cmd);
  if(CMD_GetArgc(&req) != 3) return 0;

  // get the hostname
  uint16_t len = CMD_ArgLen(&req);
  if (len > 128) return 0; // safety check
  uint8_t *rest_host = (uint8_t*)os_zalloc(len + 1);
  if (CMD_PopArg(&req, rest_host, len)) return 0;
  rest_host[len] = 0;

  // get the port
  if (CMD_PopArg(&req, (uint8_t*)&port, 4)) {
    os_free(rest_host);
    return 0;
  }

  // get the security mode
  if (CMD_PopArg(&req, (uint8_t*)&security, 4)) {
    os_free(rest_host);
    return 0;
  }

  // clear connection structures the first time
  if (restNum == 0xff) {
    os_memset(restClient, 0, MAX_REST * sizeof(RestClient));
    restNum = 0;
  }

  // allocate a connection structure
  RestClient *client = restClient + restNum;
  uint8_t clientNum = restNum;
  restNum = (restNum+1)%MAX_REST;

  // free any data structure that may be left from a previous connection
  if (client->header) os_free(client->header);
  if (client->content_type) os_free(client->content_type);
  if (client->user_agent) os_free(client->user_agent);
  if (client->data) os_free(client->data);
  if (client->pCon) {
    if (client->pCon->proto.tcp) os_free(client->pCon->proto.tcp);
    os_free(client->pCon);
  }
  os_memset(client, 0, sizeof(RestClient));
#ifdef CMDREST_DBG
  os_printf("REST: setup #%d host=%s port=%ld security=%ld\n", clientNum, rest_host, port, security);
#endif

  client->resp_cb = cmd->callback;

  client->host = (char *)rest_host;
  client->port = port;
  client->security = security;

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

  client->content_type = (char*)os_zalloc(22);
  os_sprintf((char *)client->content_type, "x-www-form-urlencoded");

  client->user_agent = (char*)os_zalloc(9);
  os_sprintf((char *)client->user_agent, "esp-link");

  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 REST_CB | (uint32_t)clientNum;
}
Beispiel #15
0
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Setup(CmdPacket *cmd) {
  CmdRequest req;
  CMD_Request(&req, cmd);

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

  // create mqtt client
  uint8_t clientLen = sizeof(MQTT_Client);
  MQTT_Client* client = (MQTT_Client*)os_zalloc(clientLen);
  if (client == NULL) return 0;
  os_memset(client, 0, clientLen);

  return 0;
#if 0

  uint16_t len;
  uint8_t *client_id, *user_data, *pass_data;
  uint32_t keepalive, clean_session, cb_data;

  // get client id
  len = CMD_ArgLen(&req);
  if (len > 32) return 0; // safety check
  client_id = (uint8_t*)os_zalloc(len + 1);
  CMD_PopArg(&req, client_id, len);
  client_id[len] = 0;

  // get username
  len = CMD_ArgLen(&req);
  if (len > 32) return 0; // safety check
  user_data = (uint8_t*)os_zalloc(len + 1);
  CMD_PopArg(&req, user_data, len);
  user_data[len] = 0;

  // get password
  len = CMD_ArgLen(&req);
  if (len > 32) return 0; // safety check
  pass_data = (uint8_t*)os_zalloc(len + 1);
  CMD_PopArg(&req, pass_data, len);
  pass_data[len] = 0;

  // get keepalive
  CMD_PopArg(&req, (uint8_t*)&keepalive, 4);

  // get clean session
  CMD_PopArg(&req, (uint8_t*)&clean_session, 4);
#ifdef MQTTCMD_DBG
  os_printf("MQTT: MQTTCMD_Setup clientid=%s, user=%s, pw=%s, keepalive=%ld, clean_session=%ld\n", client_id, user_data, pass_data, keepalive, clean_session);
#endif

  // init client
  // TODO: why malloc these all here, pass to MQTT_InitClient to be malloc'd again?
  MQTT_InitClient(client, (char*)client_id, (char*)user_data, (char*)pass_data, keepalive, clean_session);

  // create callback
  MqttCmdCb* callback = (MqttCmdCb*)os_zalloc(sizeof(MqttCmdCb));

  CMD_PopArg(&req, (uint8_t*)&cb_data, 4);
  callback->connectedCb = cb_data;
  CMD_PopArg(&req, (uint8_t*)&cb_data, 4);
  callback->disconnectedCb = cb_data;
  CMD_PopArg(&req, (uint8_t*)&cb_data, 4);
  callback->publishedCb = cb_data;
  CMD_PopArg(&req, (uint8_t*)&cb_data, 4);
  callback->dataCb = cb_data;

  client->user_data = callback;

  client->cmdConnectedCb = cmdMqttConnectedCb;
  client->cmdDisconnectedCb = cmdMqttDisconnectedCb;
  client->cmdPublishedCb = cmdMqttPublishedCb;
  client->cmdDataCb = cmdMqttDataCb;

  if (CMD_GetArgc(&req) == 10) {
    CMD_PopArg(&req, (uint8_t*)&cb_data, 4);
    callback->tcpDisconnectedCb = cb_data;
    client->cmdTcpDisconnectedCb = cmdMqttTcpDisconnectedCb;
  }

  os_free(client_id);
  os_free(user_data);
  os_free(pass_data);

  return (uint32_t)client;
#endif
}
Beispiel #16
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;
}