/* The callback to be invoked by the system engine. */ void handle_sysevt(VMINT message, VMINT param) { switch (message) { case VM_EVENT_CREATE: break; case VM_EVENT_PAINT: { VMWCHAR path[100]; VMINT fs = 0; VMCHAR flag = 0; VMCHAR write = 'a'; VMINT write_size = 0; VMINT ret = 0; /* This file is a flag to not update the firmware after the firmware was updated and the board reboots. It's to prevent calling the update API in an endless loop. */ vm_chset_ascii_to_ucs2(path, 100, "c:\\firmware.txt"); fs = vm_fs_open(path,VM_FS_MODE_READ,FALSE); if(fs<0) { fs = vm_fs_open(path,VM_FS_MODE_CREATE_ALWAYS_WRITE,FALSE); } if(fs>=0) { vm_fs_read(fs,(void *)&flag,sizeof(VMCHAR),&write_size); if(flag==0) { vm_fs_seek(fs,0,VM_FS_BASE_BEGINNING); vm_fs_write(fs,(void *)&write,sizeof(VMCHAR),&write_size); } vm_fs_close(fs); if(flag=='a') { vm_log_info("firmware:have firmware update"); ret = firmware_read_update_status(); vm_log_info("firmware:update result = %d ",ret); vm_fs_delete(path); } if(flag==0) { vm_log_info("firmware update begin"); vm_firmware_trigger_update(); vm_log_info("firmware update end"); vm_pwr_reboot(); } } } break; default: break; } }
VMINT firmware_read_update_status() { VMINT fd = -1; VMINT32 ret = 1; vm_firmware_update_status_t update_info; VMWCHAR path[100]; VMUINT read = 0; vm_chset_ascii_to_ucs2(path, 100, "c:\\update_status"); fd = vm_fs_open(path, VM_FS_MODE_READ,TRUE); if (fd >= 0) { vm_fs_read(fd, (void *)&update_info, sizeof(update_info), &read); if (read == sizeof(update_info)) { ret = update_info.error_code; /* refer to VM_FIRMWARE_UPDATE_RESULT in vmfirmware.h*/ } else { ret = 2; /*incorrect file size */ } vm_fs_close(fd); } else { ret = 1; /* file could not be found */ } return ret; }
boolean file_ext_close(void* user_data) { file_info_struct* data_file = (file_info_struct*)user_data; vm_fs_close(data_file->handle); return true; }
//========================== 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; }
void close_conf() { vm_fs_close(conf_handle); conf_handle_valid = FALSE; }
extern int _close(int file) { LOG("_close(%d)\n", file); vm_fs_close(file); return 0; }