From 4bdec4a11f2d8152ec8f1c050b20b3bd5ee2273d Mon Sep 17 00:00:00 2001 From: tomoaki Date: Sun, 3 Jul 2016 10:00:57 +0900 Subject: [PATCH 1/3] Update: Create param.conf and key files automatically. BugFix: change parameter name BroadcastIp and BroadcastPortNo to MulticastIP and MulticastPortNo, respectively. Update: Makefile to copy param.conf and build Log monitor. Update: README Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 99 +++++++++++++++++---------- MQTTSNGateway/param.conf | 10 ++- MQTTSNGateway/src/MQTTSNGWDefines.h | 4 +- MQTTSNGateway/src/MQTTSNGWProcess.cpp | 7 +- MQTTSNGateway/src/MQTTSNGWProcess.h | 5 ++ MQTTSNGateway/src/MQTTSNGateway.cpp | 11 ++- MQTTSNGateway/src/MQTTSNGateway.h | 1 + MQTTSNGateway/src/linux/Threading.cpp | 10 +++ MQTTSNGateway/src/linux/Threading.h | 5 +- Makefile | 6 +- 10 files changed, 110 insertions(+), 48 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 66270cd..b02f132 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -2,53 +2,82 @@ ### **step1. Build the gateway** ```` -$ git clone -b gateway https://github.com/eclipse/paho.mqtt-sn.embedded-c -$ make +$ git clone -b gateway https://github.com/eclipse/paho.mqtt-sn.embedded-c +$ cd paho.mqtt-sn.embedded-c +$ make $ make install +$ make clean ``` -or compile with Eclipse CDT - +MQTT-SNGateway, MQTT-SNLogmonitor and param.conf are copied into ../ directory. -### **step2. Create configuration file of the gateway** -Create **/usr/local/etc/mqttsnGateway/config/param.conf** -Contents are follows: +### **step2. Execute the Gateway.** + +```` +$ cd ../ +$ ./MQTT-SNGateway +```` + +MQTT-SN clients should run in differnt from a host of the gateway. Other wise the gateway can't receive multicast packets form clients. + +### **How to Change the configuration of the gateway** +**../param.conf** Contents are follows: + ```` -BrokerName=test.mosquitto.org -BrokerPortNo=1883 -SecureConnection=NO -#BrokerPortNo=8883 -#SecureConnection=YES -ClientAuthorization=NO -GatewayID=1 -GatewayName=PahoGateway-01 -KeepAlive=900 -#LoginID= -#Password= -BroadcastIP=225.1.1.1 -GatewayPortNo=2000 -BroadcastPortNo=1883 +# config file of MQTT-SN Gateway + +BrokerName=test.mosquitto.org +BrokerPortNo=1883 +SecureConnection=NO +#BrokerPortNo=8883 +#SecureConnection=YES +ClientAuthorization=NO +GatewayID=1 +GatewayName=PahoGateway-01 +KeepAlive=900 +#LoginID= +#Password= + +# UDP +GatewayPortNo=2000 +MulticastIP=225.1.1.1 +MulticastPortNo=1883 + +# XBee +Baudrate=38400 +SerialDevice=/dev/ttyUSB0 ``` **BrokerName** to specify a domain name of the Broker, and **BrokerPortNo** is a port No of the Broker. If the Broker have to connected via TLS, set BrokerPortNo=8883 and **SecureConnection=YES**. -**BroadcastIP** and **BroadcastPortNo** is a multicast address for ADVERTISE, GWSEARCH and GWINFO messages. Gateway is waiting GWSEARCH multicast message and when receiving it send GWINFO message via Broadcast address. Clients can get the gateway address (Gateway IP address and **GatewayPortNo**) from GWINFO message by means of std::recvfrom(), +**MulticastIP** and **MulticastPortNo** is a multicast address for ADVERTISE, GWSEARCH and GWINFO messages. Gateway is waiting GWSEARCH multicast message and when receiving it send GWINFO message via Broadcast address. Clients can get the gateway address (Gateway IP address and **GatewayPortNo**) from GWINFO message by means of std::recvfrom(), Client should know the BroadcastIP and PortNo to send a SEARCHGW message. **GatewayId** is defined by GWSEARCH message. **KeepAlive** is a duration of ADVERTISE message in seconds. -when **ClientAuthorization** is YES, see MQTTSNGWClient.cpp line53, /usr/local/etc/mqttsnGateway/config/clientList.conf file is required. this file defines connect able clients by IPaddress and PortNo. +when **ClientAuthorization** is YES, see MQTTSNGWClient.cpp line53, /usr/local/etc/mqttsnGateway/config/clientList.conf file is required. this file defines connect able clients by IPaddress and PortNo. - -### **step3. Create key files of the ring buffer** - -Create the following 3 empty files as key files. -1) /usr/local/etc/mqttsnGateway/config/**rbmutex.key** -2) /usr/local/etc/mqttsnGateway/config/**ringbuffer.key** -3) /usr/local/etc/mqttsnGateway/config/**semaphore.key** - -### **step4. Execute the Gateway.** -This must not be the same machine as the Client. - -`$ ./MQTT-SNGateway` +### ** How to change the config files directory. ** +The directory is defined in MQTTSNGWProcess.h line33. +change this value to new directory you want to use. + +`#define MQTTSNGW_CONFIG_DIRECTORY "./"` + +### ** How to monitor the gateway from remote. ** + +Uncomment line32 in MQTTSNGWDefined.h. + +`//#define RINGBUFFER // print out Packets log into shared memory./"` +```` +$ make +$ make install +$ make clean +```` +restart the gateway. +open ssh terminal and execute LogMonitor. + +`$ ./MQTT-SNLogmonitor` + +Now you can get the Log on your terminal. + diff --git a/MQTTSNGateway/param.conf b/MQTTSNGateway/param.conf index 55f1669..ecca1a1 100644 --- a/MQTTSNGateway/param.conf +++ b/MQTTSNGateway/param.conf @@ -11,6 +11,12 @@ GatewayName=PahoGateway-01 KeepAlive=900 #LoginID= #Password= -BroadcastIP=225.1.1.1 + +# UDP GatewayPortNo=2000 -BroadcastPortNo=1883 +MulticastIP=225.1.1.1 +MulticastPortNo=1883 + +# XBee +Baudrate=38400 +SerialDevice=/dev/ttyUSB0 diff --git a/MQTTSNGateway/src/MQTTSNGWDefines.h b/MQTTSNGateway/src/MQTTSNGWDefines.h index 541dad5..955047f 100644 --- a/MQTTSNGateway/src/MQTTSNGWDefines.h +++ b/MQTTSNGateway/src/MQTTSNGWDefines.h @@ -23,7 +23,7 @@ namespace MQTTSNGW /*================================= * Starting prompt ==================================*/ -#define GATEWAY_VERSION "(Ver 0.1.1)" +#define GATEWAY_VERSION "(Ver 0.2.0)" /*================================= * Log controls @@ -37,8 +37,6 @@ namespace MQTTSNGW #define MQTTSNGW_MAX_PACKET_SIZE (1024) // Max Packet size #define SIZEOF_LOG_PACKET (128) // Length of the packet log in bytes -#define MQTTSNGW_CONFIG_FILE "/usr/local/etc/mqttsnGateway/config/param.conf" -#define MQTTSNGW_CLIENT_LIST "/usr/local/etc/mqttsnGateway/config/clientList.conf" #define MQTTSNGW_TLS_CA_DIR "/etc/ssl/certs" /*================================= diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.cpp b/MQTTSNGateway/src/MQTTSNGWProcess.cpp index 224aa03..1ae4e5f 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.cpp +++ b/MQTTSNGateway/src/MQTTSNGWProcess.cpp @@ -91,11 +91,14 @@ int Process::getParam(const char* parameter, char* value) char str[MQTTSNGW_PARAM_MAX]; char param[MQTTSNGW_PARAM_MAX]; FILE *fp; + + string filename = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_CONFIG_FILE); + int i = 0, j = 0; - if ((fp = fopen(MQTTSNGW_CONFIG_FILE, "r")) == NULL) + if ((fp = fopen(filename.c_str(), "r")) == NULL) { - WRITELOG("No config file:[%s]\n", MQTTSNGW_CONFIG_FILE); + WRITELOG("No config file:[%s]\n", filename.c_str()); return -1; } diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.h b/MQTTSNGateway/src/MQTTSNGWProcess.h index 55b223b..c344503 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.h +++ b/MQTTSNGateway/src/MQTTSNGWProcess.h @@ -30,6 +30,11 @@ namespace MQTTSNGW /*================================= * Parameters ==================================*/ +#define MQTTSNGW_CONFIG_DIRECTORY "./" + +#define MQTTSNGW_CONFIG_FILE "param.conf" +#define MQTTSNGW_CLIENT_LIST "clientList.conf" + #define MQTTSNGW_MAX_TASK 10 // number of Tasks #define PROCESS_LOG_BUFFER_SIZE 16384 // Ring buffer size for Logs #define MQTTSNGW_PARAM_MAX 128 // Max length of config records. diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index 9dd10ee..b965533 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -62,6 +62,11 @@ void Gateway::initialize(int argc, char** argv) throw Exception( "Gateway::initialize: invalid Gateway Id"); } + if (getParam("GatewayName", param) == 0) + { + _params.gatewayName = (uint8_t*) strdup(param); + } + _params.mqttVersion = DEFAULT_MQTT_VERSION; if (getParam("MQTTVersion", param) == 0) { @@ -100,7 +105,9 @@ void Gateway::initialize(int argc, char** argv) { if (!strcasecmp(param, "YES")) { - if (!_clientList.authorize(MQTTSNGW_CLIENT_LIST)) + string fileName = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_CLIENT_LIST); + + if (!_clientList.authorize(fileName.c_str())) { throw Exception("Gateway::initialize: can't authorize clients."); } @@ -113,7 +120,7 @@ void Gateway::initialize(int argc, char** argv) void Gateway::run(void) { _lightIndicator.redLight(true); - WRITELOG("%s MQTT-SN Gateway has been started. %s %s\n", currentDateTime(), _sensorNetwork.getType(), GATEWAY_VERSION); + WRITELOG("%s %s has been started. %s %s\n", currentDateTime(), _params.gatewayName, _sensorNetwork.getType(), GATEWAY_VERSION); if ( getClientList()->isAuthorized() ) { WRITELOG("\n Client authentication is required by the configuration settings.\n"); diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index 8115a65..9237e08 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -143,6 +143,7 @@ typedef struct uint8_t gatewayId; uint8_t mqttVersion; uint16_t maxInflightMsgs; + uint8_t* gatewayName; }GatewayParams; /*===================================== diff --git a/MQTTSNGateway/src/linux/Threading.cpp b/MQTTSNGateway/src/linux/Threading.cpp index 4dee15a..0f02e67 100644 --- a/MQTTSNGateway/src/linux/Threading.cpp +++ b/MQTTSNGateway/src/linux/Threading.cpp @@ -25,6 +25,7 @@ #include #include #include +#include using namespace std; using namespace MQTTSNGW; @@ -235,6 +236,15 @@ void Semaphore::timedwait(uint16_t millsec) =========================================*/ RingBuffer::RingBuffer() { + int fp = 0; + string fileName = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_RINGBUFFER_KEY); + fp = open(fileName.c_str(), O_CREAT, 0); + close(fp); + + fileName = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_RB_MUTEX_KEY); + fp = open(fileName.c_str(), O_CREAT, 0); + close(fp); + key_t key = ftok(MQTTSNGW_RINGBUFFER_KEY, 1); if ((_shmid = shmget(key, PROCESS_LOG_BUFFER_SIZE, diff --git a/MQTTSNGateway/src/linux/Threading.h b/MQTTSNGateway/src/linux/Threading.h index 3c86d31..b976f3e 100644 --- a/MQTTSNGateway/src/linux/Threading.h +++ b/MQTTSNGateway/src/linux/Threading.h @@ -19,13 +19,12 @@ #include #include -#include "MQTTSNGWDefines.h" namespace MQTTSNGW { -#define MQTTSNGW_RINGBUFFER_KEY "/usr/local/etc/mqttsnGateway/config/ringbuffer.key" -#define MQTTSNGW_RB_MUTEX_KEY "/usr/local/etc/mqttsnGateway/config/rbmutex.key" +#define MQTTSNGW_RINGBUFFER_KEY "ringbuffer.key" +#define MQTTSNGW_RB_MUTEX_KEY "rbmutex.key" #define MQTTSNGW_RB_SEMAPHOR_NAME "/rbsemaphor" /*===================================== diff --git a/Makefile b/Makefile index e5d1287..7b971f0 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ APPL := mainGateway LPROGNAME := MQTT-SNLogmonitor LAPPL := mainLogmonitor +CONFIG := MQTTSNGateway/param.conf + SRCDIR := MQTTSNGateway/src SUBDIR := MQTTSNPacket/src @@ -69,7 +71,7 @@ DEPS += $(CSRCS:%.c=$(OUTDIR)/%.d) .PHONY: install clean -all: $(PROG) +all: $(PROG) $(LPROG) monitor: $(LPROG) @@ -102,5 +104,7 @@ clean: install: cp -pf $(PROG) ../ + cp -pf $(LPROG) ../ + cp -pf $(CONFIG) ../ \ No newline at end of file From 5605ae0294f59e0b6ca606be860ca027e453ee10 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 22 Jul 2016 12:24:29 +0900 Subject: [PATCH 2/3] Update: config file is a command line parameter. --- MQTTSNGateway/src/MQTTSNGWProcess.cpp | 19 +++++++++++++++---- MQTTSNGateway/src/MQTTSNGWProcess.h | 1 + MQTTSNGateway/src/MQTTSNGateway.cpp | 3 +-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.cpp b/MQTTSNGateway/src/MQTTSNGWProcess.cpp index 1ae4e5f..bfe29ef 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.cpp +++ b/MQTTSNGateway/src/MQTTSNGWProcess.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "MQTTSNGWProcess.h" #include "Threading.h" @@ -74,6 +75,18 @@ void Process::initialize(int argc, char** argv) signal(SIGINT, signalHandler); signal(SIGTERM, signalHandler); signal(SIGHUP, signalHandler); + + _configFile = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_CONFIG_FILE); + + int opt; + while ((opt = getopt(_argc, _argv, "f:")) != -1) + { + if ( opt == 'f' ) + { + _configFile = string(optarg); + } + } + WRITELOG("Using config file:[%s]\n", _configFile.c_str()); } int Process::getArgc() @@ -92,13 +105,11 @@ int Process::getParam(const char* parameter, char* value) char param[MQTTSNGW_PARAM_MAX]; FILE *fp; - string filename = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_CONFIG_FILE); - int i = 0, j = 0; - if ((fp = fopen(filename.c_str(), "r")) == NULL) + if ((fp = fopen(_configFile.c_str(), "r")) == NULL) { - WRITELOG("No config file:[%s]\n", filename.c_str()); + WRITELOG("No config file:[%s]\n", _configFile.c_str()); return -1; } diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.h b/MQTTSNGateway/src/MQTTSNGWProcess.h index c344503..f155f56 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.h +++ b/MQTTSNGateway/src/MQTTSNGWProcess.h @@ -70,6 +70,7 @@ public: private: int _argc; char** _argv; + string _configFile; RingBuffer* _rb; Semaphore* _rbsem; Mutex _mt; diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index b965533..3610410 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -50,6 +50,7 @@ Gateway::~Gateway() void Gateway::initialize(int argc, char** argv) { char param[MQTTSNGW_PARAM_MAX]; + MultiTaskProcess::initialize(argc, argv); _params.gatewayId = 0; if (getParam("GatewayID", param) == 0) @@ -113,8 +114,6 @@ void Gateway::initialize(int argc, char** argv) } } } - - MultiTaskProcess::initialize(argc, argv); } void Gateway::run(void) From 464187a08b5c62e44492a5a17f5f5c95458175d3 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Sat, 23 Jul 2016 05:56:55 +0900 Subject: [PATCH 3/3] BugFix: Exclusive control the close of TCPIP --- MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp | 11 ++++++++--- MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.h | 2 +- MQTTSNGateway/src/linux/Network.cpp | 2 ++ MQTTSNGateway/src/linux/Network.h | 1 + 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp index 83f1c05..787d5e5 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp @@ -109,7 +109,10 @@ void BrokerRecvTask::run(void) rc = packet->recv(client->getNetwork()); if ( rc > 0 ) { - log(client, packet); + if ( log(client, packet) == -1 ) + { + continue; + } /* post a BrokerRecvEvent */ ev = new Event(); @@ -161,10 +164,11 @@ void BrokerRecvTask::run(void) /** * write message content into stdout or Ringbuffer */ -void BrokerRecvTask::log(Client* client, MQTTGWPacket* packet) +int BrokerRecvTask::log(Client* client, MQTTGWPacket* packet) { char pbuf[SIZEOF_LOG_PACKET * 3]; char msgId[6]; + int rc = 0; switch (packet->getType()) { @@ -189,7 +193,8 @@ void BrokerRecvTask::log(Client* client, MQTTGWPacket* packet) WRITELOG(FORMAT_GR_NL, currentDateTime(), packet->getName(), LEFTARROW, client->getClientId(), packet->print(pbuf)); break; default: - WRITELOG(FORMAT_GR_NL, currentDateTime(), "UNKOWN_TYPE", LEFTARROW, client->getClientId(), packet->print(pbuf)); + rc = -1; break; } + return rc; } diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.h b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.h index e506e95..a5dd9a8 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.h +++ b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.h @@ -37,7 +37,7 @@ public: void run(void); private: - void log(Client*, MQTTGWPacket*); + int log(Client*, MQTTGWPacket*); Gateway* _gateway; LightIndicator* _light; diff --git a/MQTTSNGateway/src/linux/Network.cpp b/MQTTSNGateway/src/linux/Network.cpp index cde1d02..0ba5661 100644 --- a/MQTTSNGateway/src/linux/Network.cpp +++ b/MQTTSNGateway/src/linux/Network.cpp @@ -57,6 +57,7 @@ bool TCPStack::isValid() void TCPStack::close() { + _mutex.lock(); if (_sockfd > 0) { ::close(_sockfd); @@ -67,6 +68,7 @@ void TCPStack::close() _addrinfo = 0; } } + _mutex.unlock(); } diff --git a/MQTTSNGateway/src/linux/Network.h b/MQTTSNGateway/src/linux/Network.h index 5c6b79f..d8ed45b 100644 --- a/MQTTSNGateway/src/linux/Network.h +++ b/MQTTSNGateway/src/linux/Network.h @@ -59,6 +59,7 @@ public: private: int _sockfd; addrinfo* _addrinfo; + Mutex _mutex; }; /*========================================