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