int main(int argc, char** argv) { int rc = 0; unsigned char buf[1024]; unsigned char readbuf[1024]; Network n; Client c; signal(SIGINT, cfinish); signal(SIGTERM, cfinish); NewNetwork(&n); ConnectNetwork(&n, "eu.airvantage.net", 1883); MQTTClient(&c, &n, 1000, buf, 1024, readbuf, 1024); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.willFlag = 0; data.MQTTVersion = 3; data.clientID.cstring = deviceId; data.username.cstring = "00000000B6AF4A9D"; data.password.cstring = "toto"; data.keepAliveInterval = 10000; data.cleansession = 1; printf("Connecting to %s %d\n", "tcp://eu.airvantage.net", 1883); rc = MQTTConnect(&c, &data); printf("Connected %d\n", rc); char* topic = malloc(strlen(deviceId)+13); sprintf(topic, "%s/tasks/json", deviceId); printf("Subscribing to %s\n", topic); rc = MQTTSubscribe(&c, topic, 0, messageArrived); printf("Subscribed %d\n", rc); int err1 = pthread_create(&reading_tid, NULL, &readMeasuresHandler, (void*)&c); int err2 = pthread_create(&uploading_tid, NULL, &uploadMeasuresHandler, (void*) &c); if(err1 != 0 || err2 != 0) printf("Can't create simulator (%s, %d)\n", strerror(err1), err2); else { while (!toStop) { MQTTYield(&c, 1000); } } printf("Stopping\n"); MQTTDisconnect(&c); n.disconnect(&n); return 0; }
IoT_Error_t aws_iot_mqtt_disconnect() { IoT_Error_t rc = NONE_ERROR; if(0 != MQTTDisconnect(&c)) { rc = DISCONNECT_ERROR; } return rc; }
int disconnect(Iotfclient *client) { int rc = 0; rc = MQTTDisconnect(&client->c); client->n.disconnect(&client->n); return rc; }
int mqttClient_stopRunning(AJBMqttClient *client){ int rc = FAILURE; if (client->isConnected && client->keepworking==0) { MqttLog("[DISCONNECT %s]",client->clientId); client->isConnected = false; rc = MQTTDisconnect(&client->c); client->n.disconnect(&client->n); } client->keepworking = 0; return rc; }
int keepalive(Client* c) { int rc = FAILURE; if (c->keepAliveInterval == 0) { return SUCCESS; } if (expired(&c->ping_timer)) { if (!c->ping_outstanding) { // there is not a ping outstanding - send one Timer timer; InitTimer(&timer); countdown_ms(&timer, 2000); //STM: modified the value from 1000 to 2000 int len = MQTTSerialize_pingreq(c->buf, c->buf_size); INFO("-->keepalive"); rc = sendPacket(c, len, &timer); // send the ping packet DeInitTimer(&timer); //STM: added this line if (len > 0 && rc == SUCCESS) { c->ping_outstanding = 1; // start a timer to wait for PINGRESP from server countdown(&c->ping_timer, c->keepAliveInterval / 2); } else { rc = FAILURE; } } else { // timer expired while waiting for a ping - decide we are disconnected MQTTDisconnect(c); if (c->disconnectHandler != NULL) { c->disconnectHandler(); } } } else { rc = SUCCESS; } return rc; }
static enum cmd_status cmd_mqtt_disconnect_exec(char *cmd) { int32_t cnt; uint32_t tcponly; int rc; uint32_t i; /* get param */ cnt = cmd_sscanf(cmd, "tcponly=%u", &tcponly); /* check param */ if (cnt != 1) { CMD_ERR("invalid param number %d\n", cnt); return CMD_STATUS_INVALID_ARG; } if (tcponly > 1) { CMD_ERR("invalid tcponly %d\n", tcponly); return CMD_STATUS_INVALID_ARG; } OS_ThreadDelete(&mqtt_bg_thread); i = 0; while (i < MAX_SUB_TOPICS) { if (sub_topic[i] != NULL) { cmd_free(sub_topic[i]); sub_topic[i] = NULL; } i++; } if (tcponly == 0) { if ((rc = MQTTDisconnect(&client)) != 0){ CMD_ERR("Return code from MQTT disconnect is %d\n", rc); network.disconnect(&network); return CMD_STATUS_FAIL; } } else { network.disconnect(&network); } return CMD_STATUS_OK; }
int Cpush_disconnect(unsigned int clientHandle) { if (0 == clientHandle) { return CPUSH_RET_ERROR_PARA; } Client* client = (Client*)clientHandle; if (SUCCESS != MQTTDisconnect(client)) { return CPUSH_RET_FAIL; } if (0 != client->ipstack->my_socket) {client->ipstack->disconnect(client->ipstack);} if (client->ipstack) {free(client->ipstack);} if (client->buf) {free(client->buf);} if (client->readbuf) {free(client->readbuf);} if (client) {free(client);} return CPUSH_RET_OK; }
int main() { log_printf(LOG_VERBOSE, "[StartProgram]...........\n"); log_printf(LOG_VERBOSE, "[HW-VERSION]: %s\n", HW_VERSION); log_printf(LOG_VERBOSE, "[SW_VERSION]: %s\n", SW_VERSION); log_printf(LOG_NOTICE, "COMPILE_TIME[%s: %s]\n", __DATE__, __TIME__); #ifdef USE_IN_ARM sleep(10); #endif unsigned char buf[MQTT_PLAYLOADLEN]; unsigned char readbuf[MQTT_PLAYLOADLEN]; signal(SIGINT, cfinish); signal(SIGTERM, cfinish); global_loop = EV_DEFAULT; global_ev_all.mainloop = global_loop; Network n; //必须 //mqtt network实体 gateway_setting_init(); gwmsg_init(); devlist_gateway_msg_init(); // dev_init(); NewNetwork(&n); MQTTClient(&global_ev_all.client, &n, KEEPALIVE_INTERVAL, buf, MQTT_PLAYLOADLEN, readbuf, MQTT_PLAYLOADLEN); mqtt_init(); zigbee_init(); ev_run(global_ev_all.mainloop, 0); //run... log_printf(LOG_ERROR, "[Stopping]...\n"); MQTTDisconnect(&global_ev_all.client); n.disconnect(&n); return 0; }
int main(int argc, char** argv) { int rc = 0; unsigned char buf[100]; unsigned char readbuf[100]; /**********UART************/ int fd = FALSE; int ret; unsigned char rcv_buf[512]; unsigned char send_buf[64]; Data_t rcv_data; int i; int j; cJSON *root,*img,*thm; char *out; /* Our "gallery" item: */ root=cJSON_CreateObject(); cJSON_AddItemToObject(root, "Image", img=cJSON_CreateObject()); cJSON_AddNumberToObject(img,"Width",800); cJSON_AddNumberToObject(img,"Height",600); cJSON_AddStringToObject(img,"Title","View from 15th Floor"); cJSON_AddItemToObject(img, "Thumbnail", thm=cJSON_CreateObject()); cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943"); cJSON_AddNumberToObject(thm,"Height",125); cJSON_AddStringToObject(thm,"Width","100"); out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Print to text, Delete the cJSON, print it, release the string. */ fd = UART_Open(fd,"/dev/ttyS1"); if(FALSE == fd){ printf("open error\n"); exit(1); } ret = UART_Init(fd,115200,0,8,1,'N'); if (FALSE == fd){ printf("Set Port Error\n"); exit(1); } ret = UART_Send(fd,"*IDN?\n",6); if(FALSE == ret){ printf("write error!\n"); exit(1); } printf("command: %s\n","*IDN?"); memset(rcv_buf,0,sizeof(rcv_buf)); if (argc < 2) usage(); char* topic = argv[1]; // if (strchr(topic, '#') || strchr(topic, '+')) // opts.showtopics = 1; // if (opts.showtopics) // printf("topic is %s\n", topic); getopts(argc, argv); Network n; Client c; MQTTMessage message={0,0,0,0,"hello",5};//dup,qos,retained,packetid,payload,payloadlen NewNetwork(&n); ConnectNetwork(&n, opts.host, opts.port); MQTTClient(&c, &n, 1000, buf, 100, readbuf, 100); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.willFlag = 0; data.MQTTVersion = 3; data.clientID.cstring = opts.clientid; data.username.cstring = opts.username; data.password.cstring = opts.password; data.keepAliveInterval = 10; data.cleansession = 1; printf("Connecting to %s %d\n", opts.host, opts.port); rc = MQTTConnect(&c, &data); printf("Connected %d\n", rc); //test while(1) { message.payload = "efewewreweefer"; message.payloadlen = strlen(message.payload); MQTTPublish(&c,topic,&message); } while (!toStop) { MQTTYield(&c, 1000); } printf("Stopping\n"); UART_Close(fd); MQTTDisconnect(&c); n.disconnect(&n); return 0; }
int main(int argc, char** argv) { int rc = 0; struct pando_buffer *bin_buf; uint16_t payload_type = 0; MQTTMessage msg; unsigned char buf[500]; unsigned char readbuf[500]; char payload_type_c[] = {'c', 'e', 'd'}; bin_buf = construct_data_package_to_server(&payload_type); decode_data(bin_buf, PAYLOAD_TYPE_DATA); /* command test, event is same as command except the type */ bin_buf = constuct_event_package_to_server(&payload_type); decode_command(bin_buf, PAYLOAD_TYPE_COMMAND); return 0; if (argc < 2) usage(); char* topic = argv[1]; getopts(argc, argv); if (strchr(topic, '#') || strchr(topic, '+')) opts.showtopics = 1; if (opts.showtopics) printf("topic is %s\n", topic); Network n; Client c; signal(SIGINT, cfinish); signal(SIGTERM, cfinish); NewNetwork(&n); ConnectNetwork(&n, opts.host, opts.port); MQTTClient(&c, &n, 1000, buf, 100, readbuf, 100); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.willFlag = 0; data.MQTTVersion = 3; data.clientID.cstring = opts.clientid; data.username.cstring = opts.username; data.password.cstring = opts.password; data.keepAliveInterval = 10; data.cleansession = 1; printf("Connecting to %s %d\n", opts.host, opts.port); rc = MQTTConnect(&c, &data); printf("Connected %d\n", rc); printf("Subscribing to %s\n", topic); rc = MQTTSubscribe(&c, topic, opts.qos, messageArrived); printf("Subscribed %d\n", rc); while (!toStop) { MQTTYield(&c, 1000); if (toSend) { if (strstr(topic, "/d")) { /* data test */ bin_buf = construct_data_package_to_server(&payload_type); //decode_data(bin_buf, PAYLOAD_TYPE_DATA); } if (strstr(topic, "/e")) { /* command test */ bin_buf = constuct_event_package_to_server(&payload_type); //decode_command(bin_buf, PAYLOAD_TYPE_COMMAND); } msg.qos = opts.qos; msg.retained = 0; msg.dup = 0; msg.payload = bin_buf->buffer + bin_buf->offset; msg.payloadlen = bin_buf->buff_len - bin_buf->offset; /* before publish a message, you need to generate a topic with payload_type max device id is 8 bytes, hex to char need 8*2 bytes, '/' need 1 byte, type need 1 byte */ char publish_topic[8*2 + 1 + 1]; memset(publish_topic, 0, sizeof(publish_topic)); sprintf(publish_topic, "%s/%c", opts.clientid, payload_type_c[payload_type - 1]); printf(publish_topic); rc = MQTTPublish(&c, publish_topic, &msg); toSend = 0; pd_free(bin_buf); printf("published rc %d, len %d\n", rc, (int)msg.payloadlen); } } printf("Stopping\n"); MQTTDisconnect(&c); n.disconnect(&n); return 0; }