03-publish-b2c-qos2-len.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <mosquitto.h>
  6. static int run = -1;
  7. void on_connect(struct mosquitto *mosq, void *obj, int rc)
  8. {
  9. if(rc){
  10. exit(1);
  11. }
  12. }
  13. void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
  14. {
  15. if(msg->mid != 56){
  16. printf("Invalid mid (%d)\n", msg->mid);
  17. exit(1);
  18. }
  19. if(msg->qos != 2){
  20. printf("Invalid qos (%d)\n", msg->qos);
  21. exit(1);
  22. }
  23. if(strcmp(msg->topic, "len/qos2/test")){
  24. printf("Invalid topic (%s)\n", msg->topic);
  25. exit(1);
  26. }
  27. if(strcmp(msg->payload, "message")){
  28. printf("Invalid payload (%s)\n", (char *)msg->payload);
  29. exit(1);
  30. }
  31. if(msg->payloadlen != 7){
  32. printf("Invalid payloadlen (%d)\n", msg->payloadlen);
  33. exit(1);
  34. }
  35. if(msg->retain != false){
  36. printf("Invalid retain (%d)\n", msg->retain);
  37. exit(1);
  38. }
  39. mosquitto_disconnect(mosq);
  40. }
  41. void on_disconnect(struct mosquitto *mosq, void *obj, int rc)
  42. {
  43. run = 0;
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. int rc;
  48. struct mosquitto *mosq;
  49. int port = atoi(argv[1]);
  50. mosquitto_lib_init();
  51. mosq = mosquitto_new("publish-qos2-test", true, &run);
  52. if(mosq == NULL){
  53. return 1;
  54. }
  55. mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  56. mosquitto_connect_callback_set(mosq, on_connect);
  57. mosquitto_disconnect_callback_set(mosq, on_disconnect);
  58. mosquitto_message_callback_set(mosq, on_message);
  59. rc = mosquitto_connect(mosq, "localhost", port, 60);
  60. while(run == -1){
  61. mosquitto_loop(mosq, 100, 1);
  62. }
  63. mosquitto_destroy(mosq);
  64. mosquitto_lib_cleanup();
  65. return run;
  66. }