void app_parse_mqttmsg(uint8_t *packet_buffer) { uint8_t msg_type = 0; uint16_t msg_id_rcv = 0; char topic_name[56]={0}; char msg[128]={0}; msg_type = MQTTParseMessageType(packet_buffer); //printf("-----> parse:0x%02X\n", msg_type); switch(msg_type) { case MQTT_MSG_CONNACK: if(packet_buffer[3] == 0) { printf("Mqtt login server success\n"); /* subscribe */ init_topic(&g_sub_topic, sub_topic_name, sizeof(sub_topic_name)); mqtt_subscribe(&g_stMQTTBroker, g_sub_topic->name, &(g_sub_topic->msg_id)); /* publish msg with Qos 0 */ //step1:>>>publish init_topic(&g_pub_topic1, pub_topic_name1, sizeof(pub_topic_name1)); mqtt_publish(&g_stMQTTBroker, g_pub_topic1->name, pub_msg1, 0); printf("APP publish msg[%s] with Qos 0\n", pub_msg1); deinit_topic(&g_pub_topic1); /* publish msg with Qos 1 */ //step1:>>>publish //step2:<<<puback init_topic(&g_pub_topic2, pub_topic_name2, sizeof(pub_topic_name2)); mqtt_publish_with_qos(&g_stMQTTBroker, g_pub_topic2->name, pub_msg2, 0, 1, &(g_pub_topic2->msg_id)); } else printf("Mqtt login server fail!\n"); break; case MQTT_MSG_SUBACK: msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_sub_topic && msg_id_rcv == g_sub_topic->msg_id) printf("Subcribe topic[%s] success\n", g_sub_topic->name); break; case MQTT_MSG_PUBLISH: mqtt_parse_pub_topic(packet_buffer, topic_name); mqtt_parse_publish_msg(packet_buffer, msg); printf("****** Topic[%s] recv msg: *****\n%s\n",topic_name, msg); /* unsubscribe */ if(!strcmp(g_sub_topic->name, topic_name)) mqtt_unsubscribe(&g_stMQTTBroker, g_sub_topic->name, &(g_sub_topic->msg_id)); break; case MQTT_MSG_UNSUBACK: msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_sub_topic && msg_id_rcv == g_sub_topic->msg_id) { printf("Unsubcribe topic[%s] success\n", g_sub_topic->name); deinit_topic(&g_sub_topic); } break; case MQTT_MSG_PUBACK://Qos1 msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_pub_topic2 && msg_id_rcv == g_pub_topic2->msg_id) { printf("APP publish msg[%s] with Qos 1\n", pub_msg2); deinit_topic(&g_pub_topic2); /* publish msg with Qos 2 */ //step1:>>>publish //step2:<<<pubrec //step3:>>>pubrel //step4:<<<pubcomp init_topic(&g_pub_topic3, pub_topic_name3, sizeof(pub_topic_name3)); mqtt_publish_with_qos(&g_stMQTTBroker, g_pub_topic3->name, pub_msg3, 1, 2, &(g_pub_topic3->msg_id)); } break; case MQTT_MSG_PUBREC://Qos2 msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_pub_topic3 && msg_id_rcv == g_pub_topic3->msg_id) mqtt_pubrel(&g_stMQTTBroker, g_pub_topic3->msg_id); break; case MQTT_MSG_PUBCOMP://Qos2 msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_pub_topic3 && msg_id_rcv == g_pub_topic3->msg_id) { printf("APP publish msg[%s] with Qos 2\n", pub_msg3); deinit_topic(&g_pub_topic3); } break; default: printf("Unknow mqtt msg type\n"); break; } }
int32 Mqtt_DispatchPublishPacket( pgcontext pgc,u8 *packetBuffer,int32 packetLen ) { u8 topic[128]; int32 topiclen; u8 *pHiP0Data; int32 HiP0DataLen; int32 i; u8 varlen=0; u8 clientid[PHONECLIENTID + 1]; //int32 clientidlen = 0; u8 *pTemp; u16 cmd; int32 sn; u16 *pcmd=NULL; topiclen = mqtt_parse_pub_topic(packetBuffer, topic); //HiP0DataLen = packetLen - topiclen; topic[topiclen] = '\0'; HiP0DataLen = mqtt_parse_publish_msg(packetBuffer, &pHiP0Data); if(strncmp((const int8*)topic,"app2dev/",strlen("app2dev/"))==0) { varlen = mqtt_num_rem_len_bytes( pHiP0Data+3 ); if(varlen<1 || varlen>4) { return 0; } pcmd = (u16*)&pHiP0Data[4+varlen+1]; cmd = ntohs( *pcmd ); pTemp = &topic[strlen("app2dev/")]; i = 0; while (*pTemp != '/') { i++; pTemp++; } pTemp ++; /* 跳过\/ */ i=0; while (*pTemp != '\0' && i <= PHONECLIENTID) { clientid[i] = *pTemp; i++; pTemp++; } if(i > PHONECLIENTID) { /* should handle invalid phone client id.don't ack the cmd */ i = PHONECLIENTID; } clientid[i]= '\0'; strcpy( pgc->rtinfo.waninfo.phoneClientId ,(const int8*)clientid ); pgc->rtinfo.waninfo.srcAttrs.cmd = cmd; memcpy( packetBuffer,pHiP0Data,HiP0DataLen ); GAgent_Printf( GAGENT_INFO,"Cloud CMD =%04X",cmd ); if( cmd==0x0093 ) { sn = *(int32 *)&pHiP0Data[4+varlen+1 + sizeof(cmd)]; sn = ntohl(sn); Cloud_SetClientAttrs(pgc, clientid, cmd, sn); } else if( cmd == 0x0090 ) { sn = 0; Cloud_SetClientAttrs(pgc, clientid, cmd, sn); } return HiP0DataLen; } // 订阅最新固件响应 else if(strncmp((const int8*)topic,"ser2cli_res/",strlen("ser2cli_res/"))==0) { pcmd = (u16*)&pHiP0Data[4]; cmd = ntohs( *pcmd ); // pHiP0Data消息体的指针 // HiP0DataLen消息体的长度 packetBuffer switch(cmd) { /* V4.1 Don't use this cmd */ case 0x020e: break; // wan client on line numbers res. case 0x0210: Mqtt_ResOnlineClient( pgc,(int8*)pHiP0Data, HiP0DataLen); break; case 0x0211: //todo MCU OTA. GAgent_Printf( GAGENT_DEBUG,"M2M cmd to check OTA!!! "); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_GET_SOFTVER); break; default: break; } return 0; } return 0; }
/** * Main routine * */ static int mqtt_sub(void) { int packet_length,socket_id; uint16_t msg_id, msg_id_rcv; mqtt_broker_handle_t broker; packet_buffer.len=BUFSIZE; broker.socket_info = (void *)&socket_id; mqtt_init(&broker, "MQTT_SUB"); //mqtt_init_auth(&broker, "quijote", "rocinante"); socket_id = init_socket(&broker); // >>>>> CONNECT mqtt_connect(&broker); // <<<<< CONNACK // unsigned long pubTaskHandle= getTaskHandle(SUB_TASKID); // vTaskPrioritySet( (xTaskHandle)&pubTaskHandle, PUB_TASK_PRIORITY); //to degrade sub_task priority to let it as the same of pub_task packet_length = read_packet(1,socket_id,(Tranmit_t *)&packet_buffer); if(packet_length < 0) { UART_PRINT("Error(%d) on read packet!\n\r"); return -1; } if(MQTTParseMessageType(packet_buffer.buffer) != MQTT_MSG_CONNACK) { UART_PRINT("CONNACK expected!\n\r"); return -2; } if(packet_buffer.buffer[3] != 0x00) { UART_PRINT("CONNACK failed!\n\r"); return -2; } UART_PRINT("Connected to broker!\n\r"); if(OSI_OK != osi_TaskCreate( taskPub, (const signed char*)"taskPub", 2048, NULL, PUB_TASK_PRIORITY, (OsiTaskHandle)&pubTaskHandle )) UART_PRINT("taskPub failed\n\r"); // Signals after connect MQTT //signal(SIGALRM, alive); //alarm(keepalive); //signal(SIGINT, term); // >>>>> SUBSCRIBE mqtt_subscribe(&broker, "helloword", &msg_id); // <<<<< SUBACK packet_length = read_packet(1,socket_id,(Tranmit_t *)&packet_buffer); if(packet_length < 0) { UART_PRINT("Error(%d) on read packet!\n\r"); return -1; } if(MQTTParseMessageType(packet_buffer.buffer) != MQTT_MSG_SUBACK) { UART_PRINT("SUBACK expected!\n\r"); return -2; } msg_id_rcv = mqtt_parse_msg_id(packet_buffer.buffer); if(msg_id != msg_id_rcv) { UART_PRINT("%d message id was expected, but %d message id was found!\n\r"); return -3; } while(1) { // <<<<< packet_length = read_packet(0,socket_id,(Tranmit_t *)&packet_buffer); if(packet_length == -1) { UART_PRINT("Error(%d) on read packet!\n\r"); return -1; } else if(packet_length > 0) { UART_PRINT("Packet Header: 0x%x...\n\r"); if(MQTTParseMessageType(packet_buffer.buffer) == MQTT_MSG_PUBLISH) { uint8_t topic[TOPIC_LEN_MAX], msg[MSG_LEN_MAX]; uint16_t len; len = mqtt_parse_pub_topic(packet_buffer.buffer, topic); topic[len] = '\0'; // for printf len = mqtt_parse_publish_msg(packet_buffer.buffer, msg); msg[len] = '\0'; // for printf //UART_PRINT("%s %s\n\r", topic, msg); UART_PRINT(topic); UART_PRINT("\n\r"); UART_PRINT(msg); UART_PRINT("\n\r"); } } } return 0; }
/** * Main routine * */ int main() { int packet_length; uint16_t msg_id, msg_id_rcv; mqtt_init(&broker, "client-id"); //mqtt_init_auth(&broker, "quijote", "rocinante"); init_socket(&broker, "107.22.188.194", 1883, keepalive); // >>>>> CONNECT mqtt_connect(&broker); // <<<<< CONNACK packet_length = read_packet(1); if(packet_length < 0) { fprintf(stderr, "Error(%d) on read packet!\n", packet_length); return -1; } if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_CONNACK) { fprintf(stderr, "CONNACK expected!\n"); return -2; } if(packet_buffer[3] != 0x00) { fprintf(stderr, "CONNACK failed!\n"); return -2; } // Signals after connect MQTT signal(SIGALRM, alive); alarm(keepalive); signal(SIGINT, term); // >>>>> SUBSCRIBE mqtt_subscribe(&broker, "public/test/topic", &msg_id); // <<<<< SUBACK packet_length = read_packet(1); if(packet_length < 0) { fprintf(stderr, "Error(%d) on read packet!\n", packet_length); return -1; } if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_SUBACK) { fprintf(stderr, "SUBACK expected!\n"); return -2; } msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(msg_id != msg_id_rcv) { fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv); return -3; } while(1) { // <<<<< packet_length = read_packet(0); if(packet_length == -1) { fprintf(stderr, "Error(%d) on read packet!\n", packet_length); return -1; } else if(packet_length > 0) { printf("Packet Header: 0x%x...\n", packet_buffer[0]); if(MQTTParseMessageType(packet_buffer) == MQTT_MSG_PUBLISH) { uint8_t topic[255], msg[1000]; uint16_t len; len = mqtt_parse_pub_topic(packet_buffer, topic); topic[len] = '\0'; // for printf len = mqtt_parse_publish_msg(packet_buffer, msg); msg[len] = '\0'; // for printf printf("%s %s\n", topic, msg); } } } return 0; }
void *listener_thread(void *threadId) { int packet_length; uint16_t msg_id, msg_id_rcv; int error = 0; printf("Starting listener thread...\n"); // >>>>> SUBSCRIBE mqtt_subscribe(&broker, "yefsec0rk7uhatp/HighTemp", &msg_id); // <<<<< SUBACK packet_length = read_packet(1); if(packet_length < 0) { fprintf(stderr, "Error(%d) on read packet!\n", packet_length); error = 1; } //printf("%s", packet_buffer); if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_SUBACK) { fprintf(stderr, "SUBACK expected!\n"); error = 1; } //printf("%s", packet_buffer); msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(msg_id != msg_id_rcv) { fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv); error = 1; } //printf("%s", packet_buffer); if (error) { printf("error setting up listener socket\n"); } while(1) { printf("Waiting for message...\n"); packet_length = read_packet(0); if(packet_length == -1) { fprintf(stderr, "Error(%d) on read packet!\n", packet_length); break; } else if(packet_length > 0) { printf("Packet Header: 0x%x...\n", packet_buffer[0]); if(MQTTParseMessageType(packet_buffer) == MQTT_MSG_PUBLISH) { uint8_t topic[255], msg[1000]; uint16_t len; led("g1", 1); len = mqtt_parse_pub_topic(packet_buffer, topic); topic[len] = '\0'; // for printf len = mqtt_parse_publish_msg(packet_buffer, msg); msg[len] = '\0'; // for printf printf("%s %s\n", topic, msg); sleep(1); led("g1", 0); } else if (MQTTParseMessageType(packet_buffer) == MQTT_MSG_PINGRESP) { printf("Ping response received\n"); } } } pthread_exit(NULL); }