Home Alert System
Dependencies: PWM_Tone_Library DHT
EthernetInterface/Socket/Socket.h@5:569a4894abc1, 2019-03-08 (annotated)
- Committer:
- aziz111
- Date:
- Fri Mar 08 17:15:02 2019 +0000
- Revision:
- 5:569a4894abc1
- Parent:
- 3:78f223d34f36
Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ethaderu | 3:78f223d34f36 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
ethaderu | 3:78f223d34f36 | 2 | * |
ethaderu | 3:78f223d34f36 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ethaderu | 3:78f223d34f36 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
ethaderu | 3:78f223d34f36 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ethaderu | 3:78f223d34f36 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ethaderu | 3:78f223d34f36 | 7 | * furnished to do so, subject to the following conditions: |
ethaderu | 3:78f223d34f36 | 8 | * |
ethaderu | 3:78f223d34f36 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ethaderu | 3:78f223d34f36 | 10 | * substantial portions of the Software. |
ethaderu | 3:78f223d34f36 | 11 | * |
ethaderu | 3:78f223d34f36 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ethaderu | 3:78f223d34f36 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ethaderu | 3:78f223d34f36 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ethaderu | 3:78f223d34f36 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ethaderu | 3:78f223d34f36 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ethaderu | 3:78f223d34f36 | 17 | */ |
ethaderu | 3:78f223d34f36 | 18 | #ifndef SOCKET_H_ |
ethaderu | 3:78f223d34f36 | 19 | #define SOCKET_H_ |
ethaderu | 3:78f223d34f36 | 20 | |
ethaderu | 3:78f223d34f36 | 21 | #include "lwip/sockets.h" |
ethaderu | 3:78f223d34f36 | 22 | #include "lwip/netdb.h" |
ethaderu | 3:78f223d34f36 | 23 | |
ethaderu | 3:78f223d34f36 | 24 | //DNS |
ethaderu | 3:78f223d34f36 | 25 | inline struct hostent *gethostbyname(const char *name) { |
ethaderu | 3:78f223d34f36 | 26 | return lwip_gethostbyname(name); |
ethaderu | 3:78f223d34f36 | 27 | } |
ethaderu | 3:78f223d34f36 | 28 | |
ethaderu | 3:78f223d34f36 | 29 | inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) { |
ethaderu | 3:78f223d34f36 | 30 | return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop); |
ethaderu | 3:78f223d34f36 | 31 | } |
ethaderu | 3:78f223d34f36 | 32 | |
ethaderu | 3:78f223d34f36 | 33 | class TimeInterval; |
ethaderu | 3:78f223d34f36 | 34 | |
ethaderu | 3:78f223d34f36 | 35 | /** Socket file descriptor and select wrapper |
ethaderu | 3:78f223d34f36 | 36 | */ |
ethaderu | 3:78f223d34f36 | 37 | class Socket { |
ethaderu | 3:78f223d34f36 | 38 | public: |
ethaderu | 3:78f223d34f36 | 39 | /** Socket |
ethaderu | 3:78f223d34f36 | 40 | */ |
ethaderu | 3:78f223d34f36 | 41 | Socket(); |
ethaderu | 3:78f223d34f36 | 42 | |
ethaderu | 3:78f223d34f36 | 43 | /** Set blocking or non-blocking mode of the socket and a timeout on |
ethaderu | 3:78f223d34f36 | 44 | blocking socket operations |
ethaderu | 3:78f223d34f36 | 45 | \param blocking true for blocking mode, false for non-blocking mode. |
ethaderu | 3:78f223d34f36 | 46 | \param timeout timeout in ms [Default: (1500)ms]. |
ethaderu | 3:78f223d34f36 | 47 | */ |
ethaderu | 3:78f223d34f36 | 48 | void set_blocking(bool blocking, unsigned int timeout=1500); |
ethaderu | 3:78f223d34f36 | 49 | |
ethaderu | 3:78f223d34f36 | 50 | /** Set socket options |
ethaderu | 3:78f223d34f36 | 51 | \param level stack level (see: lwip/sockets.h) |
ethaderu | 3:78f223d34f36 | 52 | \param optname option ID |
ethaderu | 3:78f223d34f36 | 53 | \param optval option value |
ethaderu | 3:78f223d34f36 | 54 | \param socklen_t length of the option value |
ethaderu | 3:78f223d34f36 | 55 | \return 0 on success, -1 on failure |
ethaderu | 3:78f223d34f36 | 56 | */ |
ethaderu | 3:78f223d34f36 | 57 | int set_option(int level, int optname, const void *optval, socklen_t optlen); |
ethaderu | 3:78f223d34f36 | 58 | |
ethaderu | 3:78f223d34f36 | 59 | /** Get socket options |
ethaderu | 3:78f223d34f36 | 60 | \param level stack level (see: lwip/sockets.h) |
ethaderu | 3:78f223d34f36 | 61 | \param optname option ID |
ethaderu | 3:78f223d34f36 | 62 | \param optval buffer pointer where to write the option value |
ethaderu | 3:78f223d34f36 | 63 | \param socklen_t length of the option value |
ethaderu | 3:78f223d34f36 | 64 | \return 0 on success, -1 on failure |
ethaderu | 3:78f223d34f36 | 65 | */ |
ethaderu | 3:78f223d34f36 | 66 | int get_option(int level, int optname, void *optval, socklen_t *optlen); |
ethaderu | 3:78f223d34f36 | 67 | |
ethaderu | 3:78f223d34f36 | 68 | /** Close the socket |
ethaderu | 3:78f223d34f36 | 69 | \param shutdown free the left-over data in message queues |
ethaderu | 3:78f223d34f36 | 70 | */ |
ethaderu | 3:78f223d34f36 | 71 | int close(bool shutdown=true); |
ethaderu | 3:78f223d34f36 | 72 | |
ethaderu | 3:78f223d34f36 | 73 | ~Socket(); |
ethaderu | 3:78f223d34f36 | 74 | |
ethaderu | 3:78f223d34f36 | 75 | protected: |
ethaderu | 3:78f223d34f36 | 76 | int _sock_fd; |
ethaderu | 3:78f223d34f36 | 77 | int init_socket(int type); |
ethaderu | 3:78f223d34f36 | 78 | |
ethaderu | 3:78f223d34f36 | 79 | int wait_readable(TimeInterval& timeout); |
ethaderu | 3:78f223d34f36 | 80 | int wait_writable(TimeInterval& timeout); |
ethaderu | 3:78f223d34f36 | 81 | |
ethaderu | 3:78f223d34f36 | 82 | bool _blocking; |
ethaderu | 3:78f223d34f36 | 83 | unsigned int _timeout; |
ethaderu | 3:78f223d34f36 | 84 | |
ethaderu | 3:78f223d34f36 | 85 | private: |
ethaderu | 3:78f223d34f36 | 86 | int select(struct timeval *timeout, bool read, bool write); |
ethaderu | 3:78f223d34f36 | 87 | }; |
ethaderu | 3:78f223d34f36 | 88 | |
ethaderu | 3:78f223d34f36 | 89 | /** Time interval class used to specify timeouts |
ethaderu | 3:78f223d34f36 | 90 | */ |
ethaderu | 3:78f223d34f36 | 91 | class TimeInterval { |
ethaderu | 3:78f223d34f36 | 92 | friend class Socket; |
ethaderu | 3:78f223d34f36 | 93 | |
ethaderu | 3:78f223d34f36 | 94 | public: |
ethaderu | 3:78f223d34f36 | 95 | /** Time Interval |
ethaderu | 3:78f223d34f36 | 96 | \param ms time interval expressed in milliseconds |
ethaderu | 3:78f223d34f36 | 97 | */ |
ethaderu | 3:78f223d34f36 | 98 | TimeInterval(unsigned int ms); |
ethaderu | 3:78f223d34f36 | 99 | |
ethaderu | 3:78f223d34f36 | 100 | private: |
ethaderu | 3:78f223d34f36 | 101 | struct timeval _time; |
ethaderu | 3:78f223d34f36 | 102 | }; |
ethaderu | 3:78f223d34f36 | 103 | |
ethaderu | 3:78f223d34f36 | 104 | #endif /* SOCKET_H_ */ |