Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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