ircomm_event.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*********************************************************************
  2. *
  3. * Filename: ircomm_event.c
  4. * Version: 1.0
  5. * Description: IrCOMM layer state machine
  6. * Status: Stable
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Jun 6 20:33:11 1999
  9. * Modified at: Sun Dec 12 13:44:32 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. *
  29. ********************************************************************/
  30. #include <linux/sched.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/init.h>
  33. #include <net/irda/irda.h>
  34. #include <net/irda/irlmp.h>
  35. #include <net/irda/iriap.h>
  36. #include <net/irda/irttp.h>
  37. #include <net/irda/irias_object.h>
  38. #include <net/irda/ircomm_core.h>
  39. #include <net/irda/ircomm_event.h>
  40. static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event,
  41. struct sk_buff *skb, struct ircomm_info *info);
  42. static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event,
  43. struct sk_buff *skb, struct ircomm_info *info);
  44. static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event,
  45. struct sk_buff *skb, struct ircomm_info *info);
  46. static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event,
  47. struct sk_buff *skb, struct ircomm_info *info);
  48. char *ircomm_state[] = {
  49. "IRCOMM_IDLE",
  50. "IRCOMM_WAITI",
  51. "IRCOMM_WAITR",
  52. "IRCOMM_CONN",
  53. };
  54. #ifdef CONFIG_IRDA_DEBUG
  55. static char *ircomm_event[] = {
  56. "IRCOMM_CONNECT_REQUEST",
  57. "IRCOMM_CONNECT_RESPONSE",
  58. "IRCOMM_TTP_CONNECT_INDICATION",
  59. "IRCOMM_LMP_CONNECT_INDICATION",
  60. "IRCOMM_TTP_CONNECT_CONFIRM",
  61. "IRCOMM_LMP_CONNECT_CONFIRM",
  62. "IRCOMM_LMP_DISCONNECT_INDICATION",
  63. "IRCOMM_TTP_DISCONNECT_INDICATION",
  64. "IRCOMM_DISCONNECT_REQUEST",
  65. "IRCOMM_TTP_DATA_INDICATION",
  66. "IRCOMM_LMP_DATA_INDICATION",
  67. "IRCOMM_DATA_REQUEST",
  68. "IRCOMM_CONTROL_REQUEST",
  69. "IRCOMM_CONTROL_INDICATION",
  70. };
  71. #endif /* CONFIG_IRDA_DEBUG */
  72. static int (*state[])(struct ircomm_cb *self, IRCOMM_EVENT event,
  73. struct sk_buff *skb, struct ircomm_info *info) =
  74. {
  75. ircomm_state_idle,
  76. ircomm_state_waiti,
  77. ircomm_state_waitr,
  78. ircomm_state_conn,
  79. };
  80. /*
  81. * Function ircomm_state_idle (self, event, skb)
  82. *
  83. * IrCOMM is currently idle
  84. *
  85. */
  86. static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event,
  87. struct sk_buff *skb, struct ircomm_info *info)
  88. {
  89. int ret = 0;
  90. switch (event) {
  91. case IRCOMM_CONNECT_REQUEST:
  92. ircomm_next_state(self, IRCOMM_WAITI);
  93. ret = self->issue.connect_request(self, skb, info);
  94. break;
  95. case IRCOMM_TTP_CONNECT_INDICATION:
  96. case IRCOMM_LMP_CONNECT_INDICATION:
  97. ircomm_next_state(self, IRCOMM_WAITR);
  98. ircomm_connect_indication(self, skb, info);
  99. break;
  100. default:
  101. IRDA_DEBUG(4, "%s(), unknown event: %s\n", __FUNCTION__ ,
  102. ircomm_event[event]);
  103. ret = -EINVAL;
  104. }
  105. return ret;
  106. }
  107. /*
  108. * Function ircomm_state_waiti (self, event, skb)
  109. *
  110. * The IrCOMM user has requested an IrCOMM connection to the remote
  111. * device and is awaiting confirmation
  112. */
  113. static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event,
  114. struct sk_buff *skb, struct ircomm_info *info)
  115. {
  116. int ret = 0;
  117. switch (event) {
  118. case IRCOMM_TTP_CONNECT_CONFIRM:
  119. case IRCOMM_LMP_CONNECT_CONFIRM:
  120. ircomm_next_state(self, IRCOMM_CONN);
  121. ircomm_connect_confirm(self, skb, info);
  122. break;
  123. case IRCOMM_TTP_DISCONNECT_INDICATION:
  124. case IRCOMM_LMP_DISCONNECT_INDICATION:
  125. ircomm_next_state(self, IRCOMM_IDLE);
  126. ircomm_disconnect_indication(self, skb, info);
  127. break;
  128. default:
  129. IRDA_DEBUG(0, "%s(), unknown event: %s\n", __FUNCTION__ ,
  130. ircomm_event[event]);
  131. ret = -EINVAL;
  132. }
  133. return ret;
  134. }
  135. /*
  136. * Function ircomm_state_waitr (self, event, skb)
  137. *
  138. * IrCOMM has received an incoming connection request and is awaiting
  139. * response from the user
  140. */
  141. static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event,
  142. struct sk_buff *skb, struct ircomm_info *info)
  143. {
  144. int ret = 0;
  145. switch (event) {
  146. case IRCOMM_CONNECT_RESPONSE:
  147. ircomm_next_state(self, IRCOMM_CONN);
  148. ret = self->issue.connect_response(self, skb);
  149. break;
  150. case IRCOMM_DISCONNECT_REQUEST:
  151. ircomm_next_state(self, IRCOMM_IDLE);
  152. ret = self->issue.disconnect_request(self, skb, info);
  153. break;
  154. case IRCOMM_TTP_DISCONNECT_INDICATION:
  155. case IRCOMM_LMP_DISCONNECT_INDICATION:
  156. ircomm_next_state(self, IRCOMM_IDLE);
  157. ircomm_disconnect_indication(self, skb, info);
  158. break;
  159. default:
  160. IRDA_DEBUG(0, "%s(), unknown event = %s\n", __FUNCTION__ ,
  161. ircomm_event[event]);
  162. ret = -EINVAL;
  163. }
  164. return ret;
  165. }
  166. /*
  167. * Function ircomm_state_conn (self, event, skb)
  168. *
  169. * IrCOMM is connected to the peer IrCOMM device
  170. *
  171. */
  172. static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event,
  173. struct sk_buff *skb, struct ircomm_info *info)
  174. {
  175. int ret = 0;
  176. switch (event) {
  177. case IRCOMM_DATA_REQUEST:
  178. ret = self->issue.data_request(self, skb, 0);
  179. break;
  180. case IRCOMM_TTP_DATA_INDICATION:
  181. ircomm_process_data(self, skb);
  182. break;
  183. case IRCOMM_LMP_DATA_INDICATION:
  184. ircomm_data_indication(self, skb);
  185. break;
  186. case IRCOMM_CONTROL_REQUEST:
  187. /* Just send a separate frame for now */
  188. ret = self->issue.data_request(self, skb, skb->len);
  189. break;
  190. case IRCOMM_TTP_DISCONNECT_INDICATION:
  191. case IRCOMM_LMP_DISCONNECT_INDICATION:
  192. ircomm_next_state(self, IRCOMM_IDLE);
  193. ircomm_disconnect_indication(self, skb, info);
  194. break;
  195. case IRCOMM_DISCONNECT_REQUEST:
  196. ircomm_next_state(self, IRCOMM_IDLE);
  197. ret = self->issue.disconnect_request(self, skb, info);
  198. break;
  199. default:
  200. IRDA_DEBUG(0, "%s(), unknown event = %s\n", __FUNCTION__ ,
  201. ircomm_event[event]);
  202. ret = -EINVAL;
  203. }
  204. return ret;
  205. }
  206. /*
  207. * Function ircomm_do_event (self, event, skb)
  208. *
  209. * Process event
  210. *
  211. */
  212. int ircomm_do_event(struct ircomm_cb *self, IRCOMM_EVENT event,
  213. struct sk_buff *skb, struct ircomm_info *info)
  214. {
  215. IRDA_DEBUG(4, "%s: state=%s, event=%s\n", __FUNCTION__ ,
  216. ircomm_state[self->state], ircomm_event[event]);
  217. return (*state[self->state])(self, event, skb, info);
  218. }
  219. /*
  220. * Function ircomm_next_state (self, state)
  221. *
  222. * Switch state
  223. *
  224. */
  225. void ircomm_next_state(struct ircomm_cb *self, IRCOMM_STATE state)
  226. {
  227. self->state = state;
  228. IRDA_DEBUG(4, "%s: next state=%s, service type=%d\n", __FUNCTION__ ,
  229. ircomm_state[self->state], self->service_type);
  230. }