ircomm_event.c 6.9 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/proc_fs.h>
  31. #include <linux/init.h>
  32. #include <net/irda/irda.h>
  33. #include <net/irda/irlmp.h>
  34. #include <net/irda/iriap.h>
  35. #include <net/irda/irttp.h>
  36. #include <net/irda/irias_object.h>
  37. #include <net/irda/ircomm_core.h>
  38. #include <net/irda/ircomm_event.h>
  39. static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event,
  40. struct sk_buff *skb, struct ircomm_info *info);
  41. static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event,
  42. struct sk_buff *skb, struct ircomm_info *info);
  43. static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event,
  44. struct sk_buff *skb, struct ircomm_info *info);
  45. static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event,
  46. struct sk_buff *skb, struct ircomm_info *info);
  47. const char *const ircomm_state[] = {
  48. "IRCOMM_IDLE",
  49. "IRCOMM_WAITI",
  50. "IRCOMM_WAITR",
  51. "IRCOMM_CONN",
  52. };
  53. #ifdef CONFIG_IRDA_DEBUG
  54. static const char *const ircomm_event[] = {
  55. "IRCOMM_CONNECT_REQUEST",
  56. "IRCOMM_CONNECT_RESPONSE",
  57. "IRCOMM_TTP_CONNECT_INDICATION",
  58. "IRCOMM_LMP_CONNECT_INDICATION",
  59. "IRCOMM_TTP_CONNECT_CONFIRM",
  60. "IRCOMM_LMP_CONNECT_CONFIRM",
  61. "IRCOMM_LMP_DISCONNECT_INDICATION",
  62. "IRCOMM_TTP_DISCONNECT_INDICATION",
  63. "IRCOMM_DISCONNECT_REQUEST",
  64. "IRCOMM_TTP_DATA_INDICATION",
  65. "IRCOMM_LMP_DATA_INDICATION",
  66. "IRCOMM_DATA_REQUEST",
  67. "IRCOMM_CONTROL_REQUEST",
  68. "IRCOMM_CONTROL_INDICATION",
  69. };
  70. #endif /* CONFIG_IRDA_DEBUG */
  71. static int (*state[])(struct ircomm_cb *self, IRCOMM_EVENT event,
  72. struct sk_buff *skb, struct ircomm_info *info) =
  73. {
  74. ircomm_state_idle,
  75. ircomm_state_waiti,
  76. ircomm_state_waitr,
  77. ircomm_state_conn,
  78. };
  79. /*
  80. * Function ircomm_state_idle (self, event, skb)
  81. *
  82. * IrCOMM is currently idle
  83. *
  84. */
  85. static int ircomm_state_idle(struct ircomm_cb *self, IRCOMM_EVENT event,
  86. struct sk_buff *skb, struct ircomm_info *info)
  87. {
  88. int ret = 0;
  89. switch (event) {
  90. case IRCOMM_CONNECT_REQUEST:
  91. ircomm_next_state(self, IRCOMM_WAITI);
  92. ret = self->issue.connect_request(self, skb, info);
  93. break;
  94. case IRCOMM_TTP_CONNECT_INDICATION:
  95. case IRCOMM_LMP_CONNECT_INDICATION:
  96. ircomm_next_state(self, IRCOMM_WAITR);
  97. ircomm_connect_indication(self, skb, info);
  98. break;
  99. default:
  100. IRDA_DEBUG(4, "%s(), unknown event: %s\n", __func__ ,
  101. ircomm_event[event]);
  102. ret = -EINVAL;
  103. }
  104. return ret;
  105. }
  106. /*
  107. * Function ircomm_state_waiti (self, event, skb)
  108. *
  109. * The IrCOMM user has requested an IrCOMM connection to the remote
  110. * device and is awaiting confirmation
  111. */
  112. static int ircomm_state_waiti(struct ircomm_cb *self, IRCOMM_EVENT event,
  113. struct sk_buff *skb, struct ircomm_info *info)
  114. {
  115. int ret = 0;
  116. switch (event) {
  117. case IRCOMM_TTP_CONNECT_CONFIRM:
  118. case IRCOMM_LMP_CONNECT_CONFIRM:
  119. ircomm_next_state(self, IRCOMM_CONN);
  120. ircomm_connect_confirm(self, skb, info);
  121. break;
  122. case IRCOMM_TTP_DISCONNECT_INDICATION:
  123. case IRCOMM_LMP_DISCONNECT_INDICATION:
  124. ircomm_next_state(self, IRCOMM_IDLE);
  125. ircomm_disconnect_indication(self, skb, info);
  126. break;
  127. default:
  128. IRDA_DEBUG(0, "%s(), unknown event: %s\n", __func__ ,
  129. ircomm_event[event]);
  130. ret = -EINVAL;
  131. }
  132. return ret;
  133. }
  134. /*
  135. * Function ircomm_state_waitr (self, event, skb)
  136. *
  137. * IrCOMM has received an incoming connection request and is awaiting
  138. * response from the user
  139. */
  140. static int ircomm_state_waitr(struct ircomm_cb *self, IRCOMM_EVENT event,
  141. struct sk_buff *skb, struct ircomm_info *info)
  142. {
  143. int ret = 0;
  144. switch (event) {
  145. case IRCOMM_CONNECT_RESPONSE:
  146. ircomm_next_state(self, IRCOMM_CONN);
  147. ret = self->issue.connect_response(self, skb);
  148. break;
  149. case IRCOMM_DISCONNECT_REQUEST:
  150. ircomm_next_state(self, IRCOMM_IDLE);
  151. ret = self->issue.disconnect_request(self, skb, info);
  152. break;
  153. case IRCOMM_TTP_DISCONNECT_INDICATION:
  154. case IRCOMM_LMP_DISCONNECT_INDICATION:
  155. ircomm_next_state(self, IRCOMM_IDLE);
  156. ircomm_disconnect_indication(self, skb, info);
  157. break;
  158. default:
  159. IRDA_DEBUG(0, "%s(), unknown event = %s\n", __func__ ,
  160. ircomm_event[event]);
  161. ret = -EINVAL;
  162. }
  163. return ret;
  164. }
  165. /*
  166. * Function ircomm_state_conn (self, event, skb)
  167. *
  168. * IrCOMM is connected to the peer IrCOMM device
  169. *
  170. */
  171. static int ircomm_state_conn(struct ircomm_cb *self, IRCOMM_EVENT event,
  172. struct sk_buff *skb, struct ircomm_info *info)
  173. {
  174. int ret = 0;
  175. switch (event) {
  176. case IRCOMM_DATA_REQUEST:
  177. ret = self->issue.data_request(self, skb, 0);
  178. break;
  179. case IRCOMM_TTP_DATA_INDICATION:
  180. ircomm_process_data(self, skb);
  181. break;
  182. case IRCOMM_LMP_DATA_INDICATION:
  183. ircomm_data_indication(self, skb);
  184. break;
  185. case IRCOMM_CONTROL_REQUEST:
  186. /* Just send a separate frame for now */
  187. ret = self->issue.data_request(self, skb, skb->len);
  188. break;
  189. case IRCOMM_TTP_DISCONNECT_INDICATION:
  190. case IRCOMM_LMP_DISCONNECT_INDICATION:
  191. ircomm_next_state(self, IRCOMM_IDLE);
  192. ircomm_disconnect_indication(self, skb, info);
  193. break;
  194. case IRCOMM_DISCONNECT_REQUEST:
  195. ircomm_next_state(self, IRCOMM_IDLE);
  196. ret = self->issue.disconnect_request(self, skb, info);
  197. break;
  198. default:
  199. IRDA_DEBUG(0, "%s(), unknown event = %s\n", __func__ ,
  200. ircomm_event[event]);
  201. ret = -EINVAL;
  202. }
  203. return ret;
  204. }
  205. /*
  206. * Function ircomm_do_event (self, event, skb)
  207. *
  208. * Process event
  209. *
  210. */
  211. int ircomm_do_event(struct ircomm_cb *self, IRCOMM_EVENT event,
  212. struct sk_buff *skb, struct ircomm_info *info)
  213. {
  214. IRDA_DEBUG(4, "%s: state=%s, event=%s\n", __func__ ,
  215. ircomm_state[self->state], ircomm_event[event]);
  216. return (*state[self->state])(self, event, skb, info);
  217. }
  218. /*
  219. * Function ircomm_next_state (self, state)
  220. *
  221. * Switch state
  222. *
  223. */
  224. void ircomm_next_state(struct ircomm_cb *self, IRCOMM_STATE state)
  225. {
  226. self->state = state;
  227. IRDA_DEBUG(4, "%s: next state=%s, service type=%d\n", __func__ ,
  228. ircomm_state[self->state], self->service_type);
  229. }