NNN50 WIFI_API library

Dependents:   NNN50_CE_Test_UDP NNN50_linux_firmware NNN50_SoftAP_HelloWorld NNN50_BLEWIFISensor ... more

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 //Tsungta   #include "lwip/sockets.h"
00022 //Tsungta   #include "lwip/netdb.h"
00023 
00024 // following are added by Tsungta
00025 #define SOCKET_CB_ARRAY_SIZE    TCP_SOCK_MAX*2//double up space to prevent unexpected _sock, in some case _sock may > than TCP_SOCK_MAX 
00026 #include "stdlib.h"
00027 #include <string.h>
00028 #include "driver/include/m2m_wifi.h"//Tsungta
00029 #include "socket/include/socket.h"//Tsungta
00030 #include <stddef.h>
00031 #include "stdint.h"
00032 #define DELAY_MS_UNIT   25
00033 #define DELAY_SEC_UNIT   0.025
00034 #define socklen_t uint32_t
00035 struct timeval {
00036   long    tv_sec;         /* seconds */
00037   long    tv_usec;        /* and microseconds */
00038 };
00039 //Tsungta
00040 
00041 //DNS
00042 inline struct hostent *gethostbyname (const char *name) {
00043   return 0;//Tsungta    lwip_gethostbyname(name);
00044 }
00045 
00046 //Tsungta
00047 //inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) {
00048 //  return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
00049 //}
00050 
00051 class TimeInterval;
00052 
00053 /** Socket file descriptor and select wrapper
00054   */
00055 class Socket {
00056 public:
00057     /** Socket
00058      */
00059     Socket();
00060     
00061     /** Set blocking or non-blocking mode of the socket and a timeout on
00062         blocking socket operations
00063     \param blocking  true for blocking mode, false for non-blocking mode.
00064     \param timeout   timeout in ms [Default: (1500)ms].
00065     */
00066     void set_blocking(bool blocking, unsigned int timeout=1500);
00067     
00068     /** Set socket options
00069     \param level     stack level (see: lwip/sockets.h)
00070     \param optname   option ID
00071     \param optval    option value
00072     \param socklen_t length of the option value
00073     \return 0 on success, -1 on failure
00074     */
00075     int set_option(int level, int optname, const void *optval, socklen_t optlen);
00076     
00077     /** Get socket options
00078         \param level     stack level (see: lwip/sockets.h)
00079         \param optname   option ID
00080         \param optval    buffer pointer where to write the option value
00081         \param socklen_t length of the option value
00082         \return 0 on success, -1 on failure
00083         */
00084     int get_option(int level, int optname, void *optval, socklen_t *optlen);
00085     
00086     /** Close the socket
00087         \param shutdown   free the left-over data in message queues
00088      */
00089     int close(bool shutdown=true);
00090     
00091     ~Socket();
00092     
00093 protected:
00094     int _sock_fd;
00095     int init_socket(int type, bool sslSocket = false);
00096     
00097     int wait_readable(TimeInterval& timeout);
00098     int wait_writable(TimeInterval& timeout);
00099     
00100     bool _blocking;
00101     unsigned int _timeout;
00102     
00103 private:
00104     int select(struct timeval *timeout, bool read, bool write);
00105     bool _is_socketInit;//Tsungta
00106 };
00107 
00108 /** Time interval class used to specify timeouts
00109  */
00110 class TimeInterval {
00111     friend class Socket;
00112 
00113 public:
00114     /** Time Interval
00115      \param ms time interval expressed in milliseconds
00116       */
00117     TimeInterval(unsigned int ms);
00118     
00119 private:
00120     struct timeval _time;
00121 };
00122 
00123 #endif /* SOCKET_H_ */
00124