Esempio n. 1
0
int MQTTPublish(Client* c, const char* topicName, MQTTMessage* message)
{
    int rc = FAILURE;
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicName;

    if(c->suborder != subscribe_over)
    {
    	log_printf(LOG_ERROR, "[MQTTPublish] publish but subcribe not over\n");
    	goto exit;
    	//TODO: error
    }

    int len = 0;
    if (!c->isconnected)
        goto exit;

    if (message->qos == QOS1 || message->qos == QOS2)
        message->id = getNextPacketId(c);
    
    len = MQTTSerialize_publish(c->buf, c->buf_size, 0, message->qos, message->retained, message->id, 
              topic, (unsigned char*)message->payload, message->payloadlen);
    if (len <= 0)
        goto exit;
    if ((rc = sendPacket_ev(c, len)) != SUCCESS) // send the subscribe packet
        goto exit;
exit:
    return rc;
}
bool DataBus::AbstractClient::sendSetClientPropertyValueRequestPacket(const quint8 clientId,
                                                                      const quint8 propertyId,
                                                                      const Value &value)
{
    // Create packet
    Packet packet;

    if (SetClientPropertyValueRequestPacket::create(m_clientId,
                                                    clientId,
                                                    getNextPacketId(),
                                                    propertyId,
                                                    value,
                                                    &packet) == false)
    {
        // Error, failed to create packet
        return false;
    }

    // Send packet
    if (sendPacket(packet) == false)
    {
        // Error, failed to send packet
        return false;
    }

    // Success
    return true;
}
int  MQTTUnsubscribe (Client *c, const char *topicFilter)
{
    int         rc  = FAILURE;
    int         len = 0;
    Timer       timer;
    MQTTString  topic = MQTTString_initializer;

    topic.cstring = (char*) topicFilter;

    InitTimer (&timer);
    countdown_ms (&timer, c->command_timeout_ms);

    if (!c->isconnected)
        goto exit;

    if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
        goto exit;
    if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
        goto exit; // there was a problem

    if (waitfor(c, UNSUBACK, &timer) == UNSUBACK)
      {
        unsigned short mypacketid;  // should be the same as the packetid above
        if (MQTTDeserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1)
            rc = 0;
      }
     else rc = FAILURE;      // timed out - no UNSUBACK received

exit:
    return rc;
}
Esempio n. 4
0
int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter)
{   
    int rc = MQTT_FAILURE;
    Timer timer;    
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicFilter;
    int len = 0;

	platform_mutex_lock(&c->mutex);
	if (!c->isconnected)
		goto exit;

    platform_timer_init(&timer);
    platform_timer_countdown(&timer, c->command_timeout_ms);
    
    if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
        goto exit;
    if ((rc = sendPacket(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet
        goto exit; // there was a problem
    
    if (waitfor(c, UNSUBACK, &timer) == UNSUBACK)
    {
        unsigned short mypacketid;  // should be the same as the packetid above
        if (MQTTDeserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1)
            rc = 0; 
    }
    else 
    {
        rc = MQTT_CONNECTION_LOST;
	}
    
exit:
	platform_mutex_unlock(&c->mutex);
    return rc;
}
bool DataBus::AbstractClient::sendGetClientMethodReturnValueInfoRequestPacket(const quint8 clientId,
                                                                              const quint8 methodId,
                                                                              const quint8 returnValueIndex)
{
    // Create packet
    Packet packet;

    if (GetClientMethodParameterInfoRequestPacket::create(m_clientId,
                                                          clientId,
                                                          getNextPacketId(),
                                                          methodId,
                                                          returnValueIndex,
                                                          &packet) == false)
    {
        // Error, failed to create packet
        return false;
    }

    // Send packet
    if (sendPacket(packet) == false)
    {
        // Error, failed to send packet
        return false;
    }

    // Success
    return true;
}
int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter)
{
	int rc = FAILURE;
	Timer timer;
	MQTTString topic = MQTTString_initializer;
	topic.cstring = (char *)topicFilter;
	int len = 0;

#if defined(MQTT_TASK)
	FreeRTOS_MutexLock(&c->mutex);
#endif
	if (!c->isconnected)
		goto exit;

	TimerInit(&timer);
	TimerCountdownMS(&timer, c->command_timeout_ms);

	if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
		goto exit;
	if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
		goto exit; // there was a problem

	if (waitfor(c, UNSUBACK, &timer) == UNSUBACK) {
		unsigned short mypacketid;  // should be the same as the packetid above
		if (MQTTDeserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1)
			rc = 0;
	} else
		rc = FAILURE;

exit:
#if defined(MQTT_TASK)
	FreeRTOS_MutexUnlock(&c->mutex);
#endif
	return rc;
}
Esempio n. 7
0
int MQTTSubscribe(Client* c, const char* topicFilter, enum QoS qos, messageHandler messageHandler)
{ 
    int rc = FAILURE;  
    int len = 0;
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicFilter;


    if (!c->isconnected)
        goto exit;
    
    len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic, (int*)&qos);
    if (len <= 0)
    {
    	log_printf(LOG_ERROR, "MQTTSerialize_subscribe failed\n");
    	goto exit;
    }
    if ((rc = sendPacket_ev(c, len)) != SUCCESS) // send the subscribe packet use libev
        goto exit;             // there was a problem

	int i;
	for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
	{
		if (c->messageHandlers[i].topicFilter == 0)
		{
			c->messageHandlers[i].topicFilter = topicFilter;
			c->messageHandlers[i].fp = messageHandler;
			rc = 0;
			break;
		}
	}
exit:
    return rc;
}
int MQTTSubscribe(Client* c, const char* topicFilter, enum QoS qos, messageHandler messageHandler, pApplicationHandler_t applicationHandler)
{ 
    int rc = FAILURE;  
	Timer timer;
    int len = 0;
    int indexOfFreemessageHandler;
	
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicFilter;
    unsigned char isMessageHandlerFree = 0;
   
    InitTimer(&timer); 
    countdown_ms(&timer, c->command_timeout_ms);

    if (!c->isconnected)
        goto exit;
    
    len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic, (int*)&qos);
    if (len <= 0)
        goto exit;

    for (indexOfFreemessageHandler = 0; indexOfFreemessageHandler < MAX_MESSAGE_HANDLERS; ++indexOfFreemessageHandler){
    	if (c->messageHandlers[indexOfFreemessageHandler].topicFilter == 0){
    		isMessageHandlerFree = 1;
    		break;
    	}
    }
    if(isMessageHandlerFree == 0){
    	goto exit;
    }
    if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
        goto exit;             // there was a problem
    
    if (waitfor(c, SUBACK, &timer) == SUBACK)      // wait for suback 
    {
        int count = 0, grantedQoS = -1;
        unsigned short mypacketid;
        if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size) == 1)
            rc = grantedQoS; // 0, 1, 2 or 0x80 
        if (rc != 0x80)
        {
			c->messageHandlers[indexOfFreemessageHandler].topicFilter =
					topicFilter;
			c->messageHandlers[indexOfFreemessageHandler].fp = messageHandler;
			c->messageHandlers[indexOfFreemessageHandler].applicationHandler =
					applicationHandler;
			rc = 0;
        }
    }
    else 
        rc = FAILURE;

exit:
		DeInitTimer(&timer); //STM : added this line
    return rc;
}
Esempio n. 9
0
int MQTTSubscribeWithResults(MQTTClient *c, const char *topicFilter, enum QoS qos,
                             messageHandler messageHandler, MQTTSubackData *data)
{
    int rc = FAILURE;
    Timer timer;
    int len = 0;
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicFilter;

#if defined(MQTT_TASK)
    MutexLock(&c->mutex);
#endif

    if (!c->isconnected) {
        goto exit;
    }

    TimerInit(&timer);
    TimerCountdownMS(&timer, c->command_timeout_ms);

    len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic, (int *)&qos);

    if (len <= 0) {
        goto exit;
    }

    if ((rc = sendPacket(c, len, &timer)) != SUCCESS) { // send the subscribe packet
        goto exit;    // there was a problem
    }

    if (waitfor(c, SUBACK, &timer) == SUBACK) {    // wait for suback
        int count = 0;
        unsigned short mypacketid;
        data->grantedQoS = QOS0;

        if (MQTTDeserialize_suback(&mypacketid, 1, &count, (int *)&data->grantedQoS, c->readbuf, c->readbuf_size) == 1) {
            if (data->grantedQoS != 0x80) {
                rc = MQTTSetMessageHandler(c, topicFilter, messageHandler);
            }
        }
    } else {
        rc = FAILURE;
    }

exit:

    if (rc == FAILURE) {
        MQTTCloseSession(c);
    }

#if defined(MQTT_TASK)
    MutexUnlock(&c->mutex);
#endif
    return rc;
}
Esempio n. 10
0
int MQTTPublish(Client* c, const char* topicName, MQTTMessage* message)
{
    int rc = FAILURE;
    Timer timer; 
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicName;
    int len = 0;
	
    InitTimer(&timer); 
    countdown_ms(&timer, c->command_timeout_ms);
    
    if (!c->isconnected)
        goto exit;

    if (message->qos == QOS1 || message->qos == QOS2)
        message->id = getNextPacketId(c);
    
    len = MQTTSerialize_publish(c->buf, c->buf_size, 0, message->qos, message->retained, message->id, 
              topic, (unsigned char*)message->payload, message->payloadlen);
    if (len <= 0)
        goto exit;
    if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the publish packet
        goto exit; // there was a problem
    
    if (message->qos == QOS1)
    {
        if (waitfor(c, PUBACK, &timer) == PUBACK)
        {
            unsigned short mypacketid;
            unsigned char dup, type;
            if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
                rc = FAILURE;
        }
        else
            rc = FAILURE;
    }
    else if (message->qos == QOS2)
    {
        if (waitfor(c, PUBCOMP, &timer) == PUBCOMP)
        {
            unsigned short mypacketid;
            unsigned char dup, type;
            if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
                rc = FAILURE;
        }
        else
            rc = FAILURE;
    }
    
exit:

    DeInitTimer(&timer); //STM: added this line
    return rc;
}
int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message)
{
	int rc = FAILURE;
	Timer timer;
	MQTTString topic = MQTTString_initializer;
	topic.cstring = (char *)topicName;
	int len = 0;

#if defined(MQTT_TASK)
	FreeRTOS_MutexLock(&c->mutex);
#endif
	if (!c->isconnected)
		goto exit;

	TimerInit(&timer);
	TimerCountdownMS(&timer, c->command_timeout_ms);

	if (message->qos == QOS1 || message->qos == QOS2)
		message->id = getNextPacketId(c);

	len = MQTTSerialize_publish(c->buf, c->buf_size, 0, message->qos, message->retained, message->id,
	                            topic, (unsigned char*)message->payload, message->payloadlen);
	if (len <= 0)
		goto exit;
	if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
		goto exit; // there was a problem

	if (message->qos == QOS1) {
		if (waitfor(c, PUBACK, &timer) == PUBACK) {
			unsigned short mypacketid;
			unsigned char dup, type;
			if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
				rc = FAILURE;
		} else
			rc = FAILURE;
	} else if (message->qos == QOS2) {
		if (waitfor(c, PUBCOMP, &timer) == PUBCOMP) {
			unsigned short mypacketid;
			unsigned char dup, type;
			if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
				rc = FAILURE;
		} else
			rc = FAILURE;
	}

exit:
#if defined(MQTT_TASK)
	FreeRTOS_MutexUnlock(&c->mutex);
#endif
	return rc;
}
int  MQTTSubscribe (Client *c, const char *topicFilter,  enum QoS qos,
                    messageHandler messageHandler)
{
    int         i;
    int         rc = FAILURE;
    Timer       timer;
    int         len = 0;
    MQTTString  topic = MQTTString_initializer;

    topic.cstring = (char*) topicFilter;

    InitTimer (&timer);
    countdown_ms (&timer, c->command_timeout_ms);   // default is 1 second timeouts

    if ( ! c->isconnected)
        goto exit;

    len = MQTTSerialize_subscribe (c->buf, c->buf_size, 0, getNextPacketId(c), 1,
                                   &topic, (int*) &qos);
    if (len <= 0)
        goto exit;
    if ((rc = sendPacket(c, len, &timer)) != SUCCESS)  // send the subscribe packet
        goto exit;             // there was a problem

    if (waitfor(c, SUBACK, &timer) == SUBACK)          // wait for suback
      {
        int count = 0, grantedQoS = -1;
        unsigned short mypacketid;
        if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size) == 1)
           rc = grantedQoS;       // will be 0, 1, 2 or 0x80
        if (rc != 0x80)
          {
            for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
              {
                if (c->messageHandlers[i].topicFilter == 0)
                  {
                    c->messageHandlers[i].topicFilter = topicFilter;
                    c->messageHandlers[i].fp = messageHandler;
                    rc = 0;    // denote success
                    break;
                  }
              }
          }
      }
     else rc = FAILURE;        // timed out - no SUBACK received

exit:
    return rc;
}
Esempio n. 13
0
int MQTTUnsubscribe(MQTTClient *c, const char *topicFilter)
{
    int rc = FAILURE;
    Timer timer;
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicFilter;
    int len = 0;

#if defined(MQTT_TASK)
    MutexLock(&c->mutex);
#endif

    if (!c->isconnected) {
        goto exit;
    }

    TimerInit(&timer);
    TimerCountdownMS(&timer, c->command_timeout_ms);

    if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0) {
        goto exit;
    }

    if ((rc = sendPacket(c, len, &timer)) != SUCCESS) { // send the subscribe packet
        goto exit;    // there was a problem
    }

    if (waitfor(c, UNSUBACK, &timer) == UNSUBACK) {
        unsigned short mypacketid;  // should be the same as the packetid above

        if (MQTTDeserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1) {
            /* remove the subscription message handler associated with this topic, if there is one */
            MQTTSetMessageHandler(c, topicFilter, NULL);
        }
    } else {
        rc = FAILURE;
    }

exit:

    if (rc == FAILURE) {
        MQTTCloseSession(c);
    }

#if defined(MQTT_TASK)
    MutexUnlock(&c->mutex);
#endif
    return rc;
}
int MQTTSubscribe(MQTTClient* c, const char* topicFilter, enum QoS qos, messageHandler messageHandler)
{
	int rc = FAILURE;
	Timer timer;
	int len = 0;
	MQTTString topic = MQTTString_initializer;
	topic.cstring = (char *)topicFilter;

#if defined(MQTT_TASK)
	FreeRTOS_MutexLock(&c->mutex);
#endif
	if (!c->isconnected)
		goto exit;

	TimerInit(&timer);
	TimerCountdownMS(&timer, c->command_timeout_ms);

	len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic, (int*)&qos);
	if (len <= 0)
		goto exit;
	if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
		goto exit;             // there was a problem

	if (waitfor(c, SUBACK, &timer) == SUBACK) {    // wait for suback
		int count = 0, grantedQoS = -1;
		unsigned short mypacketid;
		if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size) == 1)
			rc = grantedQoS; // 0, 1, 2 or 0x80
		if (rc != 0x80) {
			int i;
			for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
				if (c->messageHandlers[i].topicFilter == 0) {
					c->messageHandlers[i].topicFilter = topicFilter;
					c->messageHandlers[i].fp = messageHandler;
					rc = 0;
					break;
				}
			}
		}
	} else
		rc = FAILURE;

exit:
#if defined(MQTT_TASK)
	FreeRTOS_MutexUnlock(&c->mutex);
#endif
	return rc;
}
Esempio n. 15
0
int MQTTUnsubscribe(Client* c, const char* topicFilter)
{
    int rc = FAILURE;
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicFilter;
    int len = 0;

    if (!c->isconnected)
        goto exit;

    if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
        goto exit;
    if ((rc = sendPacket_ev(c, len)) != SUCCESS) // send the subscribe packet
        goto exit; // there was a problem
exit:
    return rc;
}
Esempio n. 16
0
/**
 * @brief           Send packets in a burst
 *
 * @param devId     ID of DPDK device
 *
 * @return          true on success
 */
bool DPDKAdapter::StreamInfo::sendBurst(uint8_t devId)
{
    PacketInfo  *pPacket   = NULL;
    uint32_t    txPktCnt   = 0;
    uint8_t     txPktIndex = 0;
    MBuf_t*     txBurstBuf[DPDK_TX_MAX_PKT_BURST];

    while (txPktCnt < txBurstSize_)
    {
        for (int i = 0; i < burstSize_; i++)
        {
            txPktIndex = txPktCnt % txBurstSize_;

            if (!getMbuf(curPacketId_, txBurstBuf[txPktIndex]))
            {
                qCritical("Could not get mbuf");
                continue;
            }

            if (txPktIndex == txBurstSize_ - 1)
            {
                uint8_t txSentPkt = DPDKAdapter::instance()->sendMbufBurstWithoutFree(devId, txBurstBuf, txBurstSize_);

                if (txSentPkt == txBurstSize_)
                {
                    if (DPDKAdapter::instance()->isRxStarted(devId))
                    {
                        DPDKAdapter::instance()->saveToBuf(devId, txBurstBuf, txBurstSize_);
                    }
                }
            }

            curPacketId_ = getNextPacketId(curPacketId_);

            txPktCnt++;
            sentPackets_++;
        }

        sentBursts_++;
    }

    return true;
}
Esempio n. 17
0
bool DataBus::AbstractClient::sendGetClientMethodListRequestPacket(const quint8 clientId)
{
    // Create packet
    Packet packet;

    if (GetClientMethodListRequestPacket::create(m_clientId, clientId, getNextPacketId(), &packet) == false)
    {
        // Error, failed to create packet
        return false;
    }

    // Send packet
    if (sendPacket(packet) == false)
    {
        // Error, failed to send packet
        return false;
    }

    // Success
    return true;
}
Esempio n. 18
0
int MQTTUnsubscribe(Client* c, const char* topicFilter)
{   
    int rc = FAILURE;
    Timer timer;    
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicFilter;
    int len = 0;
    int i=0;
	
    InitTimer(&timer);
    countdown_ms(&timer, c->command_timeout_ms);
    
    if (!c->isconnected)
        goto exit;
    
    if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
        goto exit;
    if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the unsubscribe packet
        goto exit; // there was a problem
    
    if (waitfor(c, UNSUBACK, &timer) == UNSUBACK)
    {
        unsigned short mypacketid;  // should be the same as the packetid above
        if (MQTTDeserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1){
        	 for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i){
        	        if (c->messageHandlers[i].topicFilter != 0 && (strcmp(c->messageHandlers[i].topicFilter, topicFilter)==0)){
        	        	c->messageHandlers[i].topicFilter = 0;
        	        	// We dont want to break here, if the same topic is registered with 2 callbacks. Unlikeley scenario.
        	        }
        	 }
            rc = 0; 
        }
    }
    else
        rc = FAILURE;
    
exit:
		DeInitTimer(&timer); //STM: added this line
    return rc;
}
Esempio n. 19
0
int MQTTSubscribe(MQTTClient* c, const char* topicFilter, enum QoS qos)
{ 
    int rc = MQTT_FAILURE;  
    Timer timer;
    int len = 0;
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicFilter;
    
	platform_mutex_lock(&c->mutex);

	if (!c->isconnected)
		goto exit;

    platform_timer_init(&timer);
    platform_timer_countdown(&timer, c->command_timeout_ms);
    
    len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic, (int*)&qos);
    if (len <= 0)
        goto exit;
    if ((rc = sendPacket(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet
        goto exit;             // there was a problem

    if (waitfor(c, SUBACK, &timer) == SUBACK)      // wait for suback 
    {
        int count = 0, grantedQoS = -1;
        unsigned short mypacketid;
        if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size) == 1)
            rc = grantedQoS; // 0, 1, 2 or 0x80 
    }
    else 
    {
		rc = MQTT_CONNECTION_LOST;
	}
        
exit:
	platform_mutex_unlock(&c->mutex);
    return rc;
}
Esempio n. 20
0
bool DataBus::AbstractClient::start(const quint8 clientId,
                                    const QString &clientName,
                                    const quint8 activityTimeout,
                                    const quint8 pingResponseTimeout,
                                    const quint8 pingRetryCount,
                                    const int registrationTimeout)
{
    // Check input parameters
    if ((clientId == 0) ||
        (clientName.toUtf8().length() > STRING_MAX_LENGTH) ||
        (activityTimeout < 10) ||
        (pingResponseTimeout < 10) ||
        (pingResponseTimeout > 60) ||
        (pingResponseTimeout > activityTimeout) ||
        (registrationTimeout < 5000))
    {
        // Error, invalid input parameters
        return false;
    }

    // Initialize DataBus Packet parser
    m_packetParser.initialize();

    // Save client registration settings
    m_clientId = clientId;
    m_clientName = clientName;
    m_activityTimeout = activityTimeout;
    m_pingResponseTimeout = pingResponseTimeout;
    m_pingRetryCount = pingRetryCount;

    // Initialize Packet ID generator
    m_packetIdCounter = 0;

    // Initialize server timeout (2x activity timeout in milliseconds)
    m_serverTimeout = m_activityTimeout * 1000 * 2;

    // Start the registration process
    Packet packet;

    if (RegisterClientRequestPacket::create(m_clientId,
                                            getNextPacketId(),
                                            m_activityTimeout,
                                            m_pingResponseTimeout,
                                            m_pingRetryCount,
                                            &packet) == false)
    {
        // Error, failed to create packet
        return false;
    }

    if (sendPacket(packet) == false)
    {
        // Error, failed to send packet
        return false;
    }

    // Wait for client registration to finish
    m_timer.stop();

    connect(&m_timer, SIGNAL(timeout()), this, SLOT(processRegistrationTimeout()));

    m_timer.setSingleShot(true);
    m_timer.start(registrationTimeout);

    // Success
    return true;
}
Esempio n. 21
0
int MQTTSubscribe( Client* c, const char* topicFilter, enum QoS qos, messageHandler messageHandler )
{
    int rc = MQTT_FAILURE;
    int len = 0;
    Timer timer;

    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *) topicFilter;

    wiced_init_timer( &timer );
    wiced_countdown_ms( &timer, c->command_timeout_ms );

    if ( !c->isconnected )
    {
        goto exit;
    }

    len = MQTTSerialize_subscribe( c->buf, c->buf_size, 0, getNextPacketId( c ), 1, &topic, (int*) &qos );
    if ( len <= 0 )
    {
        goto exit;
    }

    if ( ( rc = sendPacket( c, len, &timer ) ) != MQTT_SUCCESS ) // send the subscribe packet
    {
        goto exit;
        // there was a problem
    }

    if ( waitfor( c, SUBACK, &timer ) == SUBACK ) // wait for suback
    {
        int count = 0, grantedQoS = -1;
        unsigned short mypacketid;

        if ( MQTTDeserialize_suback( &mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size ) == 1 )
        {
            rc = grantedQoS; // 0, 1, 2 or 0x80
        }

        if ( rc != 0x80 )
        {
            int i;
            for ( i = 0; i < MAX_MESSAGE_HANDLERS; ++i )
            {
                if ( c->messageHandlers[ i ].topicFilter == 0 )
                {
                    c->messageHandlers[ i ].topicFilter = topicFilter;
                    c->messageHandlers[ i ].fp = messageHandler;
                    rc = 0;
                    break;
                }
            }
        }
    }
    else
    {
        rc = MQTT_FAILURE;
    }

    exit: return rc;
}
Esempio n. 22
0
int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message)
{
    int rc = MQTT_FAILURE;
    Timer timer;   
    MQTTString topic = MQTTString_initializer;
    topic.cstring = (char *)topicName;
    int len = 0;

	platform_mutex_lock(&c->mutex);
	if (!c->isconnected)
		goto exit;

    platform_timer_init(&timer);
    platform_timer_countdown(&timer, c->command_timeout_ms);

    if (message->qos == QOS1 || message->qos == QOS2)
        message->id = getNextPacketId(c);
    
    len = MQTTSerialize_publish(c->buf, c->buf_size, 0, message->qos, message->retained, message->id, 
              topic, (unsigned char*)message->payload, message->payloadlen);
    if (len <= 0)
        goto exit;
    if ((rc = sendPacket(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet
    {
        goto exit; // there was a problem
    }

    if (message->qos == QOS1)
    {
        if (waitfor(c, PUBACK, &timer) == PUBACK)
        {
            unsigned short mypacketid;
            unsigned char dup, type;
            if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
            {
                platform_printf("failed to deserialize ACK\n");
                rc = MQTT_FAILURE;
            }
        }
        else
        {
            rc = MQTT_CONNECTION_LOST;
        }
    }
    else if (message->qos == QOS2)
    {
        if (waitfor(c, PUBCOMP, &timer) == PUBCOMP)
        {
            unsigned short mypacketid;
            unsigned char dup, type;
            if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
                rc = MQTT_FAILURE;
        }
        else 
        {
            rc = MQTT_CONNECTION_LOST;
		}
    }
    
exit:
	platform_mutex_unlock(&c->mutex);
    return rc;
}