uint16_t Topics::getTopicId(MQString* topic){ Topic *p = getTopic(topic); if ( p != NULL) { return p->getTopicId(); } return 0; }
/*------------------------------------------------------- Upstream MQTTSnSubscribe -------------------------------------------------------*/ void GatewayControlTask::handleSnSubscribe(Event* ev, ClientNode* clnode, MQTTSnMessage* msg){ printf(FORMAT2, currentDateTime(), "SUBSCRIBE", LEFTARROW, clnode->getNodeId()->c_str(), msgPrint(msg)); MQTTSnSubscribe* sSubscribe = new MQTTSnSubscribe(); MQTTSubscribe* subscribe = new MQTTSubscribe(); sSubscribe->absorb(msg); uint8_t topicIdType = sSubscribe->getFlags() & 0x03; subscribe->setMessageId(sSubscribe->getMsgId()); if(sSubscribe->getFlags() & MQTTSN_FLAG_DUP ){ subscribe->setDup(); } if(sSubscribe->getFlags() & MQTTSN_FLAG_RETAIN){ subscribe->setRetain(); } subscribe->setQos(sSubscribe->getQos()); if(topicIdType != MQTTSN_FLAG_TOPICID_TYPE_RESV){ if(topicIdType == MQTTSN_FLAG_TOPICID_TYPE_PREDEFINED){ /*----- Predefined TopicId ------*/ MQTTSnSubAck* sSuback = new MQTTSnSubAck(); if(sSubscribe->getMsgId()){ // MessageID sSuback->setQos(sSubscribe->getQos()); sSuback->setTopicId(sSubscribe->getTopicId()); sSuback->setMsgId(sSubscribe->getMsgId()); if(sSubscribe->getTopicId() == MQTTSN_TOPICID_PREDEFINED_TIME){ sSuback->setReturnCode(MQTT_RC_ACCEPTED); }else{ sSuback->setReturnCode(MQTT_RC_REFUSED_IDENTIFIER_REJECTED); } clnode->setClientSendMessage(sSuback); Event* evsuback = new Event(); evsuback->setClientSendEvent(clnode); printf(FORMAT1, currentDateTime(), "SUBACK", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(sSuback)); _res->getClientSendQue()->post(evsuback); } if(sSubscribe->getTopicId() == MQTTSN_TOPICID_PREDEFINED_TIME){ MQTTSnPublish* pub = new MQTTSnPublish(); pub->setTopicIdType(1); // pre-defined pub->setTopicId(MQTTSN_TOPICID_PREDEFINED_TIME); pub->setMsgId(clnode->getNextSnMsgId()); uint8_t buf[4]; uint32_t tm = time(0); setUint32(buf,tm); pub->setData(buf, 4); printf(GREEN_FORMAT1, currentDateTime(), "PUBLISH", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(pub)); clnode->setClientSendMessage(pub); Event *evpub = new Event(); evpub->setClientSendEvent(clnode); _res->getClientSendQue()->post(evpub); } delete subscribe; }else{ uint16_t tpId; Topic* tp = clnode->getTopics()->getTopic(sSubscribe->getTopicName()); if (tp){ tpId = tp->getTopicId(); }else{ tpId = clnode->getTopics()->createTopic(sSubscribe->getTopicName()); } subscribe->setTopic(sSubscribe->getTopicName(), sSubscribe->getQos()); if(sSubscribe->getMsgId()){ MQTTSnSubAck* sSuback = new MQTTSnSubAck(); sSuback->setMsgId(sSubscribe->getMsgId()); sSuback->setTopicId(tpId); clnode->setWaitedSubAck(sSuback); } clnode->setBrokerSendMessage(subscribe); Event* ev1 = new Event(); ev1->setBrokerSendEvent(clnode); _res->getBrokerSendQue()->post(ev1); delete sSubscribe; return; } }else{ /*-- Irregular TopicIdType --*/ if(sSubscribe->getMsgId()){ MQTTSnSubAck* sSuback = new MQTTSnSubAck(); sSuback->setMsgId(sSubscribe->getMsgId()); sSuback->setTopicId(sSubscribe->getTopicId()); sSuback->setReturnCode(MQTT_RC_REFUSED_IDENTIFIER_REJECTED); clnode->setClientSendMessage(sSuback); Event* evun = new Event(); evun->setClientSendEvent(clnode); printf(FORMAT1, currentDateTime(), "SUBACK", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(sSuback)); _res->getClientSendQue()->post(evun); // Send SUBACK to Client } delete subscribe; } delete sSubscribe; }