Merge pull request #20 from ty4tw/gateway

Update  #11, #12, #13  and BugFix of Network
This commit is contained in:
Ian Craggs
2016-07-26 22:00:03 +01:00
committed by GitHub
14 changed files with 135 additions and 54 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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;
}

View File

@@ -37,7 +37,7 @@ public:
void run(void);
private:
void log(Client*, MQTTGWPacket*);
int log(Client*, MQTTGWPacket*);
Gateway* _gateway;
LightIndicator* _light;

View File

@@ -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"
/*=================================

View File

@@ -21,6 +21,7 @@
#include <signal.h>
#include <Timer.h>
#include <exception>
#include <getopt.h>
#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()
@@ -91,11 +104,12 @@ int Process::getParam(const char* parameter, char* value)
char str[MQTTSNGW_PARAM_MAX];
char param[MQTTSNGW_PARAM_MAX];
FILE *fp;
int i = 0, j = 0;
if ((fp = fopen(MQTTSNGW_CONFIG_FILE, "r")) == NULL)
if ((fp = fopen(_configFile.c_str(), "r")) == NULL)
{
WRITELOG("No config file:[%s]\n", MQTTSNGW_CONFIG_FILE);
WRITELOG("No config file:[%s]\n", _configFile.c_str());
return -1;
}

View File

@@ -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.
@@ -65,6 +70,7 @@ public:
private:
int _argc;
char** _argv;
string _configFile;
RingBuffer* _rb;
Semaphore* _rbsem;
Mutex _mt;

View File

@@ -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)
@@ -62,6 +63,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,20 +106,20 @@ 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.");
}
}
}
MultiTaskProcess::initialize(argc, 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");

View File

@@ -143,6 +143,7 @@ typedef struct
uint8_t gatewayId;
uint8_t mqttVersion;
uint16_t maxInflightMsgs;
uint8_t* gatewayName;
}GatewayParams;
/*=====================================

View File

@@ -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();
}

View File

@@ -59,6 +59,7 @@ public:
private:
int _sockfd;
addrinfo* _addrinfo;
Mutex _mutex;
};
/*========================================

View File

@@ -25,6 +25,7 @@
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
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,

View File

@@ -19,13 +19,12 @@
#include <pthread.h>
#include <semaphore.h>
#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"
/*=====================================

View File

@@ -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) ../