converted some DBG macros; misc tweaks

pull/49/head
Thorsten von Eicken 9 years ago
parent 35c2eb65c8
commit f057a65d1f
  1. 4
      Makefile
  2. 2
      esp-link/config.c
  3. 33
      esp-link/main.c
  4. 9
      esp-link/mqtt_client.h
  5. 9
      include/user_config.h
  6. 18
      mqtt/mqtt.c
  7. 61
      mqtt/mqtt_cmd.c
  8. 140
      rest/rest.c
  9. 20
      serial/uart.c

@ -175,10 +175,6 @@ ifneq (,$(findstring rest,$(MODULES)))
CFLAGS += -DREST
endif
ifneq (,$(findstring tcpclient,$(MODULES)))
CFLAGS += -DTCPCLIENT
endif
# which modules (subdirectories) of the project to include in compiling
LIBRARIES_DIR = libraries
MODULES += espfs httpd user serial cmd esp-link

@ -164,7 +164,7 @@ const size_t ICACHE_FLASH_ATTR
getFlashSize() {
uint32_t id = spi_flash_get_id();
uint8_t mfgr_id = id & 0xff;
uint8_t type_id = (id >> 8) & 0xff; // not relevant for size calculation
//uint8_t type_id = (id >> 8) & 0xff; // not relevant for size calculation
uint8_t size_id = (id >> 16) & 0xff; // lucky for us, WinBond ID's their chips as a form that lets us calculate the size
if (mfgr_id != 0xEF) // 0xEF is WinBond; that's all we care about (for now)
return 0;

@ -32,24 +32,6 @@
//#define SHOW_HEAP_USE
//Function that tells the authentication system what users/passwords live on the system.
//This is disabled in the default build; if you want to try it, enable the authBasic line in
//the builtInUrls below.
//int myPassFn(HttpdConnData *connData, int no, char *user, int userLen, char *pass, int passLen) {
// if (no == 0) {
// os_strcpy(user, "admin");
// os_strcpy(pass, "s3cr3t");
// return 1;
//Add more users this way. Check against incrementing no for each user added.
// } else if (no==1) {
// os_strcpy(user, "user1");
// os_strcpy(pass, "something");
// return 1;
// }
// return 0;
//}
/*
This is the main url->function dispatching data struct.
In short, it's a struct with various URLs plus their handlers. The handlers can
@ -66,23 +48,15 @@ HttpdBuiltInUrl builtInUrls[] = {
{ "/flash/next", cgiGetFirmwareNext, NULL },
{ "/flash/upload", cgiUploadFirmware, NULL },
{ "/flash/reboot", cgiRebootFirmware, NULL },
//{"/home.html", cgiEspFsHtml, NULL},
//{"/log.html", cgiEspFsHtml, NULL},
{ "/log/text", ajaxLog, NULL },
{ "/log/dbg", ajaxLogDbg, NULL },
//{"/console.html", cgiEspFsHtml, NULL},
{ "/console/reset", ajaxConsoleReset, NULL },
{ "/console/baud", ajaxConsoleBaud, NULL },
{ "/console/text", ajaxConsole, NULL },
//Routines to make the /wifi URL and everything beneath it work.
//Enable the line below to protect the WiFi configuration with an username/password combo.
// {"/wifi/*", authBasic, myPassFn},
{ "/wifi", cgiRedirect, "/wifi/wifi.html" },
{ "/wifi/", cgiRedirect, "/wifi/wifi.html" },
//{"/wifi/wifi.html", cgiEspFsHtml, NULL},
{ "/wifi/info", cgiWifiInfo, NULL },
{ "/wifi/scan", cgiWiFiScan, NULL },
{ "/wifi/connect", cgiWiFiConnect, NULL },
@ -128,9 +102,6 @@ extern void mqtt_client_init(void);
// Main routine to initialize esp-link.
void user_init(void) {
uart_init(115200, 115200);
logInit(); // must come after init of uart
os_delay_us(10000L);
// get the flash config so we know how to init things
//configWipe(); // uncomment to reset the config for testing purposes
bool restoreOk = configRestore();
@ -138,8 +109,8 @@ void user_init(void) {
gpio_init();
gpio_output_set(0, 0, 0, (1<<15)); // some people tie it GND, gotta ensure it's disabled
// init UART
// uart_init(flashConfig.baud_rate, 115200);
// logInit(); // must come after init of uart
uart_init(flashConfig.baud_rate, 115200);
logInit(); // must come after init of uart
// say hello (leave some time to cause break in TX after boot loader's msg
os_delay_us(10000L);
os_printf("\n\n** %s\n", esp_link_version);

@ -0,0 +1,9 @@
#ifndef MQTT_CLIENT_H
#define MQTT_CLIENT_H
#include "mqtt.h"
extern MQTT_Client mqttClient;
void mqtt_client_init();
#endif

@ -8,8 +8,8 @@
#define DEBUGIP
#define CMD_DBG
#define ESPFS_DBG
#define CGI_DBG
#undef ESPFS_DBG
#undef CGI_DBG
#define CGIFLASH_DBG
#define CGIMQTT_DBG
#define CGIPINS_DBG
@ -20,7 +20,7 @@
#define HTTPD_DBG
#define MQTT_DBG
#define MQTTCMD_DBG
#define PKTBUF_DBG
#undef PKTBUF_DBG
#define REST_DBG
#define RESTCMD_DBG
#define SERBR_DBG
@ -28,7 +28,8 @@
#define SLIP_DBG
#define UART_DBG
#define CHIP_IN_HOSTNAME
// If defined, the default hostname for DHCP will include the chip ID to make it unique
#undef CHIP_IN_HOSTNAME
extern char* esp_link_version;
extern uint8_t UTILS_StrToIP(const char* str, void *ip);

@ -162,7 +162,7 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) {
switch (msg_type) {
case MQTT_MSG_TYPE_CONNACK:
DBG_MQTT("MQTT: Connect successful\n");
//DBG_MQTT("MQTT: Connect successful\n");
// callbacks for internal and external clients
if (client->connectedCb) client->connectedCb((uint32_t*)client);
if (client->cmdConnectedCb) client->cmdConnectedCb((uint32_t*)client);
@ -170,28 +170,28 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) {
case MQTT_MSG_TYPE_SUBACK:
if (pending_msg_type == MQTT_MSG_TYPE_SUBSCRIBE && pending_msg_id == msg_id) {
DBG_MQTT("MQTT: Subscribe successful\n");
//DBG_MQTT("MQTT: Subscribe successful\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
}
break;
case MQTT_MSG_TYPE_UNSUBACK:
if (pending_msg_type == MQTT_MSG_TYPE_UNSUBSCRIBE && pending_msg_id == msg_id) {
DBG_MQTT("MQTT: Unsubscribe successful\n");
//DBG_MQTT("MQTT: Unsubscribe successful\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
}
break;
case MQTT_MSG_TYPE_PUBACK: // ack for a publish we sent
if (pending_msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg_id == msg_id) {
DBG_MQTT("MQTT: QoS1 Publish successful\n");
//DBG_MQTT("MQTT: QoS1 Publish successful\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
}
break;
case MQTT_MSG_TYPE_PUBREC: // rec for a publish we sent
if (pending_msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg_id == msg_id) {
DBG_MQTT("MQTT: QoS2 publish cont\n");
//DBG_MQTT("MQTT: QoS2 publish cont\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
// we need to send PUBREL
mqtt_msg_pubrel(&client->mqtt_connection, msg_id);
@ -202,7 +202,7 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) {
case MQTT_MSG_TYPE_PUBCOMP: // comp for a pubrel we sent (originally publish we sent)
if (pending_msg_type == MQTT_MSG_TYPE_PUBREL && pending_msg_id == msg_id) {
DBG_MQTT("MQTT: QoS2 Publish successful\n");
//DBG_MQTT("MQTT: QoS2 Publish successful\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
}
break;
@ -228,7 +228,7 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) {
case MQTT_MSG_TYPE_PUBREL: // rel for a rec we sent (originally publish received)
if (pending_msg_type == MQTT_MSG_TYPE_PUBREC && pending_msg_id == msg_id) {
DBG_MQTT("MQTT: Cont QoS2 recv\n");
//DBG_MQTT("MQTT: Cont QoS2 recv\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
// we need to send PUBCOMP
mqtt_msg_pubcomp(&client->mqtt_connection, msg_id);
@ -446,11 +446,13 @@ mqtt_send_message(MQTT_Client* client) {
msg_id = msg_id;
#ifdef MQTT_DBG
os_printf("MQTT: Send type=%s id=%04X len=%d\n", mqtt_msg_type[msg_type], msg_id, buf->filled);
#if 0
for (int i=0; i<buf->filled; i++) {
if (buf->data[i] >= ' ' && buf->data[i] <= '~') os_printf("%c", buf->data[i]);
else os_printf("\\x%02X", buf->data[i]);
}
os_printf("\n");
#endif
#endif
// send the message out
@ -566,7 +568,7 @@ MQTT_Publish(MQTT_Client* client, const char* topic, const char* data, uint8_t q
buf->filled = msg.message.length;
DBG_MQTT("MQTT: Publish, topic: \"%s\", length: %d\n", topic, msg.message.length);
dumpMem(buf, buf_len);
//dumpMem(buf, buf_len);
client->msgQueue = PktBuf_Push(client->msgQueue, buf);
if (!client->sending && client->pending_buffer == NULL) {

@ -2,19 +2,24 @@
#include "mqtt.h"
#include "mqtt_cmd.h"
#ifdef MQTTCMD_DBG
#define DBG_MQTTCMD(format, ...) os_printf(format, ## __VA_ARGS__)
#else
#define DBG_MQTTCMD(format, ...) do { } while(0)
#endif
uint32_t connectedCb = 0, disconnectCb = 0, tcpDisconnectedCb = 0, publishedCb = 0, dataCb = 0;
void ICACHE_FLASH_ATTR
cmdMqttConnectedCb(uint32_t* args) {
MQTT_Client* client = (MQTT_Client*)args;
MqttCmdCb* cb = (MqttCmdCb*)client->user_data;
#ifdef MQTTCMD_DBG
os_printf("MQTT: Connected connectedCb=%p, disconnectedCb=%p, publishedCb=%p, dataCb=%p\n",
DBG_MQTTCMD("MQTT: Connected connectedCb=%p, disconnectedCb=%p, publishedCb=%p, dataCb=%p\n",
(void*)cb->connectedCb,
(void*)cb->disconnectedCb,
(void*)cb->publishedCb,
(void*)cb->dataCb);
#endif
uint16_t crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->connectedCb, 0, 0);
CMD_ResponseEnd(crc);
}
@ -23,9 +28,7 @@ void ICACHE_FLASH_ATTR
cmdMqttTcpDisconnectedCb(uint32_t *args) {
MQTT_Client* client = (MQTT_Client*)args;
MqttCmdCb *cb = (MqttCmdCb*)client->user_data;
#ifdef MQTTCMD_DBG
os_printf("MQTT: TCP Disconnected\n");
#endif
DBG_MQTTCMD("MQTT: TCP Disconnected\n");
uint16_t crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->tcpDisconnectedCb, 0, 0);
CMD_ResponseEnd(crc);
}
@ -34,9 +37,7 @@ void ICACHE_FLASH_ATTR
cmdMqttDisconnectedCb(uint32_t* args) {
MQTT_Client* client = (MQTT_Client*)args;
MqttCmdCb* cb = (MqttCmdCb*)client->user_data;
#ifdef MQTTCMD_DBG
os_printf("MQTT: Disconnected\n");
#endif
DBG_MQTTCMD("MQTT: Disconnected\n");
uint16_t crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->disconnectedCb, 0, 0);
CMD_ResponseEnd(crc);
}
@ -45,9 +46,7 @@ void ICACHE_FLASH_ATTR
cmdMqttPublishedCb(uint32_t* args) {
MQTT_Client* client = (MQTT_Client*)args;
MqttCmdCb* cb = (MqttCmdCb*)client->user_data;
#ifdef MQTTCMD_DBG
os_printf("MQTT: Published\n");
#endif
DBG_MQTTCMD("MQTT: Published\n");
uint16_t crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->publishedCb, 0, 0);
CMD_ResponseEnd(crc);
}
@ -112,7 +111,7 @@ MQTTCMD_Setup(CmdPacket *cmd) {
// 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);
DBG_MQTTCMD("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
@ -164,9 +163,7 @@ MQTTCMD_Lwt(CmdPacket *cmd) {
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
DBG_MQTTCMD("MQTT: MQTTCMD_Lwt client ptr=%p\n", (void*)client_ptr);
uint16_t len;
@ -193,13 +190,11 @@ MQTTCMD_Lwt(CmdPacket *cmd) {
// 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",
DBG_MQTTCMD("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;
}
@ -215,9 +210,7 @@ MQTTCMD_Connect(CmdPacket *cmd) {
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
DBG_MQTTCMD("MQTT: MQTTCMD_Connect client ptr=%p\n", (void*)client_ptr);
uint16_t len;
@ -235,12 +228,10 @@ MQTTCMD_Connect(CmdPacket *cmd) {
// 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",
DBG_MQTTCMD("MQTT: MQTTCMD_Connect host=%s, port=%d, security=%d\n",
client->host,
client->port,
client->security);
#endif
MQTT_Connect(client);
return 1;
@ -258,9 +249,7 @@ MQTTCMD_Disconnect(CmdPacket *cmd) {
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
DBG_MQTTCMD("MQTT: MQTTCMD_Disconnect client ptr=%p\n", (void*)client_ptr);
// disconnect
MQTT_Disconnect(client);
@ -279,9 +268,7 @@ MQTTCMD_Publish(CmdPacket *cmd) {
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
DBG_MQTTCMD("MQTT: MQTTCMD_Publish client ptr=%p\n", (void*)client_ptr);
uint16_t len;
uint8_t *topic, *data;
@ -312,13 +299,11 @@ MQTTCMD_Publish(CmdPacket *cmd) {
// 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",
DBG_MQTTCMD("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);
@ -338,9 +323,7 @@ MQTTCMD_Subscribe(CmdPacket *cmd) {
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
DBG_MQTTCMD("MQTT: MQTTCMD_Subscribe client ptr=%p\n", (void*)client_ptr);
uint16_t len;
uint8_t* topic;
@ -355,9 +338,7 @@ MQTTCMD_Subscribe(CmdPacket *cmd) {
// 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
DBG_MQTTCMD("MQTT: MQTTCMD_Subscribe topic=%s, qos=%ld\n", topic, qos);
MQTT_Subscribe(client, (char*)topic, (uint8_t)qos);
os_free(topic);
return 1;

@ -6,6 +6,13 @@
#include "rest.h"
#include "cmd.h"
#ifdef REST_DBG
#define DBG_REST(format, ...) os_printf(format, ## __VA_ARGS__)
#else
#define DBG_REST(format, ...) do { } while(0)
#endif
// Connection pool for REST clients. Attached MCU's just call REST_setup and this allocates
// a connection, They never call any 'free' and given that the attached MCU could restart at
// any time, we cannot really rely on the attached MCU to call 'free' ever, so better do without.
@ -16,15 +23,6 @@ static RestClient restClient[MAX_REST];
static uint8_t restNum = 0xff; // index into restClient for next slot to allocate
#define REST_CB 0xbeef0000 // fudge added to callback for arduino so we can detect problems
static void ICACHE_FLASH_ATTR
tcpclient_discon_cb(void *arg) {
struct espconn *pespconn = (struct espconn *)arg;
RestClient* client = (RestClient *)pespconn->reverse;
// free the data buffer, if we have one
if (client->data) os_free(client->data);
client->data = 0;
}
// Receive HTTP response - this hacky function assumes that the full response is received in
// one go. Sigh...
static void ICACHE_FLASH_ATTR
@ -73,16 +71,14 @@ tcpclient_recv(void *arg, char *pdata, unsigned short len) {
// collect body and send it
uint16_t crc;
int body_len = len-pi;
#ifdef REST_DBG
os_printf("REST: status=%ld, body=%d\n", code, body_len);
#endif
DBG_REST("REST: status=%ld, body=%d\n", code, body_len);
if (pi == len) {
crc = CMD_ResponseStart(CMD_REST_EVENTS, client->resp_cb, code, 0);
} else {
crc = CMD_ResponseStart(CMD_REST_EVENTS, client->resp_cb, code, 1);
crc = CMD_ResponseBody(crc, (uint8_t*)(pdata+pi), body_len);
CMD_ResponseEnd(crc);
#ifdef REST_DBG
#if 0
os_printf("REST: body=");
for (int j=pi; j<len; j++) os_printf(" %02x", pdata[j]);
os_printf("\n");
@ -100,9 +96,7 @@ static void ICACHE_FLASH_ATTR
tcpclient_sent_cb(void *arg) {
struct espconn *pCon = (struct espconn *)arg;
RestClient* client = (RestClient *)pCon->reverse;
#ifdef REST_DBG
os_printf("REST: Sent\n");
#endif
DBG_REST("REST: Sent\n");
if (client->data_sent != client->data_len) {
// we only sent part of the buffer, send the rest
espconn_sent(client->pCon, (uint8_t*)(client->data+client->data_sent),
@ -115,21 +109,36 @@ tcpclient_sent_cb(void *arg) {
}
}
static void ICACHE_FLASH_ATTR
tcpclient_discon_cb(void *arg) {
struct espconn *pespconn = (struct espconn *)arg;
RestClient* client = (RestClient *)pespconn->reverse;
// free the data buffer, if we have one
if (client->data) os_free(client->data);
client->data = 0;
}
static void ICACHE_FLASH_ATTR
tcpclient_recon_cb(void *arg, sint8 errType) {
struct espconn *pCon = (struct espconn *)arg;
RestClient* client = (RestClient *)pCon->reverse;
os_printf("REST #%d: conn reset, err=%d\n", client-restClient, errType);
// free the data buffer, if we have one
if (client->data) os_free(client->data);
client->data = 0;
}
static void ICACHE_FLASH_ATTR
tcpclient_connect_cb(void *arg) {
struct espconn *pCon = (struct espconn *)arg;
RestClient* client = (RestClient *)pCon->reverse;
#ifdef REST_DBG
os_printf("REST #%d: connected\n", client-restClient);
#endif
DBG_REST("REST #%d: connected\n", client-restClient);
espconn_regist_disconcb(client->pCon, tcpclient_discon_cb);
espconn_regist_recvcb(client->pCon, tcpclient_recv);
espconn_regist_sentcb(client->pCon, tcpclient_sent_cb);
client->data_sent = client->data_len <= 1400 ? client->data_len : 1400;
#ifdef REST_DBG
os_printf("REST #%d: sending %d\n", client-restClient, client->data_sent);
#endif
DBG_REST("REST #%d: sending %d\n", client-restClient, client->data_sent);
//if(client->security){
// espconn_secure_sent(client->pCon, client->data, client->data_sent);
//}
@ -138,33 +147,20 @@ tcpclient_connect_cb(void *arg) {
//}
}
static void ICACHE_FLASH_ATTR
tcpclient_recon_cb(void *arg, sint8 errType) {
struct espconn *pCon = (struct espconn *)arg;
RestClient* client = (RestClient *)pCon->reverse;
#ifdef REST_DBG
os_printf("REST #%d: conn reset\n", client-restClient);
#endif
}
static void ICACHE_FLASH_ATTR
rest_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) {
struct espconn *pConn = (struct espconn *)arg;
RestClient* client = (RestClient *)pConn->reverse;
if(ipaddr == NULL) {
#ifdef REST_DBG
os_printf("REST DNS: Got no ip, try to reconnect\n");
#endif
return;
}
#ifdef REST_DBG
os_printf("REST DNS: found ip %d.%d.%d.%d\n",
DBG_REST("REST DNS: found ip %d.%d.%d.%d\n",
*((uint8 *) &ipaddr->addr),
*((uint8 *) &ipaddr->addr + 1),
*((uint8 *) &ipaddr->addr + 2),
*((uint8 *) &ipaddr->addr + 3));
#endif
if(client->ip.addr == 0 && ipaddr->addr != 0) {
os_memcpy(client->pCon->proto.tcp->remote_ip, &ipaddr->addr, 4);
#ifdef CLIENT_SSL_ENABLE
@ -173,9 +169,7 @@ rest_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) {
} else
#endif
espconn_connect(client->pCon);
#ifdef REST_DBG
os_printf("REST: connecting...\n");
#endif
DBG_REST("REST: connecting...\n");
}
}
@ -228,9 +222,7 @@ REST_Setup(CmdPacket *cmd) {
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
DBG_REST("REST: setup #%d host=%s port=%ld security=%ld\n", clientNum, rest_host, port, security);
client->resp_cb = cmd->callback;
@ -289,9 +281,7 @@ REST_SetHeader(CmdPacket *cmd) {
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
DBG_REST("REST: Set header: %s\r\n", client->header);
break;
case HEADER_CONTENT_TYPE:
if(client->content_type) os_free(client->content_type);
@ -300,9 +290,7 @@ REST_SetHeader(CmdPacket *cmd) {
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
DBG_REST("REST: Set content_type: %s\r\n", client->content_type);
break;
case HEADER_USER_AGENT:
if(client->user_agent) os_free(client->user_agent);
@ -311,9 +299,7 @@ REST_SetHeader(CmdPacket *cmd) {
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
DBG_REST("REST: Set user_agent: %s\r\n", client->user_agent);
break;
}
return 1;
@ -323,36 +309,28 @@ uint32_t ICACHE_FLASH_ATTR
REST_Request(CmdPacket *cmd) {
CmdRequest req;
CMD_Request(&req, cmd);
#ifdef CMDREST_DBG
os_printf("REST: request");
#endif
DBG_REST("REST: request");
// 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
DBG_REST(" #%ld", clientNum);
// 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
DBG_REST(" method=%s", method);
// 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
DBG_REST(" path=%s", path);
// Get HTTP body
uint32_t realLen = 0;
if (CMD_GetArgc(&req) == 3) {
@ -364,9 +342,7 @@ REST_Request(CmdPacket *cmd) {
len = CMD_ArgLen(&req);
if (len > 2048 || realLen > len) goto fail;
}
#ifdef CMDREST_DBG
os_printf(" bodyLen=%ld", realLen);
#endif
DBG_REST(" bodyLen=%ld", realLen);
// 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
@ -381,40 +357,30 @@ REST_Request(CmdPacket *cmd) {
"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
DBG_REST(" hdrLen=%d", headerLen);
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
DBG_REST(" totLen=%ld data=%p", headerLen + realLen, client->data);
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
DBG_REST(" hdrLen=%d", client->data_len);
if (realLen > 0) {
CMD_PopArg(&req, client->data + client->data_len, realLen);
client->data_len += realLen;
}
#ifdef CMDREST_DBG
os_printf("\n");
DBG_REST("\n");
//os_printf("REST request: %s", (char*)client->data);
//DBG_REST("REST request: %s", (char*)client->data);
os_printf("REST: pCon state=%d\n", client->pCon->state);
#endif
DBG_REST("REST: pCon state=%d\n", client->pCon->state);
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
DBG_REST("REST: Connect to ip %s:%ld\n",client->host, client->port);
//if(client->security){
// espconn_secure_connect(client->pCon);
//}
@ -422,17 +388,13 @@ REST_Request(CmdPacket *cmd) {
espconn_connect(client->pCon);
//}
} else {
#ifdef CMDREST_DBG
os_printf("REST: Connect to host %s:%ld\n", client->host, client->port);
#endif
DBG_REST("REST: Connect to host %s:%ld\n", client->host, client->port);
espconn_gethostbyname(client->pCon, (char *)client->host, &client->ip, rest_dns_found);
}
return 1;
fail:
#ifdef CMDREST_DBG
os_printf("\n");
#endif
DBG_REST("\n");
return 0;
}

@ -23,6 +23,12 @@
#include "user_interface.h"
#include "uart.h"
#ifdef UART_DBG
#define DBG_UART(format, ...) os_printf(format, ## __VA_ARGS__)
#else
#define DBG_UART(format, ...) do { } while(0)
#endif
#define recvTaskPrio 1
#define recvTaskQueueLen 64
@ -190,9 +196,7 @@ uart0_rx_intr_handler(void *para)
if (READ_PERI_REG(UART_INT_RAW(uart_no)) & UART_FRM_ERR_INT_RAW) {
uint32 now = system_get_time();
if (last_frm_err == 0 || (now - last_frm_err) > one_sec) {
#ifdef UART_DBG
os_printf("UART framing error (bad baud rate?)\n");
#endif
last_frm_err = now;
}
// clear rx fifo (apparently this is not optional at this point)
@ -208,9 +212,7 @@ uart0_rx_intr_handler(void *para)
if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST)
|| UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST))
{
#ifdef UART_DBG
os_printf("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no));
#endif
//DBG_UART("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no));
ETS_UART_INTR_DISABLE();
system_os_post(recvTaskPrio, 0, 0);
}
@ -233,9 +235,7 @@ uart_recvTask(os_event_t *events)
(length < 128)) {
buf[length++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
}
#ifdef UART_DBG
os_printf("%d ix %d\n", system_get_time(), length);
#endif
//DBG_UART("%d ix %d\n", system_get_time(), length);
for (int i=0; i<MAX_CB; i++) {
if (uart_recv_cb[i] != NULL) (uart_recv_cb[i])(buf, length);
@ -247,9 +247,7 @@ uart_recvTask(os_event_t *events)
void ICACHE_FLASH_ATTR
uart0_baud(int rate) {
#ifdef UART_DBG
os_printf("UART %d baud\n", rate);
#endif
uart_div_modify(UART0, UART_CLK_FREQ / rate);
}
@ -286,9 +284,7 @@ uart_add_recv_cb(UartRecv_cb cb) {
return;
}
}
#ifdef UART_DBG
os_printf("UART: max cb count exceeded\n");
#endif
}
void ICACHE_FLASH_ATTR

Loading…
Cancel
Save