VMUINT8 gsm_sms_get_unread(void* userdata) { sms_unread_msg_struct *dest = (sms_unread_msg_struct*)userdata; VMINT16 message_id; vm_gsm_sms_read_message_data_t* message_data = NULL; VMWCHAR* content_buff; VMINT res; //message_id = vm_gsm_sms_get_message_id(VM_GSM_SMS_BOX_INBOX, 0); //dest -> id = message_id; message_id = dest->id; if(message_id >= 0) { // Allocates memory for the message data message_data = (vm_gsm_sms_read_message_data_t*)vm_calloc(sizeof(vm_gsm_sms_read_message_data_t)); if(message_data == NULL) { vm_log_info("sms read malloc message data fail"); return FALSE; } // Allocates memory for the content buffer of the message content_buff = (VMWCHAR*)vm_calloc((500+1)*sizeof(VMWCHAR)); if(content_buff == NULL) { vm_free(message_data); vm_log_info("sms read malloc content fail"); return FALSE; } message_data->content_buffer = content_buff; message_data->content_buffer_size = 500; // Reads the message res = vm_gsm_sms_read_message(message_id, VM_TRUE, message_data, sms_read_callback, NULL); //vm_log_info("vm_gsm_sms_read_message message_id is %d", message_id); if(res != VM_GSM_SMS_RESULT_OK) { vm_free(content_buff); vm_free(message_data); vm_log_info("register read callback fail"); return FALSE; } else { return TRUE; } } }
//========================= int https_get(lua_State* L) { int ret = 0; vm_https_callbacks_t callbacks = { (vm_https_set_channel_response_callback)https_send_request_set_channel_rsp_cb, (vm_https_unset_channel_response_callback)https_unset_channel_rsp_cb, (vm_https_release_all_request_response_callback)https_send_release_all_req_rsp_cb, (vm_https_termination_callback)https_send_termination_ind_cb, (vm_https_send_response_callback)https_send_read_request_rsp_cb, (vm_https_read_content_response_callback)https_send_read_read_content_rsp_cb, (vm_https_cancel_response_callback)https_send_cancel_rsp_cb, (vm_https_status_query_response_callback)https_send_status_query_rsp_cb }; size_t sl; const char* url = luaL_checklstring(L, 1, &sl); vm_free(g_https_url); g_https_url = vm_calloc(sl+1); strncpy(g_https_url, url, sl); vm_https_register_context_and_callback(gprs_bearer_type, &callbacks); if (ret != 0) { l_message(NULL, "register context & cb failed"); } else { ret = vm_https_set_channel(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } lua_pushinteger(L, ret); return 1; }
vm_phb_req_error_enum vm_sal_phb_get_contact_syn(vm_phb_get_req_struct* req_info) { srv_phb_sdk_get_req_struct req; srv_phb_sdk_contact_struct * contact = NULL; VMINT result = -1; vm_phb_req_error_enum res = VM_PHB_REQ_ERROR; contact = vm_calloc(sizeof(srv_phb_sdk_contact_struct)); if(contact == NULL) { vm_log_error("vm_sal_phb_get_contact_syn:contact == NULL"); return VM_PHB_REQ_ERROR; } memset(&req, 0, sizeof(req)); req.contact_ptr = contact; vm_phb_build_contact_pos(&req , req_info); result = srv_phb_sdk_get_contact(&req); if(result == SRV_PHB_NO_ERROR) { vm_phb_build_contact_ptr(req_info->contact_ptr, req.contact_ptr); res = VM_PHB_REQ_NO_ERROR; } vm_log_debug("vm_sal_phb_get_contact_syn:vm_get_tick_count2()=%d",vm_get_tick_count()); vm_free(contact); vm_log_debug("vm_sal_phb_get_contact_syn:end"); return res; }
VMUINT8 * vm_des_encrypt( VMDESHANDLE handle, VMUINT8 * input, VMINT input_len, VMINT * output_len ) { VMUINT8 * output = NULL,*temp_input=NULL; if( input == NULL || output_len == NULL || input_len == 0 ) return NULL; if( handle<0 || handle>DES_CTX_MAX || g_des_ctx[handle] == NULL ) return NULL; output = (VMUINT8 *)vm_calloc( (input_len/8 + 2) * 8 ); if( output == NULL ) { return NULL; } if (((VMUINT)input)&3) { temp_input = (VMUINT8 *)vm_calloc(input_len ); if( temp_input == NULL ) { MMI_TRACE(MMI_MRE_TRC_WARNING, TRC_MRE_VMDES_152 ); return NULL; } memcpy(temp_input,input,input_len); } else { temp_input=input; } che_process(&(g_des_ctx[handle]->che_str), CHE_DES, CHE_CBC, CHE_ENC, temp_input, output, input_len, KAL_TRUE); *output_len = (input_len/8 + 1) * 8; if ((((VMUINT)input)&3) && (temp_input)) { vm_free(temp_input); temp_input=NULL; } return output; }
VMINT vm_sal_phb_get_group_list(vm_phb_group_struct* group_array, VMINT group_array_len) { VMINT group_count = 0; VMINT len = 0; VMUINT8 * group_id_array = NULL; VMUINT16* group_name_array = NULL; VMINT i = 0; group_count =(VMINT)srv_phb_datamgr_get_group_list(NULL, NULL); if(group_array==NULL || group_array_len <= 0) return group_count; if(group_count <= 0) { vm_log_debug("vm_sal_phb_get_group_list: group_count <= 0"); return group_count; } len = group_count; if(group_array_len < group_count) len = group_array_len; group_id_array= (VMUINT8 *)vm_calloc(sizeof(VMUINT8) * group_count); group_name_array =(VMUINT16* )vm_calloc(sizeof(VMUINT16) *( VM_MAX_PHB_GROUP_NAME_LENGTH+1)*group_count); srv_phb_datamgr_get_group_list(group_id_array, group_name_array); for(i=0; i< len; i++) { group_array[i].group_id = group_id_array[i]; vm_wstrncpy(group_array[i].group_name, (VMWSTR)(group_name_array +i * (VM_MAX_PHB_GROUP_NAME_LENGTH + 1)), VM_MAX_PHB_GROUP_NAME_LENGTH); vm_log_debug("group_array[i].group_id =%d", group_array[i].group_id); vm_log_debug("group_array[i].group_name=%s", vm_gb2312_string(group_array[i].group_name)); } if(group_id_array) vm_free(group_id_array); if(group_name_array) vm_free(group_name_array); return len; }
VMINT vm_sal_phb_get_group_name_by_id(VMUINT group_id, VMWCHAR* group_name, VMINT len) { VMINT group_count = 0; VMUINT8 * group_id_array = NULL; VMUINT16* group_name_array = NULL; VMINT i = 0; VMINT temp_len= 0; VMINT res = FALSE; group_count =(VMINT)srv_phb_datamgr_get_group_list(NULL, NULL); if(group_count <= 0) { vm_log_debug("vm_sal_phb_get_group_name_by_id: group_count <= 0"); return FALSE; } group_id_array=(VMUINT8 *)vm_calloc(sizeof(VMUINT8) * group_count); group_name_array =(VMUINT16* ) vm_calloc(sizeof(VMUINT16) *( VM_MAX_PHB_GROUP_NAME_LENGTH+1)*group_count); srv_phb_datamgr_get_group_list(group_id_array, group_name_array); for(i=0; i< group_count; i++) { if(group_id_array[i] == group_id) { temp_len = VM_MAX_PHB_GROUP_NAME_LENGTH+1; if(len < temp_len) temp_len = len; vm_wstrncpy(group_name, (VMWSTR)(group_name_array +i * (VM_MAX_PHB_GROUP_NAME_LENGTH + 1)), temp_len); vm_log_debug("group_name=%s ,group_id=%d", vm_gb2312_string(group_name), group_id); res = TRUE; break; } } if(group_id_array) vm_free(group_id_array); if(group_name_array) vm_free(group_name_array); return res; }
void app_client_connection_callback(vm_bt_gatt_connection_t *conn, VMBOOL connected, vm_bt_gatt_address_t *bd_addr) { vm_log_debug("[AppClient]connection_cb connected[%d]", connected); if (connected) { //vm_bt_gatt_client_read_remote_rssi(conn->context_handle, bd_addr); VM_TIMER_ID_NON_PRECISE timer_id = 0; RSSI_T *rssi = (RSSI_T *)vm_calloc(sizeof(RSSI_T)); rssi->conn.connection_handle = conn->connection_handle; rssi->conn.context_handle = conn->context_handle; if (bd_addr) { memcpy(rssi->bd_addr.data, bd_addr->data, sizeof(rssi->bd_addr.data)); } timer_id = vm_timer_create_non_precise(5000, (vm_timer_non_precise_callback)timer_cb, rssi); } return; if((memcmp(bd_addr->data, appc_conn_cntx->bdaddr, VM_BT_GATT_ADDRESS_SIZE) == 0) && (NULL != bd_addr)) { vm_log_debug("[AppClient] find in list!\n"); appc_conn_cntx->connection_handle = conn->connection_handle; if(connected && (appc_conn_cntx->conn_status != APPC_STATUS_CONNECTED)) { //Do next step to discover all vm_log_debug("[AppClient] have connected,find in list!\n"); //vm_bt_gatt_client_listen(conn->connection_handle, FALSE); appc_conn_cntx->conn_status = APPC_STATUS_CONNECTING; //vm_bt_gatt_client_search_service(conn, NULL); } else if(!connected) { //vm_bt_gatt_client_listen(conn->connection_handle, TRUE); //vm_free(appc_conn_cntx); vm_log_debug("[AppClient] connection faield"); } return; } if(connected) { vm_log_debug("[AppClient] connect success, and not find in list!\n"); if (appc_conn_cntx == NULL) { appc_conn_cntx = (AppClientConnCntx *)vm_malloc(sizeof(AppClientConnCntx)); } memset(appc_conn_cntx, 0x0, sizeof(AppClientConnCntx)); memcpy(appc_conn_cntx->bdaddr, bd_addr->data, VM_BT_GATT_ADDRESS_SIZE); appc_conn_cntx->connection_handle = conn->connection_handle; //vm_bt_gatt_client_listen(conn->context_handle, FALSE); appc_conn_cntx->conn_status = APPC_STATUS_CONNECTING; //vm_bt_gatt_client_search_service(conn, NULL); } }
static void int_gatt_str_to_uuid(const VMUINT8* str, VM_BT_UUID* uuid) { #if 0 uuid->len = 16; memcpy(uuid->uuid, str, 16); #else VMUINT32 len = 0; VMUINT8* uuid_str = NULL; VMUINT32 uuid_len = 0; int i,j; VMUINT8 H4 = 0; VMUINT8 L4 = 0; len = strlen((const char *)str); //mmi_chset_utf8_strlen(str); APP_LOG("int_gatt_str_to_uuid [%s] len[%d]", str, len); uuid_str = (VMUINT8*)vm_calloc(len + 1); if (uuid_str != NULL) { memset(uuid_str, 0x0, len+1); memcpy(uuid_str, str, len); uuid_str[len] = '\0'; uuid_len = int_gatt_squeeze((VMINT8*)uuid_str, '-'); switch (uuid_len) { case UUID_STR_LEN_128: case UUID_STR_LEN_16: case UUID_STR_LEN_2: { for (i = 0, j = uuid_len - 2; j >= 0; j -= 2) { H4 = int_gatt_chr_to_u8(uuid_str[j]); L4 = int_gatt_chr_to_u8(uuid_str[j+1]); uuid->uuid[i] = H4*16 + L4; i++; } uuid->len = i; } break; default: APP_LOG("int_gatt_str_to_uuid len error"); break; } vm_free(uuid_str); uuid_str = NULL; } else { APP_LOG("int_gatt_str_to_uuid no memory"); } #endif }
ADC_HANDLE open_hx711(int scl_pin, int sda_pin) { ADC_HANDLE handle = 0; handle_details* details; hx711_task* task; while (open_handles[handle] != NULL) { handle++; if (handle > MAX_HANDLE) { return ADC_HANDLE_INVALID; } } details = vm_calloc(sizeof(handle_details)); if (details == NULL) { return ADC_HANDLE_INVALID; } task = &details->task; task->scl_handle = vm_dcl_open(VM_DCL_GPIO, scl_pin); if (task->scl_handle == VM_DCL_HANDLE_INVALID) { vm_free(details); return ADC_HANDLE_INVALID; } vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL); vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_OUT, NULL); vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL); task->sda_handle = vm_dcl_open(VM_DCL_GPIO, sda_pin); if (task->sda_handle == VM_DCL_HANDLE_INVALID) { vm_dcl_close(task->scl_handle); vm_free(details); return ADC_HANDLE_INVALID; } vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL); vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_IN, NULL); vm_mutex_init(&details->mutex); task->op = WAIT; task->delay = 100; task->callback = dummy_callback; task->callback_env = NULL; open_handles[handle] = details; write_console("open\n"); vm_thread_create(measurement, (void*) details, (VM_THREAD_PRIORITY) 0); return handle; }
VMINT vm_sal_phb_get_used_contacts(vm_phb_contact_pos_struct* pos_array, vm_phb_storage_loc_enum storage) { VMINT used_nums = 0; phb_storage_enum storage_loc; srv_phb_contact_pos_struct *contact_array = NULL; int i=0; VMINT nums = 0; storage_loc = vm_sal_phb_trans_storage(storage); used_nums = srv_phb_sdk_get_used_contacts(contact_array, storage_loc); if(used_nums < 0) { vm_log_error("vm_sal_phb_get_used_contacts:used_nums < 0"); return VM_PHB_REQ_ERROR; } if(pos_array == NULL) { vm_log_error("vm_sal_phb_get_used_contacts:pos_array == NULL"); return used_nums; } if(used_nums == 0) return used_nums; contact_array = vm_calloc(sizeof(srv_phb_contact_pos_struct)*used_nums); if(contact_array == NULL) { vm_log_error("vm_sal_phb_get_used_contacts:contact_array == NULL"); return VM_PHB_REQ_ERROR; } storage_loc = vm_sal_phb_trans_storage(storage); nums = srv_phb_sdk_get_used_contacts(contact_array, storage_loc); if(nums > 0) { for(i=0; i < used_nums; i++ ) { pos_array[i].index =contact_array[i].index; pos_array[i].storage=vm_sal_phb_trans_storage_ex(contact_array[i].storage); } } vm_free(contact_array); return used_nums; }
VMUINT8 * vm_des_decrypt( VMDESHANDLE handle, VMUINT8 * input, VMINT input_len, VMINT * output_len ) { VMUINT8 * output = NULL; if( input == NULL || output_len == NULL || input_len == 0 ) return NULL; if( handle<0 || handle>DES_CTX_MAX || g_des_ctx[handle] == NULL ) return NULL; output = (VMUINT8 *)vm_calloc( (input_len/8 + 2) * 8 ); if( output == NULL ) { MMI_TRACE(MMI_MRE_TRC_WARNING, TRC_MRE_VMDES_183 ); return NULL; } che_process(&(g_des_ctx[handle]->che_str), CHE_DES, CHE_CBC/*CHE_ECB*/, CHE_DEC, input, output, input_len, KAL_TRUE); { VMUCHAR pad[8], padch = *(output+input_len-1); memset( pad, padch, 8 ); if( !memcmp( output+input_len-(VMINT)padch, pad, (VMINT)padch ) ) { MMI_TRACE(MMI_MRE_TRC_WARNING, TRC_MRE_VMDES_198 ); *output_len = input_len-(VMINT)padch; } else { MMI_TRACE(MMI_MRE_TRC_WARNING, TRC_MRE_VMDES_204 ); vm_free( output ); return NULL; } } return output; }
VMBOOL read_conf_string(VMSTR variable_name, VMSTR output_buffer, VMUINT output_buffer_size) { VMINT res; VMUINT conf_size; VMUINT bytes_read; VMSTR conf_data; VMSTR key; VMSTR value; VMBOOL result_string_valid = FALSE; if (conf_handle_valid) { res = vm_fs_get_size(conf_handle, &conf_size); if (VM_IS_SUCCEEDED(res)) { conf_data = vm_calloc(conf_size + strlen(conf_defaults) + 1); if (conf_data != NULL) { vm_fs_seek(conf_handle, 0, VM_FS_BASE_BEGINNING); res = vm_fs_read(conf_handle, conf_data, conf_size, &bytes_read); if (VM_IS_SUCCEEDED(res)) { strcat(conf_data, conf_defaults); key = strtok(conf_data, "="); while (key != NULL) { value = strtok(NULL, "\n"); if (value != NULL && strcmp(variable_name, key) == 0 && strlen(value) < output_buffer_size) { strcpy(output_buffer, value); result_string_valid = TRUE; break; } key = strtok(NULL, "="); } } vm_free(conf_data); } } } return result_string_valid; }
void md5_init(md5_state_t *pms) { VM_P_HANDLE processid=vm_pmng_get_current_handle(); STCHE *md5_ctx_p=NULL; VMINT res_id; md5_ctx_p=vm_get_cheinfo_from_processid(processid,&res_id); if (!md5_ctx_p ) { if ((md5_ctx_p=(STCHE *)vm_calloc(sizeof(STCHE)))==NULL) { MMI_TRACE(MMI_MRE_TRC_ERROR, TRC_MRE_VMMD5_218 ); return; } } che_init(md5_ctx_p, CHE_MD5); vm_res_save_data(VM_RES_TYPE_MD5, (void*)(md5_ctx_p),sizeof(STCHE), NULL,processid ); }
vm_phb_req_error_enum vm_sal_phb_search_contact_syn(vm_phb_search_req_struct* req_info) { srv_phb_sdk_search_req_struct req; srv_phb_sdk_contact_struct * contact = NULL; VMINT result = -1; vm_phb_req_error_enum res = VM_PHB_REQ_ERROR; vm_log_debug("vm_sal_phb_search_contact_syn: start"); contact = vm_calloc(sizeof(srv_phb_sdk_contact_struct)); if(contact == NULL) { vm_log_error("vm_sal_phb_search_contact_syn:contact == NULL"); return VM_PHB_REQ_ERROR; } memset(&req, 0, sizeof(req)); req.contact_ptr = contact; vm_wstrncpy((VMWSTR)req.pattern_ucs2, req_info->pattern_ucs2, SRV_PHB_SEARCH_LENGTH-1); req.search_type = (srv_phb_search_type_enum)req_info->search_type; vm_log_debug("vm_sal_phb_search_contact_syn: req.search_type=%d,req.pattern_ucs2=%s", req.search_type, vm_gb2312_string((VMWSTR)req.pattern_ucs2)); result = srv_phb_sdk_search_contact(&req); if(result == SRV_PHB_NO_ERROR) { vm_phb_build_contact_ptr(req_info->contact_ptr, req.contact_ptr); res = VM_PHB_REQ_NO_ERROR; } vm_free(contact); vm_log_debug("vm_sal_phb_search_contact_syn:srv_phb_sdk_search_contact end"); return res; }
VMINT vm_sal_phb_search_contact_list(vm_phb_search_list_req_struct* req_info, vm_phb_contact_pos_struct* pos_array, VMINT post_array_len) { srv_phb_contact_pos_struct *contact_array = NULL; srv_phb_sdk_get_req_struct req; srv_phb_sdk_contact_struct * contact = NULL; VMINT used_nums = 0; VMINT res = 0; VMINT j = 0; VMINT i = 0; used_nums = srv_phb_sdk_get_used_contacts(contact_array, vm_sal_phb_trans_storage(VM_PHB_STORAGE_LOC_BOTH)); if(used_nums < 0) { vm_log_error("vm_sal_phb_search_contact_list:used_nums < 0"); return VM_PHB_REQ_ERROR; } if(used_nums == 0) return used_nums; contact_array = vm_calloc(sizeof(srv_phb_contact_pos_struct)*used_nums); if(contact_array == NULL) { vm_log_error("vm_sal_phb_search_contact_list:contact_array == NULL"); return VM_PHB_REQ_ERROR; } used_nums = srv_phb_sdk_get_used_contacts(contact_array, vm_sal_phb_trans_storage(VM_PHB_STORAGE_LOC_BOTH)); vm_log_debug("vm_sal_phb_search_contact_list:used_nums=%d", used_nums); contact = vm_calloc(sizeof(srv_phb_sdk_contact_struct)); if(contact == NULL) { vm_free(contact_array); vm_log_error("vm_sal_phb_search_contact_list:contact == NULL"); return VM_PHB_REQ_ERROR; } for(i=0; i< used_nums; i++) { memset(&req, 0, sizeof(srv_phb_sdk_get_req_struct)); memset(contact, 0, sizeof(srv_phb_sdk_contact_struct)); req.contact_ptr = contact; req.pos.index= contact_array[i].index; req.pos.storage= contact_array[i].storage; contact->pos.index = contact_array[i].index; contact->pos.storage = contact_array[i].storage; vm_log_debug("vm_sal_phb_search_contact_list contact_array[%d].index=%d", i, contact_array[i].index); vm_log_debug("vm_sal_phb_search_contact_list contact_array[%d].storage=%d", i, contact_array[i].storage); res = srv_phb_sdk_get_contact(&req); if(res == SRV_PHB_NO_ERROR) { if(contact != NULL ) { vm_log_debug("vm_sal_phb_search_contact_list:search_req->search_type=%d",req_info->search_type); if(req_info->search_type == VM_PHB_SEARCH_TYPE_NAME) { vm_log_debug("vm_sal_phb_search_contact_list:search_req->pattern_ucs2=%s",vm_gb2312_string(req_info->pattern_ucs2)); vm_log_debug("vm_sal_phb_search_contact_list:contact_ptr->name=%s",vm_gb2312_string((VMWSTR)contact->name)); if(vm_wstrcmp((VMWSTR)req_info->pattern_ucs2, (VMWSTR)&contact->name) == 0) { pos_array[j].index = contact->pos.index; pos_array[j].storage = vm_sal_phb_trans_storage_ex(contact->pos.storage); j++; } } else if(req_info->search_type == VM_PHB_SEARCH_TYPE_NUM) { vm_log_debug("vm_sal_phb_search_contact_list:search_req->pattern_ucs2=%s",vm_gb2312_string(req_info->pattern_ucs2)); vm_log_debug("vm_sal_phb_search_contact_list:contact_ptr->number=%s",vm_gb2312_string((VMWSTR)contact->number)); if(vm_wstrcmp((VMWSTR)req_info->pattern_ucs2, (VMWSTR)&contact->number) == 0) { pos_array[j].index = contact->pos.index; pos_array[j].storage = vm_sal_phb_trans_storage_ex(contact->pos.storage); j++; } } else { vm_log_error("vm_sal_phb_search_contact_list: req_info->search_type =%d",req_info->search_type); } } if(j == post_array_len) { break; } } } vm_free(contact_array); vm_free(contact); return j; }
static VMINT bt_server_init_spp(uint8_t* name) { VMUINT evt_mask = VM_SRV_SPP_EVENT_BIND_FAIL | VM_SRV_SPP_EVENT_AUTHORIZE | VM_SRV_SPP_EVENT_CONNECT | VM_SRV_SPP_EVENT_READY_TO_WRITE | VM_SRV_SPP_EVENT_READY_TO_READ | VM_SRV_SPP_EVENT_DISCONNECT; g_serverContext.btspp_hdl = vm_btspp_open(evt_mask,bt_server_spp_cb,NULL); if(g_serverContext.btspp_hdl < 0) { APP_LOG((char*)"[BTC]bt_server_init_spp hdl[%d]", g_serverContext.btspp_hdl); return g_serverContext.btspp_hdl; } vm_btspp_set_security_level(g_serverContext.btspp_hdl,VM_SRV_SPP_SEC_NAN); VMINT min_buf_size = vm_btspp_get_min_buf_size(); g_serverContext.btspp_buf = vm_calloc(min_buf_size); g_serverContext.btspp_buf_size = min_buf_size / 2; if(g_serverContext.btspp_buf == NULL) { APP_LOG((char*)"[BTC]bt_server_init_spp fail : alloc buffer"); return -1; } VMINT ret = vm_btspp_bind(g_serverContext.btspp_hdl, SPP_UUID); if(ret < 0) { APP_LOG((char*)"[BTC]bt_server_init_spp fail : btspp_bind"); return -1; } if(name != NULL) { ret = vm_btcm_set_host_name((VMUINT8*)name); } if(ret < 0) { APP_LOG((char*)"[BTC]bt_server_init_spp fail : btcm_set_host_name"); return -1; } if (VM_SRV_BT_CM_VISIBILITY_ON == vm_btcm_srv_get_visibility()) { APP_LOG((char*)"[BTC]bt_server_init_spp get visibility on"); *(g_serverContext.waiting_result_p) = true; g_serverContext.waiting_result_p = NULL; g_serverContext.ptr->post_signal(); ret = 0; } else { g_flag_visibility_event = 1; ret = vm_btcm_srv_set_visibility(VM_SRV_BT_CM_VISIBILITY_ON); APP_LOG((char*)"[BTC]bt_server_init_spp fail set visibility %d", ret); if (VM_SRV_BT_CM_RESULT_SUCCESS == ret) { ret = 1; } } APP_LOG("[BTC]bt_server_init_spp ret[%d], btspp_hdl=%d", ret, g_serverContext.btspp_hdl); return ret; }
//----------------------------------------------------------------- int _get_paramstr(char **param_buf, char **par_end, char **var_ptr) { int psize, err = 0; psize = vm_fs_app_data_get_free_space(); if (psize > MAX_SYSTEM_PARAMS_SIZE) { // no parameters vm_log_debug("parameters not found"); return -1; } char *parambuf = vm_calloc(MAX_SYSTEM_PARAMS_SIZE+1); if (parambuf == NULL) { vm_log_error("error allocating buffer"); return -2; } VM_FS_HANDLE phandle = vm_fs_app_data_open(VM_FS_MODE_READ, VM_FALSE); if (phandle < VM_FS_SUCCESS) { vm_log_error("error opening params"); return -3; } if (vm_fs_app_data_read(phandle, parambuf, MAX_SYSTEM_PARAMS_SIZE, &psize) < 0) { vm_log_error("error reading params"); vm_fs_app_data_close(phandle); return -4; } vm_fs_app_data_close(phandle); parambuf[psize] = '\0'; // === Check crc === char *parend = strstr(parambuf, "\n__SYSVAR=\""); // end of __SYSPAR table if (parend == NULL) { err = -5; goto exit; } char *varptr = parend + 11; // start of __SSVAR char *varend = strstr(varptr, "\""); // end of __SYSVAR if (varend == NULL){ err = -6; goto exit; } if ((varend - varptr) != SYSVAR_SIZE) { err = -7; goto exit; } // calculate crc CRC16_Context contex; uint16_t parcrc; CRC16_Init( &contex ); CRC16_Update( &contex, parambuf, (varptr - parambuf) + SYSVAR_SIZE - 4); CRC16_Final( &contex, &parcrc ); // compare char crcstr[5] = {'\0'}; memcpy(crcstr, varptr + SYSVAR_SIZE - 4, 4); uint16_t crc = (uint16_t)strtol(crcstr, NULL, 16); if (crc != parcrc) { err = -8; } else { *param_buf = parambuf; *par_end = parend; *var_ptr = varptr; return 0; } exit: vm_log_error("SYSPAR wrong format"); return err; }
//========================== int https_post(lua_State* L) { int ret; vm_https_callbacks_t callbacks = { (vm_https_set_channel_response_callback)https_post_request_set_channel_rsp_cb, (vm_https_unset_channel_response_callback)https_unset_channel_rsp_cb, (vm_https_release_all_request_response_callback)https_send_release_all_req_rsp_cb, (vm_https_termination_callback)https_send_termination_ind_cb, (vm_https_send_response_callback)https_send_read_request_rsp_cb, (vm_https_read_content_response_callback)https_send_read_read_content_rsp_cb, (vm_https_cancel_response_callback)https_send_cancel_rsp_cb, (vm_https_status_query_response_callback)https_send_status_query_rsp_cb }; size_t sl; const char* url = luaL_checklstring(L, 1, &sl); free_post_buffers(); g_https_url = vm_calloc(sl+1); if (g_https_url == NULL) { return luaL_error(L, "url buffer allocation error"); } strncpy(g_https_url, url, sl); int totalalloc = (sl+1); if ((!lua_istable(L, 2)) && (!lua_isstring(L, 2))) { free_post_buffers(); return luaL_error(L, "POST data arg missing" ); } if (lua_isstring(L, 2)) { g_post_type = 0; const char* pdata = luaL_checklstring(L, 2, &sl); if ((sl <= 0) || (pdata == NULL)) { free_post_buffers(); return luaL_error(L, "wrong post data"); } g_postdata = vm_calloc(sl+1); if (g_postdata == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error"); } totalalloc += (sl+1); strncpy(g_postdata, pdata, sl); g_postdata[sl] = '\0'; g_postdata_len = sl; vm_log_debug("[POST]: allocated memory = %d", totalalloc); } else if (lua_istable(L, 2)) { g_post_type = 1; // ** iterate the table to find number of entries and total data size int pdata_size = 0; int nentries = 0; int fnames_size = 0; int filenames_size = 0; int pathnames_size = 0; int err = 0; int nfileentry = 0; lua_pushnil(L); // first key while (lua_next(L, 2) != 0) { // Pops a key from the stack, and pushes a key-value pair from the table // 'key' (at index -2) and 'value' (at index -1) if ((lua_isstring(L, -1)) && (lua_isstring(L, -2))) { size_t klen = 0; size_t vlen = 0; const char* key = lua_tolstring(L, -2, &klen); const char* value = lua_tolstring(L, -1, &vlen); if ((klen > 0) && (vlen > 0)) { nentries++; fnames_size += (klen+1); if (strstr(key, "file") == key) { if (nfileentry < 1) { nfileentry++; filenames_size += (vlen+1); char fn[128]; short wfn[128]; sprintf(fn, "C:\\%s", value); vm_chset_ascii_to_ucs2((VMWSTR)wfn, 256, fn); int fh = vm_fs_open(wfn, VM_FS_MODE_READ, VM_TRUE); if (fh < 0) { err++; vm_log_debug("[POST]: File '%s' not found",fn); } else vm_fs_close(fh); int fnmlen = (vm_wstr_string_length(wfn)+1)*2; pathnames_size += fnmlen; } else { vm_log_debug("[POST]: Only 1 file allowed!"); } } else pdata_size += (vlen+sizeof(int)); } } lua_pop(L, 1); // removes 'value'; keeps 'key' for next iteration } if (nentries == 0) { free_post_buffers(); return luaL_error(L, "no post entries"); } if (err != 0) { free_post_buffers(); return luaL_error(L, "file not found"); } // ** Allocate buffers g_post_context = vm_calloc(sizeof(vm_https_request_context_t)); if (g_post_context == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 1"); } totalalloc += sizeof(vm_https_request_context_t); g_post_content = vm_calloc(sizeof(vm_https_content_t)*nentries); if (g_post_content == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 2"); } totalalloc += (sizeof(vm_https_content_t)*nentries); if (pdata_size > 0) { g_postdata = vm_calloc(pdata_size); if (g_postdata == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 3"); } totalalloc += pdata_size; } g_https_field_names = vm_calloc(fnames_size); if (g_https_field_names == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 4"); } totalalloc += fnames_size; if (filenames_size > 0) { g_https_file_names = vm_calloc(filenames_size); if (g_https_file_names == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 5"); } totalalloc += filenames_size; } if (pathnames_size > 0) { g_https_path_names = vm_calloc(pathnames_size); if (g_https_path_names == NULL) { free_post_buffers(); return luaL_error(L, "buffer allocation error 6"); } totalalloc += pathnames_size; } vm_log_debug("[POST]: allocated memory = %d", totalalloc); sprintf(g_https_url, url); g_post_context->number_entries = 0; g_post_context->content = (vm_https_content_t *)g_post_content; g_post_context->header = CONTENT_TYPE_FORMDATA; g_post_context->header_length = strlen(CONTENT_TYPE_FORMDATA); g_post_context->post_segment = NULL; g_post_context->post_segment_length = 0; g_post_context->url = g_https_url; g_post_context->url_length = strlen(g_post_context->url); // ** iterate the table and populate data buffers vm_https_content_t cc; g_postdata_len = 0; fnames_size = 0; filenames_size = 0; pathnames_size = 0; nfileentry = 0; lua_pushnil(L); while (lua_next(L, 2) != 0) { if ((lua_isstring(L, -1)) && (lua_isstring(L, -2))) { size_t klen = 0; size_t vlen = 0; const char* key = lua_tolstring(L, -2, &klen); const char* value = lua_tolstring(L, -1, &vlen); if ((klen > 0) && (vlen > 0)) { // field key & value are OK g_post_context->number_entries++; if (strstr(key, "file") == key) { if (nfileentry < 1) { nfileentry++; cc.data_type = VM_HTTPS_DATA_TYPE_FILE; cc.content_type = CONTENT_TYPE_OCTET; cc.content_type_length = strlen(CONTENT_TYPE_OCTET); // save file name & length sprintf(g_https_file_names+filenames_size, "%s", value); cc.filename = g_https_file_names+filenames_size; cc.filename_length = vlen; filenames_size += (vlen+1); // save local file path/name & length char fn[128]; short wfn[128]; sprintf(fn, "C:\\%s", value); vm_chset_ascii_to_ucs2((VMWSTR)wfn, 256, fn); int wfnlen = vm_wstr_copy(g_https_path_names+pathnames_size, wfn); cc.file_path_name = g_https_path_names+pathnames_size; cc.file_path_name_length = vm_wstr_string_length(cc.file_path_name); pathnames_size += ((cc.file_path_name_length+1)*2); // get file size int fh = vm_fs_open(cc.file_path_name, VM_FS_MODE_READ, VM_TRUE); VMUINT fsz; if (vm_fs_get_size(fh, &fsz) == 0) cc.data_length = fsz; else cc.data_length = 0; vm_fs_close(fh); } } else { cc.data_type = VM_HTTPS_DATA_TYPE_BUFFER; cc.content_type = CONTENT_TYPE_TEXT; cc.content_type_length = strlen(CONTENT_TYPE_TEXT); cc.filename = ""; cc.filename_length = 0; cc.file_path_name = (VMWSTR)"\0\0"; cc.file_path_name_length = 0; cc.data_length = vlen; // field data length int dlen = vlen; memcpy(g_postdata+g_postdata_len, &dlen, sizeof(int)); memcpy(g_postdata+g_postdata_len+sizeof(int), value, vlen); g_postdata_len += (vlen+sizeof(int)); } cc.charset = VM_HTTPS_CHARSET_ASCII; // save field name & length sprintf(g_https_field_names+fnames_size, "%s", key); cc.name = g_https_field_names+fnames_size; cc.name_length = klen; // field name length fnames_size += (klen+1); memcpy(g_post_content + (sizeof(vm_https_content_t) * (g_post_context->number_entries-1)), &cc, sizeof(vm_https_content_t)); } } // removes 'value'; keeps 'key' for next iteration lua_pop(L, 1); } } ret = vm_https_register_context_and_callback(gprs_bearer_type, &callbacks); if (ret != 0) { free_post_buffers(); l_message(NULL, "register context & cb failed"); } else { ret = vm_https_set_channel(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } lua_pushinteger(L, ret); return 1; }
static VMINT _vm_preload_try_download(vm_preload_ctx_t * ctx_p) { VMCHAR *http_header; VMINT ret = 0; VMINT written = 0; VMINT bufSize = 0; ASSERT(ctx_p); if (0 > ctx_p->soc_id && ctx_p->update) { // host is changed. if (0 == strstr(ctx_p->url, ctx_p->host)) { VMINT nspan = 7; // http:// is 7 bytes for(nspan = 7; ctx_p->url[nspan] != '\0'; nspan ++) { if (ctx_p->url[nspan] == ':') { ctx_p->port = atoi(&ctx_p->url[nspan + 1]); break; } if (ctx_p->url[nspan] == '/') break; } memset(ctx_p->host, 0, sizeof(ctx_p->host)); memcpy(ctx_p->host, &ctx_p->url[7], nspan - 7); } #if 1 vm_preload_do_connection(ctx_p, 2); #else /* under construction !*/ #endif if (0 > ctx_p->soc_id) { MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3023 ); return -4; } MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3026 ); ctx_p->update = 2; return 0; } http_header = vm_calloc(BUFFER_LEN); if (NULL == http_header) return -5; // http header sprintf(http_header, "GET %s HTTP/1.1\r\nHost: %s:%d\r\n" #ifdef WIN32 //"Proxy-Authorization: Basic TXRrODAwMjM6MWFjNmIxMDEx\r\n" #endif "User-Agent: %s\r\n" "Accept: %s\r\n" "Accept-Language: %s\r\n\r\n", ctx_p->url, ctx_p->host, ctx_p->port, (VMCHAR*)applib_inet_get_user_agent_string(), "*/*", "en"); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3051 , ctx_p->url, ctx_p->port, ctx_p->host); while (0 < (ret = vm_tcp_write(ctx_p->soc_id, http_header + written, strlen(http_header) - written))) { written += ret; } vm_free(http_header); return written; }