Advertisement
Guest User

Untitled

a guest
Jan 28th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.75 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include <inttypes.h>
  7. #include <netinet/in.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10.  
  11. #include <MQTTClient.h>
  12.  
  13. #define ADDRESS "tcp://localhost:1883"
  14. #define QOS0 0
  15. #define QOS1 1
  16. #define QOS2 2
  17. #define TIMEOUT 10000L
  18.  
  19. volatile MQTTClient_deliveryToken deliveredtoken;
  20.  
  21. void rtfm(char **argv) {
  22.     printf("Usage: %s hamster_client_id {<Option>} <param1> \n", argv[0]);
  23.     printf("Function: Hamster instrumentation device software\n");
  24.     printf("Optionen:\n");
  25.     printf("     -p {<port>}         - port of the mqtt server (default (no tls): 1883\n");
  26.     printf("     -s {<IP address>}   - IP address to run the server on (default: 127.0.0.1\n");
  27.     printf("     -v                  - Connect with Certificate based SSL/TLS Support to the MQTT server \n");
  28.     printf("     -V                  - Connect with Certificate based client authentification to the MQTT server \n");
  29.     printf("     -h                  - This help \n");
  30. }
  31.  
  32. void delivered(void *context, MQTTClient_deliveryToken dt) {
  33.     printf("Message with token value %d delivery confirmed\n", dt);
  34.     deliveredtoken = dt;
  35. }
  36. int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message) {
  37.     int i;
  38.     char *payloadptr;
  39.     printf("Message arrived in topic: %s\n", topicName);
  40.     printf("    Message: ");
  41.     payloadptr = message->payload;
  42.     for (i = 0; i < message->payloadlen; i++) {
  43.         putchar(*payloadptr++);
  44.     }
  45.     putchar('\n');
  46.     MQTTClient_freeMessage(&message);
  47.     MQTTClient_free(topicName);
  48.     return 1;
  49. }
  50. void connlost(void *context, char *cause) {
  51.     printf("\nConnection lost: %s\n", cause);
  52. }
  53.  
  54. void pubnewmsg(MQTTClient* client, char* clientid, char* topic, char* payload) {
  55.     MQTTClient_message pubmsg = MQTTClient_message_initializer;
  56.     MQTTClient_deliveryToken token;
  57.     pubmsg.payload = payload;
  58.     pubmsg.payloadlen = strlen(payload);
  59.     pubmsg.qos = QOS0;
  60.     pubmsg.retained = 0;
  61.     MQTTClient_publishMessage(client, topic, &pubmsg, &token);
  62.     printf("Send message to topic %s for hamster: %s\n", topic, clientid);
  63.     MQTTClient_waitForCompletion(client, token, TIMEOUT);
  64.     printf("Message successfully delivered (token %d)\n", token);
  65. }
  66.  
  67. int main(int argc, char **argv) {
  68.     unsigned int port = 1883;
  69.     char *clientid;
  70.     char topic[256];
  71.  
  72.     if (argc < 2) {
  73.         rtfm(argv);
  74.         exit(0);
  75.     }
  76.  
  77.     // parse args
  78.     clientid = argv[1];
  79.     for (int i = 1; i < argc; i++) {
  80.         if (strcmp("-p", argv[i]) == 0) {
  81.             char *end;
  82.             port = strtoul(argv[i + 1], &end, 0);
  83.             if (argv[i + 1] == end) {
  84.                 printf("%s: Not a number: %s\n", argv[0], argv[i + 1]);
  85.                 exit(1);
  86.             }
  87.         } else if (strcmp("-s", argv[i]) == 0) {
  88.             // todo
  89.         } else if (strcmp("-h", argv[i]) == 0) {
  90.             rtfm(argv);
  91.             exit(0);
  92.         } else if (strcmp("-v", argv[i]) == 0) {
  93.             // todo
  94.         } else if (strcmp("-V", argv[i]) == 0) {
  95.         }
  96.     }
  97.  
  98.     if (port) {
  99.     }
  100.  
  101.     MQTTClient client;
  102.     MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  103.     int rc;
  104.     int ch;
  105.  
  106.     MQTTClient_create(&client, ADDRESS, clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  107.     conn_opts.keepAliveInterval = 20;
  108.     conn_opts.cleansession = 1;
  109.     conn_opts.username = "hamster";
  110.     conn_opts.password = "";
  111.  
  112.     MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
  113.  
  114.     if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
  115.         printf("Failed to connect, return code %d\n", rc);
  116.         exit(EXIT_FAILURE);
  117.     }
  118.  
  119.     MQTTClient_subscribe(client, "/pension/livestock", QOS2); // exactly once
  120.     printf("Subscriped to topic: /pension/livestock\n");
  121.     MQTTClient_subscribe(client, "/pension/room/A", QOS0); // best effort ??
  122.     printf("Subscriped to topic: /pension/room/A\n");
  123.     MQTTClient_subscribe(client, "/pension/room/B", QOS0); // best effort ??
  124.     printf("Subscriped to topic: /pension/room/B\n");
  125.     MQTTClient_subscribe(client, "/pension/room/C", QOS0); // best effort ??
  126.     printf("Subscriped to topic: /pension/room/C\n");
  127.     MQTTClient_subscribe(client, "/pension/room/D", QOS0); // best effort ??
  128.     printf("Subscriped to topic: /pension/room/D\n");
  129.     sprintf(topic, "/pension/hamster/%s/wheels%c", clientid, '\0');
  130.     printf("Subscriped to topic: %s\n", topic);
  131.     MQTTClient_subscribe(client, topic, QOS0); // best effort ??
  132.     sprintf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
  133.     printf("Subscriped to topic: %s\n", topic);
  134.     MQTTClient_subscribe(client, topic, QOS1);  // min once
  135.     sprintf(topic, "/pension/hamster/%s/position%c", clientid, '\0');
  136.     printf("Subscriped to topic: %s\n", topic);
  137.     MQTTClient_subscribe(client, topic, QOS1); // min once
  138.     sprintf(topic, "/pension/hamster/%s/fondle%c", clientid, '\0');
  139.     printf("Subscriped to topic: %s\n", topic);
  140.     MQTTClient_subscribe(client, topic, QOS2); // exactly once
  141.     sprintf(topic, "/pension/hamster/%s/punish%c", clientid, '\0');
  142.     printf("Subscriped to topic: %s\n", topic);
  143.     MQTTClient_subscribe(client, topic, QOS2); // exactly once
  144.  
  145.     do {
  146.         ch = getchar();
  147.         if (ch == 'l' || ch == 'L') {
  148.             pubnewmsg(client, clientid, "/pension/livestock", "payload");
  149.         }
  150.         if (ch == 's' || ch == 'S') {
  151.             sprintf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
  152.             pubnewmsg(client, clientid, topic, "payload");
  153.         }
  154.     } while (ch != 'Q' && ch != 'q');
  155.     MQTTClient_disconnect(client, 10000);
  156.     MQTTClient_destroy(&client);
  157.  
  158.     return rc;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement