BLE mbed Endpoint network stack for mbedConnectorInterface. The stack makes use of a special BLE Socket abstraction to create socket() semantics over BLE.

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.h Source File

Socket.h

00001 /* Copyright (C) 2012 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 "network_stubs.h"
00022 
00023 class TimeInterval;
00024 
00025 /** Socket file descriptor and select wrapper
00026   */
00027 class Socket {
00028 public:
00029     /** Socket
00030      */
00031     Socket();
00032     
00033     /** Set blocking or non-blocking mode of the socket and a timeout on
00034         blocking socket operations
00035     \param blocking  true for blocking mode, false for non-blocking mode.
00036     \param timeout   timeout in ms [Default: (1500)ms].
00037     */
00038     void set_blocking(bool blocking, unsigned int timeout=1500);
00039     
00040     /** Set socket options
00041     \param level     stack level (see: lwip/sockets.h)
00042     \param optname   option ID
00043     \param optval    option value
00044     \param socklen_t length of the option value
00045     \return 0 on success, -1 on failure
00046     */
00047     int set_option(int level, int optname, const void *optval, socklen_t optlen);
00048     
00049     /** Get socket options
00050         \param level     stack level (see: lwip/sockets.h)
00051         \param optname   option ID
00052         \param optval    buffer pointer where to write the option value
00053         \param socklen_t length of the option value
00054         \return 0 on success, -1 on failure
00055         */
00056     int get_option(int level, int optname, void *optval, socklen_t *optlen);
00057     
00058     /** Close the socket
00059         \param shutdown   free the left-over data in message queues
00060      */
00061     int close(bool shutdown=true);
00062     
00063     ~Socket();
00064     
00065 protected:
00066     int _sock_fd;
00067     int init_socket(int type);
00068     
00069     int wait_readable(TimeInterval& timeout);
00070     int wait_writable(TimeInterval& timeout);
00071     
00072     bool _blocking;
00073     unsigned int _timeout;
00074     
00075 private:
00076     int select(struct timeval *timeout, bool read, bool write);
00077 };
00078 
00079 /** Time interval class used to specify timeouts
00080  */
00081 class TimeInterval {
00082     friend class Socket;
00083 
00084 public:
00085     /** Time Interval
00086      \param ms time interval expressed in milliseconds
00087       */
00088     TimeInterval(unsigned int ms);
00089     
00090 private:
00091     struct timeval _time;
00092 };
00093 
00094 #endif /* SOCKET_H_ */