static int notify_js(enum js_update_status us, const char *info) { #ifndef CS_DISABLE_JS if (!v7_is_undefined(s_updater_notify_cb)) { if (info == NULL) { sj_invoke_cb1(s_v7, s_updater_notify_cb, v7_mk_number(s_v7, us)); } else { sj_invoke_cb2(s_v7, s_updater_notify_cb, v7_mk_number(s_v7, us), v7_mk_string(s_v7, info, ~0, 1)); }; return 1; } #else (void) us; (void) info; #endif return 0; }
static void gpio_intr_handler_proxy(int pin, enum gpio_level level, void *arg) { struct v7 *v7 = (struct v7 *) arg; char prop_name[15]; int len; len = snprintf(prop_name, sizeof(prop_name), "_ih_%d", (int) pin); v7_val_t cb = v7_get(v7, v7_get_global(v7), prop_name, len); if (!v7_is_callable(v7, cb)) { return; } /* Forwarding call to common cbs queue */ v7_val_t args = v7_mk_array(v7); v7_array_push(v7, args, v7_mk_number(v7, pin)); v7_array_push(v7, args, v7_mk_number(v7, level)); sj_invoke_cb2(v7, s_isr_cb_proxy_v, cb, args); }
void sj_http_error_callback(struct v7 *v7, v7_val_t cb, int err_no) { char err_msg[32]; snprintf(err_msg, sizeof(err_msg), "connection error: %d\n", err_no); sj_invoke_cb2(v7, cb, v7_create_undefined(), v7_create_string(v7, err_msg, ~0, 1)); }
void sj_invoke_cb0(struct v7 *v7, v7_val_t cb) { sj_invoke_cb2(v7, cb, v7_create_undefined(), v7_create_undefined()); }
void sj_invoke_cb1(struct v7 *v7, v7_val_t cb, v7_val_t arg) { sj_invoke_cb2(v7, cb, arg, v7_create_undefined()); }
static void mqtt_ev_handler(struct mg_connection *nc, int ev, void *ev_data) { struct mg_mqtt_message *msg = (struct mg_mqtt_message *) ev_data; struct user_data *ud = (struct user_data *) nc->user_data; struct v7 *v7 = ud->v7; v7_val_t cb; (void) nc; (void) ev_data; switch (ev) { case MG_EV_CONNECT: if (*(int *) ev_data == 0) { mg_send_mqtt_handshake(nc, ud->client_id); } else { cb = v7_get(v7, ud->client, SJ_MQTT_ERROR_CB, ~0); if (!v7_is_undefined(cb)) { sj_invoke_cb0(v7, cb); } } break; case MG_EV_MQTT_CONNACK: { /* * Call connect or error cb if they were registered. */ const char *key; if (msg->connack_ret_code == MG_EV_MQTT_CONNACK_ACCEPTED) { key = SJ_MQTT_CONNECT_CB; } else { key = SJ_MQTT_ERROR_CB; } cb = v7_get(v7, ud->client, key, ~0); if (!v7_is_undefined(cb)) { sj_invoke_cb0(v7, cb); } break; } case MG_EV_MQTT_PUBLISH: cb = v7_get(v7, ud->client, SJ_MQTT_MESSAGE_CB, ~0); if (!v7_is_undefined(cb)) { v7_val_t topic = v7_mk_string(v7, msg->topic, strlen(msg->topic), 1); v7_val_t payload = v7_mk_string(v7, msg->payload.p, msg->payload.len, 1); sj_invoke_cb2(v7, cb, topic, payload); } break; case MG_EV_CLOSE: /* * Invoke close cb and then destroys all mg state. */ cb = v7_get(v7, ud->client, SJ_MQTT_CLOSE_CB, ~0); if (!v7_is_undefined(cb)) { sj_invoke_cb0(v7, cb); } v7_def(v7, ud->client, "_nc", ~0, _V7_DESC_HIDDEN(1), V7_UNDEFINED); v7_disown(v7, &ud->client); free(ud->client_id); free(ud); break; } }