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