Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lowlevel.c Source File

lowlevel.c

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *    Sergio R. Caprile - "commonalization" from prior samples and/or documentation extension
00016  *******************************************************************************/
00017 
00018 #include <sys/types.h>
00019 
00020 #if !defined(SOCKET_ERROR)
00021     /** error in socket operation */
00022     #define SOCKET_ERROR -1
00023 #endif
00024 
00025 #if defined(WIN32)
00026 /* default on Windows is 64 - increase to make Linux and Windows the same */
00027 #define FD_SETSIZE 1024
00028 #include <winsock2.h>
00029 #include <ws2tcpip.h>
00030 #define MAXHOSTNAMELEN 256
00031 #define EAGAIN WSAEWOULDBLOCK
00032 #define EINTR WSAEINTR
00033 #define EINVAL WSAEINVAL
00034 #define EINPROGRESS WSAEINPROGRESS
00035 #define EWOULDBLOCK WSAEWOULDBLOCK
00036 #define ENOTCONN WSAENOTCONN
00037 #define ECONNRESET WSAECONNRESET
00038 #define ioctl ioctlsocket
00039 #define socklen_t int
00040 #else
00041 #define INVALID_SOCKET SOCKET_ERROR
00042 #include <sys/socket.h>
00043 #include <sys/param.h>
00044 #include <sys/time.h>
00045 #include <netinet/in.h>
00046 #include <netinet/tcp.h>
00047 #include <arpa/inet.h>
00048 #include <netdb.h>
00049 #include <stdio.h>
00050 #include <unistd.h>
00051 #include <errno.h>
00052 #include <fcntl.h>
00053 #include <string.h>
00054 #include <stdlib.h>
00055 #endif
00056 
00057 #if defined(WIN32)
00058 #include <Iphlpapi.h>
00059 #else
00060 #include <sys/ioctl.h>
00061 #include <net/if.h>
00062 #endif
00063 
00064 /**
00065 This simple low-level implementation assumes a single connection for a single thread. Thus, a static
00066 variable is used for that connection.
00067 On other scenarios, the user must solve this by taking into account that the current implementation of
00068 MQTTSNPacket_read() has a function pointer for a function call to get the data to a buffer, but no provisions
00069 to know the caller or other indicator (the socket id): int (*getfn)(unsigned char*, int)
00070 */
00071 static int mysock = INVALID_SOCKET;
00072 
00073 int Socket_error(char* aString, int sock)
00074 {
00075 #if defined(WIN32)
00076     int errno;
00077 #endif
00078 
00079 #if defined(WIN32)
00080     errno = WSAGetLastError();
00081 #endif
00082     if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS && errno != EWOULDBLOCK)
00083     {
00084         if (strcmp(aString, "shutdown") != 0 || (errno != ENOTCONN && errno != ECONNRESET))
00085         {
00086             int orig_errno = errno;
00087             char* errmsg = strerror(errno);
00088 
00089             printf("Socket error %d (%s) in %s for socket %d\n", orig_errno, errmsg, aString, sock);
00090         }
00091     }
00092     return errno;
00093 }
00094 
00095 
00096 int lowlevel_sendPacketBuffer(char* host, int port, unsigned char* buf, int buflen)
00097 {
00098     struct sockaddr_in cliaddr;
00099     int rc = 0;
00100 
00101     memset(&cliaddr, 0, sizeof(cliaddr));
00102     cliaddr.sin_family = AF_INET;
00103     cliaddr.sin_addr.s_addr = inet_addr(host);
00104     cliaddr.sin_port = htons(port);
00105 
00106     if ((rc = sendto(mysock, buf, buflen, 0, (const struct sockaddr*)&cliaddr, sizeof(cliaddr))) == SOCKET_ERROR)
00107         Socket_error("sendto", mysock);
00108     else
00109         rc = 0;
00110     return rc;
00111 }
00112 
00113 
00114 int lowlevel_getdata(unsigned char* buf, int count)
00115 {
00116     int rc = recvfrom(mysock, buf, count, 0, NULL, NULL);
00117     //printf("received %d bytes count %d\n", rc, (int)count);
00118     return rc;
00119 }
00120 
00121 /**
00122 return >=0 for a socket descriptor, <0 for an error code
00123 */
00124 int lowlevel_open()
00125 {
00126     mysock = socket(AF_INET, SOCK_DGRAM, 0);
00127     if (mysock == INVALID_SOCKET)
00128         return Socket_error("socket", mysock);
00129 
00130     return mysock;
00131 }
00132 
00133 int lowlevel_close()
00134 {
00135 int rc;
00136 
00137     rc = shutdown(mysock, SHUT_WR);
00138     rc = close(mysock);
00139 
00140     return rc;
00141 }