Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: NNN50_CE_Test_UDP NNN50_linux_firmware NNN50_SoftAP_HelloWorld NNN50_BLEWIFISensor ... more
EthernetInterface/Socket/Socket.h@28:2abbf8463fa8, 2017-06-29 (annotated)
- Committer:
- tsungta
- Date:
- Thu Jun 29 04:18:26 2017 +0000
- Revision:
- 28:2abbf8463fa8
50:c1cdd22; Compiled with mbed-os 5.5.1 using Optimization Level 3; Host driver is upgrade to 19.5.2; Add API setTXPower and setPPAGain; Remove RTOS relative call function to save RAM space
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tsungta | 28:2abbf8463fa8 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
| tsungta | 28:2abbf8463fa8 | 2 | * |
| tsungta | 28:2abbf8463fa8 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| tsungta | 28:2abbf8463fa8 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| tsungta | 28:2abbf8463fa8 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| tsungta | 28:2abbf8463fa8 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| tsungta | 28:2abbf8463fa8 | 7 | * furnished to do so, subject to the following conditions: |
| tsungta | 28:2abbf8463fa8 | 8 | * |
| tsungta | 28:2abbf8463fa8 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| tsungta | 28:2abbf8463fa8 | 10 | * substantial portions of the Software. |
| tsungta | 28:2abbf8463fa8 | 11 | * |
| tsungta | 28:2abbf8463fa8 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| tsungta | 28:2abbf8463fa8 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| tsungta | 28:2abbf8463fa8 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| tsungta | 28:2abbf8463fa8 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| tsungta | 28:2abbf8463fa8 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| tsungta | 28:2abbf8463fa8 | 17 | */ |
| tsungta | 28:2abbf8463fa8 | 18 | #ifndef SOCKET_H_ |
| tsungta | 28:2abbf8463fa8 | 19 | #define SOCKET_H_ |
| tsungta | 28:2abbf8463fa8 | 20 | |
| tsungta | 28:2abbf8463fa8 | 21 | //Tsungta #include "lwip/sockets.h" |
| tsungta | 28:2abbf8463fa8 | 22 | //Tsungta #include "lwip/netdb.h" |
| tsungta | 28:2abbf8463fa8 | 23 | |
| tsungta | 28:2abbf8463fa8 | 24 | // following are added by Tsungta |
| tsungta | 28:2abbf8463fa8 | 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 | 28:2abbf8463fa8 | 26 | #include "mbed.h" |
| tsungta | 28:2abbf8463fa8 | 27 | #include "driver/include/m2m_wifi.h"//Tsungta |
| tsungta | 28:2abbf8463fa8 | 28 | #include "socket/include/socket.h"//Tsungta |
| tsungta | 28:2abbf8463fa8 | 29 | #include <stddef.h> |
| tsungta | 28:2abbf8463fa8 | 30 | #include "stdint.h" |
| tsungta | 28:2abbf8463fa8 | 31 | #define DELAY_MS_UNIT 25 |
| tsungta | 28:2abbf8463fa8 | 32 | #define DELAY_SEC_UNIT 0.025 |
| tsungta | 28:2abbf8463fa8 | 33 | #define socklen_t uint32_t |
| tsungta | 28:2abbf8463fa8 | 34 | struct timeval { |
| tsungta | 28:2abbf8463fa8 | 35 | long tv_sec; /* seconds */ |
| tsungta | 28:2abbf8463fa8 | 36 | long tv_usec; /* and microseconds */ |
| tsungta | 28:2abbf8463fa8 | 37 | }; |
| tsungta | 28:2abbf8463fa8 | 38 | //Tsungta |
| tsungta | 28:2abbf8463fa8 | 39 | |
| tsungta | 28:2abbf8463fa8 | 40 | //DNS |
| tsungta | 28:2abbf8463fa8 | 41 | inline struct hostent *gethostbyname(const char *name) { |
| tsungta | 28:2abbf8463fa8 | 42 | return 0;//Tsungta lwip_gethostbyname(name); |
| tsungta | 28:2abbf8463fa8 | 43 | } |
| tsungta | 28:2abbf8463fa8 | 44 | |
| tsungta | 28:2abbf8463fa8 | 45 | //Tsungta |
| tsungta | 28:2abbf8463fa8 | 46 | //inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) { |
| tsungta | 28:2abbf8463fa8 | 47 | // return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop); |
| tsungta | 28:2abbf8463fa8 | 48 | //} |
| tsungta | 28:2abbf8463fa8 | 49 | |
| tsungta | 28:2abbf8463fa8 | 50 | class TimeInterval; |
| tsungta | 28:2abbf8463fa8 | 51 | |
| tsungta | 28:2abbf8463fa8 | 52 | /** Socket file descriptor and select wrapper |
| tsungta | 28:2abbf8463fa8 | 53 | */ |
| tsungta | 28:2abbf8463fa8 | 54 | class Socket { |
| tsungta | 28:2abbf8463fa8 | 55 | public: |
| tsungta | 28:2abbf8463fa8 | 56 | /** Socket |
| tsungta | 28:2abbf8463fa8 | 57 | */ |
| tsungta | 28:2abbf8463fa8 | 58 | Socket(); |
| tsungta | 28:2abbf8463fa8 | 59 | |
| tsungta | 28:2abbf8463fa8 | 60 | /** Set blocking or non-blocking mode of the socket and a timeout on |
| tsungta | 28:2abbf8463fa8 | 61 | blocking socket operations |
| tsungta | 28:2abbf8463fa8 | 62 | \param blocking true for blocking mode, false for non-blocking mode. |
| tsungta | 28:2abbf8463fa8 | 63 | \param timeout timeout in ms [Default: (1500)ms]. |
| tsungta | 28:2abbf8463fa8 | 64 | */ |
| tsungta | 28:2abbf8463fa8 | 65 | void set_blocking(bool blocking, unsigned int timeout=1500); |
| tsungta | 28:2abbf8463fa8 | 66 | |
| tsungta | 28:2abbf8463fa8 | 67 | /** Set socket options |
| tsungta | 28:2abbf8463fa8 | 68 | \param level stack level (see: lwip/sockets.h) |
| tsungta | 28:2abbf8463fa8 | 69 | \param optname option ID |
| tsungta | 28:2abbf8463fa8 | 70 | \param optval option value |
| tsungta | 28:2abbf8463fa8 | 71 | \param socklen_t length of the option value |
| tsungta | 28:2abbf8463fa8 | 72 | \return 0 on success, -1 on failure |
| tsungta | 28:2abbf8463fa8 | 73 | */ |
| tsungta | 28:2abbf8463fa8 | 74 | int set_option(int level, int optname, const void *optval, socklen_t optlen); |
| tsungta | 28:2abbf8463fa8 | 75 | |
| tsungta | 28:2abbf8463fa8 | 76 | /** Get socket options |
| tsungta | 28:2abbf8463fa8 | 77 | \param level stack level (see: lwip/sockets.h) |
| tsungta | 28:2abbf8463fa8 | 78 | \param optname option ID |
| tsungta | 28:2abbf8463fa8 | 79 | \param optval buffer pointer where to write the option value |
| tsungta | 28:2abbf8463fa8 | 80 | \param socklen_t length of the option value |
| tsungta | 28:2abbf8463fa8 | 81 | \return 0 on success, -1 on failure |
| tsungta | 28:2abbf8463fa8 | 82 | */ |
| tsungta | 28:2abbf8463fa8 | 83 | int get_option(int level, int optname, void *optval, socklen_t *optlen); |
| tsungta | 28:2abbf8463fa8 | 84 | |
| tsungta | 28:2abbf8463fa8 | 85 | /** Close the socket |
| tsungta | 28:2abbf8463fa8 | 86 | \param shutdown free the left-over data in message queues |
| tsungta | 28:2abbf8463fa8 | 87 | */ |
| tsungta | 28:2abbf8463fa8 | 88 | int close(bool shutdown=true); |
| tsungta | 28:2abbf8463fa8 | 89 | |
| tsungta | 28:2abbf8463fa8 | 90 | ~Socket(); |
| tsungta | 28:2abbf8463fa8 | 91 | |
| tsungta | 28:2abbf8463fa8 | 92 | protected: |
| tsungta | 28:2abbf8463fa8 | 93 | int _sock_fd; |
| tsungta | 28:2abbf8463fa8 | 94 | int init_socket(int type); |
| tsungta | 28:2abbf8463fa8 | 95 | |
| tsungta | 28:2abbf8463fa8 | 96 | int wait_readable(TimeInterval& timeout); |
| tsungta | 28:2abbf8463fa8 | 97 | int wait_writable(TimeInterval& timeout); |
| tsungta | 28:2abbf8463fa8 | 98 | |
| tsungta | 28:2abbf8463fa8 | 99 | bool _blocking; |
| tsungta | 28:2abbf8463fa8 | 100 | unsigned int _timeout; |
| tsungta | 28:2abbf8463fa8 | 101 | |
| tsungta | 28:2abbf8463fa8 | 102 | private: |
| tsungta | 28:2abbf8463fa8 | 103 | int select(struct timeval *timeout, bool read, bool write); |
| tsungta | 28:2abbf8463fa8 | 104 | bool _is_socketInit;//Tsungta |
| tsungta | 28:2abbf8463fa8 | 105 | }; |
| tsungta | 28:2abbf8463fa8 | 106 | |
| tsungta | 28:2abbf8463fa8 | 107 | /** Time interval class used to specify timeouts |
| tsungta | 28:2abbf8463fa8 | 108 | */ |
| tsungta | 28:2abbf8463fa8 | 109 | class TimeInterval { |
| tsungta | 28:2abbf8463fa8 | 110 | friend class Socket; |
| tsungta | 28:2abbf8463fa8 | 111 | |
| tsungta | 28:2abbf8463fa8 | 112 | public: |
| tsungta | 28:2abbf8463fa8 | 113 | /** Time Interval |
| tsungta | 28:2abbf8463fa8 | 114 | \param ms time interval expressed in milliseconds |
| tsungta | 28:2abbf8463fa8 | 115 | */ |
| tsungta | 28:2abbf8463fa8 | 116 | TimeInterval(unsigned int ms); |
| tsungta | 28:2abbf8463fa8 | 117 | |
| tsungta | 28:2abbf8463fa8 | 118 | private: |
| tsungta | 28:2abbf8463fa8 | 119 | struct timeval _time; |
| tsungta | 28:2abbf8463fa8 | 120 | }; |
| tsungta | 28:2abbf8463fa8 | 121 | |
| tsungta | 28:2abbf8463fa8 | 122 | #endif /* SOCKET_H_ */ |
| tsungta | 28:2abbf8463fa8 | 123 |