Update revision to use TI's mqtt and Freertos.

Dependencies:   mbed client server

Fork of cc3100_Test_mqtt_CM3 by David Fletcher

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 class cc3100 _cc3100_module_(PE_5, PE_4, PE_6, SPI(PB_5, PB_4, PB_3));
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_12, P2_9, 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");Uart_Write((uint8_t*)" \n\r ERROR: Closing the socket.\n\r");
00352 
00353                 _cc3100_module_._socket.sl_Close(MqttSocketFd);
00354                 return (-1);
00355             } else // SL_ESECSNOVERIFY == Status
00356             {
00357                 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");
00358             }
00359         }
00360 
00361         // Success
00362         Uart_Write((uint8_t*)"\n\r Connected to server ....\n\r");
00363 
00364     } // end of doing binding port to udp socket or doing tcp connect
00365 
00366     // set Timer for host processor
00367     platform_timer_init();
00368     
00369     return (MqttSocketFd);
00370 
00371 } // end of function
00372 
00373 int32_t tcp_send(int32_t comm, const uint8_t *buf, uint32_t len, void *ctx)
00374 {
00375 
00376     int32_t Status;
00377 
00378     PRINTF("\n\r TCP send invoked for data with len %d\n\r", len);PRINTF("\n\r Sent Data : ");
00379 
00380 #ifdef DEBUG_NET_DEV
00381     buf_printf(buf, len, 0);
00382 #endif
00383 
00384     Status = _cc3100_module_._socket.sl_Send(comm, buf, len, 0);
00385 
00386     return (Status);
00387 
00388 } // end of function
00389 
00390 int32_t tcp_recv(int32_t comm, uint8_t *buf, uint32_t len, uint32_t wait_secs, bool *timed_out, void *ctx)
00391 {
00392     int32_t Status;
00393     int32_t MqttSocketFd = comm;
00394 
00395 #ifdef SOC_RCV_TIMEOUT_OPT
00396 
00397     // socket receive time out options
00398     SlTimeval_t timeVal;
00399 
00400     // recv time out options
00401     timeVal.tv_sec = wait_secs;      // Seconds
00402     timeVal.tv_usec = 0;          // Microseconds. 10000 microseconds resoultion
00403 
00404     /*------------------- setting receive timeout option on socket ---------------------*/
00405 
00406     Status = _cc3100_module_._socket.sl_SetSockOpt(MqttSocketFd, SOL_SOCKET, SL_SO_RCVTIMEO, &timeVal,
00407             sizeof(timeVal));
00408 
00409     if (Status == 0) {
00410 
00411     } else if (Status < 0) {
00412         Uart_Write((uint8_t*)"\n\r ERROR: setting socket recv_timeout_option unsuccessfull! \n\r");
00413 
00414     }
00415     /*--------------end of setting receive timeout option on socket ---------------------*/
00416 
00417 #endif
00418 
00419 //    printf("\n\r TCP recv invoked ...\n\r");
00420     *timed_out = 0;
00421 
00422     Status = _cc3100_module_._socket.sl_Recv(MqttSocketFd, buf, len, 0);
00423 
00424     if (Status > 0) {
00425 #ifdef DEBUG_NET_DEV
00426         buf_printf(buf, Status, 0);
00427 #endif
00428     }
00429 
00430     if (0 == Status) {
00431         Uart_Write((uint8_t*)"\n\r Connection Closed by peer....\n\r");
00432     }
00433 
00434     if (SL_EAGAIN == Status) {
00435         Uart_Write((uint8_t*)"\n\r ERROR: Recv Time out error on server socket \n\r");
00436         *timed_out = 1;
00437     }
00438 
00439     return (Status);
00440 
00441 }             // end of function
00442 
00443 int32_t comm_close(int32_t comm) {
00444     int32_t Status;
00445 
00446     Status = _cc3100_module_._socket.sl_Close(comm);
00447     return (Status);
00448 
00449 }             // end of function
00450 
00451 uint32_t rtc_secs(void) {
00452     return(platform_get_time_in_secs());   
00453 }             // end of function
00454 
00455 //--------------------------- adding functions for server functionalities ---------------------------
00456 
00457 int32_t tcp_listen(uint32_t nwconn_info, uint16_t port_number,
00458         const struct secure_conn *nw_security)
00459 {
00460     SlSockAddrIn_t sLocalAddr;
00461     int32_t iSockID, iAddrSize;
00462     int32_t iStatus;
00463 
00464     //filling the TCP server socket address
00465     sLocalAddr.sin_family = SL_AF_INET;
00466     sLocalAddr.sin_port = _cc3100_module_._socket.sl_Htons(port_number);
00467     sLocalAddr.sin_addr.s_addr = 0;
00468     iAddrSize = sizeof(SlSockAddrIn_t);
00469 
00470     // creating a TCP socket
00471     iSockID = _cc3100_module_._socket.sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);
00472     if (iSockID < 0) {
00473         // error
00474         return (-1);
00475     }
00476 
00477     // binding the TCP socket to the TCP server address
00478     iStatus = _cc3100_module_._socket.sl_Bind(iSockID, (SlSockAddr_t *) &sLocalAddr, iAddrSize);
00479     if (iStatus < 0) {
00480         // error
00481         _cc3100_module_._socket.sl_Close(iSockID);
00482         return (-1);
00483     }
00484 
00485     // putting the socket for listening to the incoming TCP connection
00486     iStatus = _cc3100_module_._socket.sl_Listen(iSockID, LISTEN_QUE_SIZE);
00487     if (iStatus < 0) {
00488         _cc3100_module_._socket.sl_Close(iSockID);
00489         return (-1);
00490     }
00491 
00492     memset(print_buf, 0x00, PRINT_BUF_LEN);
00493     sprintf((char*) print_buf, "\n\r\t Server Socket created and listening on port number: %d! \n\r", port_number);
00494     Uart_Write((uint8_t *) print_buf);
00495 
00496     return (iSockID);
00497 
00498 }             // end of function
00499 
00500 int32_t tcp_select(int32_t *recv_cvec, int32_t *send_cvec, int32_t *rsvd_cvec, uint32_t wait_secs)
00501 {
00502 
00503     SlTimeval_t tv, *p_tv;
00504     SlFdSet_t rdfds;
00505     int32_t rd_idx = 0, wr_idx = 0, max_fd = 0;
00506     int32_t rv = 0;
00507 
00508     tv.tv_sec = wait_secs;
00509     tv.tv_usec = 0;
00510 
00511     p_tv = (0xFFFFFFFF != wait_secs) ? &tv : NULL;
00512 
00513     _cc3100_module_._socket.SL_FD_ZERO(&rdfds);
00514 
00515     while (-1 != recv_cvec[rd_idx]) {
00516         int32_t fd = recv_cvec[rd_idx++];
00517 
00518         _cc3100_module_._socket.SL_FD_SET(fd, &rdfds);
00519 
00520         if (max_fd < fd){
00521             max_fd = fd;
00522         }    
00523     }
00524 
00525 //    printf("Blocking network for (%s) %u secs to monitor %d fd(s)\n\r",
00526 //            p_tv? "finite" : "forever", wait_secs, rd_idx);
00527 
00528     rv = _cc3100_module_._socket.sl_Select(max_fd + 1, &rdfds, NULL, NULL, p_tv);
00529 
00530     if (rv <= 0) {
00531 //        printf("Select Failed %i\n\r",rv);
00532         return rv;
00533     }
00534 
00535     rd_idx = 0;
00536     while (-1 != recv_cvec[rd_idx]) {
00537         int32_t fd = recv_cvec[rd_idx++];
00538         if (_cc3100_module_._socket.SL_FD_ISSET(fd, &rdfds))
00539             recv_cvec[wr_idx++] = fd;
00540     }
00541 
00542     recv_cvec[wr_idx] = NULL;
00543 
00544 //    printf("Number of sockets on which activity is observed = %d \n\r", wr_idx);
00545 
00546     return (wr_idx);
00547 }             // end of function
00548 
00549 int32_t tcp_accept(int32_t listen_hnd, uint8_t *client_ip, uint32_t *ip_length)
00550 {
00551     int32_t new_fd;
00552     SlSockAddrIn_t client_addr = {0}; // client address
00553     SlSocklen_t cl_addr_size;
00554 
00555     cl_addr_size = sizeof(client_addr);
00556 
00557     new_fd = _cc3100_module_._socket.sl_Accept(listen_hnd, (SlSockAddr_t *) &client_addr,
00558                        &cl_addr_size);
00559 
00560     if (new_fd < 0) {
00561         Uart_Write((uint8_t*)"\n\r ERROR: in accept \n\r");
00562         return (NULL);
00563     }
00564 
00565     client_ip[0] = (client_addr.sin_addr.s_addr & 0xFF000000) >> 24;
00566     client_ip[1] = (client_addr.sin_addr.s_addr & 0x00FF0000) >> 16;
00567     client_ip[2] = (client_addr.sin_addr.s_addr & 0x0000FF00) >> 8;
00568     client_ip[3] = (client_addr.sin_addr.s_addr & 0x000000FF);
00569 
00570     *ip_length = 4;
00571 
00572     return new_fd;
00573 
00574 } // end of function
00575 
00576 //--------------------------- adding functions for udp functionalities ---------------------------
00577 
00578 int32_t send_dest(int32_t comm, const uint8_t *buf, uint32_t len, uint16_t dest_port,
00579               const uint8_t *dest_ip, uint32_t ip_len)
00580 {
00581 
00582     int32_t iSockID = (int32_t) comm;
00583     int32_t iStatus, iAddrSize;
00584     SlSockAddrIn_t sAddr;
00585     uint32_t uiDestIp;
00586 
00587     //get destination ip
00588 #if 0
00589     uiDestIp = svr_addr_NB_order_IPV4((uint8_t*)dest_ip); //assuming a IPV4 address is passed in dot notation.
00590     if( 0 == uiDestIp )
00591     {
00592         Uart_Write((uint8_t*)"\n\r ERROR: Could not resolve the ip address of the destination \n\r");
00593         return(-1);
00594     }
00595 #else
00596     uiDestIp = (((uint32_t) dest_ip[0] << 24) | ((uint32_t) dest_ip[1] << 16)
00597             | (dest_ip[2] << 8) | (dest_ip[3]));
00598 
00599 //    printf("Writing to %d, %08x\r\n", (int32_t)comm, uiDestIp);
00600 
00601 #endif
00602 
00603     //filling the UDP server socket address
00604     sAddr.sin_family = SL_AF_INET;
00605     sAddr.sin_port = _cc3100_module_._socket.sl_Htons((unsigned short) dest_port);
00606     sAddr.sin_addr.s_addr = _cc3100_module_._socket.sl_Htonl(uiDestIp);
00607 
00608     iAddrSize = sizeof(SlSockAddrIn_t);
00609 
00610     // sending packet
00611     iStatus = _cc3100_module_._socket.sl_SendTo(iSockID, buf, len, 0, (SlSockAddr_t *) &sAddr,
00612             iAddrSize);
00613 
00614     if (iStatus <= 0) {
00615         // error
00616         _cc3100_module_._socket.sl_Close(iSockID);
00617         Uart_Write((uint8_t*)"Error: Closed the UDP socket\n\r");
00618     }
00619     
00620     return (iStatus);
00621 
00622 } // end of function
00623 
00624 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)
00625 {
00626 
00627     int32_t iSockID = (int32_t) comm;
00628     int32_t iStatus, iAddrSize;
00629     SlSockAddrIn_t fromAddr = {0};
00630 
00631     iAddrSize = sizeof(SlSockAddrIn_t);
00632 
00633     iStatus = _cc3100_module_._socket.sl_RecvFrom(iSockID, buf, len, 0, (SlSockAddr_t *) &fromAddr,
00634                           (SlSocklen_t*) &iAddrSize);
00635 
00636     if (iStatus < 0) {
00637         // error
00638         _cc3100_module_._socket.sl_Close(iSockID);
00639         Uart_Write((uint8_t*)"Error: Closed the UDP socket\n\r");
00640         return (iStatus);
00641     }
00642 
00643     //else populate from ip, from_port and ip_len parameters
00644     // refer to comments in .h
00645     if (from_port)
00646         *from_port = fromAddr.sin_port;
00647 
00648     if (from_ip) {
00649         from_ip[0] = (fromAddr.sin_addr.s_addr & 0xFF000000) >> 24;
00650         from_ip[1] = (fromAddr.sin_addr.s_addr & 0x00FF0000) >> 16;
00651         from_ip[2] = (fromAddr.sin_addr.s_addr & 0x0000FF00) >> 8;
00652         from_ip[3] = (fromAddr.sin_addr.s_addr & 0x000000FF);
00653         *ip_len = 4;
00654     }
00655 
00656     return (iStatus);
00657 
00658 }    // end of function
00659 
00660 }//namespace mbed_mqtt 
00661