static enum v7_err dsleep(struct v7 *v7, v7_val_t *res) { enum v7_err rcode = V7_OK; v7_val_t time_v = v7_arg(v7, 0); double time = v7_to_number(time_v); v7_val_t flags_v = v7_arg(v7, 1); uint8 flags = v7_to_number(flags_v); if (!v7_is_number(time_v) || time < 0) { *res = v7_create_boolean(false); goto clean; } if (v7_is_number(flags_v)) { if (!system_deep_sleep_set_option(flags)) { *res = v7_create_boolean(false); goto clean; } } system_deep_sleep((uint32_t) time); *res = v7_create_boolean(true); goto clean; clean: return rcode; }
static v7_val_t Wifi_changed(struct v7 *v7) { v7_val_t cb = v7_arg(v7, 0); if (!v7_is_function(cb)) return v7_create_boolean(0); v7_set(v7, s_wifi, "_ccb", ~0, V7_PROPERTY_DONT_ENUM | V7_PROPERTY_HIDDEN, cb); return v7_create_boolean(1); }
ICACHE_FLASH_ATTR static v7_val_t Wifi_setup(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { struct station_config stationConf; v7_val_t ssidv = v7_array_get(v7, args, 0); v7_val_t passv = v7_array_get(v7, args, 1); const char *ssid, *pass; size_t ssid_len, pass_len; int res; if (!v7_is_string(ssidv) || !v7_is_string(passv)) { printf("ssid/pass are not strings\n"); return v7_create_undefined(); } wifi_station_disconnect(); ssid = v7_to_string(v7, &ssidv, &ssid_len); pass = v7_to_string(v7, &passv, &pass_len); stationConf.bssid_set = 0; strncpy((char *) &stationConf.ssid, ssid, 32); strncpy((char *) &stationConf.password, pass, 64); res = v7_create_boolean(wifi_station_set_config(&stationConf)); if (!res) { printf("Failed to set station config\n"); return v7_create_boolean(0); } return v7_create_boolean(wifi_station_connect()); }
static v7_val_t gsm_on_incoming_call(struct v7* v7) { v7_val_t cb = v7_arg(v7, 0); if (!v7_is_function(cb)) { return v7_create_boolean(0); }; v7_set(v7, v7_get_global(v7), INCOMING_CALL_CB, sizeof(INCOMING_CALL_CB) - 1, 0, cb); return v7_create_boolean(1); }
static enum v7_err GPIO_setisr(struct v7 *v7, v7_val_t *res) { enum v7_err rcode = V7_OK; v7_val_t pinv = v7_arg(v7, 0); v7_val_t typev = v7_arg(v7, 1); v7_val_t cb = v7_arg(v7, 2); v7_val_t current_cb; char prop_name[15]; int pin, type, len, has_isr, new_isr_provided; if (!v7_is_number(pinv) || !v7_is_number(typev)) { printf("Invalid arguments\n"); *res = v7_create_boolean(0); goto clean; } pin = v7_to_number(pinv); type = v7_to_number(typev); len = snprintf(prop_name, sizeof(prop_name), "_ih_%d", (int) pin); current_cb = v7_get(v7, v7_get_global(v7), prop_name, len); has_isr = v7_is_function(current_cb); new_isr_provided = v7_is_function(cb); if (!has_isr && !new_isr_provided) { printf("Missing callback\n"); *res = v7_create_boolean(0); goto clean; }; if (has_isr && new_isr_provided && current_cb != cb) { printf("Only one interruption handler is allowed for pin\n"); *res = v7_create_boolean(0); goto clean; } if (type == 0 && has_isr) { v7_set(v7, v7_get_global(v7), prop_name, len, 0, v7_create_undefined()); } else if (!has_isr && new_isr_provided) { v7_set(v7, v7_get_global(v7), prop_name, len, 0, cb); } if (type != 0 && !s_gpio_intr_installed) { sj_gpio_intr_init(gpio_intr_handler_proxy); s_gpio_intr_installed = 1; } *res = v7_create_boolean(sj_gpio_intr_set(pin, type) == 0); goto clean; clean: return rcode; }
static v7_val_t dsleep(struct v7 *v7) { v7_val_t time_v = v7_arg(v7, 0); uint32 time = v7_to_number(time_v); v7_val_t flags_v = v7_arg(v7, 1); uint8 flags = v7_to_number(flags_v); if (!v7_is_number(time_v) || time < 0) return v7_create_boolean(false); if (v7_is_number(flags_v)) { if (!system_deep_sleep_set_option(flags)) return v7_create_boolean(false); } system_deep_sleep(time); return v7_create_boolean(true); }
ICACHE_FLASH_ATTR static v7_val_t Wifi_connect(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { (void) v7; (void) this_obj; (void) args; return v7_create_boolean(wifi_station_connect()); }
static v7_val_t OS_wdt_feed(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { (void) v7; (void) this_obj; (void) args; sj_wdt_feed(); return v7_create_boolean(1); }
static v7_val_t OS_reset(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { (void) v7; (void) this_obj; (void) args; sj_system_restart(); /* Unreachable */ return v7_create_boolean(1); }
static val_t Regex_multiline(struct v7 *v7, val_t this_obj, val_t args) { int flags = 0; val_t r = i_value_of(v7, this_obj); (void) args; if (v7_is_regexp(r)) flags = slre_get_flags(v7_to_regexp(r)->compiled_regexp); return v7_create_boolean(flags & SLRE_FLAG_M); }
ICACHE_FLASH_ATTR static v7_val_t GPIO_in(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { v7_val_t pinv = v7_array_get(v7, args, 0); int pin; if (!v7_is_double(pinv)) { printf("non-numeric pin\n"); return v7_create_undefined(); } pin = v7_to_double(pinv); return v7_create_boolean(read_gpio_pin(pin)); }
/* Call the callback with a list of ssids found in the air. */ static v7_val_t Wifi_scan(struct v7 *v7) { int r; v7_val_t cb = v7_get(v7, s_wifi, "_scb", ~0); if (v7_is_function(cb)) { fprintf(stderr, "scan in progress"); return v7_create_boolean(0); } cb = v7_arg(v7, 0); if (!v7_is_function(cb)) { fprintf(stderr, "invalid argument"); return v7_create_boolean(0); } v7_set(v7, s_wifi, "_scb", ~0, V7_PROPERTY_DONT_ENUM | V7_PROPERTY_HIDDEN, cb); r = sj_wifi_scan(sj_wifi_scan_done); if (r == 0) { v7_set(v7, s_wifi, "_scb", ~0, V7_PROPERTY_DONT_ENUM | V7_PROPERTY_HIDDEN, v7_create_undefined()); } return v7_create_boolean(r); }
static v7_val_t GPIO_write(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { v7_val_t pinv = v7_array_get(v7, args, 0); v7_val_t valv = v7_array_get(v7, args, 1); int pin, val; if (!v7_is_number(pinv)) { printf("non-numeric pin\n"); return v7_create_undefined(); } pin = v7_to_number(pinv); val = v7_to_number(valv); return v7_create_boolean(sj_gpio_write(pin, val) == 0); }
static v7_val_t sj_Wifi_setup(struct v7 *v7) { v7_val_t ssidv = v7_arg(v7, 0); v7_val_t passv = v7_arg(v7, 1); const char *ssid, *pass; size_t ssid_len, pass_len; if (!v7_is_string(ssidv) || !v7_is_string(passv)) { printf("ssid/pass are not strings\n"); return v7_create_undefined(); } ssid = v7_to_string(v7, &ssidv, &ssid_len); pass = v7_to_string(v7, &passv, &pass_len); return v7_create_boolean(sj_wifi_setup_sta(ssid, pass)); }
static v7_val_t GPIO_setmode(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { v7_val_t pinv = v7_array_get(v7, args, 0); v7_val_t modev = v7_array_get(v7, args, 1); v7_val_t pullv = v7_array_get(v7, args, 2); int pin, mode, pull; if (!v7_is_number(pinv) || !v7_is_number(modev) || !v7_is_number(pullv)) { printf("Invalid arguments"); return v7_create_undefined(); } pin = v7_to_number(pinv); mode = v7_to_number(modev); pull = v7_to_number(pullv); return v7_create_boolean(sj_gpio_set_mode(pin, mode, pull) == 0); }
static v7_val_t sj_http_call_helper(struct v7 *v7, v7_val_t urlv, v7_val_t bodyv, v7_val_t cb, const char *method) { const char *body = NULL; size_t url_len, body_len = 0; if (!v7_is_string(urlv)) { v7_throw(v7, "url should be a string"); } if (v7_is_string(bodyv)) { body = v7_to_string(v7, &bodyv, &body_len); } return v7_create_boolean(sj_http_call(v7, v7_to_string(v7, &urlv, &url_len), body, body_len, method, cb)); }
static enum v7_err GPIO_setmode(struct v7 *v7, v7_val_t *res) { v7_val_t pinv = v7_arg(v7, 0); v7_val_t modev = v7_arg(v7, 1); v7_val_t pullv = v7_arg(v7, 2); int pin, mode, pull; if (!v7_is_number(pinv) || !v7_is_number(modev) || !v7_is_number(pullv)) { printf("Invalid arguments"); *res = v7_create_undefined(); } else { pin = v7_to_number(pinv); mode = v7_to_number(modev); pull = v7_to_number(pullv); *res = v7_create_boolean(sj_gpio_set_mode(pin, mode, pull) == 0); } return V7_OK; }
static enum v7_err GPIO_write(struct v7 *v7, v7_val_t *res) { v7_val_t pinv = v7_arg(v7, 0); v7_val_t valv = v7_arg(v7, 1); int pin, val; if (!v7_is_number(pinv)) { printf("non-numeric pin\n"); *res = v7_create_undefined(); } else { pin = v7_to_number(pinv); /* * We assume 0 if the value is "falsy", * and 1 if the value is "truthy" */ val = !!v7_is_true(v7, valv); *res = v7_create_boolean(sj_gpio_write(pin, val) == 0); } return V7_OK; }
static v7_val_t Wifi_disconnect(struct v7 *v7) { return v7_create_boolean(sj_wifi_disconnect()); }
static val_t Regex_test(struct v7 *v7, val_t this_obj, val_t args) { return v7_create_boolean(!v7_is_null(Regex_exec(v7, this_obj, args))); }