/* * Subscribes a mqtt client to a topic. */ enum v7_err MQTT_subscribe(struct v7 *v7, v7_val_t *res) { enum v7_err rcode = V7_OK; struct user_data *ud; struct mg_connection *nc; struct mg_mqtt_topic_expression expr; v7_val_t topicv = v7_arg(v7, 0); const char *topic; nc = v7_get_ptr(v7, v7_get(v7, v7_get_this(v7), "_nc", ~0)); if (nc == NULL) { rcode = v7_throwf(v7, "Error", "unsupported protocol"); goto clean; } ud = (struct user_data *) nc->user_data; topic = v7_get_cstring(v7, &topicv); if (topic == NULL || strlen(topic) == 0) { rcode = v7_throwf(v7, "TypeError", "invalid topic"); goto clean; } expr.topic = topic; expr.qos = 0; mg_mqtt_subscribe(nc, &expr, 1, ud->msgid++); *res = v7_mk_boolean(v7, 1); clean: return rcode; }
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; } }
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); } }
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); } }