Add Bluetooth classic as a sensor network #69, #195, #90

I think the architecture of the ble sensor network, which does not use
threads per socket, can be applied to DTLS.

Known bug:
Occasionally a timeout error occurs when connecting to RFCOMM.
BLE is not supported yet. I need help to do it.


Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2021-06-02 20:15:52 +09:00
parent 982e6d4884
commit 55128f0f0e
54 changed files with 1764 additions and 934 deletions

View File

@@ -32,8 +32,8 @@ ClientList::ClientList(Gateway* gw)
_authorize = false;
_firstClient = nullptr;
_endClient = nullptr;
_clientsPool = new ClientsPool();
_gateway = gw;
_clientsPool = new ClientsPool();
_gateway = gw;
}
ClientList::~ClientList()
@@ -49,18 +49,18 @@ ClientList::~ClientList()
cl = ncl;
};
if (_clientsPool)
{
delete _clientsPool;
}
if (_clientsPool)
{
delete _clientsPool;
}
_mutex.unlock();
}
void ClientList::initialize(bool aggregate)
{
_clientsPool->allocate(_gateway->getGWParams()->maxClients);
_clientsPool->allocate(_gateway->getGWParams()->maxClients);
if (_gateway->getGWParams()->clientAuthentication)
if (_gateway->getGWParams()->clientAuthentication)
{
int type = TRANSPEARENT_TYPE;
if (aggregate)
@@ -71,7 +71,7 @@ void ClientList::initialize(bool aggregate)
_authorize = true;
}
if (_gateway->getGWParams()->predefinedTopic)
if (_gateway->getGWParams()->predefinedTopic)
{
setPredefinedTopics(aggregate);
}
@@ -79,21 +79,17 @@ void ClientList::initialize(bool aggregate)
void ClientList::setClientList(int type)
{
if (!createList(_gateway->getGWParams()->clientListName, type))
if (!createList(_gateway->getGWParams()->clientListName, type))
{
throw EXCEPTION(
"ClientList::setClientList Client list not found!", 0);
throw EXCEPTION("ClientList::setClientList Client list not found!", 0);
}
}
void ClientList::setPredefinedTopics(bool aggrecate)
{
if (!readPredefinedList(_gateway->getGWParams()->predefinedTopicFileName,
aggrecate))
if (!readPredefinedList(_gateway->getGWParams()->predefinedTopicFileName, aggrecate))
{
throw EXCEPTION(
"ClientList::setPredefinedTopics PredefindTopic list not found!",
0);
throw EXCEPTION("ClientList::setPredefinedTopics PredefindTopic list not found!", 0);
}
}
@@ -161,15 +157,13 @@ bool ClientList::createList(const char* fileName, int type)
forwarder = (data.find("forwarder") != string::npos);
secure = (data.find("secureConnection") != string::npos);
stable = !(data.find("unstableLine") != string::npos);
if ((qos_1 && type == QOSM1PROXY_TYPE)
|| (!qos_1 && type == AGGREGATER_TYPE))
if ((qos_1 && type == QOSM1PROXY_TYPE) || (!qos_1 && type == AGGREGATER_TYPE))
{
createClient(&netAddr, &clientId, stable, secure, type);
}
else if (forwarder && type == FORWARDER_TYPE)
{
_gateway->getAdapterManager()->getForwarderList()->addForwarder(
&netAddr, &clientId);
_gateway->getAdapterManager()->getForwarderList()->addForwarder(&netAddr, &clientId);
}
else if (type == TRANSPEARENT_TYPE)
{
@@ -230,8 +224,7 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
}
else
{
WRITELOG("ClientList can not open the Predefined Topic List. %s\n",
fileName);
WRITELOG("ClientList can not open the Predefined Topic List. %s\n", fileName);
return false;
}
return rc;
@@ -327,8 +320,7 @@ Client* ClientList::getClient(MQTTSNString* clientId)
while (client != nullptr)
{
if (strncmp((const char*) client->getClientId(), clID,
MQTTSNstrlen(*clientId)) == 0)
if (strncmp((const char*) client->getClientId(), clID, MQTTSNstrlen(*clientId)) == 0)
{
_mutex.unlock();
return client;
@@ -339,14 +331,12 @@ Client* ClientList::getClient(MQTTSNString* clientId)
return 0;
}
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
int type)
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, int type)
{
return createClient(addr, clientId, false, false, type);
}
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
bool unstableLine, bool secure, int type)
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, bool unstableLine, bool secure, int type)
{
Client* client = getClient(addr);
if (client)
@@ -355,16 +345,16 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
}
/* acquire a free client */
client = _clientsPool->getClient();
client = _clientsPool->getClient();
if (!client)
{
WRITELOG("%s%sMax number of Clients%s\n", currentDateTime(),
ERRMSG_HEADER, ERRMSG_FOOTER);
return nullptr;
WRITELOG("%s%sMax number of Clients%s\n", currentDateTime(),
ERRMSG_HEADER, ERRMSG_FOOTER);
return nullptr;
}
client->disconnected();
client->disconnected();
if (addr)
{
client->setClientAddress(addr);
@@ -410,19 +400,17 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
return client;
}
Client* ClientList::createPredefinedTopic(MQTTSNString* clientId,
string topicName, uint16_t topicId, bool aggregate)
Client* ClientList::createPredefinedTopic(MQTTSNString* clientId, string topicName, uint16_t topicId, bool aggregate)
{
if (topicId == 0)
{
WRITELOG("Invalid TopicId. Predefined Topic %s, TopicId is 0. \n",
topicName.c_str());
WRITELOG("Invalid TopicId. Predefined Topic %s, TopicId is 0. \n", topicName.c_str());
return nullptr;
}
if (strcmp(clientId->cstring, common_topic) == 0)
{
_gateway->getTopics()->add((const char*) topicName.c_str(), topicId);
_gateway->getTopics()->add((const char*) topicName.c_str(), topicId);
return nullptr;
}
else
@@ -490,63 +478,62 @@ bool ClientList::isAuthorized()
ClientsPool::ClientsPool()
{
_clientCnt = 0;
_firstClient = nullptr;
_endClient = nullptr;
_clientCnt = 0;
_firstClient = nullptr;
_endClient = nullptr;
}
ClientsPool::~ClientsPool()
{
Client* cl = _firstClient;
Client* ncl;
Client* cl = _firstClient;
Client* ncl;
while (cl != nullptr)
{
ncl = cl->_nextClient;
delete cl;
cl = ncl;
};
while (cl != nullptr)
{
ncl = cl->_nextClient;
delete cl;
cl = ncl;
};
}
void ClientsPool::allocate(int maxClients)
{
Client* cl = nullptr;
Client* cl = nullptr;
_firstClient = new Client();
_firstClient = new Client();
for (int i = 0; i < maxClients; i++)
{
if ((cl = new Client()) == nullptr)
{
throw Exception(
"ClientsPool::Can't allocate max number of clients\n", 0);
}
cl->_nextClient = _firstClient;
_firstClient = cl;
_clientCnt++;
}
for (int i = 0; i < maxClients; i++)
{
if ((cl = new Client()) == nullptr)
{
throw Exception("ClientsPool::Can't allocate max number of clients\n", 0);
}
cl->_nextClient = _firstClient;
_firstClient = cl;
_clientCnt++;
}
}
Client* ClientsPool::getClient(void)
{
while (_firstClient != nullptr)
{
Client* cl = _firstClient;
_firstClient = cl->_nextClient;
cl->_nextClient = nullptr;
_clientCnt--;
return cl;
}
return nullptr;
while (_firstClient != nullptr)
{
Client* cl = _firstClient;
_firstClient = cl->_nextClient;
cl->_nextClient = nullptr;
_clientCnt--;
return cl;
}
return nullptr;
}
void ClientsPool::setClient(Client* client)
{
if (client)
{
client->_nextClient = _firstClient;
_firstClient = client;
_clientCnt++;
}
if (client)
{
client->_nextClient = _firstClient;
_firstClient = client;
_clientCnt++;
}
}