mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-13 15:36:51 +01:00
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 <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -2,53 +2,82 @@
|
|||||||
|
|
||||||
### **step1. Build the gateway**
|
### **step1. Build the gateway**
|
||||||
````
|
````
|
||||||
$ git clone -b gateway https://github.com/eclipse/paho.mqtt-sn.embedded-c
|
$ git clone -b gateway https://github.com/eclipse/paho.mqtt-sn.embedded-c
|
||||||
$ make
|
$ cd paho.mqtt-sn.embedded-c
|
||||||
|
$ make
|
||||||
$ make install
|
$ 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
|
# config file of MQTT-SN Gateway
|
||||||
BrokerPortNo=1883
|
|
||||||
SecureConnection=NO
|
BrokerName=test.mosquitto.org
|
||||||
#BrokerPortNo=8883
|
BrokerPortNo=1883
|
||||||
#SecureConnection=YES
|
SecureConnection=NO
|
||||||
ClientAuthorization=NO
|
#BrokerPortNo=8883
|
||||||
GatewayID=1
|
#SecureConnection=YES
|
||||||
GatewayName=PahoGateway-01
|
ClientAuthorization=NO
|
||||||
KeepAlive=900
|
GatewayID=1
|
||||||
#LoginID=
|
GatewayName=PahoGateway-01
|
||||||
#Password=
|
KeepAlive=900
|
||||||
BroadcastIP=225.1.1.1
|
#LoginID=
|
||||||
GatewayPortNo=2000
|
#Password=
|
||||||
BroadcastPortNo=1883
|
|
||||||
|
# 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**.
|
**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.
|
Client should know the BroadcastIP and PortNo to send a SEARCHGW message.
|
||||||
**GatewayId** is defined by GWSEARCH message.
|
**GatewayId** is defined by GWSEARCH message.
|
||||||
**KeepAlive** is a duration of ADVERTISE message in seconds.
|
**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.
|
||||||
|
|
||||||
|
|
||||||
|
### ** How to change the config files directory. **
|
||||||
### **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`
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,12 @@ GatewayName=PahoGateway-01
|
|||||||
KeepAlive=900
|
KeepAlive=900
|
||||||
#LoginID=
|
#LoginID=
|
||||||
#Password=
|
#Password=
|
||||||
BroadcastIP=225.1.1.1
|
|
||||||
|
# UDP
|
||||||
GatewayPortNo=2000
|
GatewayPortNo=2000
|
||||||
BroadcastPortNo=1883
|
MulticastIP=225.1.1.1
|
||||||
|
MulticastPortNo=1883
|
||||||
|
|
||||||
|
# XBee
|
||||||
|
Baudrate=38400
|
||||||
|
SerialDevice=/dev/ttyUSB0
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ namespace MQTTSNGW
|
|||||||
/*=================================
|
/*=================================
|
||||||
* Starting prompt
|
* Starting prompt
|
||||||
==================================*/
|
==================================*/
|
||||||
#define GATEWAY_VERSION "(Ver 0.1.1)"
|
#define GATEWAY_VERSION "(Ver 0.2.0)"
|
||||||
|
|
||||||
/*=================================
|
/*=================================
|
||||||
* Log controls
|
* Log controls
|
||||||
@@ -37,8 +37,6 @@ namespace MQTTSNGW
|
|||||||
#define MQTTSNGW_MAX_PACKET_SIZE (1024) // Max Packet size
|
#define MQTTSNGW_MAX_PACKET_SIZE (1024) // Max Packet size
|
||||||
#define SIZEOF_LOG_PACKET (128) // Length of the packet log in bytes
|
#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"
|
#define MQTTSNGW_TLS_CA_DIR "/etc/ssl/certs"
|
||||||
|
|
||||||
/*=================================
|
/*=================================
|
||||||
|
|||||||
@@ -91,11 +91,14 @@ int Process::getParam(const char* parameter, char* value)
|
|||||||
char str[MQTTSNGW_PARAM_MAX];
|
char str[MQTTSNGW_PARAM_MAX];
|
||||||
char param[MQTTSNGW_PARAM_MAX];
|
char param[MQTTSNGW_PARAM_MAX];
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
|
string filename = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_CONFIG_FILE);
|
||||||
|
|
||||||
int i = 0, j = 0;
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,11 @@ namespace MQTTSNGW
|
|||||||
/*=================================
|
/*=================================
|
||||||
* Parameters
|
* 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 MQTTSNGW_MAX_TASK 10 // number of Tasks
|
||||||
#define PROCESS_LOG_BUFFER_SIZE 16384 // Ring buffer size for Logs
|
#define PROCESS_LOG_BUFFER_SIZE 16384 // Ring buffer size for Logs
|
||||||
#define MQTTSNGW_PARAM_MAX 128 // Max length of config records.
|
#define MQTTSNGW_PARAM_MAX 128 // Max length of config records.
|
||||||
|
|||||||
@@ -62,6 +62,11 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
throw Exception( "Gateway::initialize: invalid Gateway Id");
|
throw Exception( "Gateway::initialize: invalid Gateway Id");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (getParam("GatewayName", param) == 0)
|
||||||
|
{
|
||||||
|
_params.gatewayName = (uint8_t*) strdup(param);
|
||||||
|
}
|
||||||
|
|
||||||
_params.mqttVersion = DEFAULT_MQTT_VERSION;
|
_params.mqttVersion = DEFAULT_MQTT_VERSION;
|
||||||
if (getParam("MQTTVersion", param) == 0)
|
if (getParam("MQTTVersion", param) == 0)
|
||||||
{
|
{
|
||||||
@@ -100,7 +105,9 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
if (!strcasecmp(param, "YES"))
|
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.");
|
throw Exception("Gateway::initialize: can't authorize clients.");
|
||||||
}
|
}
|
||||||
@@ -113,7 +120,7 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
void Gateway::run(void)
|
void Gateway::run(void)
|
||||||
{
|
{
|
||||||
_lightIndicator.redLight(true);
|
_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() )
|
if ( getClientList()->isAuthorized() )
|
||||||
{
|
{
|
||||||
WRITELOG("\n Client authentication is required by the configuration settings.\n");
|
WRITELOG("\n Client authentication is required by the configuration settings.\n");
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ typedef struct
|
|||||||
uint8_t gatewayId;
|
uint8_t gatewayId;
|
||||||
uint8_t mqttVersion;
|
uint8_t mqttVersion;
|
||||||
uint16_t maxInflightMsgs;
|
uint16_t maxInflightMsgs;
|
||||||
|
uint8_t* gatewayName;
|
||||||
}GatewayParams;
|
}GatewayParams;
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace MQTTSNGW;
|
using namespace MQTTSNGW;
|
||||||
@@ -235,6 +236,15 @@ void Semaphore::timedwait(uint16_t millsec)
|
|||||||
=========================================*/
|
=========================================*/
|
||||||
RingBuffer::RingBuffer()
|
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);
|
key_t key = ftok(MQTTSNGW_RINGBUFFER_KEY, 1);
|
||||||
|
|
||||||
if ((_shmid = shmget(key, PROCESS_LOG_BUFFER_SIZE,
|
if ((_shmid = shmget(key, PROCESS_LOG_BUFFER_SIZE,
|
||||||
|
|||||||
@@ -19,13 +19,12 @@
|
|||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
#include "MQTTSNGWDefines.h"
|
|
||||||
|
|
||||||
namespace MQTTSNGW
|
namespace MQTTSNGW
|
||||||
{
|
{
|
||||||
|
|
||||||
#define MQTTSNGW_RINGBUFFER_KEY "/usr/local/etc/mqttsnGateway/config/ringbuffer.key"
|
#define MQTTSNGW_RINGBUFFER_KEY "ringbuffer.key"
|
||||||
#define MQTTSNGW_RB_MUTEX_KEY "/usr/local/etc/mqttsnGateway/config/rbmutex.key"
|
#define MQTTSNGW_RB_MUTEX_KEY "rbmutex.key"
|
||||||
#define MQTTSNGW_RB_SEMAPHOR_NAME "/rbsemaphor"
|
#define MQTTSNGW_RB_SEMAPHOR_NAME "/rbsemaphor"
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
|
|||||||
6
Makefile
6
Makefile
@@ -4,6 +4,8 @@ APPL := mainGateway
|
|||||||
LPROGNAME := MQTT-SNLogmonitor
|
LPROGNAME := MQTT-SNLogmonitor
|
||||||
LAPPL := mainLogmonitor
|
LAPPL := mainLogmonitor
|
||||||
|
|
||||||
|
CONFIG := MQTTSNGateway/param.conf
|
||||||
|
|
||||||
SRCDIR := MQTTSNGateway/src
|
SRCDIR := MQTTSNGateway/src
|
||||||
SUBDIR := MQTTSNPacket/src
|
SUBDIR := MQTTSNPacket/src
|
||||||
|
|
||||||
@@ -69,7 +71,7 @@ DEPS += $(CSRCS:%.c=$(OUTDIR)/%.d)
|
|||||||
|
|
||||||
.PHONY: install clean
|
.PHONY: install clean
|
||||||
|
|
||||||
all: $(PROG)
|
all: $(PROG) $(LPROG)
|
||||||
|
|
||||||
monitor: $(LPROG)
|
monitor: $(LPROG)
|
||||||
|
|
||||||
@@ -102,5 +104,7 @@ clean:
|
|||||||
|
|
||||||
install:
|
install:
|
||||||
cp -pf $(PROG) ../
|
cp -pf $(PROG) ../
|
||||||
|
cp -pf $(LPROG) ../
|
||||||
|
cp -pf $(CONFIG) ../
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user