1
0

lwp_ipc.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-10-12 Jesven first version
  9. * 2023-09-16 zmq810150896 Increased versatility of some features on dfs v2
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <lwp.h>
  14. #include "lwp_ipc.h"
  15. #include "lwp_ipc_internal.h"
  16. #include <dfs_file.h>
  17. #include <poll.h>
  18. /**
  19. * the IPC channel states
  20. */
  21. enum
  22. {
  23. RT_IPC_STAT_IDLE, /* no suspended threads */
  24. RT_IPC_STAT_WAIT, /* suspended receivers exist */
  25. RT_IPC_STAT_ACTIVE, /* suspended senders exist */
  26. };
  27. /**
  28. * IPC message structure.
  29. *
  30. * They are allocated and released in the similar way like 'rt_chfd'.
  31. */
  32. struct rt_ipc_msg
  33. {
  34. struct rt_channel_msg msg; /**< the payload of msg */
  35. rt_list_t mlist; /**< the msg list */
  36. rt_uint8_t need_reply; /**< whether msg wait reply*/
  37. };
  38. typedef struct rt_ipc_msg *rt_ipc_msg_t;
  39. static rt_ipc_msg_t _ipc_msg_free_list = (rt_ipc_msg_t)RT_NULL; /* released chain */
  40. static int rt_ipc_msg_used = 0; /* first unallocated entry */
  41. static struct rt_ipc_msg ipc_msg_pool[RT_CH_MSG_MAX_NR]; /* initial message array */
  42. /**
  43. * Allocate an IPC message from the statically-allocated array.
  44. */
  45. static rt_ipc_msg_t _ipc_msg_alloc(void)
  46. {
  47. rt_ipc_msg_t p = (rt_ipc_msg_t)RT_NULL;
  48. if (_ipc_msg_free_list) /* use the released chain first */
  49. {
  50. p = _ipc_msg_free_list;
  51. _ipc_msg_free_list = (rt_ipc_msg_t)p->msg.sender; /* emtry payload as a pointer */
  52. }
  53. else if (rt_ipc_msg_used < RT_CH_MSG_MAX_NR)
  54. {
  55. p = &ipc_msg_pool[rt_ipc_msg_used];
  56. rt_ipc_msg_used++;
  57. }
  58. return p;
  59. }
  60. /**
  61. * Put a released IPC message back to the released chain.
  62. */
  63. static void _ipc_msg_free(rt_ipc_msg_t p_msg)
  64. {
  65. p_msg->msg.sender = (void*)_ipc_msg_free_list;
  66. _ipc_msg_free_list = p_msg;
  67. }
  68. /**
  69. * Initialized the IPC message.
  70. */
  71. static void rt_ipc_msg_init(rt_ipc_msg_t msg, struct rt_channel_msg *data, rt_uint8_t need_reply)
  72. {
  73. RT_ASSERT(msg != RT_NULL);
  74. msg->need_reply = need_reply;
  75. msg->msg = *data;
  76. msg->msg.sender = (void*)rt_thread_self();
  77. rt_list_init(&msg->mlist);
  78. }
  79. /**
  80. * Initialized the list of the waiting receivers on the IPC channel.
  81. */
  82. rt_inline rt_err_t rt_channel_object_init(struct rt_ipc_object *ipc)
  83. {
  84. rt_list_init(&(ipc->suspend_thread)); /* receiver list */
  85. return RT_EOK;
  86. }
  87. /**
  88. * Wakeup the first suspened thread in the list.
  89. */
  90. rt_inline rt_err_t rt_channel_list_resume(rt_list_t *list)
  91. {
  92. struct rt_thread *thread;
  93. /* get the first thread entry waiting for sending */
  94. thread = rt_list_entry(list->next, struct rt_thread, tlist);
  95. rt_thread_resume(thread);
  96. return RT_EOK;
  97. }
  98. /**
  99. * Wakeup all the suspended threads in the list.
  100. */
  101. rt_inline rt_err_t rt_channel_list_resume_all(rt_list_t *list)
  102. {
  103. struct rt_thread *thread;
  104. register rt_ubase_t temp;
  105. /* wakeup all suspended threads for sending */
  106. while (!rt_list_isempty(list))
  107. {
  108. temp = rt_hw_interrupt_disable();
  109. thread = rt_list_entry(list->next, struct rt_thread, tlist);
  110. thread->error = -RT_ERROR;
  111. rt_thread_resume(thread);
  112. rt_hw_interrupt_enable(temp);
  113. }
  114. return RT_EOK;
  115. }
  116. /**
  117. * Suspend the thread and chain it into the end of the list.
  118. */
  119. rt_inline rt_err_t rt_channel_list_suspend(rt_list_t *list, struct rt_thread *thread)
  120. {
  121. /* suspend thread */
  122. rt_err_t ret = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE);
  123. if (ret == RT_EOK)
  124. {
  125. rt_list_insert_before(list, &(thread->tlist)); /* list end */
  126. }
  127. return ret;
  128. }
  129. static void _rt_channel_check_wq_wakup(rt_channel_t ch)
  130. {
  131. rt_base_t level;
  132. level = rt_hw_interrupt_disable();
  133. if (rt_list_isempty(&ch->wait_msg))
  134. {
  135. rt_hw_interrupt_enable(level);
  136. return;
  137. }
  138. rt_wqueue_wakeup(&ch->reader_queue, 0);
  139. rt_hw_interrupt_enable(level);
  140. }
  141. /**
  142. * Create a new or open an existing IPC channel.
  143. */
  144. rt_channel_t rt_raw_channel_open(const char *name, int flags)
  145. {
  146. register rt_ubase_t temp = 0;
  147. rt_channel_t ch = RT_NULL;
  148. struct rt_object *object;
  149. struct rt_list_node *node;
  150. struct rt_object_information *information;
  151. temp = rt_hw_interrupt_disable();
  152. information = rt_object_get_information(RT_Object_Class_Channel);
  153. RT_ASSERT(information != RT_NULL);
  154. /* retrieve the existing IPC channels */
  155. for (node = information->object_list.next;
  156. node != &(information->object_list);
  157. node = node->next)
  158. {
  159. object = rt_list_entry(node, struct rt_object, list);
  160. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  161. {
  162. if ((flags & O_CREAT) && (flags & O_EXCL))
  163. {
  164. goto quit;
  165. }
  166. /* find the IPC channel with the specific name */
  167. ch = (rt_channel_t)object;
  168. ch->ref++; /* increase the reference count */
  169. break;
  170. }
  171. }
  172. if (!ch) /* create a new IPC channel */
  173. {
  174. if (flags & O_CREAT)
  175. {
  176. RT_DEBUG_NOT_IN_INTERRUPT;
  177. /* allocate a real IPC channel structure */
  178. ch = (rt_channel_t)rt_object_allocate(RT_Object_Class_Channel, name);
  179. }
  180. if (!ch)
  181. {
  182. goto quit;
  183. }
  184. rt_channel_object_init(&ch->parent); /* suspended receivers */
  185. rt_list_init(&ch->wait_msg); /* unhandled messages */
  186. rt_list_init(&ch->wait_thread); /* suspended senders */
  187. rt_wqueue_init(&ch->reader_queue); /* reader poll queue */
  188. ch->reply = RT_NULL;
  189. ch->stat = RT_IPC_STAT_IDLE; /* no suspended threads */
  190. ch->ref = 1;
  191. }
  192. quit:
  193. rt_hw_interrupt_enable(temp);
  194. return ch;
  195. }
  196. /**
  197. * Close an existiong IPC channel, release the resources.
  198. */
  199. rt_err_t rt_raw_channel_close(rt_channel_t ch)
  200. {
  201. register rt_ubase_t temp;
  202. RT_DEBUG_NOT_IN_INTERRUPT;
  203. if (ch == RT_NULL)
  204. {
  205. return -RT_EIO;
  206. }
  207. temp = rt_hw_interrupt_disable();
  208. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  209. {
  210. rt_hw_interrupt_enable(temp);
  211. return -RT_EIO;
  212. }
  213. if (rt_object_is_systemobject(&ch->parent.parent) != RT_FALSE)
  214. {
  215. rt_hw_interrupt_enable(temp);
  216. return -RT_EIO;
  217. }
  218. if (ch->ref == 0)
  219. {
  220. rt_hw_interrupt_enable(temp);
  221. return -RT_EIO;
  222. }
  223. ch->ref--;
  224. if (ch->ref == 0)
  225. {
  226. /* wakeup all the suspended receivers and senders */
  227. rt_channel_list_resume_all(&ch->parent.suspend_thread);
  228. rt_channel_list_resume_all(&ch->wait_thread);
  229. /* all ipc msg will lost */
  230. rt_list_init(&ch->wait_msg);
  231. rt_object_delete(&ch->parent.parent); /* release the IPC channel structure */
  232. }
  233. rt_hw_interrupt_enable(temp);
  234. return RT_EOK;
  235. }
  236. static rt_err_t wakeup_sender_wait_recv(void *object, struct rt_thread *thread)
  237. {
  238. rt_channel_t ch;
  239. ch = (rt_channel_t)object;
  240. if (ch->stat == RT_IPC_STAT_ACTIVE && ch->reply == thread)
  241. {
  242. ch->stat = RT_IPC_STAT_IDLE;
  243. ch->reply = RT_NULL;
  244. }
  245. else
  246. {
  247. rt_ipc_msg_t msg;
  248. rt_list_t *l;
  249. l = ch->wait_msg.next;
  250. while (l != &ch->wait_msg)
  251. {
  252. msg = rt_list_entry(l, struct rt_ipc_msg, mlist);
  253. if (msg->need_reply && msg->msg.sender == thread)
  254. {
  255. rt_list_remove(&msg->mlist); /* remove the msg from the channel */
  256. _ipc_msg_free(msg);
  257. break;
  258. }
  259. l = l->next;
  260. }
  261. }
  262. thread->error = -RT_EINTR;
  263. return rt_thread_resume(thread); /* wake up the sender */
  264. }
  265. static rt_err_t wakeup_sender_wait_reply(void *object, struct rt_thread *thread)
  266. {
  267. rt_channel_t ch;
  268. ch = (rt_channel_t)object;
  269. RT_ASSERT(ch->stat == RT_IPC_STAT_ACTIVE && ch->reply == thread);
  270. ch->stat = RT_IPC_STAT_IDLE;
  271. ch->reply = RT_NULL;
  272. thread->error = -RT_EINTR;
  273. return rt_thread_resume(thread); /* wake up the sender */
  274. }
  275. static void sender_timeout(void *parameter)
  276. {
  277. struct rt_thread *thread = (struct rt_thread*)parameter;
  278. rt_channel_t ch;
  279. ch = (rt_channel_t)(thread->wakeup.user_data);
  280. if (ch->stat == RT_IPC_STAT_ACTIVE && ch->reply == thread)
  281. {
  282. ch->stat = RT_IPC_STAT_IDLE;
  283. ch->reply = RT_NULL;
  284. }
  285. else
  286. {
  287. rt_ipc_msg_t msg;
  288. rt_list_t *l;
  289. l = ch->wait_msg.next;
  290. while (l != &ch->wait_msg)
  291. {
  292. msg = rt_list_entry(l, struct rt_ipc_msg, mlist);
  293. if (msg->need_reply && msg->msg.sender == thread)
  294. {
  295. rt_list_remove(&msg->mlist); /* remove the msg from the channel */
  296. _ipc_msg_free(msg);
  297. break;
  298. }
  299. l = l->next;
  300. }
  301. }
  302. thread->error = -RT_ETIMEOUT;
  303. thread->wakeup.func = RT_NULL;
  304. rt_list_remove(&(thread->tlist));
  305. /* insert to schedule ready list */
  306. rt_schedule_insert_thread(thread);
  307. /* do schedule */
  308. rt_schedule();
  309. }
  310. /**
  311. * Get file vnode from fd.
  312. */
  313. static void *_ipc_msg_get_file(int fd)
  314. {
  315. struct dfs_file *d;
  316. d = fd_get(fd);
  317. if (d == RT_NULL)
  318. return RT_NULL;
  319. if (!d->vnode)
  320. return RT_NULL;
  321. d->vnode->ref_count++;
  322. return (void *)d;
  323. }
  324. /**
  325. * Get fd from file vnode.
  326. */
  327. static int _ipc_msg_fd_new(void *file)
  328. {
  329. int fd;
  330. struct dfs_file *d;
  331. struct dfs_file *df = RT_NULL;
  332. if (file == RT_NULL)
  333. {
  334. return -1;
  335. }
  336. df = (struct dfs_file *)file;
  337. fd = fd_new();
  338. if (fd < 0)
  339. {
  340. return -1;
  341. }
  342. d = fd_get(fd);
  343. if (!d)
  344. {
  345. fd_release(fd);
  346. return -1;
  347. }
  348. #ifdef RT_USING_DFS_V2
  349. d->fops = df->fops;
  350. d->mode = df->mode;
  351. d->dentry = df->dentry;
  352. #endif
  353. d->vnode = df->vnode;
  354. d->flags = df->flags;
  355. d->data = df->data;
  356. d->magic = df->magic;
  357. return fd;
  358. }
  359. /**
  360. * Send data through an IPC channel, wait for the reply or not.
  361. */
  362. static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, int need_reply, rt_channel_msg_t data_ret, rt_int32_t time)
  363. {
  364. rt_ipc_msg_t msg;
  365. struct rt_thread *thread_recv, *thread_send = 0;
  366. register rt_base_t temp;
  367. rt_err_t ret;
  368. void (*old_timeout_func)(void *) = 0;
  369. if (need_reply)
  370. {
  371. RT_DEBUG_NOT_IN_INTERRUPT;
  372. }
  373. if (ch == RT_NULL)
  374. {
  375. return -RT_EIO;
  376. }
  377. temp = rt_hw_interrupt_disable();
  378. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  379. {
  380. rt_hw_interrupt_enable(temp);
  381. return -RT_EIO;
  382. }
  383. if (need_reply && time == 0)
  384. {
  385. rt_hw_interrupt_enable(temp);
  386. return -RT_ETIMEOUT;
  387. }
  388. /* allocate an IPC message */
  389. msg = _ipc_msg_alloc();
  390. if (!msg)
  391. {
  392. rt_hw_interrupt_enable(temp);
  393. return -RT_ENOMEM;
  394. }
  395. /* IPC message : file descriptor */
  396. if (data->type == RT_CHANNEL_FD)
  397. {
  398. data->u.fd.file = _ipc_msg_get_file(data->u.fd.fd);
  399. }
  400. rt_ipc_msg_init(msg, data, need_reply);
  401. if (need_reply)
  402. {
  403. thread_send = rt_thread_self();
  404. thread_send->error = RT_EOK;
  405. }
  406. switch (ch->stat)
  407. {
  408. case RT_IPC_STAT_IDLE:
  409. case RT_IPC_STAT_ACTIVE:
  410. if (need_reply)
  411. {
  412. ret = rt_channel_list_suspend(&ch->wait_thread, thread_send);
  413. if (ret != RT_EOK)
  414. {
  415. _ipc_msg_free(msg);
  416. rt_hw_interrupt_enable(temp);
  417. return ret;
  418. }
  419. rt_thread_wakeup_set(thread_send, wakeup_sender_wait_recv, (void*)ch);
  420. if (time > 0)
  421. {
  422. rt_timer_control(&(thread_send->thread_timer),
  423. RT_TIMER_CTRL_GET_FUNC,
  424. &old_timeout_func);
  425. rt_timer_control(&(thread_send->thread_timer),
  426. RT_TIMER_CTRL_SET_FUNC,
  427. sender_timeout);
  428. /* reset the timeout of thread timer and start it */
  429. rt_timer_control(&(thread_send->thread_timer),
  430. RT_TIMER_CTRL_SET_TIME,
  431. &time);
  432. rt_timer_start(&(thread_send->thread_timer));
  433. }
  434. }
  435. /*
  436. * If there is no thread waiting for messages, chain the message
  437. * into the list.
  438. */
  439. rt_list_insert_before(&ch->wait_msg, &msg->mlist);
  440. break;
  441. case RT_IPC_STAT_WAIT:
  442. /*
  443. * If there are suspended receivers on the IPC channel, transfer the
  444. * pointer of the message to the first receiver directly and wake it
  445. * up.
  446. */
  447. RT_ASSERT(ch->parent.suspend_thread.next != &ch->parent.suspend_thread);
  448. if (need_reply)
  449. {
  450. ret = rt_channel_list_suspend(&ch->wait_thread, thread_send);
  451. if (ret != RT_EOK)
  452. {
  453. _ipc_msg_free(msg);
  454. rt_hw_interrupt_enable(temp);
  455. return ret;
  456. }
  457. ch->reply = thread_send; /* record the current waiting sender */
  458. ch->stat = RT_IPC_STAT_ACTIVE;
  459. rt_thread_wakeup_set(thread_send, wakeup_sender_wait_reply, (void*)ch);
  460. if (time > 0)
  461. {
  462. rt_timer_control(&(thread_send->thread_timer),
  463. RT_TIMER_CTRL_GET_FUNC,
  464. &old_timeout_func);
  465. rt_timer_control(&(thread_send->thread_timer),
  466. RT_TIMER_CTRL_SET_FUNC,
  467. sender_timeout);
  468. /* reset the timeout of thread timer and start it */
  469. rt_timer_control(&(thread_send->thread_timer),
  470. RT_TIMER_CTRL_SET_TIME,
  471. &time);
  472. rt_timer_start(&(thread_send->thread_timer));
  473. }
  474. }
  475. else
  476. {
  477. ch->stat = RT_IPC_STAT_IDLE;
  478. }
  479. thread_recv = rt_list_entry(ch->parent.suspend_thread.next, struct rt_thread, tlist);
  480. thread_recv->msg_ret = msg; /* to the first suspended receiver */
  481. thread_recv->error = RT_EOK;
  482. rt_channel_list_resume(&ch->parent.suspend_thread);
  483. break;
  484. default:
  485. break;
  486. }
  487. if ( ch->stat == RT_IPC_STAT_IDLE)
  488. {
  489. _rt_channel_check_wq_wakup(ch);
  490. }
  491. rt_hw_interrupt_enable(temp);
  492. /* reschedule in order to let the potential receivers run */
  493. rt_schedule();
  494. if (need_reply)
  495. {
  496. temp = rt_hw_interrupt_disable();
  497. if (old_timeout_func)
  498. {
  499. rt_timer_control(&(thread_send->thread_timer),
  500. RT_TIMER_CTRL_SET_FUNC,
  501. old_timeout_func);
  502. }
  503. ret = thread_send->error;
  504. rt_hw_interrupt_enable(temp);
  505. if (ret != RT_EOK)
  506. {
  507. return ret;
  508. }
  509. /* If the sender gets the chance to run, the requested reply must be valid. */
  510. RT_ASSERT(data_ret != RT_NULL);
  511. *data_ret = ((rt_ipc_msg_t)(thread_send->msg_ret))->msg; /* extract data */
  512. temp = rt_hw_interrupt_disable();
  513. _ipc_msg_free(thread_send->msg_ret); /* put back the message to kernel */
  514. rt_hw_interrupt_enable(temp);
  515. thread_send->msg_ret = RT_NULL;
  516. }
  517. return RT_EOK;
  518. }
  519. /**
  520. * Send data through an IPC channel with no reply.
  521. */
  522. rt_err_t rt_raw_channel_send(rt_channel_t ch, rt_channel_msg_t data)
  523. {
  524. return _rt_raw_channel_send_recv_timeout(ch, data, 0, 0, RT_WAITING_FOREVER);
  525. }
  526. /**
  527. * Send data through an IPC channel and wait for the relpy.
  528. */
  529. rt_err_t rt_raw_channel_send_recv(rt_channel_t ch, rt_channel_msg_t data, rt_channel_msg_t data_ret)
  530. {
  531. return _rt_raw_channel_send_recv_timeout(ch, data, 1, data_ret, RT_WAITING_FOREVER);
  532. }
  533. /**
  534. * Send data through an IPC channel and wait for the relpy.
  535. */
  536. rt_err_t rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_channel_msg_t data_ret, rt_int32_t time)
  537. {
  538. return _rt_raw_channel_send_recv_timeout(ch, data, 1, data_ret, time);
  539. }
  540. /**
  541. * Reply to the waiting sender and wake it up.
  542. */
  543. rt_err_t rt_raw_channel_reply(rt_channel_t ch, rt_channel_msg_t data)
  544. {
  545. rt_ipc_msg_t msg;
  546. struct rt_thread *thread;
  547. register rt_base_t temp;
  548. if (ch == RT_NULL)
  549. {
  550. return -RT_EIO;
  551. }
  552. temp = rt_hw_interrupt_disable();
  553. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  554. {
  555. rt_hw_interrupt_enable(temp);
  556. return -RT_EIO;
  557. }
  558. if (ch->stat != RT_IPC_STAT_ACTIVE)
  559. {
  560. rt_hw_interrupt_enable(temp);
  561. return -RT_ERROR;
  562. }
  563. if (ch->reply == RT_NULL)
  564. {
  565. rt_hw_interrupt_enable(temp);
  566. return -RT_ERROR;
  567. }
  568. /* allocate an IPC message */
  569. msg = _ipc_msg_alloc();
  570. if (!msg)
  571. {
  572. rt_hw_interrupt_enable(temp);
  573. return -RT_ENOMEM;
  574. }
  575. rt_ipc_msg_init(msg, data, 0);
  576. thread = ch->reply;
  577. thread->msg_ret = msg; /* transfer the reply to the sender */
  578. rt_thread_resume(thread); /* wake up the sender */
  579. ch->stat = RT_IPC_STAT_IDLE;
  580. ch->reply = RT_NULL;
  581. _rt_channel_check_wq_wakup(ch);
  582. rt_hw_interrupt_enable(temp);
  583. rt_schedule();
  584. return RT_EOK;
  585. }
  586. static rt_err_t wakeup_receiver(void *object, struct rt_thread *thread)
  587. {
  588. rt_channel_t ch;
  589. rt_err_t ret;
  590. ch = (rt_channel_t)object;
  591. ch->stat = RT_IPC_STAT_IDLE;
  592. thread->error = -RT_EINTR;
  593. ret = rt_channel_list_resume(&ch->parent.suspend_thread);
  594. _rt_channel_check_wq_wakup(ch);
  595. return ret;
  596. }
  597. static void receiver_timeout(void *parameter)
  598. {
  599. struct rt_thread *thread = (struct rt_thread*)parameter;
  600. rt_channel_t ch;
  601. ch = (rt_channel_t)(thread->wakeup.user_data);
  602. ch->stat = RT_IPC_STAT_IDLE;
  603. thread->error = -RT_ETIMEOUT;
  604. thread->wakeup.func = RT_NULL;
  605. rt_list_remove(&(thread->tlist));
  606. /* insert to schedule ready list */
  607. rt_schedule_insert_thread(thread);
  608. _rt_channel_check_wq_wakup(ch);
  609. /* do schedule */
  610. rt_schedule();
  611. }
  612. /**
  613. * Fetch a message from the specified IPC channel.
  614. */
  615. static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_int32_t time)
  616. {
  617. struct rt_thread *thread;
  618. rt_ipc_msg_t msg_ret;
  619. register rt_base_t temp;
  620. rt_err_t ret;
  621. void (*old_timeout_func)(void *) = 0;
  622. RT_DEBUG_NOT_IN_INTERRUPT;
  623. if (ch == RT_NULL)
  624. {
  625. return -RT_EIO;
  626. }
  627. temp = rt_hw_interrupt_disable();
  628. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  629. {
  630. rt_hw_interrupt_enable(temp);
  631. return -RT_EIO;
  632. }
  633. if (ch->stat != RT_IPC_STAT_IDLE)
  634. {
  635. rt_hw_interrupt_enable(temp);
  636. return -RT_ERROR;
  637. }
  638. if (ch->wait_msg.next != &ch->wait_msg) /* there exist unhandled messages */
  639. {
  640. msg_ret = rt_list_entry(ch->wait_msg.next, struct rt_ipc_msg, mlist);
  641. rt_list_remove(ch->wait_msg.next); /* remove the message from the channel */
  642. if (msg_ret->need_reply)
  643. {
  644. RT_ASSERT(ch->wait_thread.next != &ch->wait_thread);
  645. thread = rt_list_entry(ch->wait_thread.next, struct rt_thread, tlist);
  646. rt_list_remove(ch->wait_thread.next);
  647. ch->reply = thread; /* record the waiting sender */
  648. ch->stat = RT_IPC_STAT_ACTIVE; /* no valid suspened receivers */
  649. }
  650. *data = msg_ret->msg; /* extract the transferred data */
  651. if (data->type == RT_CHANNEL_FD)
  652. {
  653. data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
  654. }
  655. _ipc_msg_free(msg_ret); /* put back the message to kernel */
  656. }
  657. else
  658. {
  659. if (time == 0)
  660. {
  661. rt_hw_interrupt_enable(temp);
  662. return -RT_ETIMEOUT;
  663. }
  664. /* no valid message, we must wait */
  665. thread = rt_thread_self();
  666. ret = rt_channel_list_suspend(&ch->parent.suspend_thread, thread);
  667. if (ret != RT_EOK)
  668. {
  669. rt_hw_interrupt_enable(temp);
  670. return ret;
  671. }
  672. rt_thread_wakeup_set(thread, wakeup_receiver, (void*)ch);
  673. ch->stat = RT_IPC_STAT_WAIT;/* no valid suspended senders */
  674. thread->error = RT_EOK;
  675. if (time > 0)
  676. {
  677. rt_timer_control(&(thread->thread_timer),
  678. RT_TIMER_CTRL_GET_FUNC,
  679. &old_timeout_func);
  680. rt_timer_control(&(thread->thread_timer),
  681. RT_TIMER_CTRL_SET_FUNC,
  682. receiver_timeout);
  683. /* reset the timeout of thread timer and start it */
  684. rt_timer_control(&(thread->thread_timer),
  685. RT_TIMER_CTRL_SET_TIME,
  686. &time);
  687. rt_timer_start(&(thread->thread_timer));
  688. }
  689. rt_hw_interrupt_enable(temp);
  690. rt_schedule(); /* let the senders run */
  691. temp = rt_hw_interrupt_disable();
  692. if (old_timeout_func)
  693. {
  694. rt_timer_control(&(thread->thread_timer),
  695. RT_TIMER_CTRL_SET_FUNC,
  696. old_timeout_func);
  697. }
  698. ret = thread->error;
  699. if ( ret != RT_EOK)
  700. {
  701. rt_hw_interrupt_enable(temp);
  702. return ret;
  703. }
  704. /* If waked up, the received message has been store into the thread. */
  705. *data = ((rt_ipc_msg_t)(thread->msg_ret))->msg; /* extract data */
  706. if (data->type == RT_CHANNEL_FD)
  707. {
  708. data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
  709. }
  710. _ipc_msg_free(thread->msg_ret); /* put back the message to kernel */
  711. thread->msg_ret = RT_NULL;
  712. }
  713. rt_hw_interrupt_enable(temp);
  714. return RT_EOK;
  715. }
  716. rt_err_t rt_raw_channel_recv(rt_channel_t ch, rt_channel_msg_t data)
  717. {
  718. return _rt_raw_channel_recv_timeout(ch, data, RT_WAITING_FOREVER);
  719. }
  720. rt_err_t rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_int32_t time)
  721. {
  722. return _rt_raw_channel_recv_timeout(ch, data, time);
  723. }
  724. /**
  725. * Peek a message from the specified IPC channel.
  726. */
  727. rt_err_t rt_raw_channel_peek(rt_channel_t ch, rt_channel_msg_t data)
  728. {
  729. return _rt_raw_channel_recv_timeout(ch, data, 0);
  730. }
  731. /* for API */
  732. static int lwp_fd_new(int fdt_type)
  733. {
  734. struct dfs_fdtable *fdt;
  735. if (fdt_type)
  736. {
  737. fdt = dfs_fdtable_get_global();
  738. }
  739. else
  740. {
  741. fdt = dfs_fdtable_get();
  742. }
  743. return fdt_fd_new(fdt);
  744. }
  745. static struct dfs_file *lwp_fd_get(int fdt_type, int fd)
  746. {
  747. struct dfs_fdtable *fdt;
  748. if (fdt_type)
  749. {
  750. fdt = dfs_fdtable_get_global();
  751. }
  752. else
  753. {
  754. fdt = dfs_fdtable_get();
  755. }
  756. return fdt_fd_get(fdt, fd);
  757. }
  758. static void lwp_fd_release(int fdt_type, int fd)
  759. {
  760. struct dfs_fdtable *fdt;
  761. if (fdt_type)
  762. {
  763. fdt = dfs_fdtable_get_global();
  764. }
  765. else
  766. {
  767. fdt = dfs_fdtable_get();
  768. }
  769. fdt_fd_release(fdt, fd);
  770. }
  771. static int _chfd_alloc(int fdt_type)
  772. {
  773. /* create a BSD socket */
  774. int fd;
  775. /* allocate a fd */
  776. fd = lwp_fd_new(fdt_type);
  777. if (fd < 0)
  778. {
  779. return -1;
  780. }
  781. return fd;
  782. }
  783. static void _chfd_free(int fd, int fdt_type)
  784. {
  785. struct dfs_file *d;
  786. d = lwp_fd_get(fdt_type, fd);
  787. if (d == RT_NULL)
  788. {
  789. return;
  790. }
  791. lwp_fd_release(fdt_type, fd);
  792. }
  793. /* for fops */
  794. static int channel_fops_poll(struct dfs_file *file, struct rt_pollreq *req)
  795. {
  796. int mask = POLLOUT;
  797. rt_channel_t ch;
  798. ch = (rt_channel_t)file->vnode->data;
  799. rt_poll_add(&(ch->reader_queue), req);
  800. if (ch->stat != RT_IPC_STAT_IDLE)
  801. {
  802. return mask;
  803. }
  804. if (!rt_list_isempty(&ch->wait_msg))
  805. {
  806. mask |= POLLIN;
  807. }
  808. return mask;
  809. }
  810. static int channel_fops_close(struct dfs_file *file)
  811. {
  812. rt_channel_t ch;
  813. rt_base_t level;
  814. level = rt_hw_interrupt_disable();
  815. ch = (rt_channel_t)file->vnode->data;
  816. if (file->vnode->ref_count == 1)
  817. {
  818. ch->ref--;
  819. if (ch->ref == 0)
  820. {
  821. /* wakeup all the suspended receivers and senders */
  822. rt_channel_list_resume_all(&ch->parent.suspend_thread);
  823. rt_channel_list_resume_all(&ch->wait_thread);
  824. /* all ipc msg will lost */
  825. rt_list_init(&ch->wait_msg);
  826. rt_object_delete(&ch->parent.parent); /* release the IPC channel structure */
  827. }
  828. }
  829. rt_hw_interrupt_enable(level);
  830. return 0;
  831. }
  832. static const struct dfs_file_ops channel_fops =
  833. {
  834. .close = channel_fops_close, /* close */
  835. .poll = channel_fops_poll, /* poll */
  836. };
  837. int lwp_channel_open(int fdt_type, const char *name, int flags)
  838. {
  839. int fd;
  840. rt_channel_t ch = RT_NULL;
  841. struct dfs_file *d;
  842. fd = _chfd_alloc(fdt_type); /* allocate an IPC channel descriptor */
  843. if (fd == -1)
  844. {
  845. goto quit;
  846. }
  847. d = lwp_fd_get(fdt_type, fd);
  848. d->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
  849. if (!d->vnode)
  850. {
  851. _chfd_free(fd, fdt_type);
  852. fd = -1;
  853. goto quit;
  854. }
  855. ch = rt_raw_channel_open(name, flags);
  856. if (ch)
  857. {
  858. /* initialize vnode */
  859. dfs_vnode_init(d->vnode, FT_USER, &channel_fops);
  860. d->flags = O_RDWR; /* set flags as read and write */
  861. /* set socket to the data of dfs_file */
  862. d->vnode->data = (void *)ch;
  863. }
  864. else
  865. {
  866. rt_free(d->vnode);
  867. d->vnode = RT_NULL;
  868. _chfd_free(fd, fdt_type);
  869. fd = -1;
  870. }
  871. quit:
  872. return fd;
  873. }
  874. static rt_channel_t fd_2_channel(int fdt_type, int fd)
  875. {
  876. struct dfs_file *d;
  877. d = lwp_fd_get(fdt_type, fd);
  878. if (d)
  879. {
  880. rt_channel_t ch;
  881. ch = (rt_channel_t)d->vnode->data;
  882. if (ch)
  883. {
  884. return ch;
  885. }
  886. }
  887. return RT_NULL;
  888. }
  889. rt_err_t lwp_channel_close(int fdt_type, int fd)
  890. {
  891. rt_channel_t ch;
  892. struct dfs_file *d;
  893. struct dfs_vnode *vnode;
  894. d = lwp_fd_get(fdt_type, fd);
  895. if (!d)
  896. {
  897. return -RT_EIO;
  898. }
  899. vnode = d->vnode;
  900. if (!vnode)
  901. {
  902. return -RT_EIO;
  903. }
  904. ch = fd_2_channel(fdt_type, fd);
  905. if (!ch)
  906. {
  907. return -RT_EIO;
  908. }
  909. _chfd_free(fd, fdt_type);
  910. if (vnode->ref_count == 1)
  911. {
  912. rt_free(vnode);
  913. return rt_raw_channel_close(ch);
  914. }
  915. return 0;
  916. }
  917. rt_err_t lwp_channel_send(int fdt_type, int fd, rt_channel_msg_t data)
  918. {
  919. rt_channel_t ch;
  920. ch = fd_2_channel(fdt_type, fd);
  921. if (ch)
  922. {
  923. return rt_raw_channel_send(ch, data);
  924. }
  925. return -RT_EIO;
  926. }
  927. rt_err_t lwp_channel_send_recv_timeout(int fdt_type, int fd, rt_channel_msg_t data, rt_channel_msg_t data_ret, rt_int32_t time)
  928. {
  929. rt_channel_t ch;
  930. ch = fd_2_channel(fdt_type, fd);
  931. if (ch)
  932. {
  933. return rt_raw_channel_send_recv_timeout(ch, data, data_ret, time);
  934. }
  935. return -RT_EIO;
  936. }
  937. rt_err_t lwp_channel_reply(int fdt_type, int fd, rt_channel_msg_t data)
  938. {
  939. rt_channel_t ch;
  940. ch = fd_2_channel(fdt_type, fd);
  941. if (ch)
  942. {
  943. return rt_raw_channel_reply(ch, data);
  944. }
  945. return -RT_EIO;
  946. }
  947. rt_err_t lwp_channel_recv_timeout(int fdt_type, int fd, rt_channel_msg_t data, rt_int32_t time)
  948. {
  949. rt_channel_t ch;
  950. ch = fd_2_channel(fdt_type, fd);
  951. if (ch)
  952. {
  953. return rt_raw_channel_recv_timeout(ch, data, time);
  954. }
  955. return -RT_EIO;
  956. }
  957. int rt_channel_open(const char *name, int flags)
  958. {
  959. return lwp_channel_open(FDT_TYPE_KERNEL, name, flags);
  960. }
  961. rt_err_t rt_channel_close(int fd)
  962. {
  963. return lwp_channel_close(FDT_TYPE_KERNEL, fd);
  964. }
  965. rt_err_t rt_channel_send(int fd, rt_channel_msg_t data)
  966. {
  967. return lwp_channel_send(FDT_TYPE_KERNEL, fd, data);
  968. }
  969. rt_err_t rt_channel_send_recv_timeout(int fd, rt_channel_msg_t data, rt_channel_msg_t data_ret, rt_int32_t time)
  970. {
  971. return lwp_channel_send_recv_timeout(FDT_TYPE_KERNEL, fd, data, data_ret, time);
  972. }
  973. rt_err_t rt_channel_send_recv(int fd, rt_channel_msg_t data, rt_channel_msg_t data_ret)
  974. {
  975. return lwp_channel_send_recv_timeout(FDT_TYPE_KERNEL, fd, data, data_ret, RT_WAITING_FOREVER);
  976. }
  977. rt_err_t rt_channel_reply(int fd, rt_channel_msg_t data)
  978. {
  979. return lwp_channel_reply(FDT_TYPE_KERNEL, fd, data);
  980. }
  981. rt_err_t rt_channel_recv_timeout(int fd, rt_channel_msg_t data, rt_int32_t time)
  982. {
  983. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, time);
  984. }
  985. rt_err_t rt_channel_recv(int fd, rt_channel_msg_t data)
  986. {
  987. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, RT_WAITING_FOREVER);
  988. }
  989. rt_err_t rt_channel_peek(int fd, rt_channel_msg_t data)
  990. {
  991. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, 0);
  992. }
  993. #ifdef RT_USING_FINSH
  994. static int list_channel(void)
  995. {
  996. rt_base_t level;
  997. rt_channel_t *channels;
  998. rt_ubase_t index, count;
  999. struct rt_object *object;
  1000. struct rt_list_node *node;
  1001. struct rt_object_information *information;
  1002. const char* stat_strs[] = {"idle", "wait", "active"};
  1003. information = rt_object_get_information(RT_Object_Class_Channel);
  1004. RT_ASSERT(information != RT_NULL);
  1005. count = 0;
  1006. level = rt_hw_interrupt_disable();
  1007. /* get the count of IPC channels */
  1008. for (node = information->object_list.next;
  1009. node != &(information->object_list);
  1010. node = node->next)
  1011. {
  1012. count ++;
  1013. }
  1014. rt_hw_interrupt_enable(level);
  1015. if (count == 0) return 0;
  1016. channels = (rt_channel_t *) rt_calloc(count, sizeof(rt_channel_t));
  1017. if (channels == RT_NULL) return 0; /* out of memory */
  1018. index = 0;
  1019. level = rt_hw_interrupt_disable();
  1020. /* retrieve pointer of IPC channels */
  1021. for (node = information->object_list.next;
  1022. node != &(information->object_list);
  1023. node = node->next)
  1024. {
  1025. object = rt_list_entry(node, struct rt_object, list);
  1026. channels[index] = (rt_channel_t)object;
  1027. index ++;
  1028. }
  1029. rt_hw_interrupt_enable(level);
  1030. rt_kprintf(" channel state\n");
  1031. rt_kprintf("-------- -------\n");
  1032. for (index = 0; index < count; index ++)
  1033. {
  1034. if (channels[index] != RT_NULL)
  1035. {
  1036. rt_kprintf("%-*.s", RT_NAME_MAX, channels[index]->parent.parent.name);
  1037. if (channels[index]->stat < 3)
  1038. rt_kprintf(" %s\n", stat_strs[channels[index]->stat]);
  1039. }
  1040. }
  1041. rt_free(channels);
  1042. return 0;
  1043. }
  1044. MSH_CMD_EXPORT(list_channel, list IPC channel information);
  1045. #endif