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