11-prop-send-payload-format.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <mosquitto.h>
  6. #include <mqtt_protocol.h>
  7. static int run = -1;
  8. static int sent_mid = -1;
  9. void on_connect(struct mosquitto *mosq, void *obj, int rc)
  10. {
  11. int rc2;
  12. mosquitto_property *proplist = NULL;
  13. if(rc){
  14. exit(1);
  15. }else{
  16. rc2 = mosquitto_property_add_byte(&proplist, MQTT_PROP_PAYLOAD_FORMAT_INDICATOR, 1);
  17. mosquitto_publish_v5(mosq, &sent_mid, "prop/qos0", strlen("message"), "message", 0, false, proplist);
  18. mosquitto_property_free_all(&proplist);
  19. }
  20. }
  21. void on_publish(struct mosquitto *mosq, void *obj, int mid)
  22. {
  23. if(mid == sent_mid){
  24. mosquitto_disconnect(mosq);
  25. run = 0;
  26. }else{
  27. exit(1);
  28. }
  29. }
  30. int main(int argc, char *argv[])
  31. {
  32. int rc;
  33. int tmp;
  34. struct mosquitto *mosq;
  35. int port = atoi(argv[1]);
  36. mosquitto_lib_init();
  37. mosq = mosquitto_new("prop-test", true, NULL);
  38. if(mosq == NULL){
  39. return 1;
  40. }
  41. mosquitto_connect_callback_set(mosq, on_connect);
  42. mosquitto_publish_callback_set(mosq, on_publish);
  43. tmp = MQTT_PROTOCOL_V5;
  44. mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &tmp);
  45. rc = mosquitto_connect(mosq, "localhost", port, 60);
  46. while(run == -1){
  47. rc = mosquitto_loop(mosq, -1, 1);
  48. }
  49. mosquitto_destroy(mosq);
  50. mosquitto_lib_cleanup();
  51. return run;
  52. }