-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirlap2.c
214 lines (186 loc) · 9.14 KB
/
irlap2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Copyright (C) 2002-2003 Gerd Rausch, BlauLogic (http://blaulogic.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Except as contained in this notice, neither the name of BlauLogic
nor the name(s) of the author(s) may be used to endorse or promote
products derived from this software without specific prior written
permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHOR(S) OR BLAULOGIC BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright (C) 2006-2007 Mathieu Albinet, [email protected]
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
o Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
o Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
o The names of the authors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* 19-02-07 MA modified irlap_append_neg_params call */
/* 19-02-07 MA Changed irlap_send_frame call */
#include <string.h>
#include "pspirda.h"
#include "irlap.h"
void irlap2_send_i_frame(IrLAP_Context *context_p, IrLAP_Frame *frame_p, uint16_t size)
{
frame_p->head.addr=context_p->conn_addr;
frame_p->head.ctrl=IRLAP_CTRL_P_F_MASK | context_p->vr<<IRLAP_CTRL_NR_BIT_POS | context_p->vs<<IRLAP_CTRL_NS_BIT_POS;
irlap_send_frame(context_p, frame_p, size);
}
int16_t irlap2_receive_i_frame(IrLAP_Context *context_p, IrLAP_Frame *frame_p,
IrLAP_Frame *last_resp_p, uint16_t last_resp_size,
uint16_t (*get_dev_info_cb)(uint8_t *dev_info_p, uint8_t user_data),
uint8_t get_dev_info_user_data)
{
IrLAP_Small_Frame resp;
int16_t n;
uint16_t param_len;
uint8_t old_vs;
old_vs=context_p->vs;
for(;;) {
if((n=irlap_receive_frame(frame_p))<0) {
DEBUG( 5, "Error irlap_receive_frame return code\n");
return -1;
}
if(!(frame_p->head.addr & IRLAP_ADDR_C_TEST))
/* discard response frames in secondary mode */
continue;
/* command frame */
if(frame_p->head.ctrl & IRLAP_CTRL_I_TEST) {
/* U or S format */
if(frame_p->head.ctrl & IRLAP_CTRL_S_TEST) {
/* U format */
switch(frame_p->head.ctrl & IRLAP_CTRL_U_MASK) {
case IRLAP_CTRL_U_SNRM_CMD:
if(frame_p->head.addr==(IRLAP_ADDR_BROADCAST | IRLAP_ADDR_C_TEST) &&
n>=sizeof(IrLAP_Head)+sizeof(IrLAP_SNRM) &&
irlap_matching_dest_addr(context_p, frame_p->u.snrm.dest)) {
context_p->conn_addr=frame_p->u.snrm.conn_addr;
context_p->vr=0;
context_p->vs=0;
get_neg_params(context_p, frame_p->u.snrm.info);
resp.head.addr=context_p->conn_addr;
resp.head.ctrl=IRLAP_CTRL_U_UA_RESP | IRLAP_CTRL_P_F_MASK;
memcpy(resp.u.ua.source, context_p->dev_addr, sizeof(IrLAP_Device_Addr));
memcpy(resp.u.ua.dest, frame_p->u.snrm.source, sizeof(IrLAP_Device_Addr));
param_len=irlap_append_neg_params(context_p, resp.u.ua.info);
irlap_send_frame(context_p, (IrLAP_Frame *)&resp, sizeof(IrLAP_Head)+sizeof(IrLAP_UA)+param_len);
}
break;
case IRLAP_CTRL_U_DISC_CMD:
if((frame_p->head.addr & ~IRLAP_ADDR_C_TEST)==context_p->conn_addr) {
resp.head.addr=context_p->conn_addr;
resp.head.ctrl=IRLAP_CTRL_U_UA_RESP | IRLAP_CTRL_P_F_MASK;
memcpy(resp.u.ua.source, context_p->dev_addr, sizeof(IrLAP_Device_Addr));
memcpy(resp.u.ua.dest, frame_p->u.snrm.source, sizeof(IrLAP_Device_Addr));
irlap_send_frame(context_p, (IrLAP_Frame *)&resp, sizeof(IrLAP_Head)+sizeof(IrLAP_UA));
/* BUG should return -1 ??? */
}
break;
case IRLAP_CTRL_U_UI_CMD:
break;
case IRLAP_CTRL_U_XID_CMD:
if(frame_p->head.addr==(IRLAP_ADDR_BROADCAST | IRLAP_ADDR_C_TEST) &&
n>=sizeof(IrLAP_Head)+sizeof(IrLAP_Discovery) &&
frame_p->u.discovery.format==IRLAP_XID_FMT_DISCOVERY &&
irlap_matching_dest_addr(context_p, frame_p->u.discovery.dest)) {
if(frame_p->u.discovery.slot==((frame_p->u.discovery.flags&IRLAP_XID_FLAGS_SLOTS_MASK)==IRLAP_XID_FLAGS_SLOTS_1 ? 0 : 1)) { /* BUG : fonctionnement correct mais implementation louche */
resp.head.addr=IRLAP_ADDR_BROADCAST;
resp.head.ctrl=IRLAP_CTRL_U_XID_RESP | IRLAP_CTRL_P_F_MASK;
resp.u.discovery.format=IRLAP_XID_FMT_DISCOVERY;
memcpy(resp.u.discovery.source, context_p->dev_addr, sizeof(IrLAP_Device_Addr));
memcpy(resp.u.discovery.dest, frame_p->u.discovery.source, sizeof(IrLAP_Device_Addr));
resp.u.discovery.flags=frame_p->u.discovery.flags;
resp.u.discovery.slot=0;
resp.u.discovery.version=IRLAP_VERSION;
if(get_dev_info_cb)
param_len=(*get_dev_info_cb)(resp.u.discovery.info, get_dev_info_user_data);
else
param_len=0;
irlap_send_frame(context_p, (IrLAP_Frame *)&resp, sizeof(IrLAP_Head)+sizeof(IrLAP_Discovery)+param_len);
}
}
break;
case IRLAP_CTRL_U_TEST_CMD:
if(n>=sizeof(IrLAP_Head)+sizeof(IrLAP_Test) &&
irlap_matching_dest_addr(context_p, frame_p->u.test.dest)) {
resp.head.addr=IRLAP_ADDR_NULL;
resp.head.ctrl=IRLAP_CTRL_U_TEST_RESP | IRLAP_CTRL_P_F_MASK;
memcpy(resp.u.test.source, context_p->dev_addr, sizeof(IrLAP_Device_Addr));
memcpy(resp.u.test.dest, frame_p->u.test.source, sizeof(IrLAP_Device_Addr));
irlap_send_frame(context_p, (IrLAP_Frame *)&resp, sizeof(IrLAP_Head)+sizeof(IrLAP_Test));
}
break;
}
} else {
/* S format */
switch(frame_p->head.ctrl & IRLAP_CTRL_S_MASK) {
case IRLAP_CTRL_S_RR:
if((frame_p->head.addr & ~IRLAP_ADDR_C_TEST)==context_p->conn_addr) {
context_p->vs=(frame_p->head.ctrl>>IRLAP_CTRL_NR_BIT_POS)&IRLAP_CTRL_N_MASK;
if(last_resp_p && context_p->vs==old_vs) {
/* resend last response */
irlap2_send_i_frame(context_p, last_resp_p, last_resp_size);
} else {
/* send RR response */
resp.head.addr=context_p->conn_addr;
resp.head.ctrl=IRLAP_CTRL_S_RR | IRLAP_CTRL_P_F_MASK | context_p->vr<<IRLAP_CTRL_NR_BIT_POS;
irlap_send_frame(context_p, (IrLAP_Frame *)&resp, sizeof(IrLAP_Head));
}
}
break;
case IRLAP_CTRL_S_RNR:
break;
case IRLAP_CTRL_S_REJ:
break;
case IRLAP_CTRL_S_SREJ:
break;
}
}
} else {
/* I format */
if((frame_p->head.addr & ~IRLAP_ADDR_C_TEST)==context_p->conn_addr &&
context_p->vr==((frame_p->head.ctrl>>IRLAP_CTRL_NS_BIT_POS)&IRLAP_CTRL_N_MASK)) {
context_p->vs=(frame_p->head.ctrl>>IRLAP_CTRL_NR_BIT_POS)&IRLAP_CTRL_N_MASK;
if(last_resp_p && context_p->vs==old_vs) {
/* reject received frame */
resp.head.addr=context_p->conn_addr;
resp.head.ctrl=IRLAP_CTRL_S_REJ | IRLAP_CTRL_P_F_MASK | context_p->vr<<IRLAP_CTRL_NR_BIT_POS;
irlap_send_frame(context_p, (IrLAP_Frame *)&resp, sizeof(IrLAP_Head));
} else {
context_p->vr=(context_p->vr+1)&IRLAP_CTRL_N_MASK;
return n;
}
}
}
}
}