erez i / cc3000_hostdriver_mbedsocket

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.h Source File

Socket.h

00001 /* Copyright (C) 2013 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 #ifndef SOCKET_H
00019 #define SOCKET_H
00020 
00021 #include "cc3000.h"
00022 
00023 using namespace mbed_cc3000;
00024 
00025 class TimeInterval;
00026 
00027 /** Socket file descriptor and select wrapper
00028   */
00029 class Socket {
00030 public:
00031     /** Socket
00032      */
00033     Socket();
00034 
00035     /** Set blocking or non-blocking mode of the socket and a timeout on
00036         blocking socket operations
00037     \param blocking  true for blocking mode, false for non-blocking mode.
00038     \param timeout   timeout in ms [Default: (1500)ms].
00039     */
00040     void set_blocking(bool blocking, unsigned int timeout=1500);
00041 
00042     /** Set socket options
00043     \param level     stack level (see: lwip/sockets.h)
00044     \param optname   option ID
00045     \param optval    option value
00046     \param socklen_t length of the option value
00047     \return 0 on success, -1 on failure
00048     */
00049     int set_option(int level, int optname, const void *optval, socklen_t optlen);
00050 
00051     /** Get socket options
00052         \param level     stack level (see: lwip/sockets.h)
00053         \param optname   option ID
00054         \param optval    buffer pointer where to write the option value
00055         \param socklen_t length of the option value
00056         \return 0 on success, -1 on failure
00057         */
00058     int get_option(int level, int optname, void *optval, socklen_t *optlen);
00059 
00060 
00061     /** Close the socket file descriptor
00062      */
00063     int close();
00064 
00065     ~Socket();
00066 
00067 protected:
00068     int _sock_fd;
00069 
00070     int init_socket(int type, int protocol);
00071 
00072     int wait_readable(TimeInterval& timeout);
00073     int wait_writable(TimeInterval& timeout);
00074 
00075     bool _blocking;
00076     int _timeout;
00077 
00078     cc3000 *_cc3000_module;
00079 private:
00080     int select(struct timeval *timeout, bool read, bool write);
00081 };
00082 
00083 /** Time interval class used to specify timeouts
00084  */
00085 class TimeInterval {
00086     friend class Socket;
00087 
00088 public:
00089     /** Time Interval
00090      \param ms time interval expressed in milliseconds
00091       */
00092     TimeInterval(unsigned int ms);
00093 
00094 private:
00095     struct timeval _time;
00096 };
00097 
00098 #endif /* SOCKET_H */