liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /*******************************************************************************
|
| 2 | * Copyright (c) 2014 IBM Corp.
|
| 3 | *
|
| 4 | * All rights reserved. This program and the accompanying materials
|
| 5 | * are made available under the terms of the Eclipse Public License v1.0
|
| 6 | * and Eclipse Distribution License v1.0 which accompany this distribution.
|
| 7 | *
|
| 8 | * The Eclipse Public License is available at
|
| 9 | * http://www.eclipse.org/legal/epl-v10.html
|
| 10 | * and the Eclipse Distribution License is available at
|
| 11 | * http://www.eclipse.org/org/documents/edl-v10.php.
|
| 12 | *
|
| 13 | * Contributors:
|
| 14 | * Allan Stockdill-Mander - initial API and implementation and/or initial documentation
|
| 15 | *******************************************************************************/
|
| 16 | // #include <openssl/ssl.h>
|
| 17 | // #include <openssl/err.h>
|
| 18 | #include "MQTTLinux.h"
|
| 19 | //#include "global.h"
|
| 20 | //#include "DC_iot_port.h"
|
| 21 | #include "mbtk_sock2.h"
|
| 22 | #define TIMEOUT 60*1000
|
| 23 |
|
| 24 | char expired(Timer* timer)
|
| 25 | {
|
| 26 | struct timeval now, res;
|
| 27 | gettimeofday(&now, NULL);
|
| 28 | timersub(&timer->end_time, &now, &res);
|
| 29 | return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0);
|
| 30 | }
|
| 31 |
|
| 32 |
|
| 33 | void countdown_ms(Timer* timer, unsigned int timeout)
|
| 34 | {
|
| 35 | struct timeval now;
|
| 36 | gettimeofday(&now, NULL);
|
| 37 | struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
|
| 38 | timeradd(&now, &interval, &timer->end_time);
|
| 39 | }
|
| 40 |
|
| 41 |
|
| 42 | void countdown(Timer* timer, unsigned int timeout)
|
| 43 | {
|
| 44 | struct timeval now;
|
| 45 | gettimeofday(&now, NULL);
|
| 46 | struct timeval interval = {timeout, 0};
|
| 47 | timeradd(&now, &interval, &timer->end_time);
|
| 48 | }
|
| 49 |
|
| 50 |
|
| 51 | int left_ms(Timer* timer)
|
| 52 | {
|
| 53 | struct timeval now, res;
|
| 54 | gettimeofday(&now, NULL);
|
| 55 | timersub(&timer->end_time, &now, &res);
|
| 56 | //printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
|
| 57 | return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
|
| 58 | }
|
| 59 |
|
| 60 |
|
| 61 | void InitTimer(Timer* timer)
|
| 62 | {
|
| 63 | timer->end_time = (struct timeval){0, 0};
|
| 64 | }
|
| 65 |
|
| 66 |
|
| 67 | int linux_read(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
| 68 | {
|
| 69 |
|
| 70 | int err;
|
| 71 | int read_len = mbtk_sock_read(n->handle,n->my_socket, buffer, len, TIMEOUT, &err);
|
| 72 | if(read_len < 0) {
|
| 73 | if(err == MBTK_SOCK_ETIMEOUT) {
|
| 74 | return -2;
|
| 75 | } else {
|
| 76 | return -1;
|
| 77 | }
|
| 78 | } else {
|
| 79 | return read_len;
|
| 80 | }
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 | #if 0
|
| 85 |
|
| 86 | struct timeval interval = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
|
| 87 | //mDEBUG("[%s]timeout_ms = %d \n", __FUNCTION__, timeout_ms);
|
| 88 | if (interval.tv_sec < 0 || (interval.tv_sec == 0 && interval.tv_usec <= 0))
|
| 89 | {
|
| 90 | interval.tv_sec = 0;
|
| 91 | interval.tv_usec = 100;
|
| 92 | }
|
| 93 |
|
| 94 | setsockopt(n->my_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&interval, sizeof(struct timeval));
|
| 95 |
|
| 96 | int bytes = 0;
|
| 97 | while (bytes < len)
|
| 98 | {
|
| 99 |
|
| 100 | int rc = recv(n->my_socket, &buffer[bytes], (size_t)(len - bytes), 0);
|
| 101 | //mDEBUG("[%s]socket recv rc = %d \n", __FUNCTION__, rc);
|
| 102 |
|
| 103 | if(rc == 0)
|
| 104 | {
|
| 105 | //mDEBUG("[%s]socket close \n", __FUNCTION__);
|
| 106 | bytes = 0;
|
| 107 | break;
|
| 108 | }
|
| 109 |
|
| 110 | if (rc == -1)
|
| 111 | {
|
| 112 | if (errno != ENOTCONN && errno != ECONNRESET)
|
| 113 | {
|
| 114 | bytes = -1;
|
| 115 | break;
|
| 116 | }
|
| 117 | }
|
| 118 | else
|
| 119 | bytes += rc;
|
| 120 | }
|
| 121 | return bytes;
|
| 122 | #endif
|
| 123 | }
|
| 124 |
|
| 125 |
|
| 126 | int linux_write(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
| 127 | {
|
| 128 |
|
| 129 | int err;
|
| 130 | printf("Write[%d]:%s",len,(char*)buffer);
|
| 131 | return mbtk_sock_write(n->handle, n->my_socket, buffer, len, TIMEOUT, &err);
|
| 132 |
|
| 133 | #if 0
|
| 134 | struct timeval tv;
|
| 135 |
|
| 136 | tv.tv_sec = 0; /* 30 Secs Timeout */
|
| 137 | tv.tv_usec = timeout_ms * 1000; // Not init'ing this can cause strange errors
|
| 138 |
|
| 139 | setsockopt(n->my_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
|
| 140 |
|
| 141 | int rc = write(n->my_socket, buffer, len);
|
| 142 |
|
| 143 | return rc;
|
| 144 | #endif
|
| 145 | }
|
| 146 |
|
| 147 |
|
| 148 | void linux_disconnect(Network* n)
|
| 149 | {
|
| 150 | int errno;
|
| 151 | mbtk_sock_close(n->handle,n->my_socket,TIMEOUT,&errno);
|
| 152 | }
|
| 153 |
|
| 154 |
|
| 155 | void NewNetwork(Network* n)
|
| 156 | {
|
| 157 |
|
| 158 | n->my_socket = 0;
|
| 159 | n->mqttread = linux_read;
|
| 160 | n->mqttwrite = linux_write;
|
| 161 | n->disconnect = linux_disconnect;
|
| 162 | n->ingnore_cert = true;
|
| 163 | n->is_support_ssl = false;
|
| 164 | }
|
| 165 |
|
| 166 |
|
| 167 | int ConnectNetwork(Network* n, char* addr, int port, bool is_support_ssl, bool ingnore_cert)
|
| 168 | {
|
| 169 | mbtk_init_info *info ;
|
| 170 | mbtk_sock_info *sock_info;
|
| 171 | int rc = 0;
|
| 172 | int errno;
|
| 173 | info = NULL;
|
| 174 | sock_info = (mbtk_sock_info *)malloc(sizeof(mbtk_sock_info));
|
| 175 | if(sock_info ==NULL)
|
| 176 | {
|
| 177 | rc = -1;
|
| 178 | return rc;
|
| 179 | }
|
| 180 | memcpy(sock_info->address, addr, strlen(addr));
|
| 181 | sock_info->port = port;
|
| 182 | sock_info->is_support_ssl = is_support_ssl;
|
| 183 | sock_info->ingnore_cert = ingnore_cert;
|
| 184 |
|
| 185 | 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);
|
| 186 |
|
| 187 | mbtk_sock_handle handle = mbtk_sock_init(info);
|
| 188 | if (handle < 0 )
|
| 189 | {
|
| 190 | rc = -1;
|
| 191 | return rc;
|
| 192 | }
|
| 193 |
|
| 194 | int fd = mbtk_sock_open(handle, sock_info, TIMEOUT, &errno);
|
| 195 | if(fd < 0)
|
| 196 | {
|
| 197 | rc = -1;
|
| 198 | return rc;
|
| 199 | }
|
| 200 |
|
| 201 | n->my_socket = fd;
|
| 202 | n->handle = handle;
|
| 203 | return rc;
|
| 204 | }
|