ircomm_event.c 6.8 KB

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