Beispiel #1
0
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
    struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
    static int count1 = 0;
    switch (ev) {
    case MG_EV_CONNECT:
        if (*(int *) p != 0) {
            SYS_PRINT("Failed to connect to %s\r\n", MQTT_BROKER_ADDRESS);
        } else {
            SYS_PRINT("Connected to %s\r\n", MQTT_BROKER_ADDRESS);
        }
        struct mg_send_mqtt_handshake_opts opts;
        memset(&opts, 0, sizeof(opts));
        opts.user_name = MQTT_USER_NAME;
        opts.password = MQTT_USER_PWD;
        mg_set_protocol_mqtt(nc);
        mg_send_mqtt_handshake_opt(nc, "PIC32", opts);
        break;
    case MG_EV_MQTT_CONNACK:
        if (msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED) {
            SYS_PRINT("Got mqtt connection error %d\n\r", msg->connack_ret_code);
        } else {
            SYS_PRINT("Connected to broker\n\r");
        }
        SYS_PRINT("Subscribing to /test\n\r");
        mg_mqtt_subscribe(nc, topic_expressions,
                          sizeof(topic_expressions) / sizeof(*topic_expressions),
                          ++msg_id);
        nc->flags |= MG_F_USER_1;
        break;
    case MG_EV_MQTT_SUBACK:
        SYS_PRINT("Subscription acknowledged\r\n");
        break;
    case MG_EV_MQTT_PUBLISH:
        SYS_PRINT("Got incoming message %s: %.*s\r\n", msg->topic,
                  (int) msg->payload.len, msg->payload.p);
        break;
    case MG_EV_POLL: {
        if (nc->flags & MG_F_USER_1) {
            char msg[100];
            static uint32_t prev_send = 0;
            uint32_t now = DRV_RTCC_TimeGet();
            if (now - prev_send > 1000) {
                int len = snprintf(msg, sizeof(msg), "Current RTC value: %u", now);
                SYS_PRINT("Publishing message with RTC value=%u\r\n", now);
                mg_mqtt_publish(nc, "/stuff", ++msg_id, MG_MQTT_QOS(0), msg, len);
                prev_send = now;
            }
        }
        break;
    }
    case MG_EV_CLOSE:
        SYS_PRINT("Connection to broker is closed\r\n");
        appData.state = APP_STATE_DONE;
        break;
    default:
        break;
    }
}
Beispiel #2
0
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
    struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
    (void) nc;

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

    switch (ev) {
    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;

        mg_set_protocol_mqtt(nc);
        mg_send_mqtt_handshake_opt(nc, "dummy", opts);
        break;
    }
    case MG_EV_MQTT_CONNACK:
        if (msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED) {
            printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
            exit(1);
        }
        s_topic_expr.topic = s_topic;
        printf("Subscribing to '%s'\n", s_topic);
        mg_mqtt_subscribe(nc, &s_topic_expr, 1, 42);
        break;
    case MG_EV_MQTT_PUBACK:
        printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
        break;
    case MG_EV_MQTT_SUBACK:
        printf("Subscription acknowledged, forwarding to '/test'\n");
        break;
    case MG_EV_MQTT_PUBLISH: {
#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", (int)msg->topic.len, msg->topic.p, hex);
#else
        printf("Got incoming message %.*s: %.*s\n", (int) msg->topic.len,
               msg->topic.p, (int) msg->payload.len, msg->payload.p);
#endif

        printf("Forwarding to /test\n");
        mg_mqtt_publish(nc, "/test", 65, MG_MQTT_QOS(0), msg->payload.p,
                        msg->payload.len);
        break;
    }
    case MG_EV_CLOSE:
        printf("Connection closed\n");
        exit(1);
    }
}
Beispiel #3
0
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
    struct mg_mqtt_message *msg = (struct mg_mqtt_message *)p;
    (void) nc;

#if 0
    if (ev != NS_POLL)
        printf("USER HANDLER GOT %d\n", ev);
#endif

    switch (ev) {
    case NS_CONNECT:
        mg_set_protocol_mqtt(nc);
        mg_send_mqtt_handshake(nc, "dummy");
        break;
    case NS_MQTT_CONNACK:
        if (msg->connack_ret_code != NS_MQTT_CONNACK_ACCEPTED) {
            printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
            exit(1);
        }
        printf("Subscribing to '/stuff'\n");
        mg_mqtt_subscribe(nc, topic_expressions, sizeof(topic_expressions)/sizeof(*topic_expressions), 42);
        break;
    case NS_MQTT_PUBACK:
        printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
        break;
    case NS_MQTT_SUBACK:
        printf("Subscription acknowledged, forwarding to '/test'\n");
        break;
    case NS_MQTT_PUBLISH:
    {
#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
        printf("Got incoming message %s: %.*s\n", msg->topic, (int)msg->payload.len, msg->payload.p);
#endif

        printf("Forwarding to /test\n");
        mg_mqtt_publish(nc, "/test", 65, NS_MQTT_QOS(0), msg->payload.p, msg->payload.len);
    }
    break;
    case NS_CLOSE:
        printf("Connection closed\n");
        exit(1);
    }
}
Beispiel #4
0
/*
 * Make a new MQTT client.
 *
 * Arguments:
 * url: url where to connect to
 * opts: option object
 *
 * Recognized option object properties:
 *
 * - clientId: string; mqtt client id. defaults to
 *             Math.random().toString(16).substr(2, 10)
 *
 * Example:
 *
 * var client = MQTT.connect('mqtt://test.mosquitto.org');
 *
 * client.on('connect', function () {
 *   client.subscribe('presence');
 *   client.publish('presence', 'Hello mqtt');
 * });
 *
 * client.on('message', function (topic, message) {
 *   console.log(message);
 * });
 *
 * TLS can be enabled by choosing the `mqtts://` protocol. In that
 * case the default port is 8883 as defined by IANA.
 *
 * The API is modeled after https://www.npmjs.com/package/mqtt.
 *
 */
enum v7_err sj_mqtt_connect(struct v7 *v7, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  const char *url;
  size_t len;
  struct mg_str host, scheme;
  unsigned int port;
  struct mg_connection *nc;
  struct user_data *ud;
  char *url_with_port = NULL;
  int use_ssl = 0;
  v7_val_t urlv = v7_arg(v7, 0), opts = v7_arg(v7, 1);
  v7_val_t client_id;
  v7_val_t proto =
      v7_get(v7, v7_get(v7, v7_get_global(v7), "MQTT", ~0), "proto", ~0);

  if (!v7_is_string(urlv)) {
    rcode = v7_throwf(v7, "Error", "invalid url string");
    goto clean;
  }

  url = v7_get_string(v7, &urlv, &len);

  if (mg_parse_uri(mg_mk_str(url), &scheme, NULL, &host, &port, NULL, NULL,
                   NULL) < 0) {
    rcode = v7_throwf(v7, "Error", "invalid url string");
    goto clean;
  }

  if (mg_vcmp(&scheme, "mqtt") == 0) {
    url += sizeof("mqtt://") - 1;
  } else if (mg_vcmp(&scheme, "mqtts") == 0) {
    url += sizeof("mqtts://") - 1;
    use_ssl = 1;
  } else {
    rcode = v7_throwf(v7, "Error", "unsupported protocol");
    goto clean;
  }

  client_id = v7_get(v7, opts, "clientId", ~0);
  if (v7_is_undefined(client_id)) {
    rcode = v7_exec(v7, "Math.random().toString(16).substr(2,8)", &client_id);
    if (rcode != V7_OK) {
      goto clean;
    }
  }

  if (port == 0) {
    if (asprintf(&url_with_port, "%.*s%s", (int) host.len, host.p,
                 (use_ssl ? ":8883" : ":1883")) < 0) {
      rcode = v7_throwf(v7, "Error", "Out of memory");
      goto clean;
    }
  }

  nc =
      mg_connect(&sj_mgr, url_with_port ? url_with_port : url, mqtt_ev_handler);
  if (nc == NULL) {
    rcode = v7_throwf(v7, "Error", "cannot create connection");
    goto clean;
  }

  if (use_ssl) {
#ifdef MG_ENABLE_SSL
    mg_set_ssl(nc, NULL, NULL);
#else
    rcode = v7_throwf(v7, "Error", "SSL not enabled");
    goto clean;
#endif
  }
  mg_set_protocol_mqtt(nc);

  *res = v7_mk_object(v7);
  v7_set_proto(v7, *res, proto);

  ud = calloc(1, sizeof(*ud));
  if (ud == NULL) {
    rcode = v7_throwf(v7, "Error", "Out of memory");
    goto clean;
  }
  ud->v7 = v7;
  ud->client = *res;
  ud->client_id = strdup(v7_get_cstring(v7, &client_id));
  if (ud->client_id == NULL) {
    free(ud);
    rcode = v7_throwf(v7, "Error", "Out of memory");
    goto clean;
  }
  nc->user_data = ud;
  v7_own(v7, &ud->client);

  v7_def(v7, *res, "_nc", ~0, _V7_DESC_HIDDEN(1), v7_mk_foreign(v7, nc));

clean:
  free(url_with_port);

  return rcode;
}