Example 2: Upload a file in MQTT data stream mode
You can use the MQTT data stream mode to upload files of an unknown size. This mode is useful for files, such as audio recordings, that are uploaded during capture. The final size of the file is not known when the upload starts.
Procedure
Initialize the MQTT module.
Initialize the MQTT Upload module. In the
mqtt_upload_stream_send_demo.cfile, set the information of the file that you want to upload./* Initialize the MQTT UPLOAD component and create an upload session instance. */ void *up_handle = aiot_mqtt_upload_init(); /* Associate the upload session with the MQTT session. */ aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_MQTT_HANDLE, mqtt_handle); /* (Optional) Set the timeout period for retransmitting a data packet. The default is 5 s. */ uint32_t rsp_timeout = 10000; aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_RSP_TIMEOUT_MS, &rsp_timeout); /* (Optional) Set the number of retries for a timed-out data packet. The default is 10. */ uint32_t rety_count = 5; aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_RETRY_COUNT, &rety_count); /* (Optional) Set the size of each data packet. The default is 2 KB. The minimum size is 256 bytes and the maximum size is 128 KB. */ uint32_t block_size = 256; aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_DEFAULT_BLOCK_SIZE, &block_size); /* Configure the information for the file to upload over MQTT. For a file of unknown size, set the file length to -1 and the read_data_handler callback function to NULL. Use the aiot_mqtt_upload_send_data interface to send data. */ /* When the file length for an MQTT upload is -1, files up to 16 MB can be uploaded. During the upload, set the is_commplete parameter to 1 at any time to finish the upload. */ uint32_t file_size = -1; aiot_mqtt_upload_file_opt_t file_option = { .file_name = MQTT_UPLOAD_SINGLE_FILE_NAME, .file_size = file_size, .mode = AIOT_MQTT_UPLOAD_FILE_MODE_OVERWRITE, .digest = NULL, .read_data_handler = NULL, .userdata = NULL }; aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_FILE_OPTION, &file_option);Request the file upload.
The aiot_mqtt_upload_open_stream interface is a sync call. You must wait for an acknowledgement before you start the upload.
aiot_mqtt_upload_recv_t recv_packet = {0}; res = aiot_mqtt_upload_open_stream(up_handle, MQTT_UPLOAD_SINGLE_FILE_NAME, &recv_packet); if (res < STATE_SUCCESS) { goto exit; }Upload the file shards.
Call the aiot_mqtt_upload_send_data interface to upload the shard data.
The
is_commpleteparameter indicates to IoT Platform whether a packet is the last one for the file upload. Set this parameter to 0 during the upload. When you upload the last data packet, set theis_commpleteparameter to 1.if ((upload_file_len + data_len) < MQTT_UPLOAD_SINGLE_FILE_SIZE) { /* Send the data packet. */ data_len = block_size; is_commplete = 0; } else { /* Send the last packet of the file. */ data_len = MQTT_UPLOAD_SINGLE_FILE_SIZE - upload_file_len; is_commplete = 1; } res = aiot_mqtt_upload_send_data(up_handle, MQTT_UPLOAD_SINGLE_FILE_NAME, &g_upload_single_file_buf[upload_file_len], data_len, is_commplete); if (res == STATE_SUCCESS) { upload_file_len += data_len; printf("upload_file_len:%d\r\n", upload_file_len); }After the upload is complete, destroy the session handle.
/* Destroy the MQTT Upload session instance only after all files are uploaded. */ aiot_mqtt_upload_deinit(&up_handle);
Log description
The MQTT connection is successful.
[1637138922.457][LK-0313] MQTT user calls aiot_mqtt_connect api, connect [1637138922.457][LK-032A] mqtt host: iot-*******.iot-as-mqtt.unify.aliyuncs.com [1637138922.457][LK-0317] user name: mqtt*******************Dih [1637138922.457][LK-0318] password: 2AE57DEEAADE00***********44BA8F5622507D1EFD6CB7687 success to establish tcp, fd=5 local port: 57**9 [1637138922.488][LK-1000] establish mbedtls connection with server(host='io***********t-as-mqtt.unify.aliyuncs.com', port=[1883]) [1637138922.507][LK-1000] success to establish mbedtls connection, (cost 45247 bytes in total, max used 47983 bytes) [1637138922.654][LK-0313] MQTT connect success in 197 ms AIOT_MQTTEVT_CONNECTThe MQTT UPLOAD component creates a session and subscribes to subtopics.
[1637138922.654][LK-2013] aiot_mqtt_upload_init init [1637138922.654][LK-0309] sub: /sys/a1*****bDih/mqttupload0001/thing/file/upload/mqtt/init_reply [1637138922.654][LK-0309] sub: /sys/a1*****bDih/mqttupload0001/thing/file/upload/mqtt/send_reply [1637138922.654][LK-0309] sub: /sys/a1*****bDih/mqttupload0001/thing/file/upload/mqtt/cancel_replyRequest a file upload from IoT Platform.
The demo requests a file upload with a file length of -1, a file name of mqttuploadfile.txt, and the upload mode set to overwrite.
[LK-030A] > 7B 22 69 64 22 3A 22 31 22 2C 22 70 61 72 61 6D | {"id":"1","param [LK-030A] > 73 22 3A 7B 22 66 69 6C 65 4E 61 6D 65 22 3A 22 | s":{"fileName":" [LK-030A] > 6D 71 74 74 75 70 6C 6F 61 64 66 69 6C 65 2E 74 | mqttuploadfile.t [LK-030A] > 78 74 22 2C 22 66 69 6C 65 53 69 7A 65 22 3A 2D | xt","fileSize":- [LK-030A] > 31 2C 22 63 6F 6E 66 6C 69 63 74 53 74 72 61 74 | 1,"conflictStart [LK-030A] > 65 67 79 22 3A 22 6F 76 65 72 77 72 69 74 65 22 | egy":"overwrite" [LK-030A] > 2C 22 69 6E 69 74 55 69 64 22 3A 22 31 36 34 34 | ,"initUid":"1644 [LK-030A] > 32 31 39 33 37 33 33 33 39 37 35 22 7D 7D | 2193********5"}} heartbeat responseAn acknowledgement message is received from IoT Platform.
[1644219373.441][LK-0309] pub: /sys/a1P*****Dih/mqttupload0002/thing/file/upload/mqtt/init_reply [LK-030A] < 7B 22 63 6F 64 65 22 3A 32 30 30 2C 22 64 61 74 | {"code":200,"dat [LK-030A] < 61 22 3A 7B 22 66 69 6C 65 4E 61 6D 65 22 3A 22 | a":{"fileName":" [LK-030A] < 6D 71 74 74 75 70 6C 6F 61 64 66 69 6C 65 2E 74 | mqttuploadfile.t [LK-030A] < 78 74 22 2C 22 75 70 6C 6F 61 64 49 64 22 3A 22 | xt","uploadId":" [LK-030A] < 64 4E 48 79 33 35 4F 55 4A 66 64 4A 49 31 6B 31 | dNH*********dJI1k1 [LK-030A] < 6D 31 62 55 30 31 30 32 30 30 22 2C 22 6F 66 66 | m1bU010200","off [LK-030A] < 73 65 74 22 3A 30 2C 22 66 69 6C 65 53 69 7A 65 | set":0,"fileSize [LK-030A] < 22 3A 2D 31 2C 22 63 6F 6E 66 6C 69 63 74 53 74 | ":-1,"conflictSt [LK-030A] < 72 61 74 65 67 79 22 3A 22 6F 76 65 72 77 72 69 | rategy":"overwri [LK-030A] < 74 65 22 7D 2C 22 69 64 22 3A 22 31 22 2C 22 6D | te"},"id":"1","m [LK-030A] < 65 73 73 61 67 65 22 3A 22 73 75 63 63 65 73 73 | essage":"success [LK-030A] < 22 7D | "} [1644219373.441][LK-210B] Recv file name:mqttuploadfile.txtSend the file shards.
Repeatedly call the aiot_mqtt_upload_send_data function to upload data. This function sends data synchronously. You must wait for the STATE_SUCCESS acknowledgement before you upload the next data packet.
[1644219373.462][LK-0309] pub: /sys/a1*****Dih/mqttupload0002/thing/file/upload/mqtt/send [LK-030A] > 00 67 7B 22 69 64 22 3A 22 32 22 2C 22 70 61 72 | .g{"id":"2","par [LK-030A] > 61 6D 73 22 3A 7B 22 75 70 6C 6F 61 64 49 64 22 | ams":{"uploadId" [LK-030A] > 3A 22 64 4E 48 79 33 35 4F 55 4A 66 64 4A 49 31 | :"dN*****(fdJI1 [LK-030A] > 6B 31 6D 31 62 55 30 31 30 32 30 30 22 2C 22 6F | k1m1bU010200","o [LK-030A] > 66 66 73 65 74 22 3A 30 2C 22 62 53 69 7A 65 22 | ffset":0,"bSize" [LK-030A] > 3A 32 35 36 2C 22 69 73 43 6F 6D 70 6C 65 74 65 | :256,"isComplete [LK-030A] > 22 3A 66 61 6C 73 65 7D 7D 00 00 00 00 00 00 00 | ":false}}....... [LK-030A] > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ [LK-030A] > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ [LK-030A] > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ [LK-030A] > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ ...... ...... ......End the file upload.
When you upload the last data packet, set the
isCompletefield to 1. This indicates to IoT Platform that this is the last packet and completes the file upload.[1644219373.734][LK-0309] pub: /sys/a1*****Dih/mqttupload0002/thing/file/upload/mqtt/send [LK-030A] > 00 68 7B 22 69 64 22 3A 22 35 22 2C 22 70 61 72 | .h{"id":"5","par [LK-030A] > 61 6D 73 22 3A 7B 22 75 70 6C 6F 61 64 49 64 22 | ams":{"uploadId" [LK-030A] > 3A 22 64 4E 48 79 33 35 4F 55 4A 66 64 4A 49 31 | :"dN*******dJI1 [LK-030A] > 6B 31 6D 31 62 55 30 31 30 32 30 30 22 2C 22 6F | k1m1bU010200","o [LK-030A] > 66 66 73 65 74 22 3A 37 36 38 2C 22 62 53 69 7A | ffset":768,"bSiz [LK-030A] > 65 22 3A 32 35 36 2C 22 69 73 43 6F 6D 70 6C 65 | e":256,"isComple [LK-030A] > 74 65 22 3A 74 72 75 65 7D 7D 00 00 00 00 00 00 | te":true}}...... [LK-030A] > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................ ...... ...... ...... [1644219373.822][LK-0309] pub: /sys/a1*******Dih/mqttupload0002/thing/file/upload/mqtt/send_reply [LK-030A] < 7B 22 63 6F 64 65 22 3A 32 30 30 2C 22 64 61 74 | {"code":200,"dat [LK-030A] < 61 22 3A 7B 22 66 69 63 4D 6F 64 65 22 3A 22 63 | a":{"ficMode":"c [LK-030A] < 72 63 36 34 22 2C 22 75 70 6C 6F 61 64 49 64 22 | rc64","uploadId" [LK-030A] < 3A 22 64 4E 48 79 33 35 4F 55 4A 66 64 4A 49 31 | :"dNHy*******JI1 [LK-030A] < 6B 31 6D 31 62 55 30 31 30 32 30 30 22 2C 22 6F | k1m1bU010200","o [LK-030A] < 66 66 73 65 74 22 3A 37 36 38 2C 22 63 6F 6D 70 | ffset":768,"comp [LK-030A] < 6C 65 74 65 22 3A 74 72 75 65 2C 22 66 69 63 56 | lete":true,"ficV [LK-030A] < 61 6C 75 65 53 65 72 76 65 72 22 3A 22 63 33 37 | alueServer":"c37 [LK-030A] < 38 36 33 39 37 32 30 36 39 32 37 30 63 22 2C 22 | 863972069270c"," [LK-030A] < 62 53 69 7A 65 22 3A 32 35 36 7D 2C 22 69 64 22 | bSize":256},"id" [LK-030A] < 3A 22 35 22 2C 22 6D 65 73 73 61 67 65 22 3A 22 | :"5","message":" [LK-030A] < 73 75 63 63 65 73 73 22 7D | success"} [1644219373.822][LK-2125] Complete upload total size:1024 upload_file_len:1024 [1644219373.830][LK-2100] Finish upload,Deleted upload task.MQTT Upload file(mqttuploadfile.txt) uploadid(dNHy35OU*********1bU010200) success.After the upload is complete, destroy the handle to release resources.
[1644219378.827][LK-210F] aiot_mqtt_upload_deinit [1644219378.827][LK-0309] unsub: /sys/a1P8*****ih/mqttupload0002/thing/file/upload/mqtt/init_reply [1644219378.827][LK-0309] unsub: /sys/a1P8*****ih/mqttupload0002/thing/file/upload/mqtt/send_reply [1644219378.827][LK-0309] unsub: /sys/a1P8*****ih/mqttupload0002/thing/file/upload/mqtt/cancel_reply [1644219378.827][LK-1000] adapter_network_deinit