Selaa lähdekoodia

添加mqtt的封装接口

guohui 1 vuosi sitten
vanhempi
commit
0e930a0074
1 muutettua tiedostoa jossa 105 lisäystä ja 0 poistoa
  1. 105 0
      pro/src/Cellular.c

+ 105 - 0
pro/src/Cellular.c

@@ -930,6 +930,111 @@ static int cell_sock_connected(cell_handle_t *h)
 {
     return 0;
 }
+static int cell_mqtt_init(cell_handle_t *h)
+{
+    int i;
+    at_cmd_t *pcmd=NULL;
+
+    for(i=0; i<MQTT_MAX; i++) {
+        h->mq_conn[i].cliId = i;
+    }
+
+    return 0;
+}
+static int cell_mqtt_conn(cell_handle_t *h, mqtt_para_t *para)
+{
+    int r;
+    char tmp[200],buf[100];
+    at_cmd_t *pcmd=NULL;
+    mqtt_conn_t *conn=NULL;
+
+    conn = get_mqtt_conn_free(h);
+    if(!conn) {
+        return -1;
+    }
+    conn_init(h, conn);
+
+    pcmd = data_of(CMD_QMTOPEN);
+    sprintf(tmp, "=%d,\"%s\",%d", conn->cliId, para->host, para->port);
+    r = send_cmd(h, pcmd, tmp, buf, sizeof(buf), 1);
+    if(r!=CELL_OK) {
+        return -1;
+    }
+
+    pcmd = data_of(CMD_QMTCONN);
+    sprintf(tmp, "=%d,\"mqtt%d\",\"%s\",\"%s\"", conn->cliId, conn->cliId, para->username, para->password);
+    r = send_cmd(h, pcmd, tmp, buf, sizeof(buf), 1);
+    if(r!=CELL_OK) {
+        return -1;
+    }
+    conn->h = h;
+    conn->qos = 2;
+    conn->retain = 0;
+
+    return 0;
+}
+static int cell_mqtt_disconn(mqtt_conn_t *conn)
+{
+    int r;
+    char tmp[100],buf[100];
+    at_cmd_t *pcmd=NULL;
+
+    pcmd = data_of(CMD_QMTDISC);
+    sprintf(tmp, "=%d", conn->cliId);
+    r = send_cmd(conn->h, pcmd, tmp, buf, sizeof(buf), 1);
+    if(r!=CELL_OK) {
+        return -1;
+    }
+
+    pcmd = data_of(CMD_QMTCLOSE);
+    r = send_cmd(conn->h, pcmd, tmp, buf, sizeof(buf), 1);
+    if(r!=CELL_OK) {
+        return -1;
+    }
+    conn->msgId = 0;
+    conn->h = NULL;
+
+    return 0;
+}
+static int cell_mqtt_sub(mqtt_conn_t *conn, char *topic)
+{
+    int r;
+    char buf[500];
+    at_cmd_t *pcmd=NULL;
+
+    pcmd = data_of(CMD_QMTSUB);
+    sprintf(buf, "=%d, %d, \"%s\",%d", conn->cliId, conn->msgId, topic, conn->qos);
+    r = send_cmd(conn->h, pcmd, buf, NULL, 0, 1);
+    if(r!=CELL_OK) {
+        return -1;
+    }
+    return 0;
+}
+static int cell_mqtt_pub(mqtt_conn_t *conn, char *topic, void *data, int len)
+{
+    char buf[200],*pd=(char*)data;
+    at_cmd_t *pcmd=data_of(CMD_QMTPUB);
+    int r=0,rt,offset=0,remain_len,send_len;
+
+    sprintf(buf, "=%d,%d,%d,%d,\"%s\",%d", conn->cliId, conn->msgId, conn->qos, conn->retain, topic, len);
+    rt = send_cmd(conn->h, pcmd, NULL, buf, sizeof(buf), 1);
+    if(rt!=CELL_OK) {
+        return -1;
+    }
+
+    while(offset<len) {
+        remain_len = len - offset;
+        send_len = (remain_len>CHUNCK_SIZE)?CHUNCK_SIZE:remain_len;
+
+        rt = send_data(conn->h, data+offset, send_len, 500);
+        if(rt!=CELL_OK) {
+            r = -1; break;
+        }
+        offset += send_len;
+    }
+
+    return 0;
+}
 static int cell_send_sms(cell_handle_t *h, char *number, char *txt)
 {
     int r=-1,rt;