Statistics
| Branch: | Tag: | Revision:

mongoose / examples / mqtt_client / mqtt_client.c @ eaef5bd1

History | View | Annotate | Download (3.3 KB)

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

    
18
#include "mongoose.h"
19

    
20
static const char *s_address = "localhost:1883";
21
static const char *s_user_name = NULL;
22
static const char *s_password = NULL;
23
static const char *s_topic = "/stuff";
24
static struct mg_mqtt_topic_expression s_topic_expr = {NULL, 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
  (void) nc;
29

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

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

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

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

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

    
86
  mg_mgr_init(&mgr, NULL);
87

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

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

    
106
  for (;;) {
107
    mg_mgr_poll(&mgr, 1000);
108
  }
109
}