Skip to content

Commit

Permalink
massive rework of MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
tve committed Sep 8, 2015
1 parent da7a1b9 commit 9e4cbb0
Show file tree
Hide file tree
Showing 27 changed files with 946 additions and 1,010 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ MODULES = espfs httpd user serial cmd mqtt esp-link
EXTRA_INCDIR = include .

# libraries used in this project, mainly provided by the SDK
LIBS = c gcc hal phy pp net80211 wpa main lwip
LIBS = c gcc hal phy pp net80211 wpa main lwip # crypto ssl

# compiler flags using during compilation of source files
CFLAGS = -Os -ggdb -std=c99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \
Expand Down
2 changes: 1 addition & 1 deletion cmd/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ cmdCallback* ICACHE_FLASH_ATTR
CMD_GetCbByName(char* name) {
char checkname[16];
os_strncpy(checkname, name, sizeof(checkname));
for (uint8_t i = 0; i < sizeof(commands); i++) {
for (uint8_t i = 0; i < MAX_CALLBACKS; i++) {
//os_printf("CMD_GetCbByName: index %d name=%s cb=%p\n", i, callbacks[i].name,
// (void *)callbacks[i].callback);
// if callback doesn't exist or it's null
Expand Down
47 changes: 26 additions & 21 deletions cmd/mqtt_cmd.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <esp8266.h>
#include "mqtt.h"
#include "mqtt_cmd.h"

uint32_t connectedCb = 0, disconnectCb = 0, tcpDisconnectedCb = 0, publishedCb = 0, dataCb = 0;

void ICACHE_FLASH_ATTR
void ICACHE_FLASH_ATTR
cmdMqttConnectedCb(uint32_t* args) {
MQTT_Client* client = (MQTT_Client*)args;
MqttCmdCb* cb = (MqttCmdCb*)client->user_data;
Expand All @@ -24,7 +26,7 @@ cmdMqttTcpDisconnectedCb(uint32_t *args) {
CMD_ResponseEnd(crc);
}

void ICACHE_FLASH_ATTR
void ICACHE_FLASH_ATTR
cmdMqttDisconnectedCb(uint32_t* args) {
MQTT_Client* client = (MQTT_Client*)args;
MqttCmdCb* cb = (MqttCmdCb*)client->user_data;
Expand All @@ -33,7 +35,7 @@ cmdMqttDisconnectedCb(uint32_t* args) {
CMD_ResponseEnd(crc);
}

void ICACHE_FLASH_ATTR
void ICACHE_FLASH_ATTR
cmdMqttPublishedCb(uint32_t* args) {
MQTT_Client* client = (MQTT_Client*)args;
MqttCmdCb* cb = (MqttCmdCb*)client->user_data;
Expand All @@ -42,7 +44,7 @@ cmdMqttPublishedCb(uint32_t* args) {
CMD_ResponseEnd(crc);
}

void ICACHE_FLASH_ATTR
void ICACHE_FLASH_ATTR
cmdMqttDataCb(uint32_t* args, const char* topic, uint32_t topic_len, const char* data, uint32_t data_len) {
uint16_t crc = 0;
MQTT_Client* client = (MQTT_Client*)args;
Expand All @@ -54,7 +56,7 @@ cmdMqttDataCb(uint32_t* args, const char* topic, uint32_t topic_len, const char*
CMD_ResponseEnd(crc);
}

uint32_t ICACHE_FLASH_ATTR
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Setup(CmdPacket *cmd) {
CmdRequest req;
CMD_Request(&req, cmd);
Expand All @@ -65,10 +67,12 @@ MQTTCMD_Setup(CmdPacket *cmd) {
// create mqtt client
uint8_t clientLen = sizeof(MQTT_Client);
MQTT_Client* client = (MQTT_Client*)os_zalloc(clientLen);
if (client == NULL)
return 0;
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;
Expand Down Expand Up @@ -102,7 +106,7 @@ MQTTCMD_Setup(CmdPacket *cmd) {

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);

// init client
// 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);

Expand All @@ -112,11 +116,11 @@ MQTTCMD_Setup(CmdPacket *cmd) {
CMD_PopArg(&req, (uint8_t*)&cb_data, 4);
callback->connectedCb = cb_data;
CMD_PopArg(&req, (uint8_t*)&cb_data, 4);
callback->disconnectedCb = cb_data;
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;
callback->dataCb = cb_data;

client->user_data = callback;

Expand All @@ -136,9 +140,10 @@ MQTTCMD_Setup(CmdPacket *cmd) {
os_free(pass_data);

return (uint32_t)client;
#endif
}

uint32_t ICACHE_FLASH_ATTR
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Lwt(CmdPacket *cmd) {
CmdRequest req;
CMD_Request(&req, cmd);
Expand All @@ -153,7 +158,7 @@ MQTTCMD_Lwt(CmdPacket *cmd) {
os_printf("MQTT: MQTTCMD_Lwt client ptr=%p\n", (void*)client_ptr);

uint16_t len;

// get topic
if (client->connect_info.will_topic)
os_free(client->connect_info.will_topic);
Expand All @@ -174,7 +179,7 @@ MQTTCMD_Lwt(CmdPacket *cmd) {

// 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);

Expand All @@ -186,13 +191,13 @@ MQTTCMD_Lwt(CmdPacket *cmd) {
return 1;
}

uint32_t ICACHE_FLASH_ATTR
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Connect(CmdPacket *cmd) {
CmdRequest req;
CMD_Request(&req, cmd);

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

// get mqtt client
uint32_t client_ptr;
Expand All @@ -202,7 +207,7 @@ MQTTCMD_Connect(CmdPacket *cmd) {

uint16_t len;

// get host
// get host
if (client->host)
os_free(client->host);
len = CMD_ArgLen(&req);
Expand All @@ -217,7 +222,7 @@ MQTTCMD_Connect(CmdPacket *cmd) {
// get security
CMD_PopArg(&req, (uint8_t*)&client->security, 4);

os_printf("MQTT: MQTTCMD_Connect host=%s, port=%ld, security=%d\n",
os_printf("MQTT: MQTTCMD_Connect host=%s, port=%d, security=%d\n",
client->host,
client->port,
client->security);
Expand All @@ -226,7 +231,7 @@ MQTTCMD_Connect(CmdPacket *cmd) {
return 1;
}

uint32_t ICACHE_FLASH_ATTR
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Disconnect(CmdPacket *cmd) {
CmdRequest req;
CMD_Request(&req, cmd);
Expand All @@ -245,7 +250,7 @@ MQTTCMD_Disconnect(CmdPacket *cmd) {
return 1;
}

uint32_t ICACHE_FLASH_ATTR
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Publish(CmdPacket *cmd) {
CmdRequest req;
CMD_Request(&req, cmd);
Expand Down Expand Up @@ -279,7 +284,7 @@ MQTTCMD_Publish(CmdPacket *cmd) {
// TODO: next line not originally present
data[len] = 0;

// get data length
// 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);

Expand All @@ -301,7 +306,7 @@ MQTTCMD_Publish(CmdPacket *cmd) {
return 1;
}

uint32_t ICACHE_FLASH_ATTR
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Subscribe(CmdPacket *cmd) {
CmdRequest req;
CMD_Request(&req, cmd);
Expand Down
1 change: 0 additions & 1 deletion cmd/mqtt_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define MODULES_MQTT_CMD_H_

#include "cmd.h"
#include "mqtt.h"

typedef struct {
uint32_t connectedCb;
Expand Down
12 changes: 7 additions & 5 deletions cmd/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ rest_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) {

if(client->ip.addr == 0 && ipaddr->addr != 0) {
os_memcpy(client->pCon->proto.tcp->remote_ip, &ipaddr->addr, 4);
//if(client->security){
// espconn_secure_connect(client->pCon);
//}
//else {
#ifdef CLIENT_SSL_ENABLE
if(client->security) {
espconn_secure_connect(client->pCon);
} else
#endif
espconn_connect(client->pCon);
//}
os_printf("REST: connecting...\n");
}
}
Expand Down Expand Up @@ -368,6 +368,8 @@ REST_Request(CmdPacket *cmd) {
}
os_printf("\n");

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

os_printf("REST: pCon state=%d\n", client->pCon->state);
client->pCon->state = ESPCONN_NONE;
espconn_regist_connectcb(client->pCon, tcpclient_connect_cb);
Expand Down
46 changes: 23 additions & 23 deletions esp-link/cgipins.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ static const int num_map_func = sizeof(map_func)/sizeof(char*);

// Cgi to return choice of pin assignments
int ICACHE_FLASH_ATTR cgiPinsGet(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted

char buff[2048];
char buff[2048];
int len;

// figure out current mapping
Expand Down Expand Up @@ -62,27 +62,27 @@ int ICACHE_FLASH_ATTR cgiPinsGet(HttpdConnData *connData) {
}
len += os_sprintf(buff+len, "\n] }");

jsonHeader(connData, 200);
httpdSend(connData, buff, len);
return HTTPD_CGI_DONE;
jsonHeader(connData, 200);
httpdSend(connData, buff, len);
return HTTPD_CGI_DONE;
}

// Cgi to change choice of pin assignments
int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) {
if (connData->conn==NULL) {
return HTTPD_CGI_DONE; // Connection aborted
}
if (connData->conn==NULL) {
return HTTPD_CGI_DONE; // Connection aborted
}

char buff[128];
int len = httpdFindArg(connData->getArgs, "map", buff, sizeof(buff));
if (len <= 0) {
jsonHeader(connData, 400);
int len = httpdFindArg(connData->getArgs, "map", buff, sizeof(buff));
if (len <= 0) {
jsonHeader(connData, 400);
return HTTPD_CGI_DONE;
}

int m = atoi(buff);
if (m < 0 || m >= num_map_names) {
jsonHeader(connData, 400);
if (m < 0 || m >= num_map_names) {
jsonHeader(connData, 400);
return HTTPD_CGI_DONE;
}

Expand All @@ -106,17 +106,17 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) {
httpdEndHeaders(connData);
httpdSend(connData, "Failed to save config", -1);
}
return HTTPD_CGI_DONE;
return HTTPD_CGI_DONE;
}

int ICACHE_FLASH_ATTR cgiPins(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (connData->requestType == HTTPD_METHOD_GET) {
return cgiPinsGet(connData);
} else if (connData->requestType == HTTPD_METHOD_POST) {
return cgiPinsSet(connData);
} else {
jsonHeader(connData, 404);
return HTTPD_CGI_DONE;
}
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (connData->requestType == HTTPD_METHOD_GET) {
return cgiPinsGet(connData);
} else if (connData->requestType == HTTPD_METHOD_POST) {
return cgiPinsSet(connData);
} else {
jsonHeader(connData, 404);
return HTTPD_CGI_DONE;
}
}
12 changes: 7 additions & 5 deletions esp-link/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "log.h"
#include <gpio.h>

//#define SHOW_HEAP_USE
#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
Expand Down Expand Up @@ -120,13 +120,16 @@ static char *rst_codes[] = {
# define VERS_STR(V) VERS_STR_STR(V)
char* esp_link_version = VERS_STR(VERSION);

//Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done.
extern void app_init(void);

// Main routine to initialize esp-link.
void user_init(void) {
// get the flash config so we know how to init things
//configWipe(); // uncomment to reset the config for testing purposes
bool restoreOk = configRestore();
// init gpio pin registers
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
Expand Down Expand Up @@ -163,6 +166,5 @@ void user_init(void) {

os_printf("** esp-link ready\n");

// call user_main init
init();
}
app_init();
}
Loading

0 comments on commit 9e4cbb0

Please sign in to comment.