Example #1
0
void sj_timers_api_setup(struct v7 *v7) {
  v7_set_method(v7, v7_get_global(v7), "setTimeout", global_setTimeout);
  v7_set_method(v7, v7_get_global(v7), "setInterval", global_setInterval);
  v7_set_method(v7, v7_get_global(v7), "clearTimeout",
                global_clearTimeoutOrInterval);
  v7_set_method(v7, v7_get_global(v7), "clearInterval",
                global_clearTimeoutOrInterval);
}
Example #2
0
void sj_v7_ext_api_setup(struct v7 *v7) {
  v7_val_t gc;

  v7_set_method(v7, v7_get_global(v7), "usleep", global_usleep);

  gc = v7_mk_object(v7);
  v7_set(v7, v7_get_global(v7), "GC", ~0, gc);
  v7_set_method(v7, gc, "stat", GC_stat);
  v7_set_method(v7, gc, "gc", GC_gc);
}
Example #3
0
SJ_PRIVATE 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_mk_boolean(v7, 0);
    goto clean;
  }

  pin = v7_get_double(v7, pinv);
  type = v7_get_double(v7, 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_callable(v7, current_cb);
  new_isr_provided = v7_is_callable(v7, cb);

  if (!has_isr && !new_isr_provided) {
    printf("Missing callback\n");
    *res = v7_mk_boolean(v7, 0);
    goto clean;
  };

  if (has_isr && new_isr_provided && current_cb != cb) {
    printf("Only one interruption handler is allowed for pin\n");
    *res = v7_mk_boolean(v7, 0);
    goto clean;
  }

  if (type == 0 && has_isr) {
    v7_set(v7, v7_get_global(v7), prop_name, len, V7_UNDEFINED);
  } else if (!has_isr && new_isr_provided) {
    v7_set(v7, v7_get_global(v7), prop_name, len, cb);
  }

  if (type != 0 && !s_gpio_intr_installed) {
    sj_gpio_intr_init(gpio_intr_handler_proxy, v7);
    s_isr_cb_proxy_v = v7_mk_cfunction(isr_cb_proxy);
    v7_own(v7, &s_isr_cb_proxy_v);
    s_gpio_intr_installed = 1;
  }
  *res =
      v7_mk_boolean(v7, sj_gpio_intr_set(pin, (enum gpio_int_mode) type) == 0);
  goto clean;

clean:
  return rcode;
}
Example #4
0
void init_v7(void *stack_base) {
  struct v7_create_opts opts;
  v7_val_t dht11, debug;

  opts.object_arena_size = 164;
  opts.function_arena_size = 26;
  opts.property_arena_size = 400;
  opts.c_stack_base = stack_base;
  v7 = v7_create_opt(opts);

  v7_set_method(v7, v7_get_global(v7), "dsleep", dsleep);
  v7_set_method(v7, v7_get_global(v7), "crash", crash);

#if V7_ESP_ENABLE__DHT11
  dht11 = v7_create_object(v7);
  v7_set(v7, v7_get_global(v7), "DHT11", 5, 0, dht11);
  v7_set_method(v7, dht11, "read", DHT11_read);
#else
  (void) dht11;
#endif /* V7_ESP_ENABLE__DHT11 */

  debug = v7_create_object(v7);
  v7_set(v7, v7_get_global(v7), "Debug", 5, 0, debug);
  v7_set_method(v7, debug, "mode", Debug_mode);
  v7_set_method(v7, debug, "print", Debug_print);

  sj_init_timers(v7);
  sj_init_v7_ext(v7);

  init_gpiojs(v7);
  init_adcjs(v7);
  init_i2cjs(v7);
  init_pwm(v7);
  init_spijs(v7);
  init_wifi(v7);

  mongoose_init();
  sj_init_http(v7);
  sj_init_ws_client(v7);

  /* NOTE(lsm): must be done after mongoose_init(). */
  init_device(v7);

#ifndef DISABLE_OTA
  init_updater(v7);
#endif

#ifndef DISABLE_C_CLUBBY
  sj_init_clubby(v7);
#endif

  v7_gc(v7, 1);
}
Example #5
0
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;
}
Example #6
0
void sj_init_uart(struct v7 *v7) {
  v7_val_t uart = v7_create_undefined(), uart_proto = v7_create_undefined(),
           uart_ctor = v7_create_undefined();
  v7_own(v7, &uart);
  v7_own(v7, &uart_proto);
  v7_own(v7, &uart_ctor);

  uart = v7_create_object(v7);
  uart_proto = v7_create_object(v7);
  uart_ctor = v7_create_constructor(v7, uart_proto, UART_ctor);

  v7_set(v7, uart, "dev", ~0, V7_PROPERTY_HIDDEN, uart_ctor);
  v7_set_method(v7, uart_proto, "read", UART_read);
  v7_set_method(v7, uart_proto, "write", UART_write);
  v7_set_method(v7, uart_proto, "recv", UART_recv);
  v7_set(v7, v7_get_global(v7), "UART", ~0, 0, uart);
  {
    enum v7_err rcode = v7_exec(
        v7, "UART.open = function (d) { return new UART.dev(d); }", NULL);
    assert(rcode == V7_OK);
#if defined(NDEBUG)
    (void) rcode;
#endif
  }

  v7_disown(v7, &uart_ctor);
  v7_disown(v7, &uart_proto);
  v7_disown(v7, &uart);
}
Example #7
0
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);
}
Example #8
0
void sj_init_uart(struct v7 *v7) {
  v7_val_t uart = V7_UNDEFINED, uart_proto = V7_UNDEFINED,
           uart_ctor = V7_UNDEFINED;
  v7_own(v7, &uart);
  v7_own(v7, &uart_proto);
  v7_own(v7, &uart_ctor);

  uart = v7_mk_object(v7);
  uart_proto = v7_mk_object(v7);
  uart_ctor = v7_mk_function_with_proto(v7, UART_ctor, uart_proto);

  v7_def(v7, uart, "dev", ~0, _V7_DESC_HIDDEN(1), uart_ctor);
  v7_set_method(v7, uart_proto, "read", UART_read);
  v7_set_method(v7, uart_proto, "write", UART_write);
  v7_set_method(v7, uart_proto, "recv", UART_recv);
  v7_set(v7, v7_get_global(v7), "UART", ~0, uart);
  {
    enum v7_err rcode = v7_exec(
        v7, "UART.open = function (d) { return new UART.dev(d); }", NULL);
    assert(rcode == V7_OK);
#if defined(NDEBUG)
    (void) rcode;
#endif
  }

  v7_disown(v7, &uart_ctor);
  v7_disown(v7, &uart_proto);
  v7_disown(v7, &uart);
}
Example #9
0
void sj_gpio_api_setup(struct v7 *v7) {
  v7_val_t gpio = v7_mk_object(v7);
  v7_set(v7, v7_get_global(v7), "GPIO", ~0, gpio);
  v7_set_method(v7, gpio, "setMode", GPIO_setMode);
  v7_set_method(v7, gpio, "read", GPIO_read);
  v7_set_method(v7, gpio, "write", GPIO_write);
  v7_set_method(v7, gpio, "setISR", GPIO_setISR);

  v7_set(v7, gpio, "INOUT", ~0, v7_mk_number(v7, GPIO_MODE_INOUT));
  v7_set(v7, gpio, "IN", ~0, v7_mk_number(v7, GPIO_MODE_INPUT));
  v7_set(v7, gpio, "OUT", ~0, v7_mk_number(v7, GPIO_MODE_OUTPUT));

  v7_set(v7, gpio, "FLOAT", ~0, v7_mk_number(v7, GPIO_PULL_FLOAT));
  v7_set(v7, gpio, "PULLUP", ~0, v7_mk_number(v7, GPIO_PULL_PULLUP));
  v7_set(v7, gpio, "PULLDOWN", ~0, v7_mk_number(v7, GPIO_PULL_PULLDOWN));

  v7_set(v7, gpio, "OFF", ~0, v7_mk_number(v7, GPIO_INTR_OFF));
  v7_set(v7, gpio, "POSEDGE", ~0, v7_mk_number(v7, GPIO_INTR_POSEDGE));
  v7_set(v7, gpio, "NEGEDGE", ~0, v7_mk_number(v7, GPIO_INTR_NEGEDGE));
  v7_set(v7, gpio, "ANYEDGE", ~0, v7_mk_number(v7, GPIO_INTR_ANYEDGE));
  v7_set(v7, gpio, "LOLEVEL", ~0, v7_mk_number(v7, GPIO_INTR_LOLEVEL));
  v7_set(v7, gpio, "HILEVEL", ~0, v7_mk_number(v7, GPIO_INTR_HILEVEL));
  /*
   * TODO(mkm): figure out what to do with this "esp specific" mode.
   * It's not really ESP specific, but the soft debouncer is currently
   * implemented in esp8266 platform code.
   */
  v7_set(v7, gpio, "CLICK", ~0,
         v7_mk_number(v7, 6 /* GPIO_INTR_TYPE_ONCLICK */));

  if (v7_exec_file(v7, "gpio.js", NULL) != V7_OK) {
    /* TODO(mkm): make setup functions return an error code */
    abort();
  }
}
Example #10
0
File: js_oop.c Project: QUSIR/v7
int main(void) {
    enum v7_err rcode = V7_OK;
    struct v7 *v7 = v7_create();
    v7_val_t ctor_func, proto, eval_result;

    proto = v7_mk_object(v7);
    ctor_func = v7_mk_function_with_proto(v7, MyThing_ctor, proto);
    v7_def(v7, ctor_func, "MY_CONST", ~0,
           (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), v7_mk_number(123));
    v7_set_method(v7, proto, "myMethod", &MyThing_myMethod);
    v7_set(v7, v7_get_global(v7), "MyThing", ~0, ctor_func);

    rcode = v7_exec(v7,
                    "\
      print('MyThing.MY_CONST = ', MyThing.MY_CONST); \
      var t = new MyThing(456); \
      print('t.MY_CONST = ', t.MY_CONST); \
      print('t.myMethod = ', t.myMethod); \
      print('t.myMethod() = ', t.myMethod());",
                    &eval_result);
    if (rcode != V7_OK) {
        fprintf(stderr, "exec error: %d\n", (int) rcode);
    }

    v7_destroy(v7);
    return (int) rcode;
}
Example #11
0
void sj_init_simple_http_client(struct v7 *v7) {
  v7_val_t http;

  http = v7_create_object(v7);
  v7_set(v7, v7_get_global(v7), "Http", 4, 0, http);
  v7_set_method(v7, http, "get", sj_http_get);
  v7_set_method(v7, http, "post", sj_http_post);
}
Example #12
0
void sj_gpio_api_setup(struct v7 *v7) {
  s_v7 = v7;
  v7_val_t gpio = v7_mk_object(v7);
  v7_set(v7, v7_get_global(v7), "GPIO", ~0, gpio);
  v7_set_method(v7, gpio, "setmode", GPIO_setmode);
  v7_set_method(v7, gpio, "read", GPIO_read);
  v7_set_method(v7, gpio, "write", GPIO_write);
  v7_set_method(v7, gpio, "setisr", GPIO_setisr);
}
Example #13
0
void init_v7(void *stack_base) {
  struct v7_mk_opts opts;

#ifdef V7_THAW
  opts.object_arena_size = 85;
  opts.function_arena_size = 16;
  opts.property_arena_size = 170;
#else
  opts.object_arena_size = 164;
  opts.function_arena_size = 26;
  opts.property_arena_size = 400;
#endif
  opts.c_stack_base = stack_base;
  v7 = v7_create_opt(opts);

  v7_set_method(v7, v7_get_global(v7), "dsleep", dsleep);
  v7_set_method(v7, v7_get_global(v7), "crash", crash);
}
Example #14
0
void miot_console_api_setup(struct v7 *v7) {
  v7_val_t console_v = v7_mk_object(v7);
  v7_own(v7, &console_v);

  v7_set_method(v7, console_v, "log", Console_log);
  v7_set(v7, v7_get_global(v7), "console", ~0, console_v);

  v7_disown(v7, &console_v);
}
Example #15
0
void init_gpiojs(struct v7 *v7) {
  s_v7 = v7;
  v7_val_t gpio = v7_create_object(v7);
  v7_set(v7, v7_get_global(v7), "GPIO", ~0, 0, gpio);
  v7_set_method(v7, gpio, "setmode", GPIO_setmode);
  v7_set_method(v7, gpio, "read", GPIO_read);
  v7_set_method(v7, gpio, "write", GPIO_write);
  v7_set_method(v7, gpio, "setisr", GPIO_setisr);
}
Example #16
0
static void export_read_only_vars_to_v7(struct v7 *v7) {
  struct ro_var *rv;
  if (v7 == NULL) return;
  v7_val_t obj = v7_mk_object(v7);
  for (rv = g_ro_vars; rv != NULL; rv = rv->next) {
    v7_set(v7, obj, rv->name, ~0, v7_mk_string(v7, *rv->ptr, ~0, 1));
  }
  v7_val_t Sys = v7_get(v7, v7_get_global(v7), "Sys", ~0);
  v7_set(v7, Sys, "ro_vars", ~0, obj);
}
Example #17
0
void sj_init_v7_ext(struct v7 *v7) {
  v7_val_t os, gc;

  v7_set(v7, v7_get_global(v7), "version", 7, 0,
         v7_create_string(v7, sj_version, strlen(sj_version), 1));

  v7_set_method(v7, v7_get_global(v7), "usleep", global_usleep);

  gc = v7_create_object(v7);
  v7_set(v7, v7_get_global(v7), "GC", 2, 0, gc);
  v7_set_method(v7, gc, "stat", GC_stat);
  v7_set_method(v7, gc, "gc", GC_gc);

  os = v7_create_object(v7);
  v7_set(v7, v7_get_global(v7), "OS", 2, 0, os);
  v7_set_method(v7, os, "prof", OS_prof);
  v7_set_method(v7, os, "wdt_feed", OS_wdt_feed);
  v7_set_method(v7, os, "reset", OS_reset);
}
Example #18
0
void sj_init_sys(struct v7 *v7) {
  v7_val_t sys;

  sys = v7_mk_object(v7);
  v7_set(v7, v7_get_global(v7), "Sys", ~0, sys);
  v7_set_method(v7, sys, "prof", Sys_prof);
  v7_set_method(v7, sys, "wdtFeed", Sys_wdtFeed);
  v7_set_method(v7, sys, "reboot", Sys_reboot);
  v7_set_method(v7, sys, "setLogLevel", Sys_setLogLevel);
}
Example #19
0
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);
}
Example #20
0
static void call_sum(struct v7 *v7) {
  v7_val_t func, result, args;

  func = v7_get(v7, v7_get_global(v7), "sum", 3);

  args = v7_mk_array(v7);
  v7_array_push(v7, args, v7_mk_number(123.0));
  v7_array_push(v7, args, v7_mk_number(456.789));

  v7_apply(v7, func, v7_mk_undefined(), args, &result);
  printf("Result: %g\n", v7_to_number(result));
}
Example #21
0
clubby_handle_t console_get_current_clubby(struct v7 *v7) {
  v7_val_t clubby_v = v7_get(v7, v7_get_global(v7), s_clubby_prop, ~0);
  if (!v7_is_object(clubby_v)) {
    LOG(LL_ERROR, ("Clubby is not set"));
    return NULL;
  }

  clubby_handle_t ret = sj_clubby_get_handle(v7, clubby_v);
  LOG(LL_DEBUG, ("clubby handle: %p", ret));

  return ret;
}
Example #22
0
void js_init_gsm(struct v7* v7)
{
    vm_gsm_tel_call_reg_listener(call_listener_func);
    
    v7_val_t gsm = v7_create_object(v7);
    v7_set(v7, v7_get_global(v7), "gsm", 3, 0, gsm);
    v7_set_method(v7, gsm, "call", gsm_call);
    v7_set_method(v7, gsm, "answer", gsm_answer);
    v7_set_method(v7, gsm, "hang", gsm_hang);
    v7_set_method(v7, gsm, "on_incoming_call", gsm_on_incoming_call);
    v7_set_method(v7, gsm, "text", gsm_text);
    v7_set_method(v7, gsm, "on_new_message", gsm_on_new_message);
}
Example #23
0
void sj_wifi_init(struct v7 *v7) {
  s_v7 = v7;
  s_wifi = v7_create_object(v7);
  v7_set_method(v7, s_wifi, "setup", sj_Wifi_setup);
  v7_set_method(v7, s_wifi, "connect", Wifi_connect);
  v7_set_method(v7, s_wifi, "disconnect", Wifi_disconnect);
  v7_set_method(v7, s_wifi, "status", Wifi_status);
  v7_set_method(v7, s_wifi, "show", Wifi_show);
  v7_set_method(v7, s_wifi, "ip", Wifi_ip);
  v7_set_method(v7, s_wifi, "changed", Wifi_changed);
  v7_set_method(v7, s_wifi, "scan", Wifi_scan);
  v7_set(v7, v7_get_global(v7), "Wifi", 4, 0, s_wifi);
}
Example #24
0
int main(void) {
  enum v7_err rcode = V7_OK;
  struct v7 *v7 = v7_create();
  v7_val_t result;
  v7_set_method(v7, v7_get_global(v7), "sum", &js_sum);

  rcode = v7_exec(v7, "print(sum(1.2, 3.4))", &result);
  if (rcode != V7_OK) {
    v7_print_error(stderr, v7, "Evaluation error", result);
  }

  v7_destroy(v7);
  return (int) rcode;
}
Example #25
0
static enum v7_err isr_cb_proxy(struct v7 *v7, v7_val_t *res) {
  v7_val_t cb = v7_arg(v7, 0);
  v7_val_t args = v7_arg(v7, 1);

  v7_own(v7, &cb);
  v7_own(v7, &args);

  enum v7_err ret = v7_apply(v7, cb, v7_get_global(v7), args, res);

  sj_reenable_intr(v7_get_double(v7, v7_array_get(v7, args, 0)));

  v7_disown(v7, &args);
  v7_disown(v7, &cb);

  return ret;
}
Example #26
0
void sj_invoke_cb2(struct v7 *v7, v7_val_t cb, v7_val_t arg1, v7_val_t arg2) {
  v7_val_t args;
  v7_own(v7, &cb);
  v7_own(v7, &arg1);
  v7_own(v7, &arg2);

  args = v7_create_array(v7);
  v7_own(v7, &args);
  v7_array_push(v7, args, arg1);
  v7_array_push(v7, args, arg2);
  sj_invoke_cb(v7, cb, v7_get_global(v7), args);
  v7_disown(v7, &args);
  v7_disown(v7, &arg2);
  v7_disown(v7, &arg1);
  v7_disown(v7, &cb);
}
Example #27
0
void sj_sys_js_init(struct v7 *v7) {
  v7_val_t sys, fs;

  sys = v7_mk_object(v7);
  v7_set(v7, v7_get_global(v7), "Sys", ~0, sys);
  v7_set_method(v7, sys, "prof", Sys_prof);
  v7_set_method(v7, sys, "reboot", Sys_reboot);
  v7_set_method(v7, sys, "setLogLevel", Sys_setLogLevel);
  v7_set_method(v7, sys, "time", Sys_time);
  v7_set_method(v7, sys, "wdtFeed", Sys_wdtFeed);
  v7_set_method(v7, sys, "wdtSetTimeout", Sys_wdtSetTimeout);
  v7_set_method(v7, sys, "wdtEnable", Sys_wdtEnable);
  v7_set_method(v7, sys, "wdtDisable", Sys_wdtDisable);
  fs = v7_mk_object(v7);
  v7_set(v7, sys, "fs", ~0, fs);
  v7_set_method(v7, fs, "free", Sys_fs_getFreeSpace);
}
Example #28
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);
}
Example #29
0
void sj_mqtt_api_setup(struct v7 *v7) {
  v7_val_t mqtt_proto = v7_mk_object(v7);
  v7_val_t mqtt_connect =
      v7_mk_function_with_proto(v7, sj_mqtt_connect, mqtt_proto);
  v7_val_t mqtt;
  v7_own(v7, &mqtt_connect);

  mqtt = v7_mk_object(v7);
  v7_own(v7, &mqtt);

  v7_set(v7, mqtt, "connect", ~0, mqtt_connect);
  v7_def(v7, mqtt, "proto", ~0, V7_DESC_ENUMERABLE(0), mqtt_proto);

  v7_set_method(v7, mqtt_proto, "publish", MQTT_publish);
  v7_set_method(v7, mqtt_proto, "subscribe", MQTT_subscribe);
  v7_set_method(v7, mqtt_proto, "on", MQTT_on);
  v7_set(v7, v7_get_global(v7), "MQTT", ~0, mqtt);

  v7_disown(v7, &mqtt);
  v7_disown(v7, &mqtt_connect);
}
Example #30
0
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_callable(s_v7, cb)) {
    return;
  }

  args = v7_mk_array(s_v7);
  v7_array_push(s_v7, args, v7_mk_number(pin));
  v7_array_push(s_v7, args, v7_mk_number(level));

  if (v7_apply(s_v7, cb, v7_mk_undefined(), args, &res) != V7_OK) {
    /* TODO(mkm): make it print stack trace */
    fprintf(stderr, "cb threw an exception\n");
  }
}