Add a new sensor network Bluetooth RFCOMM

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2021-06-04 13:01:47 +09:00
parent de355e28b6
commit d3626bb68d
31 changed files with 928 additions and 165 deletions

View File

@@ -135,7 +135,7 @@
<sourceEntries>
<entry excluding="src/linux/ble|src/mainLogmonitor.cpp|src/linux/xbee|src/linux/udp6|src/linux/loralink|src/tests|GatewayTester|GatewayTester/samples|GatewayTester/src" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="MQTTSNGateway"/>
<entry excluding="src/linux/rfcomm|src/linux/udp6|src/tests|GatewayTester|src/linux/xbee|GatewayTester/samples|src/mainLogmonitor.cpp|src/linux/loralink|GatewayTester/src" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="MQTTSNGateway"/>
<entry excluding="samples|src" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="MQTTSNGateway/GatewayTester"/>
@@ -279,9 +279,9 @@
<sourceEntries>
<entry excluding="MQTTSNGateway|MQTTSNGateway/GatewayTester|MQTTSNGateway/src|MQTTSNGateway/GatewayTester/samples/ClientPubQoS-1|MQTTSNGateway/GatewayTester/samples/ClientSub|MQTTSNGateway/GatewayTester/samples/ClientPub|MQTTSNGateway/src/tests/mainTestProcess.cpp|MQTTSNGateway/src/linux|MQTTSNPacket/src|MQTTSNClient|MQTTSNPacket/test|MQTTSNPacket/samples|MQTTSNGateway/GatewayTester/samples/mainOTA.cpp|MQTTSNGateway/src/mainLogmonitor.cpp|MQTTSNGateway/GatewayTester/samples/mainTemplate.cpp|ClientPubQoS-1" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
<entry excluding="MQTTSNPacket/test|MQTTSNPacket/samples|MQTTSNGateway/GatewayTester/samples/ClientPub|MQTTSNGateway/src/tests/mainTestProcess.cpp|MQTTSNGateway/src/linux|MQTTSNGateway|MQTTSNGateway/GatewayTester/samples/mainOTA.cpp|MQTTSNGateway/GatewayTester/samples/ClientSub|MQTTSNClient|MQTTSNGateway/src/mainLogmonitor.cpp|MQTTSNGateway/GatewayTester/samples/ClientPubQoS-1|MQTTSNGateway/GatewayTester/samples/mainTemplate.cpp|MQTTSNGateway/src|MQTTSNPacket/src|MQTTSNGateway/GatewayTester|ClientPubQoS-1" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
<entry excluding="linux/ble" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="MQTTSNGateway/src"/>
<entry excluding="linux/rfcomm|linux/ble" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="MQTTSNGateway/src"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="MQTTSNPacket/src"/>
@@ -325,18 +325,6 @@
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.debug.1685199227;cdt.managedbuild.config.gnu.exe.debug.1685199227.;cdt.managedbuild.tool.gnu.c.compiler.exe.debug.2017162618;cdt.managedbuild.tool.gnu.c.compiler.input.814497727">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.release.561557339;cdt.managedbuild.config.gnu.exe.release.561557339.;cdt.managedbuild.tool.gnu.cpp.compiler.exe.release.1903732701;cdt.managedbuild.tool.gnu.cpp.compiler.input.1606625536">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.debug.1685199227;cdt.managedbuild.config.gnu.exe.debug.1685199227.;cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug.109068141;cdt.managedbuild.tool.gnu.cpp.compiler.input.1626802967">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
@@ -348,6 +336,18 @@
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.debug.1685199227;cdt.managedbuild.config.gnu.exe.debug.1685199227.;cdt.managedbuild.tool.gnu.c.compiler.exe.debug.2017162618;cdt.managedbuild.tool.gnu.c.compiler.input.814497727">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.release.561557339;cdt.managedbuild.config.gnu.exe.release.561557339.;cdt.managedbuild.tool.gnu.cpp.compiler.exe.release.1903732701;cdt.managedbuild.tool.gnu.cpp.compiler.input.1606625536">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>

View File

@@ -20,7 +20,7 @@ CPPSRCS := \
$(SUBDIR)/LGwProxy.cpp \
$(SUBDIR)/LMqttsnClient.cpp \
$(SUBDIR)/LNetworkUdp.cpp \
$(SUBDIR)/LNetworkBle.cpp \
$(SUBDIR)/LNetworkRfcomm.cpp \
$(SUBDIR)/LPublishManager.cpp \
$(SUBDIR)/LRegisterManager.cpp \
$(SUBDIR)/LSubscribeManager.cpp \

View File

@@ -59,10 +59,10 @@ UDPCONF = {
};
/*------------------------------------------------------
* BLE Configuration (theNetcon)
* RFCOMM Configuration (theNetcon)
*------------------------------------------------------*/
BLECONF = { "GatewayTestClient", // ClientId
{ 0x60, 0x57, 0x18, 0x06, 0x8b, 0x72 }, // GW Address
RFCOMMCONF = { "GatewayTestClient", // ClientId
"60:57:18:06:8B:72", // GW Address
1, // Rfcomm channel
};

View File

@@ -59,10 +59,10 @@ UDPCONF = {
};
/*------------------------------------------------------
* BLE Configuration (theNetcon)
* RFCOMM Configuration (theNetcon)
*------------------------------------------------------*/
BLECONF = { "GatewayTestClient", // ClientId
{ 0x60, 0x57, 0x18, 0x06, 0x8b, 0x72 }, // GW Address
RFCOMMCONF = { "GatewayTestClient", // ClientId
"60:57:18:06:8B:72", // GW Address
1, // Rfcomm channel
};

View File

@@ -59,10 +59,10 @@ UDPCONF = {
};
/*------------------------------------------------------
* BLE Configuration (theNetcon)
* RFCOMM Configuration (theNetcon)
*------------------------------------------------------*/
BLECONF = { "GatewayTestClient", // ClientId
{ 0x44, 0x1C, 0xA8, 0x16, 0x94, 0x94 }, // GW Address
RFCOMMCONF = { "GatewayTestClient", // ClientId
"60:57:18:06:8B:72", // GW Address
1, // Rfcomm channel
};

View File

@@ -58,10 +58,10 @@ UDPCONF = { "GatewayTestClient", // ClientId
};
/*------------------------------------------------------
* BLE Configuration (theNetcon)
* RFCOMM Configuration (theNetcon)
*------------------------------------------------------*/
BLECONF = { "GatewayTestClient", // ClientId
{ 0x60, 0x57, 0x18, 0x06, 0x8b, 0x72 }, // GW Address
RFCOMMCONF = { "GatewayTestClient", // ClientId
"60:57:18:06:8B:72", // GW Address
1, // Rfcomm channel
};

View File

@@ -66,16 +66,16 @@ LGwProxy::~LGwProxy()
_topicTbl.clearTopic();
}
void LGwProxy::initialize(SENSORNET_CONFIG_t netconf, LMqttsnConfig mqconf)
void LGwProxy::initialize(SENSORNET_CONFIG_t* netconf, LMqttsnConfig* mqconf)
{
_network.initialize(netconf);
_clientId = netconf.clientId;
_willTopic = mqconf.willTopic;
_willMsg = mqconf.willMsg;
_qosWill = mqconf.willQos;
_retainWill = mqconf.willRetain;
_cleanSession = mqconf.cleanSession;
_tkeepAlive = mqconf.keepAlive;
_clientId = netconf->clientId;
_willTopic = mqconf->willTopic;
_willMsg = mqconf->willMsg;
_qosWill = mqconf->willQos;
_retainWill = mqconf->willRetain;
_cleanSession = mqconf->cleanSession;
_tkeepAlive = mqconf->keepAlive;
_initialized = 1;
}

View File

@@ -23,7 +23,7 @@
#include "LMqttsnClientApp.h"
#include "LNetworkUdp.h"
#include "LNetworkBle.h"
#include "LNetworkRfcomm.h"
#include "LRegisterManager.h"
#include "LTimer.h"
#include "LTopicTable.h"
@@ -55,7 +55,7 @@ public:
LGwProxy();
~LGwProxy();
void initialize(SENSORNET_CONFIG_t netconf, LMqttsnConfig mqconf);
void initialize(SENSORNET_CONFIG_t* netconf, LMqttsnConfig* mqconf);
void connect(void);
void disconnect(uint16_t sec = 0);
int getMessage(void);

View File

@@ -53,8 +53,8 @@ int main(int argc, char** argv)
printf("\n%s", PAHO_COPYRIGHT0);
#if defined(UDP)
printf(" UDP\n");
#elif defined(BLE)
printf(" BLE\n");
#elif defined(RFCOMM)
printf(" RFCOMM\n");
#else
printf("\n");
#endif
@@ -90,7 +90,7 @@ int main(int argc, char** argv)
setup();
theClient->addTask(theClientMode);
theClient->initialize( theNetcon, theMqcon);
theClient->initialize( &theNetcon, &theMqcon);
do
{
theClient->run();
@@ -115,10 +115,10 @@ LMqttsnClient::~LMqttsnClient()
}
void LMqttsnClient::initialize(SENSORNET_CONFIG_t netconf, LMqttsnConfig mqconf)
void LMqttsnClient::initialize(SENSORNET_CONFIG_t* netconf, LMqttsnConfig* mqconf)
{
_gwProxy.initialize(netconf, mqconf);
setSleepDuration(mqconf.sleepDuration);
setSleepDuration(mqconf->sleepDuration);
}
void LMqttsnClient::addTask(bool clientMode)

View File

@@ -58,7 +58,7 @@ public:
void unsubscribe(const char* topicName);
void unsubscribe(const uint16_t topicId);
void disconnect(uint16_t sleepInSecs);
void initialize(SENSORNET_CONFIG_t netconf, LMqttsnConfig mqconf);
void initialize(SENSORNET_CONFIG_t* netconf, LMqttsnConfig* mqconf);
void run(void);
void addTask(bool test);
void setSleepDuration(uint32_t duration);

View File

@@ -22,7 +22,7 @@
======================================*/
//#define CLIENT_MODE
#define UDP
//#define BLE
//#define RFCOMM
/*======================================
* Debug Flag
======================================*/
@@ -75,10 +75,10 @@ struct LUdpConfig
uint16_t uPortNo;
};
struct LBleConfig
struct LRfcommConfig
{
const char* clientId;
uint8_t gwAddress[6];
const char* gwAddress;
uint8_t channel;
};
@@ -100,13 +100,16 @@ typedef enum
#ifdef UDP
#define NETWORK_CONFIG UdpConfig theNetworkConfig
#define UDPCONF LUdpConfig theNetcon
#define BLECONF LBleConfig theConf
#define RFCOMMCONF LRfcommConfig theConf
#define SENSORNET_CONFIG_t LUdpConfig
#else
#ifdef RFCOMM
#define NETWORK_CONFIG BleConfig theNetworkConfig
#define BLECONF LBleConfig theNetcon
#define RFCOMMCONF LRfcommConfig theNetcon
#define UDPCONF LUdpConfig theConf
#define SENSORNET_CONFIG_t LBleConfig
#define SENSORNET_CONFIG_t LRfcommConfig
#endif
#error "UDP and RFCOMM are not defined in LMqttsnClientApp.h"
#endif
#define CONNECT(...) theClient->getGwProxy()->connect(__VA_ARGS__)

View File

@@ -14,7 +14,7 @@
* Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
**************************************************************************************/
#include "LMqttsnClientApp.h"
#ifdef BLE
#ifdef RFCOMM
#include <stdio.h>
#include <sys/time.h>
@@ -29,7 +29,7 @@
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include "LNetworkBle.h"
#include "LNetworkRfcomm.h"
#include "LTimer.h"
#include "LScreen.h"
@@ -40,7 +40,7 @@ extern uint16_t getUint16(const uint8_t* pos);
extern uint32_t getUint32(const uint8_t* pos);
extern LScreen* theScreen;
extern bool theClientMode;
extern LBleConfig theNetcon;
extern LRfcommConfig theNetcon;
/*=========================================
Class LNetwork
=========================================*/
@@ -57,12 +57,12 @@ LNetwork::~LNetwork()
int LNetwork::broadcast(const uint8_t* xmitData, uint16_t dataLen)
{
return LBlePort::unicast(xmitData, dataLen);
return LRfcommPort::unicast(xmitData, dataLen);
}
int LNetwork::unicast(const uint8_t* xmitData, uint16_t dataLen)
{
return LBlePort::unicast(xmitData, dataLen);
return LRfcommPort::unicast(xmitData, dataLen);
}
uint8_t* LNetwork::getMessage(int* len)
@@ -70,7 +70,7 @@ uint8_t* LNetwork::getMessage(int* len)
*len = 0;
if (checkRecvBuf())
{
uint16_t recvLen = LBlePort::recv(_rxDataBuf, MQTTSN_MAX_PACKET_SIZE, false);
uint16_t recvLen = LRfcommPort::recv(_rxDataBuf, MQTTSN_MAX_PACKET_SIZE, false);
if (recvLen < 0)
{
@@ -87,12 +87,7 @@ uint8_t* LNetwork::getMessage(int* len)
{
*len = _rxDataBuf[0];
}
//if(recvLen != *len){
// *len = 0;
// return 0;
//}else{
return _rxDataBuf;
//}
}
}
return 0;
@@ -104,13 +99,13 @@ void LNetwork::setGwAddress(void)
void LNetwork::setFixedGwAddress(void)
{
_channel = LBlePort::_channel;
memcpy(_gwAddress, theNetcon.gwAddress, 6);
_channel = LRfcommPort::_channel;
str2ba( theNetcon.gwAddress, (bdaddr_t*)_gwAddress);
}
bool LNetwork::initialize(LBleConfig config)
bool LNetwork::initialize(LRfcommConfig* config)
{
return LBlePort::open(config);
return LRfcommPort::open(config);
}
void LNetwork::setSleep()
@@ -124,55 +119,51 @@ bool LNetwork::isBroadcastable()
}
/*=========================================
Class BleStack
Class RFCOMM Stack
=========================================*/
LBlePort::LBlePort()
LRfcommPort::LRfcommPort()
{
_disconReq = false;
_sockBle = 0;
_sockRfcomm = 0;
_channel = 0;
}
LBlePort::~LBlePort()
LRfcommPort::~LRfcommPort()
{
close();
}
void LBlePort::close()
void LRfcommPort::close()
{
if (_sockBle > 0)
if (_sockRfcomm > 0)
{
::close(_sockBle);
_sockBle = 0;
::close(_sockRfcomm);
_sockRfcomm = 0;
}
}
bool LBlePort::open(LBleConfig config)
bool LRfcommPort::open(LRfcommConfig* config)
{
const int reuse = 1;
uint8_t* gw = config.gwAddress + 5;
for (int i = 0; i < 6; i++)
{
*(_gwAddress + i) = *gw--;
}
_channel = config.channel;
str2ba(config->gwAddress, (bdaddr_t*)_gwAddress);
_channel = config->channel;
if (_channel == 0 || _gwAddress == 0 || _devAddress == 0)
if (_channel == 0 || _gwAddress == 0 )
{
D_NWLOG("\033[0m\033[0;31merror BLE Address in BlePort::open\033[0m\033[0;37m\n");
DISPLAY("\033[0m\033[0;31m\nerror BLE Address in BlePort::open\033[0m\033[0;37m\n");
D_NWLOG("\033[0m\033[0;31merror Bluetooth Address in LRfcommPort::open\033[0m\033[0;37m\n");
DISPLAY("\033[0m\033[0;31m\nerror Bluetooth Address in LRfcommPort::open\033[0m\033[0;37m\n");
return false;
}
_sockBle = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (_sockBle < 0)
_sockRfcomm = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (_sockRfcomm < 0)
{
D_NWLOG("\033[0m\033[0;31merror Can't create socket in BlePort::open\033[0m\033[0;37m\n");
DISPLAY("\033[0m\033[0;31m\nerror Can't create socket in BlePort::open\033[0m\033[0;37m\n");
D_NWLOG("\033[0m\033[0;31merror Can't create socket in LRfcommPort::open\033[0m\033[0;37m\n");
DISPLAY("\033[0m\033[0;31m\nerror Can't create socket in LRfcommPort::open\033[0m\033[0;37m\n");
return false;
}
setsockopt(_sockBle, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
setsockopt(_sockRfcomm, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
struct sockaddr_rc addru = { 0 };
addru.rc_family = AF_BLUETOOTH;
@@ -185,24 +176,24 @@ bool LBlePort::open(LBleConfig config)
// connect to server
errno = 0;
int status = connect(_sockBle, (struct sockaddr *) &addru, sizeof(addru));
int status = connect(_sockRfcomm, (struct sockaddr *) &addru, sizeof(addru));
if (status < 0)
{
D_NWLOG("\033[0m\033[0;31merror = %d Can't connect to GW in BlePort::open\033[0m\033[0;37m\n", errno);
DISPLAY("\033[0m\033[0;31mCan't connect to GW Ble socket in BlePort::open\033[0m\033[0;37m\n");
D_NWLOG("\033[0m\033[0;31merror = %d Can't connect to GW in LRfcommPort::open\033[0m\033[0;37m\n", errno);
DISPLAY("\033[0m\033[0;31merror = %d Can't connect to GW Ble socket in LRfcommPort::open\033[0m\033[0;37m\n",errno);
close();
return false;
}
return true;
}
int LBlePort::unicast(const uint8_t* buf, uint32_t length)
int LRfcommPort::unicast(const uint8_t* buf, uint32_t length)
{
int status = ::write(_sockBle, buf, length);
int status = ::write(_sockRfcomm, buf, length);
if (status < 0)
{
D_NWLOG("errno == %d in LBlePort::unicast\n", errno);
DISPLAY("errno == %d in LBlePort::unicast\n", errno);
D_NWLOG("errno == %d in LRfcommPort::unicast\n", errno);
DISPLAY("errno == %d in LRfcommPort::unicast\n", errno);
}
else
{
@@ -235,25 +226,25 @@ int LBlePort::unicast(const uint8_t* buf, uint32_t length)
return status;
}
bool LBlePort::checkRecvBuf()
bool LRfcommPort::checkRecvBuf()
{
uint8_t buf[2];
if (::recv(_sockBle, buf, 1, MSG_DONTWAIT | MSG_PEEK) > 0)
if (::recv(_sockRfcomm, buf, 1, MSG_DONTWAIT | MSG_PEEK) > 0)
{
return true;
}
return false;
}
int LBlePort::recv(uint8_t* buf, uint16_t length, bool flg)
int LRfcommPort::recv(uint8_t* buf, uint16_t length, bool flg)
{
int flags = flg ? MSG_DONTWAIT : 0;
int status = ::recv(_sockBle, buf, length, flags);
int status = ::recv(_sockRfcomm, buf, length, flags);
if (status < 0 && errno != EAGAIN)
{
D_NWLOG("\033[0m\033[0;31merrno == %d in BlePort::recv \033[0m\033[0;37m\n", errno);
DISPLAY("\033[0m\033[0;31merrno == %d in BlePort::recv \033[0m\033[0;37m\n", errno);
D_NWLOG("\033[0m\033[0;31merrno = %d in LRfcommPort::recv \033[0m\033[0;37m\n", errno);
DISPLAY("\033[0m\033[0;31merrno = %d in LRfcommPort::recv \033[0m\033[0;37m\n", errno);
}
else if (status > 0)
{

View File

@@ -14,11 +14,11 @@
* Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
**************************************************************************************/
#ifndef NETWORKBLE_H_
#define NETWORKBLE_H_
#ifndef NETWORKRFCOMM_H_
#define NETWORKRFCOMM_H_
#include "LMqttsnClientApp.h"
#ifdef BLE
#ifdef RFCOMM
#include <sys/time.h>
#include <iostream>
@@ -44,17 +44,15 @@ using namespace std;
namespace linuxAsyncClient
{
/*========================================
Class LBlePort
Class LRfcommPort
=======================================*/
class LBlePort
class LRfcommPort
{
friend class LNetwork;
public:
LBlePort();
virtual ~LBlePort();
bool open(LBleConfig config);
LRfcommPort();
virtual ~LRfcommPort();
bool open(LRfcommConfig* config);
int unicast(const uint8_t* buf, uint32_t length);
int recv(uint8_t* buf, uint16_t len, bool nonblock);
bool checkRecvBuf();
@@ -63,8 +61,7 @@ public:
private:
void close();
int _sockBle;
uint8_t _devAddress[6];
int _sockRfcomm;
uint8_t _gwAddress[6];
uint8_t _channel;
bool _disconReq;
@@ -76,7 +73,7 @@ private:
/*===========================================
Class Network
============================================*/
class LNetwork: public LBlePort
class LNetwork: public LRfcommPort
{
public:
LNetwork();
@@ -87,7 +84,7 @@ public:
void setGwAddress(void);
void resetGwAddress(void);
void setFixedGwAddress(void);
bool initialize(LBleConfig config);
bool initialize(LRfcommConfig* config);
uint8_t* getMessage(int* len);
bool isBroadcastable();
@@ -102,5 +99,5 @@ private:
};
} /* end of namespace */
#endif /* BLE */
#endif /* NETWORKBLE_H_ */
#endif /* RFCOMM */
#endif /* NETWORKRFCOM_H_ */

View File

@@ -104,7 +104,7 @@ void LNetwork::resetGwAddress(void){
}
bool LNetwork::initialize(LUdpConfig config){
bool LNetwork::initialize(LUdpConfig* config){
return LUdpPort::open(config);
}
@@ -119,43 +119,48 @@ bool LNetwork::isBroadcastable()
/*=========================================
Class udpStack
=========================================*/
LUdpPort::LUdpPort(){
LUdpPort::LUdpPort()
{
_disconReq = false;
_sockfdUcast = -1;
_sockfdMcast = -1;
_castStat = 0;
}
LUdpPort::~LUdpPort(){
LUdpPort::~LUdpPort()
{
close();
}
void LUdpPort::close(){
if(_sockfdMcast > 0){
if(_sockfdMcast > 0)
{
::close( _sockfdMcast);
_sockfdMcast = -1;
if(_sockfdUcast > 0){
if(_sockfdUcast > 0)
{
::close( _sockfdUcast);
_sockfdUcast = -1;
}
}
}
bool LUdpPort::open(LUdpConfig config){
bool LUdpPort::open(LUdpConfig* config)
{
const int reuse = 1;
char loopch = 1;
uint8_t sav = config.ipAddress[3];
config.ipAddress[3] = config.ipAddress[0];
config.ipAddress[0] = sav;
sav = config.ipAddress[2];
config.ipAddress[2] = config.ipAddress[1];
config.ipAddress[1] = sav;
uint8_t sav = config->ipAddress[3];
config->ipAddress[3] = config->ipAddress[0];
config->ipAddress[0] = sav;
sav = config->ipAddress[2];
config->ipAddress[2] = config->ipAddress[1];
config->ipAddress[1] = sav;
_gPortNo = htons(config.gPortNo);
_gIpAddr = getUint32((const uint8_t*)config.ipAddress);
_uPortNo = htons(config.uPortNo);
_gPortNo = htons(config->gPortNo);
_gIpAddr = getUint32((const uint8_t*)config->ipAddress);
_uPortNo = htons(config->uPortNo);
if( _gPortNo == 0 || _gIpAddr == 0 || _uPortNo == 0){
return false;

View File

@@ -51,7 +51,7 @@ public:
LUdpPort();
virtual ~LUdpPort();
bool open(LUdpConfig config);
bool open(LUdpConfig* config);
int unicast(const uint8_t* buf, uint32_t length, uint32_t ipaddress, uint16_t port );
int multicast( const uint8_t* buf, uint32_t length );
@@ -89,7 +89,7 @@ public:
void setGwAddress(void);
void resetGwAddress(void);
void setFixedGwAddress(void);
bool initialize(LUdpConfig config);
bool initialize(LUdpConfig* config);
uint8_t* getMessage(int* len);
bool isBroadcastable();
private:

View File

@@ -7,7 +7,7 @@ This Gateway can run as a transparent or aggregating Gateway by specifying the g
````
$ git clone -b develop https://github.com/eclipse/paho.mqtt-sn.embedded-c
$ cd paho.mqtt-sn.embedded-c/MQTTSNGateway
$ ./build.sh [udp|udp6|xbee|loralink | ble]
$ ./build.sh [udp|udp6|xbee|loralink|rfcomm]
````
In order to build a gateway, an argument is required.
@@ -92,8 +92,8 @@ BaudrateLoRaLink=115200
DeviceRxLoRaLink=/dev/ttyLoRaLinkRx
DeviceTxLoRaLink=/dev/ttyLoRaLinkTx
# BLE RFCOMM
BleAddress=60:57:18:06:8B:72.*
# Bluetooth RFCOMM
RFCOMMAddress=60:57:18:06:8B:72.*
# LOG
ShearedMemory=NO;

View File

@@ -1,7 +1,7 @@
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: build.sh { udp | udp6 | xbee | loralink }"
echo "Usage: build.sh [ udp | udp6 | xbee | loralink | rfcomm ]"
else
echo "Start building MQTT-SN Gateway"

View File

@@ -69,8 +69,8 @@ BaudrateLoRaLink=115200
DeviceRxLoRaLink=/dev/loralinkRx
DeviceTxLoRaLink=/dev/loralinkTx
# BLE RFCOMM
BleAddress=60:57:18:06:8B:72.*
# Bluetooth RFCOMM
RFCOMMAddress=60:57:18:06:8B:72.*
# LOG
ShearedMemory=NO;

View File

@@ -87,7 +87,7 @@ TARGET_INCLUDE_DIRECTORIES(mqtt-sngateway_common
/usr/local/opt/openssl/include
)
IF(SENSORNET MATCHES "ble")
IF(SENSORNET MATCHES "rfcomm")
TARGET_LINK_LIBRARIES(mqtt-sngateway_common
PRIVATE

View File

@@ -158,7 +158,7 @@ SensorNetwork::~SensorNetwork()
int SensorNetwork::unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendToAddr)
{
uint16_t ch = sendToAddr->getPortNo();
BlePort* blep = &_rfPorts[ch - 1];
RfcommPort* blep = &_rfPorts[ch - 1];
int rc = 0;
errno = 0;
@@ -220,7 +220,6 @@ int SensorNetwork::read(uint8_t* buf, uint16_t bufLen)
int rc = 0;
if (select(maxSock + 1, &recvfds, 0, 0, &timeout) > 0)
{
WRITELOG("RECV\n");
for (int i = 0; i < MAX_RFCOMM_CH; i++)
{
if (_rfPorts[i]._rfCommSock > 0)
@@ -243,7 +242,6 @@ int SensorNetwork::read(uint8_t* buf, uint16_t bufLen)
{
_rfPorts[i]._rfCommSock = sock;
}
WRITELOG("accept sock= %d CH = %d\n", sock, i + 1);
}
}
}
@@ -269,24 +267,24 @@ void SensorNetwork::initialize(void)
* in Gateway.conf e.g.
*
* # BLE
* BleAddress=XX:XX:XX:XX:XX:XX.0
* RFCOMM=XX:XX:XX:XX:XX:XX.0
*
*/
if (theProcess->getParam("BleAddress", param) == 0)
if (theProcess->getParam("RFCOMMAddress", param) == 0)
{
devAddr = param;
_description = "BLE RFCOMM ";
_description = "Bluetooth RFCOMM ";
_description += param;
}
errno = 0;
if (sa.setAddress(&devAddr) == -1)
{
throw EXCEPTION("Invalid BLE Address", errno);
throw EXCEPTION("Invalid Bluetooth Address", errno);
}
/* Prepare BLE sockets */
WRITELOG("Initialize ble\n");
WRITELOG("Initialize RFCOMM\n");
int rc = MAX_RFCOMM_CH;
for (uint16_t i = 0; i < MAX_RFCOMM_CH; i++)
{
@@ -295,7 +293,7 @@ void SensorNetwork::initialize(void)
}
if (rc == 0)
{
throw EXCEPTION("Can't open BLE RFComms", errno);
throw EXCEPTION("Can't open Bluetooth RFComms", errno);
}
}
@@ -313,7 +311,7 @@ SensorNetAddress* SensorNetwork::getSenderAddress(void)
Class BleStack
=========================================*/
BlePort::BlePort()
RfcommPort::RfcommPort()
{
_disconReq = false;
_rfCommSock = 0;
@@ -321,7 +319,7 @@ BlePort::BlePort()
_channel = 0;
}
BlePort::~BlePort()
RfcommPort::~RfcommPort()
{
close();
@@ -331,7 +329,7 @@ BlePort::~BlePort()
}
}
void BlePort::close(void)
void RfcommPort::close(void)
{
if (_rfCommSock > 0)
{
@@ -340,7 +338,7 @@ void BlePort::close(void)
}
}
int BlePort::open(bdaddr_t* devAddr, uint16_t channel)
int RfcommPort::open(bdaddr_t* devAddr, uint16_t channel)
{
const int reuse = 1;
@@ -380,13 +378,12 @@ int BlePort::open(bdaddr_t* devAddr, uint16_t channel)
return 1;
}
int BlePort::send(const uint8_t* buf, uint32_t length)
int RfcommPort::send(const uint8_t* buf, uint32_t length)
{
WRITELOG("sock = %d\n", _rfCommSock);
return ::send(_rfCommSock, buf, length, 0);
}
int BlePort::recv(uint8_t* buf, uint16_t len)
int RfcommPort::recv(uint8_t* buf, uint16_t len)
{
int rc = 0;
errno = 0;
@@ -400,7 +397,7 @@ int BlePort::recv(uint8_t* buf, uint16_t len)
return rc;
}
int BlePort::accept(SensorNetAddress* addr)
int RfcommPort::accept(SensorNetAddress* addr)
{
struct sockaddr_rc devAddr = { 0 };
socklen_t opt = sizeof(devAddr);
@@ -419,7 +416,7 @@ int BlePort::accept(SensorNetAddress* addr)
return sock;
}
int BlePort::getSock(void)
int RfcommPort::getSock(void)
{
return _rfCommSock;
}

View File

@@ -55,14 +55,14 @@ private:
};
/*========================================
Class BlePort
Class RfcommPort
=======================================*/
class BlePort
class RfcommPort
{
friend class SensorNetwork;
public:
BlePort();
virtual ~BlePort();
RfcommPort();
virtual ~RfcommPort();
int open(bdaddr_t* devAddress, uint16_t channel);
void close(void);
@@ -95,7 +95,7 @@ public:
private:
// sockets for RFCOMM
BlePort _rfPorts[MAX_RFCOMM_CH];
RfcommPort _rfPorts[MAX_RFCOMM_CH];
SensorNetAddress _senderAddr;
string _description;
};

View File

@@ -0,0 +1,14 @@
rm -rf bin
mkdir bin
cd bin
PKTSRC=../../../../src
SRC=..
gcc -Wall -c $SRC/rfcomm.c -Os -s
gcc -Wall $SRC/qos0pub.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNPacket.c $PKTSRC/MQTTSNConnectClient.c -o qos0pub -Os -s
gcc -Wall $SRC/qos0pub_register.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNDeserializePublish.c $PKTSRC/MQTTSNPacket.c $PKTSRC/MQTTSNConnectClient.c -o qos0pub_register -Os -s
gcc -Wall $SRC/qos-1pub.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNPacket.c -o qos-1pub -Os -s
gcc -Wall $SRC/qos-1pub_extended.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNPacket.c -o qos-1pub_extended -Os -s
gcc -Wall $SRC/qos1pub.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNDeserializePublish.c $PKTSRC/MQTTSNPacket.c $PKTSRC/MQTTSNConnectClient.c -o qos1pub -Os -s
gcc -Wall $SRC/pub0sub1.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNDeserializePublish.c $PKTSRC/MQTTSNPacket.c $PKTSRC/MQTTSNConnectClient.c $PKTSRC/MQTTSNSubscribeClient.c -o pub0sub1 -Os -s
rm rfcomm.o

View File

@@ -0,0 +1,164 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Sergio R. Caprile - clarifications and/or documentation extension
*
* Description:
* Normal topic name is automatically registered at subscription, then
* a message is published and the node receives it itself
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MQTTSNPacket.h"
#include "rfcomm.h"
int main(int argc, char** argv)
{
int rc = 0;
unsigned char buf[200];
int buflen = sizeof(buf);
MQTTSN_topicid topic;
unsigned char* payload = (unsigned char*)"mypayload";
int payloadlen = strlen((char*)payload);
int len = 0;
unsigned char dup = 0;
int qos = 1;
unsigned char retained = 0;
short packetid = 1;
char *topicname = "a long topic name";
char *host = "";
int channel = 1;
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
unsigned short topicid;
if (argc > 1)
host = argv[1];
if (argc > 2)
channel = atoi(argv[2]);
printf("Sending to address %s channel %d\n", host, channel);
if (rfcomm_open(host, channel) < 0)
{
goto exit;
}
options.clientID.cstring = "pub0sub1 MQTT-SN";
len = MQTTSNSerialize_connect(buf, buflen, &options);
rc = rfcomm_sendPacketBuffer(buf, len);
/* wait for connack */
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_CONNACK)
{
int connack_rc = -1;
if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
{
printf("Unable to connect, return code %d\n", connack_rc);
goto exit;
}
else
printf("connected rc %d\n", connack_rc);
}
else
goto exit;
/* subscribe */
printf("Subscribing\n");
topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
topic.data.long_.name = topicname;
topic.data.long_.len = strlen(topic.data.long_.name);
len = MQTTSNSerialize_subscribe(buf, buflen, 0, 2, packetid, &topic);
rc = rfcomm_sendPacketBuffer(buf, len);
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_SUBACK) /* wait for suback */
{
unsigned short submsgid;
int granted_qos;
unsigned char returncode;
rc = MQTTSNDeserialize_suback(&granted_qos, &topicid, &submsgid, &returncode, buf, buflen);
if (granted_qos != 2 || returncode != 0)
{
printf("granted qos != 2, %d return code %d\n", granted_qos, returncode);
goto exit;
}
else
printf("suback topic id %d\n", topicid);
}
else
goto exit;
printf("Publishing\n");
/* publish with short name */
topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
topic.data.id = topicid;
++packetid;
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
topic, payload, payloadlen);
rc = rfcomm_sendPacketBuffer(buf, len);
/* wait for puback */
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_PUBACK)
{
unsigned short packet_id, topic_id;
unsigned char returncode;
if (MQTTSNDeserialize_puback(&topic_id, &packet_id, &returncode, buf, buflen) != 1 || returncode != MQTTSN_RC_ACCEPTED)
printf("Unable to publish, return code %d\n", returncode);
else
printf("puback received, msgid %d topic id %d\n", packet_id, topic_id);
}
else
goto exit;
printf("Receive publish\n");
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_PUBLISH)
{
unsigned short packet_id;
int qos, payloadlen;
unsigned char* payload;
unsigned char dup, retained;
MQTTSN_topicid pubtopic;
if (MQTTSNDeserialize_publish(&dup, &qos, &retained, &packet_id, &pubtopic,
&payload, &payloadlen, buf, buflen) != 1)
printf("Error deserializing publish\n");
else
printf("publish received, id %d qos %d\n", packet_id, qos);
if (qos == 1)
{
len = MQTTSNSerialize_puback(buf, buflen, pubtopic.data.id, packet_id, MQTTSN_RC_ACCEPTED);
rc = rfcomm_sendPacketBuffer(buf, len);
if (rc == 0)
printf("puback sent\n");
}
}
else
goto exit;
len = MQTTSNSerialize_disconnect(buf, buflen, 0);
rc = rfcomm_sendPacketBuffer(buf, len);
exit:
rfcomm_close();
return 0;
}

View File

@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Sergio R. Caprile - clarifications and/or documentation extension
*
* Description:
* A qos -1 message can be sent without connecting
* Short topic name used to avoid registration process
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MQTTSNPacket.h"
#include "rfcomm.h"
int main(int argc, char** argv)
{
unsigned char buf[200];
int buflen = sizeof(buf);
MQTTSN_topicid topic;
unsigned char* payload = (unsigned char*)"mypayload";
int payloadlen = strlen((char*)payload);
int len = 0;
int dup = 0;
int qos = 3;
int retained = 0;
short packetid = 0;
char *host = "";
int channel = 1;
if (argc > 1)
host = argv[1];
if (argc > 2)
channel = atoi(argv[2]);
printf("Sending to address %s channel %d\n", host, channel);
if (rfcomm_open(host, channel) < 0)
{
return -1;
}
/* publish with short name */
topic.type = MQTTSN_TOPIC_TYPE_SHORT;
memcpy(topic.data.short_name, "tt", 2);
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
topic, payload, payloadlen);
rfcomm_sendPacketBuffer(buf, len);
rfcomm_close();
return 0;
}

View File

@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Sergio R. Caprile - clarifications and/or documentation extension
*
* Description:
* Extension to the specs in which a node can send a normal (long) topic name inside the
* payload area to avoid the registration process and the usage of short/predefined types
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MQTTSNPacket.h"
#include "rfcomm.h"
int main(int argc, char** argv)
{
unsigned char buf[200];
int buflen = sizeof(buf);
MQTTSN_topicid topic;
unsigned char* payload = (unsigned char*)"mypayload";
int payloadlen = strlen((char*)payload);
int len = 0;
int dup = 0;
int qos = 3;
int retained = 0;
short packetid = 0;
char *topicname = "a long topic name";
char *host = "";
int channel = 1;
if (argc > 1)
host = argv[1];
if (argc > 2)
channel = atoi(argv[2]);
printf("Sending to Address %s channel %d\n", host, channel);
if (rfcomm_open(host, channel) < 0)
{
return -1;;
}
topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
topic.data.long_.name = topicname;
topic.data.long_.len = strlen(topicname);
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
topic, payload, payloadlen);
rfcomm_sendPacketBuffer(buf, len);
rfcomm_close();
return 0;
}

View File

@@ -0,0 +1,93 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Sergio R. Caprile - clarifications and/or documentation extension
*
* Description:
* Short topic name used to avoid registration process
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MQTTSNPacket.h"
#include "rfcomm.h"
int main(int argc, char** argv)
{
int rc = 0;
unsigned char buf[200];
int buflen = sizeof(buf);
MQTTSN_topicid topic;
unsigned char* payload = (unsigned char*)"mypayload";
int payloadlen = strlen((char*)payload);
int len = 0;
int dup = 0;
int qos = 0;
int retained = 0;
short packetid = 0;
// char *topicname = "a long topic name";
char *host = "";
int channel = 1;
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
if (argc > 1)
host = argv[1];
if (argc > 2)
channel = atoi(argv[2]);
printf("Sending to address %s port %d\n", host, channel);
if (rfcomm_open(host, channel) < 0)
{
goto exit;
}
options.clientID.cstring = "myclientid";
len = MQTTSNSerialize_connect(buf, buflen, &options);
rc = rfcomm_sendPacketBuffer(buf, len);
/* wait for connack */
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_CONNACK)
{
int connack_rc = -1;
if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
{
printf("Unable to connect, return code %d\n", connack_rc);
goto exit;
}
else
printf("connected rc %d\n", connack_rc);
}
else
goto exit;
/* publish with short name */
topic.type = MQTTSN_TOPIC_TYPE_SHORT;
memcpy(topic.data.short_name, "tt", 2);
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
topic, payload, payloadlen);
rc = rfcomm_sendPacketBuffer(buf, len);
printf("rc %d from send packet for publish length %d\n", rc, len);
exit:
rfcomm_close();
return 0;
}

View File

@@ -0,0 +1,120 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Sergio R. Caprile - clarifications and/or documentation extension
*
* Description:
* Normal topic name used to show registration process
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MQTTSNPacket.h"
#include "rfcomm.h"
int main(int argc, char** argv)
{
int rc = 0;
unsigned char buf[200];
int buflen = sizeof(buf);
MQTTSN_topicid topic;
MQTTSNString topicstr;
unsigned char* payload = (unsigned char*)"mypayload";
int payloadlen = strlen((char*)payload);
int len = 0;
int dup = 0;
int qos = 0;
int retained = 0;
short packetid = 0;
char *topicname = "a long topic name";
char *host = "";
int channel = 1;
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
unsigned short topicid;
if (argc > 1)
host = argv[1];
if (argc > 2)
channel = atoi(argv[2]);
printf("Sending to address %s channel %d\n", host, channel);
if (rfcomm_open(host, (unsigned char) channel) < 0)
{
goto exit;
}
options.clientID.cstring = "myclientid";
len = MQTTSNSerialize_connect(buf, buflen, &options);
rc = rfcomm_sendPacketBuffer(buf, len);
/* wait for connack */
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_CONNACK)
{
int connack_rc = -1;
if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
{
printf("Unable to connect, return code %d\n", connack_rc);
goto exit;
}
else
printf("connected rc %d\n", connack_rc);
}
else
goto exit;
/* register topic name */
printf("Registering\n");
topicstr.cstring = topicname;
topicstr.lenstring.len = strlen(topicname);
len = MQTTSNSerialize_register(buf, buflen, 0, packetid, &topicstr);
rc = rfcomm_sendPacketBuffer(buf, len);
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_REGACK) /* wait for regack */
{
unsigned short submsgid;
unsigned char returncode;
rc = MQTTSNDeserialize_regack(&topicid, &submsgid, &returncode, buf, buflen);
if (returncode != 0)
{
printf("return code %d\n", returncode);
goto exit;
}
else
printf("regack topic id %d\n", topicid);
}
else
goto exit;
/* publish with obtained id */
printf("Publishing\n");
topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
topic.data.id = topicid;
++packetid;
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
topic, payload, payloadlen);
rc = rfcomm_sendPacketBuffer(buf, len);
printf("rc %d from send packet for publish length %d\n", rc, len);
exit:
rfcomm_close();
return 0;
}

View File

@@ -0,0 +1,105 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Sergio R. Caprile - clarifications and/or documentation extension
*
* Description:
* Short topic name used to avoid registration process
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MQTTSNPacket.h"
#include "rfcomm.h"
int main(int argc, char** argv)
{
unsigned char buf[200];
int buflen = sizeof(buf);
MQTTSN_topicid topic;
unsigned char* payload = (unsigned char*)"mypayload";
int payloadlen = strlen((char*)payload);
int len = 0;
int dup = 0;
int qos = 1;
int retained = 0;
short packetid = 1;
char *host = "";
int channel = 1;
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
if (argc > 1)
host = argv[1];
if (argc > 2)
channel = atoi(argv[2]);
printf("Sending to address %s channel %d\n", host, channel);
if (rfcomm_open(host, channel) < 0)
{
goto exit;
}
options.clientID.cstring = "myclientid";
len = MQTTSNSerialize_connect(buf, buflen, &options);
rfcomm_sendPacketBuffer(buf, len);
/* wait for connack */
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_CONNACK)
{
int connack_rc = -1;
if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
{
printf("Unable to connect, return code %d\n", connack_rc);
goto exit;
}
else
printf("connected rc %d\n", connack_rc);
}
else
goto exit;
/* publish with short name */
topic.type = MQTTSN_TOPIC_TYPE_SHORT;
memcpy(topic.data.short_name, "tt", 2);
len = MQTTSNSerialize_publish(buf, buflen - len, dup, qos, retained, packetid,
topic, payload, payloadlen);
rfcomm_sendPacketBuffer(buf, len);
/* wait for puback */
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_PUBACK)
{
unsigned short packet_id, topic_id;
unsigned char returncode;
if (MQTTSNDeserialize_puback(&topic_id, &packet_id, &returncode, buf, buflen) != 1 || returncode != MQTTSN_RC_ACCEPTED)
printf("Unable to publish, return code %d\n", returncode);
else
printf("puback received, id %d\n", packet_id);
}
else
goto exit;
len = MQTTSNSerialize_disconnect(buf, buflen, 0);
rfcomm_sendPacketBuffer(buf, len);
exit:
rfcomm_close();
return 0;
}

View File

@@ -0,0 +1,115 @@
/*******************************************************************************
* Copyright (c) 2021 tomoaki@tomy-tech.com
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Tomoaki Yamaguchi - initial implementation
*******************************************************************************/
#if defined(WIN32) || defined(__APP__)
#error "Only available on Linux."
#endif
#if !defined(SOCKET_ERROR)
/** error in socket operation */
#define SOCKET_ERROR -1
#endif
#define INVALID_SOCKET SOCKET_ERROR
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "rfcomm.h"
static int mysock = INVALID_SOCKET;
int Socket_error(char* aString, int sock)
{
if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS && errno != EWOULDBLOCK)
{
if (strcmp(aString, "shutdown") != 0 || (errno != ENOTCONN && errno != ECONNRESET))
{
int orig_errno = errno;
char* errmsg = strerror(errno);
printf("Socket error %d (%s) in %s for socket %d\n", orig_errno, errmsg, aString, sock);
}
}
return -errno;
}
int rfcomm_sendPacketBuffer(unsigned char* buf, int buflen)
{
int rc = 0;
if ((rc = write(mysock, buf, buflen)) == SOCKET_ERROR)
{
Socket_error("sendto", mysock);
}
else
{
rc = 0;
}
return rc;
}
int rfcomm_getdata(unsigned char* buf, int count)
{
int rc = recv(mysock, buf, count, 0);
printf("received %d bytes count %d\n", rc, (int) count);
return rc;
}
/**
return >=0 for a socket descriptor, <0 for an error code
*/
int rfcomm_open(char* addr, unsigned char channel)
{
const int reuse = 1;
mysock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (mysock == INVALID_SOCKET)
return Socket_error("socket", mysock);
setsockopt(mysock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
struct sockaddr_rc addru = { 0 };
addru.rc_family = AF_BLUETOOTH;
addru.rc_channel = channel;
str2ba(addr, &addru.rc_bdaddr);
// connect to server
errno = 0;
if (connect(mysock, (struct sockaddr *) &addru, sizeof(addru)) < 0)
{
rfcomm_close();
return Socket_error("connect", mysock);
}
return mysock;
}
int rfcomm_close()
{
int rc;
rc = shutdown(mysock, SHUT_WR);
rc = close(mysock);
mysock = INVALID_SOCKET;
return rc;
}

View File

@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Sergio R. Caprile - "commonalization" from prior samples and/or documentation extension
*******************************************************************************/
int rfcom_sendPacketBuffer(unsigned char* buf, int buflen);
int rfcomm_getdata(unsigned char* buf, int count);
int rfcomm_open(char* address, unsigned char channel);
int rfcomm_close(void);

View File

@@ -9,7 +9,7 @@ echo "travis build dir $TRAVIS_BUILD_DIR pwd $PWD"
cmake .. -DSENSORNET=loralink
make
ctest -VV --timeout 600
cmake .. -DSENSORNET=ble
cmake .. -DSENSORNET=rfcomm
make MQTT-SNGateway
cmake .. -DSENSORNET=xbee
make MQTT-SNGateway