| /******************************************************************************* | |
| * Copyright (c) 2014 IBM Corp. | |
| * | |
| * All rights reserved. This program and the accompanying materials | |
| * are made available under the terms of the Eclipse Public License v1.0 | |
| * and Eclipse Distribution License v1.0 which accompany this distribution. | |
| * | |
| * The Eclipse Public License is available at | |
| * http://www.eclipse.org/legal/epl-v10.html | |
| * and the Eclipse Distribution License is available at | |
| * http://www.eclipse.org/org/documents/edl-v10.php. | |
| * | |
| * Contributors: | |
| * Allan Stockdill-Mander - initial API and implementation and/or initial documentation | |
| *******************************************************************************/ | |
| // #include <openssl/ssl.h> | |
| // #include <openssl/err.h> | |
| #include "MQTTLinux.h" | |
| //#include "global.h" | |
| //#include "DC_iot_port.h" | |
| #include "mbtk_sock2.h" | |
| #define TIMEOUT 60*1000 | |
| char expired(Timer* timer) | |
| { | |
| struct timeval now, res; | |
| gettimeofday(&now, NULL); | |
| timersub(&timer->end_time, &now, &res); | |
| return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0); | |
| } | |
| void countdown_ms(Timer* timer, unsigned int timeout) | |
| { | |
| struct timeval now; | |
| gettimeofday(&now, NULL); | |
| struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000}; | |
| timeradd(&now, &interval, &timer->end_time); | |
| } | |
| void countdown(Timer* timer, unsigned int timeout) | |
| { | |
| struct timeval now; | |
| gettimeofday(&now, NULL); | |
| struct timeval interval = {timeout, 0}; | |
| timeradd(&now, &interval, &timer->end_time); | |
| } | |
| int left_ms(Timer* timer) | |
| { | |
| struct timeval now, res; | |
| gettimeofday(&now, NULL); | |
| timersub(&timer->end_time, &now, &res); | |
| //printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000); | |
| return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000; | |
| } | |
| void InitTimer(Timer* timer) | |
| { | |
| timer->end_time = (struct timeval){0, 0}; | |
| } | |
| int linux_read(Network* n, unsigned char* buffer, int len, int timeout_ms) | |
| { | |
| int err; | |
| int read_len = mbtk_sock_read(n->handle,n->my_socket, buffer, len, TIMEOUT, &err); | |
| if(read_len < 0) { | |
| if(err == MBTK_SOCK_ETIMEOUT) { | |
| return -2; | |
| } else { | |
| return -1; | |
| } | |
| } else { | |
| return read_len; | |
| } | |
| #if 0 | |
| struct timeval interval = {timeout_ms / 1000, (timeout_ms % 1000) * 1000}; | |
| //mDEBUG("[%s]timeout_ms = %d \n", __FUNCTION__, timeout_ms); | |
| if (interval.tv_sec < 0 || (interval.tv_sec == 0 && interval.tv_usec <= 0)) | |
| { | |
| interval.tv_sec = 0; | |
| interval.tv_usec = 100; | |
| } | |
| setsockopt(n->my_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&interval, sizeof(struct timeval)); | |
| int bytes = 0; | |
| while (bytes < len) | |
| { | |
| int rc = recv(n->my_socket, &buffer[bytes], (size_t)(len - bytes), 0); | |
| //mDEBUG("[%s]socket recv rc = %d \n", __FUNCTION__, rc); | |
| if(rc == 0) | |
| { | |
| //mDEBUG("[%s]socket close \n", __FUNCTION__); | |
| bytes = 0; | |
| break; | |
| } | |
| if (rc == -1) | |
| { | |
| if (errno != ENOTCONN && errno != ECONNRESET) | |
| { | |
| bytes = -1; | |
| break; | |
| } | |
| } | |
| else | |
| bytes += rc; | |
| } | |
| return bytes; | |
| #endif | |
| } | |
| int linux_write(Network* n, unsigned char* buffer, int len, int timeout_ms) | |
| { | |
| int err; | |
| printf("Write[%d]:%s",len,(char*)buffer); | |
| return mbtk_sock_write(n->handle, n->my_socket, buffer, len, TIMEOUT, &err); | |
| #if 0 | |
| struct timeval tv; | |
| tv.tv_sec = 0; /* 30 Secs Timeout */ | |
| tv.tv_usec = timeout_ms * 1000; // Not init'ing this can cause strange errors | |
| setsockopt(n->my_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); | |
| int rc = write(n->my_socket, buffer, len); | |
| return rc; | |
| #endif | |
| } | |
| void linux_disconnect(Network* n) | |
| { | |
| int errno; | |
| mbtk_sock_close(n->handle,n->my_socket,TIMEOUT,&errno); | |
| } | |
| void NewNetwork(Network* n) | |
| { | |
| n->my_socket = 0; | |
| n->mqttread = linux_read; | |
| n->mqttwrite = linux_write; | |
| n->disconnect = linux_disconnect; | |
| n->ingnore_cert = true; | |
| n->is_support_ssl = false; | |
| } | |
| int ConnectNetwork(Network* n, char* addr, int port, bool is_support_ssl, bool ingnore_cert) | |
| { | |
| mbtk_init_info *info ; | |
| mbtk_sock_info *sock_info; | |
| int rc = 0; | |
| int errno; | |
| info = NULL; | |
| sock_info = (mbtk_sock_info *)malloc(sizeof(mbtk_sock_info)); | |
| if(sock_info ==NULL) | |
| { | |
| rc = -1; | |
| return rc; | |
| } | |
| memcpy(sock_info->address, addr, strlen(addr)); | |
| sock_info->port = port; | |
| sock_info->is_support_ssl = is_support_ssl; | |
| sock_info->ingnore_cert = ingnore_cert; | |
| printf("host %s\nport %d\nis_support_ssl %d\ningnore_cert %d\n",sock_info->address,sock_info->port,sock_info->is_support_ssl,sock_info->ingnore_cert); | |
| mbtk_sock_handle handle = mbtk_sock_init(info); | |
| if (handle < 0 ) | |
| { | |
| rc = -1; | |
| return rc; | |
| } | |
| int fd = mbtk_sock_open(handle, sock_info, TIMEOUT, &errno); | |
| if(fd < 0) | |
| { | |
| rc = -1; | |
| return rc; | |
| } | |
| n->my_socket = fd; | |
| n->handle = handle; | |
| return rc; | |
| } |