mqtt,rest,uart: use common syntax for DBG macro

pull/95/head
susisstrolch 10 years ago
parent f17ea10687
commit 57011de179
  1. 56
      mqtt/mqtt.c
  2. 52
      rest/rest.c
  3. 8
      serial/uart.c

@ -42,9 +42,9 @@
#include "mqtt.h" #include "mqtt.h"
#ifdef MQTT_DBG #ifdef MQTT_DBG
#define DBG_MQTT(format, ...) os_printf(format, ## __VA_ARGS__) #define DBG(format, ...) os_printf(format, ## __VA_ARGS__)
#else #else
#define DBG_MQTT(format, ...) do { } while(0) #define DBG(format, ...) do { } while(0)
#endif #endif
extern void dumpMem(void *buf, int len); extern void dumpMem(void *buf, int len);
@ -145,7 +145,7 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) {
if (client->connState != MQTT_CONNECTED) { if (client->connState != MQTT_CONNECTED) {
// why are we receiving something?? // why are we receiving something??
DBG_MQTT("MQTT ERROR: recv in invalid state %d\n", client->connState); DBG("MQTT ERROR: recv in invalid state %d\n", client->connState);
mqtt_doAbort(client); mqtt_doAbort(client);
return; return;
} }
@ -157,12 +157,12 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) {
pending_msg_type = mqtt_get_type(client->pending_buffer->data); pending_msg_type = mqtt_get_type(client->pending_buffer->data);
pending_msg_id = mqtt_get_id(client->pending_buffer->data, client->pending_buffer->filled); pending_msg_id = mqtt_get_id(client->pending_buffer->data, client->pending_buffer->filled);
} }
DBG_MQTT("MQTT: Recv type=%s id=%04X len=%d; Pend type=%s id=%02X\n", DBG("MQTT: Recv type=%s id=%04X len=%d; Pend type=%s id=%02X\n",
mqtt_msg_type[msg_type], msg_id, msg_len, mqtt_msg_type[pending_msg_type],pending_msg_id); mqtt_msg_type[msg_type], msg_id, msg_len, mqtt_msg_type[pending_msg_type],pending_msg_id);
switch (msg_type) { switch (msg_type) {
case MQTT_MSG_TYPE_CONNACK: case MQTT_MSG_TYPE_CONNACK:
//DBG_MQTT("MQTT: Connect successful\n"); //DBG("MQTT: Connect successful\n");
// callbacks for internal and external clients // callbacks for internal and external clients
if (client->connectedCb) client->connectedCb((uint32_t*)client); if (client->connectedCb) client->connectedCb((uint32_t*)client);
if (client->cmdConnectedCb) client->cmdConnectedCb((uint32_t*)client); if (client->cmdConnectedCb) client->cmdConnectedCb((uint32_t*)client);
@ -171,28 +171,28 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) {
case MQTT_MSG_TYPE_SUBACK: case MQTT_MSG_TYPE_SUBACK:
if (pending_msg_type == MQTT_MSG_TYPE_SUBSCRIBE && pending_msg_id == msg_id) { if (pending_msg_type == MQTT_MSG_TYPE_SUBSCRIBE && pending_msg_id == msg_id) {
//DBG_MQTT("MQTT: Subscribe successful\n"); //DBG("MQTT: Subscribe successful\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
} }
break; break;
case MQTT_MSG_TYPE_UNSUBACK: case MQTT_MSG_TYPE_UNSUBACK:
if (pending_msg_type == MQTT_MSG_TYPE_UNSUBSCRIBE && pending_msg_id == msg_id) { if (pending_msg_type == MQTT_MSG_TYPE_UNSUBSCRIBE && pending_msg_id == msg_id) {
//DBG_MQTT("MQTT: Unsubscribe successful\n"); //DBG("MQTT: Unsubscribe successful\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
} }
break; break;
case MQTT_MSG_TYPE_PUBACK: // ack for a publish we sent case MQTT_MSG_TYPE_PUBACK: // ack for a publish we sent
if (pending_msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg_id == msg_id) { if (pending_msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg_id == msg_id) {
//DBG_MQTT("MQTT: QoS1 Publish successful\n"); //DBG("MQTT: QoS1 Publish successful\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
} }
break; break;
case MQTT_MSG_TYPE_PUBREC: // rec for a publish we sent case MQTT_MSG_TYPE_PUBREC: // rec for a publish we sent
if (pending_msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg_id == msg_id) { if (pending_msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg_id == msg_id) {
//DBG_MQTT("MQTT: QoS2 publish cont\n"); //DBG("MQTT: QoS2 publish cont\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
// we need to send PUBREL // we need to send PUBREL
mqtt_msg_pubrel(&client->mqtt_connection, msg_id); mqtt_msg_pubrel(&client->mqtt_connection, msg_id);
@ -203,7 +203,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) 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) { if (pending_msg_type == MQTT_MSG_TYPE_PUBREL && pending_msg_id == msg_id) {
//DBG_MQTT("MQTT: QoS2 Publish successful\n"); //DBG("MQTT: QoS2 Publish successful\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
} }
break; break;
@ -229,7 +229,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) 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) { if (pending_msg_type == MQTT_MSG_TYPE_PUBREC && pending_msg_id == msg_id) {
//DBG_MQTT("MQTT: Cont QoS2 recv\n"); //DBG("MQTT: Cont QoS2 recv\n");
client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer);
// we need to send PUBCOMP // we need to send PUBCOMP
mqtt_msg_pubcomp(&client->mqtt_connection, msg_id); mqtt_msg_pubcomp(&client->mqtt_connection, msg_id);
@ -262,16 +262,16 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) {
*/ */
static void ICACHE_FLASH_ATTR static void ICACHE_FLASH_ATTR
mqtt_tcpclient_sent_cb(void* arg) { mqtt_tcpclient_sent_cb(void* arg) {
//DBG_MQTT("MQTT: sent CB\n"); //DBG("MQTT: sent CB\n");
struct espconn* pCon = (struct espconn *)arg; struct espconn* pCon = (struct espconn *)arg;
MQTT_Client* client = (MQTT_Client *)pCon->reverse; MQTT_Client* client = (MQTT_Client *)pCon->reverse;
if (client == NULL) return; // aborted connection ? if (client == NULL) return; // aborted connection ?
//DBG_MQTT("MQTT: Sent\n"); //DBG("MQTT: Sent\n");
// if the message we sent is not a "pending" one, we need to free the buffer // if the message we sent is not a "pending" one, we need to free the buffer
if (client->sending_buffer != NULL) { if (client->sending_buffer != NULL) {
PktBuf *buf = client->sending_buffer; PktBuf *buf = client->sending_buffer;
//DBG_MQTT("PktBuf free %p l=%d\n", buf, buf->filled); //DBG("PktBuf free %p l=%d\n", buf, buf->filled);
os_free(buf); os_free(buf);
client->sending_buffer = NULL; client->sending_buffer = NULL;
} }
@ -290,7 +290,7 @@ mqtt_tcpclient_sent_cb(void* arg) {
static void ICACHE_FLASH_ATTR static void ICACHE_FLASH_ATTR
mqtt_timer(void* arg) { mqtt_timer(void* arg) {
MQTT_Client* client = (MQTT_Client*)arg; MQTT_Client* client = (MQTT_Client*)arg;
//DBG_MQTT("MQTT: timer CB\n"); //DBG("MQTT: timer CB\n");
switch (client->connState) { switch (client->connState) {
default: break; default: break;
@ -314,7 +314,7 @@ mqtt_timer(void* arg) {
// check whether we need to send a keep-alive message // check whether we need to send a keep-alive message
if (client->keepAliveTick > 0 && --client->keepAliveTick == 0) { if (client->keepAliveTick > 0 && --client->keepAliveTick == 0) {
// timeout: we need to send a ping message // timeout: we need to send a ping message
//DBG_MQTT("MQTT: Send keepalive\n"); //DBG("MQTT: Send keepalive\n");
mqtt_msg_pingreq(&client->mqtt_connection); mqtt_msg_pingreq(&client->mqtt_connection);
PktBuf *buf = PktBuf_New(client->mqtt_connection.message.length); PktBuf *buf = PktBuf_New(client->mqtt_connection.message.length);
os_memcpy(buf->data, client->mqtt_connection.message.data, os_memcpy(buf->data, client->mqtt_connection.message.data,
@ -350,13 +350,13 @@ void ICACHE_FLASH_ATTR
mqtt_tcpclient_discon_cb(void* arg) { mqtt_tcpclient_discon_cb(void* arg) {
struct espconn* pespconn = (struct espconn *)arg; struct espconn* pespconn = (struct espconn *)arg;
MQTT_Client* client = (MQTT_Client *)pespconn->reverse; MQTT_Client* client = (MQTT_Client *)pespconn->reverse;
DBG_MQTT("MQTT: Disconnect CB, freeing espconn %p\n", arg); DBG("MQTT: Disconnect CB, freeing espconn %p\n", arg);
if (pespconn->proto.tcp) os_free(pespconn->proto.tcp); if (pespconn->proto.tcp) os_free(pespconn->proto.tcp);
os_free(pespconn); os_free(pespconn);
// if this is an aborted connection we're done // if this is an aborted connection we're done
if (client == NULL) return; if (client == NULL) return;
DBG_MQTT("MQTT: Disconnected from %s:%d\n", client->host, client->port); DBG("MQTT: Disconnected from %s:%d\n", client->host, client->port);
if (client->disconnectedCb) client->disconnectedCb((uint32_t*)client); if (client->disconnectedCb) client->disconnectedCb((uint32_t*)client);
if (client->cmdDisconnectedCb) client->cmdDisconnectedCb((uint32_t*)client); if (client->cmdDisconnectedCb) client->cmdDisconnectedCb((uint32_t*)client);
@ -376,7 +376,7 @@ static void ICACHE_FLASH_ATTR
mqtt_tcpclient_recon_cb(void* arg, int8_t err) { mqtt_tcpclient_recon_cb(void* arg, int8_t err) {
struct espconn* pespconn = (struct espconn *)arg; struct espconn* pespconn = (struct espconn *)arg;
MQTT_Client* client = (MQTT_Client *)pespconn->reverse; MQTT_Client* client = (MQTT_Client *)pespconn->reverse;
//DBG_MQTT("MQTT: Reset CB, freeing espconn %p (err=%d)\n", arg, err); //DBG("MQTT: Reset CB, freeing espconn %p (err=%d)\n", arg, err);
if (pespconn->proto.tcp) os_free(pespconn->proto.tcp); if (pespconn->proto.tcp) os_free(pespconn->proto.tcp);
os_free(pespconn); os_free(pespconn);
os_printf("MQTT: Connection reset from %s:%d\n", client->host, client->port); os_printf("MQTT: Connection reset from %s:%d\n", client->host, client->port);
@ -439,7 +439,7 @@ mqtt_enq_message(MQTT_Client *client, const uint8_t *data, uint16_t len) {
*/ */
static void ICACHE_FLASH_ATTR static void ICACHE_FLASH_ATTR
mqtt_send_message(MQTT_Client* client) { mqtt_send_message(MQTT_Client* client) {
//DBG_MQTT("MQTT: Send_message\n"); //DBG("MQTT: Send_message\n");
PktBuf *buf = client->msgQueue; PktBuf *buf = client->msgQueue;
if (buf == NULL || client->sending) return; // ahem... if (buf == NULL || client->sending) return; // ahem...
client->msgQueue = PktBuf_Shift(client->msgQueue); client->msgQueue = PktBuf_Shift(client->msgQueue);
@ -502,7 +502,7 @@ mqtt_dns_found(const char* name, ip_addr_t* ipaddr, void* arg) {
client->connState = TCP_RECONNECT_REQ; // the timer will kick-off a reconnection client->connState = TCP_RECONNECT_REQ; // the timer will kick-off a reconnection
return; return;
} }
DBG_MQTT("MQTT: ip %d.%d.%d.%d\n", DBG("MQTT: ip %d.%d.%d.%d\n",
*((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr),
*((uint8 *)&ipaddr->addr + 1), *((uint8 *)&ipaddr->addr + 1),
*((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 2),
@ -521,7 +521,7 @@ mqtt_dns_found(const char* name, ip_addr_t* ipaddr, void* arg) {
if (client->reconTimeout < 128) client->reconTimeout <<= 1; if (client->reconTimeout < 128) client->reconTimeout <<= 1;
client->connState = TCP_RECONNECT_REQ; client->connState = TCP_RECONNECT_REQ;
} else { } else {
DBG_MQTT("MQTT: connecting...\n"); DBG("MQTT: connecting...\n");
} }
} }
} }
@ -573,7 +573,7 @@ MQTT_Publish(MQTT_Client* client, const char* topic, const char* data, uint8_t q
os_memcpy(buf->data, msg.message.data, msg.message.length); os_memcpy(buf->data, msg.message.data, msg.message.length);
buf->filled = msg.message.length; buf->filled = msg.message.length;
DBG_MQTT("MQTT: Publish, topic: \"%s\", length: %d\n", topic, msg.message.length); DBG("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); client->msgQueue = PktBuf_Push(client->msgQueue, buf);
@ -597,7 +597,7 @@ MQTT_Subscribe(MQTT_Client* client, char* topic, uint8_t qos) {
os_printf("MQTT ERROR: Queuing Subscribe failed (too long)\n"); os_printf("MQTT ERROR: Queuing Subscribe failed (too long)\n");
return FALSE; return FALSE;
} }
DBG_MQTT("MQTT: Subscribe, topic: \"%s\"\n", topic); DBG("MQTT: Subscribe, topic: \"%s\"\n", topic);
mqtt_enq_message(client, client->mqtt_connection.message.data, mqtt_enq_message(client, client->mqtt_connection.message.data,
client->mqtt_connection.message.length); client->mqtt_connection.message.length);
return TRUE; return TRUE;
@ -626,7 +626,7 @@ void ICACHE_FLASH_ATTR
MQTT_Init(MQTT_Client* client, char* host, uint32 port, uint8_t security, uint8_t sendTimeout, MQTT_Init(MQTT_Client* client, char* host, uint32 port, uint8_t security, uint8_t sendTimeout,
char* client_id, char* client_user, char* client_pass, char* client_id, char* client_user, char* client_pass,
uint8_t keepAliveTime) { uint8_t keepAliveTime) {
DBG_MQTT("MQTT_Init\n"); DBG("MQTT_Init\n");
os_memset(client, 0, sizeof(MQTT_Client)); os_memset(client, 0, sizeof(MQTT_Client));
@ -753,7 +753,7 @@ mqtt_doAbort(MQTT_Client* client) {
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
MQTT_Reconnect(MQTT_Client* client) { MQTT_Reconnect(MQTT_Client* client) {
DBG_MQTT("MQTT: Reconnect requested\n"); DBG("MQTT: Reconnect requested\n");
if (client->connState == MQTT_DISCONNECTED) if (client->connState == MQTT_DISCONNECTED)
MQTT_Connect(client); MQTT_Connect(client);
else if (client->connState == MQTT_CONNECTED) else if (client->connState == MQTT_CONNECTED)
@ -763,7 +763,7 @@ MQTT_Reconnect(MQTT_Client* client) {
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
MQTT_Disconnect(MQTT_Client* client) { MQTT_Disconnect(MQTT_Client* client) {
DBG_MQTT("MQTT: Disconnect requested\n"); DBG("MQTT: Disconnect requested\n");
os_timer_disarm(&client->mqttTimer); os_timer_disarm(&client->mqttTimer);
if (client->connState == MQTT_DISCONNECTED) return; if (client->connState == MQTT_DISCONNECTED) return;
if (client->connState == TCP_RECONNECT_REQ) { if (client->connState == TCP_RECONNECT_REQ) {
@ -778,7 +778,7 @@ MQTT_Disconnect(MQTT_Client* client) {
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
MQTT_Free(MQTT_Client* client) { MQTT_Free(MQTT_Client* client) {
DBG_MQTT("MQTT: Free requested\n"); DBG("MQTT: Free requested\n");
MQTT_Disconnect(client); MQTT_Disconnect(client);
if (client->host) os_free(client->host); if (client->host) os_free(client->host);

@ -7,9 +7,9 @@
#include "cmd.h" #include "cmd.h"
#ifdef REST_DBG #ifdef REST_DBG
#define DBG_REST(format, ...) os_printf(format, ## __VA_ARGS__) #define DBG(format, ...) os_printf(format, ## __VA_ARGS__)
#else #else
#define DBG_REST(format, ...) do { } while(0) #define DBG(format, ...) do { } while(0)
#endif #endif
@ -71,7 +71,7 @@ tcpclient_recv(void *arg, char *pdata, unsigned short len) {
// collect body and send it // collect body and send it
uint16_t crc; uint16_t crc;
int body_len = len-pi; int body_len = len-pi;
DBG_REST("REST: status=%ld, body=%d\n", code, body_len); DBG("REST: status=%ld, body=%d\n", code, body_len);
if (pi == len) { if (pi == len) {
crc = CMD_ResponseStart(CMD_REST_EVENTS, client->resp_cb, code, 0); crc = CMD_ResponseStart(CMD_REST_EVENTS, client->resp_cb, code, 0);
} else { } else {
@ -96,7 +96,7 @@ static void ICACHE_FLASH_ATTR
tcpclient_sent_cb(void *arg) { tcpclient_sent_cb(void *arg) {
struct espconn *pCon = (struct espconn *)arg; struct espconn *pCon = (struct espconn *)arg;
RestClient* client = (RestClient *)pCon->reverse; RestClient* client = (RestClient *)pCon->reverse;
DBG_REST("REST: Sent\n"); DBG("REST: Sent\n");
if (client->data_sent != client->data_len) { if (client->data_sent != client->data_len) {
// we only sent part of the buffer, send the rest // we only sent part of the buffer, send the rest
espconn_sent(client->pCon, (uint8_t*)(client->data+client->data_sent), espconn_sent(client->pCon, (uint8_t*)(client->data+client->data_sent),
@ -132,13 +132,13 @@ static void ICACHE_FLASH_ATTR
tcpclient_connect_cb(void *arg) { tcpclient_connect_cb(void *arg) {
struct espconn *pCon = (struct espconn *)arg; struct espconn *pCon = (struct espconn *)arg;
RestClient* client = (RestClient *)pCon->reverse; RestClient* client = (RestClient *)pCon->reverse;
DBG_REST("REST #%d: connected\n", client-restClient); DBG("REST #%d: connected\n", client-restClient);
espconn_regist_disconcb(client->pCon, tcpclient_discon_cb); espconn_regist_disconcb(client->pCon, tcpclient_discon_cb);
espconn_regist_recvcb(client->pCon, tcpclient_recv); espconn_regist_recvcb(client->pCon, tcpclient_recv);
espconn_regist_sentcb(client->pCon, tcpclient_sent_cb); espconn_regist_sentcb(client->pCon, tcpclient_sent_cb);
client->data_sent = client->data_len <= 1400 ? client->data_len : 1400; client->data_sent = client->data_len <= 1400 ? client->data_len : 1400;
DBG_REST("REST #%d: sending %d\n", client-restClient, client->data_sent); DBG("REST #%d: sending %d\n", client-restClient, client->data_sent);
//if(client->security){ //if(client->security){
// espconn_secure_sent(client->pCon, client->data, client->data_sent); // espconn_secure_sent(client->pCon, client->data, client->data_sent);
//} //}
@ -156,7 +156,7 @@ rest_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) {
os_printf("REST DNS: Got no ip, try to reconnect\n"); os_printf("REST DNS: Got no ip, try to reconnect\n");
return; return;
} }
DBG_REST("REST DNS: found ip %d.%d.%d.%d\n", DBG("REST DNS: found ip %d.%d.%d.%d\n",
*((uint8 *) &ipaddr->addr), *((uint8 *) &ipaddr->addr),
*((uint8 *) &ipaddr->addr + 1), *((uint8 *) &ipaddr->addr + 1),
*((uint8 *) &ipaddr->addr + 2), *((uint8 *) &ipaddr->addr + 2),
@ -169,7 +169,7 @@ rest_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) {
} else } else
#endif #endif
espconn_connect(client->pCon); espconn_connect(client->pCon);
DBG_REST("REST: connecting...\n"); DBG("REST: connecting...\n");
} }
} }
@ -222,7 +222,7 @@ REST_Setup(CmdPacket *cmd) {
os_free(client->pCon); os_free(client->pCon);
} }
os_memset(client, 0, sizeof(RestClient)); os_memset(client, 0, sizeof(RestClient));
DBG_REST("REST: setup #%d host=%s port=%ld security=%ld\n", clientNum, rest_host, port, security); DBG("REST: setup #%d host=%s port=%ld security=%ld\n", clientNum, rest_host, port, security);
client->resp_cb = cmd->callback; client->resp_cb = cmd->callback;
@ -281,7 +281,7 @@ REST_SetHeader(CmdPacket *cmd) {
client->header[len] = '\r'; client->header[len] = '\r';
client->header[len+1] = '\n'; client->header[len+1] = '\n';
client->header[len+2] = 0; client->header[len+2] = 0;
DBG_REST("REST: Set header: %s\r\n", client->header); DBG("REST: Set header: %s\r\n", client->header);
break; break;
case HEADER_CONTENT_TYPE: case HEADER_CONTENT_TYPE:
if(client->content_type) os_free(client->content_type); if(client->content_type) os_free(client->content_type);
@ -290,7 +290,7 @@ REST_SetHeader(CmdPacket *cmd) {
client->content_type[len] = '\r'; client->content_type[len] = '\r';
client->content_type[len+1] = '\n'; client->content_type[len+1] = '\n';
client->content_type[len+2] = 0; client->content_type[len+2] = 0;
DBG_REST("REST: Set content_type: %s\r\n", client->content_type); DBG("REST: Set content_type: %s\r\n", client->content_type);
break; break;
case HEADER_USER_AGENT: case HEADER_USER_AGENT:
if(client->user_agent) os_free(client->user_agent); if(client->user_agent) os_free(client->user_agent);
@ -299,7 +299,7 @@ REST_SetHeader(CmdPacket *cmd) {
client->user_agent[len] = '\r'; client->user_agent[len] = '\r';
client->user_agent[len+1] = '\n'; client->user_agent[len+1] = '\n';
client->user_agent[len+2] = 0; client->user_agent[len+2] = 0;
DBG_REST("REST: Set user_agent: %s\r\n", client->user_agent); DBG("REST: Set user_agent: %s\r\n", client->user_agent);
break; break;
} }
return 1; return 1;
@ -309,28 +309,28 @@ uint32_t ICACHE_FLASH_ATTR
REST_Request(CmdPacket *cmd) { REST_Request(CmdPacket *cmd) {
CmdRequest req; CmdRequest req;
CMD_Request(&req, cmd); CMD_Request(&req, cmd);
DBG_REST("REST: request"); DBG("REST: request");
// Get client // Get client
uint32_t clientNum; uint32_t clientNum;
if (CMD_PopArg(&req, (uint8_t*)&clientNum, 4)) goto fail; if (CMD_PopArg(&req, (uint8_t*)&clientNum, 4)) goto fail;
if ((clientNum & 0xffff0000) != REST_CB) goto fail; if ((clientNum & 0xffff0000) != REST_CB) goto fail;
clientNum &= 0xffff; clientNum &= 0xffff;
RestClient *client = restClient + clientNum % MAX_REST; RestClient *client = restClient + clientNum % MAX_REST;
DBG_REST(" #%ld", clientNum); DBG(" #%ld", clientNum);
// Get HTTP method // Get HTTP method
uint16_t len = CMD_ArgLen(&req); uint16_t len = CMD_ArgLen(&req);
if (len > 15) goto fail; if (len > 15) goto fail;
char method[16]; char method[16];
CMD_PopArg(&req, method, len); CMD_PopArg(&req, method, len);
method[len] = 0; method[len] = 0;
DBG_REST(" method=%s", method); DBG(" method=%s", method);
// Get HTTP path // Get HTTP path
len = CMD_ArgLen(&req); len = CMD_ArgLen(&req);
if (len > 1023) goto fail; if (len > 1023) goto fail;
char path[1024]; char path[1024];
CMD_PopArg(&req, path, len); CMD_PopArg(&req, path, len);
path[len] = 0; path[len] = 0;
DBG_REST(" path=%s", path); DBG(" path=%s", path);
// Get HTTP body // Get HTTP body
uint32_t realLen = 0; uint32_t realLen = 0;
if (CMD_GetArgc(&req) == 3) { if (CMD_GetArgc(&req) == 3) {
@ -342,7 +342,7 @@ REST_Request(CmdPacket *cmd) {
len = CMD_ArgLen(&req); len = CMD_ArgLen(&req);
if (len > 2048 || realLen > len) goto fail; if (len > 2048 || realLen > len) goto fail;
} }
DBG_REST(" bodyLen=%ld", realLen); DBG(" bodyLen=%ld", realLen);
// we need to allocate memory for the header plus the body. First we count the length of the // 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 // header (including some extra counted "%s" and then we add the body length. We allocate the
@ -357,30 +357,30 @@ REST_Request(CmdPacket *cmd) {
"User-Agent: %s\r\n\r\n"; "User-Agent: %s\r\n\r\n";
uint16_t headerLen = strlen(headerFmt) + strlen(method) + strlen(path) + strlen(client->host) + uint16_t headerLen = strlen(headerFmt) + strlen(method) + strlen(path) + strlen(client->host) +
strlen(client->header) + strlen(client->content_type) + strlen(client->user_agent); strlen(client->header) + strlen(client->content_type) + strlen(client->user_agent);
DBG_REST(" hdrLen=%d", headerLen); DBG(" hdrLen=%d", headerLen);
if (client->data) os_free(client->data); if (client->data) os_free(client->data);
client->data = (char*)os_zalloc(headerLen + realLen); client->data = (char*)os_zalloc(headerLen + realLen);
if (client->data == NULL) goto fail; if (client->data == NULL) goto fail;
DBG_REST(" totLen=%ld data=%p", headerLen + realLen, client->data); DBG(" totLen=%ld data=%p", headerLen + realLen, client->data);
client->data_len = os_sprintf((char*)client->data, headerFmt, method, path, client->host, client->data_len = os_sprintf((char*)client->data, headerFmt, method, path, client->host,
client->header, realLen, client->content_type, client->user_agent); client->header, realLen, client->content_type, client->user_agent);
DBG_REST(" hdrLen=%d", client->data_len); DBG(" hdrLen=%d", client->data_len);
if (realLen > 0) { if (realLen > 0) {
CMD_PopArg(&req, client->data + client->data_len, realLen); CMD_PopArg(&req, client->data + client->data_len, realLen);
client->data_len += realLen; client->data_len += realLen;
} }
DBG_REST("\n"); DBG("\n");
//DBG_REST("REST request: %s", (char*)client->data); //DBG("REST request: %s", (char*)client->data);
DBG_REST("REST: pCon state=%d\n", client->pCon->state); DBG("REST: pCon state=%d\n", client->pCon->state);
client->pCon->state = ESPCONN_NONE; client->pCon->state = ESPCONN_NONE;
espconn_regist_connectcb(client->pCon, tcpclient_connect_cb); espconn_regist_connectcb(client->pCon, tcpclient_connect_cb);
espconn_regist_reconcb(client->pCon, tcpclient_recon_cb); espconn_regist_reconcb(client->pCon, tcpclient_recon_cb);
if(UTILS_StrToIP((char *)client->host, &client->pCon->proto.tcp->remote_ip)) { if(UTILS_StrToIP((char *)client->host, &client->pCon->proto.tcp->remote_ip)) {
DBG_REST("REST: Connect to ip %s:%ld\n",client->host, client->port); DBG("REST: Connect to ip %s:%ld\n",client->host, client->port);
//if(client->security){ //if(client->security){
// espconn_secure_connect(client->pCon); // espconn_secure_connect(client->pCon);
//} //}
@ -388,13 +388,13 @@ REST_Request(CmdPacket *cmd) {
espconn_connect(client->pCon); espconn_connect(client->pCon);
//} //}
} else { } else {
DBG_REST("REST: Connect to host %s:%ld\n", client->host, client->port); DBG("REST: Connect to host %s:%ld\n", client->host, client->port);
espconn_gethostbyname(client->pCon, (char *)client->host, &client->ip, rest_dns_found); espconn_gethostbyname(client->pCon, (char *)client->host, &client->ip, rest_dns_found);
} }
return 1; return 1;
fail: fail:
DBG_REST("\n"); DBG("\n");
return 0; return 0;
} }

@ -22,9 +22,9 @@
#include "uart.h" #include "uart.h"
#ifdef UART_DBG #ifdef UART_DBG
#define DBG_UART(format, ...) os_printf(format, ## __VA_ARGS__) #define DBG(format, ...) os_printf(format, ## __VA_ARGS__)
#else #else
#define DBG_UART(format, ...) do { } while(0) #define DBG(format, ...) do { } while(0)
#endif #endif
LOCAL uint8_t uart_recvTaskNum; LOCAL uint8_t uart_recvTaskNum;
@ -203,7 +203,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) 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)) || UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST))
{ {
//DBG_UART("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no)); //DBG("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no));
ETS_UART_INTR_DISABLE(); ETS_UART_INTR_DISABLE();
post_usr_task(uart_recvTaskNum, 0); post_usr_task(uart_recvTaskNum, 0);
} }
@ -226,7 +226,7 @@ uart_recvTask(os_event_t *events)
(length < 128)) { (length < 128)) {
buf[length++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; buf[length++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
} }
//DBG_UART("%d ix %d\n", system_get_time(), length); //DBG("%d ix %d\n", system_get_time(), length);
for (int i=0; i<MAX_CB; i++) { for (int i=0; i<MAX_CB; i++) {
if (uart_recv_cb[i] != NULL) (uart_recv_cb[i])(buf, length); if (uart_recv_cb[i] != NULL) (uart_recv_cb[i])(buf, length);

Loading…
Cancel
Save