Guest User

Untitled

a guest
Dec 11th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "MQTTClient.h"
  5. #include "../tools.c"
  6. #include <cjson/cJSON.h>
  7.  
  8. #define ADDRESS "tcp://localhost:1883"
  9. #define CLIENTID "MY_PUB"
  10. #define TOPIC "MQTT/Test"
  11.  
  12. int main(int argc, char* argv[])
  13. {
  14.  
  15. frame1 test = {42,"test"};
  16.  
  17. cJSON* frm = NULL;
  18. frm = cJSON_CreateObject();
  19.  
  20. cJSON_AddNumberToObject(frm,"entier",test.E);
  21. cJSON_AddStringToObject(frm,"string",test.S);
  22.  
  23. print_frame1(frm);
  24.  
  25. int i = cJSON_GetArraySize(frm);
  26. printf("number of items in frame : %dn",i);
  27. cJSON* entier = NULL;
  28. entier = cJSON_GetObjectItem(frm,"entier");
  29. char* pt = cJSON_Print(entier);
  30. printf("entier : %sn",pt);
  31. cJSON* str = NULL;
  32. str = cJSON_GetObjectItem(frm,"string");
  33. char* st = cJSON_Print(str);
  34. printf("string : %sn",st);
  35.  
  36. printf("size of message : %dn",sizeof(cJSON));
  37.  
  38. MQTTClient publisher;
  39. MQTTClient_connectOptions connexion = MQTTClient_connectOptions_initializer;
  40.  
  41. MQTTClient_message msg = msg_creation(frm,sizeof(cJSON),0,0);
  42. MQTTClient_deliveryToken token;
  43. int rc;
  44. MQTTClient_create(&publisher, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  45. connexion.cleansession = 1;
  46. MQTTClient_setCallbacks(publisher, NULL, connlost, frame_json_arrvd, NULL);
  47. if ((rc = MQTTClient_connect(publisher,&connexion)) != MQTTCLIENT_SUCCESS)
  48. {
  49. printf("Failed to connect, return code %dn", rc);
  50. }
  51. MQTTClient_publishMessage(publisher, TOPIC,&msg,NULL);
  52. printf("Message sent!n");
  53. cJSON_Delete(frm);
  54. MQTTClient_disconnect(publisher,10000);
  55. MQTTClient_destroy(&publisher);
  56. return rc;
  57. }
  58.  
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include "MQTTClient.h"
  63. #include "../tools.c"
  64. #include <cjson/cJSON.h>
  65.  
  66.  
  67. #define ADDRESS "tcp://localhost:1883"
  68. #define CLIENTID "ClientID"
  69. #define TOPIC "MQTT/Test"
  70. #define QOS 0
  71. #define TIMEOUT 10000L
  72.  
  73. int main(int argc, char* argv[])
  74. {
  75. MQTTClient client;
  76. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  77. int rc;
  78. int ch;
  79. MQTTClient_create(&client, ADDRESS, CLIENTID,
  80. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  81. conn_opts.keepAliveInterval = 20;
  82. conn_opts.cleansession = 1;
  83. MQTTClient_setCallbacks(client, NULL, connlost, frame_json_arrvd, NULL);
  84. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  85. {
  86. printf("Failed to connect, return code %dn", rc);
  87. exit(EXIT_FAILURE);
  88. }
  89. printf("Subscribing to topic %snfor client %s using QoS%dnn"
  90. "Press Q<Enter> to quitnn", TOPIC, CLIENTID, QOS);
  91. MQTTClient_subscribe(client, TOPIC, QOS);
  92. do
  93. {
  94. ch = getchar();
  95. } while(ch!='Q' && ch != 'q');
  96. MQTTClient_disconnect(client, 10000);
  97. MQTTClient_destroy(&client);
  98. return rc;
  99. }
  100.  
  101. #include <stdio.h>
  102. #include <stdlib.h>
  103. #include <string.h>
  104. #include <time.h>
  105. #include <sys/time.h>
  106. #include <cjson/cJSON.h>
  107. #include "MQTTClient.h"
  108.  
  109. int compteur;
  110.  
  111. // message frames
  112.  
  113. typedef struct frame1
  114. {
  115. int E;
  116. char* S;
  117. } frame1;
  118.  
  119. void print_frame1bis(frame1* F)
  120. {
  121. printf(" %sn",F->S);
  122. printf(" %dn",F->E);
  123. }
  124.  
  125. void print_frame1(cJSON* frame1)
  126. {
  127. char * str = cJSON_Print(frame1);
  128. printf("%sn",str);
  129. }
  130.  
  131. int frame_json_arrvd(void* context, char* topicName, int topicLen, MQTTClient_message* msg)
  132. {
  133.  
  134. cJSON* payload_ptr = NULL;
  135.  
  136. printf("size of message : %dn",sizeof(msg->payload));
  137.  
  138. int j = cJSON_GetArraySize(msg->payload);
  139. printf("okn");
  140. printf("number of items in frame : %dn",j);
  141.  
  142. payload_ptr = cJSON_CreateObject();
  143. payload_ptr = msg->payload;
  144.  
  145. int i = cJSON_GetArraySize(payload_ptr);
  146. printf("number of items in frame : %dn",i);
  147.  
  148. print_frame1(payload_ptr);
  149.  
  150. cJSON_Delete(payload_ptr);
  151. MQTTClient_freeMessage(&msg);
  152. MQTTClient_free(topicName);
  153. return 1;
  154. }
  155.  
  156. // in case connexion is lost
  157.  
  158. void connlost(void *context, char *cause)
  159. {
  160. printf("nConnection lostn");
  161. printf(" cause: %sn", cause);
  162. }
  163.  
  164.  
  165. // create message
  166.  
  167. MQTTClient_message msg_creation(void* payload, int length, int qos, int retained)
  168. {
  169. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  170. pubmsg.payload = payload;
  171. pubmsg.payloadlen = length;
  172. pubmsg.qos = qos;
  173. pubmsg.retained = retained;
  174. return pubmsg;
  175. }
  176.  
  177. Subscribing to topic MQTT/Test
  178. for client ClientID using QoS0
  179.  
  180. Press Q<Enter> to quit
  181.  
  182. size of message : 8
  183. Segmentation fault (core dumped)
  184.  
  185. Program terminated with signal 11, Segmentation fault.
  186. #0 0x00007f4e49a8b63e in cJSON_GetArraySize () from /usr/lib64/libcjson.so.1
  187. (gdb) bt
  188. #0 0x00007f4e49a8b63e in cJSON_GetArraySize () from /usr/lib64/libcjson.so.1
  189. #1 0x00000000004010c9 in frame_json_arrvd (context=0x0,
  190. topicName=0x7f4e440009e4 "MQTT/Test", topicLen=0, msg=0x7f4e44000bb4)
  191. at ../tools.c:110
  192. #2 0x00007f4e49c962a5 in MQTTClient_run (n=<value optimized out>)
  193. at src/MQTTClient.c:604
  194. #3 0x000000378b807aa1 in start_thread (arg=0x7f4e49a83700) at pthread_create.c:301
  195. #4 0x000000378b4e8bcd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:115
Add Comment
Please, Sign In to add comment