ircomm_event.c 6.8 KB

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