NNN50 WIFI_API library
Dependents: NNN50_CE_Test_UDP NNN50_linux_firmware NNN50_SoftAP_HelloWorld NNN50_BLEWIFISensor ... more
This is mbed compatible EthernetInterface lib exclude for Delta DFCM-NNN50 platform.
Additional information and examples can be found in mbed Handbook
EthernetInterface/Socket/Socket.h@32:8298a2fb074f, 2017-09-04 (annotated)
- Committer:
- tsungta
- Date:
- Mon Sep 04 05:40:11 2017 +0000
- Revision:
- 32:8298a2fb074f
56:f4cc53f; Add getRSSI() to readout RSSI while connected with AP router; Add SSL support refer to TCPSocketConnection.connect()
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tsungta | 32:8298a2fb074f | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
| tsungta | 32:8298a2fb074f | 2 | * |
| tsungta | 32:8298a2fb074f | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| tsungta | 32:8298a2fb074f | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| tsungta | 32:8298a2fb074f | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| tsungta | 32:8298a2fb074f | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| tsungta | 32:8298a2fb074f | 7 | * furnished to do so, subject to the following conditions: |
| tsungta | 32:8298a2fb074f | 8 | * |
| tsungta | 32:8298a2fb074f | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| tsungta | 32:8298a2fb074f | 10 | * substantial portions of the Software. |
| tsungta | 32:8298a2fb074f | 11 | * |
| tsungta | 32:8298a2fb074f | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| tsungta | 32:8298a2fb074f | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| tsungta | 32:8298a2fb074f | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| tsungta | 32:8298a2fb074f | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| tsungta | 32:8298a2fb074f | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| tsungta | 32:8298a2fb074f | 17 | */ |
| tsungta | 32:8298a2fb074f | 18 | #ifndef SOCKET_H_ |
| tsungta | 32:8298a2fb074f | 19 | #define SOCKET_H_ |
| tsungta | 32:8298a2fb074f | 20 | |
| tsungta | 32:8298a2fb074f | 21 | //Tsungta #include "lwip/sockets.h" |
| tsungta | 32:8298a2fb074f | 22 | //Tsungta #include "lwip/netdb.h" |
| tsungta | 32:8298a2fb074f | 23 | |
| tsungta | 32:8298a2fb074f | 24 | // following are added by Tsungta |
| tsungta | 32:8298a2fb074f | 25 | #define SOCKET_CB_ARRAY_SIZE TCP_SOCK_MAX*2//double up space to prevent unexpected _sock, in some case _sock may > than TCP_SOCK_MAX |
| tsungta | 32:8298a2fb074f | 26 | #include "stdlib.h" |
| tsungta | 32:8298a2fb074f | 27 | #include <string.h> |
| tsungta | 32:8298a2fb074f | 28 | #include "driver/include/m2m_wifi.h"//Tsungta |
| tsungta | 32:8298a2fb074f | 29 | #include "socket/include/socket.h"//Tsungta |
| tsungta | 32:8298a2fb074f | 30 | #include <stddef.h> |
| tsungta | 32:8298a2fb074f | 31 | #include "stdint.h" |
| tsungta | 32:8298a2fb074f | 32 | #define DELAY_MS_UNIT 25 |
| tsungta | 32:8298a2fb074f | 33 | #define DELAY_SEC_UNIT 0.025 |
| tsungta | 32:8298a2fb074f | 34 | #define socklen_t uint32_t |
| tsungta | 32:8298a2fb074f | 35 | struct timeval { |
| tsungta | 32:8298a2fb074f | 36 | long tv_sec; /* seconds */ |
| tsungta | 32:8298a2fb074f | 37 | long tv_usec; /* and microseconds */ |
| tsungta | 32:8298a2fb074f | 38 | }; |
| tsungta | 32:8298a2fb074f | 39 | //Tsungta |
| tsungta | 32:8298a2fb074f | 40 | |
| tsungta | 32:8298a2fb074f | 41 | //DNS |
| tsungta | 32:8298a2fb074f | 42 | inline struct hostent *gethostbyname(const char *name) { |
| tsungta | 32:8298a2fb074f | 43 | return 0;//Tsungta lwip_gethostbyname(name); |
| tsungta | 32:8298a2fb074f | 44 | } |
| tsungta | 32:8298a2fb074f | 45 | |
| tsungta | 32:8298a2fb074f | 46 | //Tsungta |
| tsungta | 32:8298a2fb074f | 47 | //inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) { |
| tsungta | 32:8298a2fb074f | 48 | // return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop); |
| tsungta | 32:8298a2fb074f | 49 | //} |
| tsungta | 32:8298a2fb074f | 50 | |
| tsungta | 32:8298a2fb074f | 51 | class TimeInterval; |
| tsungta | 32:8298a2fb074f | 52 | |
| tsungta | 32:8298a2fb074f | 53 | /** Socket file descriptor and select wrapper |
| tsungta | 32:8298a2fb074f | 54 | */ |
| tsungta | 32:8298a2fb074f | 55 | class Socket { |
| tsungta | 32:8298a2fb074f | 56 | public: |
| tsungta | 32:8298a2fb074f | 57 | /** Socket |
| tsungta | 32:8298a2fb074f | 58 | */ |
| tsungta | 32:8298a2fb074f | 59 | Socket(); |
| tsungta | 32:8298a2fb074f | 60 | |
| tsungta | 32:8298a2fb074f | 61 | /** Set blocking or non-blocking mode of the socket and a timeout on |
| tsungta | 32:8298a2fb074f | 62 | blocking socket operations |
| tsungta | 32:8298a2fb074f | 63 | \param blocking true for blocking mode, false for non-blocking mode. |
| tsungta | 32:8298a2fb074f | 64 | \param timeout timeout in ms [Default: (1500)ms]. |
| tsungta | 32:8298a2fb074f | 65 | */ |
| tsungta | 32:8298a2fb074f | 66 | void set_blocking(bool blocking, unsigned int timeout=1500); |
| tsungta | 32:8298a2fb074f | 67 | |
| tsungta | 32:8298a2fb074f | 68 | /** Set socket options |
| tsungta | 32:8298a2fb074f | 69 | \param level stack level (see: lwip/sockets.h) |
| tsungta | 32:8298a2fb074f | 70 | \param optname option ID |
| tsungta | 32:8298a2fb074f | 71 | \param optval option value |
| tsungta | 32:8298a2fb074f | 72 | \param socklen_t length of the option value |
| tsungta | 32:8298a2fb074f | 73 | \return 0 on success, -1 on failure |
| tsungta | 32:8298a2fb074f | 74 | */ |
| tsungta | 32:8298a2fb074f | 75 | int set_option(int level, int optname, const void *optval, socklen_t optlen); |
| tsungta | 32:8298a2fb074f | 76 | |
| tsungta | 32:8298a2fb074f | 77 | /** Get socket options |
| tsungta | 32:8298a2fb074f | 78 | \param level stack level (see: lwip/sockets.h) |
| tsungta | 32:8298a2fb074f | 79 | \param optname option ID |
| tsungta | 32:8298a2fb074f | 80 | \param optval buffer pointer where to write the option value |
| tsungta | 32:8298a2fb074f | 81 | \param socklen_t length of the option value |
| tsungta | 32:8298a2fb074f | 82 | \return 0 on success, -1 on failure |
| tsungta | 32:8298a2fb074f | 83 | */ |
| tsungta | 32:8298a2fb074f | 84 | int get_option(int level, int optname, void *optval, socklen_t *optlen); |
| tsungta | 32:8298a2fb074f | 85 | |
| tsungta | 32:8298a2fb074f | 86 | /** Close the socket |
| tsungta | 32:8298a2fb074f | 87 | \param shutdown free the left-over data in message queues |
| tsungta | 32:8298a2fb074f | 88 | */ |
| tsungta | 32:8298a2fb074f | 89 | int close(bool shutdown=true); |
| tsungta | 32:8298a2fb074f | 90 | |
| tsungta | 32:8298a2fb074f | 91 | ~Socket(); |
| tsungta | 32:8298a2fb074f | 92 | |
| tsungta | 32:8298a2fb074f | 93 | protected: |
| tsungta | 32:8298a2fb074f | 94 | int _sock_fd; |
| tsungta | 32:8298a2fb074f | 95 | int init_socket(int type, bool sslSocket = false); |
| tsungta | 32:8298a2fb074f | 96 | |
| tsungta | 32:8298a2fb074f | 97 | int wait_readable(TimeInterval& timeout); |
| tsungta | 32:8298a2fb074f | 98 | int wait_writable(TimeInterval& timeout); |
| tsungta | 32:8298a2fb074f | 99 | |
| tsungta | 32:8298a2fb074f | 100 | bool _blocking; |
| tsungta | 32:8298a2fb074f | 101 | unsigned int _timeout; |
| tsungta | 32:8298a2fb074f | 102 | |
| tsungta | 32:8298a2fb074f | 103 | private: |
| tsungta | 32:8298a2fb074f | 104 | int select(struct timeval *timeout, bool read, bool write); |
| tsungta | 32:8298a2fb074f | 105 | bool _is_socketInit;//Tsungta |
| tsungta | 32:8298a2fb074f | 106 | }; |
| tsungta | 32:8298a2fb074f | 107 | |
| tsungta | 32:8298a2fb074f | 108 | /** Time interval class used to specify timeouts |
| tsungta | 32:8298a2fb074f | 109 | */ |
| tsungta | 32:8298a2fb074f | 110 | class TimeInterval { |
| tsungta | 32:8298a2fb074f | 111 | friend class Socket; |
| tsungta | 32:8298a2fb074f | 112 | |
| tsungta | 32:8298a2fb074f | 113 | public: |
| tsungta | 32:8298a2fb074f | 114 | /** Time Interval |
| tsungta | 32:8298a2fb074f | 115 | \param ms time interval expressed in milliseconds |
| tsungta | 32:8298a2fb074f | 116 | */ |
| tsungta | 32:8298a2fb074f | 117 | TimeInterval(unsigned int ms); |
| tsungta | 32:8298a2fb074f | 118 | |
| tsungta | 32:8298a2fb074f | 119 | private: |
| tsungta | 32:8298a2fb074f | 120 | struct timeval _time; |
| tsungta | 32:8298a2fb074f | 121 | }; |
| tsungta | 32:8298a2fb074f | 122 | |
| tsungta | 32:8298a2fb074f | 123 | #endif /* SOCKET_H_ */ |
| tsungta | 32:8298a2fb074f | 124 |