mqtt_client.c 3.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2014 Cesanta Software Limited
 * All rights reserved
 * This software is dual-licensed: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation. For the terms of this
 * license, see <http://www.gnu.org/licenses/>.
 *
 * You are free to use this software under the terms of the GNU General
 * Public License, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * Alternatively, you can license this software under a commercial
 * license, as set out in <https://www.cesanta.com/license>.
 */

#include "mongoose.h"

20 21 22 23 24
static const char *s_address = "localhost:1883";
static const char *s_user_name = NULL;
static const char *s_password = NULL;

struct mg_mqtt_topic_expression topic_expressions[] = {{"/stuff", 0}};
25 26

static void ev_handler(struct mg_connection *nc, int ev, void *p) {
27
  struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
28 29 30
  (void) nc;

#if 0
31
  if (ev != MG_EV_POLL)
32 33 34 35
    printf("USER HANDLER GOT %d\n", ev);
#endif

  switch (ev) {
36 37 38 39 40 41
    case MG_EV_CONNECT: {
      struct mg_send_mqtt_handshake_opts opts;
      memset(&opts, 0, sizeof(opts));
      opts.user_name = s_user_name;
      opts.password = s_password;

42
      mg_set_protocol_mqtt(nc);
43
      mg_send_mqtt_handshake_opt(nc, "dummy", opts);
44
      break;
45
    }
46 47
    case MG_EV_MQTT_CONNACK:
      if (msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED) {
48 49 50 51
        printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
        exit(1);
      }
      printf("Subscribing to '/stuff'\n");
52 53 54
      mg_mqtt_subscribe(nc, topic_expressions,
                        sizeof(topic_expressions) / sizeof(*topic_expressions),
                        42);
55
      break;
56
    case MG_EV_MQTT_PUBACK:
57 58
      printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
      break;
59
    case MG_EV_MQTT_SUBACK:
60 61
      printf("Subscription acknowledged, forwarding to '/test'\n");
      break;
62
    case MG_EV_MQTT_PUBLISH: {
63 64 65 66 67
#if 0
        char hex[1024] = {0};
        mg_hexdump(nc->recv_mbuf.buf, msg->payload.len, hex, sizeof(hex));
        printf("Got incoming message %s:\n%s", msg->topic, hex);
#else
68 69
      printf("Got incoming message %s: %.*s\n", msg->topic,
             (int) msg->payload.len, msg->payload.p);
70 71
#endif

72 73 74
      printf("Forwarding to /test\n");
      mg_mqtt_publish(nc, "/test", 65, MG_MQTT_QOS(0), msg->payload.p,
                      msg->payload.len);
75
      break;
76
    }
77
    case MG_EV_CLOSE:
78 79 80 81 82
      printf("Connection closed\n");
      exit(1);
  }
}

83
int main(int argc, char **argv) {
84
  struct mg_mgr mgr;
85
  int i;
86 87 88

  mg_mgr_init(&mgr, NULL);

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  /* Parse command line arguments */
  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-h") == 0) {
      s_address = argv[i + 1];
      i++;
    } else if (strcmp(argv[i], "-u") == 0) {
      s_user_name = argv[i + 1];
      i++;
    } else if (strcmp(argv[i], "-p") == 0) {
      s_password = argv[i + 1];
      i++;
    }
  }

  if (mg_connect(&mgr, s_address, ev_handler) == NULL) {
    fprintf(stderr, "mg_connect(%s) failed\n", s_address);
105 106 107
    exit(EXIT_FAILURE);
  }

108
  for (;;) {
109 110 111
    mg_mgr_poll(&mgr, 1000);
  }
}