Advertisement
Guest User

Untitled

a guest
Jan 28th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.14 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 DEFAULT_ADDRESS "127.0.0.1"
  14. #define DEFAULT_PORT 1883
  15. #define ROOM_A "A"
  16. #define ROOM_B "B"
  17. #define ROOM_C "C"
  18. #define ROOM_D "D"
  19. #define RUNNING "RUNNING"
  20. #define SLEEPING "SLEEPING"
  21. #define EATING "EATING"
  22. #define MATEING "MATEING"
  23. #define QOS0 0
  24. #define QOS1 1
  25. #define QOS2 2
  26. #define TIMEOUT 10000L
  27.  
  28. #define DEBUG 0
  29.  
  30. volatile MQTTClient_deliveryToken deliveredtoken;
  31.  
  32. void rtfm(char **argv) {
  33.     printf("Usage: %s hamster_client_id {<Option>} <param1> \n", argv[0]);
  34.     printf("Function: Hamster instrumentation device software\n");
  35.     printf("Optionen:\n");
  36.     printf("     -p {<port>}         - port of the mqtt server (default (no tls): 1883\n");
  37.     printf("     -s {<IP address>}   - IP address to run the server on (default: 127.0.0.1\n");
  38.     printf("     -v                  - Connect with Certificate based SSL/TLS Support to the MQTT server \n");
  39.     printf("     -V                  - Connect with Certificate based client authentification to the MQTT server \n");
  40.     printf("     -h                  - This help \n");
  41. }
  42.  
  43. void rtfm_hamster() {
  44.     printf("Type an argument to controll your hamster\n");
  45.     printf("Optionen:\n");
  46.     printf("    S{<option>}    - Change status - Possible options R (RUNNING), E (EATING), M (MATEING), S (SLEEPING)\n");
  47.     printf("    R{<option>}    - Change room - Possible options A, B, C, D\n");
  48.     printf("    W{<integer>}   - Let the hamster spin - Integer from 0 to 3\n");
  49.     printf("    H              - This help\n");
  50.     printf("    Q              - Quit programm\n");
  51.     printf("Example:\n");
  52.     printf("    SR<enter> to chhange status to running\n");
  53. }
  54.  
  55. void delivered(void *context, MQTTClient_deliveryToken dt) {
  56.     if (DEBUG) printf("Message with token value %d delivery confirmed\n", dt);
  57.     deliveredtoken = dt;
  58. }
  59. int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message) {
  60.     int i;
  61.     char *payloadptr;
  62.     if (DEBUG) printf("Message arrived in topic: %s\n", topicName);
  63.     if (DEBUG) printf("    Message: ");
  64.     payloadptr = message->payload;
  65.     for (i = 0; i < message->payloadlen; i++) {
  66.         if (DEBUG) putchar(*payloadptr++);
  67.     }
  68.     if (DEBUG) putchar('\n');
  69.     MQTTClient_freeMessage(&message);
  70.     MQTTClient_free(topicName);
  71.     return 1;
  72. }
  73. void connlost(void *context, char *cause) {
  74.     printf("\nConnection lost: %s\n", cause);
  75. }
  76.  
  77. void pubnewmsg(MQTTClient *client, char *clientid, char *topic, char *payload, int qos) {
  78.     MQTTClient_message pubmsg = MQTTClient_message_initializer;
  79.     MQTTClient_deliveryToken token;
  80.     pubmsg.payload = payload;
  81.     pubmsg.payloadlen = strlen(payload);
  82.     pubmsg.qos = qos;
  83.     pubmsg.retained = 1;
  84.     MQTTClient_publishMessage(client, topic, &pubmsg, &token);
  85.     if (DEBUG)printf ("Send message to topic %s for hamster: %s\n", topic, clientid);
  86.     MQTTClient_waitForCompletion(client, token, TIMEOUT);
  87.     if (DEBUG) printf("Message successfully delivered (token %d)\n", token);
  88. }
  89.  
  90. int main(int argc, char **argv) {
  91.     MQTTClient client;
  92.     MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  93.     int rc;
  94.     int ch;
  95.     unsigned int port = DEFAULT_PORT;
  96.     char *ipaddress = DEFAULT_ADDRESS;
  97.     char serverURI[64];
  98.     char *clientid;
  99.     char topic[128];
  100.  
  101.     if (argc < 2) {
  102.         rtfm(argv);
  103.         exit(0);
  104.     }
  105.  
  106.     // parse args
  107.     clientid = argv[1];
  108.     char *end;
  109.     for (int i = 1; i < argc; i++) {
  110.         if (strcmp("-p", argv[i]) == 0) {
  111.             port = strtoul(argv[i + 1], &end, 0);
  112.             if (argv[i + 1] == end) {
  113.                 printf("%s: Not a number: %s\n", argv[0], argv[i + 1]);
  114.                 exit(1);
  115.             }
  116.         } else if (strcmp("-s", argv[i]) == 0) {
  117.             ipaddress = argv[i + 1];
  118.             if (argv[i + 1] == end) {
  119.                 printf("%s: Not a ip: %s\n", argv[0], argv[i + 1]);
  120.                 exit(1);
  121.             }
  122.         } else if (strcmp("-h", argv[i]) == 0) {
  123.             rtfm(argv);
  124.             exit(0);
  125.         } else if (strcmp("-v", argv[i]) == 0) {
  126.             // todo
  127.         } else if (strcmp("-V", argv[i]) == 0) {
  128.             // todo
  129.         }
  130.     }
  131.  
  132.     sprintf(serverURI, "%s://%s:%d", "tcp", ipaddress, port);
  133.  
  134.     MQTTClient_create(&client, serverURI, clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  135.     conn_opts.keepAliveInterval = 20;
  136.     conn_opts.cleansession = 1;
  137.     conn_opts.username = "hamster";
  138.     conn_opts.password = "";
  139.  
  140.     MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
  141.  
  142.     if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
  143.         printf("Failed to connect, return code %d\n", rc);
  144.         exit(EXIT_FAILURE);
  145.     }
  146.  
  147.     // Subscribe to all topics
  148.     // Livestock
  149.     MQTTClient_subscribe(client, "/pension/livestock", QOS2);  // exactly once
  150.     if (DEBUG) printf("Subscriped to topic: /pension/livestock\n");
  151.     // Room A-D
  152.     MQTTClient_subscribe(client, "/pension/room/A", QOS0);  // best effort
  153.     if (DEBUG) printf("Subscriped to topic: /pension/room/A\n");
  154.     MQTTClient_subscribe(client, "/pension/room/B", QOS0);  // best effort
  155.     if (DEBUG) printf("Subscriped to topic: /pension/room/B\n");
  156.     MQTTClient_subscribe(client, "/pension/room/C", QOS0);  // best effort
  157.     if (DEBUG) printf("Subscriped to topic: /pension/room/C\n");
  158.     MQTTClient_subscribe(client, "/pension/room/D", QOS0);  // best effort
  159.     if (DEBUG) printf("Subscriped to topic: /pension/room/D\n");
  160.     // Wheels
  161.     sprintf(topic, "/pension/hamster/%s/wheels%c", clientid, '\0');
  162.     if (DEBUG) printf("Subscriped to topic: %s\n", topic);
  163.     MQTTClient_subscribe(client, topic, QOS0);  // best effort
  164.     // State
  165.     if (DEBUG) printf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
  166.     if (DEBUG) printf("Subscriped to topic: %s\n", topic);
  167.     MQTTClient_subscribe(client, topic, QOS1);  // min once
  168.     // Position
  169.     sprintf(topic, "/pension/hamster/%s/position%c", clientid, '\0');
  170.     if (DEBUG) printf("Subscriped to topic: %s\n", topic);
  171.     MQTTClient_subscribe(client, topic, QOS1);  // min once
  172.     // Fondle
  173.     sprintf(topic, "/pension/hamster/%s/fondle%c", clientid, '\0');
  174.     if (DEBUG) printf("Subscriped to topic: %s\n", topic);
  175.     MQTTClient_subscribe(client, topic, QOS2);  // exactly once
  176.     // Punish
  177.     sprintf(topic, "/pension/hamster/%s/punish%c", clientid, '\0');
  178.     if (DEBUG) printf("Subscriped to topic: %s\n", topic);
  179.     MQTTClient_subscribe(client, topic, QOS2);  // exactly once
  180.  
  181.     // Submit default values
  182.     pubnewmsg(client, clientid, "/pension/livestock", clientid, QOS2);
  183.     sprintf(topic, "/pension/hamster/%s/position%c", clientid, '\0');
  184.     pubnewmsg(client, clientid, topic, ROOM_A, QOS1);
  185.     sprintf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
  186.     pubnewmsg(client, clientid, topic, SLEEPING, QOS1);
  187.     sprintf(topic, "/pension/hamster/%s/wheels%c", clientid, '\0');
  188.     pubnewmsg(client, clientid, topic, "0", QOS0);
  189.  
  190.     rtfm_hamster();
  191.     while(1) {
  192.         ch = getchar();
  193.         if (ch == 'R' || ch == 'r') {
  194.             ch = getchar();
  195.             sprintf(topic, "/pension/hamster/%s/position%c", clientid, '\0');
  196.             switch (ch) {
  197.                 case 'A':
  198.                 case 'a':
  199.                     pubnewmsg(client, clientid, topic, ROOM_A, QOS1);
  200.                     break;
  201.                 case 'B':
  202.                 case 'b':
  203.                     pubnewmsg(client, clientid, topic, ROOM_B, QOS1);
  204.                     break;
  205.                 case 'C':
  206.                 case 'c':
  207.                     pubnewmsg(client, clientid, topic, ROOM_C, QOS1);
  208.                     break;
  209.                 case 'D':
  210.                 case 'd':
  211.                     pubnewmsg(client, clientid, topic, ROOM_D, QOS1);
  212.                     break;
  213.                 default:
  214.                     printf("Invalid input: Type H<enter> for help\n");
  215.                     break;
  216.             }
  217.         }
  218.         else if (ch == 'S' || ch == 's') {
  219.             ch = getchar();
  220.             sprintf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
  221.             switch (ch) {
  222.                 case 'R':
  223.                 case 'r':
  224.                     pubnewmsg(client, clientid, topic, RUNNING, QOS1);
  225.                     break;
  226.                 case 'M':
  227.                 case 'm':
  228.                     pubnewmsg(client, clientid, topic, MATEING, QOS1);
  229.                     break;
  230.                 case 'S':
  231.                 case 's':
  232.                     pubnewmsg(client, clientid, topic, SLEEPING, QOS1);
  233.                     break;
  234.                 case 'E':
  235.                 case 'e':
  236.                     pubnewmsg(client, clientid, topic, EATING, QOS1);
  237.                     break;
  238.                 default:
  239.                     printf("Invalid input: Type H<enter> for help\n");
  240.                     break;
  241.             }
  242.         }
  243.         else if (ch == 'W' || ch == 'w') {
  244.             ch = getchar();
  245.             sprintf(topic, "/pension/hamster/%s/wheels%c", clientid, '\0');
  246.             switch (ch) {
  247.                 case '0':
  248.                     pubnewmsg(client, clientid, topic, "0", QOS0);
  249.                     break;
  250.                 case '1':
  251.                     pubnewmsg(client, clientid, topic, "1", QOS0);
  252.                     break;
  253.                 case '2':
  254.                     pubnewmsg(client, clientid, topic, "2", QOS0);
  255.                     break;
  256.                 case '3':
  257.                     pubnewmsg(client, clientid, topic, "3", QOS0);
  258.                     break;
  259.                 default:
  260.                     printf("Invalid input: Type H<enter> for help\n");
  261.                     break;
  262.             }
  263.         }
  264.         else if (ch == 'H' || ch == 'h') {
  265.             rtfm_hamster();
  266.         }
  267.         else if (ch == 'Q' || ch == 'q') {
  268.             MQTTClient_disconnect(client, 10000);
  269.             MQTTClient_destroy(&client);
  270.             return rc;
  271.         }
  272.     }
  273.  
  274.     return rc;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement