Version of HelloMQTT with u-blox cellular (C027 and C030) boards added.
Dependencies: C12832 MQTT easy-connect ublox-at-cellular-interface-ext ublox-cellular-base ublox-cellular-driver-gen ublox-ppp-cellular-interface ublox-at-cellular-interface-n2xx ublox-cellular-base-n2xx
Fork of HelloMQTT by
LinuxIPStack.h
00001 #if !defined(LINUX_IPSTACK_H) 00002 #define LINUX_IPSTACK_H 00003 00004 class IPStack 00005 { 00006 public: 00007 IPStack() 00008 { 00009 00010 } 00011 00012 int Socket_error(const char* aString) 00013 { 00014 00015 if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS && errno != EWOULDBLOCK) 00016 { 00017 if (strcmp(aString, "shutdown") != 0 || (errno != ENOTCONN && errno != ECONNRESET)) 00018 printf("Socket error %s in %s for socket %d\n", strerror(errno), aString, mysock); 00019 } 00020 return errno; 00021 } 00022 00023 int connect(const char* hostname, int port) 00024 { 00025 int type = SOCK_STREAM; 00026 struct sockaddr_in address; 00027 int rc = -1; 00028 sa_family_t family = AF_INET; 00029 struct addrinfo *result = NULL; 00030 struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL}; 00031 00032 if ((rc = getaddrinfo(hostname, NULL, &hints, &result)) == 0) 00033 { 00034 struct addrinfo* res = result; 00035 00036 /* prefer ip4 addresses */ 00037 while (res) 00038 { 00039 if (res->ai_family == AF_INET) 00040 { 00041 result = res; 00042 break; 00043 } 00044 res = res->ai_next; 00045 } 00046 00047 if (result->ai_family == AF_INET) 00048 { 00049 address.sin_port = htons(port); 00050 address.sin_family = family = AF_INET; 00051 address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr; 00052 } 00053 else 00054 rc = -1; 00055 00056 freeaddrinfo(result); 00057 } 00058 00059 if (rc == 0) 00060 { 00061 mysock = socket(family, type, 0); 00062 if (mysock != -1) 00063 { 00064 int opt = 1; 00065 00066 //if (setsockopt(mysock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0) 00067 // printf("Could not set SO_NOSIGPIPE for socket %d", mysock); 00068 00069 rc = ::connect(mysock, (struct sockaddr*)&address, sizeof(address)); 00070 } 00071 } 00072 00073 return rc; 00074 } 00075 00076 int read(char* buffer, int len, int timeout_ms) 00077 { 00078 struct timeval interval = {timeout_ms / 1000, (timeout_ms % 1000) * 1000}; 00079 if (interval.tv_sec < 0 || (interval.tv_sec == 0 && interval.tv_usec <= 0)) 00080 { 00081 interval.tv_sec = 0; 00082 interval.tv_usec = 100; 00083 } 00084 00085 setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&interval, sizeof(struct timeval)); 00086 00087 //printf("reading %d bytes\n", len); 00088 int rc = ::recv(mysock, buffer, (size_t)len, 0); 00089 if (rc == -1) 00090 Socket_error("read"); 00091 //printf("read %d bytes\n", rc); 00092 return rc; 00093 } 00094 00095 int write(char* buffer, int len, int timeout) 00096 { 00097 struct timeval tv; 00098 00099 tv.tv_sec = 0; /* 30 Secs Timeout */ 00100 tv.tv_usec = timeout * 1000; // Not init'ing this can cause strange errors 00101 00102 setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); 00103 int rc = ::write(mysock, buffer, len); 00104 //printf("write rc %d\n", rc); 00105 return rc; 00106 } 00107 00108 int disconnect() 00109 { 00110 return ::close(mysock); 00111 } 00112 00113 private: 00114 00115 int mysock; 00116 00117 }; 00118 00119 #endif
Generated on Tue Jul 12 2022 21:12:58 by 1.7.2