TI's MQTT Demo with freertos CM4F

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc31xx_sl_net.cpp Source File

cc31xx_sl_net.cpp

00001 /******************************************************************************
00002  *
00003  *   Copyright (C) 2014 Texas Instruments Incorporated
00004  *
00005  *   All rights reserved. Property of Texas Instruments Incorporated.
00006  *   Restricted rights to use, duplicate or disclose this code are
00007  *   granted through contract.
00008  *
00009  *   The program may not be used without the written permission of
00010  *   Texas Instruments Incorporated or against the terms and conditions
00011  *   stipulated in the agreement under which this program has been supplied,
00012  *   and under no circumstances can it be used with non-TI connectivity device.
00013  *
00014  ******************************************************************************/
00015 //#include "mbed.h"
00016 #include "cc3100_simplelink.h"
00017 #include "cc3100_sl_common.h"
00018 #include "cc31xx_sl_net.h"
00019 #include "sl_mqtt_client.h"
00020 #include "mqtt_client.h"
00021 #include "cc3200_platform.h"
00022 #include "cc3100.h"
00023 #include "fPtr_func.h"
00024 #include "myBoardInit.h"
00025 #include "cli_uart.h"
00026 
00027 #define PRINT_BUF_LEN    128
00028 extern int8_t print_buf[PRINT_BUF_LEN];
00029 
00030 using namespace mbed_cc3100;
00031 
00032 #if (THIS_BOARD == MBED_BOARD_LPC1768)
00033 cc3100 _cc3100_module_(p16, p17, p9, p10, p8, SPI(p5, p6, p7));//LPC1768  irq, nHib, cs, mosi, miso, sck
00034 //cc3100 _cc3100_module_(p9, p10, p8, SPI(p11, p12, p13));//LPC1768  irq, nHib, cs, mosi, miso, sck
00035 #elif (THIS_BOARD == Seeed_Arch_Max)
00036 cc3100 _cc3100_module_(PB_9, PB_8, PD_12, PD_13, PD_11, SPI(PB_5, PB_4, PB_3));//Seeed_Arch_Max  irq, nHib, cs, mosi, miso, sck
00037 #elif (THIS_BOARD == ST_MBED_NUCLEOF103)
00038 class cc3100 _cc3100_module_(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF103  irq, nHib, cs, mosi, miso, sck
00039 #elif (THIS_BOARD == ST_MBED_NUCLEOF411)
00040 class cc3100 _cc3100_module_(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF411  irq, nHib, cs, mosi, miso, sck
00041 #elif (THIS_BOARD == ST_MBED_NUCLEOF401)
00042 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
00043 #elif (THIS_BOARD == EA_MBED_LPC4088)
00044 class cc3100 _cc3100_module_(p14, p15, p9, p10, p8, SPI(p5, p6, p7));//LPC4088  irq, nHib, cs, mosi, miso, sck
00045 #elif (THIS_BOARD == LPCXpresso4337)
00046 class cc3100 _cc3100_module_(P2_2, P3_5, P1_2, SPI(P1_4, P1_3, PF_4));//LPCXpresso4337  irq, nHib, cs, mosi, miso, sck
00047 #endif
00048 
00049 namespace mbed_mqtt {
00050 
00051 #ifdef DEBUG_NET_DEV
00052 extern int32_t (*debug_printf)(const char *fmt, ...);
00053 #define PRINTF(x,...)    debug_printf(x,##__VA_ARGS__)
00054 #else
00055 #define PRINTF(x,...)
00056 #endif
00057 /*
00058  3200 Devices specific Network Services Implementation
00059  */
00060 
00061 #define LISTEN_QUE_SIZE 2
00062 
00063 //*****************************************************************************
00064 //                      GLOBAL VARIABLES
00065 //*****************************************************************************
00066 
00067 //*****************************************************************************
00068 //                      STATIC FUNCTIONS
00069 //*****************************************************************************
00070 
00071 #ifdef DEBUG_NET_DEV
00072 static int32_t buf_printf(const uint8_t *buf, uint32_t len, uint32_t idt)
00073 {
00074     int32_t i = 0;
00075     for(i = 0; i < len; i++)
00076     {
00077         memset(print_buf, 0x00, PRINT_BUF_LEN);
00078         sprintf((char*) print_buf, "%02x ", *buf++);
00079         Uart_Write((uint8_t *) print_buf);
00080 
00081         if(0x03 == (i & 0x03))
00082         Uart_Write((uint8_t *)" ");
00083 
00084         if(0x0F == (i & 0x0F))
00085         {
00086             int32_t j = 0;
00087             Uart_Write((uint8_t *)"\n\r");
00088 
00089             for(j = 0; j < idt; j++)
00090             Uart_Write((uint8_t *)" ");
00091         }
00092     }
00093 
00094     Uart_Write((uint8_t *)"\n\r");
00095 
00096     return len;
00097 }
00098 #endif
00099 
00100 /*-----------------------------------------------------------------------------
00101  Open a TCP socket and modify its properties i.e security options if req.
00102  Socket properties modified in this function are based on the options set
00103  outside the scope of this function.
00104  Returns a valid handle on success, otherwise a negative number.
00105  -----------------------------------------------------------------------------*/
00106 
00107 static int32_t create_socket(uint32_t nwconn_opts, struct secure_conn *nw_security_opts)
00108 {
00109 
00110     int32_t MqttSocketFd, Status;
00111 
00112     //local variables for creating secure socket
00113     uint8_t SecurityMethod;
00114     uint32_t SecurityCypher;
00115     int8_t i;
00116 
00117     //If TLS is required
00118     if ((nwconn_opts & DEV_NETCONN_OPT_SEC) != 0) // bit was set to 1
00119             {
00120         MqttSocketFd = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_STREAM, SL_SEC_SOCKET);
00121         if (MqttSocketFd < 0) {
00122             memset(print_buf, 0x00, PRINT_BUF_LEN);
00123             sprintf((char*) print_buf, "MqttSocketFd  fail %i\r\n",MqttSocketFd);
00124             Uart_Write((uint8_t *) print_buf);
00125             return (MqttSocketFd);
00126         }
00127 
00128         SecurityMethod = *((uint8_t *) (nw_security_opts->method));
00129         SecurityCypher = *((uint32_t *) (nw_security_opts->cipher));
00130 
00131         if (nw_security_opts->n_file < 1  || nw_security_opts->n_file > 4 ) {
00132             Uart_Write((uint8_t*)"\n\r ERROR: security files missing or wrong number of security files\n\r");
00133             Uart_Write((uint8_t*)"\n\r ERROR: Did not create socket\n\r");
00134             return (-1);
00135         }
00136 
00137         //Set Socket Options that were just defined
00138         Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SL_SOL_SOCKET, SL_SO_SECMETHOD,
00139                 &SecurityMethod, sizeof(SecurityMethod));
00140         if (Status < 0) {
00141             memset(print_buf, 0x00, PRINT_BUF_LEN);
00142             sprintf((char*) print_buf, "Status error %i socket closed\n\r",Status);
00143             Uart_Write((uint8_t *) print_buf);
00144             _cc3100_module_._socket.sl_Close(MqttSocketFd);
00145             return (Status);
00146         }
00147 
00148         Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SL_SOL_SOCKET, SL_SO_SECURE_MASK,
00149                 &SecurityCypher, sizeof(SecurityCypher));
00150         if (Status < 0) {
00151             memset(print_buf, 0x00, PRINT_BUF_LEN);
00152             sprintf((char*) print_buf, "Status error %i socket closed\n\r",Status);
00153             Uart_Write((uint8_t *) print_buf);
00154             _cc3100_module_._socket.sl_Close(MqttSocketFd);
00155             return (Status);
00156         }
00157 
00158         if(nw_security_opts->n_file == 1){
00159             Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CA_FILE_NAME,
00160                      nw_security_opts->files[0], strlen(nw_security_opts->files[0]));
00161             if (Status < 0) {
00162                 memset(print_buf, 0x00, PRINT_BUF_LEN);
00163                 sprintf((char*) print_buf, "Status error %i socket closed\n\r",Status);
00164                 Uart_Write((uint8_t *) print_buf);
00165                 _cc3100_module_._socket.sl_Close(MqttSocketFd);
00166                 return (Status);
00167             }
00168         }else{
00169             for(i=0; i<nw_security_opts->n_file;i++){
00170                 if(NULL != nw_security_opts->files[i]){
00171                     Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SL_SOL_SOCKET,
00172                             (SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME+i),
00173                             nw_security_opts->files[i],
00174                             strlen(nw_security_opts->files[i]));
00175                     if (Status < 0) {
00176                         memset(print_buf, 0x00, PRINT_BUF_LEN);
00177                         sprintf((char*) print_buf, "Status error %i socket closed\n\r",Status);
00178                         Uart_Write((uint8_t *) print_buf);
00179                         _cc3100_module_._socket.sl_Close(MqttSocketFd);
00180                         return (Status);
00181                     }
00182                 }
00183             }
00184         }
00185 
00186     }
00187     // If no TLS required
00188     else {
00189         // check to create a udp or tcp socket
00190         if ((nwconn_opts & DEV_NETCONN_OPT_UDP) != 0) // bit is set ; create a udp socket
00191                 {
00192             MqttSocketFd = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, SL_IPPROTO_UDP);
00193         } else // socket for tcp
00194         {
00195             MqttSocketFd = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_STREAM,
00196             SL_IPPROTO_TCP); // consider putting 0 in place of SL_IPPROTO_TCP
00197         }
00198     }
00199     
00200     return (MqttSocketFd);
00201 
00202 } // end of function
00203 
00204 /*-----------------------------------------------------------------------------
00205  This function takes an ipv4 address in dot format i.e "a.b.c.d" and returns the
00206  ip address in Network byte Order, which can be used in connect call
00207  It returns 0, if a valid ip address is not detected.
00208  -----------------------------------------------------------------------------*/
00209 
00210 static uint32_t svr_addr_NB_order_IPV4(char *svr_addr_str)
00211 {
00212     uint8_t addr[4];
00213     int8_t i = 0;
00214     char *token;
00215     uint32_t svr_addr;
00216     int32_t temp;
00217 
00218     /*take a temporary copy of the string. strtok modifies the input string*/
00219     int8_t svr_addr_size = strlen(svr_addr_str);
00220     char *svr_addr_cpy = (char*)malloc(svr_addr_size + 1); //1 for null
00221     if(NULL == svr_addr_cpy) return 0;
00222     strcpy(svr_addr_cpy, svr_addr_str);
00223 
00224     memset(print_buf, 0x00, PRINT_BUF_LEN);
00225     sprintf((char*) print_buf, "\n\r server address = %s\n\r", svr_addr_cpy);
00226     Uart_Write((uint8_t *) print_buf);
00227     memset(print_buf, 0x00, PRINT_BUF_LEN);
00228     sprintf((char*) print_buf, "\n\r server address string length = %d\n\r", strlen(svr_addr_cpy));
00229     Uart_Write((uint8_t *) print_buf);
00230 
00231     /* get the first token */
00232     token = strtok((char*)svr_addr_cpy, ".");
00233 
00234     /* walk through other tokens */
00235     while (token != NULL) {
00236         temp = atoi((const char*)token);
00237 
00238         //check for invalid tokens or if already 4 tokens were obtained
00239         if ((temp < 0) || (temp > 255) || (i >= 4)) {
00240             free(svr_addr_cpy);
00241             return (0);
00242         }
00243 
00244         addr[i++] = (uint8_t) temp;
00245         token = strtok(NULL, ".");
00246     }
00247 
00248     // check if exactly 4 valid tokens are available or not
00249     if (i != 4) {
00250         free(svr_addr_cpy);
00251         return (0);
00252     }
00253 
00254     //form address if above test passed
00255     svr_addr = *((uint32_t *) &addr);
00256     free(svr_addr_cpy);
00257 
00258     return (svr_addr);
00259 
00260 } // end of function
00261 
00262 //*****************************************************************************
00263 //                      Network Services functions
00264 //*****************************************************************************
00265 
00266 /*-----------------------------------------------------------------------------
00267  Open a TCP socket with required properties
00268  Also connect to the server.
00269  Returns a valid handle on success, NULL on failure.
00270  -----------------------------------------------------------------------------*/
00271 
00272 int32_t comm_open(uint32_t nwconn_opts, const char *server_addr, uint16_t port_number,
00273         const struct secure_conn *nw_security)
00274 {
00275 
00276     int32_t Status, MqttSocketFd;
00277 
00278     SlSockAddrIn_t LocalAddr; //address of the server to connect to
00279     int32_t LocalAddrSize;
00280 
00281     uint32_t uiIP;
00282 
00283     // create socket
00284     MqttSocketFd = create_socket(nwconn_opts,
00285             (struct secure_conn*) nw_security);
00286 
00287     if (MqttSocketFd < 0) {
00288         Uart_Write((uint8_t*)"\n\r ERROR: Could not create a socket.\n\r");
00289         return -1;
00290     }
00291 
00292     if ((nwconn_opts & DEV_NETCONN_OPT_UDP) != 0) // bit is set ; create a udp socket
00293             {
00294         //filling the UDP server socket address
00295         LocalAddr.sin_family = SL_AF_INET;
00296         LocalAddr.sin_port = _cc3100_module_._socket.sl_Htons((unsigned short) port_number);
00297         LocalAddr.sin_addr.s_addr = 0;
00298         LocalAddrSize = sizeof(SlSockAddrIn_t);
00299 
00300         Status = _cc3100_module_._socket.sl_Bind(MqttSocketFd, (SlSockAddr_t *) &LocalAddr,
00301                 LocalAddrSize);
00302         if (Status < 0) {
00303             // error
00304             Uart_Write((uint8_t*)"\n\r ERROR: Could not bind socket.\n\r");
00305             _cc3100_module_._socket.sl_Close(MqttSocketFd);
00306             return -1;
00307         }
00308     } else // do tcp connection
00309     {
00310         // get the ip address of server to do tcp connect
00311         if ((nwconn_opts & DEV_NETCONN_OPT_URL) != 0) // server address is a URL
00312                 {
00313             Status = _cc3100_module_._netapp.sl_NetAppDnsGetHostByName((uint8_t*) server_addr,
00314                     strlen(server_addr), (uint32_t*) &uiIP, SL_AF_INET);
00315 
00316             if (Status < 0) {
00317                 Uart_Write((uint8_t*)"\n\r ERROR: Could not resolve the ip address of the server \n\r");
00318                 return (-1);
00319             }
00320             // convert the address to network byte order as the function returns in host byte order
00321             uiIP = _cc3100_module_._socket.sl_Htonl(uiIP);
00322         } else // server address is a string in dot notation
00323         {
00324             if ((nwconn_opts & DEV_NETCONN_OPT_IP6) != 0) // server address is an IPV6 address string
00325                     {
00326                 Uart_Write((uint8_t*)"\n\r ERROR: Currently do not support IPV6 addresses \n\r");
00327                 return (-1);
00328             } else // address is an IPv4 string
00329             {
00330                 // get the server ip address in Network Byte order
00331                 uiIP = svr_addr_NB_order_IPV4((char*) server_addr);
00332                 if (0 == uiIP) {
00333                     Uart_Write((uint8_t*)"\n\r ERROR: Could not resolve the ip address of the server \n\r");
00334                     return (-1);
00335                 }
00336             }
00337 
00338         }
00339 
00340         LocalAddr.sin_family = SL_AF_INET;
00341         LocalAddr.sin_addr.s_addr = uiIP;
00342         LocalAddr.sin_port = _cc3100_module_._socket.sl_Htons(port_number);
00343         LocalAddrSize = sizeof(SlSockAddrIn_t);
00344 
00345         // do tcp connect
00346         Status = _cc3100_module_._socket.sl_Connect(MqttSocketFd, (SlSockAddr_t *) &LocalAddr,
00347                 LocalAddrSize);
00348 
00349         if (Status < 0) {
00350             if (SL_ESECSNOVERIFY != Status) {
00351                 Uart_Write((uint8_t*)" \n\r ERROR: Could not establish connection to server.\n\r");
00352                 Uart_Write((uint8_t*)" \n\r ERROR: Closing the socket.\n\r");
00353 
00354                 _cc3100_module_._socket.sl_Close(MqttSocketFd);
00355                 return (-1);
00356             } else // SL_ESECSNOVERIFY == Status
00357             {
00358                 Uart_Write((uint8_t*)" \n\r ERROR: Could not establish secure connection to server.\n\r");
00359                 Uart_Write((uint8_t*)" \n\r Continuing with unsecure connection to server...\n\r");
00360             }
00361         }
00362 
00363         // Success
00364         Uart_Write((uint8_t*)"\n\r Connected to server ....\n\r");
00365 
00366     } // end of doing binding port to udp socket or doing tcp connect
00367 
00368     // set Timer for host processor
00369     platform_timer_init();
00370     
00371     return (MqttSocketFd);
00372 
00373 } // end of function
00374 
00375 int32_t tcp_send(int32_t comm, const uint8_t *buf, uint32_t len, void *ctx)
00376 {
00377 
00378     int32_t Status;
00379 
00380     PRINTF("\n\r TCP send invoked for data with len %d\n\r", len);PRINTF("\n\r Sent Data : ");
00381 
00382 #ifdef DEBUG_NET_DEV
00383     buf_printf(buf, len, 0);
00384 #endif
00385 
00386     Status = _cc3100_module_._socket.sl_Send(comm, buf, len, 0);
00387 
00388     return (Status);
00389 
00390 } // end of function
00391 
00392 int32_t tcp_recv(int32_t comm, uint8_t *buf, uint32_t len, uint32_t wait_secs, bool *timed_out, void *ctx)
00393 {
00394     int32_t Status;
00395     int32_t MqttSocketFd = comm;
00396 
00397 #ifdef SOC_RCV_TIMEOUT_OPT
00398 
00399     // socket receive time out options
00400     SlTimeval_t timeVal;
00401 
00402     // recv time out options
00403     timeVal.tv_sec = wait_secs;      // Seconds
00404     timeVal.tv_usec = 0;          // Microseconds. 10000 microseconds resoultion
00405 
00406     /*------------------- setting receive timeout option on socket ---------------------*/
00407 
00408     Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SOL_SOCKET, SL_SO_RCVTIMEO, &timeVal,
00409             sizeof(timeVal));
00410 
00411     if (Status == 0) {
00412 
00413     } else if (Status < 0) {
00414         Uart_Write((uint8_t*)"\n\r ERROR: setting socket recv_timeout_option unsuccessfull! \n\r");
00415 
00416     }
00417     /*--------------end of setting receive timeout option on socket ---------------------*/
00418 
00419 #endif
00420 
00421 //    printf("\n\r TCP recv invoked ...\n\r");
00422     *timed_out = 0;
00423 
00424     Status = _cc3100_module_._socket.sl_Recv(MqttSocketFd, buf, len, 0);
00425 
00426     if (Status > 0) {
00427 #ifdef DEBUG_NET_DEV
00428         buf_printf(buf, Status, 0);
00429 #endif
00430     }
00431 
00432     if (0 == Status) {
00433         Uart_Write((uint8_t*)"\n\r Connection Closed by peer....\n\r");
00434     }
00435 
00436     if (SL_EAGAIN == Status) {
00437         Uart_Write((uint8_t*)"\n\r ERROR: Recv Time out error on server socket \n\r");
00438         *timed_out = 1;
00439     }
00440 
00441     return (Status);
00442 
00443 }             // end of function
00444 
00445 int32_t comm_close(int32_t comm) {
00446     int32_t Status;
00447 
00448     Status = _cc3100_module_._socket.sl_Close(comm);
00449     return (Status);
00450 
00451 }             // end of function
00452 
00453 uint32_t rtc_secs(void) {
00454     return(platform_get_time_in_secs());   
00455 }             // end of function
00456 
00457 //--------------------------- adding functions for server functionalities ---------------------------
00458 
00459 int32_t tcp_listen(uint32_t nwconn_info, uint16_t port_number,
00460         const struct secure_conn *nw_security)
00461 {
00462     SlSockAddrIn_t sLocalAddr;
00463     int32_t iSockID, iAddrSize;
00464     int32_t iStatus;
00465 
00466     //filling the TCP server socket address
00467     sLocalAddr.sin_family = SL_AF_INET;
00468     sLocalAddr.sin_port = _cc3100_module_._socket.sl_Htons(port_number);
00469     sLocalAddr.sin_addr.s_addr = 0;
00470     iAddrSize = sizeof(SlSockAddrIn_t);
00471 
00472     // creating a TCP socket
00473     iSockID = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);
00474     if (iSockID < 0) {
00475         // error
00476         return (-1);
00477     }
00478 
00479     // binding the TCP socket to the TCP server address
00480     iStatus = _cc3100_module_._socket.sl_Bind(iSockID, (SlSockAddr_t *) &sLocalAddr, iAddrSize);
00481     if (iStatus < 0) {
00482         // error
00483         _cc3100_module_._socket.sl_Close(iSockID);
00484         return (-1);
00485     }
00486 
00487     // putting the socket for listening to the incoming TCP connection
00488     iStatus = _cc3100_module_._socket.sl_Listen(iSockID, LISTEN_QUE_SIZE);
00489     if (iStatus < 0) {
00490         _cc3100_module_._socket.sl_Close(iSockID);
00491         return (-1);
00492     }
00493 
00494     memset(print_buf, 0x00, PRINT_BUF_LEN);
00495     sprintf((char*) print_buf, "\n\r\t Server Socket created and listening on port number: %d! \n\r", port_number);
00496     Uart_Write((uint8_t *) print_buf);
00497 
00498     return (iSockID);
00499 
00500 }             // end of function
00501 
00502 int32_t tcp_select(int32_t *recv_cvec, int32_t *send_cvec, int32_t *rsvd_cvec, uint32_t wait_secs)
00503 {
00504 
00505     SlTimeval_t tv, *p_tv;
00506     SlFdSet_t rdfds;
00507     int32_t rd_idx = 0, wr_idx = 0, max_fd = 0;
00508     int32_t rv = 0;
00509 
00510     tv.tv_sec = wait_secs;
00511     tv.tv_usec = 0;
00512 
00513     p_tv = (0xFFFFFFFF != wait_secs) ? &tv : NULL;
00514 
00515     _cc3100_module_._socket.SL_FD_ZERO(&rdfds);
00516 
00517     while (-1 != recv_cvec[rd_idx]) {
00518         int32_t fd = recv_cvec[rd_idx++];
00519 
00520         _cc3100_module_._socket.SL_FD_SET(fd, &rdfds);
00521 
00522         if (max_fd < fd){
00523             max_fd = fd;
00524         }    
00525     }
00526 
00527 //    printf("Blocking network for (%s) %u secs to monitor %d fd(s)\n\r",
00528 //            p_tv? "finite" : "forever", wait_secs, rd_idx);
00529 
00530     rv = _cc3100_module_._socket.sl_Select(max_fd + 1, &rdfds, NULL, NULL, p_tv);
00531 
00532     if (rv <= 0) {
00533 //        printf("Select Failed %i\n\r",rv);
00534         return rv;
00535     }
00536 
00537     rd_idx = 0;
00538     while (-1 != recv_cvec[rd_idx]) {
00539         int32_t fd = recv_cvec[rd_idx++];
00540         if (_cc3100_module_._socket.SL_FD_ISSET(fd, &rdfds))
00541             recv_cvec[wr_idx++] = fd;
00542     }
00543 
00544     recv_cvec[wr_idx] = NULL;
00545 
00546 //    printf("Number of sockets on which activity is observed = %d \n\r", wr_idx);
00547 
00548     return (wr_idx);
00549 }             // end of function
00550 
00551 int32_t tcp_accept(int32_t listen_hnd, uint8_t *client_ip, uint32_t *ip_length)
00552 {
00553     int32_t new_fd;
00554     SlSockAddrIn_t client_addr = {0}; // client address
00555     SlSocklen_t cl_addr_size;
00556 
00557     cl_addr_size = sizeof(client_addr);
00558 
00559     new_fd = _cc3100_module_._socket.sl_Accept(listen_hnd, (SlSockAddr_t *) &client_addr,
00560                        &cl_addr_size);
00561 
00562     if (new_fd < 0) {
00563         Uart_Write((uint8_t*)"\n\r ERROR: in accept \n\r");
00564         return (NULL);
00565     }
00566 
00567     client_ip[0] = (client_addr.sin_addr.s_addr & 0xFF000000) >> 24;
00568     client_ip[1] = (client_addr.sin_addr.s_addr & 0x00FF0000) >> 16;
00569     client_ip[2] = (client_addr.sin_addr.s_addr & 0x0000FF00) >> 8;
00570     client_ip[3] = (client_addr.sin_addr.s_addr & 0x000000FF);
00571 
00572     *ip_length = 4;
00573 
00574     return new_fd;
00575 
00576 } // end of function
00577 
00578 //--------------------------- adding functions for udp functionalities ---------------------------
00579 
00580 int32_t send_dest(int32_t comm, const uint8_t *buf, uint32_t len, uint16_t dest_port,
00581               const uint8_t *dest_ip, uint32_t ip_len)
00582 {
00583 
00584     int32_t iSockID = (int32_t) comm;
00585     int32_t iStatus, iAddrSize;
00586     SlSockAddrIn_t sAddr;
00587     uint32_t uiDestIp;
00588 
00589     //get destination ip
00590 #if 0
00591     uiDestIp = svr_addr_NB_order_IPV4((uint8_t*)dest_ip); //assuming a IPV4 address is passed in dot notation.
00592     if( 0 == uiDestIp )
00593     {
00594         Uart_Write((uint8_t*)"\n\r ERROR: Could not resolve the ip address of the destination \n\r");
00595         return(-1);
00596     }
00597 #else
00598     uiDestIp = (((uint32_t) dest_ip[0] << 24) | ((uint32_t) dest_ip[1] << 16)
00599             | (dest_ip[2] << 8) | (dest_ip[3]));
00600 
00601 //    printf("Writing to %d, %08x\r\n", (int32_t)comm, uiDestIp);
00602 
00603 #endif
00604 
00605     //filling the UDP server socket address
00606     sAddr.sin_family = SL_AF_INET;
00607     sAddr.sin_port = _cc3100_module_._socket.sl_Htons((unsigned short) dest_port);
00608     sAddr.sin_addr.s_addr = _cc3100_module_._socket.sl_Htonl(uiDestIp);
00609 
00610     iAddrSize = sizeof(SlSockAddrIn_t);
00611 
00612     // sending packet
00613     iStatus = _cc3100_module_._socket.sl_SendTo(iSockID, buf, len, 0, (SlSockAddr_t *) &sAddr,
00614             iAddrSize);
00615 
00616     if (iStatus <= 0) {
00617         // error
00618         _cc3100_module_._socket.sl_Close(iSockID);
00619         Uart_Write((uint8_t*)"Error: Closed the UDP socket\n\r");
00620     }
00621     
00622     return (iStatus);
00623 
00624 } // end of function
00625 
00626 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)
00627 {
00628 
00629     int32_t iSockID = (int32_t) comm;
00630     int32_t iStatus, iAddrSize;
00631     SlSockAddrIn_t fromAddr = {0};
00632 
00633     iAddrSize = sizeof(SlSockAddrIn_t);
00634 
00635     iStatus = _cc3100_module_._socket.sl_RecvFrom(iSockID, buf, len, 0, (SlSockAddr_t *) &fromAddr,
00636                           (SlSocklen_t*) &iAddrSize);
00637 
00638     if (iStatus < 0) {
00639         // error
00640         _cc3100_module_._socket.sl_Close(iSockID);
00641         Uart_Write((uint8_t*)"Error: Closed the UDP socket\n\r");
00642         return (iStatus);
00643     }
00644 
00645     //else populate from ip, from_port and ip_len parameters
00646     // refer to comments in .h
00647     if (from_port)
00648         *from_port = fromAddr.sin_port;
00649 
00650     if (from_ip) {
00651         from_ip[0] = (fromAddr.sin_addr.s_addr & 0xFF000000) >> 24;
00652         from_ip[1] = (fromAddr.sin_addr.s_addr & 0x00FF0000) >> 16;
00653         from_ip[2] = (fromAddr.sin_addr.s_addr & 0x0000FF00) >> 8;
00654         from_ip[3] = (fromAddr.sin_addr.s_addr & 0x000000FF);
00655         *ip_len = 4;
00656     }
00657 
00658     return (iStatus);
00659 
00660 }    // end of function
00661 
00662 }//namespace mbed_mqtt 
00663 
00664 
00665