|
@@ -14,7 +14,7 @@
|
|
|
#include "switch_ctrl.h"
|
|
#include "switch_ctrl.h"
|
|
|
#include "sqlite_handle.h"
|
|
#include "sqlite_handle.h"
|
|
|
|
|
|
|
|
-#if 0
|
|
|
|
|
|
|
+#if 1
|
|
|
#define LOGD log_d
|
|
#define LOGD log_d
|
|
|
#define LOGE log_e
|
|
#define LOGE log_e
|
|
|
#define LOGW log_w
|
|
#define LOGW log_w
|
|
@@ -29,8 +29,55 @@
|
|
|
#define SEND_PERIOD 5000 //ms
|
|
#define SEND_PERIOD 5000 //ms
|
|
|
|
|
|
|
|
|
|
|
|
|
-#ifdef USE_MQTT
|
|
|
|
|
|
|
+enum {
|
|
|
|
|
+ SERV_NTP=0,
|
|
|
|
|
+ SERV_SMTP,
|
|
|
|
|
+ SERV_MESG,
|
|
|
|
|
+ SERV_MQTT,
|
|
|
|
|
+ SERV_CLOUD,
|
|
|
|
|
+ SERV_TELNET,
|
|
|
|
|
+ SERV_SNMP_V1,
|
|
|
|
|
+ SERV_SNMP_V2C,
|
|
|
|
|
+ SERV_SNMP_V3,
|
|
|
|
|
+ SERV_SNMP_TRAP,
|
|
|
|
|
+
|
|
|
|
|
+ SERV_MAX
|
|
|
|
|
+};
|
|
|
|
|
+#define BGET(flag,mask) ((flag)&(1<<(mask)))
|
|
|
|
|
+#define BSET(flag,mask) ((flag)|=(1<<(mask)))
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+typedef struct mg_mgr mgr_t;
|
|
|
|
|
+typedef struct mg_timer mg_timer_t;
|
|
|
|
|
+typedef struct mg_mqtt_opts mg_opts_t;
|
|
|
|
|
+typedef struct mg_connection mg_conn_t;
|
|
|
|
|
+
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ int id;
|
|
|
|
|
+ char topic[256];
|
|
|
|
|
+ char content[2048];
|
|
|
|
|
+}mqtt_pkt_t;
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ mgr_t mgr;
|
|
|
|
|
+ mg_conn_t *c;
|
|
|
|
|
+ mqtt_server_t ser;
|
|
|
|
|
+ pthread_t tid;
|
|
|
|
|
+ int quit;
|
|
|
|
|
+
|
|
|
|
|
+ void *h;
|
|
|
|
|
+}mqtt_conn_t;
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ int inited;
|
|
|
|
|
+ uint32_t prod_id;
|
|
|
|
|
+
|
|
|
|
|
+ mqtt_conn_t conn[MQTT_SER_MAX];
|
|
|
|
|
+ handle_t list;
|
|
|
|
|
+ mg_timer_t *timer;
|
|
|
|
|
+
|
|
|
|
|
+ GlobalDeviceManager *dm;
|
|
|
|
|
+}mqtt_handle_t;
|
|
|
|
|
+
|
|
|
|
|
+#ifdef USE_MQTT
|
|
|
char *topic_sub[MQTT_SUB_MAX]={
|
|
char *topic_sub[MQTT_SUB_MAX]={
|
|
|
"/pdu/%d/control/power/all_channel",
|
|
"/pdu/%d/control/power/all_channel",
|
|
|
"/pdu/%d/control/power/+",
|
|
"/pdu/%d/control/power/+",
|
|
@@ -50,60 +97,25 @@ char *topic_pub[MQTT_PUB_MAX]={
|
|
|
"/pdu/%d/alarm/sensor",
|
|
"/pdu/%d/alarm/sensor",
|
|
|
"/pdu/%d/status/service",
|
|
"/pdu/%d/status/service",
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
-typedef struct mg_mgr mgr_t;
|
|
|
|
|
-typedef struct mg_timer mg_timer_t;
|
|
|
|
|
-typedef struct mg_mqtt_opts mg_opts_t;
|
|
|
|
|
-typedef struct mg_connection mg_conn_t;
|
|
|
|
|
-
|
|
|
|
|
-typedef struct {
|
|
|
|
|
- int id;
|
|
|
|
|
- char topic[256];
|
|
|
|
|
- char content[2048];
|
|
|
|
|
-}mqtt_pkt_t;
|
|
|
|
|
-typedef struct _mqtt_conn_t{
|
|
|
|
|
- mg_conn_t *c;
|
|
|
|
|
- mqtt_server_t ser;
|
|
|
|
|
- //mg_opts_t opts;
|
|
|
|
|
- uint8_t flag[MQTT_PUB_MAX];
|
|
|
|
|
-}mqtt_conn_t;
|
|
|
|
|
-typedef struct {
|
|
|
|
|
- int inited;
|
|
|
|
|
- int prod_id;
|
|
|
|
|
- GlobalDeviceManager *dm;
|
|
|
|
|
-
|
|
|
|
|
- mgr_t mgr;
|
|
|
|
|
- mg_opts_t opts;
|
|
|
|
|
- mqtt_conn_t conn[MQTT_SER_MAX];
|
|
|
|
|
-
|
|
|
|
|
- handle_t list;
|
|
|
|
|
- mg_timer_t *timer;
|
|
|
|
|
-}mqtt_handle_t;
|
|
|
|
|
|
|
+const char *serv_str[SERV_MAX]={
|
|
|
|
|
+ "ntp",
|
|
|
|
|
+ "smtp",
|
|
|
|
|
+ "message",
|
|
|
|
|
+ "mqtt",
|
|
|
|
|
+ "cloud",
|
|
|
|
|
+ "telnet",
|
|
|
|
|
+ "snmp_v1",
|
|
|
|
|
+ "snmp_v2c",
|
|
|
|
|
+ "snmp_v3",
|
|
|
|
|
+ "snmp_trap",
|
|
|
|
|
+};
|
|
|
static mqtt_handle_t mqHandle={0};
|
|
static mqtt_handle_t mqHandle={0};
|
|
|
static int my_recv(char *topic, char *data);
|
|
static int my_recv(char *topic, char *data);
|
|
|
static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data);
|
|
static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data);
|
|
|
-static void timer_start(mqtt_handle_t *h);
|
|
|
|
|
-static int send_stat(mqtt_handle_t *h, int type);
|
|
|
|
|
-static uint32_t get_serv(char *json);
|
|
|
|
|
-static void send_once(mqtt_handle_t *h);
|
|
|
|
|
-
|
|
|
|
|
-enum {
|
|
|
|
|
- SERV_NTP=0,
|
|
|
|
|
- SERV_SMTP,
|
|
|
|
|
- SERV_MESG,
|
|
|
|
|
- SERV_MQTT,
|
|
|
|
|
- SERV_CLOUD,
|
|
|
|
|
- SERV_TELNET,
|
|
|
|
|
- SERV_SNMP_V1,
|
|
|
|
|
- SERV_SNMP_V2C,
|
|
|
|
|
- SERV_SNMP_V3,
|
|
|
|
|
- SERV_SNMP_TRAP,
|
|
|
|
|
-
|
|
|
|
|
- SERV_MAX
|
|
|
|
|
-};
|
|
|
|
|
-#define BGET(flag,mask) ((flag)&(1<<(mask)))
|
|
|
|
|
-#define BSET(flag,mask) ((flag)|=(1<<(mask)))
|
|
|
|
|
-
|
|
|
|
|
|
|
+static int send_stat(mqtt_conn_t *conn, int type);
|
|
|
|
|
+static int get_serv(char *json);
|
|
|
|
|
+static void* client_thread(void *arg);
|
|
|
|
|
+/////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static char *get_int_str(int n)
|
|
static char *get_int_str(int n)
|
|
|
{
|
|
{
|
|
@@ -186,32 +198,14 @@ static int is_period(int id)
|
|
|
}
|
|
}
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
-static int my_pub(mqtt_handle_t *h, int id, char *topic, char *data)
|
|
|
|
|
|
|
+static int my_pub(mqtt_conn_t *conn, char *topic, char *data)
|
|
|
{
|
|
{
|
|
|
- int i;
|
|
|
|
|
- for(i=0; i<MQTT_SER_MAX; i++) {
|
|
|
|
|
- if(h->conn[i].c) {
|
|
|
|
|
- if(is_period(id) || !h->conn[i].flag[id]) {
|
|
|
|
|
- pub_one(h->conn[i].c, topic, data);
|
|
|
|
|
- h->conn[i].flag[id] = 1;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if(conn->c) {
|
|
|
|
|
+ pub_one(conn->c, topic, data);
|
|
|
}
|
|
}
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
-static int my_disconn(mqtt_handle_t *h)
|
|
|
|
|
-{
|
|
|
|
|
- int i,r=-1;
|
|
|
|
|
|
|
|
|
|
- for(i=0; i<MQTT_SER_MAX; i++) {
|
|
|
|
|
- if(h->conn[i].c) {
|
|
|
|
|
- mg_mqtt_disconnect(h->conn[i].c, &h->opts);
|
|
|
|
|
- h->conn[i].c = NULL;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return 0;
|
|
|
|
|
-}
|
|
|
|
|
static GlobalPowerManger* get_power(mqtt_handle_t *h, int ch)
|
|
static GlobalPowerManger* get_power(mqtt_handle_t *h, int ch)
|
|
|
{
|
|
{
|
|
|
GlobalPowerManger *tmp=NULL;
|
|
GlobalPowerManger *tmp=NULL;
|
|
@@ -226,12 +220,14 @@ static GlobalPowerManger* get_power(mqtt_handle_t *h, int ch)
|
|
|
}
|
|
}
|
|
|
static int set_ch(mqtt_handle_t *h, int ch, int flag)
|
|
static int set_ch(mqtt_handle_t *h, int ch, int flag)
|
|
|
{
|
|
{
|
|
|
- int r,sendAddr=0;
|
|
|
|
|
|
|
+ int r;
|
|
|
GlobalPowerManger *tmp=NULL;
|
|
GlobalPowerManger *tmp=NULL;
|
|
|
|
|
+
|
|
|
list_for_each_entry(tmp, &h->dm->_globalPowerManger.list, list)
|
|
list_for_each_entry(tmp, &h->dm->_globalPowerManger.list, list)
|
|
|
{
|
|
{
|
|
|
- if(tmp->product_saddr==sendAddr) continue;
|
|
|
|
|
- sendAddr = tmp->product_saddr;
|
|
|
|
|
|
|
+ if(tmp==NULL) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
if(tmp->product_ch_id==ch) {
|
|
if(tmp->product_ch_id==ch) {
|
|
|
r = g_switch_set_all_chn_ctrl(&h->dm->_globalRelaySampManger, tmp, tmp->product_saddr, tmp->product_ch_addr, flag, false);
|
|
r = g_switch_set_all_chn_ctrl(&h->dm->_globalRelaySampManger, tmp, tmp->product_saddr, tmp->product_ch_addr, flag, false);
|
|
@@ -245,6 +241,7 @@ static int set_ch(mqtt_handle_t *h, int ch, int flag)
|
|
|
g_switch_set_all_ctrl(&h->dm->_globalRelaySampManger, tmp->product_ch_type, tmp->product_saddr, flag);
|
|
g_switch_set_all_ctrl(&h->dm->_globalRelaySampManger, tmp->product_ch_type, tmp->product_saddr, flag);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
static GlobalSensorManger* get_sensor(mqtt_handle_t *h, int id)
|
|
static GlobalSensorManger* get_sensor(mqtt_handle_t *h, int id)
|
|
@@ -290,7 +287,66 @@ static int get_url(mqtt_server_t *ser, char *url)
|
|
|
sprintf(url, "%s://%s:%d", head, ser->server, port);
|
|
sprintf(url, "%s://%s:%d", head, ser->server, port);
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
-static int my_conn(mqtt_handle_t *h)
|
|
|
|
|
|
|
+
|
|
|
|
|
+static int start_one(mqtt_conn_t *conn)
|
|
|
|
|
+{
|
|
|
|
|
+ int r;
|
|
|
|
|
+ if(conn->tid==0) {
|
|
|
|
|
+ conn->quit = 0;
|
|
|
|
|
+ r = pthread_create(&conn->tid, NULL, client_thread, conn);
|
|
|
|
|
+ }
|
|
|
|
|
+ return r;
|
|
|
|
|
+}
|
|
|
|
|
+static int stop_one(mqtt_conn_t *conn)
|
|
|
|
|
+{
|
|
|
|
|
+ if(conn->tid) {
|
|
|
|
|
+ conn->quit = 1;
|
|
|
|
|
+ pthread_join(conn->tid, NULL);
|
|
|
|
|
+ conn->tid = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+static int stop_all(mqtt_handle_t *h)
|
|
|
|
|
+{
|
|
|
|
|
+ int i;
|
|
|
|
|
+ for(i=0; i<MQTT_SER_MAX; i++) {
|
|
|
|
|
+ stop_one(&h->conn[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int conn_one(mqtt_conn_t *conn)
|
|
|
|
|
+{
|
|
|
|
|
+ int r=-1;
|
|
|
|
|
+ mqtt_handle_t *h=&mqHandle;
|
|
|
|
|
+
|
|
|
|
|
+ if(!conn->c) {
|
|
|
|
|
+ char url[1024];
|
|
|
|
|
+ mg_opts_t opts={
|
|
|
|
|
+ .clean = true,
|
|
|
|
|
+ .qos = 1,
|
|
|
|
|
+ .version = 4,
|
|
|
|
|
+ .keepalive = 60,
|
|
|
|
|
+ .topic = mg_str("hello"),
|
|
|
|
|
+ .message = mg_str("bye"),
|
|
|
|
|
+ .client_id = mg_str(conn->ser.cid),
|
|
|
|
|
+ .user = mg_str(conn->ser.user),
|
|
|
|
|
+ .pass = mg_str(conn->ser.password),
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ get_url(&conn->ser, url);
|
|
|
|
|
+ conn->c = mg_mqtt_connect(&conn->mgr, url, &opts, mqtt_fn, conn);
|
|
|
|
|
+ if(conn->c) {
|
|
|
|
|
+ sprintf(conn->ser.cid, "smartPDU_%d\n", h->prod_id);
|
|
|
|
|
+ my_sub(conn->c, h->prod_id);
|
|
|
|
|
+ r = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return r;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int my_check(mqtt_handle_t *h)
|
|
|
{
|
|
{
|
|
|
int i,j,r=-1;
|
|
int i,j,r=-1;
|
|
|
char url[1024];
|
|
char url[1024];
|
|
@@ -298,24 +354,16 @@ static int my_conn(mqtt_handle_t *h)
|
|
|
|
|
|
|
|
for(i=0; i<MQTT_SER_MAX; i++) {
|
|
for(i=0; i<MQTT_SER_MAX; i++) {
|
|
|
r = get_url(&ser[i], url);
|
|
r = get_url(&ser[i], url);
|
|
|
- if((r==0) && (!h->conn[i].c)) {
|
|
|
|
|
- mg_opts_t opts={
|
|
|
|
|
- .clean = true,
|
|
|
|
|
- .qos = 1,
|
|
|
|
|
- .version = 4,
|
|
|
|
|
- .keepalive = 60,
|
|
|
|
|
- .topic = mg_str("hello"),
|
|
|
|
|
- .message = mg_str("bye"),
|
|
|
|
|
- .client_id = mg_str(ser[i].cid),
|
|
|
|
|
- .user = mg_str(ser[i].user),
|
|
|
|
|
- .pass = mg_str(ser[i].password),
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- h->conn[i].c = mg_mqtt_connect(&h->mgr, url, &opts, mqtt_fn, &h->conn[i]);
|
|
|
|
|
- if(h->conn[i].c) {
|
|
|
|
|
|
|
+ if((r==0)) {
|
|
|
|
|
+ if(h->conn[i].tid==0) {
|
|
|
h->conn[i].ser = ser[i];
|
|
h->conn[i].ser = ser[i];
|
|
|
- sprintf(h->conn[i].ser.cid, "smartPDU_%d\n", h->prod_id);
|
|
|
|
|
- my_sub(h->conn[i].c, h->prod_id);
|
|
|
|
|
|
|
+ h->conn[i].h = h;
|
|
|
|
|
+ start_one(&h->conn[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ if(h->conn[i].tid) {
|
|
|
|
|
+ stop_one(&h->conn[i]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -323,53 +371,53 @@ static int my_conn(mqtt_handle_t *h)
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static int my_send(mqtt_handle_t *h)
|
|
|
|
|
|
|
+static int my_send(mqtt_conn_t *conn)
|
|
|
{
|
|
{
|
|
|
int r=-1;
|
|
int r=-1;
|
|
|
|
|
+ mqtt_handle_t *h=(mqtt_handle_t*)conn->h;
|
|
|
list_node_t *ln=NULL;
|
|
list_node_t *ln=NULL;
|
|
|
|
|
|
|
|
r = xlist_get_node(h->list, &ln, 0);
|
|
r = xlist_get_node(h->list, &ln, 0);
|
|
|
if(r==0) {
|
|
if(r==0) {
|
|
|
mqtt_pkt_t *pkt=(mqtt_pkt_t*)ln->data.buf;
|
|
mqtt_pkt_t *pkt=(mqtt_pkt_t*)ln->data.buf;
|
|
|
- my_pub(h, pkt->id, pkt->topic, pkt->content);
|
|
|
|
|
|
|
+ my_pub(conn, pkt->topic, pkt->content);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
return r;
|
|
|
}
|
|
}
|
|
|
-static void send_once(mqtt_handle_t *h)
|
|
|
|
|
|
|
+static int send_once(mqtt_conn_t *conn)
|
|
|
{
|
|
{
|
|
|
- send_stat(h, MQTT_PUB_INFO_DEVICE);
|
|
|
|
|
- send_stat(h, MQTT_PUB_STAT_NETWORK);
|
|
|
|
|
- send_stat(h, MQTT_PUB_STAT_SENSOR);
|
|
|
|
|
- send_stat(h, MQTT_PUB_STAT_SERVICE);
|
|
|
|
|
|
|
+ send_stat(conn, MQTT_PUB_INFO_DEVICE);
|
|
|
|
|
+ send_stat(conn, MQTT_PUB_STAT_NETWORK);
|
|
|
|
|
+ send_stat(conn, MQTT_PUB_STAT_SENSOR);
|
|
|
|
|
+ send_stat(conn, MQTT_PUB_STAT_SERVICE);
|
|
|
|
|
+ return 0;
|
|
|
}
|
|
}
|
|
|
-static void send_period(mqtt_handle_t *h)
|
|
|
|
|
|
|
+static void send_period(mqtt_conn_t *conn)
|
|
|
{
|
|
{
|
|
|
- send_stat(h, MQTT_PUB_STAT_POWER_ALL);
|
|
|
|
|
- send_stat(h, MQTT_PUB_STAT_POWER_CHN);
|
|
|
|
|
|
|
+ send_stat(conn, MQTT_PUB_STAT_POWER_ALL);
|
|
|
|
|
+ send_stat(conn, MQTT_PUB_STAT_POWER_CHN);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void timer_conn_fn(void *arg)
|
|
static void timer_conn_fn(void *arg)
|
|
|
{
|
|
{
|
|
|
- mqtt_handle_t *h=(mqtt_handle_t*)arg;
|
|
|
|
|
- my_conn(h);
|
|
|
|
|
|
|
+ mqtt_conn_t *conn=(mqtt_conn_t*)arg;
|
|
|
|
|
+ conn_one(conn);
|
|
|
}
|
|
}
|
|
|
static void timer_period_fn(void *arg)
|
|
static void timer_period_fn(void *arg)
|
|
|
{
|
|
{
|
|
|
- mqtt_handle_t *h=(mqtt_handle_t*)arg;
|
|
|
|
|
- send_period(h);
|
|
|
|
|
|
|
+ mqtt_conn_t *conn=(mqtt_conn_t*)arg;
|
|
|
|
|
+ send_period(conn);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-static void timer_start(mqtt_handle_t *h)
|
|
|
|
|
|
|
+static void timer_add(mqtt_conn_t *conn)
|
|
|
{
|
|
{
|
|
|
- mg_timer_add(&h->mgr, CONN_PERIOD, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_conn_fn, h);
|
|
|
|
|
- mg_timer_add(&h->mgr, SEND_PERIOD, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_period_fn, h);
|
|
|
|
|
|
|
+ mg_timer_add(&conn->mgr, CONN_PERIOD, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_conn_fn, conn);
|
|
|
|
|
+ mg_timer_add(&conn->mgr, SEND_PERIOD, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_period_fn, conn);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
{
|
|
{
|
|
|
- mqtt_handle_t *h=&mqHandle;
|
|
|
|
|
- mqtt_conn_t *mc=((mqtt_conn_t*)(c->fn_data));
|
|
|
|
|
|
|
+ mqtt_conn_t *conn=((mqtt_conn_t*)(c->fn_data));
|
|
|
|
|
|
|
|
switch(ev) {
|
|
switch(ev) {
|
|
|
case MG_EV_OPEN:
|
|
case MG_EV_OPEN:
|
|
@@ -381,10 +429,11 @@ static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
case MG_EV_CONNECT:
|
|
case MG_EV_CONNECT:
|
|
|
{
|
|
{
|
|
|
char url[1024];
|
|
char url[1024];
|
|
|
- get_url(&mc->ser, url);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ get_url(&conn->ser, url);
|
|
|
if (mg_url_is_ssl(url)) {
|
|
if (mg_url_is_ssl(url)) {
|
|
|
- struct mg_tls_opts opts = {.ca = mg_str(mc->ser.cert),
|
|
|
|
|
- .name = mg_url_host(mc->ser.server)};
|
|
|
|
|
|
|
+ struct mg_tls_opts opts = {.ca = mg_str(conn->ser.cert),
|
|
|
|
|
+ .name = mg_url_host(conn->ser.server)};
|
|
|
mg_tls_init(c, &opts);
|
|
mg_tls_init(c, &opts);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -392,13 +441,13 @@ static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
|
|
|
|
|
case MG_EV_MQTT_OPEN:
|
|
case MG_EV_MQTT_OPEN:
|
|
|
{
|
|
{
|
|
|
- send_once(h);
|
|
|
|
|
|
|
+ send_once(conn);
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case MG_EV_POLL:
|
|
case MG_EV_POLL:
|
|
|
{
|
|
{
|
|
|
- my_send(h);
|
|
|
|
|
|
|
+ my_send(conn);
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
@@ -413,32 +462,51 @@ static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
|
|
|
|
|
case MG_EV_ERROR:
|
|
case MG_EV_ERROR:
|
|
|
{
|
|
{
|
|
|
- MG_ERROR(("___MG_EV_ERROR, %p %s", c->fd, (char *) ev_data));
|
|
|
|
|
- memset(mc->flag, 0, sizeof(mc->flag));
|
|
|
|
|
|
|
+ //MG_ERROR(("___MG_EV_ERROR, %p %s", c->fd, (char *) ev_data));
|
|
|
|
|
+ //memset(mc->flag, 0, sizeof(mc->flag));
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case MG_EV_CLOSE:
|
|
case MG_EV_CLOSE:
|
|
|
{
|
|
{
|
|
|
- LOGD("____ MG_EV_CLOSE\n");
|
|
|
|
|
- mc->c = NULL;
|
|
|
|
|
- memset(mc->flag, 0, sizeof(mc->flag));
|
|
|
|
|
|
|
+ conn->c = NULL;
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-static void* mqtt_thread(void *arg)
|
|
|
|
|
|
|
+static void* client_thread(void *arg)
|
|
|
|
|
+{
|
|
|
|
|
+ int r;
|
|
|
|
|
+ mqtt_conn_t *conn=(mqtt_conn_t*)arg;
|
|
|
|
|
+
|
|
|
|
|
+ mg_mgr_init(&conn->mgr);
|
|
|
|
|
+ conn->mgr.dns4.url = "udp://114.114.114.114:53";
|
|
|
|
|
+ //conn->mgr.dns6.url = "udp://114.114.114.114:53";
|
|
|
|
|
+
|
|
|
|
|
+ timer_add(conn);
|
|
|
|
|
+ while(conn->quit==0) {
|
|
|
|
|
+ mg_mgr_poll(&conn->mgr, 1000);
|
|
|
|
|
+ }
|
|
|
|
|
+ mg_mqtt_disconnect(conn->c, NULL);
|
|
|
|
|
+ mg_mgr_free(&conn->mgr);
|
|
|
|
|
+ conn->c = NULL;
|
|
|
|
|
+
|
|
|
|
|
+ pthread_exit(NULL);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+void* mqtt_thread(void *arg)
|
|
|
{
|
|
{
|
|
|
int r;
|
|
int r;
|
|
|
thread_handle_t *th=(thread_handle_t*)arg;
|
|
thread_handle_t *th=(thread_handle_t*)arg;
|
|
|
- mqtt_handle_t *h=(mqtt_handle_t*)th->arg;
|
|
|
|
|
- mqtt_info_t *info=&h->dm->mqttInfo;
|
|
|
|
|
|
|
+ mqtt_handle_t *h=(mqtt_handle_t*)th->arg;
|
|
|
|
|
|
|
|
- timer_start(h);
|
|
|
|
|
while(th->quit==0) {
|
|
while(th->quit==0) {
|
|
|
- mg_mgr_poll(&h->mgr, 2000);
|
|
|
|
|
|
|
+ my_check(h);
|
|
|
|
|
+ sleep(1);
|
|
|
}
|
|
}
|
|
|
- my_disconn(h);
|
|
|
|
|
|
|
+ stop_all(h);
|
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
pthread_exit(NULL);
|
|
|
}
|
|
}
|
|
@@ -454,7 +522,6 @@ int mqtt_init(void)
|
|
|
mqtt_handle_t *h=&mqHandle;
|
|
mqtt_handle_t *h=&mqHandle;
|
|
|
|
|
|
|
|
memset(h, 0, sizeof(mqtt_handle_t));
|
|
memset(h, 0, sizeof(mqtt_handle_t));
|
|
|
- mg_mgr_init(&h->mgr);
|
|
|
|
|
|
|
|
|
|
//mg_log_set(MG_LL_DEBUG);
|
|
//mg_log_set(MG_LL_DEBUG);
|
|
|
|
|
|
|
@@ -462,9 +529,6 @@ int mqtt_init(void)
|
|
|
h->prod_id = h->dm->_globalDevInfo.product.id;
|
|
h->prod_id = h->dm->_globalDevInfo.product.id;
|
|
|
//sys_get_chip_id(&h->prod_id);
|
|
//sys_get_chip_id(&h->prod_id);
|
|
|
|
|
|
|
|
- h->mgr.dns4.url = "udp://114.114.114.114:53";
|
|
|
|
|
- //h->mgr.dns6.url = "udp://114.114.114.114:53";
|
|
|
|
|
-
|
|
|
|
|
list_cfg_t lc;
|
|
list_cfg_t lc;
|
|
|
lc.mode = LIST_FULL_FIFO;
|
|
lc.mode = LIST_FULL_FIFO;
|
|
|
lc.max = 100;
|
|
lc.max = 100;
|
|
@@ -472,8 +536,7 @@ int mqtt_init(void)
|
|
|
h->list = xlist_init(&lc);
|
|
h->list = xlist_init(&lc);
|
|
|
|
|
|
|
|
dev_mqtt_init(h->dm->db, &h->dm->mqttInfo);
|
|
dev_mqtt_init(h->dm->db, &h->dm->mqttInfo);
|
|
|
-
|
|
|
|
|
- thread_start(THREAD_ID_MQTT, mqtt_thread, h, 4*MB, 0);
|
|
|
|
|
|
|
+ thread_start(THREAD_ID_MQTT, h);
|
|
|
h->inited = 1;
|
|
h->inited = 1;
|
|
|
r = 0;
|
|
r = 0;
|
|
|
#endif
|
|
#endif
|
|
@@ -490,7 +553,6 @@ int mqtt_deinit(void)
|
|
|
mqtt_handle_t *h=&mqHandle;
|
|
mqtt_handle_t *h=&mqHandle;
|
|
|
|
|
|
|
|
thread_stop(THREAD_ID_MQTT);
|
|
thread_stop(THREAD_ID_MQTT);
|
|
|
- mg_mgr_free(&h->mgr);
|
|
|
|
|
xlist_free(h->list);
|
|
xlist_free(h->list);
|
|
|
h->inited = 0;
|
|
h->inited = 0;
|
|
|
r = 0;
|
|
r = 0;
|
|
@@ -499,20 +561,23 @@ int mqtt_deinit(void)
|
|
|
return r;
|
|
return r;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#ifdef USE_MQTT
|
|
|
|
|
-static int send_stat(mqtt_handle_t *h, int type)
|
|
|
|
|
|
|
+
|
|
|
|
|
+static int send_stat(mqtt_conn_t *conn, int type)
|
|
|
{
|
|
{
|
|
|
int i,r=-1;
|
|
int i,r=-1;
|
|
|
|
|
+
|
|
|
|
|
+#ifdef USE_MQTT
|
|
|
float p1,p2,p3;
|
|
float p1,p2,p3;
|
|
|
char topic[256];
|
|
char topic[256];
|
|
|
char* content=NULL;
|
|
char* content=NULL;
|
|
|
char buf[20],date[40],time[40];
|
|
char buf[20],date[40],time[40];
|
|
|
|
|
+ mqtt_handle_t *h=(mqtt_handle_t*)conn->h;
|
|
|
|
|
|
|
|
- if(!h->inited || type<0 || type>=MQTT_PUB_MAX) {
|
|
|
|
|
|
|
+ if((!conn->c) || (type<0) || (type>=MQTT_PUB_MAX)) {
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if(type!=MQTT_PUB_STAT_POWER_CHN && type!=MQTT_PUB_STAT_SENSOR) {
|
|
|
|
|
|
|
+ if((type!=MQTT_PUB_STAT_POWER_CHN) && (type!=MQTT_PUB_STAT_SENSOR)) {
|
|
|
snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id);
|
|
snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -533,7 +598,7 @@ static int send_stat(mqtt_handle_t *h, int type)
|
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
|
|
content = cJSON_Print(root);
|
|
content = cJSON_Print(root);
|
|
|
- my_pub(h, type, topic, content);
|
|
|
|
|
|
|
+ my_pub(conn, topic, content);
|
|
|
|
|
|
|
|
cJSON_free(content);
|
|
cJSON_free(content);
|
|
|
cJSON_Delete(root);
|
|
cJSON_Delete(root);
|
|
@@ -592,7 +657,7 @@ static int send_stat(mqtt_handle_t *h, int type)
|
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
|
|
content = cJSON_Print(root);
|
|
content = cJSON_Print(root);
|
|
|
- my_pub(h, type, topic, content);
|
|
|
|
|
|
|
+ my_pub(conn, topic, content);
|
|
|
|
|
|
|
|
cJSON_free(content);
|
|
cJSON_free(content);
|
|
|
cJSON_Delete(root);
|
|
cJSON_Delete(root);
|
|
@@ -633,7 +698,7 @@ static int send_stat(mqtt_handle_t *h, int type)
|
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
|
|
content = cJSON_Print(root);
|
|
content = cJSON_Print(root);
|
|
|
- my_pub(h, type, topic, content);
|
|
|
|
|
|
|
+ my_pub(conn, topic, content);
|
|
|
cJSON_free(content);
|
|
cJSON_free(content);
|
|
|
|
|
|
|
|
cJSON_Delete(root);
|
|
cJSON_Delete(root);
|
|
@@ -680,7 +745,7 @@ static int send_stat(mqtt_handle_t *h, int type)
|
|
|
|
|
|
|
|
snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id, tmp->product_ch_id);
|
|
snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id, tmp->product_ch_id);
|
|
|
content = cJSON_Print(root);
|
|
content = cJSON_Print(root);
|
|
|
- my_pub(h, type, topic, content);
|
|
|
|
|
|
|
+ my_pub(conn, topic, content);
|
|
|
|
|
|
|
|
cJSON_free(content);
|
|
cJSON_free(content);
|
|
|
cJSON_Delete(root);
|
|
cJSON_Delete(root);
|
|
@@ -718,7 +783,7 @@ static int send_stat(mqtt_handle_t *h, int type)
|
|
|
|
|
|
|
|
snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id, tmp->sensor_id);
|
|
snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id, tmp->sensor_id);
|
|
|
content = cJSON_Print(root);
|
|
content = cJSON_Print(root);
|
|
|
- my_pub(h, type, topic, content);
|
|
|
|
|
|
|
+ my_pub(conn, topic, content);
|
|
|
|
|
|
|
|
cJSON_free(content);
|
|
cJSON_free(content);
|
|
|
cJSON_Delete(root);
|
|
cJSON_Delete(root);
|
|
@@ -733,7 +798,7 @@ static int send_stat(mqtt_handle_t *h, int type)
|
|
|
cJSON* root=cJSON_CreateObject();
|
|
cJSON* root=cJSON_CreateObject();
|
|
|
if(root) {
|
|
if(root) {
|
|
|
|
|
|
|
|
- uint32_t flag = get_serv(NULL);
|
|
|
|
|
|
|
+ int flag = get_serv(NULL);
|
|
|
cJSON_AddStringToObject(root,"telnet", BGET(flag, SERV_TELNET)?"1":"0");
|
|
cJSON_AddStringToObject(root,"telnet", BGET(flag, SERV_TELNET)?"1":"0");
|
|
|
cJSON_AddStringToObject(root,"smtp", BGET(flag, SERV_SMTP)?"1":"0");
|
|
cJSON_AddStringToObject(root,"smtp", BGET(flag, SERV_SMTP)?"1":"0");
|
|
|
cJSON_AddStringToObject(root,"snmp_v1", BGET(flag, SERV_SNMP_V1)?"1":"0");
|
|
cJSON_AddStringToObject(root,"snmp_v1", BGET(flag, SERV_SNMP_V1)?"1":"0");
|
|
@@ -750,7 +815,7 @@ static int send_stat(mqtt_handle_t *h, int type)
|
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
|
|
content = cJSON_Print(root);
|
|
content = cJSON_Print(root);
|
|
|
- my_pub(h, type, topic, content);
|
|
|
|
|
|
|
+ my_pub(conn, topic, content);
|
|
|
|
|
|
|
|
cJSON_free(content);
|
|
cJSON_free(content);
|
|
|
cJSON_Delete(root);
|
|
cJSON_Delete(root);
|
|
@@ -761,11 +826,10 @@ static int send_stat(mqtt_handle_t *h, int type)
|
|
|
default:
|
|
default:
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
return r;
|
|
return r;
|
|
|
}
|
|
}
|
|
|
-#endif
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
static int post_alarm(int type, int subtype, char *content, alarm_para_t *para)
|
|
static int post_alarm(int type, int subtype, char *content, alarm_para_t *para)
|
|
|
{
|
|
{
|
|
@@ -838,6 +902,7 @@ int mqtt_post_alarm(int type, int subtype, char *content, alarm_para_t *para)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
#ifdef USE_MQTT
|
|
#ifdef USE_MQTT
|
|
|
//////////////////////////////////////////////////////////
|
|
//////////////////////////////////////////////////////////
|
|
|
static int get_cmd(char *json)
|
|
static int get_cmd(char *json)
|
|
@@ -856,24 +921,12 @@ static int get_cmd(char *json)
|
|
|
|
|
|
|
|
return cmd;
|
|
return cmd;
|
|
|
}
|
|
}
|
|
|
-const char *serv_str[SERV_MAX]={
|
|
|
|
|
- "ntp",
|
|
|
|
|
- "smtp",
|
|
|
|
|
- "message",
|
|
|
|
|
- "mqtt",
|
|
|
|
|
- "cloud",
|
|
|
|
|
- "telnet",
|
|
|
|
|
- "snmp_v1",
|
|
|
|
|
- "snmp_v2c",
|
|
|
|
|
- "snmp_v3",
|
|
|
|
|
- "snmp_trap",
|
|
|
|
|
-};
|
|
|
|
|
|
|
|
|
|
-static uint32_t get_serv(char *json)
|
|
|
|
|
|
|
+
|
|
|
|
|
+static int get_serv(char *json)
|
|
|
{
|
|
{
|
|
|
- int i;
|
|
|
|
|
|
|
+ int i,flag=0;
|
|
|
cJSON* tmp=NULL;
|
|
cJSON* tmp=NULL;
|
|
|
- uint32_t flag=0;
|
|
|
|
|
|
|
|
|
|
if(json) {
|
|
if(json) {
|
|
|
cJSON* cjson=cJSON_Parse(json);
|
|
cJSON* cjson=cJSON_Parse(json);
|
|
@@ -927,7 +980,7 @@ static uint32_t get_serv(char *json)
|
|
|
|
|
|
|
|
return flag;
|
|
return flag;
|
|
|
}
|
|
}
|
|
|
-static int set_serv(uint32_t flag)
|
|
|
|
|
|
|
+static int set_serv(int flag)
|
|
|
{
|
|
{
|
|
|
if(BGET(flag,SERV_NTP)) {
|
|
if(BGET(flag,SERV_NTP)) {
|
|
|
if(!thread_is_running(THREAD_ID_NTP)) {
|
|
if(!thread_is_running(THREAD_ID_NTP)) {
|
|
@@ -997,7 +1050,7 @@ static int my_recv(char *topic, char *data)
|
|
|
|
|
|
|
|
#ifdef USE_MQTT
|
|
#ifdef USE_MQTT
|
|
|
char *p,temp[512];
|
|
char *p,temp[512];
|
|
|
- uint32_t flag=0;
|
|
|
|
|
|
|
+ int flag=0;
|
|
|
mqtt_handle_t *h=&mqHandle;
|
|
mqtt_handle_t *h=&mqHandle;
|
|
|
|
|
|
|
|
for(i=0; i<MQTT_SUB_MAX; i++) {
|
|
for(i=0; i<MQTT_SUB_MAX; i++) {
|
|
@@ -1078,9 +1131,7 @@ static int my_recv(char *topic, char *data)
|
|
|
cmd = get_cmd(data);
|
|
cmd = get_cmd(data);
|
|
|
LOGD("___ MQTT_SUB_CMD_RESET, %d\n", cmd);
|
|
LOGD("___ MQTT_SUB_CMD_RESET, %d\n", cmd);
|
|
|
if(cmd==1) {
|
|
if(cmd==1) {
|
|
|
- //sys_set_factory();
|
|
|
|
|
- char temp[200];
|
|
|
|
|
- sys_get_path(temp, "");
|
|
|
|
|
|
|
+ sys_set_factory();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
@@ -1127,11 +1178,6 @@ int mqtt_test(void)
|
|
|
mInfo.ser[0] = ser;
|
|
mInfo.ser[0] = ser;
|
|
|
h->dm->mqttInfo = mInfo;
|
|
h->dm->mqttInfo = mInfo;
|
|
|
|
|
|
|
|
- my_conn(h);
|
|
|
|
|
- while(1) {
|
|
|
|
|
- send_period(h);
|
|
|
|
|
- sleep(3);
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
#endif
|
|
#endif
|
|
|
return 0;
|
|
return 0;
|