module_win32.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * File : module.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2013-02-26 prife first version for win32
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include <rtm.h>
  17. #ifdef RT_USING_MODULE
  18. static void rt_module_init_object_container(struct rt_module *module)
  19. {
  20. RT_ASSERT(module != RT_NULL);
  21. /* initialize object container - thread */
  22. rt_list_init(&(module->module_object[RT_Object_Class_Thread].object_list));
  23. module->module_object[RT_Object_Class_Thread].object_size = sizeof(struct rt_thread);
  24. module->module_object[RT_Object_Class_Thread].type = RT_Object_Class_Thread;
  25. #ifdef RT_USING_SEMAPHORE
  26. /* initialize object container - semaphore */
  27. rt_list_init(&(module->module_object[RT_Object_Class_Semaphore].object_list));
  28. module->module_object[RT_Object_Class_Semaphore].object_size = sizeof(struct rt_semaphore);
  29. module->module_object[RT_Object_Class_Semaphore].type = RT_Object_Class_Semaphore;
  30. #endif
  31. #ifdef RT_USING_MUTEX
  32. /* initialize object container - mutex */
  33. rt_list_init(&(module->module_object[RT_Object_Class_Mutex].object_list));
  34. module->module_object[RT_Object_Class_Mutex].object_size = sizeof(struct rt_mutex);
  35. module->module_object[RT_Object_Class_Mutex].type = RT_Object_Class_Mutex;
  36. #endif
  37. #ifdef RT_USING_EVENT
  38. /* initialize object container - event */
  39. rt_list_init(&(module->module_object[RT_Object_Class_Event].object_list));
  40. module->module_object[RT_Object_Class_Event].object_size = sizeof(struct rt_event);
  41. module->module_object[RT_Object_Class_Event].type = RT_Object_Class_Event;
  42. #endif
  43. #ifdef RT_USING_MAILBOX
  44. /* initialize object container - mailbox */
  45. rt_list_init(&(module->module_object[RT_Object_Class_MailBox].object_list));
  46. module->module_object[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);
  47. module->module_object[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;
  48. #endif
  49. #ifdef RT_USING_MESSAGEQUEUE
  50. /* initialize object container - message queue */
  51. rt_list_init(&(module->module_object[RT_Object_Class_MessageQueue].object_list));
  52. module->module_object[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);
  53. module->module_object[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;
  54. #endif
  55. #ifdef RT_USING_MEMHEAP
  56. /* initialize object container - memory heap */
  57. rt_list_init(&(module->module_object[RT_Object_Class_MemHeap].object_list));
  58. module->module_object[RT_Object_Class_MemHeap].object_size = sizeof(struct rt_memheap);
  59. module->module_object[RT_Object_Class_MemHeap].type = RT_Object_Class_MemHeap;
  60. #endif
  61. #ifdef RT_USING_MEMPOOL
  62. /* initialize object container - memory pool */
  63. rt_list_init(&(module->module_object[RT_Object_Class_MemPool].object_list));
  64. module->module_object[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);
  65. module->module_object[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;
  66. #endif
  67. #ifdef RT_USING_DEVICE
  68. /* initialize object container - device */
  69. rt_list_init(&(module->module_object[RT_Object_Class_Device].object_list));
  70. module->module_object[RT_Object_Class_Device].object_size = sizeof(struct rt_device);
  71. module->module_object[RT_Object_Class_Device].type = RT_Object_Class_Device;
  72. #endif
  73. /* initialize object container - timer */
  74. rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
  75. module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
  76. module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
  77. }
  78. #ifdef RT_USING_HOOK
  79. static void (*rt_module_load_hook)(rt_module_t module);
  80. static void (*rt_module_unload_hook)(rt_module_t module);
  81. /**
  82. * @addtogroup Hook
  83. */
  84. /*@{*/
  85. /**
  86. * This function will set a hook function, which will be invoked when module
  87. * be loaded to system.
  88. *
  89. * @param hook the hook function
  90. */
  91. void rt_module_load_sethook(void (*hook)(rt_module_t module))
  92. {
  93. rt_module_load_hook = hook;
  94. }
  95. /**
  96. * This function will set a hook function, which will be invoked when module
  97. * be unloaded from system.
  98. *
  99. * @param hook the hook function
  100. */
  101. void rt_module_unload_sethook(void (*hook)(rt_module_t module))
  102. {
  103. rt_module_unload_hook = hook;
  104. }
  105. /*@}*/
  106. #endif
  107. /**
  108. * @ingroup SystemInit
  109. *
  110. * This function will initialize system module
  111. */
  112. int rt_system_module_init(void)
  113. {
  114. }
  115. /**
  116. * This function will return self module object
  117. *
  118. * @return the self module object
  119. */
  120. rt_module_t rt_module_self(void)
  121. {
  122. rt_thread_t tid;
  123. tid = rt_thread_self();
  124. if (tid == RT_NULL)
  125. return RT_NULL;
  126. /* return current module */
  127. return (rt_module_t)tid->module_id;
  128. }
  129. /**
  130. * This function will find the specified module.
  131. *
  132. * @param name the name of module finding
  133. *
  134. * @return the module
  135. */
  136. rt_module_t rt_module_find(const char *name)
  137. {
  138. struct rt_object_information *information;
  139. struct rt_object *object;
  140. struct rt_list_node *node;
  141. extern struct rt_object_information rt_object_container[];
  142. RT_DEBUG_NOT_IN_INTERRUPT;
  143. /* enter critical */
  144. rt_enter_critical();
  145. /* try to find device object */
  146. information = &rt_object_container[RT_Object_Class_Module];
  147. for (node = information->object_list.next;
  148. node != &(information->object_list);
  149. node = node->next)
  150. {
  151. object = rt_list_entry(node, struct rt_object, list);
  152. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  153. {
  154. /* leave critical */
  155. rt_exit_critical();
  156. return (rt_module_t)object;
  157. }
  158. }
  159. /* leave critical */
  160. rt_exit_critical();
  161. /* not found */
  162. return RT_NULL;
  163. }
  164. #ifdef RT_USING_DFS
  165. #include <windows.h>
  166. #include <dfs_posix.h>
  167. extern char * dfs_win32_dirdup(char * path);
  168. static char* _module_name(const char *path)
  169. {
  170. const char *first, *end, *ptr;
  171. char *name;
  172. int size;
  173. ptr = (char *)path;
  174. first = ptr;
  175. end = path + rt_strlen(path);
  176. while (*ptr != '\0')
  177. {
  178. if (*ptr == '/')
  179. first = ptr + 1;
  180. if (*ptr == '.')
  181. end = ptr - 1;
  182. ptr ++;
  183. }
  184. size = end - first + 1;
  185. name = rt_malloc(size);
  186. rt_strncpy(name, first, size);
  187. name[size] = '\0';
  188. return name;
  189. }
  190. typedef int (*appentry_t)(void);
  191. /**
  192. * This function will load a module from a file
  193. *
  194. * @param path the full path of application module
  195. *
  196. * @return the module object
  197. */
  198. rt_module_t rt_module_open(const char *path)
  199. {
  200. struct dfs_filesystem *fs;
  201. appentry_t fptr;
  202. HINSTANCE hinstlib;
  203. int len;
  204. char * winpath;
  205. rt_module_t module;
  206. char * name;
  207. RT_DEBUG_NOT_IN_INTERRUPT;
  208. /* check parameters */
  209. RT_ASSERT(path != RT_NULL);
  210. /* app module should only in DFS_WIN32 */
  211. fs = dfs_filesystem_lookup(path);
  212. if ((fs == RT_NULL) || (strcmp(fs->ops->name,"wdir") != 0))
  213. {
  214. rt_kprintf("invalid path: %s\n", path);
  215. return RT_NULL;
  216. }
  217. /* change path */
  218. len = strlen(path+1);
  219. if ((winpath = dfs_win32_dirdup((char *)path)) == RT_NULL)
  220. {
  221. rt_kprintf("out of memory, exit", path);
  222. return RT_NULL;
  223. }
  224. hinstlib = LoadLibrary(winpath);
  225. if (hinstlib == NULL)
  226. {
  227. rt_kprintf("error: unable to open %s\n", winpath);
  228. return RT_NULL;
  229. }
  230. fptr = (appentry_t)GetProcAddress(hinstlib, "main");
  231. if (fptr == NULL)
  232. {
  233. rt_kprintf("error: unable to find function in %s\n", winpath);
  234. FreeLibrary(hinstlib);
  235. return RT_NULL;
  236. }
  237. /* get the name of the module */
  238. name = _module_name(path);
  239. /* allocate module */
  240. module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module,
  241. name);
  242. if (!module)
  243. return RT_NULL;
  244. module->nref = 0;
  245. module->module_entry = fptr;
  246. /* init module object container */
  247. rt_module_init_object_container(module);
  248. /* increase module reference count */
  249. module->nref ++;
  250. if (module->module_entry != 0)
  251. {
  252. rt_uint32_t *stack_size;
  253. rt_uint8_t *priority;
  254. #ifdef RT_USING_SLAB
  255. /* init module memory allocator */
  256. module->mem_list = RT_NULL;
  257. /* create page array */
  258. module->page_array =
  259. (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info));
  260. module->page_cnt = 0;
  261. #endif
  262. /* get the main thread stack size */
  263. module->stack_size = 2048;
  264. module->thread_priority = RT_THREAD_PRIORITY_MAX - 2;
  265. /* create module thread */
  266. module->module_thread =
  267. rt_thread_create(name,
  268. (void(*)(void *))module->module_entry,
  269. RT_NULL,
  270. module->stack_size,
  271. module->thread_priority,
  272. 10);
  273. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("thread entry 0x%x\n",
  274. module->module_entry));
  275. /* set module id */
  276. module->module_thread->module_id = (void *)module;
  277. module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
  278. /* startup module thread */
  279. rt_thread_startup(module->module_thread);
  280. }
  281. else
  282. {
  283. /* without entry point */
  284. module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
  285. }
  286. #ifdef RT_USING_HOOK
  287. if (rt_module_load_hook != RT_NULL)
  288. {
  289. rt_module_load_hook(module);
  290. }
  291. #endif
  292. rt_free(name);
  293. return module;
  294. /* FreeLibrary(hinstlib); */
  295. }
  296. #if defined(RT_USING_FINSH)
  297. #include <finsh.h>
  298. FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from a file);
  299. #endif
  300. #endif
  301. rt_err_t rt_module_destroy(rt_module_t module)
  302. {
  303. return 0;
  304. }
  305. #endif