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 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); }
/* Currently can only handle one timer */ ICACHE_FLASH_ATTR static v7_val_t set_timeout(struct v7 *v7, v7_val_t this_obj, v7_val_t args) { v7_val_t cb = v7_array_get(v7, args, 0); v7_val_t msecsv = v7_array_get(v7, args, 1); int msecs; if (!v7_is_function(cb)) { printf("cb is not a function\n"); return v7_create_undefined(); } if (!v7_is_double(msecsv)) { printf("msecs is not a double\n"); return v7_create_undefined(); } msecs = v7_to_double(msecsv); /* * used to convey the callback to the timer handler _and_ to root * the function so that the GC doesn't deallocate it. */ v7_set(v7, v7_get_global_object(v7), "_js_timeout_handler", 19, 0, cb); os_timer_disarm(&js_timeout_timer); os_timer_setfn(&js_timeout_timer, js_timeout, NULL); os_timer_arm(&js_timeout_timer, msecs, 0); return v7_create_undefined(); }
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); }
/* 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 void gpio_intr_handler_proxy(int pin, int level) { char prop_name[15]; int len; v7_val_t args; len = snprintf(prop_name, sizeof(prop_name), "_ih_%d", (int) pin); v7_val_t cb = v7_get(s_v7, v7_get_global_object(s_v7), prop_name, len); if (!v7_is_function(cb)) { return; } args = v7_create_array(s_v7); v7_array_push(s_v7, args, v7_create_number(pin)); v7_array_push(s_v7, args, v7_create_number(level)); v7_apply(s_v7, cb, v7_create_undefined(), args); }
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_function(cb)) { v7_throw(v7, "cb must be a function"); } 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 void gpio_intr_handler_proxy(int pin, int level) { char prop_name[15]; int len; v7_val_t res, args; len = snprintf(prop_name, sizeof(prop_name), "_ih_%d", (int) pin); v7_val_t cb = v7_get(s_v7, v7_get_global(s_v7), prop_name, len); if (!v7_is_function(cb)) { return; } args = v7_create_array(s_v7); v7_array_push(s_v7, args, v7_create_number(pin)); v7_array_push(s_v7, args, v7_create_number(level)); if (v7_apply(s_v7, cb, v7_create_undefined(), args, &res) != V7_OK) { /* TODO(mkm): make it print stack trace */ fprintf(stderr, "cb threw an exception\n"); } }
/* Currently can only handle one timer */ static v7_val_t global_set_timeout(struct v7 *v7) { v7_val_t *cb; v7_val_t msecsv = v7_arg(v7, 1); int msecs; cb = (v7_val_t *) malloc(sizeof(*cb)); v7_own(v7, cb); *cb = v7_arg(v7, 0); if (!v7_is_function(*cb)) { printf("cb is not a function\n"); return v7_create_undefined(); } if (!v7_is_number(msecsv)) { printf("msecs is not a double\n"); return v7_create_undefined(); } msecs = v7_to_number(msecsv); sj_set_timeout(msecs, cb); return v7_create_undefined(); }
void sj_wifi_scan_done(const char **ssids) { struct v7 *v7 = s_v7; v7_val_t cb = v7_get(v7, s_wifi, "_scb", ~0); v7_val_t res = v7_create_undefined(); const char **p; if (!v7_is_function(cb)) return; v7_own(v7, &res); if (ssids != NULL) { res = v7_create_array(v7); for (p = ssids; *p != NULL; p++) { v7_array_push(v7, res, v7_create_string(v7, *p, strlen(*p), 1)); } } else { res = v7_create_undefined(); } sj_invoke_cb1(v7, cb, res); v7_disown(v7, &res); v7_set(v7, s_wifi, "_scb", ~0, V7_PROPERTY_DONT_ENUM | V7_PROPERTY_HIDDEN, v7_create_undefined()); }
void call_listener_func(vm_gsm_tel_call_listener_data_t* data) { vm_log_info("call_listener_func"); if(data->call_type == VM_GSM_TEL_INDICATION_INCOMING_CALL) { vm_gsm_tel_call_info_t* ind = (vm_gsm_tel_call_info_t*)data->data; g_uid_info.call_id = ind->uid_info.call_id; g_uid_info.group_id = ind->uid_info.group_id; g_uid_info.sim = ind->uid_info.sim; strcpy(g_incoming_number, (char*)ind->num_uri); g_call_status = RECEIVINGCALL; vm_log_info("incoming call"); // to-do: js callback { v7_val_t cb = v7_get(v7, v7_get_global(v7), INCOMING_CALL_CB, sizeof(INCOMING_CALL_CB) - 1); if (v7_is_function(cb)) { v7_val_t res; v7_val_t number = v7_create_string(v7, g_incoming_number, strlen(g_incoming_number), 1); if (v7_apply(v7, &res, cb, v7_create_undefined(), number) != V7_OK) { /* TODO(mkm): make it print stack trace */ fprintf(stderr, "cb threw an exception\n"); } } } } else if(data->call_type == VM_GSM_TEL_INDICATION_OUTGOING_CALL) { vm_gsm_tel_call_info_t* ind = (vm_gsm_tel_call_info_t*)data->data; g_uid_info.call_id = ind->uid_info.call_id; g_uid_info.group_id = ind->uid_info.group_id; g_uid_info.sim = ind->uid_info.sim; strcpy(g_incoming_number, (char*)ind->num_uri); g_call_status = CALLING; vm_log_info("calling"); } else if(data->call_type == VM_GSM_TEL_INDICATION_CONNECTED) { vm_gsm_tel_connect_indication_t* ind = (vm_gsm_tel_connect_indication_t*)data->data; g_uid_info.call_id = ind->uid_info.call_id; g_uid_info.group_id = ind->uid_info.group_id; g_uid_info.sim = ind->uid_info.sim; g_call_status = TALKING; vm_log_info("connected"); } else if(data->call_type == VM_GSM_TEL_INDICATION_CALL_ENDED) { g_call_status = IDLE_CALL; vm_log_info("endded"); } else { vm_log_info("bad operation type"); } vm_log_info("g_call_status is %d", g_call_status); }