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