Update revision to use TI's mqtt and Freertos.

Dependencies:   mbed client server

Fork of cc3100_Test_mqtt_CM3 by David Fletcher

Committer:
dflet
Date:
Thu Sep 03 14:02:37 2015 +0000
Revision:
3:a8c249046181
SPI Mode change 1 to 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 3:a8c249046181 1 /******************************************************************************
dflet 3:a8c249046181 2 *
dflet 3:a8c249046181 3 * Copyright (C) 2014 Texas Instruments Incorporated
dflet 3:a8c249046181 4 *
dflet 3:a8c249046181 5 * All rights reserved. Property of Texas Instruments Incorporated.
dflet 3:a8c249046181 6 * Restricted rights to use, duplicate or disclose this code are
dflet 3:a8c249046181 7 * granted through contract.
dflet 3:a8c249046181 8 *
dflet 3:a8c249046181 9 * The program may not be used without the written permission of
dflet 3:a8c249046181 10 * Texas Instruments Incorporated or against the terms and conditions
dflet 3:a8c249046181 11 * stipulated in the agreement under which this program has been supplied,
dflet 3:a8c249046181 12 * and under no circumstances can it be used with non-TI connectivity device.
dflet 3:a8c249046181 13 *
dflet 3:a8c249046181 14 ******************************************************************************/
dflet 3:a8c249046181 15
dflet 3:a8c249046181 16 #include "sl_mqtt_client.h"
dflet 3:a8c249046181 17 #include "cc31xx_sl_net.h"
dflet 3:a8c249046181 18 //#include "cc3100_nonos.h"
dflet 3:a8c249046181 19 #include "myBoardInit.h"
dflet 3:a8c249046181 20 #include "cli_uart.h"
dflet 3:a8c249046181 21 #include "osi.h"
dflet 3:a8c249046181 22
dflet 3:a8c249046181 23 using namespace mbed_cc3100;
dflet 3:a8c249046181 24
dflet 3:a8c249046181 25 #if (THIS_BOARD == MBED_BOARD_LPC1768)
dflet 3:a8c249046181 26 //cc3100 _cc3100module(p9, p10, p8, SPI(p11, p12, p13));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 27 cc3100 _cc3100module(p16, p17, p9, p10, p8, SPI(p5, p6, p7));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 28 #elif (THIS_BOARD == Seeed_Arch_Max)
dflet 3:a8c249046181 29 class cc3100 _cc3100module(PE_5, PE_4, PE_6, SPI(PB_5, PB_4, PB_3));
dflet 3:a8c249046181 30 #elif (THIS_BOARD == ST_MBED_NUCLEOF103)
dflet 3:a8c249046181 31 class cc3100 _cc3100module(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF103 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 32 #elif (THIS_BOARD == ST_MBED_NUCLEOF411)
dflet 3:a8c249046181 33 class cc3100 _cc3100module(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF411 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 34 #elif (THIS_BOARD == ST_MBED_NUCLEOF401)
dflet 3:a8c249046181 35 class cc3100 _cc3100module(PA_8, PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF401 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 36 #elif (THIS_BOARD == EA_MBED_LPC4088)
dflet 3:a8c249046181 37 class cc3100 _cc3100module(p14, p15, p9, p10, p8, SPI(p5, p6, p7));//LPC4088 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 38 #elif (THIS_BOARD == LPCXpresso4337)
dflet 3:a8c249046181 39 class cc3100 _cc3100module(P2_12, P2_9, P2_2, P3_5, P1_2, SPI(P1_4, P1_3, PF_4));//LPCXpresso4337 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 40 #endif
dflet 3:a8c249046181 41
dflet 3:a8c249046181 42 namespace mbed_mqtt {
dflet 3:a8c249046181 43
dflet 3:a8c249046181 44 #ifndef CFG_SL_CL_BUF_LEN
dflet 3:a8c249046181 45 #define BUF_LEN 1024 /*Buffer length*/
dflet 3:a8c249046181 46 #else
dflet 3:a8c249046181 47 #define BUF_LEN CFG_SL_CL_BUF_LEN
dflet 3:a8c249046181 48 #endif
dflet 3:a8c249046181 49
dflet 3:a8c249046181 50 #ifndef CFG_SL_CL_MAX_MQP
dflet 3:a8c249046181 51 #define MAX_MQP 2 /* # of buffers */
dflet 3:a8c249046181 52 #else
dflet 3:a8c249046181 53 #define MAX_MQP CFG_SL_CL_MAX_MQP
dflet 3:a8c249046181 54 #endif
dflet 3:a8c249046181 55
dflet 3:a8c249046181 56 #ifndef CFG_SL_CL_STACK
dflet 3:a8c249046181 57 #define OSI_STACK_SIZE 2048
dflet 3:a8c249046181 58 #else
dflet 3:a8c249046181 59 #define OSI_STACK_SIZE CFG_SL_CL_STACK
dflet 3:a8c249046181 60 #endif
dflet 3:a8c249046181 61 //*****************************************************************************
dflet 3:a8c249046181 62 // global variables used
dflet 3:a8c249046181 63 //*****************************************************************************
dflet 3:a8c249046181 64
dflet 3:a8c249046181 65
dflet 3:a8c249046181 66 struct sl_client_ctx {
dflet 3:a8c249046181 67 /* Client information details */
dflet 3:a8c249046181 68 char *client_id;
dflet 3:a8c249046181 69 char *usr_name;
dflet 3:a8c249046181 70 char *usr_pwd;
dflet 3:a8c249046181 71 SlMqttWill_t mqtt_will;
dflet 3:a8c249046181 72
dflet 3:a8c249046181 73 /* Client management variables */
dflet 3:a8c249046181 74 bool in_use;
dflet 3:a8c249046181 75 char awaited_ack;
dflet 3:a8c249046181 76 uint16_t conn_ack;
dflet 3:a8c249046181 77
dflet 3:a8c249046181 78 /*Sync Object used to ensure single inflight message -
dflet 3:a8c249046181 79 used in blocking mode to wait on ack*/
dflet 3:a8c249046181 80 _SlSyncObj_t ack_sync_obj;
dflet 3:a8c249046181 81 /* Suback QoS pointer to store passed by application */
dflet 3:a8c249046181 82 char *suback_qos;
dflet 3:a8c249046181 83
dflet 3:a8c249046181 84 /* Application information */
dflet 3:a8c249046181 85 void* app_hndl;
dflet 3:a8c249046181 86 SlMqttClientCbs_t app_cbs;
dflet 3:a8c249046181 87
dflet 3:a8c249046181 88 /* Library information */
dflet 3:a8c249046181 89 void *cli_hndl;
dflet 3:a8c249046181 90 bool blocking_send;
dflet 3:a8c249046181 91 };
dflet 3:a8c249046181 92
dflet 3:a8c249046181 93 #ifndef CFG_CL_MQTT_CTXS
dflet 3:a8c249046181 94 #define MAX_SIMULTANEOUS_SERVER_CONN 4
dflet 3:a8c249046181 95 #else
dflet 3:a8c249046181 96 #define MAX_SIMULTANEOUS_SERVER_CONN CFG_MQTT_CL_CTXS
dflet 3:a8c249046181 97 #endif
dflet 3:a8c249046181 98
dflet 3:a8c249046181 99
dflet 3:a8c249046181 100 static struct sl_client_ctx sl_cli_ctx[MAX_SIMULTANEOUS_SERVER_CONN];
dflet 3:a8c249046181 101
dflet 3:a8c249046181 102 /* Lock Object to be passed to the MQTT lib */
dflet 3:a8c249046181 103 _SlLockObj_t mqtt_lib_lockobj;
dflet 3:a8c249046181 104
dflet 3:a8c249046181 105 /* Creating a pool of MQTT coonstructs that can be used by the MQTT Lib
dflet 3:a8c249046181 106 mqp_vec =>pointer to a pool of the mqtt packet constructs
dflet 3:a8c249046181 107 buf_vec => the buffer area which is attached with each of mqp_vec*/
dflet 3:a8c249046181 108 DEFINE_MQP_BUF_VEC(MAX_MQP, mqp_vec, BUF_LEN, buf_vec);
dflet 3:a8c249046181 109
dflet 3:a8c249046181 110 /*Task Priority and Response Time*/
dflet 3:a8c249046181 111 uint32_t g_wait_secs;
dflet 3:a8c249046181 112
dflet 3:a8c249046181 113 bool g_multi_srvr_conn = false;
dflet 3:a8c249046181 114
dflet 3:a8c249046181 115 /* Receive task handle */
dflet 3:a8c249046181 116 OsiTaskHandle g_rx_task_hndl;
dflet 3:a8c249046181 117
dflet 3:a8c249046181 118 /* Synchronization object used between the Rx and App task */
dflet 3:a8c249046181 119 _SlSyncObj_t g_rx_tx_sync_obj;
dflet 3:a8c249046181 120
dflet 3:a8c249046181 121 #define RX_TX_SIGNAL_WAIT() _cc3100module._nonos.sl_SyncObjWait(&g_rx_tx_sync_obj, NON_OS_SYNC_OBJ_SIGNAL_VALUE, NON_OS_SYNC_OBJ_CLEAR_VALUE, SL_OS_WAIT_FOREVER)
dflet 3:a8c249046181 122 #define RX_TX_SIGNAL_POST() _cc3100module._nonos.sl_SyncObjSignal(&g_rx_tx_sync_obj, NON_OS_SYNC_OBJ_SIGNAL_VALUE)
dflet 3:a8c249046181 123
dflet 3:a8c249046181 124 /* MQTT Quality of Service */
dflet 3:a8c249046181 125 static const enum mqtt_qos qos[] ={
dflet 3:a8c249046181 126 MQTT_QOS0,
dflet 3:a8c249046181 127 MQTT_QOS1,
dflet 3:a8c249046181 128 MQTT_QOS2
dflet 3:a8c249046181 129 };
dflet 3:a8c249046181 130
dflet 3:a8c249046181 131 /* Network Services specific to cc3200*/
dflet 3:a8c249046181 132 struct device_net_services net={&comm_open,&tcp_send,&tcp_recv,&send_dest,&recv_from,&comm_close,
dflet 3:a8c249046181 133 &tcp_listen,&tcp_accept,&tcp_select,&rtc_secs};
dflet 3:a8c249046181 134
dflet 3:a8c249046181 135 /* Defining Retain Flag. Getting retain flag bit from fixed header*/
dflet 3:a8c249046181 136 #define MQP_PUB_RETAIN(mqp) ( ((mqp->fh_byte1 ) & 0x01)?true:false )
dflet 3:a8c249046181 137
dflet 3:a8c249046181 138 /*Defining Duplicate flag. Getting Duplicate flag bit from fixed header*/
dflet 3:a8c249046181 139 #define MQP_PUB_DUP(mqp) (((mqp->fh_byte1>>3) & 0x01)?true:false )
dflet 3:a8c249046181 140
dflet 3:a8c249046181 141 /*Defining QOS value. Getting QOS value from fixed header*/
dflet 3:a8c249046181 142 #define MQP_PUB_QOS(mqp) ((mqp->fh_byte1 >>1) & 0x03)
dflet 3:a8c249046181 143
dflet 3:a8c249046181 144 #define ACK_RX_SIGNAL_WAIT(ack_sync_obj) _cc3100module._nonos.sl_SyncObjWait(ack_sync_obj, NON_OS_SYNC_OBJ_SIGNAL_VALUE, NON_OS_SYNC_OBJ_CLEAR_VALUE, SL_OS_WAIT_FOREVER)
dflet 3:a8c249046181 145 #define ACK_RX_SIGNAL_POST(ack_sync_obj) _cc3100module._nonos.sl_SyncObjSignal(ack_sync_obj, NON_OS_SYNC_OBJ_SIGNAL_VALUE)
dflet 3:a8c249046181 146
dflet 3:a8c249046181 147 #define STR2UTF_CONV(utf_s, str) {utf_s.buffer = (char *)str; utf_s.length = strlen(str);}
dflet 3:a8c249046181 148
dflet 3:a8c249046181 149 /*Defining Event Messages*/
dflet 3:a8c249046181 150 #define MQTT_ACK "Ack Received from server"
dflet 3:a8c249046181 151 #define MQTT_ERROR "Connection Lost with broker"
dflet 3:a8c249046181 152
dflet 3:a8c249046181 153 //*****************************************************************************
dflet 3:a8c249046181 154 // process_notify_ack_cb
dflet 3:a8c249046181 155 //*****************************************************************************
dflet 3:a8c249046181 156 static void
dflet 3:a8c249046181 157 process_notify_ack_cb(void *app, uint8_t msg_type, uint16_t msg_id, uint8_t *buf, uint32_t len)
dflet 3:a8c249046181 158 {
dflet 3:a8c249046181 159
dflet 3:a8c249046181 160 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)app;
dflet 3:a8c249046181 161 int32_t loopcnt;
dflet 3:a8c249046181 162
dflet 3:a8c249046181 163 switch(msg_type) {
dflet 3:a8c249046181 164
dflet 3:a8c249046181 165 case MQTT_CONNACK:
dflet 3:a8c249046181 166 client_ctx->conn_ack = ((buf[0] << 8) | buf[1]);
dflet 3:a8c249046181 167 client_ctx->awaited_ack = 0;
dflet 3:a8c249046181 168 ACK_RX_SIGNAL_POST(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 169 break;
dflet 3:a8c249046181 170 case MQTT_SUBACK:
dflet 3:a8c249046181 171 if(true == client_ctx->blocking_send) {
dflet 3:a8c249046181 172 for(loopcnt = 0; loopcnt < len; loopcnt++) {
dflet 3:a8c249046181 173 client_ctx->suback_qos[loopcnt] = buf[loopcnt];
dflet 3:a8c249046181 174 }
dflet 3:a8c249046181 175
dflet 3:a8c249046181 176 if(client_ctx->awaited_ack == msg_type) {
dflet 3:a8c249046181 177 client_ctx->awaited_ack = 0;
dflet 3:a8c249046181 178 ACK_RX_SIGNAL_POST(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 179 }
dflet 3:a8c249046181 180 } else {
dflet 3:a8c249046181 181 client_ctx->app_cbs.sl_ExtLib_MqttEvent(
dflet 3:a8c249046181 182 client_ctx->app_hndl,
dflet 3:a8c249046181 183 SL_MQTT_CL_EVT_SUBACK, buf, len);
dflet 3:a8c249046181 184 }
dflet 3:a8c249046181 185 break;
dflet 3:a8c249046181 186 /* Error returns --- TBD */
dflet 3:a8c249046181 187 default:
dflet 3:a8c249046181 188 if(true == client_ctx->blocking_send) {
dflet 3:a8c249046181 189 if(client_ctx->awaited_ack == msg_type) {
dflet 3:a8c249046181 190 client_ctx->awaited_ack = 0;
dflet 3:a8c249046181 191 ACK_RX_SIGNAL_POST(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 192 }
dflet 3:a8c249046181 193 } else {
dflet 3:a8c249046181 194 client_ctx->app_cbs.sl_ExtLib_MqttEvent(
dflet 3:a8c249046181 195 client_ctx->app_hndl,
dflet 3:a8c249046181 196 msg_type, MQTT_ACK, strlen(MQTT_ACK));
dflet 3:a8c249046181 197 }
dflet 3:a8c249046181 198 break;
dflet 3:a8c249046181 199
dflet 3:a8c249046181 200 }
dflet 3:a8c249046181 201
dflet 3:a8c249046181 202 return ;
dflet 3:a8c249046181 203 }
dflet 3:a8c249046181 204
dflet 3:a8c249046181 205 //*****************************************************************************
dflet 3:a8c249046181 206 // process_publish_rx_cb
dflet 3:a8c249046181 207 //*****************************************************************************
dflet 3:a8c249046181 208 static bool
dflet 3:a8c249046181 209 process_publish_rx_cb(void *app, bool dup, enum mqtt_qos qos, bool retain,
dflet 3:a8c249046181 210 struct mqtt_packet *mqp)
dflet 3:a8c249046181 211 {
dflet 3:a8c249046181 212 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)app;
dflet 3:a8c249046181 213 /*If incoming message is a publish message from broker */
dflet 3:a8c249046181 214 /* Invokes the event handler with topic,topic length,payload and
dflet 3:a8c249046181 215 payload length values*/
dflet 3:a8c249046181 216 client_ctx->app_cbs.sl_ExtLib_MqttRecv(client_ctx->app_hndl,
dflet 3:a8c249046181 217 (char const*)MQP_PUB_TOP_BUF(mqp),
dflet 3:a8c249046181 218 MQP_PUB_TOP_LEN(mqp),MQP_PUB_PAY_BUF(mqp),
dflet 3:a8c249046181 219 MQP_PUB_PAY_LEN(mqp), dup,
dflet 3:a8c249046181 220 MQP_PUB_QOS(mqp), retain);
dflet 3:a8c249046181 221 return true;
dflet 3:a8c249046181 222 }
dflet 3:a8c249046181 223
dflet 3:a8c249046181 224 //*****************************************************************************
dflet 3:a8c249046181 225 // process_disconn_cb
dflet 3:a8c249046181 226 //*****************************************************************************
dflet 3:a8c249046181 227 static void process_disconn_cb(void *app, int32_t cause)
dflet 3:a8c249046181 228 {
dflet 3:a8c249046181 229 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)app;
dflet 3:a8c249046181 230
dflet 3:a8c249046181 231 if(client_ctx->awaited_ack != 0) {
dflet 3:a8c249046181 232 client_ctx->awaited_ack = MQTT_DISCONNECT;
dflet 3:a8c249046181 233 ACK_RX_SIGNAL_POST(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 234 } else {
dflet 3:a8c249046181 235 if(false == client_ctx->blocking_send) {
dflet 3:a8c249046181 236 /* Invoke the disconnect callback */
dflet 3:a8c249046181 237 client_ctx->app_cbs.sl_ExtLib_MqttDisconn(
dflet 3:a8c249046181 238 client_ctx->app_hndl);
dflet 3:a8c249046181 239 }
dflet 3:a8c249046181 240 }
dflet 3:a8c249046181 241 return;
dflet 3:a8c249046181 242 }
dflet 3:a8c249046181 243
dflet 3:a8c249046181 244
dflet 3:a8c249046181 245 //*****************************************************************************
dflet 3:a8c249046181 246 // Receive Task. Invokes in the context of a task
dflet 3:a8c249046181 247 //*****************************************************************************
dflet 3:a8c249046181 248 void
dflet 3:a8c249046181 249 VMqttRecvTask(void *pArgs)
dflet 3:a8c249046181 250 {
dflet 3:a8c249046181 251
dflet 3:a8c249046181 252 int32_t retval;
dflet 3:a8c249046181 253 do {
dflet 3:a8c249046181 254 if(false == g_multi_srvr_conn) {
dflet 3:a8c249046181 255 /* wait on broker connection */
dflet 3:a8c249046181 256 RX_TX_SIGNAL_WAIT();
dflet 3:a8c249046181 257 while(1) {
dflet 3:a8c249046181 258 /* Forcing to use the index 0 always - caution */
dflet 3:a8c249046181 259 retval = mqtt_client_ctx_run(
dflet 3:a8c249046181 260 sl_cli_ctx[0].cli_hndl,
dflet 3:a8c249046181 261 g_wait_secs);
dflet 3:a8c249046181 262 if((retval < 0) &&
dflet 3:a8c249046181 263 (retval != MQP_ERR_TIMEOUT)) {
dflet 3:a8c249046181 264 break;
dflet 3:a8c249046181 265 }
dflet 3:a8c249046181 266 }
dflet 3:a8c249046181 267 } else {
dflet 3:a8c249046181 268 mqtt_client_run(g_wait_secs);
dflet 3:a8c249046181 269 }
dflet 3:a8c249046181 270 }while(1);
dflet 3:a8c249046181 271 }
dflet 3:a8c249046181 272
dflet 3:a8c249046181 273 static struct sl_client_ctx *get_available_clictx_mem()
dflet 3:a8c249046181 274 {
dflet 3:a8c249046181 275 int32_t loopcnt, max_store;
dflet 3:a8c249046181 276
dflet 3:a8c249046181 277 if(false == g_multi_srvr_conn) {
dflet 3:a8c249046181 278 max_store = 1;
dflet 3:a8c249046181 279 } else {
dflet 3:a8c249046181 280 max_store = MAX_SIMULTANEOUS_SERVER_CONN;
dflet 3:a8c249046181 281 }
dflet 3:a8c249046181 282
dflet 3:a8c249046181 283 for(loopcnt = 0; loopcnt < max_store; loopcnt++) {
dflet 3:a8c249046181 284 if(false == sl_cli_ctx[loopcnt].in_use) {
dflet 3:a8c249046181 285 sl_cli_ctx[loopcnt].in_use = true;
dflet 3:a8c249046181 286 return(&(sl_cli_ctx[loopcnt]));
dflet 3:a8c249046181 287 }
dflet 3:a8c249046181 288 }
dflet 3:a8c249046181 289
dflet 3:a8c249046181 290 return NULL;
dflet 3:a8c249046181 291 }
dflet 3:a8c249046181 292
dflet 3:a8c249046181 293 //*****************************************************************************
dflet 3:a8c249046181 294 // sl_ExtLib_MqttClientCtxCreate
dflet 3:a8c249046181 295 //*****************************************************************************
dflet 3:a8c249046181 296 void *sl_ExtLib_MqttClientCtxCreate(const SlMqttClientCtxCfg_t *ctx_cfg,
dflet 3:a8c249046181 297 const SlMqttClientCbs_t *msg_cbs,
dflet 3:a8c249046181 298 void *app_hndl)
dflet 3:a8c249046181 299 {
dflet 3:a8c249046181 300
dflet 3:a8c249046181 301 struct sl_client_ctx *client_ctx_ptr;
dflet 3:a8c249046181 302 struct mqtt_client_ctx_cfg lib_ctx_cfg;
dflet 3:a8c249046181 303 struct mqtt_client_ctx_cbs lib_cli_cbs;
dflet 3:a8c249046181 304 struct secure_conn lib_nw_security;
dflet 3:a8c249046181 305 int32_t retval;
dflet 3:a8c249046181 306
dflet 3:a8c249046181 307 /* Get a client context storage area */
dflet 3:a8c249046181 308 client_ctx_ptr = get_available_clictx_mem();
dflet 3:a8c249046181 309 if(client_ctx_ptr == NULL) {
dflet 3:a8c249046181 310 return NULL;
dflet 3:a8c249046181 311 }
dflet 3:a8c249046181 312
dflet 3:a8c249046181 313 /* Create the sync object to signal on arrival of ACK packet */
dflet 3:a8c249046181 314 _cc3100module._nonos.sl_SyncObjCreate(&client_ctx_ptr->ack_sync_obj, "AckSyncObject");
dflet 3:a8c249046181 315 _cc3100module._nonos.sl_SyncObjWait(&client_ctx_ptr->ack_sync_obj, NON_OS_SYNC_OBJ_SIGNAL_VALUE, NON_OS_SYNC_OBJ_CLEAR_VALUE, SL_OS_NO_WAIT);
dflet 3:a8c249046181 316
dflet 3:a8c249046181 317 /* Store the application handle */
dflet 3:a8c249046181 318 client_ctx_ptr->app_hndl = app_hndl;
dflet 3:a8c249046181 319 /* Initialize the ACK awaited */
dflet 3:a8c249046181 320 client_ctx_ptr->awaited_ack = 0;
dflet 3:a8c249046181 321
dflet 3:a8c249046181 322 /* Store the application callbacks, to be invoked later */
dflet 3:a8c249046181 323 client_ctx_ptr->app_cbs.sl_ExtLib_MqttRecv = \
dflet 3:a8c249046181 324 msg_cbs->sl_ExtLib_MqttRecv;
dflet 3:a8c249046181 325 client_ctx_ptr->app_cbs.sl_ExtLib_MqttEvent = \
dflet 3:a8c249046181 326 msg_cbs->sl_ExtLib_MqttEvent;
dflet 3:a8c249046181 327 client_ctx_ptr->app_cbs.sl_ExtLib_MqttDisconn = \
dflet 3:a8c249046181 328 msg_cbs->sl_ExtLib_MqttDisconn;
dflet 3:a8c249046181 329
dflet 3:a8c249046181 330 /* Initialize the client lib */
dflet 3:a8c249046181 331 lib_ctx_cfg.config_opts = ctx_cfg->mqtt_mode31;
dflet 3:a8c249046181 332
dflet 3:a8c249046181 333 if(true == g_multi_srvr_conn) {
dflet 3:a8c249046181 334 lib_ctx_cfg.config_opts |=
dflet 3:a8c249046181 335 (MQTT_CFG_APP_HAS_RTSK | MQTT_CFG_MK_GROUP_CTX);
dflet 3:a8c249046181 336 } else {
dflet 3:a8c249046181 337 lib_ctx_cfg.config_opts |= MQTT_CFG_APP_HAS_RTSK;
dflet 3:a8c249046181 338 }
dflet 3:a8c249046181 339
dflet 3:a8c249046181 340 /* get the network connection options */
dflet 3:a8c249046181 341 client_ctx_ptr->blocking_send = ctx_cfg->blocking_send;
dflet 3:a8c249046181 342 lib_ctx_cfg.nwconn_opts = ctx_cfg->server_info.netconn_flags;
dflet 3:a8c249046181 343 lib_ctx_cfg.nwconn_opts |= DEV_NETCONN_OPT_TCP;
dflet 3:a8c249046181 344 lib_ctx_cfg.server_addr = (char*)ctx_cfg->server_info.server_addr;
dflet 3:a8c249046181 345 lib_ctx_cfg.port_number = ctx_cfg->server_info.port_number;
dflet 3:a8c249046181 346 /*initialize secure socket parameters */
dflet 3:a8c249046181 347 if(ctx_cfg->server_info.netconn_flags & SL_MQTT_NETCONN_SEC) {
dflet 3:a8c249046181 348 lib_ctx_cfg.nw_security = &lib_nw_security;
dflet 3:a8c249046181 349 /* initialize secure socket parameters */
dflet 3:a8c249046181 350 lib_ctx_cfg.nw_security->cipher =
dflet 3:a8c249046181 351 (void*)&(ctx_cfg->server_info.cipher);
dflet 3:a8c249046181 352 lib_ctx_cfg.nw_security->method =
dflet 3:a8c249046181 353 (void*)&(ctx_cfg->server_info.method);
dflet 3:a8c249046181 354 lib_ctx_cfg.nw_security->n_file =
dflet 3:a8c249046181 355 ctx_cfg->server_info.n_files;
dflet 3:a8c249046181 356 lib_ctx_cfg.nw_security->files =
dflet 3:a8c249046181 357 (char**)ctx_cfg->server_info.secure_files;
dflet 3:a8c249046181 358 }
dflet 3:a8c249046181 359 else {
dflet 3:a8c249046181 360 lib_ctx_cfg.nw_security=NULL;
dflet 3:a8c249046181 361 }
dflet 3:a8c249046181 362
dflet 3:a8c249046181 363 lib_cli_cbs.publish_rx = process_publish_rx_cb;
dflet 3:a8c249046181 364 lib_cli_cbs.ack_notify = process_notify_ack_cb;
dflet 3:a8c249046181 365 lib_cli_cbs.disconn_cb = process_disconn_cb;
dflet 3:a8c249046181 366
dflet 3:a8c249046181 367 retval = mqtt_client_ctx_create(&lib_ctx_cfg,
dflet 3:a8c249046181 368 &lib_cli_cbs, client_ctx_ptr,
dflet 3:a8c249046181 369 &client_ctx_ptr->cli_hndl);
dflet 3:a8c249046181 370
dflet 3:a8c249046181 371 if(retval < 0) {
dflet 3:a8c249046181 372 Uart_Write((uint8_t*)"mqtt_client_ctx_create failed\r\n");
dflet 3:a8c249046181 373 client_ctx_ptr->in_use = false;
dflet 3:a8c249046181 374 return NULL;
dflet 3:a8c249046181 375 }
dflet 3:a8c249046181 376
dflet 3:a8c249046181 377 return (void*)client_ctx_ptr;
dflet 3:a8c249046181 378 }
dflet 3:a8c249046181 379
dflet 3:a8c249046181 380 //*****************************************************************************
dflet 3:a8c249046181 381 // sl_ExtLib_MqttClientCtxDelete
dflet 3:a8c249046181 382 //*****************************************************************************
dflet 3:a8c249046181 383 int32_t sl_ExtLib_MqttClientCtxDelete(void *cli_ctx)
dflet 3:a8c249046181 384 {
dflet 3:a8c249046181 385 struct sl_client_ctx *client_ctx=(struct sl_client_ctx *)cli_ctx;
dflet 3:a8c249046181 386 int32_t retval;
dflet 3:a8c249046181 387
dflet 3:a8c249046181 388 retval = mqtt_client_ctx_delete(client_ctx->cli_hndl);
dflet 3:a8c249046181 389 if(retval >= 0) {
dflet 3:a8c249046181 390 /* Check for more paramaters --- TBD */
dflet 3:a8c249046181 391 _cc3100module._nonos.sl_SyncObjDelete(&client_ctx->ack_sync_obj, 0);
dflet 3:a8c249046181 392 /* Free up the context */
dflet 3:a8c249046181 393 memset(client_ctx, 0, sizeof(struct sl_client_ctx));
dflet 3:a8c249046181 394 }
dflet 3:a8c249046181 395
dflet 3:a8c249046181 396 return (retval < 0)? -1: 0;
dflet 3:a8c249046181 397 }
dflet 3:a8c249046181 398
dflet 3:a8c249046181 399 void
dflet 3:a8c249046181 400 mutex_lockup(void *mqtt_lib_lock)
dflet 3:a8c249046181 401 {
dflet 3:a8c249046181 402 _cc3100module._nonos.sl_LockObjLock((_SlLockObj_t*)mqtt_lib_lock, SL_OS_WAIT_FOREVER, NON_OS_LOCK_OBJ_LOCK_VALUE, SL_OS_WAIT_FOREVER);
dflet 3:a8c249046181 403 }
dflet 3:a8c249046181 404
dflet 3:a8c249046181 405 void
dflet 3:a8c249046181 406 mutex_unlock(void *mqtt_lib_lock)
dflet 3:a8c249046181 407 {
dflet 3:a8c249046181 408 _cc3100module._nonos.sl_LockObjUnlock((_SlLockObj_t*)mqtt_lib_lock, NON_OS_LOCK_OBJ_UNLOCK_VALUE);
dflet 3:a8c249046181 409 }
dflet 3:a8c249046181 410
dflet 3:a8c249046181 411 //*****************************************************************************
dflet 3:a8c249046181 412 // sl_ExtLib_MqttClientInit
dflet 3:a8c249046181 413 //*****************************************************************************
dflet 3:a8c249046181 414 int32_t sl_ExtLib_MqttClientInit(const SlMqttClientLibCfg_t *cfg)
dflet 3:a8c249046181 415 {
dflet 3:a8c249046181 416
dflet 3:a8c249046181 417 struct mqtt_client_lib_cfg lib_cfg;
dflet 3:a8c249046181 418 /* Initialize the control variables */
dflet 3:a8c249046181 419 memset(sl_cli_ctx, 0, sizeof(sl_cli_ctx));
dflet 3:a8c249046181 420
dflet 3:a8c249046181 421 /* Setup the MQTT client lib configurations */
dflet 3:a8c249046181 422 lib_cfg.loopback_port = cfg->loopback_port;
dflet 3:a8c249046181 423 /* Initializing the sync object */
dflet 3:a8c249046181 424 g_rx_tx_sync_obj = 0;
dflet 3:a8c249046181 425
dflet 3:a8c249046181 426 if(cfg->loopback_port) {
dflet 3:a8c249046181 427 g_multi_srvr_conn = true;
dflet 3:a8c249046181 428 lib_cfg.grp_uses_cbfn = true; /* Does use the group callback func */
dflet 3:a8c249046181 429 } else {
dflet 3:a8c249046181 430 g_multi_srvr_conn = false;
dflet 3:a8c249046181 431 lib_cfg.grp_uses_cbfn = false; /* Doesnt use the group callback func */
dflet 3:a8c249046181 432 /* Create the sync object between Rx and App tasks */
dflet 3:a8c249046181 433 _cc3100module._nonos.sl_SyncObjCreate(&g_rx_tx_sync_obj, "RxTxSyncObject");
dflet 3:a8c249046181 434 _cc3100module._nonos.sl_SyncObjWait(&g_rx_tx_sync_obj, NON_OS_SYNC_OBJ_SIGNAL_VALUE, NON_OS_SYNC_OBJ_CLEAR_VALUE, SL_OS_NO_WAIT);
dflet 3:a8c249046181 435 }
dflet 3:a8c249046181 436
dflet 3:a8c249046181 437 g_wait_secs = cfg->resp_time;
dflet 3:a8c249046181 438 /* Setup the mutex operations */
dflet 3:a8c249046181 439 _cc3100module._nonos.sl_LockObjCreate(&mqtt_lib_lockobj,"MQTT Lock");
dflet 3:a8c249046181 440 lib_cfg.mutex = (void *)&mqtt_lib_lockobj;
dflet 3:a8c249046181 441 lib_cfg.mutex_lockin = mutex_lockup;
dflet 3:a8c249046181 442 lib_cfg.mutex_unlock = mutex_unlock;
dflet 3:a8c249046181 443 /* hooking DBG print function */
dflet 3:a8c249046181 444 lib_cfg.debug_printf = cfg->dbg_print;
dflet 3:a8c249046181 445 /* Initialize client library */
dflet 3:a8c249046181 446 if(mqtt_client_lib_init(&lib_cfg) < 0) {
dflet 3:a8c249046181 447 Uart_Write((uint8_t*)"mqtt_client_lib_init failed\r\n");
dflet 3:a8c249046181 448 return -1;
dflet 3:a8c249046181 449 }
dflet 3:a8c249046181 450
dflet 3:a8c249046181 451 /* provide MQTT Lib information to create a pool of MQTT constructs. */
dflet 3:a8c249046181 452 mqtt_client_buffers_register(MAX_MQP,mqp_vec,BUF_LEN,&buf_vec[0][0]);
dflet 3:a8c249046181 453 /* Register network services speicific to CC3200 */
dflet 3:a8c249046181 454 mqtt_client_net_svc_register(&net);
dflet 3:a8c249046181 455 /* start the receive task */
dflet 3:a8c249046181 456 osi_TaskCreate( VMqttRecvTask,
dflet 3:a8c249046181 457 (const signed char *) "MQTTRecv",
dflet 3:a8c249046181 458 OSI_STACK_SIZE,
dflet 3:a8c249046181 459 NULL,
dflet 3:a8c249046181 460 cfg->rx_tsk_priority, &g_rx_task_hndl );
dflet 3:a8c249046181 461
dflet 3:a8c249046181 462 return 0;
dflet 3:a8c249046181 463 }
dflet 3:a8c249046181 464
dflet 3:a8c249046181 465 //*****************************************************************************
dflet 3:a8c249046181 466 // sl_ExtLib_MqttClientExit
dflet 3:a8c249046181 467 //*****************************************************************************
dflet 3:a8c249046181 468 int32_t
dflet 3:a8c249046181 469 sl_ExtLib_MqttClientExit()
dflet 3:a8c249046181 470 {
dflet 3:a8c249046181 471 int32_t retval;
dflet 3:a8c249046181 472
dflet 3:a8c249046181 473 /* Deinitialize the MQTT client lib */
dflet 3:a8c249046181 474 retval = mqtt_client_lib_exit();
dflet 3:a8c249046181 475 if(retval >= 0) {
dflet 3:a8c249046181 476 if(mqtt_lib_lockobj != NULL)
dflet 3:a8c249046181 477 {
dflet 3:a8c249046181 478 /* Delete the MQTT lib lock object */
dflet 3:a8c249046181 479 _cc3100module._nonos.sl_LockObjDelete(&mqtt_lib_lockobj, 0);
dflet 3:a8c249046181 480 }
dflet 3:a8c249046181 481 if(g_rx_task_hndl != NULL)
dflet 3:a8c249046181 482 {
dflet 3:a8c249046181 483 /* Delete the Rx Task */
dflet 3:a8c249046181 484 osi_TaskDelete(&g_rx_task_hndl);
dflet 3:a8c249046181 485 }
dflet 3:a8c249046181 486
dflet 3:a8c249046181 487 if(g_rx_tx_sync_obj != NULL)
dflet 3:a8c249046181 488 {
dflet 3:a8c249046181 489 /* Delete the Rx-Tx task sync object */
dflet 3:a8c249046181 490 _cc3100module._nonos.sl_SyncObjDelete(&g_rx_tx_sync_obj, 0);
dflet 3:a8c249046181 491 }
dflet 3:a8c249046181 492
dflet 3:a8c249046181 493 g_rx_task_hndl = NULL;
dflet 3:a8c249046181 494 g_rx_tx_sync_obj = NULL;
dflet 3:a8c249046181 495 mqtt_lib_lockobj = NULL;
dflet 3:a8c249046181 496 }
dflet 3:a8c249046181 497
dflet 3:a8c249046181 498 return (retval < 0)? -1: 0;
dflet 3:a8c249046181 499 }
dflet 3:a8c249046181 500
dflet 3:a8c249046181 501 //*****************************************************************************
dflet 3:a8c249046181 502 // sl_ExtLib_MqttClientSet
dflet 3:a8c249046181 503 //*****************************************************************************
dflet 3:a8c249046181 504 int32_t
dflet 3:a8c249046181 505 sl_ExtLib_MqttClientSet(void *app, int32_t param, const void *value, uint32_t len)
dflet 3:a8c249046181 506 {
dflet 3:a8c249046181 507
dflet 3:a8c249046181 508 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)app;
dflet 3:a8c249046181 509 switch(param)
dflet 3:a8c249046181 510 {
dflet 3:a8c249046181 511 case SL_MQTT_PARAM_CLIENT_ID:
dflet 3:a8c249046181 512 /* Save the reference to the Client ID */
dflet 3:a8c249046181 513 client_ctx->client_id =(char*)value;
dflet 3:a8c249046181 514 break;
dflet 3:a8c249046181 515
dflet 3:a8c249046181 516 case SL_MQTT_PARAM_USER_NAME:
dflet 3:a8c249046181 517 /* Save the reference to the Username */
dflet 3:a8c249046181 518 client_ctx->usr_name =(char*)value;
dflet 3:a8c249046181 519 break;
dflet 3:a8c249046181 520
dflet 3:a8c249046181 521 case SL_MQTT_PARAM_PASS_WORD:
dflet 3:a8c249046181 522 /* Save the reference to the password */
dflet 3:a8c249046181 523 client_ctx->usr_pwd =(char*)value;
dflet 3:a8c249046181 524 break;
dflet 3:a8c249046181 525
dflet 3:a8c249046181 526 case SL_MQTT_PARAM_WILL_PARAM:
dflet 3:a8c249046181 527 /* Save the reference to will parameters*/
dflet 3:a8c249046181 528 client_ctx->mqtt_will = *((SlMqttWill_t*)value);
dflet 3:a8c249046181 529 break;
dflet 3:a8c249046181 530 default:
dflet 3:a8c249046181 531 break;
dflet 3:a8c249046181 532 }
dflet 3:a8c249046181 533
dflet 3:a8c249046181 534 return 0;
dflet 3:a8c249046181 535
dflet 3:a8c249046181 536 }
dflet 3:a8c249046181 537
dflet 3:a8c249046181 538 //*****************************************************************************
dflet 3:a8c249046181 539 // sl_ExtLib_MqttClientConnect
dflet 3:a8c249046181 540 //*****************************************************************************
dflet 3:a8c249046181 541 int32_t
dflet 3:a8c249046181 542 sl_ExtLib_MqttClientConnect(void *cli_ctx, bool clean, uint16_t keep_alive_time)
dflet 3:a8c249046181 543 {
dflet 3:a8c249046181 544
dflet 3:a8c249046181 545
dflet 3:a8c249046181 546 int32_t ret = -1;
dflet 3:a8c249046181 547 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)cli_ctx;
dflet 3:a8c249046181 548
dflet 3:a8c249046181 549 /*utf8 strings into which client info will be stored*/
dflet 3:a8c249046181 550 struct utf8_string client_id, username, usr_pwd, will_topic, will_msg;
dflet 3:a8c249046181 551 struct utf8_string *usrname = NULL, *usrpasswd = NULL, *clientid=NULL;
dflet 3:a8c249046181 552
dflet 3:a8c249046181 553 /* Provide Client ID,user name and password into MQTT Library */
dflet 3:a8c249046181 554 if(client_ctx->client_id != NULL) {
dflet 3:a8c249046181 555 STR2UTF_CONV(client_id, client_ctx->client_id);
dflet 3:a8c249046181 556 clientid = &client_id;
dflet 3:a8c249046181 557 }
dflet 3:a8c249046181 558
dflet 3:a8c249046181 559 if(client_ctx->usr_name != NULL) {
dflet 3:a8c249046181 560 STR2UTF_CONV(username, client_ctx->usr_name);
dflet 3:a8c249046181 561 usrname = &username;
dflet 3:a8c249046181 562 }
dflet 3:a8c249046181 563 if(client_ctx->usr_pwd != NULL) {
dflet 3:a8c249046181 564 STR2UTF_CONV(usr_pwd, client_ctx->usr_pwd);
dflet 3:a8c249046181 565 usrpasswd = &usr_pwd;
dflet 3:a8c249046181 566 }
dflet 3:a8c249046181 567 ret = mqtt_client_ctx_info_register(client_ctx->cli_hndl,
dflet 3:a8c249046181 568 clientid, usrname, usrpasswd);
dflet 3:a8c249046181 569 if(ret < 0) {
dflet 3:a8c249046181 570 goto mqtt_connect_exit1;
dflet 3:a8c249046181 571 }
dflet 3:a8c249046181 572
dflet 3:a8c249046181 573 /* Register a will message, if specified, into MQTT Library */
dflet 3:a8c249046181 574 if(NULL != client_ctx->mqtt_will.will_topic ) {
dflet 3:a8c249046181 575 STR2UTF_CONV(will_topic, client_ctx->mqtt_will.will_topic);
dflet 3:a8c249046181 576 STR2UTF_CONV(will_msg, client_ctx->mqtt_will.will_msg);
dflet 3:a8c249046181 577 ret = mqtt_client_ctx_will_register(client_ctx->cli_hndl,
dflet 3:a8c249046181 578 &will_topic, &will_msg,
dflet 3:a8c249046181 579 qos[client_ctx->mqtt_will.will_qos],
dflet 3:a8c249046181 580 client_ctx->mqtt_will.retain);
dflet 3:a8c249046181 581 if(ret < 0) {
dflet 3:a8c249046181 582 Uart_Write((uint8_t*)"mqtt_client_ctx_will_register fail\r\n");
dflet 3:a8c249046181 583 goto mqtt_connect_exit1;
dflet 3:a8c249046181 584 }
dflet 3:a8c249046181 585 }
dflet 3:a8c249046181 586
dflet 3:a8c249046181 587 client_ctx->awaited_ack = MQTT_CONNACK;
dflet 3:a8c249046181 588 client_ctx->conn_ack = 0;
dflet 3:a8c249046181 589
dflet 3:a8c249046181 590 /* Connect to the server */
dflet 3:a8c249046181 591 ret = mqtt_connect_msg_send(client_ctx->cli_hndl, clean, keep_alive_time);
dflet 3:a8c249046181 592 /*Network or Socket Error happens*/
dflet 3:a8c249046181 593 if(ret < 0) {
dflet 3:a8c249046181 594 Uart_Write((uint8_t*)"mqtt_connect_msg_send failed \r\n");
dflet 3:a8c249046181 595 goto mqtt_connect_exit1;
dflet 3:a8c249046181 596 }
dflet 3:a8c249046181 597
dflet 3:a8c249046181 598 if(false == g_multi_srvr_conn) {
dflet 3:a8c249046181 599 /* Unblock the receive task here */
dflet 3:a8c249046181 600 RX_TX_SIGNAL_POST();
dflet 3:a8c249046181 601 }
dflet 3:a8c249046181 602
dflet 3:a8c249046181 603 /* Wait for a CONNACK here */
dflet 3:a8c249046181 604 ACK_RX_SIGNAL_WAIT(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 605
dflet 3:a8c249046181 606 if(MQTT_DISCONNECT == client_ctx->awaited_ack) {
dflet 3:a8c249046181 607 Uart_Write((uint8_t*)"sl_ExtLib_MqttClientConnect fail\r\n");
dflet 3:a8c249046181 608 ret = -1;
dflet 3:a8c249046181 609 } else {
dflet 3:a8c249046181 610 ret = (int32_t)client_ctx->conn_ack;
dflet 3:a8c249046181 611 }
dflet 3:a8c249046181 612
dflet 3:a8c249046181 613 mqtt_connect_exit1:
dflet 3:a8c249046181 614
dflet 3:a8c249046181 615 client_ctx->awaited_ack = 0;
dflet 3:a8c249046181 616 client_ctx->conn_ack = 0;
dflet 3:a8c249046181 617 return (ret < 0)? -1: ret;
dflet 3:a8c249046181 618 }
dflet 3:a8c249046181 619
dflet 3:a8c249046181 620 //*****************************************************************************
dflet 3:a8c249046181 621 // sl_ExtLib_MqttClientDisconnect
dflet 3:a8c249046181 622 //*****************************************************************************
dflet 3:a8c249046181 623 int32_t
dflet 3:a8c249046181 624 sl_ExtLib_MqttClientDisconnect(void *cli_ctx)
dflet 3:a8c249046181 625 {
dflet 3:a8c249046181 626 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)cli_ctx;
dflet 3:a8c249046181 627 int32_t retval;
dflet 3:a8c249046181 628
dflet 3:a8c249046181 629 if(!(mqtt_client_is_connected(client_ctx->cli_hndl)))
dflet 3:a8c249046181 630 return -1;
dflet 3:a8c249046181 631
dflet 3:a8c249046181 632 client_ctx->awaited_ack = MQTT_DISCONNECT;
dflet 3:a8c249046181 633
dflet 3:a8c249046181 634 /* send the disconnect command. */
dflet 3:a8c249046181 635 retval = mqtt_disconn_send(client_ctx->cli_hndl);
dflet 3:a8c249046181 636
dflet 3:a8c249046181 637 if(retval >= 0) {
dflet 3:a8c249046181 638 /* wait on Rx task to acknowledge */
dflet 3:a8c249046181 639 ACK_RX_SIGNAL_WAIT(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 640 }
dflet 3:a8c249046181 641 client_ctx->awaited_ack = 0;
dflet 3:a8c249046181 642
dflet 3:a8c249046181 643 return (retval < 0)? -1: 0;
dflet 3:a8c249046181 644 }
dflet 3:a8c249046181 645
dflet 3:a8c249046181 646
dflet 3:a8c249046181 647 //*****************************************************************************
dflet 3:a8c249046181 648 // sl_ExtLib_MqttClientSub
dflet 3:a8c249046181 649 //*****************************************************************************
dflet 3:a8c249046181 650 int32_t
dflet 3:a8c249046181 651 sl_ExtLib_MqttClientSub(void *cli_ctx, char* const *topics,
dflet 3:a8c249046181 652 uint8_t *qos_level, int32_t count)
dflet 3:a8c249046181 653 {
dflet 3:a8c249046181 654 #define MAX_SIMULTANEOUS_SUB_TOPICS 4
dflet 3:a8c249046181 655
dflet 3:a8c249046181 656 int32_t ret = -1, i;
dflet 3:a8c249046181 657
dflet 3:a8c249046181 658 struct utf8_strqos qos_topics[MAX_SIMULTANEOUS_SUB_TOPICS];
dflet 3:a8c249046181 659 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)cli_ctx;
dflet 3:a8c249046181 660
dflet 3:a8c249046181 661 if(!(mqtt_client_is_connected(client_ctx->cli_hndl)) || (count > MAX_SIMULTANEOUS_SUB_TOPICS)) {
dflet 3:a8c249046181 662 goto mqtt_sub_exit1; /* client not connected*/
dflet 3:a8c249046181 663 }
dflet 3:a8c249046181 664
dflet 3:a8c249046181 665 for(i = 0; i < count; i++) {
dflet 3:a8c249046181 666 qos_topics[i].buffer = topics[i];
dflet 3:a8c249046181 667 qos_topics[i].qosreq = qos[qos_level[i]];
dflet 3:a8c249046181 668 qos_topics[i].length = strlen(topics[i]);
dflet 3:a8c249046181 669 }
dflet 3:a8c249046181 670
dflet 3:a8c249046181 671 /* Set up all variables to receive an ACK for the message to be sent*/
dflet 3:a8c249046181 672 if(true == client_ctx->blocking_send) {
dflet 3:a8c249046181 673 /* Update the qos_level to be in-out parameter */
dflet 3:a8c249046181 674 client_ctx->suback_qos = (char*)qos_level;
dflet 3:a8c249046181 675 client_ctx->awaited_ack = MQTT_SUBACK;
dflet 3:a8c249046181 676 }
dflet 3:a8c249046181 677
dflet 3:a8c249046181 678 /* Send the subscription MQTT message */
dflet 3:a8c249046181 679 ret = mqtt_sub_msg_send(client_ctx->cli_hndl, qos_topics, count);
dflet 3:a8c249046181 680 if(ret < 0) {
dflet 3:a8c249046181 681 goto mqtt_sub_exit1;
dflet 3:a8c249046181 682 }
dflet 3:a8c249046181 683
dflet 3:a8c249046181 684 if(true == client_ctx->blocking_send) {
dflet 3:a8c249046181 685 ACK_RX_SIGNAL_WAIT(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 686 if(MQTT_DISCONNECT == client_ctx->awaited_ack) {
dflet 3:a8c249046181 687 ret = -1;
dflet 3:a8c249046181 688 }
dflet 3:a8c249046181 689 }
dflet 3:a8c249046181 690
dflet 3:a8c249046181 691 mqtt_sub_exit1:
dflet 3:a8c249046181 692 client_ctx->awaited_ack = 0;
dflet 3:a8c249046181 693 return (ret < 0)? -1: 0;
dflet 3:a8c249046181 694 }
dflet 3:a8c249046181 695
dflet 3:a8c249046181 696
dflet 3:a8c249046181 697 //*****************************************************************************
dflet 3:a8c249046181 698 // sl_ExtLib_MqttClientUnsub
dflet 3:a8c249046181 699 //*****************************************************************************
dflet 3:a8c249046181 700 int32_t
dflet 3:a8c249046181 701 sl_ExtLib_MqttClientUnsub(void *cli_ctx, char* const *topics, int32_t count)
dflet 3:a8c249046181 702 {
dflet 3:a8c249046181 703 #define MAX_SIMULTANEOUS_UNSUB_TOPICS 4
dflet 3:a8c249046181 704
dflet 3:a8c249046181 705 int32_t ret = -1, i;
dflet 3:a8c249046181 706 struct utf8_string unsub_topics[MAX_SIMULTANEOUS_UNSUB_TOPICS];
dflet 3:a8c249046181 707 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)cli_ctx;
dflet 3:a8c249046181 708
dflet 3:a8c249046181 709 if(!(mqtt_client_is_connected(client_ctx->cli_hndl)) || (count > MAX_SIMULTANEOUS_UNSUB_TOPICS)) {
dflet 3:a8c249046181 710 goto mqtt_unsub_exit1; /* Check client status */
dflet 3:a8c249046181 711 }
dflet 3:a8c249046181 712
dflet 3:a8c249046181 713 for(i = 0; i < count; i++) {
dflet 3:a8c249046181 714 STR2UTF_CONV(unsub_topics[i], topics[i]);
dflet 3:a8c249046181 715 }
dflet 3:a8c249046181 716
dflet 3:a8c249046181 717 /* Set up all variables to receive an ACK for the message to be sent*/
dflet 3:a8c249046181 718 if(true == client_ctx->blocking_send) {
dflet 3:a8c249046181 719 client_ctx->awaited_ack = MQTT_UNSUBACK;
dflet 3:a8c249046181 720 }
dflet 3:a8c249046181 721
dflet 3:a8c249046181 722 /* Send the unsubscription MQTT message */
dflet 3:a8c249046181 723 ret = mqtt_unsub_msg_send(client_ctx->cli_hndl, unsub_topics, count);
dflet 3:a8c249046181 724 if(ret < 0) {
dflet 3:a8c249046181 725 goto mqtt_unsub_exit1;
dflet 3:a8c249046181 726 }
dflet 3:a8c249046181 727
dflet 3:a8c249046181 728 if(true == client_ctx->blocking_send) {
dflet 3:a8c249046181 729 ACK_RX_SIGNAL_WAIT(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 730 if(MQTT_DISCONNECT == client_ctx->awaited_ack) {
dflet 3:a8c249046181 731 ret = -1;
dflet 3:a8c249046181 732 }
dflet 3:a8c249046181 733 }
dflet 3:a8c249046181 734
dflet 3:a8c249046181 735 mqtt_unsub_exit1:
dflet 3:a8c249046181 736 client_ctx->awaited_ack = 0;
dflet 3:a8c249046181 737 return (ret < 0)? -1: 0;
dflet 3:a8c249046181 738 }
dflet 3:a8c249046181 739 //*****************************************************************************
dflet 3:a8c249046181 740 // sl_ExtLib_MqttClientSend
dflet 3:a8c249046181 741 //*****************************************************************************
dflet 3:a8c249046181 742 int32_t
dflet 3:a8c249046181 743 sl_ExtLib_MqttClientSend(void *cli_ctx, const char *topic,
dflet 3:a8c249046181 744 const void *data, int32_t len,
dflet 3:a8c249046181 745 char qos_level, bool retain)
dflet 3:a8c249046181 746 {
dflet 3:a8c249046181 747
dflet 3:a8c249046181 748 int32_t ret = -1;
dflet 3:a8c249046181 749 struct utf8_string topic_utf8;
dflet 3:a8c249046181 750 struct sl_client_ctx *client_ctx = (struct sl_client_ctx *)cli_ctx;
dflet 3:a8c249046181 751
dflet 3:a8c249046181 752 /* Check whether Client is connected or not? */
dflet 3:a8c249046181 753 if(!(mqtt_client_is_connected(client_ctx->cli_hndl))) {
dflet 3:a8c249046181 754 Uart_Write((uint8_t*)"mqtt client is not connected\n\r");
dflet 3:a8c249046181 755 return ret;
dflet 3:a8c249046181 756 }
dflet 3:a8c249046181 757
dflet 3:a8c249046181 758 STR2UTF_CONV(topic_utf8, topic);
dflet 3:a8c249046181 759
dflet 3:a8c249046181 760 /*Set up all variables to receive an ACK for the message to be sent*/
dflet 3:a8c249046181 761 if(MQTT_QOS1 == qos[qos_level]) {
dflet 3:a8c249046181 762 client_ctx->awaited_ack = MQTT_PUBACK;
dflet 3:a8c249046181 763 } else if(MQTT_QOS2 == qos[qos_level]) {
dflet 3:a8c249046181 764 client_ctx->awaited_ack = MQTT_PUBCOMP;
dflet 3:a8c249046181 765 }
dflet 3:a8c249046181 766 /*publish the message*/
dflet 3:a8c249046181 767 ret = mqtt_client_pub_msg_send(client_ctx->cli_hndl, &topic_utf8,
dflet 3:a8c249046181 768 (const uint8_t*)data, len,
dflet 3:a8c249046181 769 qos[qos_level], retain);
dflet 3:a8c249046181 770 if(ret < 0) {
dflet 3:a8c249046181 771 Uart_Write((uint8_t*)"mqtt_client_pub_msg_send failed\n\r");
dflet 3:a8c249046181 772 goto mqtt_pub_exit1;
dflet 3:a8c249046181 773 }
dflet 3:a8c249046181 774
dflet 3:a8c249046181 775 if(MQTT_QOS0 != qos[qos_level]) {
dflet 3:a8c249046181 776 if(true == client_ctx->blocking_send) {
dflet 3:a8c249046181 777 ACK_RX_SIGNAL_WAIT(&client_ctx->ack_sync_obj);
dflet 3:a8c249046181 778 if(MQTT_DISCONNECT == client_ctx->awaited_ack) {
dflet 3:a8c249046181 779 Uart_Write((uint8_t*)"MQTT_DISCONNECT\n\r");
dflet 3:a8c249046181 780 ret = -1;
dflet 3:a8c249046181 781 }
dflet 3:a8c249046181 782 }
dflet 3:a8c249046181 783 }
dflet 3:a8c249046181 784
dflet 3:a8c249046181 785 mqtt_pub_exit1:
dflet 3:a8c249046181 786 client_ctx->awaited_ack = 0;
dflet 3:a8c249046181 787 return (ret < 0)? -1: 0;
dflet 3:a8c249046181 788 }
dflet 3:a8c249046181 789
dflet 3:a8c249046181 790 }//namespace mbed_mqtt
dflet 3:a8c249046181 791