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 17 #if !defined(__MQTT_LINUX_) 18 #define __MQTT_LINUX_ 19 20 #if defined(WIN32_DLL) || defined(WIN64_DLL) 21 #define DLLImport __declspec(dllimport) 22 #define DLLExport __declspec(dllexport) 23 #elif defined(LINUX_SO) 24 #define DLLImport extern 25 #define DLLExport __attribute__ ((visibility ("default"))) 26 #else 27 #define DLLImport 28 #define DLLExport 29 #endif 30 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <sys/param.h> 34 #include <sys/time.h> 35 #include <sys/select.h> 36 #include <netinet/in.h> 37 #include <netinet/tcp.h> 38 #include <arpa/inet.h> 39 #include <netdb.h> 40 #include <stdio.h> 41 #include <unistd.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include "log_c.h" 45 46 #include <stdlib.h> 47 #include <string.h> 48 #include <signal.h> 49 #define LOG_DOMAIN_MQTT 0x9527 50 51 #ifndef NAPI_MQTT_LOG_TAG 52 #define NAPI_MQTT_LOG_TAG "MQTT" 53 #endif // !NAPI_MQTT_LOG_TAG 54 // #define MQTT_RELEASE_BUILD 1 55 #ifndef MQTT_RELEASE_BUILD 56 #define LogDebug(...) ((void)HiLogPrint(LOG_APP, LOG_DEBUG, LOG_DOMAIN_MQTT, NAPI_MQTT_LOG_TAG, __VA_ARGS__)) 57 #define LogInfo(...) ((void)HiLogPrint(LOG_APP, LOG_INFO, LOG_DOMAIN_MQTT, NAPI_MQTT_LOG_TAG, __VA_ARGS__)) 58 #define LogWarn(...) ((void)HiLogPrint(LOG_APP, LOG_WARN, LOG_DOMAIN_MQTT, NAPI_MQTT_LOG_TAG, __VA_ARGS__)) 59 #define LogError(...) ((void)HiLogPrint(LOG_APP, LOG_ERROR, LOG_DOMAIN_MQTT, NAPI_MQTT_LOG_TAG, __VA_ARGS__)) 60 #define LogFatal(...) ((void)HiLogPrint(LOG_APP, LOG_FATAL, LOG_DOMAIN_MQTT, NAPI_MQTT_LOG_TAG, __VA_ARGS__)) 61 #else // !MQTT_RELEASE_BUILD 62 #define LogDebug(...) 63 #define LogInfo(...) 64 #define LogWarn(...) 65 #define LogError(...) ((void)HiLogPrint(LOG_APP, LOG_ERROR, LOG_DOMAIN_MQTT, NAPI_MQTT_LOG_TAG, __VA_ARGS__)) 66 #define LogFatal(...) ((void)HiLogPrint(LOG_APP, LOG_FATAL, LOG_DOMAIN_MQTT, NAPI_MQTT_LOG_TAG, __VA_ARGS__)) 67 #endif 68 typedef struct Timer 69 { 70 struct timeval end_time; 71 } Timer; 72 73 void TimerInit(Timer*); 74 char TimerIsExpired(Timer*); 75 void TimerCountdownMS(Timer*, unsigned int); 76 void TimerCountdown(Timer*, unsigned int); 77 int TimerLeftMS(Timer*); 78 void TimerAddSecond(Timer* timer, unsigned int time); 79 80 typedef struct Network 81 { 82 int my_socket; 83 int (*mqttread) (struct Network*, unsigned char*, int, int); 84 int (*mqttwrite) (struct Network*, unsigned char*, int, int); 85 } Network; 86 87 int linux_read(Network*, unsigned char*, int, int); 88 int linux_write(Network*, unsigned char*, int, int); 89 90 DLLExport void NetworkInit(Network*); 91 DLLExport int NetworkConnect(Network*, char*, int); 92 DLLExport void NetworkDisconnect(Network*); 93 94 #endif 95