static be_jse_symbol_t * uart_close(void){ int8_t ret = -1; int8_t result = -1; item_handle_t uart_handle; be_jse_symbol_t * arg0 = NULL; uart_dev_t * uart_device = NULL; be_jse_handle_function(0,&arg0,NULL,NULL,NULL); if(!arg0 || !symbol_is_int(arg0)){ goto out; } uart_handle.handle = get_symbol_value_int(arg0); uart_device = board_get_node_by_handle(MODULE_UART,&uart_handle); if(NULL == uart_device){ be_error("uart","board_get_node_by_handle fail!\n"); goto out; } ret = hal_uart_finalize(uart_device); if(0 != ret){ be_error("uart","hal_uart_init fail!\n"); goto out; } board_disattach_item(MODULE_UART,&uart_handle); uart_del_recv(uart_handle.handle); result = 0; out: symbol_unlock(arg0); return new_int_symbol(result); }
static be_jse_symbol_t *i2c_close(void) { int8_t ret = -1; int8_t result = -1; item_handle_t i2c_handle; be_jse_symbol_t *arg0 = NULL; i2c_dev_t *i2c_device = NULL; be_jse_handle_function(0, &arg0, NULL, NULL, NULL); if (!arg0 || !symbol_is_int(arg0)) { goto out; } i2c_handle.handle = get_symbol_value_int(arg0); i2c_device = board_get_node_by_handle(MODULE_I2C, &i2c_handle); if (NULL == i2c_device) { be_error("i2c", "board_get_node_by_handle fail!\n"); goto out; } ret = hal_i2c_finalize(i2c_device); if (0 != ret) { be_error("i2c", "hal_i2c_finalize fail!\n"); goto out; } board_disattach_item(MODULE_I2C, &i2c_handle); result = 0; out: symbol_unlock(arg0); return new_int_symbol(result); }
static be_jse_symbol_t *i2c_write(void) { int8_t ret = -1; int8_t result = -1; uint8_t *data = NULL; uint32_t len = 0; uint32_t i = 0; item_handle_t i2c_handle; be_jse_symbol_t *item = NULL; be_jse_symbol_t *arg0 = NULL; be_jse_symbol_t *arg1 = NULL; i2c_dev_t *i2c_device = NULL; be_jse_handle_function(0, &arg0, &arg1, NULL, NULL); if (!arg0 || !symbol_is_int(arg0)) { goto out; } i2c_handle.handle = get_symbol_value_int(arg0); i2c_device = board_get_node_by_handle(MODULE_I2C, &i2c_handle); if (NULL == i2c_device) { be_error("i2c", "board_get_node_by_handle fail!\n"); goto out; } if (!arg1 || !symbol_is_array(arg1)) { goto out; } len = get_symbol_array_length(arg1); data = calloc(1, sizeof(uint8_t) * (len + 1)); if (NULL == data) { goto out; } for (i = 0; i < len; ++i) { item = get_symbol_array_item(arg1, i); if (!item || !symbol_is_int(item)) { goto out; } data[i] = (uint8_t)get_symbol_value_int(item); symbol_unlock(item); } ret = hal_i2c_master_send(i2c_device, i2c_device->config.dev_addr, data, len, I2C_TIMEOUT); if (-1 == ret) { be_error("i2c", "hal_i2c_master_send fail!\n"); goto out; } result = 0; out: symbol_unlock(arg0); symbol_unlock(arg1); if (NULL != data) { free(data); data = NULL; } return new_int_symbol(len); }
static be_jse_symbol_t *i2c_read(void) { int8_t ret = -1; int8_t result = -1; uint8_t *data = NULL; uint32_t len = 0; uint32_t i = 0; item_handle_t i2c_handle; be_jse_symbol_t *arr = NULL; be_jse_symbol_t *item = NULL; be_jse_symbol_t *arg0 = NULL; be_jse_symbol_t *arg1 = NULL; i2c_dev_t *i2c_device = NULL; be_jse_handle_function(0, &arg0, &arg1, NULL, NULL); if (!arg0 || !symbol_is_int(arg0)) { goto out; } i2c_handle.handle = get_symbol_value_int(arg0); i2c_device = board_get_node_by_handle(MODULE_I2C, &i2c_handle); if (NULL == i2c_device) { be_error("i2c", "board_get_node_by_handle fail!\n"); goto out; } if (!arg1 || !symbol_is_int(arg1)) { goto out; } len = get_symbol_value_int(arg1); data = calloc(1, sizeof(uint8_t) * (len + 1)); if (NULL == data) { goto out; } ret = hal_i2c_master_recv(i2c_device, i2c_device->config.dev_addr, data, len, I2C_TIMEOUT); if (-1 == ret) { be_error("i2c", "hal_i2c_master_recv fail!\n"); goto out; } arr = new_symbol(BE_SYM_ARRAY); for (i = 0; i < len; ++i) { be_jse_symbol_t *idx = new_int_symbol(data[i]); symbol_array_push(arr, idx); symbol_unlock(idx); } result = 0; out: symbol_unlock(arg0); symbol_unlock(arg1); if (NULL != data) { free(data); data = NULL; } return (0 == result) ? arr : new_symbol(BE_SYM_NULL); }
/********************************************************* *Function: module_net_connect *Description: js native addon for NET.connect *Called by: js api *Input: NET.connect(options),options is a object like this { addr: 'tcp://127.0.0.1:1234', onconnect: function(conn) {}, ondata: function(conn, data){}, onclose: function(conn) {}, onerror: function(err) {}, } *Output: 0 try connect ok ,other try connect fail *********************************************************/ static be_jse_symbol_t* module_net_connect() { be_jse_symbol_t *arg0 = NULL; int ret = -1; be_jse_handle_function(0, &arg0,NULL,NULL,NULL); if ( (arg0 == NULL) || !symbol_is_object(arg0) ){ be_error(JS_NET_TAG,"module_net_connect param invalid"); goto done; } be_jse_symbol_t* addrNetSymbol = unlock_symbol_value(lookup_named_child_symbol(get_symbol_node_id(arg0),"addr", false)); be_jse_symbol_t* onConnNetSymbol = unlock_symbol_value(lookup_named_child_symbol(get_symbol_node_id(arg0), "onconnect", false)); be_jse_symbol_t* onDataNetSymbol = unlock_symbol_value(lookup_named_child_symbol(get_symbol_node_id(arg0), "ondata", false)); be_jse_symbol_t* onCloseNetSymbol = unlock_symbol_value(lookup_named_child_symbol(get_symbol_node_id(arg0), "onclose", false)); be_jse_symbol_t* onErrorNetSymbol = unlock_symbol_value(lookup_named_child_symbol(get_symbol_node_id(arg0), "onerror", false)); if ( addrNetSymbol == NULL || onConnNetSymbol == NULL ){ be_error(JS_NET_TAG,"module_net_connect param invalid"); goto done; } net_options_t *p_net_options = calloc(1,sizeof(net_options_t)); p_net_options->addr = calloc(1,symbol_str_len(addrNetSymbol) + 1); if (!p_net_options || p_net_options->addr == NULL) goto done; /*because be_jse_async_event_cb will auto DEC_SYMBL_REF func,so need INC_SYMBL_REF before*/ symbol_to_str(addrNetSymbol, p_net_options->addr, symbol_str_len(addrNetSymbol)); symbol_unlock(addrNetSymbol); if(onConnNetSymbol && symbol_is_function(onConnNetSymbol)){ p_net_options->onconn_cb = (onconnect)onConnNetSymbol; INC_SYMBL_REF(onConnNetSymbol); } if(onDataNetSymbol && symbol_is_function(onDataNetSymbol)){ p_net_options->ondata_cb = (ondata)onDataNetSymbol; INC_SYMBL_REF(onDataNetSymbol); } if(onCloseNetSymbol && symbol_is_function(onCloseNetSymbol)){ p_net_options->onclose_cb = (onclose)onCloseNetSymbol; INC_SYMBL_REF(onCloseNetSymbol); } if(onErrorNetSymbol && symbol_is_function(onErrorNetSymbol)){ p_net_options->onerror_cb = (onerror)onErrorNetSymbol; INC_SYMBL_REF(onErrorNetSymbol); } ret = be_osal_new_task("net_connect",task_net_connect_fun,p_net_options,4096,NULL); done: symbol_unlock(arg0); return new_int_symbol(ret); }
static be_jse_symbol_t *i2c_open(void) { int32_t len = -1; char *data = NULL; int8_t ret = -1; int8_t result = -1; item_handle_t i2c_handle; i2c_handle.handle = 0xFFFFFFFF; be_jse_symbol_t *arg0 = NULL; i2c_dev_t *i2c_device = NULL; be_jse_handle_function(0, &arg0, NULL, NULL, NULL); if (!arg0 || !symbol_is_string(arg0)) { goto out; } len = symbol_str_len(arg0); data = calloc(1, sizeof(char) * (len + 1)); if (NULL == data) { goto out; } symbol_to_str(arg0, data, len); ret = board_attach_item(MODULE_I2C, data, &i2c_handle); if (0 != ret) { be_error("i2c", "board_attach_item fail!\n"); goto out; } be_debug("i2c", "i2c handle:%u\n", i2c_handle.handle); i2c_device = board_get_node_by_handle(MODULE_I2C, &i2c_handle); if (NULL == i2c_device) { be_error("i2c", "board_get_node_by_handle fail!\n"); goto out; } ret = hal_i2c_init(i2c_device); if (0 != ret) { be_error("i2c", "hal_i2c_init fail!\n"); goto out; } result = 0; out: if (NULL != data) { free(data); data = NULL; } symbol_unlock(arg0); if (0 != result) { board_disattach_item(MODULE_I2C, &i2c_handle); return new_int_symbol(-1); } return new_int_symbol(i2c_handle.handle); }
static be_jse_symbol_t* module_net_send() { be_jse_symbol_t *arg0 = NULL,*arg1 = NULL; int ret = -1;int sock_id = 0; char *data = NULL; send_data_t *p_send_data= NULL; be_jse_handle_function(0, &arg0,&arg1,NULL,NULL); if ( (arg0 == NULL) || !symbol_is_int(arg0) || (arg1 == NULL) || !symbol_is_string(arg1)){ be_error(JS_NET_TAG,"module_net_connect param invalid"); goto done; } sock_id = get_symbol_value_int(arg0); data = calloc(1,symbol_str_len(arg1)+1); if (!data) goto done; symbol_to_str(arg1, data, symbol_str_len(arg1)); be_debug(JS_NET_TAG,"NET.send(%d,%s)",sock_id,data); p_send_data = calloc(1,sizeof(send_data_t)); p_send_data->p_data = data; p_send_data->sock_id = sock_id; ret = be_osal_new_task("net_connect",task_net_send_fun,p_send_data,4096,NULL); if (ret < 0){ free(p_send_data); free(p_send_data->p_data); } done: symbol_unlock(arg0); symbol_unlock(arg1); return new_int_symbol(ret); }
/*for example addr: tcp://www.baidu.com:80*/ static int parse_net_option_addr(const char *addr,net_sckt_param_t *p_sckt_param) { char *begin,*colon; if (!addr || !p_sckt_param) return -1; if (strncmp(addr,"tcp",strlen("tcp")) == 0){ p_sckt_param->proto = SOCK_STREAM; }else if (strncmp(addr,"udp",strlen("udp")) == 0){ p_sckt_param->proto = SOCK_DGRAM; }else if (strncmp(addr,"raw",strlen("raw")) == 0){ p_sckt_param->proto = SOCK_RAW; }else{ be_error(JS_NET_TAG,"invalid net connection protocol type"); return -1; } begin = strstr(addr, "//"); if(!begin) return -1; begin = begin + 2; colon = strrchr(addr, ':'); if (!colon) return -1; if (colon-begin <= 0) return -1; strncpy(p_sckt_param->host_ip,begin,colon-begin); p_sckt_param->host_ip[colon-begin] = '\0'; colon = colon + 1; p_sckt_param->port=atoi(colon); if (p_sckt_param->port <= 0) return -1; return 0; }
static int8_t uart_add_recv(uart_dev_t * uart,uint32_t item_handle,be_jse_symbol_t * fun_symbol){ int8_t ret = -1; uart_module_t * module = uart_get_free(); module = calloc(1,sizeof(uart_module_t)); if(NULL == module){ be_error("uart","uart_start_recv fail!\n"); return(-1); } module->fun_symbol = fun_symbol; module->item_handle = item_handle; module->uart = uart; ret = be_osal_post_delayed_action(100,uart_handle,module); if(ret < 0){ free(module); module = NULL; return (-1); } return (0); }
static be_jse_symbol_t * adc_close(void){ int8_t ret = -1; item_handle_t adc_handle; be_jse_symbol_t * arg0 = NULL; adc_dev_t * adc_device = NULL; be_jse_handle_function(0,&arg0,NULL,NULL,NULL); if(!arg0 || !symbol_is_int(arg0)){ goto out; } adc_handle.handle = get_symbol_value_int(arg0); adc_device = board_get_node_by_handle(MODULE_ADC,&adc_handle); if(NULL == adc_device){ be_error("adc","board_get_node_by_handle fail!\n"); goto out; } ret = hal_adc_finalize(adc_device); board_disattach_item(MODULE_ADC,&adc_handle); out: symbol_unlock(arg0); return new_int_symbol(ret); }
void module_uart_register(void){ g_uart_module = calloc(1,MAX_UART_PORT * sizeof(uart_module_t*)); if(NULL == g_uart_module){ be_error("uart","calloc fail!\n"); return; } be_jse_module_load("UART", uart_module_handle_cb); }
static be_jse_symbol_t * uart_write(void){ int8_t ret = -1; int8_t result = -1; char * data = NULL; uint32_t len = 0; item_handle_t uart_handle; be_jse_symbol_t * arg0 = NULL; be_jse_symbol_t * arg1 = NULL; uart_dev_t * uart_device = NULL; be_jse_handle_function(0,&arg0,&arg1,NULL,NULL); if(!arg0 || !symbol_is_int(arg0)){ goto out; } uart_handle.handle = get_symbol_value_int(arg0); uart_device = board_get_node_by_handle(MODULE_UART,&uart_handle); if(NULL == uart_device){ be_error("uart","board_get_node_by_handle fail!\n"); goto out; } if(!arg1 || !symbol_is_string(arg1)){ goto out; } len = symbol_str_len(arg1); data = calloc(1,sizeof(char)*(len+1)); if(NULL==data){ goto out; } symbol_to_str(arg1,data,len); ret = hal_uart_send(uart_device,data,len,0); if(-1 == ret){ be_error("uart","hal_uart_send fail!\n"); goto out; } result = 0; out: symbol_unlock(arg0); symbol_unlock(arg1); if(NULL != data){ free(data); data = NULL; } return new_int_symbol(len); }
static be_jse_symbol_t * uart_watch(void){ int32_t len = -1; char * data = NULL; int8_t ret = -1; int8_t result = -1; item_handle_t uart_handle; uart_handle.handle = 0xFFFFFFFF; be_jse_symbol_t * arg0 = NULL; be_jse_symbol_t * arg1 = NULL; uart_dev_t * uart_device = NULL; be_jse_handle_function(0,&arg0,&arg1,NULL,NULL); if(!arg0 || !symbol_is_int(arg0)){ goto out; } uart_handle.handle = get_symbol_value_int(arg0); uart_device = board_get_node_by_handle(MODULE_UART,&uart_handle); if(NULL == uart_device){ be_error("uart","board_get_node_by_handle fail!\n"); goto out; } if(NULL != uart_get_module(uart_handle.handle)){ be_error("uart","uart_get_module fail!\n"); goto out; } if(!arg1 || !symbol_is_function(arg1)){ goto out; } ret = uart_add_recv(uart_device,uart_handle.handle,arg1); if(ret < 0){ be_error("uart","uart_add_recv fail!\n"); goto out; } result = 0; out: symbol_unlock(arg0); if(0 != result){ symbol_unlock(arg1); } return new_int_symbol(result); }
void disk::del_all(delblocklist_t &ids) { if (!m_maxBlockSize) throw be_error("Can't delete; engine is read-only"); for (int i = 0; i < ids.size(); i++) { del_one(ids[i]); } }
void disk::put_all(putblocklist_t &blocklist) { if (!m_maxBlockSize) throw be_error("Can't put; engine is read-only"); for (int i = 0; i < blocklist.size(); i++) { put_one(blocklist[i]); } }
static be_jse_symbol_t* module_net_close() { be_jse_symbol_t *arg0 = NULL; int ret = -1;int sock_id = 0; be_jse_handle_function(0, &arg0,NULL,NULL,NULL); if ( (arg0 == NULL) || !symbol_is_int(arg0) ){ be_error(JS_NET_TAG,"module_net_close param invalid"); goto done; } sock_id = get_symbol_value_int(arg0); if (sock_id > 0){ be_warn(JS_NET_TAG,"socket id[%d] has been closed",sock_id); ret = close(sock_id); } done: symbol_unlock(arg0); return new_int_symbol(ret); }
mempage s3be::readGetOutcome(const nodeid_t &id, GetObjectOutcome &response) { if (!response.IsSuccess()) throw be_error((std::string("Error fetching ") + boost::lexical_cast<std::string>(id) + ": " + response.GetError().GetMessage()).c_str()); std::stringstream ss; // Decompress into the stringstream io::filtering_ostreambuf in; in.push(io::zlib_decompressor()); in.push(ss); io::copy(response.GetResult().GetBody(), in); mempage ret(ss.str().size()); memcpy((void*)ret.ptr(), ss.str().data(), ss.str().size()); // Put in the cache m_cache.put(id, ret); return ret; }
static be_jse_symbol_t * adc_read(void){ int32_t ret = -1; int32_t adc_value = -1; item_handle_t adc_handle; be_jse_symbol_t * arg0 = NULL; adc_dev_t * adc_device = NULL; be_jse_handle_function(0,&arg0,NULL,NULL,NULL); if(!arg0 || !symbol_is_int(arg0)){ goto out; } adc_handle.handle = get_symbol_value_int(arg0); adc_device = board_get_node_by_handle(MODULE_ADC,&adc_handle); if(NULL == adc_device){ be_error("adc","board_get_node_by_handle fail!\n"); goto out; } ret = hal_adc_value_get(adc_device,(void*)&adc_value,0); out: symbol_unlock(arg0); return new_int_symbol(adc_value>0 ? adc_value : -1); }
static be_jse_symbol_t * uart_read(void){ be_error("uart","todo......\n"); return new_int_symbol(-1); }
static void call_action(void *arg) { schedule_msg_t* schedule_msg = (schedule_msg_t*)arg; if (schedule_msg != NULL) { if (schedule_msg->type == ON_CONNECT) { char productKey[64] = {0}; char deviceName[64] = {0}; char deviceSecret[64] = {0}; sscanf((char*)schedule_msg->msg, "%[^|]|%[^|]|%s", productKey, deviceName, deviceSecret); if (strlen(productKey) == 0 || strlen(deviceName) == 0 || strlen(deviceSecret) == 0) { be_error(JS_ZIGBEE_TAG, "productKey or deviceName or deviceSecret not exist"); return; } unsigned char content[256] = {0}; sprintf((char*)content, "clientId%sdeviceName%sproductKey%s", deviceName, deviceName, productKey); mbedtls_sha1_context sha1_ctx; unsigned char k_ipad[64] = {0}; unsigned char k_opad[64] = {0}; unsigned char out[20]; memcpy(k_ipad, deviceSecret, strlen(deviceSecret)); memcpy(k_opad, deviceSecret, strlen(deviceSecret)); for (int i = 0; i < sizeof(k_ipad); i++) { k_ipad[i] ^= 0x36; k_opad[i] ^= 0x5c; } mbedtls_sha1_init(&sha1_ctx); mbedtls_sha1_starts(&sha1_ctx); mbedtls_sha1_update(&sha1_ctx, k_ipad, sizeof(k_ipad)); mbedtls_sha1_update(&sha1_ctx, content, strlen((char*)content)); mbedtls_sha1_finish(&sha1_ctx, out); mbedtls_sha1_init(&sha1_ctx); mbedtls_sha1_starts(&sha1_ctx); mbedtls_sha1_update(&sha1_ctx, k_opad, sizeof(k_opad)); mbedtls_sha1_update(&sha1_ctx, out, sizeof(out)); mbedtls_sha1_finish(&sha1_ctx, out); mbedtls_sha1_free(&sha1_ctx); char sign[41] = {0}; for (int i=0; i<sizeof(out); ++i) { unsigned char byte[2] = {0}; byte[0] = out[i] / 16; byte[1] = out[i] % 16; for(int j=0; j<2; ++j) { if(byte[j] >= 0 && byte[j] <= 9) sign[2 * i + j] = '0' + byte[j]; else sign[2 * i + j] = 'a' + byte[j] - 10; } } char json_str[256] = {0}; sprintf(json_str, "{\"productKey\": \"%s\", \"deviceName\": \"%s\", \"deviceSecret\": \"%s\", \"sign\": \"%s\"}", productKey, deviceName, deviceSecret, sign); be_jse_symbol_t* argv[2]; char addr_str[12] = {0}; sprintf(addr_str, "%d", schedule_msg->addr); argv[0] = new_str_symbol(addr_str); argv[1] = new_json_symbol(json_str, strlen(json_str)); //symbol_relock(argv[0]); //symbol_relock(argv[1]); be_jse_execute_func(bone_engine_get_executor(), on_connect_func, 2, argv); //symbol_unlock(argv[0]); //symbol_unlock(argv[1]); } else if (schedule_msg->type == ON_MESSAGE) { be_jse_symbol_t* argv[2]; char addr_str[12] = {0}; sprintf(addr_str, "%d", schedule_msg->addr); argv[0] = new_str_symbol(addr_str); argv[1] = new_json_symbol((char*)schedule_msg->msg, schedule_msg->msg_size); //symbol_relock(argv[0]); //symbol_relock(argv[1]); be_jse_execute_func(bone_engine_get_executor(), on_message_func, 2, argv); //symbol_unlock(argv[0]); //symbol_unlock(argv[1]); } else if (schedule_msg->type == BZB_NOTIFY) { bone_zigbee_notify(&zigbee); } else if (schedule_msg->type == BZB_SEARCH) { bone_zigbee_search(&zigbee); } free(schedule_msg); schedule_msg = NULL; } }