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; }
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; }
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; }
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; }
static int mqttClient_processSubAck(mqttClient_t* clientData) { int count = 0; int grantedQoS = -1; int rc = LE_OK; unsigned short packetId = 0; LE_DEBUG("---> SUBACK"); LE_ASSERT(clientData); rc = le_timer_Stop(clientData->session.cmdTimer); if (rc) { LE_ERROR("le_timer_Stop() failed(%d)", rc); goto cleanup; } rc = MQTTDeserialize_suback(&packetId, 1, &count, &grantedQoS, clientData->session.rx.buf, sizeof(clientData->session.rx.buf)); if (rc != 1) { LE_ERROR("MQTTDeserialize_suback() failed"); rc = LE_BAD_PARAMETER; goto cleanup; } else if (clientData->session.nextPacketId != packetId) { LE_ERROR("invalid packet ID(%u != %u)", clientData->session.nextPacketId, packetId); rc = LE_BAD_PARAMETER; goto cleanup; } rc = LE_OK; LE_DEBUG("returned count(%u) granted QoS(0x%02x)", count, grantedQoS); if ((count == 0) && (grantedQoS == 0x80)) { LE_ERROR("no granted QoS"); rc = LE_BAD_PARAMETER; goto cleanup; } cleanup: return rc; }
int mqtt_subscribe(mqtt_client_t *c, char* topicstr, int qos) { MQTTString topic = MQTTString_initializer; int msgid = 1; int len; int rc = -1; topic.cstring = topicstr; len = MQTTSerialize_subscribe(c->wbuf, c->wbuflen, 0, msgid, 1, &topic, &qos); if (len <= 0) goto exit; rc = mqtt_write(c->sockfd, c->wbuf, len); if (rc < 0) goto exit; if (MQTTPacket_read(c->rbuf, c->rbuflen, c->getfn) == SUBACK) { unsigned short submsgid; int subcount; int granted_qos; rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, c->rbuf, c->rbuflen); if (granted_qos != 0) { DEBUG("granted qos != 0, %d\n", granted_qos); rc = -1; } else { rc = 0; } } else { rc = -1; } exit: return rc; }
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; }
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen) { int index = 0; int rem_length = 0; MQTTHeader header = {0}; int strindex = 0; header.byte = buf[index++]; index += MQTTPacket_decodeBuf(&buf[index], &rem_length); switch (header.bits.type) { case CONNACK: { unsigned char sessionPresent, connack_rc; if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1) strindex = MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent); } break; case PUBLISH: { unsigned char dup, retained, *payload; uint64_t packetid; int qos, payloadlen; MQTTString topicName = MQTTString_initializer; if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName, &payload, &payloadlen, buf, buflen) == 1) strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid, topicName, payload, payloadlen); } break; case PUBACK: case PUBREC: case PUBREL: case PUBCOMP: { unsigned char packettype, dup; uint64_t packetid; if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1) strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid); } break; case SUBACK: { uint64_t packetid; int maxcount = 1, count = 0; int grantedQoSs[1]; if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1) strindex = MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs); } break; case UNSUBACK: { uint64_t packetid; if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1) strindex = MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid); } break; case PINGREQ: case PINGRESP: case DISCONNECT: strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]); break; } return strbuf; }
/** * @brief 向服务器订阅一个消息,该函数会因为TCP接收数据函数而阻塞 * @param pTopic 消息主题,传入 * @param pMessage 消息内容,传出 * @retval 小于0表示订阅消息失败 */ int mqtt_subscrib(char *pTopic,char *pMessage) { MQTTPacket_connectData data = MQTTPacket_connectData_initializer; int rc = 0; unsigned char buf[200]; int buflen = sizeof(buf); int msgid = 1; MQTTString topicString = MQTTString_initializer; int req_qos = 0; int len = 0; rc = transport_open(); if(rc < 0){ printf("transport_open error\n\r"); return rc; } data.clientID.cstring = ""; data.keepAliveInterval = 5;//服务器保持连接时间,超过该时间后,服务器会主动断开连接,单位为秒 data.cleansession = 1; data.username.cstring = ""; data.password.cstring = ""; len = MQTTSerialize_connect(buf, buflen, &data); rc = transport_sendPacketBuffer(buf, len); if(rc != len){ printf("connect transport_sendPacketBuffer error\n\r"); goto exit; } /* wait for connack */ if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK) { unsigned char sessionPresent, connack_rc; if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0) { printf("Unable to connect, return code %d\n\r", connack_rc); goto exit; } }else{ printf("MQTTPacket_read error\n\r"); goto exit; } /* subscribe */ topicString.cstring = pTopic; len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos); rc = transport_sendPacketBuffer(buf, len); if(rc != len){ printf("connect transport_sendPacketBuffer error\n\r"); goto exit; } if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK) /* wait for suback */ { unsigned short submsgid; int subcount; int granted_qos; rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); if (granted_qos != 0) { printf("granted qos != 0, %d\n\r", granted_qos); goto exit; } } else goto exit; /* loop getting msgs on subscribed topic */ topicString.cstring = pTopic; memset(buf,0,buflen); //transport_getdata接收数据会阻塞,除非服务器断开连接后才返回 if (MQTTPacket_read(buf, buflen, transport_getdata) == PUBLISH){ unsigned char dup; int qos; unsigned char retained; unsigned short msgid; int payloadlen_in; unsigned char* payload_in; MQTTString receivedTopic; rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, &payload_in, &payloadlen_in, buf, buflen); printf("message arrived %d: %s\n\r", payloadlen_in, payload_in); strcpy(pMessage,(const char *)payload_in); } printf("disconnecting\n\r"); len = MQTTSerialize_disconnect(buf, buflen); rc = transport_sendPacketBuffer(buf, len); exit: transport_close(); return rc; }
int main() { MQTTPacket_connectData data = MQTTPacket_connectData_initializer; int rc = 0; char buf[200]; int buflen = sizeof(buf); int msgid = 1; MQTTString topicString = MQTTString_initializer; int req_qos = 0; char* payload = "mypayload"; int payloadlen = strlen(payload); int len = 0; signal(SIGINT, cfinish); signal(SIGTERM, cfinish); rc = Socket_new("127.0.0.1", 1883, &mysock); data.clientID.cstring = "me"; data.keepAliveInterval = 20; data.cleansession = 1; data.username.cstring = "testuser"; data.password.cstring = "testpassword"; struct timeval tv; tv.tv_sec = 1; /* 1 second Timeout */ tv.tv_usec = 0; setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); len = MQTTSerialize_connect(buf, buflen, &data); rc = write(mysock, buf, len); /* wait for connack */ if (MQTTPacket_read(buf, buflen, getdata) == CONNACK) { int connack_rc; if (MQTTDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0) { printf("Unable to connect, return code %d\n", connack_rc); goto exit; } } else goto exit; /* subscribe */ topicString.cstring = "substopic"; len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos); rc = write(mysock, buf, len); if (MQTTPacket_read(buf, buflen, getdata) == SUBACK) /* wait for suback */ { int submsgid; int subcount; int granted_qos; rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); if (granted_qos != 0) { printf("granted qos != 0, %d\n", granted_qos); goto exit; } } else goto exit; topicString.cstring = "pubtopic"; while (!toStop) { if (MQTTPacket_read(buf, buflen, getdata) == PUBLISH) { int dup; int qos; int retained; int msgid; int payloadlen_in; char* payload_in; int rc; MQTTString receivedTopic; rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, &payload_in, &payloadlen_in, buf, buflen); printf("message arrived %.*s\n", payloadlen_in, payload_in); } printf("publishing reading\n"); len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen); rc = write(mysock, buf, len); } printf("disconnecting\n"); len = MQTTSerialize_disconnect(buf, buflen); rc = write(mysock, buf, len); exit: rc = shutdown(mysock, SHUT_WR); rc = recv(mysock, NULL, 0, 0); rc = close(mysock); return 0; }
int main(int argc, char *argv[]) { MQTTPacket_connectData data = MQTTPacket_connectData_initializer; int rc = 0; int mysock = 0; unsigned char buf[200]; int buflen = sizeof(buf); int msgid = 1; MQTTString topicString = MQTTString_initializer; int req_qos = 0; char* payload = "mypayload"; int payloadlen = strlen(payload); int len = 0; char *host = "m2m.eclipse.org"; int port = 1883; MQTTTransport mytransport; stop_init(); if (argc > 1) host = argv[1]; if (argc > 2) port = atoi(argv[2]); mysock = transport_open(host, port); if(mysock < 0) return mysock; printf("Sending to hostname %s port %d\n", host, port); mytransport.sck = &mysock; mytransport.getfn = transport_getdatanb; mytransport.state = 0; data.clientID.cstring = "me"; data.keepAliveInterval = 20; data.cleansession = 1; data.username.cstring = "testuser"; data.password.cstring = "testpassword"; len = MQTTSerialize_connect(buf, buflen, &data); rc = transport_sendPacketBuffer(mysock, buf, len); /* wait for connack */ if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK) { unsigned char sessionPresent, connack_rc; if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0) { printf("Unable to connect, return code %d\n", connack_rc); goto exit; } } else goto exit; /* subscribe */ topicString.cstring = "substopic"; len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos); rc = transport_sendPacketBuffer(mysock, buf, len); do { int frc; if ((frc=MQTTPacket_readnb(buf, buflen, &mytransport)) == SUBACK) /* wait for suback */ { unsigned short submsgid; int subcount; int granted_qos; rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); if (granted_qos != 0) { printf("granted qos != 0, %d\n", granted_qos); goto exit; } break; } else if (frc == -1) goto exit; } while (1); /* handle timeouts here */ /* loop getting msgs on subscribed topic */ topicString.cstring = "pubtopic"; while (!toStop) { /* handle timeouts */ if (MQTTPacket_readnb(buf, buflen, &mytransport) == PUBLISH) { unsigned char dup; int qos; unsigned char retained; unsigned short msgid; int payloadlen_in; unsigned char* payload_in; int rc; MQTTString receivedTopic; rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, &payload_in, &payloadlen_in, buf, buflen); printf("message arrived %.*s\n", payloadlen_in, payload_in); printf("publishing reading\n"); len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen); rc = transport_sendPacketBuffer(mysock, buf, len); } } printf("disconnecting\n"); len = MQTTSerialize_disconnect(buf, buflen); rc = transport_sendPacketBuffer(mysock, buf, len); exit: transport_close(mysock); return 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; }
int cycle(Client* c, Timer* timer) { // read the socket, see what work is due int read_status; unsigned short packet_type; read_status = readPacket(c, timer); packet_type = (unsigned short )read_status; if(read_status == SOCKET_CLOSED) { MQTTReConnect(c); goto exit; } int len = 0, rc = SUCCESS; switch (packet_type) { case CONNACK: { { unsigned char connack_rc = 255; char sessionPresent = 0; if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, c->readbuf, c->readbuf_size) == 1) { c->isconnected = 1; //开cloud灯 system(SET_LIGHT_CLOUD); log_printf(LOG_NOTICE, "[MqttConnected]: recv connack\n"); //subtopics and pubtopics init sprintf(&subtopics[0][0], "%s%s", SUBTOPIC_PRE1, gateway_info.did); sprintf(&subtopics[1][0], "%s%s", SUBTOPIC_PRE2, gateway_info.productkey); sprintf(&subtopics[2][0], "%s%s/#", SUBTOPIC_PRE3, gateway_info.did); sprintf(&pubtopics[0][0], "%s%s", PUBTOPIC_PRE1, gateway_info.did); sprintf(&pubtopics[1][0], "%s%s", PUBTOPIC_PRE2, gateway_info.did); sprintf(&pubtopics[2][0], "%s%s/#", PUBTOPIC_PRE3, gateway_info.did); rc = MQTTSubscribe(c, subtopics[c->suborder], 0, mh[c->suborder]); //for test log_printf(LOG_NOTICE, "Subscribing to %s\n", subtopics[c->suborder]); c->suborder++; } } } break; case PUBACK: break; case 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) { if(c->suborder <subscribe_over) { rc = MQTTSubscribe(c, subtopics[c->suborder], 0, mh[c->suborder]); log_printf(LOG_NOTICE, "Subscribing to %s\n",subtopics[c->suborder]); c->suborder++; } // int i; // for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) // { // if (c->messageHandlers[i].topicFilter == 0) // { // c->messageHandlers[i].topicFilter = mytopics[i]; // c->messageHandlers[i].fp = mh[i]; // rc = 0; // break; // } // } } else { log_printf(LOG_ERROR, "SUCACK FAILED\n"); //TODO: error handle } } break; case PUBLISH: { if(c->suborder != subscribe_over) { log_printf(LOG_ERROR, "[REC_MSG] rece publish msg but subcribe not over\n"); //TODO: error } MQTTString topicName = MQTTString_initializer; MQTTMessage msg; if (MQTTDeserialize_publish((unsigned char*)&msg.dup, (int*)&msg.qos, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName, (unsigned char**)&msg.payload, (int*)&msg.payloadlen, c->readbuf, c->readbuf_size) != 1) goto exit; deliverMessage(c, &topicName, &msg); // MQTTPublish(c, "applerespond", &msg); //add by yaly for test if (msg.qos != QOS0) { if (msg.qos == QOS1) len = MQTTSerialize_ack(c->buf, c->buf_size, PUBACK, 0, msg.id); else if (msg.qos == QOS2) len = MQTTSerialize_ack(c->buf, c->buf_size, PUBREC, 0, msg.id); if (len <= 0) rc = FAILURE; else rc = sendPacket_ev(c, len); if (rc == FAILURE) goto exit; } } break; case PUBREC: { unsigned short mypacketid; unsigned char dup, type; if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1) rc = FAILURE; else if ((len = MQTTSerialize_ack(c->buf, c->buf_size, PUBREL, 0, mypacketid)) <= 0) rc = FAILURE; else if ((rc = sendPacket_ev(c, len)) != SUCCESS) // send the PUBREL packet rc = FAILURE; if (rc == FAILURE) goto exit; break; } break; case PUBCOMP: break; case PINGRESP: log_printf(LOG_NOTICE, "[MqttPingResp]\n"); c->ping_outstanding = 0; break; } // keepalive(c); //modify by yanly exit: if (rc == SUCCESS) rc = packet_type; return rc; }