mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-13 15:36:51 +01:00
Update: Add Pre-defined-Topic
Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -16,16 +16,17 @@
|
||||
*
|
||||
* Supported functions.
|
||||
*
|
||||
* void PUBLISH ( const char* topicName, uint8_t* payload,
|
||||
* uint16_t len, uint8_t qos, bool retain = false );
|
||||
* void PUBLISH ( const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
|
||||
*
|
||||
* void PUBLISH ( uint16_t topicId, uint8_t* payload,
|
||||
* uint16_t len, uint8_t qos, bool retain = false );
|
||||
* void PUBLISH ( uint16_t topicId, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
|
||||
*
|
||||
* void SUBSCRIBE ( const char* topicName, TopicCallback onPublish,
|
||||
* uint8_t qos );
|
||||
* void SUBSCRIBE ( const char* topicName, TopicCallback onPublish, uint8_t qos );
|
||||
*
|
||||
* void UNSUBSCRIBE( const char* topicName );
|
||||
* void SUBSCRIBE ( uint16_t topicId, TopicCallback onPublish, uint8_t qos );
|
||||
*
|
||||
* void UNSUBSCRIBE ( const char* topicName );
|
||||
*
|
||||
* void UNSUBSCRIBE ( uint16_t topicId );
|
||||
*
|
||||
* void DISCONNECT ( uint16_t sleepInSecs );
|
||||
*
|
||||
|
||||
@@ -16,16 +16,17 @@
|
||||
*
|
||||
* Supported functions.
|
||||
*
|
||||
* void PUBLISH ( const char* topicName, uint8_t* payload,
|
||||
* uint16_t len, uint8_t qos, bool retain = false );
|
||||
* void PUBLISH ( const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
|
||||
*
|
||||
* void PUBLISH ( uint16_t topicId, uint8_t* payload,
|
||||
* uint16_t len, uint8_t qos, bool retain = false );
|
||||
* void PUBLISH ( uint16_t topicId, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
|
||||
*
|
||||
* void SUBSCRIBE ( const char* topicName, TopicCallback onPublish,
|
||||
* uint8_t qos );
|
||||
* void SUBSCRIBE ( const char* topicName, TopicCallback onPublish, uint8_t qos );
|
||||
*
|
||||
* void UNSUBSCRIBE( const char* topicName );
|
||||
* void SUBSCRIBE ( uint16_t topicId, TopicCallback onPublish, uint8_t qos );
|
||||
*
|
||||
* void UNSUBSCRIBE ( const char* topicName );
|
||||
*
|
||||
* void UNSUBSCRIBE ( uint16_t topicId );
|
||||
*
|
||||
* void DISCONNECT ( uint16_t sleepInSecs );
|
||||
*
|
||||
@@ -112,11 +113,13 @@ int on_Topic03(uint8_t* pload, uint16_t ploadlen)
|
||||
* A Link list of Callback routines and Topics
|
||||
*------------------------------------------------------*/
|
||||
|
||||
SUBSCRIBE_LIST = {// e.g. SUB(topic, callback, QoS),
|
||||
//SUB(topic1, on_Topic01, 1),
|
||||
//SUB(topic4, on_Topic03, 1),
|
||||
END_OF_SUBSCRIBE_LIST
|
||||
};
|
||||
SUBSCRIBE_LIST = {// e.g. SUB(TopicType, topicName, TopicId, callback, QoSx),
|
||||
|
||||
// SUB(MQTTSN_TOPIC_TYPE_NORMAL, topic1, 0, on_Topic01, QoS1),
|
||||
// SUB(MQTTSN_TOPIC_TYPE_NORMAL, topic2, 0, on_Topic02, QoS1),
|
||||
END_OF_SUBSCRIBE_LIST
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------
|
||||
|
||||
@@ -16,16 +16,17 @@
|
||||
*
|
||||
* Supported functions.
|
||||
*
|
||||
* void PUBLISH ( const char* topicName, uint8_t* payload,
|
||||
* uint16_t len, uint8_t qos, bool retain = false );
|
||||
* void PUBLISH ( const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
|
||||
*
|
||||
* void PUBLISH ( uint16_t topicId, uint8_t* payload,
|
||||
* uint16_t len, uint8_t qos, bool retain = false );
|
||||
* void PUBLISH ( uint16_t topicId, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
|
||||
*
|
||||
* void SUBSCRIBE ( const char* topicName, TopicCallback onPublish,
|
||||
* uint8_t qos );
|
||||
* void SUBSCRIBE ( const char* topicName, TopicCallback onPublish, uint8_t qos );
|
||||
*
|
||||
* void UNSUBSCRIBE( const char* topicName );
|
||||
* void SUBSCRIBE ( uint16_t topicId, TopicCallback onPublish, uint8_t qos );
|
||||
*
|
||||
* void UNSUBSCRIBE ( const char* topicName );
|
||||
*
|
||||
* void UNSUBSCRIBE ( uint16_t topicId );
|
||||
*
|
||||
* void DISCONNECT ( uint16_t sleepInSecs );
|
||||
*
|
||||
@@ -76,6 +77,8 @@ MQTTSNCONF = {
|
||||
const char* topic1 = "ty4tw/topic1";
|
||||
const char* topic2 = "ty4tw/topic2";
|
||||
const char* topic3 = "ty4tw/topic3";
|
||||
const char* topic4 = "ty4tw/topic4";
|
||||
const char* topic5 = "ty4tw/topic5";
|
||||
|
||||
|
||||
/*------------------------------------------------------
|
||||
@@ -100,7 +103,7 @@ int on_Topic02(uint8_t* pload, uint16_t ploadlen)
|
||||
|
||||
int on_Topic03(uint8_t* pload, uint16_t ploadlen)
|
||||
{
|
||||
DISPLAY("\n\nNew callback recv Topic2\n");
|
||||
DISPLAY("\n\nNew callback recv Topic3\n");
|
||||
pload[ploadlen-1]= 0; // set null terminator
|
||||
DISPLAY("Payload -->%s <--\n\n",pload);
|
||||
return 0;
|
||||
@@ -110,8 +113,9 @@ int on_Topic03(uint8_t* pload, uint16_t ploadlen)
|
||||
* A Link list of Callback routines and Topics
|
||||
*------------------------------------------------------*/
|
||||
|
||||
SUBSCRIBE_LIST = {// e.g. SUB(topic, callback, QoS),
|
||||
SUB(topic1, on_Topic01, 1),
|
||||
SUBSCRIBE_LIST = {// e.g. SUB(TopicType, topicName, TopicId, callback, QoSx),
|
||||
SUB(MQTTSN_TOPIC_TYPE_NORMAL, topic1, 0, on_Topic01, QoS1),
|
||||
SUB(MQTTSN_TOPIC_TYPE_NORMAL, topic2, 0, on_Topic02, QoS1),
|
||||
END_OF_SUBSCRIBE_LIST
|
||||
};
|
||||
|
||||
@@ -119,29 +123,32 @@ SUBSCRIBE_LIST = {// e.g. SUB(topic, callback, QoS),
|
||||
/*------------------------------------------------------
|
||||
* Test functions
|
||||
*------------------------------------------------------*/
|
||||
void subscribePredefTopic1(void)
|
||||
{
|
||||
SUBSCRIBE(1, on_Topic03, QoS1);
|
||||
}
|
||||
|
||||
void publishTopic1(void)
|
||||
{
|
||||
char payload[300];
|
||||
sprintf(payload, "publish \"ty4tw/Topic1\" \n");
|
||||
uint8_t qos = 0;
|
||||
PUBLISH(topic1,(uint8_t*)payload, strlen(payload), qos);
|
||||
PUBLISH(topic1,(uint8_t*)payload, strlen(payload), QoS0);
|
||||
}
|
||||
|
||||
void subscribeTopic2(void)
|
||||
{
|
||||
uint8_t qos = 1;
|
||||
SUBSCRIBE(topic2, on_Topic02, qos);
|
||||
SUBSCRIBE(10, on_Topic02, QoS1);
|
||||
}
|
||||
|
||||
void publishTopic2(void)
|
||||
{
|
||||
char payload[300];
|
||||
sprintf(payload, "publish \"ty4tw/topic2\" \n");
|
||||
uint8_t qos = 0;
|
||||
PUBLISH(topic2,(uint8_t*)payload, strlen(payload), qos);
|
||||
PUBLISH(topic2,(uint8_t*)payload, strlen(payload), QoS1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void unsubscribe(void)
|
||||
{
|
||||
UNSUBSCRIBE(topic2);
|
||||
@@ -149,8 +156,7 @@ void unsubscribe(void)
|
||||
|
||||
void subscribechangeCallback(void)
|
||||
{
|
||||
uint8_t qos = 1;
|
||||
SUBSCRIBE(topic2, on_Topic03, qos);
|
||||
SUBSCRIBE(topic2, on_Topic02, QoS1);
|
||||
}
|
||||
|
||||
void test3(void)
|
||||
@@ -178,6 +184,7 @@ void asleep(void)
|
||||
*------------------------------------------------------*/
|
||||
|
||||
TEST_LIST = {// e.g. TEST( Label, Test),
|
||||
TEST("Step0:Subscribe predef topic1", subscribePredefTopic1),
|
||||
TEST("Step1:Publish topic1", publishTopic1),
|
||||
TEST("Step2:Publish topic2", publishTopic2),
|
||||
TEST("Step3:Subscribe topic2", subscribeTopic2),
|
||||
|
||||
@@ -183,9 +183,9 @@ void LMqttsnClient::subscribe(const char* topicName, TopicCallback onPublish, ui
|
||||
_subMgr.subscribe(topicName, onPublish, qos);
|
||||
}
|
||||
|
||||
void LMqttsnClient::subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos, uint8_t topicType)
|
||||
void LMqttsnClient::subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos)
|
||||
{
|
||||
_subMgr.subscribe(topicId, onPublish, qos, topicType);
|
||||
_subMgr.subscribe(topicId, onPublish, qos);
|
||||
}
|
||||
|
||||
void LMqttsnClient::unsubscribe(const char* topicName)
|
||||
@@ -193,6 +193,11 @@ void LMqttsnClient::unsubscribe(const char* topicName)
|
||||
_subMgr.unsubscribe(topicName);
|
||||
}
|
||||
|
||||
void LMqttsnClient::unsubscribe(const uint16_t topicId)
|
||||
{
|
||||
_subMgr.unsubscribe(topicId);
|
||||
}
|
||||
|
||||
void LMqttsnClient::disconnect(uint16_t sleepInSecs)
|
||||
{
|
||||
_gwProxy.disconnect(sleepInSecs);
|
||||
|
||||
@@ -34,7 +34,9 @@ namespace linuxAsyncClient {
|
||||
|
||||
struct OnPublishList
|
||||
{
|
||||
MQTTSN_topicTypes type;
|
||||
const char* topic;
|
||||
uint16_t id;
|
||||
int (*pubCallback)(uint8_t* payload, uint16_t payloadlen);
|
||||
uint8_t qos;
|
||||
};
|
||||
@@ -52,8 +54,9 @@ public:
|
||||
void publish(uint16_t topicId, Payload* payload, uint8_t qos, bool retain = false);
|
||||
void publish(uint16_t topicId, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false);
|
||||
void subscribe(const char* topicName, TopicCallback onPublish, uint8_t qos);
|
||||
void subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos, uint8_t topicType);
|
||||
void subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos);
|
||||
void unsubscribe(const char* topicName);
|
||||
void unsubscribe(const uint16_t topicId);
|
||||
void disconnect(uint16_t sleepInSecs);
|
||||
void initialize(LUdpConfig netconf, LMqttsnConfig mqconf);
|
||||
void run(void);
|
||||
|
||||
@@ -73,6 +73,15 @@ struct LUdpConfig{
|
||||
uint16_t uPortNo;
|
||||
};
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MQTTSN_TOPIC_TYPE_NORMAL,
|
||||
MQTTSN_TOPIC_TYPE_PREDEFINED,
|
||||
MQTTSN_TOPIC_TYPE_SHORT
|
||||
} MQTTSN_topicTypes;
|
||||
|
||||
|
||||
/*======================================
|
||||
MACROs for Application
|
||||
=======================================*/
|
||||
@@ -93,7 +102,7 @@ struct LUdpConfig{
|
||||
#define END_OF_TEST_LIST {0, 0, 0}
|
||||
#define SUBSCRIBE_LIST OnPublishList theOnPublishList[]
|
||||
#define SUB(...) {__VA_ARGS__}
|
||||
#define END_OF_SUBSCRIBE_LIST {0,0,0}
|
||||
#define END_OF_SUBSCRIBE_LIST {MQTTSN_TOPIC_TYPE_NORMAL,0,0,0, 0}
|
||||
#define UDPCONF LUdpConfig theNetcon
|
||||
#define MQTTSNCONF LMqttsnConfig theMqcon
|
||||
#ifdef CLIENT_MODE
|
||||
@@ -129,6 +138,9 @@ struct LUdpConfig{
|
||||
/*======================================
|
||||
MQTT-SN Defines
|
||||
========================================*/
|
||||
#define QoS0 0
|
||||
#define QoS1 1
|
||||
#define QoS2 2
|
||||
#define MQTTSN_TYPE_ADVERTISE 0x00
|
||||
#define MQTTSN_TYPE_SEARCHGW 0x01
|
||||
#define MQTTSN_TYPE_GWINFO 0x02
|
||||
@@ -157,10 +169,7 @@ struct LUdpConfig{
|
||||
#define MQTTSN_TYPE_WILLMSGUPD 0x1C
|
||||
#define MQTTSN_TYPE_WILLMSGRESP 0x1D
|
||||
|
||||
#define MQTTSN_TOPIC_TYPE_NORMAL 0x00
|
||||
#define MQTTSN_TOPIC_TYPE_PREDEFINED 0x01
|
||||
#define MQTTSN_TOPIC_TYPE_SHORT 0x02
|
||||
#define MQTTSN_TOPIC_TYPE 0x03
|
||||
#define MQTTSN_TOPIC_TYPE 0x03
|
||||
|
||||
#define MQTTSN_FLAG_DUP 0x80
|
||||
#define MQTTSN_FLAG_QOS_0 0x0
|
||||
@@ -179,14 +188,10 @@ struct LUdpConfig{
|
||||
#define MQTTSN_RC_REJECTED_INVALID_TOPIC_ID 0x02
|
||||
#define MQTTSN_RC_REJECTED_NOT_SUPPORTED 0x03
|
||||
|
||||
#define PREDEFINEDID_OTA_REQ (0x0ff0)
|
||||
#define PREDEFINEDID_OTA_READY (0x0ff1)
|
||||
#define PREDEFINEDID_OTA_NO_CLIENT (0x0ff2)
|
||||
|
||||
/*=================================
|
||||
* Starting prompt
|
||||
==================================*/
|
||||
#define TESTER_VERSION " * Version: 1.0.0"
|
||||
#define TESTER_VERSION " * Version: 2.0.0"
|
||||
|
||||
#define PAHO_COPYRIGHT0 " * MQTT-SN Gateway Tester"
|
||||
#define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse"
|
||||
|
||||
@@ -66,25 +66,22 @@ void LPublishManager::publish(const char* topicName, Payload* payload, uint8_t q
|
||||
publish(topicName, payload->getRowData(), payload->getLen(), qos, retain);
|
||||
}
|
||||
|
||||
|
||||
void LPublishManager::publish(const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, bool retain)
|
||||
{
|
||||
uint8_t topicType = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
if ( strlen(topicName) < 2 )
|
||||
{
|
||||
topicType = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
}
|
||||
publish(topicName, payload, len, qos, topicType, retain);
|
||||
}
|
||||
|
||||
void LPublishManager::publish(const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, uint8_t topicType, bool retain)
|
||||
{
|
||||
uint16_t msgId = 0;
|
||||
uint8_t topicType = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
if ( strlen(topicName) > 2 )
|
||||
{
|
||||
topicType = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
}
|
||||
|
||||
if ( qos > 0 )
|
||||
{
|
||||
msgId = theClient->getGwProxy()->getNextMsgId();
|
||||
}
|
||||
|
||||
PubElement* elm = add(topicName, 0, payload, len, qos, retain, msgId, topicType);
|
||||
|
||||
if (elm->status == TOPICID_IS_READY)
|
||||
{
|
||||
sendPublish(elm);
|
||||
@@ -286,7 +283,7 @@ void LPublishManager::published(uint8_t* msg, uint16_t msglen)
|
||||
}
|
||||
|
||||
_publishedFlg = NEG_TASK_INDEX;
|
||||
theClient->getTopicTable()->execCallback(topicId, msg + 6, msglen - 6, msg[1] & 0x03);
|
||||
theClient->getTopicTable()->execCallback(topicId, msg + 6, msglen - 6, (MQTTSN_topicTypes)(msg[1] & MQTTSN_TOPIC_TYPE));
|
||||
_publishedFlg = SAVE_TASK_INDEX;
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,6 @@ public:
|
||||
~LPublishManager();
|
||||
void publish(const char* topicName, Payload* payload, uint8_t qos, bool retain = false);
|
||||
void publish(const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false);
|
||||
void publish(const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, uint8_t topicType, bool retain = false);
|
||||
void publish(uint16_t topicId, Payload* payload, uint8_t qos, bool retain = false);
|
||||
void publish(uint16_t topicId, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false);
|
||||
void responce(const uint8_t* msg, uint16_t msglen);
|
||||
|
||||
@@ -190,13 +190,13 @@ void LRegisterManager::registerTopic(char* topicName)
|
||||
void LRegisterManager::responceRegAck(uint16_t msgId, uint16_t topicId)
|
||||
{
|
||||
const char* topicName = getTopic(msgId);
|
||||
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
if (topicName)
|
||||
{
|
||||
uint8_t topicType = strlen((char*) topicName) > 2 ? MQTTSN_TOPIC_TYPE_NORMAL : MQTTSN_TOPIC_TYPE_SHORT;
|
||||
theClient->getGwProxy()->getTopicTable()->setTopicId((char*) topicName, topicId, topicType); // Add Topic to TopicTable
|
||||
theClient->getGwProxy()->getTopicTable()->setTopicId((char*) topicName, topicId, type); // Add Topic to TopicTable
|
||||
RegQueElement* elm = getElement(msgId);
|
||||
remove(elm);
|
||||
theClient->getPublishManager()->sendSuspend((char*) topicName, topicId, topicType);
|
||||
theClient->getPublishManager()->sendSuspend((char*) topicName, topicId, type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ void LRegisterManager::responceRegister(uint8_t* msg, uint16_t msglen)
|
||||
{
|
||||
TopicCallback callback = tp->getCallback();
|
||||
void* topicName = calloc(strlen((char*) msg + 5) + 1, sizeof(char));
|
||||
theClient->getGwProxy()->getTopicTable()->add((char*) topicName, 0, MQTTSN_TOPIC_TYPE_NORMAL, callback, 1);
|
||||
theClient->getGwProxy()->getTopicTable()->add((char*) topicName, MQTTSN_TOPIC_TYPE_NORMAL, 0, callback, 1);
|
||||
regack[6] = MQTTSN_RC_ACCEPTED;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -65,7 +65,14 @@ void LSubscribeManager::onConnect(void)
|
||||
{
|
||||
for (uint8_t i = 0; theOnPublishList[i].topic != 0; i++)
|
||||
{
|
||||
subscribe(theOnPublishList[i].topic, theOnPublishList[i].pubCallback, theOnPublishList[i].qos);
|
||||
if ( theOnPublishList[i].type == MQTTSN_TOPIC_TYPE_PREDEFINED)
|
||||
{
|
||||
subscribe(theOnPublishList[i].id, theOnPublishList[i].pubCallback, theOnPublishList[i].qos);
|
||||
}
|
||||
else
|
||||
{
|
||||
subscribe(theOnPublishList[i].topic, theOnPublishList[i].pubCallback, theOnPublishList[i].qos);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -149,30 +156,43 @@ void LSubscribeManager::send(SubElement* elm)
|
||||
|
||||
void LSubscribeManager::subscribe(const char* topicName, TopicCallback onPublish, uint8_t qos)
|
||||
{
|
||||
uint8_t topicType = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
if ( strlen(topicName) <= 2)
|
||||
{
|
||||
topicType = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
}
|
||||
SubElement* elm = add(MQTTSN_TYPE_SUBSCRIBE, topicName, 0, topicType, qos, onPublish);
|
||||
send(elm);
|
||||
MQTTSN_topicTypes topicType;
|
||||
if ( strlen(topicName) > 2 )
|
||||
{
|
||||
topicType = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
topicType = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
}
|
||||
SubElement* elm = add(MQTTSN_TYPE_SUBSCRIBE, topicName, topicType, 0, qos, onPublish);
|
||||
send(elm);
|
||||
}
|
||||
|
||||
void LSubscribeManager::subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos, uint8_t topicType)
|
||||
void LSubscribeManager::subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos)
|
||||
{
|
||||
SubElement* elm = add(MQTTSN_TYPE_SUBSCRIBE, 0, topicId, topicType, qos, onPublish);
|
||||
SubElement* elm = add(MQTTSN_TYPE_SUBSCRIBE, 0, MQTTSN_TOPIC_TYPE_PREDEFINED, topicId, qos, onPublish);
|
||||
send(elm);
|
||||
}
|
||||
|
||||
void LSubscribeManager::unsubscribe(const char* topicName)
|
||||
{
|
||||
SubElement* elm = add(MQTTSN_TYPE_UNSUBSCRIBE, topicName, 0, MQTTSN_TOPIC_TYPE_NORMAL, 0, 0);
|
||||
MQTTSN_topicTypes topicType;
|
||||
if ( strlen(topicName) > 2 )
|
||||
{
|
||||
topicType = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
topicType = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
}
|
||||
SubElement* elm = add(MQTTSN_TYPE_UNSUBSCRIBE, topicName, topicType, 0, 0, 0);
|
||||
send(elm);
|
||||
}
|
||||
|
||||
void LSubscribeManager::unsubscribe(uint16_t topicId, uint8_t topicType)
|
||||
void LSubscribeManager::unsubscribe( uint16_t topicId)
|
||||
{
|
||||
SubElement* elm = add(MQTTSN_TYPE_UNSUBSCRIBE, 0, topicId, topicType, 0, 0);
|
||||
SubElement* elm = add(MQTTSN_TYPE_UNSUBSCRIBE, 0, MQTTSN_TOPIC_TYPE_PREDEFINED, topicId, 0, 0);
|
||||
send(elm);
|
||||
}
|
||||
|
||||
@@ -214,20 +234,19 @@ void LSubscribeManager::responce(const uint8_t* msg)
|
||||
uint16_t msgId = getUint16(msg + 4);
|
||||
uint8_t rc = msg[6];
|
||||
|
||||
LTopicTable* tt = theClient->getGwProxy()->getTopicTable();
|
||||
SubElement* elm = getElement(msgId);
|
||||
if (elm)
|
||||
{
|
||||
if ( rc == MQTTSN_RC_ACCEPTED )
|
||||
{
|
||||
tt->add((char*) elm->topicName, topicId, elm->topicType, elm->callback);
|
||||
theClient->getGwProxy()->getTopicTable()->add((char*) elm->topicName, elm->topicType, topicId, elm->callback);
|
||||
getElement(msgId)->done = SUB_DONE;
|
||||
DISPLAY("\033[0m\033[0;32m Topic \"%s\" Id : %d was Subscribed. \033[0m\033[0;37m\n\n", getElement(msgId)->topicName, topicId);
|
||||
}
|
||||
else
|
||||
{
|
||||
remove(elm);
|
||||
DISPLAY("\033[0m\033[0;31m SUBACK Invalid messageId. %s\033[0m\033[0;37m\n\n", getElement(msgId)->topicName);
|
||||
remove(elm);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,22 +256,20 @@ void LSubscribeManager::responce(const uint8_t* msg)
|
||||
SubElement* elm = getElement(msgId);
|
||||
if (elm)
|
||||
{
|
||||
LTopicTable* tt = theClient->getGwProxy()->getTopicTable();
|
||||
tt->setCallback(elm->topicName, 0);
|
||||
//theClient->getGwProxy()->getTopicTable()->setCallback(elm->topicName, 0);
|
||||
DISPLAY("\033[0m\033[0;32m Topic \"%s\" was Unsubscribed. \033[0m\033[0;37m\n\n", getElement(msgId)->topicName);
|
||||
remove(getElement(msgId));
|
||||
remove(elm);
|
||||
}
|
||||
else
|
||||
{
|
||||
DISPLAY("\033[0m\033[0;31m UNSUBACK Invalid messageId. %s\033[0m\033[0;37m\n\n", getElement(msgId)->topicName);
|
||||
remove(getElement(msgId));
|
||||
DISPLAY("\033[0m\033[0;31m UNSUBACK Invalid messageId. \033[0m\033[0;37m\n\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* SubElement operations */
|
||||
|
||||
SubElement* LSubscribeManager::add(uint8_t msgType, const char* topicName, uint16_t topicId, uint8_t topicType,
|
||||
SubElement* LSubscribeManager::add(uint8_t msgType, const char* topicName, MQTTSN_topicTypes topicType, uint16_t topicId,
|
||||
uint8_t qos, TopicCallback callback)
|
||||
{
|
||||
SubElement* elm = 0;
|
||||
@@ -358,7 +375,7 @@ SubElement* LSubscribeManager::getElement(const char* topicName, uint8_t msgType
|
||||
SubElement* elm = _first;
|
||||
while (elm)
|
||||
{
|
||||
if (strcmp(elm->topicName, topicName) == 0 && elm->msgType == msgType)
|
||||
if ( elm->msgType == msgType && strncmp(elm->topicName, topicName, strlen(topicName)) == 0 )
|
||||
{
|
||||
return elm;
|
||||
}
|
||||
@@ -370,7 +387,7 @@ SubElement* LSubscribeManager::getElement(const char* topicName, uint8_t msgType
|
||||
return 0;
|
||||
}
|
||||
|
||||
SubElement* LSubscribeManager::getElement(uint16_t topicId, uint8_t topicType)
|
||||
SubElement* LSubscribeManager::getElement(uint16_t topicId, MQTTSN_topicTypes topicType)
|
||||
{
|
||||
SubElement* elm = _first;
|
||||
while (elm)
|
||||
|
||||
@@ -37,7 +37,7 @@ typedef struct SubElement{
|
||||
time_t sendUTC;
|
||||
uint16_t topicId;
|
||||
uint8_t msgType;
|
||||
uint8_t topicType;
|
||||
MQTTSN_topicTypes topicType;
|
||||
uint8_t qos;
|
||||
|
||||
int retryCount;
|
||||
@@ -56,9 +56,9 @@ public:
|
||||
~LSubscribeManager();
|
||||
void onConnect(void);
|
||||
void subscribe(const char* topicName, TopicCallback onPublish, uint8_t qos);
|
||||
void subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos, uint8_t topicType);
|
||||
void subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos);
|
||||
void unsubscribe(const char* topicName);
|
||||
void unsubscribe(uint16_t topicId, uint8_t topicType);
|
||||
void unsubscribe(uint16_t topicId);
|
||||
void responce(const uint8_t* msg);
|
||||
void checkTimeout(void);
|
||||
bool isDone(void);
|
||||
@@ -66,9 +66,9 @@ private:
|
||||
void send(SubElement* elm);
|
||||
SubElement* getFirstElement(void);
|
||||
SubElement* getElement(uint16_t msgId);
|
||||
SubElement* getElement(uint16_t topicId, uint8_t topicType);
|
||||
SubElement* getElement(uint16_t topicId, MQTTSN_topicTypes topicType);
|
||||
SubElement* getElement(const char* topicName, uint8_t msgType);
|
||||
SubElement* add(uint8_t msgType, const char* topicName, uint16_t topicId, uint8_t topicType, uint8_t qos, TopicCallback callback);
|
||||
SubElement* add(uint8_t msgType, const char* topicName, MQTTSN_topicTypes topicType, uint16_t topicId, uint8_t qos, TopicCallback callback);
|
||||
void remove(SubElement* elm);
|
||||
SubElement* _first;
|
||||
SubElement* _last;
|
||||
|
||||
@@ -131,7 +131,7 @@ LTopic* LTopicTable::getTopic(const char* topic){
|
||||
return 0;
|
||||
}
|
||||
|
||||
LTopic* LTopicTable::getTopic(uint16_t topicId, uint8_t topicType){
|
||||
LTopic* LTopicTable::getTopic(uint16_t topicId, MQTTSN_topicTypes topicType){
|
||||
LTopic* p = _first;
|
||||
while(p){
|
||||
if (p->_topicId == topicId && p->_topicType == topicType){
|
||||
@@ -156,12 +156,12 @@ char* LTopicTable::getTopicName(LTopic* topic){
|
||||
}
|
||||
|
||||
|
||||
void LTopicTable::setTopicId(const char* topic, uint16_t id, uint8_t type){
|
||||
void LTopicTable::setTopicId(const char* topic, uint16_t id, MQTTSN_topicTypes type){
|
||||
LTopic* tp = getTopic(topic);
|
||||
if (tp){
|
||||
tp->_topicId = id;
|
||||
}else{
|
||||
add(topic, id, type, 0);
|
||||
add(topic, type, id, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ bool LTopicTable::setCallback(const char* topic, TopicCallback callback){
|
||||
}
|
||||
|
||||
|
||||
bool LTopicTable::setCallback(uint16_t topicId, uint8_t topicType, TopicCallback callback){
|
||||
bool LTopicTable::setCallback(uint16_t topicId, MQTTSN_topicTypes topicType, TopicCallback callback){
|
||||
LTopic* p = getTopic(topicId, topicType);
|
||||
if (p){
|
||||
p->_callback = callback;
|
||||
@@ -186,7 +186,7 @@ bool LTopicTable::setCallback(uint16_t topicId, uint8_t topicType, TopicCallback
|
||||
}
|
||||
|
||||
|
||||
int LTopicTable::execCallback(uint16_t topicId, uint8_t* payload, uint16_t payloadlen, uint8_t topicType){
|
||||
int LTopicTable::execCallback(uint16_t topicId, uint8_t* payload, uint16_t payloadlen, MQTTSN_topicTypes topicType){
|
||||
LTopic* p = getTopic(topicId, topicType);
|
||||
if (p){;
|
||||
return p->execCallback(payload, payloadlen);
|
||||
@@ -195,7 +195,7 @@ int LTopicTable::execCallback(uint16_t topicId, uint8_t* payload, uint16_t payl
|
||||
}
|
||||
|
||||
|
||||
LTopic* LTopicTable::add(const char* topicName, uint16_t id, uint8_t type, TopicCallback callback, uint8_t alocFlg)
|
||||
LTopic* LTopicTable::add(const char* topicName, MQTTSN_topicTypes type, uint16_t id, TopicCallback callback, uint8_t alocFlg)
|
||||
{
|
||||
LTopic* elm;
|
||||
|
||||
@@ -234,9 +234,9 @@ exit:
|
||||
return elm;
|
||||
}
|
||||
|
||||
void LTopicTable::remove(uint16_t topicId)
|
||||
void LTopicTable::remove(uint16_t topicId, MQTTSN_topicTypes type)
|
||||
{
|
||||
LTopic* elm = getTopic(topicId);
|
||||
LTopic* elm = getTopic(topicId, type);
|
||||
|
||||
if (elm){
|
||||
if (elm->_prev == 0)
|
||||
|
||||
@@ -42,7 +42,7 @@ public:
|
||||
TopicCallback getCallback(void);
|
||||
private:
|
||||
uint16_t _topicId;
|
||||
uint8_t _topicType;
|
||||
MQTTSN_topicTypes _topicType;
|
||||
char* _topicStr;
|
||||
TopicCallback _callback;
|
||||
uint8_t _malocFlg;
|
||||
@@ -60,16 +60,16 @@ public:
|
||||
uint16_t getTopicId(const char* topic);
|
||||
char* getTopicName(LTopic* topic);
|
||||
LTopic* getTopic(const char* topic);
|
||||
LTopic* getTopic(uint16_t topicId, uint8_t topicType = MQTTSN_TOPIC_TYPE_NORMAL);
|
||||
void setTopicId(const char* topic, uint16_t id, uint8_t topicType);
|
||||
LTopic* getTopic(uint16_t topicId, MQTTSN_topicTypes topicType);
|
||||
void setTopicId(const char* topic, uint16_t id, MQTTSN_topicTypes topicType);
|
||||
bool setCallback(const char* topic, TopicCallback callback);
|
||||
bool setCallback(uint16_t topicId, uint8_t type, TopicCallback callback);
|
||||
int execCallback(uint16_t topicId, uint8_t* payload, uint16_t payloadlen, uint8_t topicType = MQTTSN_TOPIC_TYPE_NORMAL);
|
||||
LTopic* add(const char* topic, uint16_t id = 0, uint8_t type = MQTTSN_TOPIC_TYPE_NORMAL, TopicCallback callback = 0, uint8_t alocFlg = 0);
|
||||
LTopic* add(uint16_t topicId, uint16_t id, uint8_t type, TopicCallback callback, uint8_t alocFlg);
|
||||
bool setCallback(uint16_t topicId, MQTTSN_topicTypes type, TopicCallback callback);
|
||||
int execCallback(uint16_t topicId, uint8_t* payload, uint16_t payloadlen, MQTTSN_topicTypes topicType);
|
||||
LTopic* add(const char* topic, MQTTSN_topicTypes type, uint16_t id = 0, TopicCallback callback = 0, uint8_t alocFlg = 0);
|
||||
//LTopic* add(uint16_t topicId, uint16_t id, MQTTSN_topicTypes type, TopicCallback callback, uint8_t alocFlg);
|
||||
LTopic* match(const char* topic);
|
||||
void clearTopic(void);
|
||||
void remove(uint16_t topicId);
|
||||
void remove(uint16_t topicId, MQTTSN_topicTypes type);
|
||||
|
||||
private:
|
||||
LTopic* _first;
|
||||
|
||||
Reference in New Issue
Block a user