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 //#include "mbed.h"
dflet 3:a8c249046181 16 #include "cc3100_simplelink.h"
dflet 3:a8c249046181 17 #include "cc3100_sl_common.h"
dflet 3:a8c249046181 18 #include "cc31xx_sl_net.h"
dflet 3:a8c249046181 19 #include "sl_mqtt_client.h"
dflet 3:a8c249046181 20 #include "mqtt_client.h"
dflet 3:a8c249046181 21 #include "cc3200_platform.h"
dflet 3:a8c249046181 22 #include "cc3100.h"
dflet 3:a8c249046181 23 #include "fPtr_func.h"
dflet 3:a8c249046181 24 #include "myBoardInit.h"
dflet 3:a8c249046181 25 #include "cli_uart.h"
dflet 3:a8c249046181 26
dflet 3:a8c249046181 27 #define PRINT_BUF_LEN 128
dflet 3:a8c249046181 28 extern int8_t print_buf[PRINT_BUF_LEN];
dflet 3:a8c249046181 29
dflet 3:a8c249046181 30 using namespace mbed_cc3100;
dflet 3:a8c249046181 31
dflet 3:a8c249046181 32 #if (THIS_BOARD == MBED_BOARD_LPC1768)
dflet 3:a8c249046181 33 cc3100 _cc3100_module_(p16, p17, p9, p10, p8, SPI(p5, p6, p7));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 34 //cc3100 _cc3100_module_(p9, p10, p8, SPI(p11, p12, p13));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 35 #elif (THIS_BOARD == Seeed_Arch_Max)
dflet 3:a8c249046181 36 class cc3100 _cc3100_module_(PE_5, PE_4, PE_6, SPI(PB_5, PB_4, PB_3));
dflet 3:a8c249046181 37 #elif (THIS_BOARD == ST_MBED_NUCLEOF103)
dflet 3:a8c249046181 38 class cc3100 _cc3100_module_(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF103 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 39 #elif (THIS_BOARD == ST_MBED_NUCLEOF411)
dflet 3:a8c249046181 40 class cc3100 _cc3100_module_(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF411 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 41 #elif (THIS_BOARD == ST_MBED_NUCLEOF401)
dflet 3:a8c249046181 42 class cc3100 _cc3100_module_(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 43 #elif (THIS_BOARD == EA_MBED_LPC4088)
dflet 3:a8c249046181 44 class cc3100 _cc3100_module_(p14, p15, p9, p10, p8, SPI(p5, p6, p7));//LPC4088 irq, nHib, cs, mosi, miso, sck
dflet 3:a8c249046181 45 #elif (THIS_BOARD == LPCXpresso4337)
dflet 3:a8c249046181 46 class cc3100 _cc3100_module_(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 47 #endif
dflet 3:a8c249046181 48
dflet 3:a8c249046181 49 namespace mbed_mqtt {
dflet 3:a8c249046181 50
dflet 3:a8c249046181 51 #ifdef DEBUG_NET_DEV
dflet 3:a8c249046181 52 extern int32_t (*debug_printf)(const char *fmt, ...);
dflet 3:a8c249046181 53 #define PRINTF(x,...) debug_printf(x,##__VA_ARGS__)
dflet 3:a8c249046181 54 #else
dflet 3:a8c249046181 55 #define PRINTF(x,...)
dflet 3:a8c249046181 56 #endif
dflet 3:a8c249046181 57 /*
dflet 3:a8c249046181 58 3200 Devices specific Network Services Implementation
dflet 3:a8c249046181 59 */
dflet 3:a8c249046181 60
dflet 3:a8c249046181 61 #define LISTEN_QUE_SIZE 2
dflet 3:a8c249046181 62
dflet 3:a8c249046181 63 //*****************************************************************************
dflet 3:a8c249046181 64 // GLOBAL VARIABLES
dflet 3:a8c249046181 65 //*****************************************************************************
dflet 3:a8c249046181 66
dflet 3:a8c249046181 67 //*****************************************************************************
dflet 3:a8c249046181 68 // STATIC FUNCTIONS
dflet 3:a8c249046181 69 //*****************************************************************************
dflet 3:a8c249046181 70
dflet 3:a8c249046181 71 #ifdef DEBUG_NET_DEV
dflet 3:a8c249046181 72 static int32_t buf_printf(const uint8_t *buf, uint32_t len, uint32_t idt)
dflet 3:a8c249046181 73 {
dflet 3:a8c249046181 74 int32_t i = 0;
dflet 3:a8c249046181 75 for(i = 0; i < len; i++)
dflet 3:a8c249046181 76 {
dflet 3:a8c249046181 77 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 78 sprintf((char*) print_buf, "%02x ", *buf++);
dflet 3:a8c249046181 79 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 80
dflet 3:a8c249046181 81 if(0x03 == (i & 0x03))
dflet 3:a8c249046181 82 Uart_Write((uint8_t *)" ");
dflet 3:a8c249046181 83
dflet 3:a8c249046181 84 if(0x0F == (i & 0x0F))
dflet 3:a8c249046181 85 {
dflet 3:a8c249046181 86 int32_t j = 0;
dflet 3:a8c249046181 87 Uart_Write((uint8_t *)"\n\r");
dflet 3:a8c249046181 88
dflet 3:a8c249046181 89 for(j = 0; j < idt; j++)
dflet 3:a8c249046181 90 Uart_Write((uint8_t *)" ");
dflet 3:a8c249046181 91 }
dflet 3:a8c249046181 92 }
dflet 3:a8c249046181 93
dflet 3:a8c249046181 94 Uart_Write((uint8_t *)"\n\r");
dflet 3:a8c249046181 95
dflet 3:a8c249046181 96 return len;
dflet 3:a8c249046181 97 }
dflet 3:a8c249046181 98 #endif
dflet 3:a8c249046181 99
dflet 3:a8c249046181 100 /*-----------------------------------------------------------------------------
dflet 3:a8c249046181 101 Open a TCP socket and modify its properties i.e security options if req.
dflet 3:a8c249046181 102 Socket properties modified in this function are based on the options set
dflet 3:a8c249046181 103 outside the scope of this function.
dflet 3:a8c249046181 104 Returns a valid handle on success, otherwise a negative number.
dflet 3:a8c249046181 105 -----------------------------------------------------------------------------*/
dflet 3:a8c249046181 106
dflet 3:a8c249046181 107 static int32_t create_socket(uint32_t nwconn_opts, struct secure_conn *nw_security_opts)
dflet 3:a8c249046181 108 {
dflet 3:a8c249046181 109
dflet 3:a8c249046181 110 int32_t MqttSocketFd, Status;
dflet 3:a8c249046181 111
dflet 3:a8c249046181 112 //local variables for creating secure socket
dflet 3:a8c249046181 113 uint8_t SecurityMethod;
dflet 3:a8c249046181 114 uint32_t SecurityCypher;
dflet 3:a8c249046181 115 int8_t i;
dflet 3:a8c249046181 116
dflet 3:a8c249046181 117 //If TLS is required
dflet 3:a8c249046181 118 if ((nwconn_opts & DEV_NETCONN_OPT_SEC) != 0) // bit was set to 1
dflet 3:a8c249046181 119 {
dflet 3:a8c249046181 120 MqttSocketFd = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_STREAM, SL_SEC_SOCKET);
dflet 3:a8c249046181 121 if (MqttSocketFd < 0) {
dflet 3:a8c249046181 122 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 123 sprintf((char*) print_buf, "MqttSocketFd fail %i\r\n",MqttSocketFd);
dflet 3:a8c249046181 124 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 125 return (MqttSocketFd);
dflet 3:a8c249046181 126 }
dflet 3:a8c249046181 127
dflet 3:a8c249046181 128 SecurityMethod = *((uint8_t *) (nw_security_opts->method));
dflet 3:a8c249046181 129 SecurityCypher = *((uint32_t *) (nw_security_opts->cipher));
dflet 3:a8c249046181 130
dflet 3:a8c249046181 131 if (nw_security_opts->n_file < 1 || nw_security_opts->n_file > 4 ) {
dflet 3:a8c249046181 132 Uart_Write((uint8_t*)"\n\r ERROR: security files missing or wrong number of security files\n\r");
dflet 3:a8c249046181 133 Uart_Write((uint8_t*)"\n\r ERROR: Did not create socket\n\r");
dflet 3:a8c249046181 134 return (-1);
dflet 3:a8c249046181 135 }
dflet 3:a8c249046181 136
dflet 3:a8c249046181 137 //Set Socket Options that were just defined
dflet 3:a8c249046181 138 Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SL_SOL_SOCKET, SL_SO_SECMETHOD,
dflet 3:a8c249046181 139 &SecurityMethod, sizeof(SecurityMethod));
dflet 3:a8c249046181 140 if (Status < 0) {
dflet 3:a8c249046181 141 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 142 sprintf((char*) print_buf, "Status error %i socket closed\n\r",Status);
dflet 3:a8c249046181 143 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 144 _cc3100_module_._socket.sl_Close(MqttSocketFd);
dflet 3:a8c249046181 145 return (Status);
dflet 3:a8c249046181 146 }
dflet 3:a8c249046181 147
dflet 3:a8c249046181 148 Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SL_SOL_SOCKET, SL_SO_SECURE_MASK,
dflet 3:a8c249046181 149 &SecurityCypher, sizeof(SecurityCypher));
dflet 3:a8c249046181 150 if (Status < 0) {
dflet 3:a8c249046181 151 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 152 sprintf((char*) print_buf, "Status error %i socket closed\n\r",Status);
dflet 3:a8c249046181 153 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 154 _cc3100_module_._socket.sl_Close(MqttSocketFd);
dflet 3:a8c249046181 155 return (Status);
dflet 3:a8c249046181 156 }
dflet 3:a8c249046181 157
dflet 3:a8c249046181 158 if(nw_security_opts->n_file == 1){
dflet 3:a8c249046181 159 Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CA_FILE_NAME,
dflet 3:a8c249046181 160 nw_security_opts->files[0], strlen(nw_security_opts->files[0]));
dflet 3:a8c249046181 161 if (Status < 0) {
dflet 3:a8c249046181 162 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 163 sprintf((char*) print_buf, "Status error %i socket closed\n\r",Status);
dflet 3:a8c249046181 164 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 165 _cc3100_module_._socket.sl_Close(MqttSocketFd);
dflet 3:a8c249046181 166 return (Status);
dflet 3:a8c249046181 167 }
dflet 3:a8c249046181 168 }else{
dflet 3:a8c249046181 169 for(i=0; i<nw_security_opts->n_file;i++){
dflet 3:a8c249046181 170 if(NULL != nw_security_opts->files[i]){
dflet 3:a8c249046181 171 Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SL_SOL_SOCKET,
dflet 3:a8c249046181 172 (SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME+i),
dflet 3:a8c249046181 173 nw_security_opts->files[i],
dflet 3:a8c249046181 174 strlen(nw_security_opts->files[i]));
dflet 3:a8c249046181 175 if (Status < 0) {
dflet 3:a8c249046181 176 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 177 sprintf((char*) print_buf, "Status error %i socket closed\n\r",Status);
dflet 3:a8c249046181 178 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 179 _cc3100_module_._socket.sl_Close(MqttSocketFd);
dflet 3:a8c249046181 180 return (Status);
dflet 3:a8c249046181 181 }
dflet 3:a8c249046181 182 }
dflet 3:a8c249046181 183 }
dflet 3:a8c249046181 184 }
dflet 3:a8c249046181 185
dflet 3:a8c249046181 186 }
dflet 3:a8c249046181 187 // If no TLS required
dflet 3:a8c249046181 188 else {
dflet 3:a8c249046181 189 // check to create a udp or tcp socket
dflet 3:a8c249046181 190 if ((nwconn_opts & DEV_NETCONN_OPT_UDP) != 0) // bit is set ; create a udp socket
dflet 3:a8c249046181 191 {
dflet 3:a8c249046181 192 MqttSocketFd = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, SL_IPPROTO_UDP);
dflet 3:a8c249046181 193 } else // socket for tcp
dflet 3:a8c249046181 194 {
dflet 3:a8c249046181 195 MqttSocketFd = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_STREAM,
dflet 3:a8c249046181 196 SL_IPPROTO_TCP); // consider putting 0 in place of SL_IPPROTO_TCP
dflet 3:a8c249046181 197 }
dflet 3:a8c249046181 198 }
dflet 3:a8c249046181 199
dflet 3:a8c249046181 200 return (MqttSocketFd);
dflet 3:a8c249046181 201
dflet 3:a8c249046181 202 } // end of function
dflet 3:a8c249046181 203
dflet 3:a8c249046181 204 /*-----------------------------------------------------------------------------
dflet 3:a8c249046181 205 This function takes an ipv4 address in dot format i.e "a.b.c.d" and returns the
dflet 3:a8c249046181 206 ip address in Network byte Order, which can be used in connect call
dflet 3:a8c249046181 207 It returns 0, if a valid ip address is not detected.
dflet 3:a8c249046181 208 -----------------------------------------------------------------------------*/
dflet 3:a8c249046181 209
dflet 3:a8c249046181 210 static uint32_t svr_addr_NB_order_IPV4(char *svr_addr_str)
dflet 3:a8c249046181 211 {
dflet 3:a8c249046181 212 uint8_t addr[4];
dflet 3:a8c249046181 213 int8_t i = 0;
dflet 3:a8c249046181 214 char *token;
dflet 3:a8c249046181 215 uint32_t svr_addr;
dflet 3:a8c249046181 216 int32_t temp;
dflet 3:a8c249046181 217
dflet 3:a8c249046181 218 /*take a temporary copy of the string. strtok modifies the input string*/
dflet 3:a8c249046181 219 int8_t svr_addr_size = strlen(svr_addr_str);
dflet 3:a8c249046181 220 char *svr_addr_cpy = (char*)malloc(svr_addr_size + 1); //1 for null
dflet 3:a8c249046181 221 if(NULL == svr_addr_cpy) return 0;
dflet 3:a8c249046181 222 strcpy(svr_addr_cpy, svr_addr_str);
dflet 3:a8c249046181 223
dflet 3:a8c249046181 224 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 225 sprintf((char*) print_buf, "\n\r server address = %s\n\r", svr_addr_cpy);
dflet 3:a8c249046181 226 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 227 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 228 sprintf((char*) print_buf, "\n\r server address string length = %d\n\r", strlen(svr_addr_cpy));
dflet 3:a8c249046181 229 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 230
dflet 3:a8c249046181 231 /* get the first token */
dflet 3:a8c249046181 232 token = strtok((char*)svr_addr_cpy, ".");
dflet 3:a8c249046181 233
dflet 3:a8c249046181 234 /* walk through other tokens */
dflet 3:a8c249046181 235 while (token != NULL) {
dflet 3:a8c249046181 236 temp = atoi((const char*)token);
dflet 3:a8c249046181 237
dflet 3:a8c249046181 238 //check for invalid tokens or if already 4 tokens were obtained
dflet 3:a8c249046181 239 if ((temp < 0) || (temp > 255) || (i >= 4)) {
dflet 3:a8c249046181 240 free(svr_addr_cpy);
dflet 3:a8c249046181 241 return (0);
dflet 3:a8c249046181 242 }
dflet 3:a8c249046181 243
dflet 3:a8c249046181 244 addr[i++] = (uint8_t) temp;
dflet 3:a8c249046181 245 token = strtok(NULL, ".");
dflet 3:a8c249046181 246 }
dflet 3:a8c249046181 247
dflet 3:a8c249046181 248 // check if exactly 4 valid tokens are available or not
dflet 3:a8c249046181 249 if (i != 4) {
dflet 3:a8c249046181 250 free(svr_addr_cpy);
dflet 3:a8c249046181 251 return (0);
dflet 3:a8c249046181 252 }
dflet 3:a8c249046181 253
dflet 3:a8c249046181 254 //form address if above test passed
dflet 3:a8c249046181 255 svr_addr = *((uint32_t *) &addr);
dflet 3:a8c249046181 256 free(svr_addr_cpy);
dflet 3:a8c249046181 257
dflet 3:a8c249046181 258 return (svr_addr);
dflet 3:a8c249046181 259
dflet 3:a8c249046181 260 } // end of function
dflet 3:a8c249046181 261
dflet 3:a8c249046181 262 //*****************************************************************************
dflet 3:a8c249046181 263 // Network Services functions
dflet 3:a8c249046181 264 //*****************************************************************************
dflet 3:a8c249046181 265
dflet 3:a8c249046181 266 /*-----------------------------------------------------------------------------
dflet 3:a8c249046181 267 Open a TCP socket with required properties
dflet 3:a8c249046181 268 Also connect to the server.
dflet 3:a8c249046181 269 Returns a valid handle on success, NULL on failure.
dflet 3:a8c249046181 270 -----------------------------------------------------------------------------*/
dflet 3:a8c249046181 271
dflet 3:a8c249046181 272 int32_t comm_open(uint32_t nwconn_opts, const char *server_addr, uint16_t port_number,
dflet 3:a8c249046181 273 const struct secure_conn *nw_security)
dflet 3:a8c249046181 274 {
dflet 3:a8c249046181 275
dflet 3:a8c249046181 276 int32_t Status, MqttSocketFd;
dflet 3:a8c249046181 277
dflet 3:a8c249046181 278 SlSockAddrIn_t LocalAddr; //address of the server to connect to
dflet 3:a8c249046181 279 int32_t LocalAddrSize;
dflet 3:a8c249046181 280
dflet 3:a8c249046181 281 uint32_t uiIP;
dflet 3:a8c249046181 282
dflet 3:a8c249046181 283 // create socket
dflet 3:a8c249046181 284 MqttSocketFd = create_socket(nwconn_opts,
dflet 3:a8c249046181 285 (struct secure_conn*) nw_security);
dflet 3:a8c249046181 286
dflet 3:a8c249046181 287 if (MqttSocketFd < 0) {
dflet 3:a8c249046181 288 Uart_Write((uint8_t*)"\n\r ERROR: Could not create a socket.\n\r");
dflet 3:a8c249046181 289 return -1;
dflet 3:a8c249046181 290 }
dflet 3:a8c249046181 291
dflet 3:a8c249046181 292 if ((nwconn_opts & DEV_NETCONN_OPT_UDP) != 0) // bit is set ; create a udp socket
dflet 3:a8c249046181 293 {
dflet 3:a8c249046181 294 //filling the UDP server socket address
dflet 3:a8c249046181 295 LocalAddr.sin_family = SL_AF_INET;
dflet 3:a8c249046181 296 LocalAddr.sin_port = _cc3100_module_._socket.sl_Htons((unsigned short) port_number);
dflet 3:a8c249046181 297 LocalAddr.sin_addr.s_addr = 0;
dflet 3:a8c249046181 298 LocalAddrSize = sizeof(SlSockAddrIn_t);
dflet 3:a8c249046181 299
dflet 3:a8c249046181 300 Status = _cc3100_module_._socket.sl_Bind(MqttSocketFd, (SlSockAddr_t *) &LocalAddr,
dflet 3:a8c249046181 301 LocalAddrSize);
dflet 3:a8c249046181 302 if (Status < 0) {
dflet 3:a8c249046181 303 // error
dflet 3:a8c249046181 304 Uart_Write((uint8_t*)"\n\r ERROR: Could not bind socket.\n\r");
dflet 3:a8c249046181 305 _cc3100_module_._socket.sl_Close(MqttSocketFd);
dflet 3:a8c249046181 306 return -1;
dflet 3:a8c249046181 307 }
dflet 3:a8c249046181 308 } else // do tcp connection
dflet 3:a8c249046181 309 {
dflet 3:a8c249046181 310 // get the ip address of server to do tcp connect
dflet 3:a8c249046181 311 if ((nwconn_opts & DEV_NETCONN_OPT_URL) != 0) // server address is a URL
dflet 3:a8c249046181 312 {
dflet 3:a8c249046181 313 Status = _cc3100_module_._netapp.sl_NetAppDnsGetHostByName((uint8_t*) server_addr,
dflet 3:a8c249046181 314 strlen(server_addr), (uint32_t*) &uiIP, SL_AF_INET);
dflet 3:a8c249046181 315
dflet 3:a8c249046181 316 if (Status < 0) {
dflet 3:a8c249046181 317 Uart_Write((uint8_t*)"\n\r ERROR: Could not resolve the ip address of the server \n\r");
dflet 3:a8c249046181 318 return (-1);
dflet 3:a8c249046181 319 }
dflet 3:a8c249046181 320 // convert the address to network byte order as the function returns in host byte order
dflet 3:a8c249046181 321 uiIP = _cc3100_module_._socket.sl_Htonl(uiIP);
dflet 3:a8c249046181 322 } else // server address is a string in dot notation
dflet 3:a8c249046181 323 {
dflet 3:a8c249046181 324 if ((nwconn_opts & DEV_NETCONN_OPT_IP6) != 0) // server address is an IPV6 address string
dflet 3:a8c249046181 325 {
dflet 3:a8c249046181 326 Uart_Write((uint8_t*)"\n\r ERROR: Currently do not support IPV6 addresses \n\r");
dflet 3:a8c249046181 327 return (-1);
dflet 3:a8c249046181 328 } else // address is an IPv4 string
dflet 3:a8c249046181 329 {
dflet 3:a8c249046181 330 // get the server ip address in Network Byte order
dflet 3:a8c249046181 331 uiIP = svr_addr_NB_order_IPV4((char*) server_addr);
dflet 3:a8c249046181 332 if (0 == uiIP) {
dflet 3:a8c249046181 333 Uart_Write((uint8_t*)"\n\r ERROR: Could not resolve the ip address of the server \n\r");
dflet 3:a8c249046181 334 return (-1);
dflet 3:a8c249046181 335 }
dflet 3:a8c249046181 336 }
dflet 3:a8c249046181 337
dflet 3:a8c249046181 338 }
dflet 3:a8c249046181 339
dflet 3:a8c249046181 340 LocalAddr.sin_family = SL_AF_INET;
dflet 3:a8c249046181 341 LocalAddr.sin_addr.s_addr = uiIP;
dflet 3:a8c249046181 342 LocalAddr.sin_port = _cc3100_module_._socket.sl_Htons(port_number);
dflet 3:a8c249046181 343 LocalAddrSize = sizeof(SlSockAddrIn_t);
dflet 3:a8c249046181 344
dflet 3:a8c249046181 345 // do tcp connect
dflet 3:a8c249046181 346 Status = _cc3100_module_._socket.sl_Connect(MqttSocketFd, (SlSockAddr_t *) &LocalAddr,
dflet 3:a8c249046181 347 LocalAddrSize);
dflet 3:a8c249046181 348
dflet 3:a8c249046181 349 if (Status < 0) {
dflet 3:a8c249046181 350 if (SL_ESECSNOVERIFY != Status) {
dflet 3:a8c249046181 351 Uart_Write((uint8_t*)" \n\r ERROR: Could not establish connection to server.\n\r");Uart_Write((uint8_t*)" \n\r ERROR: Closing the socket.\n\r");
dflet 3:a8c249046181 352
dflet 3:a8c249046181 353 _cc3100_module_._socket.sl_Close(MqttSocketFd);
dflet 3:a8c249046181 354 return (-1);
dflet 3:a8c249046181 355 } else // SL_ESECSNOVERIFY == Status
dflet 3:a8c249046181 356 {
dflet 3:a8c249046181 357 Uart_Write((uint8_t*)" \n\r ERROR: Could not establish secure connection to server.\n\r");Uart_Write((uint8_t*)" \n\r Continuing with unsecure connection to server...\n\r");
dflet 3:a8c249046181 358 }
dflet 3:a8c249046181 359 }
dflet 3:a8c249046181 360
dflet 3:a8c249046181 361 // Success
dflet 3:a8c249046181 362 Uart_Write((uint8_t*)"\n\r Connected to server ....\n\r");
dflet 3:a8c249046181 363
dflet 3:a8c249046181 364 } // end of doing binding port to udp socket or doing tcp connect
dflet 3:a8c249046181 365
dflet 3:a8c249046181 366 // set Timer for host processor
dflet 3:a8c249046181 367 platform_timer_init();
dflet 3:a8c249046181 368
dflet 3:a8c249046181 369 return (MqttSocketFd);
dflet 3:a8c249046181 370
dflet 3:a8c249046181 371 } // end of function
dflet 3:a8c249046181 372
dflet 3:a8c249046181 373 int32_t tcp_send(int32_t comm, const uint8_t *buf, uint32_t len, void *ctx)
dflet 3:a8c249046181 374 {
dflet 3:a8c249046181 375
dflet 3:a8c249046181 376 int32_t Status;
dflet 3:a8c249046181 377
dflet 3:a8c249046181 378 PRINTF("\n\r TCP send invoked for data with len %d\n\r", len);PRINTF("\n\r Sent Data : ");
dflet 3:a8c249046181 379
dflet 3:a8c249046181 380 #ifdef DEBUG_NET_DEV
dflet 3:a8c249046181 381 buf_printf(buf, len, 0);
dflet 3:a8c249046181 382 #endif
dflet 3:a8c249046181 383
dflet 3:a8c249046181 384 Status = _cc3100_module_._socket.sl_Send(comm, buf, len, 0);
dflet 3:a8c249046181 385
dflet 3:a8c249046181 386 return (Status);
dflet 3:a8c249046181 387
dflet 3:a8c249046181 388 } // end of function
dflet 3:a8c249046181 389
dflet 3:a8c249046181 390 int32_t tcp_recv(int32_t comm, uint8_t *buf, uint32_t len, uint32_t wait_secs, bool *timed_out, void *ctx)
dflet 3:a8c249046181 391 {
dflet 3:a8c249046181 392 int32_t Status;
dflet 3:a8c249046181 393 int32_t MqttSocketFd = comm;
dflet 3:a8c249046181 394
dflet 3:a8c249046181 395 #ifdef SOC_RCV_TIMEOUT_OPT
dflet 3:a8c249046181 396
dflet 3:a8c249046181 397 // socket receive time out options
dflet 3:a8c249046181 398 SlTimeval_t timeVal;
dflet 3:a8c249046181 399
dflet 3:a8c249046181 400 // recv time out options
dflet 3:a8c249046181 401 timeVal.tv_sec = wait_secs; // Seconds
dflet 3:a8c249046181 402 timeVal.tv_usec = 0; // Microseconds. 10000 microseconds resoultion
dflet 3:a8c249046181 403
dflet 3:a8c249046181 404 /*------------------- setting receive timeout option on socket ---------------------*/
dflet 3:a8c249046181 405
dflet 3:a8c249046181 406 Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SOL_SOCKET, SL_SO_RCVTIMEO, &timeVal,
dflet 3:a8c249046181 407 sizeof(timeVal));
dflet 3:a8c249046181 408
dflet 3:a8c249046181 409 if (Status == 0) {
dflet 3:a8c249046181 410
dflet 3:a8c249046181 411 } else if (Status < 0) {
dflet 3:a8c249046181 412 Uart_Write((uint8_t*)"\n\r ERROR: setting socket recv_timeout_option unsuccessfull! \n\r");
dflet 3:a8c249046181 413
dflet 3:a8c249046181 414 }
dflet 3:a8c249046181 415 /*--------------end of setting receive timeout option on socket ---------------------*/
dflet 3:a8c249046181 416
dflet 3:a8c249046181 417 #endif
dflet 3:a8c249046181 418
dflet 3:a8c249046181 419 // printf("\n\r TCP recv invoked ...\n\r");
dflet 3:a8c249046181 420 *timed_out = 0;
dflet 3:a8c249046181 421
dflet 3:a8c249046181 422 Status = _cc3100_module_._socket.sl_Recv(MqttSocketFd, buf, len, 0);
dflet 3:a8c249046181 423
dflet 3:a8c249046181 424 if (Status > 0) {
dflet 3:a8c249046181 425 #ifdef DEBUG_NET_DEV
dflet 3:a8c249046181 426 buf_printf(buf, Status, 0);
dflet 3:a8c249046181 427 #endif
dflet 3:a8c249046181 428 }
dflet 3:a8c249046181 429
dflet 3:a8c249046181 430 if (0 == Status) {
dflet 3:a8c249046181 431 Uart_Write((uint8_t*)"\n\r Connection Closed by peer....\n\r");
dflet 3:a8c249046181 432 }
dflet 3:a8c249046181 433
dflet 3:a8c249046181 434 if (SL_EAGAIN == Status) {
dflet 3:a8c249046181 435 Uart_Write((uint8_t*)"\n\r ERROR: Recv Time out error on server socket \n\r");
dflet 3:a8c249046181 436 *timed_out = 1;
dflet 3:a8c249046181 437 }
dflet 3:a8c249046181 438
dflet 3:a8c249046181 439 return (Status);
dflet 3:a8c249046181 440
dflet 3:a8c249046181 441 } // end of function
dflet 3:a8c249046181 442
dflet 3:a8c249046181 443 int32_t comm_close(int32_t comm) {
dflet 3:a8c249046181 444 int32_t Status;
dflet 3:a8c249046181 445
dflet 3:a8c249046181 446 Status = _cc3100_module_._socket.sl_Close(comm);
dflet 3:a8c249046181 447 return (Status);
dflet 3:a8c249046181 448
dflet 3:a8c249046181 449 } // end of function
dflet 3:a8c249046181 450
dflet 3:a8c249046181 451 uint32_t rtc_secs(void) {
dflet 3:a8c249046181 452 return(platform_get_time_in_secs());
dflet 3:a8c249046181 453 } // end of function
dflet 3:a8c249046181 454
dflet 3:a8c249046181 455 //--------------------------- adding functions for server functionalities ---------------------------
dflet 3:a8c249046181 456
dflet 3:a8c249046181 457 int32_t tcp_listen(uint32_t nwconn_info, uint16_t port_number,
dflet 3:a8c249046181 458 const struct secure_conn *nw_security)
dflet 3:a8c249046181 459 {
dflet 3:a8c249046181 460 SlSockAddrIn_t sLocalAddr;
dflet 3:a8c249046181 461 int32_t iSockID, iAddrSize;
dflet 3:a8c249046181 462 int32_t iStatus;
dflet 3:a8c249046181 463
dflet 3:a8c249046181 464 //filling the TCP server socket address
dflet 3:a8c249046181 465 sLocalAddr.sin_family = SL_AF_INET;
dflet 3:a8c249046181 466 sLocalAddr.sin_port = _cc3100_module_._socket.sl_Htons(port_number);
dflet 3:a8c249046181 467 sLocalAddr.sin_addr.s_addr = 0;
dflet 3:a8c249046181 468 iAddrSize = sizeof(SlSockAddrIn_t);
dflet 3:a8c249046181 469
dflet 3:a8c249046181 470 // creating a TCP socket
dflet 3:a8c249046181 471 iSockID = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);
dflet 3:a8c249046181 472 if (iSockID < 0) {
dflet 3:a8c249046181 473 // error
dflet 3:a8c249046181 474 return (-1);
dflet 3:a8c249046181 475 }
dflet 3:a8c249046181 476
dflet 3:a8c249046181 477 // binding the TCP socket to the TCP server address
dflet 3:a8c249046181 478 iStatus = _cc3100_module_._socket.sl_Bind(iSockID, (SlSockAddr_t *) &sLocalAddr, iAddrSize);
dflet 3:a8c249046181 479 if (iStatus < 0) {
dflet 3:a8c249046181 480 // error
dflet 3:a8c249046181 481 _cc3100_module_._socket.sl_Close(iSockID);
dflet 3:a8c249046181 482 return (-1);
dflet 3:a8c249046181 483 }
dflet 3:a8c249046181 484
dflet 3:a8c249046181 485 // putting the socket for listening to the incoming TCP connection
dflet 3:a8c249046181 486 iStatus = _cc3100_module_._socket.sl_Listen(iSockID, LISTEN_QUE_SIZE);
dflet 3:a8c249046181 487 if (iStatus < 0) {
dflet 3:a8c249046181 488 _cc3100_module_._socket.sl_Close(iSockID);
dflet 3:a8c249046181 489 return (-1);
dflet 3:a8c249046181 490 }
dflet 3:a8c249046181 491
dflet 3:a8c249046181 492 memset(print_buf, 0x00, PRINT_BUF_LEN);
dflet 3:a8c249046181 493 sprintf((char*) print_buf, "\n\r\t Server Socket created and listening on port number: %d! \n\r", port_number);
dflet 3:a8c249046181 494 Uart_Write((uint8_t *) print_buf);
dflet 3:a8c249046181 495
dflet 3:a8c249046181 496 return (iSockID);
dflet 3:a8c249046181 497
dflet 3:a8c249046181 498 } // end of function
dflet 3:a8c249046181 499
dflet 3:a8c249046181 500 int32_t tcp_select(int32_t *recv_cvec, int32_t *send_cvec, int32_t *rsvd_cvec, uint32_t wait_secs)
dflet 3:a8c249046181 501 {
dflet 3:a8c249046181 502
dflet 3:a8c249046181 503 SlTimeval_t tv, *p_tv;
dflet 3:a8c249046181 504 SlFdSet_t rdfds;
dflet 3:a8c249046181 505 int32_t rd_idx = 0, wr_idx = 0, max_fd = 0;
dflet 3:a8c249046181 506 int32_t rv = 0;
dflet 3:a8c249046181 507
dflet 3:a8c249046181 508 tv.tv_sec = wait_secs;
dflet 3:a8c249046181 509 tv.tv_usec = 0;
dflet 3:a8c249046181 510
dflet 3:a8c249046181 511 p_tv = (0xFFFFFFFF != wait_secs) ? &tv : NULL;
dflet 3:a8c249046181 512
dflet 3:a8c249046181 513 _cc3100_module_._socket.SL_FD_ZERO(&rdfds);
dflet 3:a8c249046181 514
dflet 3:a8c249046181 515 while (-1 != recv_cvec[rd_idx]) {
dflet 3:a8c249046181 516 int32_t fd = recv_cvec[rd_idx++];
dflet 3:a8c249046181 517
dflet 3:a8c249046181 518 _cc3100_module_._socket.SL_FD_SET(fd, &rdfds);
dflet 3:a8c249046181 519
dflet 3:a8c249046181 520 if (max_fd < fd){
dflet 3:a8c249046181 521 max_fd = fd;
dflet 3:a8c249046181 522 }
dflet 3:a8c249046181 523 }
dflet 3:a8c249046181 524
dflet 3:a8c249046181 525 // printf("Blocking network for (%s) %u secs to monitor %d fd(s)\n\r",
dflet 3:a8c249046181 526 // p_tv? "finite" : "forever", wait_secs, rd_idx);
dflet 3:a8c249046181 527
dflet 3:a8c249046181 528 rv = _cc3100_module_._socket.sl_Select(max_fd + 1, &rdfds, NULL, NULL, p_tv);
dflet 3:a8c249046181 529
dflet 3:a8c249046181 530 if (rv <= 0) {
dflet 3:a8c249046181 531 // printf("Select Failed %i\n\r",rv);
dflet 3:a8c249046181 532 return rv;
dflet 3:a8c249046181 533 }
dflet 3:a8c249046181 534
dflet 3:a8c249046181 535 rd_idx = 0;
dflet 3:a8c249046181 536 while (-1 != recv_cvec[rd_idx]) {
dflet 3:a8c249046181 537 int32_t fd = recv_cvec[rd_idx++];
dflet 3:a8c249046181 538 if (_cc3100_module_._socket.SL_FD_ISSET(fd, &rdfds))
dflet 3:a8c249046181 539 recv_cvec[wr_idx++] = fd;
dflet 3:a8c249046181 540 }
dflet 3:a8c249046181 541
dflet 3:a8c249046181 542 recv_cvec[wr_idx] = NULL;
dflet 3:a8c249046181 543
dflet 3:a8c249046181 544 // printf("Number of sockets on which activity is observed = %d \n\r", wr_idx);
dflet 3:a8c249046181 545
dflet 3:a8c249046181 546 return (wr_idx);
dflet 3:a8c249046181 547 } // end of function
dflet 3:a8c249046181 548
dflet 3:a8c249046181 549 int32_t tcp_accept(int32_t listen_hnd, uint8_t *client_ip, uint32_t *ip_length)
dflet 3:a8c249046181 550 {
dflet 3:a8c249046181 551 int32_t new_fd;
dflet 3:a8c249046181 552 SlSockAddrIn_t client_addr = {0}; // client address
dflet 3:a8c249046181 553 SlSocklen_t cl_addr_size;
dflet 3:a8c249046181 554
dflet 3:a8c249046181 555 cl_addr_size = sizeof(client_addr);
dflet 3:a8c249046181 556
dflet 3:a8c249046181 557 new_fd = _cc3100_module_._socket.sl_Accept(listen_hnd, (SlSockAddr_t *) &client_addr,
dflet 3:a8c249046181 558 &cl_addr_size);
dflet 3:a8c249046181 559
dflet 3:a8c249046181 560 if (new_fd < 0) {
dflet 3:a8c249046181 561 Uart_Write((uint8_t*)"\n\r ERROR: in accept \n\r");
dflet 3:a8c249046181 562 return (NULL);
dflet 3:a8c249046181 563 }
dflet 3:a8c249046181 564
dflet 3:a8c249046181 565 client_ip[0] = (client_addr.sin_addr.s_addr & 0xFF000000) >> 24;
dflet 3:a8c249046181 566 client_ip[1] = (client_addr.sin_addr.s_addr & 0x00FF0000) >> 16;
dflet 3:a8c249046181 567 client_ip[2] = (client_addr.sin_addr.s_addr & 0x0000FF00) >> 8;
dflet 3:a8c249046181 568 client_ip[3] = (client_addr.sin_addr.s_addr & 0x000000FF);
dflet 3:a8c249046181 569
dflet 3:a8c249046181 570 *ip_length = 4;
dflet 3:a8c249046181 571
dflet 3:a8c249046181 572 return new_fd;
dflet 3:a8c249046181 573
dflet 3:a8c249046181 574 } // end of function
dflet 3:a8c249046181 575
dflet 3:a8c249046181 576 //--------------------------- adding functions for udp functionalities ---------------------------
dflet 3:a8c249046181 577
dflet 3:a8c249046181 578 int32_t send_dest(int32_t comm, const uint8_t *buf, uint32_t len, uint16_t dest_port,
dflet 3:a8c249046181 579 const uint8_t *dest_ip, uint32_t ip_len)
dflet 3:a8c249046181 580 {
dflet 3:a8c249046181 581
dflet 3:a8c249046181 582 int32_t iSockID = (int32_t) comm;
dflet 3:a8c249046181 583 int32_t iStatus, iAddrSize;
dflet 3:a8c249046181 584 SlSockAddrIn_t sAddr;
dflet 3:a8c249046181 585 uint32_t uiDestIp;
dflet 3:a8c249046181 586
dflet 3:a8c249046181 587 //get destination ip
dflet 3:a8c249046181 588 #if 0
dflet 3:a8c249046181 589 uiDestIp = svr_addr_NB_order_IPV4((uint8_t*)dest_ip); //assuming a IPV4 address is passed in dot notation.
dflet 3:a8c249046181 590 if( 0 == uiDestIp )
dflet 3:a8c249046181 591 {
dflet 3:a8c249046181 592 Uart_Write((uint8_t*)"\n\r ERROR: Could not resolve the ip address of the destination \n\r");
dflet 3:a8c249046181 593 return(-1);
dflet 3:a8c249046181 594 }
dflet 3:a8c249046181 595 #else
dflet 3:a8c249046181 596 uiDestIp = (((uint32_t) dest_ip[0] << 24) | ((uint32_t) dest_ip[1] << 16)
dflet 3:a8c249046181 597 | (dest_ip[2] << 8) | (dest_ip[3]));
dflet 3:a8c249046181 598
dflet 3:a8c249046181 599 // printf("Writing to %d, %08x\r\n", (int32_t)comm, uiDestIp);
dflet 3:a8c249046181 600
dflet 3:a8c249046181 601 #endif
dflet 3:a8c249046181 602
dflet 3:a8c249046181 603 //filling the UDP server socket address
dflet 3:a8c249046181 604 sAddr.sin_family = SL_AF_INET;
dflet 3:a8c249046181 605 sAddr.sin_port = _cc3100_module_._socket.sl_Htons((unsigned short) dest_port);
dflet 3:a8c249046181 606 sAddr.sin_addr.s_addr = _cc3100_module_._socket.sl_Htonl(uiDestIp);
dflet 3:a8c249046181 607
dflet 3:a8c249046181 608 iAddrSize = sizeof(SlSockAddrIn_t);
dflet 3:a8c249046181 609
dflet 3:a8c249046181 610 // sending packet
dflet 3:a8c249046181 611 iStatus = _cc3100_module_._socket.sl_SendTo(iSockID, buf, len, 0, (SlSockAddr_t *) &sAddr,
dflet 3:a8c249046181 612 iAddrSize);
dflet 3:a8c249046181 613
dflet 3:a8c249046181 614 if (iStatus <= 0) {
dflet 3:a8c249046181 615 // error
dflet 3:a8c249046181 616 _cc3100_module_._socket.sl_Close(iSockID);
dflet 3:a8c249046181 617 Uart_Write((uint8_t*)"Error: Closed the UDP socket\n\r");
dflet 3:a8c249046181 618 }
dflet 3:a8c249046181 619
dflet 3:a8c249046181 620 return (iStatus);
dflet 3:a8c249046181 621
dflet 3:a8c249046181 622 } // end of function
dflet 3:a8c249046181 623
dflet 3:a8c249046181 624 int32_t recv_from(int32_t comm, uint8_t *buf, uint32_t len, uint16_t *from_port, uint8_t *from_ip, uint32_t *ip_len)
dflet 3:a8c249046181 625 {
dflet 3:a8c249046181 626
dflet 3:a8c249046181 627 int32_t iSockID = (int32_t) comm;
dflet 3:a8c249046181 628 int32_t iStatus, iAddrSize;
dflet 3:a8c249046181 629 SlSockAddrIn_t fromAddr = {0};
dflet 3:a8c249046181 630
dflet 3:a8c249046181 631 iAddrSize = sizeof(SlSockAddrIn_t);
dflet 3:a8c249046181 632
dflet 3:a8c249046181 633 iStatus = _cc3100_module_._socket.sl_RecvFrom(iSockID, buf, len, 0, (SlSockAddr_t *) &fromAddr,
dflet 3:a8c249046181 634 (SlSocklen_t*) &iAddrSize);
dflet 3:a8c249046181 635
dflet 3:a8c249046181 636 if (iStatus < 0) {
dflet 3:a8c249046181 637 // error
dflet 3:a8c249046181 638 _cc3100_module_._socket.sl_Close(iSockID);
dflet 3:a8c249046181 639 Uart_Write((uint8_t*)"Error: Closed the UDP socket\n\r");
dflet 3:a8c249046181 640 return (iStatus);
dflet 3:a8c249046181 641 }
dflet 3:a8c249046181 642
dflet 3:a8c249046181 643 //else populate from ip, from_port and ip_len parameters
dflet 3:a8c249046181 644 // refer to comments in .h
dflet 3:a8c249046181 645 if (from_port)
dflet 3:a8c249046181 646 *from_port = fromAddr.sin_port;
dflet 3:a8c249046181 647
dflet 3:a8c249046181 648 if (from_ip) {
dflet 3:a8c249046181 649 from_ip[0] = (fromAddr.sin_addr.s_addr & 0xFF000000) >> 24;
dflet 3:a8c249046181 650 from_ip[1] = (fromAddr.sin_addr.s_addr & 0x00FF0000) >> 16;
dflet 3:a8c249046181 651 from_ip[2] = (fromAddr.sin_addr.s_addr & 0x0000FF00) >> 8;
dflet 3:a8c249046181 652 from_ip[3] = (fromAddr.sin_addr.s_addr & 0x000000FF);
dflet 3:a8c249046181 653 *ip_len = 4;
dflet 3:a8c249046181 654 }
dflet 3:a8c249046181 655
dflet 3:a8c249046181 656 return (iStatus);
dflet 3:a8c249046181 657
dflet 3:a8c249046181 658 } // end of function
dflet 3:a8c249046181 659
dflet 3:a8c249046181 660 }//namespace mbed_mqtt
dflet 3:a8c249046181 661