static int MqttSample_CmdSubscribe(struct MqttSampleContext *ctx) { int err; char topic[1024]; sprintf(topic, "%s/%s/45523/test-1", ctx->proid, ctx->apikey); err = Mqtt_PackSubscribePkt(ctx->mqttbuf, 11, topic, MQTT_QOS_LEVEL1); if(err != MQTTERR_NOERROR) { printf("Critical bug: failed to pack the subscribe packet.\n"); return -1; } sprintf(topic, "%s/%s/45523/test-2", ctx->proid, ctx->apikey); err = Mqtt_AppendSubscribeTopic(ctx->mqttbuf, topic, MQTT_QOS_LEVEL1); if (err != MQTTERR_NOERROR) { printf("Critical bug: failed to pack the subscribe packet.\n"); return -1; } return 0; }
/** * @brief 设备订阅报文,TOPIC是TOPIC_TO_SUB * @param ctx:上下文变量 * @retval ret:0,成功,<0 失败 **/ static int MqttSample_CmdSubscribe(struct MqttSampleContext *ctx) { int err; int bytes = 0; printf("%s %d\n", __func__, __LINE__); err = Mqtt_PackSubscribePkt(ctx->mqttbuf, 1, TOPIC_TO_SUB, MQTT_QOS_LEVEL1); if(err != MQTTERR_NOERROR) { printf("Critical bug: failed to pack the subscribe packet.\n"); return -1; } printf("%s %d\n", __func__, __LINE__); /* err = Mqtt_AppendSubscribeTopic(ctx->mqttbuf, "433223/Bs04OCJioNgpmvjRphRak15j7Z8=/25267/test-2", MQTT_QOS_LEVEL2); */ if(err != MQTTERR_NOERROR) { printf("Critical bug: failed to append the topic to the " "subscribe packet.\n"); return -1; } bytes = Mqtt_SendPkt(ctx->mqttctx, ctx->mqttbuf, 0); MqttBuffer_Reset(ctx->mqttbuf); return 0; }