version_2.0

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
Kojto
Date:
Thu Sep 19 07:55:14 2013 +0000
Revision:
0:615c697c33b0
Child:
4:15b58c119a0a
initial commit

Who changed what in which revision?

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