コード例 #1
0
ファイル: sj_mqtt.c プロジェクト: 2bright/mongoose-iot
/*
 * Publishes a message to a topic.
 *
 * Args:
 * - `topic`: topic, string.
 * - `message`: message, string.
 *
 * Only QOS 0 is implemented at the moment.
 */
enum v7_err MQTT_publish(struct v7 *v7, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  struct user_data *ud;
  struct mg_connection *nc;
  const char *topic;
  const char *message;
  size_t message_len;
  v7_val_t topicv = v7_arg(v7, 0), messagev = v7_arg(v7, 1);
  (void) res;

  topic = v7_get_cstring(v7, &topicv);
  if (topic == NULL || strlen(topic) == 0) {
    rcode = v7_throwf(v7, "TypeError", "invalid topic");
    goto clean;
  }

  if (!v7_is_string(messagev)) {
    rcode = v7_throwf(v7, "TypeError", "invalid message");
    goto clean;
  }
  message = v7_get_string(v7, &messagev, &message_len);

  nc = v7_get_ptr(v7, v7_get(v7, v7_get_this(v7), "_nc", ~0));
  if (nc == NULL) {
    rcode = v7_throwf(v7, "Error", "invalid connection");
    goto clean;
  }
  ud = (struct user_data *) nc->user_data;

  mg_mqtt_publish(nc, topic, ud->msgid++, MG_MQTT_QOS(0), message, message_len);

clean:
  return rcode;
}
コード例 #2
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;
    }
}
コード例 #3
0
ファイル: mqtt_client.c プロジェクト: cesanta/mongoose
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);
    }
}