irlan_client_event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*********************************************************************
  2. *
  3. * Filename: irlan_client_event.c
  4. * Version: 0.9
  5. * Description: IrLAN client state machine
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Aug 31 20:14:37 1997
  9. * Modified at: Sun Dec 26 21:52:24 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
  13. * All Rights Reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * Neither Dag Brattli nor University of Tromsø admit liability nor
  21. * provide warranty for any of this software. This material is
  22. * provided "AS-IS" and at no charge.
  23. *
  24. ********************************************************************/
  25. #include <linux/skbuff.h>
  26. #include <net/irda/irda.h>
  27. #include <net/irda/timer.h>
  28. #include <net/irda/irmod.h>
  29. #include <net/irda/iriap.h>
  30. #include <net/irda/irlmp.h>
  31. #include <net/irda/irttp.h>
  32. #include <net/irda/irlan_common.h>
  33. #include <net/irda/irlan_client.h>
  34. #include <net/irda/irlan_event.h>
  35. static int irlan_client_state_idle (struct irlan_cb *self, IRLAN_EVENT event,
  36. struct sk_buff *skb);
  37. static int irlan_client_state_query(struct irlan_cb *self, IRLAN_EVENT event,
  38. struct sk_buff *skb);
  39. static int irlan_client_state_conn (struct irlan_cb *self, IRLAN_EVENT event,
  40. struct sk_buff *skb);
  41. static int irlan_client_state_info (struct irlan_cb *self, IRLAN_EVENT event,
  42. struct sk_buff *skb);
  43. static int irlan_client_state_media(struct irlan_cb *self, IRLAN_EVENT event,
  44. struct sk_buff *skb);
  45. static int irlan_client_state_open (struct irlan_cb *self, IRLAN_EVENT event,
  46. struct sk_buff *skb);
  47. static int irlan_client_state_wait (struct irlan_cb *self, IRLAN_EVENT event,
  48. struct sk_buff *skb);
  49. static int irlan_client_state_arb (struct irlan_cb *self, IRLAN_EVENT event,
  50. struct sk_buff *skb);
  51. static int irlan_client_state_data (struct irlan_cb *self, IRLAN_EVENT event,
  52. struct sk_buff *skb);
  53. static int irlan_client_state_close(struct irlan_cb *self, IRLAN_EVENT event,
  54. struct sk_buff *skb);
  55. static int irlan_client_state_sync (struct irlan_cb *self, IRLAN_EVENT event,
  56. struct sk_buff *skb);
  57. static int (*state[])(struct irlan_cb *, IRLAN_EVENT event, struct sk_buff *) =
  58. {
  59. irlan_client_state_idle,
  60. irlan_client_state_query,
  61. irlan_client_state_conn,
  62. irlan_client_state_info,
  63. irlan_client_state_media,
  64. irlan_client_state_open,
  65. irlan_client_state_wait,
  66. irlan_client_state_arb,
  67. irlan_client_state_data,
  68. irlan_client_state_close,
  69. irlan_client_state_sync
  70. };
  71. void irlan_do_client_event(struct irlan_cb *self, IRLAN_EVENT event,
  72. struct sk_buff *skb)
  73. {
  74. IRDA_ASSERT(self != NULL, return;);
  75. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  76. (*state[ self->client.state]) (self, event, skb);
  77. }
  78. /*
  79. * Function irlan_client_state_idle (event, skb, info)
  80. *
  81. * IDLE, We are waiting for an indication that there is a provider
  82. * available.
  83. */
  84. static int irlan_client_state_idle(struct irlan_cb *self, IRLAN_EVENT event,
  85. struct sk_buff *skb)
  86. {
  87. IRDA_ASSERT(self != NULL, return -1;);
  88. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
  89. switch (event) {
  90. case IRLAN_DISCOVERY_INDICATION:
  91. if (self->client.iriap) {
  92. net_warn_ratelimited("%s(), busy with a previous query\n",
  93. __func__);
  94. return -EBUSY;
  95. }
  96. self->client.iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
  97. irlan_client_get_value_confirm);
  98. /* Get some values from peer IAS */
  99. irlan_next_client_state(self, IRLAN_QUERY);
  100. iriap_getvaluebyclass_request(self->client.iriap,
  101. self->saddr, self->daddr,
  102. "IrLAN", "IrDA:TinyTP:LsapSel");
  103. break;
  104. case IRLAN_WATCHDOG_TIMEOUT:
  105. pr_debug("%s(), IRLAN_WATCHDOG_TIMEOUT\n", __func__);
  106. break;
  107. default:
  108. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  109. break;
  110. }
  111. if (skb)
  112. dev_kfree_skb(skb);
  113. return 0;
  114. }
  115. /*
  116. * Function irlan_client_state_query (event, skb, info)
  117. *
  118. * QUERY, We have queryed the remote IAS and is ready to connect
  119. * to provider, just waiting for the confirm.
  120. *
  121. */
  122. static int irlan_client_state_query(struct irlan_cb *self, IRLAN_EVENT event,
  123. struct sk_buff *skb)
  124. {
  125. IRDA_ASSERT(self != NULL, return -1;);
  126. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
  127. switch(event) {
  128. case IRLAN_IAS_PROVIDER_AVAIL:
  129. IRDA_ASSERT(self->dtsap_sel_ctrl != 0, return -1;);
  130. self->client.open_retries = 0;
  131. irttp_connect_request(self->client.tsap_ctrl,
  132. self->dtsap_sel_ctrl,
  133. self->saddr, self->daddr, NULL,
  134. IRLAN_MTU, NULL);
  135. irlan_next_client_state(self, IRLAN_CONN);
  136. break;
  137. case IRLAN_IAS_PROVIDER_NOT_AVAIL:
  138. pr_debug("%s(), IAS_PROVIDER_NOT_AVAIL\n", __func__);
  139. irlan_next_client_state(self, IRLAN_IDLE);
  140. /* Give the client a kick! */
  141. if ((self->provider.access_type == ACCESS_PEER) &&
  142. (self->provider.state != IRLAN_IDLE))
  143. irlan_client_wakeup(self, self->saddr, self->daddr);
  144. break;
  145. case IRLAN_LMP_DISCONNECT:
  146. case IRLAN_LAP_DISCONNECT:
  147. irlan_next_client_state(self, IRLAN_IDLE);
  148. break;
  149. case IRLAN_WATCHDOG_TIMEOUT:
  150. pr_debug("%s(), IRLAN_WATCHDOG_TIMEOUT\n", __func__);
  151. break;
  152. default:
  153. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  154. break;
  155. }
  156. if (skb)
  157. dev_kfree_skb(skb);
  158. return 0;
  159. }
  160. /*
  161. * Function irlan_client_state_conn (event, skb, info)
  162. *
  163. * CONN, We have connected to a provider but has not issued any
  164. * commands yet.
  165. *
  166. */
  167. static int irlan_client_state_conn(struct irlan_cb *self, IRLAN_EVENT event,
  168. struct sk_buff *skb)
  169. {
  170. IRDA_ASSERT(self != NULL, return -1;);
  171. switch (event) {
  172. case IRLAN_CONNECT_COMPLETE:
  173. /* Send getinfo cmd */
  174. irlan_get_provider_info(self);
  175. irlan_next_client_state(self, IRLAN_INFO);
  176. break;
  177. case IRLAN_LMP_DISCONNECT:
  178. case IRLAN_LAP_DISCONNECT:
  179. irlan_next_client_state(self, IRLAN_IDLE);
  180. break;
  181. case IRLAN_WATCHDOG_TIMEOUT:
  182. pr_debug("%s(), IRLAN_WATCHDOG_TIMEOUT\n", __func__);
  183. break;
  184. default:
  185. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  186. break;
  187. }
  188. if (skb)
  189. dev_kfree_skb(skb);
  190. return 0;
  191. }
  192. /*
  193. * Function irlan_client_state_info (self, event, skb, info)
  194. *
  195. * INFO, We have issued a GetInfo command and is awaiting a reply.
  196. */
  197. static int irlan_client_state_info(struct irlan_cb *self, IRLAN_EVENT event,
  198. struct sk_buff *skb)
  199. {
  200. IRDA_ASSERT(self != NULL, return -1;);
  201. switch (event) {
  202. case IRLAN_DATA_INDICATION:
  203. IRDA_ASSERT(skb != NULL, return -1;);
  204. irlan_client_parse_response(self, skb);
  205. irlan_next_client_state(self, IRLAN_MEDIA);
  206. irlan_get_media_char(self);
  207. break;
  208. case IRLAN_LMP_DISCONNECT:
  209. case IRLAN_LAP_DISCONNECT:
  210. irlan_next_client_state(self, IRLAN_IDLE);
  211. break;
  212. case IRLAN_WATCHDOG_TIMEOUT:
  213. pr_debug("%s(), IRLAN_WATCHDOG_TIMEOUT\n", __func__);
  214. break;
  215. default:
  216. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  217. break;
  218. }
  219. if (skb)
  220. dev_kfree_skb(skb);
  221. return 0;
  222. }
  223. /*
  224. * Function irlan_client_state_media (self, event, skb, info)
  225. *
  226. * MEDIA, The irlan_client has issued a GetMedia command and is awaiting a
  227. * reply.
  228. *
  229. */
  230. static int irlan_client_state_media(struct irlan_cb *self, IRLAN_EVENT event,
  231. struct sk_buff *skb)
  232. {
  233. IRDA_ASSERT(self != NULL, return -1;);
  234. switch(event) {
  235. case IRLAN_DATA_INDICATION:
  236. irlan_client_parse_response(self, skb);
  237. irlan_open_data_channel(self);
  238. irlan_next_client_state(self, IRLAN_OPEN);
  239. break;
  240. case IRLAN_LMP_DISCONNECT:
  241. case IRLAN_LAP_DISCONNECT:
  242. irlan_next_client_state(self, IRLAN_IDLE);
  243. break;
  244. case IRLAN_WATCHDOG_TIMEOUT:
  245. pr_debug("%s(), IRLAN_WATCHDOG_TIMEOUT\n", __func__);
  246. break;
  247. default:
  248. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  249. break;
  250. }
  251. if (skb)
  252. dev_kfree_skb(skb);
  253. return 0;
  254. }
  255. /*
  256. * Function irlan_client_state_open (self, event, skb, info)
  257. *
  258. * OPEN, The irlan_client has issued a OpenData command and is awaiting a
  259. * reply
  260. *
  261. */
  262. static int irlan_client_state_open(struct irlan_cb *self, IRLAN_EVENT event,
  263. struct sk_buff *skb)
  264. {
  265. struct qos_info qos;
  266. IRDA_ASSERT(self != NULL, return -1;);
  267. switch(event) {
  268. case IRLAN_DATA_INDICATION:
  269. irlan_client_parse_response(self, skb);
  270. /*
  271. * Check if we have got the remote TSAP for data
  272. * communications
  273. */
  274. IRDA_ASSERT(self->dtsap_sel_data != 0, return -1;);
  275. /* Check which access type we are dealing with */
  276. switch (self->client.access_type) {
  277. case ACCESS_PEER:
  278. if (self->provider.state == IRLAN_OPEN) {
  279. irlan_next_client_state(self, IRLAN_ARB);
  280. irlan_do_client_event(self, IRLAN_CHECK_CON_ARB,
  281. NULL);
  282. } else {
  283. irlan_next_client_state(self, IRLAN_WAIT);
  284. }
  285. break;
  286. case ACCESS_DIRECT:
  287. case ACCESS_HOSTED:
  288. qos.link_disc_time.bits = 0x01; /* 3 secs */
  289. irttp_connect_request(self->tsap_data,
  290. self->dtsap_sel_data,
  291. self->saddr, self->daddr, &qos,
  292. IRLAN_MTU, NULL);
  293. irlan_next_client_state(self, IRLAN_DATA);
  294. break;
  295. default:
  296. pr_debug("%s(), unknown access type!\n", __func__);
  297. break;
  298. }
  299. break;
  300. case IRLAN_LMP_DISCONNECT:
  301. case IRLAN_LAP_DISCONNECT:
  302. irlan_next_client_state(self, IRLAN_IDLE);
  303. break;
  304. case IRLAN_WATCHDOG_TIMEOUT:
  305. pr_debug("%s(), IRLAN_WATCHDOG_TIMEOUT\n", __func__);
  306. break;
  307. default:
  308. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  309. break;
  310. }
  311. if (skb)
  312. dev_kfree_skb(skb);
  313. return 0;
  314. }
  315. /*
  316. * Function irlan_client_state_wait (self, event, skb, info)
  317. *
  318. * WAIT, The irlan_client is waiting for the local provider to enter the
  319. * provider OPEN state.
  320. *
  321. */
  322. static int irlan_client_state_wait(struct irlan_cb *self, IRLAN_EVENT event,
  323. struct sk_buff *skb)
  324. {
  325. IRDA_ASSERT(self != NULL, return -1;);
  326. switch(event) {
  327. case IRLAN_PROVIDER_SIGNAL:
  328. irlan_next_client_state(self, IRLAN_ARB);
  329. irlan_do_client_event(self, IRLAN_CHECK_CON_ARB, NULL);
  330. break;
  331. case IRLAN_LMP_DISCONNECT:
  332. case IRLAN_LAP_DISCONNECT:
  333. irlan_next_client_state(self, IRLAN_IDLE);
  334. break;
  335. case IRLAN_WATCHDOG_TIMEOUT:
  336. pr_debug("%s(), IRLAN_WATCHDOG_TIMEOUT\n", __func__);
  337. break;
  338. default:
  339. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  340. break;
  341. }
  342. if (skb)
  343. dev_kfree_skb(skb);
  344. return 0;
  345. }
  346. static int irlan_client_state_arb(struct irlan_cb *self, IRLAN_EVENT event,
  347. struct sk_buff *skb)
  348. {
  349. struct qos_info qos;
  350. IRDA_ASSERT(self != NULL, return -1;);
  351. switch(event) {
  352. case IRLAN_CHECK_CON_ARB:
  353. if (self->client.recv_arb_val == self->provider.send_arb_val) {
  354. irlan_next_client_state(self, IRLAN_CLOSE);
  355. irlan_close_data_channel(self);
  356. } else if (self->client.recv_arb_val <
  357. self->provider.send_arb_val)
  358. {
  359. qos.link_disc_time.bits = 0x01; /* 3 secs */
  360. irlan_next_client_state(self, IRLAN_DATA);
  361. irttp_connect_request(self->tsap_data,
  362. self->dtsap_sel_data,
  363. self->saddr, self->daddr, &qos,
  364. IRLAN_MTU, NULL);
  365. } else if (self->client.recv_arb_val >
  366. self->provider.send_arb_val)
  367. {
  368. pr_debug("%s(), lost the battle :-(\n", __func__);
  369. }
  370. break;
  371. case IRLAN_DATA_CONNECT_INDICATION:
  372. irlan_next_client_state(self, IRLAN_DATA);
  373. break;
  374. case IRLAN_LMP_DISCONNECT:
  375. case IRLAN_LAP_DISCONNECT:
  376. irlan_next_client_state(self, IRLAN_IDLE);
  377. break;
  378. case IRLAN_WATCHDOG_TIMEOUT:
  379. pr_debug("%s(), IRLAN_WATCHDOG_TIMEOUT\n", __func__);
  380. break;
  381. default:
  382. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  383. break;
  384. }
  385. if (skb)
  386. dev_kfree_skb(skb);
  387. return 0;
  388. }
  389. /*
  390. * Function irlan_client_state_data (self, event, skb, info)
  391. *
  392. * DATA, The data channel is connected, allowing data transfers between
  393. * the local and remote machines.
  394. *
  395. */
  396. static int irlan_client_state_data(struct irlan_cb *self, IRLAN_EVENT event,
  397. struct sk_buff *skb)
  398. {
  399. IRDA_ASSERT(self != NULL, return -1;);
  400. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
  401. switch(event) {
  402. case IRLAN_DATA_INDICATION:
  403. irlan_client_parse_response(self, skb);
  404. break;
  405. case IRLAN_LMP_DISCONNECT: /* FALLTHROUGH */
  406. case IRLAN_LAP_DISCONNECT:
  407. irlan_next_client_state(self, IRLAN_IDLE);
  408. break;
  409. default:
  410. pr_debug("%s(), Unknown event %d\n", __func__ , event);
  411. break;
  412. }
  413. if (skb)
  414. dev_kfree_skb(skb);
  415. return 0;
  416. }
  417. /*
  418. * Function irlan_client_state_close (self, event, skb, info)
  419. *
  420. *
  421. *
  422. */
  423. static int irlan_client_state_close(struct irlan_cb *self, IRLAN_EVENT event,
  424. struct sk_buff *skb)
  425. {
  426. if (skb)
  427. dev_kfree_skb(skb);
  428. return 0;
  429. }
  430. /*
  431. * Function irlan_client_state_sync (self, event, skb, info)
  432. *
  433. *
  434. *
  435. */
  436. static int irlan_client_state_sync(struct irlan_cb *self, IRLAN_EVENT event,
  437. struct sk_buff *skb)
  438. {
  439. if (skb)
  440. dev_kfree_skb(skb);
  441. return 0;
  442. }