void esp_sj_uart_init(struct v7 *v7) { sj_us[0].v7 = sj_us[1].v7 = v7; os_timer_setfn(&sj_us[0].timer, esp_sj_uart_dispatcher, (void *) 0); os_timer_setfn(&sj_us[1].timer, esp_sj_uart_dispatcher, (void *) 1); s_uart_proto = v7_mk_object(v7); v7_set_method(v7, s_uart_proto, "configure", UART_configure); v7_set_method(v7, s_uart_proto, "onRecv", UART_onRecv); v7_set_method(v7, s_uart_proto, "recv", UART_recv); v7_set_method(v7, s_uart_proto, "setRXEnabled", UART_setRXEnabled); v7_set_method(v7, s_uart_proto, "onTXEmpty", UART_onTXEmpty); v7_set_method(v7, s_uart_proto, "sendAvail", UART_sendAvail); v7_set_method(v7, s_uart_proto, "send", UART_send); v7_set_method(v7, v7_get_global(v7), "UART", UART_get); sj_us[0].uart_no = 0; sj_us[0].obj = v7_mk_object(v7); v7_set_proto(v7, sj_us[0].obj, s_uart_proto); v7_set(v7, sj_us[0].obj, "_u", ~0, v7_mk_number(0)); v7_own(v7, &sj_us[0].obj); sj_us[1].uart_no = 1; sj_us[1].obj = v7_mk_object(v7); v7_set_proto(v7, sj_us[1].obj, s_uart_proto); v7_set(v7, sj_us[1].obj, "_u", ~0, v7_mk_number(1)); v7_own(v7, &sj_us[1].obj); }
SJ_PRIVATE enum v7_err Http_createServer(struct v7 *v7, v7_val_t *res) { enum v7_err rcode = V7_OK; v7_val_t cb = v7_arg(v7, 0); if (!v7_is_callable(v7, cb)) { rcode = v7_throwf(v7, "Error", "Invalid argument"); goto clean; } *res = v7_mk_object(v7); v7_set_proto(v7, *res, sj_http_server_proto); v7_set(v7, *res, "_cb", ~0, cb); clean: return rcode; }
static void setup_response_object(struct v7 *v7, v7_val_t response, struct mg_connection *c, v7_val_t request) { v7_set_proto(v7, response, sj_http_response_proto); v7_set(v7, response, "_c", ~0, v7_mk_foreign(c)); v7_set(v7, response, "_r", ~0, request); }
/* * Create request object, used by `Http.request()` and `Http.get()` */ WARN_UNUSED_RESULT static enum v7_err sj_http_request_common(struct v7 *v7, v7_val_t opts, v7_val_t cb, v7_val_t *res) { enum v7_err rcode = V7_OK; char addr[200]; struct mg_connection *c; struct user_data *ud; struct mg_connect_opts copts; #ifdef MG_ENABLE_SSL int force_ssl; #endif memset(&copts, 0, sizeof(copts)); /* * Determine type of provided `opts`, and if it's a string, then parse * it to object */ if (v7_is_string(opts)) { rcode = sj_url_parse(v7, opts, &opts); if (rcode != V7_OK) { goto clean; } } else if (!v7_is_object(opts)) { rcode = v7_throwf(v7, "Error", "opts must be an object or a string URL"); goto clean; } /* * Now, `opts` is guaranteed to be an object. * Let's retrieve needed properties */ v7_val_t v_h = v7_get(v7, opts, "hostname", ~0); if (v7_is_undefined(v_h)) { v_h = v7_get(v7, opts, "host", ~0); } v7_val_t v_p = v7_get(v7, opts, "port", ~0); v7_val_t v_uri = v7_get(v7, opts, "path", ~0); v7_val_t v_m = v7_get(v7, opts, "method", ~0); v7_val_t v_hdrs = v7_get(v7, opts, "headers", ~0); /* Perform options validation and set defaults if needed */ int port = v7_is_number(v_p) ? v7_to_number(v_p) : 80; const char *host = v7_is_string(v_h) ? v7_to_cstring(v7, &v_h) : ""; const char *uri = v7_is_string(v_uri) ? v7_to_cstring(v7, &v_uri) : "/"; const char *method = v7_is_string(v_m) ? v7_to_cstring(v7, &v_m) : "GET"; #ifdef MG_ENABLE_SSL v7_val_t v_pr = v7_get(v7, opts, "protocol", ~0); const char *protocol = v7_is_string(v_pr) ? v7_to_cstring(v7, &v_pr) : ""; force_ssl = (strcasecmp(protocol, "https") == 0); if ((rcode = fill_ssl_connect_opts(v7, opts, force_ssl, &copts)) != V7_OK) { goto clean; } #endif /* Compose address like host:port */ snprintf(addr, sizeof(addr), "%s:%d", host, port); /* * Try to connect, passing `http_ev_handler` as the callback, which will * call provided JavaScript function (we'll set it in user data below). * TODO(alashkin): change mg_connect_opt to mg_connect_http_opt */ if ((c = mg_connect_opt(&sj_mgr, addr, http_ev_handler, copts)) == NULL) { rcode = v7_throwf(v7, "Error", "Cannot connect"); goto clean; } /* * Attach mongoose's built-in HTTP event handler to the connection, and send * necessary headers */ mg_set_protocol_http_websocket(c); mg_printf(c, "%s %s HTTP/1.1\r\n", method, uri); mg_printf(c, "Host: %s\r\n", host); http_write_headers(v7, v_hdrs, c); http_write_chunked_encoding_header(c); mg_printf(c, "%s", "\r\n"); /* * Allocate and initialize user data structure that is used by the JS HTTP * interface. Create the request object (which will have the request * prototype `sj_http_request_proto`), and set provided callback function. */ c->user_data = ud = (struct user_data *) calloc(1, sizeof(*ud)); ud->v7 = v7; ud->obj = v7_mk_object(v7); ud->handler = cb; v7_own(v7, &ud->obj); v7_set_proto(v7, ud->obj, sj_http_request_proto); /* internal property: mongoose connection */ v7_set(v7, ud->obj, "_c", ~0, v7_mk_foreign(c)); /* internal property: callback function that was passed as an argument */ v7_set(v7, ud->obj, "_cb", ~0, ud->handler); *res = ud->obj; clean: return rcode; }
/* * 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; }