[6319] | 1 | /** @file wlan_exp_ip_udp_socket_t.c |
---|
| 2 | * @brief Mango wlan_exp IP/UDP Library (Socket) |
---|
| 3 | * |
---|
| 4 | * @copyright Copyright 2014-2019, Mango Communications. All rights reserved. |
---|
| 5 | * Distributed under the Mango Reference Design license (https://mangocomm.com/802.11/license) |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /***************************** Include Files *********************************/ |
---|
| 9 | #include "wlan_mac_high_sw_config.h" |
---|
| 10 | |
---|
| 11 | #if WLAN_SW_CONFIG_ENABLE_WLAN_EXP |
---|
| 12 | |
---|
| 13 | // Xilinx / Standard library includes |
---|
| 14 | #include "xparameters.h" |
---|
| 15 | #include "xstatus.h" |
---|
| 16 | #include "xil_io.h" |
---|
| 17 | #include "stdlib.h" |
---|
| 18 | #include "stdio.h" |
---|
| 19 | #include "string.h" |
---|
| 20 | |
---|
| 21 | // Mango wlan_exp IP/UDP Library includes |
---|
| 22 | #include "wlan_exp_ip_udp.h" |
---|
| 23 | #include "wlan_exp_ip_udp_socket.h" |
---|
| 24 | #include "wlan_exp_ip_udp_arp.h" |
---|
| 25 | |
---|
| 26 | #include "wlan_high_types.h" |
---|
| 27 | |
---|
| 28 | /*********************** Global Variable Definitions *************************/ |
---|
| 29 | wlan_exp_ip_udp_socket_t ETH_sockets[WLAN_EXP_IP_UDP_NUM_SOCKETS]; |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | /*************************** Function Prototypes *****************************/ |
---|
| 33 | |
---|
| 34 | int _socket_check(int socket_index); |
---|
| 35 | wlan_exp_ip_udp_socket_t* _socket_get_l(int socket_index); |
---|
| 36 | wlan_exp_ip_udp_socket_t* _socket_alloc_l(); |
---|
| 37 | |
---|
| 38 | /******************************** Functions **********************************/ |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | /*****************************************************************************/ |
---|
| 42 | /** |
---|
| 43 | * Initialize the Socket structures |
---|
| 44 | * |
---|
| 45 | * @param None |
---|
| 46 | * |
---|
| 47 | * @return None |
---|
| 48 | * |
---|
| 49 | ******************************************************************************/ |
---|
| 50 | void socket_init() { |
---|
| 51 | u32 i; |
---|
| 52 | |
---|
| 53 | // Initialize all the sockets |
---|
| 54 | for (i = 0; i < WLAN_EXP_IP_UDP_NUM_SOCKETS; i++) { |
---|
| 55 | ETH_sockets[i].index = i; |
---|
| 56 | ETH_sockets[i].state = SOCKET_CLOSED; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | /*****************************************************************************/ |
---|
| 62 | /** |
---|
| 63 | * Allocate a socket from the library |
---|
| 64 | * |
---|
| 65 | * @param domain - Communications domain in which a socket is to be created |
---|
| 66 | * - AF_INET - Inter-network: UDP, TCP, etc. (only supported value) |
---|
| 67 | * @param type - Type of socket to be created |
---|
| 68 | * - SOCK_DGRAM - Socket datagram (connectionless) (UDP) (only supported value) |
---|
| 69 | * @param protocol - Particular protocol to be used with the socket |
---|
| 70 | * - 0 - Use default protocol appropriate for the requested socket type (only supported value) |
---|
| 71 | * |
---|
| 72 | * @return int - Socket Index |
---|
| 73 | * - WLAN_EXP_IP_UDP_FAILURE if there was an error |
---|
| 74 | * |
---|
| 75 | * @note Only UDP sockets are supported: socket_alloc(AF_INET, SOCK_DGRAM, 0); |
---|
| 76 | * |
---|
| 77 | *****************************************************************************/ |
---|
| 78 | int socket_alloc(int domain, int type, int protocol) { |
---|
| 79 | |
---|
| 80 | wlan_exp_ip_udp_socket_t* socket = NULL; |
---|
| 81 | |
---|
| 82 | // Check this is an appropriate socket |
---|
| 83 | if ((domain == AF_INET) && (type == SOCK_DGRAM) && (protocol == 0)) { |
---|
| 84 | |
---|
| 85 | // Allocate the socket from the library |
---|
| 86 | socket = _socket_alloc_l(); |
---|
| 87 | |
---|
| 88 | if (socket != NULL) { |
---|
| 89 | // Record values in the socket |
---|
| 90 | socket->sin_family = domain; |
---|
| 91 | |
---|
| 92 | // Return the index of the socket |
---|
| 93 | return socket->index; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | } else { |
---|
| 97 | |
---|
| 98 | // !!! TBD !!! - Print error message |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | return WLAN_FAILURE; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | /*****************************************************************************/ |
---|
| 107 | /** |
---|
| 108 | * Bind the socket to an Ethernet device |
---|
| 109 | * |
---|
| 110 | * @param socket_index - Index of the socket |
---|
| 111 | * @param port - Port to bind |
---|
| 112 | * |
---|
| 113 | * @return int - Status |
---|
| 114 | * - WLAN_EXP_IP_UDP_SUCCESS if socket is bound |
---|
| 115 | * - WLAN_EXP_IP_UDP_FAILURE if there was an error |
---|
| 116 | * |
---|
| 117 | *****************************************************************************/ |
---|
| 118 | int socket_bind_eth(int socket_index, u16 port) { |
---|
| 119 | |
---|
| 120 | wlan_exp_ip_udp_socket_t* socket; |
---|
| 121 | u8 ip_addr[IP_ADDR_LEN]; // Ethernet device IP address |
---|
| 122 | |
---|
| 123 | eth_get_ip_addr(ip_addr); |
---|
| 124 | // Get the socket from the socket index |
---|
| 125 | socket = _socket_get_l(socket_index); |
---|
| 126 | |
---|
| 127 | if (socket != NULL) { |
---|
| 128 | // Populate the socket |
---|
| 129 | socket->state = SOCKET_OPEN; |
---|
| 130 | socket->sin_port = port; |
---|
| 131 | // This is a recent change. We store the IP address little endian. It must be |
---|
| 132 | // endian swapped in order to be used in any outgoing transmissions. Previous |
---|
| 133 | // code was inconsistent -- swapping some things (like IP address) and not others |
---|
| 134 | // (like port). |
---|
| 135 | memcpy(socket->sin_addr, ip_addr, IP_ADDR_LEN); |
---|
| 136 | } else { |
---|
| 137 | |
---|
| 138 | // !!! TBD !!! - Print error message |
---|
| 139 | |
---|
| 140 | return WLAN_FAILURE; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | return WLAN_SUCCESS; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | sockaddr_in_t socket_get(int socket_index){ |
---|
| 147 | sockaddr_in_t sockaddr_in; |
---|
| 148 | bzero(&sockaddr_in, sizeof(sockaddr_in_t)); |
---|
| 149 | wlan_exp_ip_udp_socket_t* socket = _socket_get_l(socket_index); |
---|
| 150 | |
---|
| 151 | if(socket == NULL) return sockaddr_in; |
---|
| 152 | |
---|
| 153 | sockaddr_in.sin_family = AF_INET; |
---|
| 154 | memcpy(sockaddr_in.sin_addr.s_addr, socket->sin_addr, IP_ADDR_LEN); |
---|
| 155 | sockaddr_in.sin_port = socket->sin_port; |
---|
| 156 | |
---|
| 157 | return sockaddr_in; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | /*****************************************************************************/ |
---|
| 163 | /** |
---|
| 164 | * Close a socket |
---|
| 165 | * |
---|
| 166 | * @param socket_index - Index of socket to send the message on |
---|
| 167 | * @param to - Pointer to socket address structure to send the data |
---|
| 168 | * @param buffers - Array of wlan_exp_ip_udp_buffer describing data to send |
---|
| 169 | * @param num_buffers - Number of wlan_exp_ip_udp_buffer in array |
---|
| 170 | * |
---|
| 171 | * @return int - Socket Index |
---|
| 172 | * WLAN_EXP_IP_UDP_FAILURE if there was an error |
---|
| 173 | * |
---|
| 174 | *****************************************************************************/ |
---|
| 175 | void socket_close(int socket_index) { |
---|
| 176 | |
---|
| 177 | wlan_exp_ip_udp_socket_t * socket = NULL; |
---|
| 178 | |
---|
| 179 | // Get the socket from the socket index |
---|
| 180 | socket = _socket_get_l(socket_index); |
---|
| 181 | |
---|
| 182 | if (socket != NULL) { |
---|
| 183 | // Mark the socket as CLOSED |
---|
| 184 | socket->state = SOCKET_CLOSED; |
---|
| 185 | } else { |
---|
| 186 | xil_printf("Error, unable to to close socket %d\n", socket_index); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /*****************************************************************************/ |
---|
| 191 | /** |
---|
| 192 | * Find the socket_index by Ethernet device & Port |
---|
| 193 | * |
---|
| 194 | * @param port - Port of the socket |
---|
| 195 | * |
---|
| 196 | * @return int - Socket Index |
---|
| 197 | * WLAN_EXP_IP_UDP_FAILURE if there was an error |
---|
| 198 | * |
---|
| 199 | *****************************************************************************/ |
---|
| 200 | int socket_find_index_by_port(u16 port) { |
---|
| 201 | |
---|
| 202 | u32 i; |
---|
| 203 | int socket_index = SOCKET_INVALID_SOCKET; |
---|
| 204 | |
---|
| 205 | // Search through socket pool |
---|
| 206 | for (i = 0; i < WLAN_EXP_IP_UDP_NUM_SOCKETS; i++) { |
---|
| 207 | |
---|
| 208 | // Check socket parameters |
---|
| 209 | if ((ETH_sockets[i].state == SOCKET_OPEN) && |
---|
| 210 | (ETH_sockets[i].sin_port == port ) ) { |
---|
| 211 | |
---|
| 212 | socket_index = i; |
---|
| 213 | break; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | return socket_index; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | /**********************************************************************************************************************/ |
---|
| 222 | /** |
---|
| 223 | * @brief Internal Methods |
---|
| 224 | * |
---|
| 225 | **********************************************************************************************************************/ |
---|
| 226 | |
---|
| 227 | /******************************************************************************/ |
---|
| 228 | /** |
---|
| 229 | * Allocate a socket from the global pool |
---|
| 230 | * |
---|
| 231 | * @param None |
---|
| 232 | * |
---|
| 233 | * @return wlan_exp_ip_udp_socket_t * - Pointer to the socket |
---|
| 234 | * NULL if not able to get the socket |
---|
| 235 | * |
---|
| 236 | *****************************************************************************/ |
---|
| 237 | wlan_exp_ip_udp_socket_t* _socket_alloc_l() { |
---|
| 238 | u32 i; |
---|
| 239 | int socket_index = SOCKET_INVALID_SOCKET; |
---|
| 240 | wlan_exp_ip_udp_socket_t* socket = NULL; |
---|
| 241 | |
---|
| 242 | // Search through socket pool for a closed socket |
---|
| 243 | for (i = 0; i < WLAN_EXP_IP_UDP_NUM_SOCKETS; i++) { |
---|
| 244 | if (ETH_sockets[i].state == SOCKET_CLOSED) { |
---|
| 245 | socket_index = i; |
---|
| 246 | break; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | if (socket_index != SOCKET_INVALID_SOCKET) { |
---|
| 251 | // Get the socket from the global structure |
---|
| 252 | socket = &(ETH_sockets[socket_index]); |
---|
| 253 | |
---|
| 254 | // Set the socket state to "ALLOCATED" |
---|
| 255 | socket->state = SOCKET_ALLOCATED; |
---|
| 256 | |
---|
| 257 | } else { |
---|
| 258 | |
---|
| 259 | // !!! TBD !!! - Print error message |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | return socket; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | /******************************************************************************/ |
---|
| 268 | /** |
---|
| 269 | * Get a pointer to the socket from the socket index |
---|
| 270 | * |
---|
| 271 | * @param socket_index - Index of the socket |
---|
| 272 | * |
---|
| 273 | * @return wlan_exp_ip_udp_socket_t * - Pointer to the socket |
---|
| 274 | * NULL if not able to get the socket |
---|
| 275 | * |
---|
| 276 | *****************************************************************************/ |
---|
| 277 | wlan_exp_ip_udp_socket_t* _socket_get_l(int socket_index) { |
---|
| 278 | |
---|
| 279 | wlan_exp_ip_udp_socket_t* socket = NULL; |
---|
| 280 | |
---|
| 281 | // Check the socket index |
---|
| 282 | if (_socket_check(socket_index) == WLAN_SUCCESS) { |
---|
| 283 | |
---|
| 284 | // Get the socket from the global structure |
---|
| 285 | socket = &(ETH_sockets[socket_index]); |
---|
| 286 | |
---|
| 287 | // Check the socket status; if it is closed, then return NULL |
---|
| 288 | if (socket->state == SOCKET_CLOSED) { |
---|
| 289 | |
---|
| 290 | // !!! TBD !!! - Print error message |
---|
| 291 | |
---|
| 292 | socket = NULL; |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | return socket; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | /*****************************************************************************/ |
---|
| 302 | /** |
---|
| 303 | * Check the socket index |
---|
| 304 | * |
---|
| 305 | * @param socket_index - Index of the socket |
---|
| 306 | * |
---|
| 307 | * @return int - Status of the command: |
---|
| 308 | * WLAN_SUCCESS - Command completed successfully |
---|
| 309 | * WLAN_FAILURE - There was an error in the command |
---|
| 310 | * |
---|
| 311 | *****************************************************************************/ |
---|
| 312 | int _socket_check(int socket_index) { |
---|
| 313 | |
---|
| 314 | // Check that the socket_index is valid |
---|
| 315 | if ((socket_index < 0) || (socket_index > WLAN_EXP_IP_UDP_NUM_SOCKETS)) { |
---|
| 316 | |
---|
| 317 | // !!! TBD !!! - Print error message |
---|
| 318 | |
---|
| 319 | return WLAN_FAILURE; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | return WLAN_SUCCESS; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | #endif // #if WLAN_SW_CONFIG_ENABLE_WLAN_EXP |
---|