1 /*********************************************************************
3 * Filename: irlan_client_event.c
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>
12 * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
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.
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.
24 ********************************************************************/
26 #include <linux/skbuff.h>
28 #include <net/irda/irda.h>
29 #include <net/irda/timer.h>
30 #include <net/irda/irmod.h>
31 #include <net/irda/iriap.h>
32 #include <net/irda/irlmp.h>
33 #include <net/irda/irttp.h>
35 #include <net/irda/irlan_common.h>
36 #include <net/irda/irlan_client.h>
37 #include <net/irda/irlan_event.h>
39 static int irlan_client_state_idle (struct irlan_cb *self, IRLAN_EVENT event,
41 static int irlan_client_state_query(struct irlan_cb *self, IRLAN_EVENT event,
43 static int irlan_client_state_conn (struct irlan_cb *self, IRLAN_EVENT event,
45 static int irlan_client_state_info (struct irlan_cb *self, IRLAN_EVENT event,
47 static int irlan_client_state_media(struct irlan_cb *self, IRLAN_EVENT event,
49 static int irlan_client_state_open (struct irlan_cb *self, IRLAN_EVENT event,
51 static int irlan_client_state_wait (struct irlan_cb *self, IRLAN_EVENT event,
53 static int irlan_client_state_arb (struct irlan_cb *self, IRLAN_EVENT event,
55 static int irlan_client_state_data (struct irlan_cb *self, IRLAN_EVENT event,
57 static int irlan_client_state_close(struct irlan_cb *self, IRLAN_EVENT event,
59 static int irlan_client_state_sync (struct irlan_cb *self, IRLAN_EVENT event,
62 static int (*state[])(struct irlan_cb *, IRLAN_EVENT event, struct sk_buff *) =
64 irlan_client_state_idle,
65 irlan_client_state_query,
66 irlan_client_state_conn,
67 irlan_client_state_info,
68 irlan_client_state_media,
69 irlan_client_state_open,
70 irlan_client_state_wait,
71 irlan_client_state_arb,
72 irlan_client_state_data,
73 irlan_client_state_close,
74 irlan_client_state_sync
77 void irlan_do_client_event(struct irlan_cb *self, IRLAN_EVENT event,
80 ASSERT(self != NULL, return;);
81 ASSERT(self->magic == IRLAN_MAGIC, return;);
83 (*state[ self->client.state]) (self, event, skb);
87 * Function irlan_client_state_idle (event, skb, info)
89 * IDLE, We are waiting for an indication that there is a provider
92 static int irlan_client_state_idle(struct irlan_cb *self, IRLAN_EVENT event,
95 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
97 ASSERT(self != NULL, return -1;);
98 ASSERT(self->magic == IRLAN_MAGIC, return -1;);
101 case IRLAN_DISCOVERY_INDICATION:
102 if (self->client.iriap) {
103 WARNING("%s(), busy with a previous query\n", __FUNCTION__);
107 self->client.iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
108 irlan_client_get_value_confirm);
109 /* Get some values from peer IAS */
110 irlan_next_client_state(self, IRLAN_QUERY);
111 iriap_getvaluebyclass_request(self->client.iriap,
112 self->saddr, self->daddr,
113 "IrLAN", "IrDA:TinyTP:LsapSel");
115 case IRLAN_WATCHDOG_TIMEOUT:
116 IRDA_DEBUG(2, "%s(), IRLAN_WATCHDOG_TIMEOUT\n", __FUNCTION__ );
119 IRDA_DEBUG(4, "%s(), Unknown event %d\n", __FUNCTION__ , event);
129 * Function irlan_client_state_query (event, skb, info)
131 * QUERY, We have queryed the remote IAS and is ready to connect
132 * to provider, just waiting for the confirm.
135 static int irlan_client_state_query(struct irlan_cb *self, IRLAN_EVENT event,
138 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
140 ASSERT(self != NULL, return -1;);
141 ASSERT(self->magic == IRLAN_MAGIC, return -1;);
144 case IRLAN_IAS_PROVIDER_AVAIL:
145 ASSERT(self->dtsap_sel_ctrl != 0, return -1;);
147 self->client.open_retries = 0;
149 irttp_connect_request(self->client.tsap_ctrl,
150 self->dtsap_sel_ctrl,
151 self->saddr, self->daddr, NULL,
153 irlan_next_client_state(self, IRLAN_CONN);
155 case IRLAN_IAS_PROVIDER_NOT_AVAIL:
156 IRDA_DEBUG(2, "%s(), IAS_PROVIDER_NOT_AVAIL\n", __FUNCTION__ );
157 irlan_next_client_state(self, IRLAN_IDLE);
159 /* Give the client a kick! */
160 if ((self->provider.access_type == ACCESS_PEER) &&
161 (self->provider.state != IRLAN_IDLE))
162 irlan_client_wakeup(self, self->saddr, self->daddr);
164 case IRLAN_LMP_DISCONNECT:
165 case IRLAN_LAP_DISCONNECT:
166 irlan_next_client_state(self, IRLAN_IDLE);
168 case IRLAN_WATCHDOG_TIMEOUT:
169 IRDA_DEBUG(2, "%s(), IRLAN_WATCHDOG_TIMEOUT\n", __FUNCTION__ );
172 IRDA_DEBUG(2, "%s(), Unknown event %d\n", __FUNCTION__ , event);
182 * Function irlan_client_state_conn (event, skb, info)
184 * CONN, We have connected to a provider but has not issued any
188 static int irlan_client_state_conn(struct irlan_cb *self, IRLAN_EVENT event,
191 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
193 ASSERT(self != NULL, return -1;);
196 case IRLAN_CONNECT_COMPLETE:
197 /* Send getinfo cmd */
198 irlan_get_provider_info(self);
199 irlan_next_client_state(self, IRLAN_INFO);
201 case IRLAN_LMP_DISCONNECT:
202 case IRLAN_LAP_DISCONNECT:
203 irlan_next_client_state(self, IRLAN_IDLE);
205 case IRLAN_WATCHDOG_TIMEOUT:
206 IRDA_DEBUG(2, "%s(), IRLAN_WATCHDOG_TIMEOUT\n", __FUNCTION__ );
209 IRDA_DEBUG(2, "%s(), Unknown event %d\n", __FUNCTION__ , event);
219 * Function irlan_client_state_info (self, event, skb, info)
221 * INFO, We have issued a GetInfo command and is awaiting a reply.
223 static int irlan_client_state_info(struct irlan_cb *self, IRLAN_EVENT event,
226 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
228 ASSERT(self != NULL, return -1;);
231 case IRLAN_DATA_INDICATION:
232 ASSERT(skb != NULL, return -1;);
234 irlan_client_parse_response(self, skb);
236 irlan_next_client_state(self, IRLAN_MEDIA);
238 irlan_get_media_char(self);
241 case IRLAN_LMP_DISCONNECT:
242 case IRLAN_LAP_DISCONNECT:
243 irlan_next_client_state(self, IRLAN_IDLE);
245 case IRLAN_WATCHDOG_TIMEOUT:
246 IRDA_DEBUG(2, "%s(), IRLAN_WATCHDOG_TIMEOUT\n", __FUNCTION__ );
249 IRDA_DEBUG(2, "%s(), Unknown event %d\n", __FUNCTION__ , event);
259 * Function irlan_client_state_media (self, event, skb, info)
261 * MEDIA, The irlan_client has issued a GetMedia command and is awaiting a
265 static int irlan_client_state_media(struct irlan_cb *self, IRLAN_EVENT event,
268 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
270 ASSERT(self != NULL, return -1;);
273 case IRLAN_DATA_INDICATION:
274 irlan_client_parse_response(self, skb);
275 irlan_open_data_channel(self);
276 irlan_next_client_state(self, IRLAN_OPEN);
278 case IRLAN_LMP_DISCONNECT:
279 case IRLAN_LAP_DISCONNECT:
280 irlan_next_client_state(self, IRLAN_IDLE);
282 case IRLAN_WATCHDOG_TIMEOUT:
283 IRDA_DEBUG(2, "%s(), IRLAN_WATCHDOG_TIMEOUT\n", __FUNCTION__ );
286 IRDA_DEBUG(2, "%s(), Unknown event %d\n", __FUNCTION__ , event);
296 * Function irlan_client_state_open (self, event, skb, info)
298 * OPEN, The irlan_client has issued a OpenData command and is awaiting a
302 static int irlan_client_state_open(struct irlan_cb *self, IRLAN_EVENT event,
307 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
309 ASSERT(self != NULL, return -1;);
312 case IRLAN_DATA_INDICATION:
313 irlan_client_parse_response(self, skb);
316 * Check if we have got the remote TSAP for data
319 ASSERT(self->dtsap_sel_data != 0, return -1;);
321 /* Check which access type we are dealing with */
322 switch (self->client.access_type) {
324 if (self->provider.state == IRLAN_OPEN) {
326 irlan_next_client_state(self, IRLAN_ARB);
327 irlan_do_client_event(self, IRLAN_CHECK_CON_ARB,
331 irlan_next_client_state(self, IRLAN_WAIT);
336 qos.link_disc_time.bits = 0x01; /* 3 secs */
338 irttp_connect_request(self->tsap_data,
339 self->dtsap_sel_data,
340 self->saddr, self->daddr, &qos,
343 irlan_next_client_state(self, IRLAN_DATA);
346 IRDA_DEBUG(2, "%s(), unknown access type!\n", __FUNCTION__ );
350 case IRLAN_LMP_DISCONNECT:
351 case IRLAN_LAP_DISCONNECT:
352 irlan_next_client_state(self, IRLAN_IDLE);
354 case IRLAN_WATCHDOG_TIMEOUT:
355 IRDA_DEBUG(2, "%s(), IRLAN_WATCHDOG_TIMEOUT\n", __FUNCTION__ );
358 IRDA_DEBUG(2, "%s(), Unknown event %d\n", __FUNCTION__ , event);
369 * Function irlan_client_state_wait (self, event, skb, info)
371 * WAIT, The irlan_client is waiting for the local provider to enter the
372 * provider OPEN state.
375 static int irlan_client_state_wait(struct irlan_cb *self, IRLAN_EVENT event,
378 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
380 ASSERT(self != NULL, return -1;);
383 case IRLAN_PROVIDER_SIGNAL:
384 irlan_next_client_state(self, IRLAN_ARB);
385 irlan_do_client_event(self, IRLAN_CHECK_CON_ARB, NULL);
387 case IRLAN_LMP_DISCONNECT:
388 case IRLAN_LAP_DISCONNECT:
389 irlan_next_client_state(self, IRLAN_IDLE);
391 case IRLAN_WATCHDOG_TIMEOUT:
392 IRDA_DEBUG(2, "%s(), IRLAN_WATCHDOG_TIMEOUT\n", __FUNCTION__ );
395 IRDA_DEBUG(2, "%s(), Unknown event %d\n", __FUNCTION__ , event);
404 static int irlan_client_state_arb(struct irlan_cb *self, IRLAN_EVENT event,
409 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
411 ASSERT(self != NULL, return -1;);
414 case IRLAN_CHECK_CON_ARB:
415 if (self->client.recv_arb_val == self->provider.send_arb_val) {
416 irlan_next_client_state(self, IRLAN_CLOSE);
417 irlan_close_data_channel(self);
418 } else if (self->client.recv_arb_val <
419 self->provider.send_arb_val)
421 qos.link_disc_time.bits = 0x01; /* 3 secs */
423 irlan_next_client_state(self, IRLAN_DATA);
424 irttp_connect_request(self->tsap_data,
425 self->dtsap_sel_data,
426 self->saddr, self->daddr, &qos,
428 } else if (self->client.recv_arb_val >
429 self->provider.send_arb_val)
431 IRDA_DEBUG(2, "%s(), lost the battle :-(\n", __FUNCTION__ );
434 case IRLAN_DATA_CONNECT_INDICATION:
435 irlan_next_client_state(self, IRLAN_DATA);
437 case IRLAN_LMP_DISCONNECT:
438 case IRLAN_LAP_DISCONNECT:
439 irlan_next_client_state(self, IRLAN_IDLE);
441 case IRLAN_WATCHDOG_TIMEOUT:
442 IRDA_DEBUG(2, "%s(), IRLAN_WATCHDOG_TIMEOUT\n", __FUNCTION__ );
445 IRDA_DEBUG(2, "%s(), Unknown event %d\n", __FUNCTION__ , event);
455 * Function irlan_client_state_data (self, event, skb, info)
457 * DATA, The data channel is connected, allowing data transfers between
458 * the local and remote machines.
461 static int irlan_client_state_data(struct irlan_cb *self, IRLAN_EVENT event,
464 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
466 ASSERT(self != NULL, return -1;);
467 ASSERT(self->magic == IRLAN_MAGIC, return -1;);
470 case IRLAN_DATA_INDICATION:
471 irlan_client_parse_response(self, skb);
473 case IRLAN_LMP_DISCONNECT: /* FALLTHROUGH */
474 case IRLAN_LAP_DISCONNECT:
475 irlan_next_client_state(self, IRLAN_IDLE);
478 IRDA_DEBUG(2, "%s(), Unknown event %d\n", __FUNCTION__ , event);
488 * Function irlan_client_state_close (self, event, skb, info)
493 static int irlan_client_state_close(struct irlan_cb *self, IRLAN_EVENT event,
496 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
505 * Function irlan_client_state_sync (self, event, skb, info)
510 static int irlan_client_state_sync(struct irlan_cb *self, IRLAN_EVENT event,
513 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );