Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <inttypes.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <sys/types.h>
- #include <MQTTClient.h>
- #define DEFAULT_ADDRESS "127.0.0.1"
- #define DEFAULT_PORT 1883
- #define ROOM_A "A"
- #define ROOM_B "B"
- #define ROOM_C "C"
- #define ROOM_D "D"
- #define RUNNING "RUNNING"
- #define SLEEPING "SLEEPING"
- #define EATING "EATING"
- #define MATEING "MATEING"
- #define QOS0 0
- #define QOS1 1
- #define QOS2 2
- #define TIMEOUT 10000L
- #define DEBUG 0
- volatile MQTTClient_deliveryToken deliveredtoken;
- void rtfm(char **argv) {
- printf("Usage: %s hamster_client_id {<Option>} <param1> \n", argv[0]);
- printf("Function: Hamster instrumentation device software\n");
- printf("Optionen:\n");
- printf(" -p {<port>} - port of the mqtt server (default (no tls): 1883\n");
- printf(" -s {<IP address>} - IP address to run the server on (default: 127.0.0.1\n");
- printf(" -v - Connect with Certificate based SSL/TLS Support to the MQTT server \n");
- printf(" -V - Connect with Certificate based client authentification to the MQTT server \n");
- printf(" -h - This help \n");
- }
- void rtfm_hamster() {
- printf("Type an argument to controll your hamster\n");
- printf("Optionen:\n");
- printf(" S{<option>} - Change status - Possible options R (RUNNING), E (EATING), M (MATEING), S (SLEEPING)\n");
- printf(" R{<option>} - Change room - Possible options A, B, C, D\n");
- printf(" W{<integer>} - Let the hamster spin - Integer from 0 to 3\n");
- printf(" H - This help\n");
- printf(" Q - Quit programm\n");
- printf("Example:\n");
- printf(" SR<enter> to chhange status to running\n");
- }
- void delivered(void *context, MQTTClient_deliveryToken dt) {
- if (DEBUG) printf("Message with token value %d delivery confirmed\n", dt);
- deliveredtoken = dt;
- }
- int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message) {
- int i;
- char *payloadptr;
- if (DEBUG) printf("Message arrived in topic: %s\n", topicName);
- if (DEBUG) printf(" Message: ");
- payloadptr = message->payload;
- for (i = 0; i < message->payloadlen; i++) {
- if (DEBUG) putchar(*payloadptr++);
- }
- if (DEBUG) putchar('\n');
- MQTTClient_freeMessage(&message);
- MQTTClient_free(topicName);
- return 1;
- }
- void connlost(void *context, char *cause) {
- printf("\nConnection lost: %s\n", cause);
- }
- void pubnewmsg(MQTTClient *client, char *clientid, char *topic, char *payload, int qos) {
- MQTTClient_message pubmsg = MQTTClient_message_initializer;
- MQTTClient_deliveryToken token;
- pubmsg.payload = payload;
- pubmsg.payloadlen = strlen(payload);
- pubmsg.qos = qos;
- pubmsg.retained = 1;
- MQTTClient_publishMessage(client, topic, &pubmsg, &token);
- if (DEBUG)printf ("Send message to topic %s for hamster: %s\n", topic, clientid);
- MQTTClient_waitForCompletion(client, token, TIMEOUT);
- if (DEBUG) printf("Message successfully delivered (token %d)\n", token);
- }
- int main(int argc, char **argv) {
- MQTTClient client;
- MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
- int rc;
- int ch;
- unsigned int port = DEFAULT_PORT;
- char *ipaddress = DEFAULT_ADDRESS;
- char serverURI[64];
- char *clientid;
- char topic[128];
- if (argc < 2) {
- rtfm(argv);
- exit(0);
- }
- // parse args
- clientid = argv[1];
- char *end;
- for (int i = 1; i < argc; i++) {
- if (strcmp("-p", argv[i]) == 0) {
- port = strtoul(argv[i + 1], &end, 0);
- if (argv[i + 1] == end) {
- printf("%s: Not a number: %s\n", argv[0], argv[i + 1]);
- exit(1);
- }
- } else if (strcmp("-s", argv[i]) == 0) {
- ipaddress = argv[i + 1];
- if (argv[i + 1] == end) {
- printf("%s: Not a ip: %s\n", argv[0], argv[i + 1]);
- exit(1);
- }
- } else if (strcmp("-h", argv[i]) == 0) {
- rtfm(argv);
- exit(0);
- } else if (strcmp("-v", argv[i]) == 0) {
- // todo
- } else if (strcmp("-V", argv[i]) == 0) {
- // todo
- }
- }
- sprintf(serverURI, "%s://%s:%d", "tcp", ipaddress, port);
- MQTTClient_create(&client, serverURI, clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);
- conn_opts.keepAliveInterval = 20;
- conn_opts.cleansession = 1;
- conn_opts.username = "hamster";
- conn_opts.password = "";
- MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
- if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
- printf("Failed to connect, return code %d\n", rc);
- exit(EXIT_FAILURE);
- }
- // Subscribe to all topics
- // Livestock
- MQTTClient_subscribe(client, "/pension/livestock", QOS2); // exactly once
- if (DEBUG) printf("Subscriped to topic: /pension/livestock\n");
- // Room A-D
- MQTTClient_subscribe(client, "/pension/room/A", QOS0); // best effort
- if (DEBUG) printf("Subscriped to topic: /pension/room/A\n");
- MQTTClient_subscribe(client, "/pension/room/B", QOS0); // best effort
- if (DEBUG) printf("Subscriped to topic: /pension/room/B\n");
- MQTTClient_subscribe(client, "/pension/room/C", QOS0); // best effort
- if (DEBUG) printf("Subscriped to topic: /pension/room/C\n");
- MQTTClient_subscribe(client, "/pension/room/D", QOS0); // best effort
- if (DEBUG) printf("Subscriped to topic: /pension/room/D\n");
- // Wheels
- sprintf(topic, "/pension/hamster/%s/wheels%c", clientid, '\0');
- if (DEBUG) printf("Subscriped to topic: %s\n", topic);
- MQTTClient_subscribe(client, topic, QOS0); // best effort
- // State
- if (DEBUG) printf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
- if (DEBUG) printf("Subscriped to topic: %s\n", topic);
- MQTTClient_subscribe(client, topic, QOS1); // min once
- // Position
- sprintf(topic, "/pension/hamster/%s/position%c", clientid, '\0');
- if (DEBUG) printf("Subscriped to topic: %s\n", topic);
- MQTTClient_subscribe(client, topic, QOS1); // min once
- // Fondle
- sprintf(topic, "/pension/hamster/%s/fondle%c", clientid, '\0');
- if (DEBUG) printf("Subscriped to topic: %s\n", topic);
- MQTTClient_subscribe(client, topic, QOS2); // exactly once
- // Punish
- sprintf(topic, "/pension/hamster/%s/punish%c", clientid, '\0');
- if (DEBUG) printf("Subscriped to topic: %s\n", topic);
- MQTTClient_subscribe(client, topic, QOS2); // exactly once
- // Submit default values
- pubnewmsg(client, clientid, "/pension/livestock", clientid, QOS2);
- sprintf(topic, "/pension/hamster/%s/position%c", clientid, '\0');
- pubnewmsg(client, clientid, topic, ROOM_A, QOS1);
- sprintf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
- pubnewmsg(client, clientid, topic, SLEEPING, QOS1);
- sprintf(topic, "/pension/hamster/%s/wheels%c", clientid, '\0');
- pubnewmsg(client, clientid, topic, "0", QOS0);
- rtfm_hamster();
- while(1) {
- ch = getchar();
- if (ch == 'R' || ch == 'r') {
- ch = getchar();
- sprintf(topic, "/pension/hamster/%s/position%c", clientid, '\0');
- switch (ch) {
- case 'A':
- case 'a':
- pubnewmsg(client, clientid, topic, ROOM_A, QOS1);
- break;
- case 'B':
- case 'b':
- pubnewmsg(client, clientid, topic, ROOM_B, QOS1);
- break;
- case 'C':
- case 'c':
- pubnewmsg(client, clientid, topic, ROOM_C, QOS1);
- break;
- case 'D':
- case 'd':
- pubnewmsg(client, clientid, topic, ROOM_D, QOS1);
- break;
- default:
- printf("Invalid input: Type H<enter> for help\n");
- break;
- }
- }
- else if (ch == 'S' || ch == 's') {
- ch = getchar();
- sprintf(topic, "/pension/hamster/%s/state%c", clientid, '\0');
- switch (ch) {
- case 'R':
- case 'r':
- pubnewmsg(client, clientid, topic, RUNNING, QOS1);
- break;
- case 'M':
- case 'm':
- pubnewmsg(client, clientid, topic, MATEING, QOS1);
- break;
- case 'S':
- case 's':
- pubnewmsg(client, clientid, topic, SLEEPING, QOS1);
- break;
- case 'E':
- case 'e':
- pubnewmsg(client, clientid, topic, EATING, QOS1);
- break;
- default:
- printf("Invalid input: Type H<enter> for help\n");
- break;
- }
- }
- else if (ch == 'W' || ch == 'w') {
- ch = getchar();
- sprintf(topic, "/pension/hamster/%s/wheels%c", clientid, '\0');
- switch (ch) {
- case '0':
- pubnewmsg(client, clientid, topic, "0", QOS0);
- break;
- case '1':
- pubnewmsg(client, clientid, topic, "1", QOS0);
- break;
- case '2':
- pubnewmsg(client, clientid, topic, "2", QOS0);
- break;
- case '3':
- pubnewmsg(client, clientid, topic, "3", QOS0);
- break;
- default:
- printf("Invalid input: Type H<enter> for help\n");
- break;
- }
- }
- else if (ch == 'H' || ch == 'h') {
- rtfm_hamster();
- }
- else if (ch == 'Q' || ch == 'q') {
- MQTTClient_disconnect(client, 10000);
- MQTTClient_destroy(&client);
- return rc;
- }
- }
- return rc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement