/** * This function will do a executable program with main function and parameters. * * @param path the full path of application module * @param cmd_line the command line of program * @param size the size of command line of program * * @return the module object */ rt_module_t rt_module_exec_cmd(const char *path, const char* cmd_line, int line_size) { struct dfs_filesystem *fs; appentry_t fptr; HINSTANCE hinstlib; rt_module_t module; char * winpath = RT_NULL; char * name = RT_NULL; char *full_path = RT_NULL; RT_DEBUG_NOT_IN_INTERRUPT; /* check parameters */ RT_ASSERT(path != RT_NULL); if (*path != '/') { full_path = dfs_normalize_path(RT_NULL, path); } else { full_path = (const char*)path; } /* app module should only in DFS_WIN32 */ fs = dfs_filesystem_lookup(full_path); if ((fs == RT_NULL) || (strcmp(fs->ops->name,"wdir") != 0)) { rt_kprintf("invalid path: %s\n", path); goto __exit; } /* change path */ // len = strlen(full_path + 1); if ((winpath = dfs_win32_dirdup((char *)full_path)) == RT_NULL) { rt_kprintf("out of memory, exit", path); goto __exit; } hinstlib = LoadLibrary(winpath); if (hinstlib == NULL) { rt_kprintf("error: unable to open %s\n", winpath); goto __exit; } fptr = (appentry_t)GetProcAddress(hinstlib, "main"); if (fptr == NULL) { rt_kprintf("error: unable to find function in %s\n", winpath); FreeLibrary(hinstlib); goto __exit; } /* release winpath */ rt_free(winpath); /* get the name of the module */ name = _module_name(path); /* allocate module */ module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, name); if (!module) { goto __exit; } module->nref = 0; module->module_entry = fptr; /* init module object container */ rt_module_init_object_container(module); /* increase module reference count */ module->nref ++; if (module->module_entry != 0) { /* set module argument */ module->module_cmd_line = (rt_uint8_t*)rt_malloc(line_size + 1); rt_memcpy(module->module_cmd_line, cmd_line, line_size); module->module_cmd_line[line_size] = '\0'; module->module_cmd_size = line_size; #ifdef RT_USING_SLAB /* init module memory allocator */ module->mem_list = RT_NULL; /* create page array */ module->page_array = (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info)); module->page_cnt = 0; #endif /* create module thread */ module->module_thread = rt_thread_create(name, module_main_entry, module, 2048, RT_THREAD_PRIORITY_MAX - 2, 10); /* set module id */ module->module_thread->module_id = (void *)module; module->parent.flag = RT_MODULE_FLAG_WITHENTRY; /* startup module thread */ rt_thread_startup(module->module_thread); } else { /* without entry point */ module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY; } #ifdef RT_USING_HOOK if (rt_module_load_hook != RT_NULL) { rt_module_load_hook(module); } #endif rt_free(name); return module; __exit: if (full_path != path) rt_free(full_path); if (name != RT_NULL) rt_free(full_path); if (winpath != RT_NULL)rt_free(winpath); return RT_NULL; /* FreeLibrary(hinstlib); */ }
/** * This function will load a module from a file * * @param path the full path of application module * * @return the module object */ rt_module_t rt_module_open(const char *path) { struct dfs_filesystem *fs; appentry_t fptr; HINSTANCE hinstlib; int len; char * winpath; rt_module_t module; char * name; RT_DEBUG_NOT_IN_INTERRUPT; /* check parameters */ RT_ASSERT(path != RT_NULL); /* app module should only in DFS_WIN32 */ fs = dfs_filesystem_lookup(path); if ((fs == RT_NULL) || (strcmp(fs->ops->name,"wdir") != 0)) { rt_kprintf("invalid path: %s\n", path); return RT_NULL; } /* change path */ len = strlen(path+1); if ((winpath = dfs_win32_dirdup((char *)path)) == RT_NULL) { rt_kprintf("out of memory, exit", path); return RT_NULL; } hinstlib = LoadLibrary(winpath); if (hinstlib == NULL) { rt_kprintf("error: unable to open %s\n", winpath); return RT_NULL; } fptr = (appentry_t)GetProcAddress(hinstlib, "main"); if (fptr == NULL) { rt_kprintf("error: unable to find function in %s\n", winpath); FreeLibrary(hinstlib); return RT_NULL; } /* get the name of the module */ name = _module_name(path); /* allocate module */ module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, name); if (!module) return RT_NULL; module->nref = 0; module->module_entry = fptr; /* init module object container */ rt_module_init_object_container(module); /* increase module reference count */ module->nref ++; if (module->module_entry != 0) { rt_uint32_t *stack_size; rt_uint8_t *priority; #ifdef RT_USING_SLAB /* init module memory allocator */ module->mem_list = RT_NULL; /* create page array */ module->page_array = (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info)); module->page_cnt = 0; #endif /* get the main thread stack size */ module->stack_size = 2048; module->thread_priority = RT_THREAD_PRIORITY_MAX - 2; /* create module thread */ module->module_thread = rt_thread_create(name, (void(*)(void *))module->module_entry, RT_NULL, module->stack_size, module->thread_priority, 10); RT_DEBUG_LOG(RT_DEBUG_MODULE, ("thread entry 0x%x\n", module->module_entry)); /* set module id */ module->module_thread->module_id = (void *)module; module->parent.flag = RT_MODULE_FLAG_WITHENTRY; /* startup module thread */ rt_thread_startup(module->module_thread); } else { /* without entry point */ module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY; } #ifdef RT_USING_HOOK if (rt_module_load_hook != RT_NULL) { rt_module_load_hook(module); } #endif rt_free(name); return module; /* FreeLibrary(hinstlib); */ }