~ubuntu-branches/ubuntu/jaunty/irda-utils/jaunty-proposed

« back to all changes in this revision

Viewing changes to irdadump/src/irlmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Sivan Greenberg
  • Date: 2006-12-07 18:32:56 UTC
  • mfrom: (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20061207183256-tmlir0kv8t5vavnd
Tags: 0.9.18-5ubuntu1
* Merged with Debian unstable.
Remaining changes:
* findchip/smc.c,
  findchip/nsc.c,
  findchip/winbond.c:
  + Include sys/io.h instead of asm/io.h to fix FTBFS on i386
* debian/control,
  debian/irda-utils.init:
  + read -d doesn't work with dash. Run with bash and depend on it.
    Thanks to Alexander Schremmer for noticing.
* Remove stop script symlinks from rc0 and rc6.
* Autoconfiguration support (Debian #324404)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*********************************************************************
2
 
 *                
3
 
 * Filename:      irlmp.c
4
 
 * Version:       
5
 
 * Description:   
6
 
 * Status:        Experimental.
7
 
 * Author:        Dag Brattli <dagb@cs.uit.no>
8
 
 * Created at:    Wed Mar 31 15:30:46 1999
9
 
 * Modified at:   Wed Dec  8 09:46:16 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 <sys/socket.h>
32
 
#include <sys/types.h>
33
 
 
34
 
#include <string.h>             /* For strstr */
35
 
#include <stdio.h>              /* For printf */
36
 
 
37
 
#include <irda.h>
38
 
 
39
 
#include "irdadump.h"
40
 
 
41
 
extern struct ias_query last_ias;
42
 
extern struct lsap_state conn[];
43
 
 
44
 
/*
45
 
 * Function parse_iriap (buf)
46
 
 *
47
 
 *    Try to parse and print the LM-IAS query
48
 
 *
49
 
 */
50
 
inline void parse_iriap_command(GNetBuf *buf, GString *str, guint8 slsap_sel)
51
 
{
52
 
        guint8 opcode;
53
 
        char name[255];
54
 
        char attr[255];
55
 
        int n = 0;
56
 
        int name_len;
57
 
        int attr_len;
58
 
 
59
 
        /* Kill "unused" warning */
60
 
        slsap_sel = slsap_sel;
61
 
 
62
 
        opcode = buf->data[n++];
63
 
 
64
 
        opcode &= ~IAP_LST; /* Mask away LST bit */
65
 
        switch (opcode) {
66
 
        case GET_INFO_BASE:
67
 
        case GET_VALUE_BY_CLASS:
68
 
                name_len = buf->data[n++];
69
 
                memcpy( name, buf->data+n, name_len); n+=name_len;
70
 
                name[name_len] = '\0';
71
 
                
72
 
                attr_len = buf->data[n++]; 
73
 
                memcpy( attr, buf->data+n, attr_len); n+=attr_len;
74
 
                attr[attr_len] = '\0';
75
 
 
76
 
                g_string_sprintfa(str, "GET_VALUE_BY_CLASS: \"%s\" \"%s\" ",
77
 
                                  name, attr);
78
 
 
79
 
                /* Check if this is a TinyTP lookup */
80
 
                if (strstr(attr, "TinyTP"))
81
 
                        last_ias.ttp = 1;
82
 
                else
83
 
                        last_ias.ttp = 0;
84
 
                /* Reset to undefined (handle IAS failures properly) */
85
 
                last_ias.lsap_sel = LSAP_ANY;
86
 
 
87
 
                /* Check if this is a OBEX lookup */
88
 
                if (strstr(name, "OBEX"))
89
 
                        last_ias.obex = 1;
90
 
                else
91
 
                        last_ias.obex = 0;
92
 
                /* Check if this is a IrCOMM lookup */
93
 
                if (strstr(name, "IrCOMM") || strstr(name, "IrLPT"))
94
 
                        last_ias.ircomm = 1;
95
 
                else
96
 
                        last_ias.ircomm = 0;
97
 
                /* Check if this is a IrNET lookup */
98
 
                if (strstr(name, "IrNet"))
99
 
                        last_ias.irnet = 1;
100
 
                else
101
 
                        last_ias.irnet = 0;
102
 
                break;
103
 
        default:
104
 
                break;
105
 
        }
106
 
}
107
 
 
108
 
/*
109
 
 * Function parse_irias_value (buf)
110
 
 *
111
 
 *    
112
 
 *
113
 
 */
114
 
void parse_irias_value(GNetBuf *buf, GString *str)
115
 
{
116
 
        char string[255];
117
 
        int n = 0;
118
 
        int charset;
119
 
        guint32 value_len;
120
 
        guint32 tmp_cpu32;
121
 
        guint16 tmp_cpu16;
122
 
        guint16 obj_id;
123
 
        guint16 len;
124
 
        guint8  type;
125
 
 
126
 
        /* Get length, MSB first */
127
 
        memcpy(&len, buf->data+n, 2); n += 2;
128
 
        len = GUINT16_FROM_BE(len); 
129
 
 
130
 
        /* Get object ID, MSB first */
131
 
        memcpy(&obj_id, buf->data+n, 2); n += 2;
132
 
        obj_id = GUINT16_FROM_BE(obj_id);
133
 
 
134
 
        type = buf->data[n++];
135
 
 
136
 
        switch(type) {
137
 
        case IAS_INTEGER:
138
 
                memcpy(&tmp_cpu32, buf->data+n, 4); n += 4;
139
 
                tmp_cpu32 = GUINT32_FROM_BE(tmp_cpu32);
140
 
 
141
 
                /*  Legal values restricted to 0x01-0x6f, page 15 irttp */
142
 
                g_string_sprintfa(str, "Integer: %02x ", tmp_cpu32);
143
 
                last_ias.lsap_sel = tmp_cpu32;
144
 
                break;
145
 
        case IAS_STRING:
146
 
                charset = buf->data[n++];
147
 
 
148
 
                switch (charset) {
149
 
                case CS_ASCII:
150
 
                        break;
151
 
                case CS_ISO_8859_1:
152
 
                case CS_ISO_8859_2:
153
 
                case CS_ISO_8859_3:
154
 
                case CS_ISO_8859_4:
155
 
                case CS_ISO_8859_5:
156
 
                case CS_ISO_8859_6:
157
 
                case CS_ISO_8859_7:
158
 
                case CS_ISO_8859_8:
159
 
                case CS_ISO_8859_9:
160
 
                case CS_UNICODE:
161
 
                default:
162
 
                        break;
163
 
                }
164
 
                value_len = buf->data[n++];
165
 
                
166
 
                /* Make sure the string is null-terminated */
167
 
                memcpy(string, buf->data+n, value_len);
168
 
                string[value_len] = 0x00;
169
 
                
170
 
                g_string_sprintfa(str, "String: %s ", string);
171
 
                break;
172
 
        case IAS_OCT_SEQ:
173
 
                memcpy(&tmp_cpu16, buf->data+n, 2);  n += 2;
174
 
                tmp_cpu16 = GUINT16_FROM_BE(tmp_cpu16);
175
 
                value_len = tmp_cpu16;
176
 
                
177
 
                g_netbuf_pull(buf, n);
178
 
                if (last_ias.ircomm) {
179
 
                        g_string_append(str, "\n\tIrCOMM Parameters ");
180
 
                        parse_ircomm_params(value_len, buf, str);
181
 
                } else
182
 
                        g_string_append(str, "N/A ");
183
 
                break;
184
 
        default:
185
 
                g_print("%s() Unknown IAS value type! ", __FUNCTION__);
186
 
                break;
187
 
        }
188
 
}
189
 
 
190
 
/*
191
 
 * Function parse_iriap_response (buf)
192
 
 *
193
 
 *    
194
 
 *
195
 
 */
196
 
inline void parse_iriap_response(GNetBuf *buf, GString *str, guint8 dlsap_sel)
197
 
{
198
 
        guint8 opcode, rsp;
199
 
 
200
 
        /* Kill "unused" warning */
201
 
        dlsap_sel = dlsap_sel;
202
 
 
203
 
        opcode = buf->data[0] & ~IAP_LST; /* Mask away LST bit */
204
 
        rsp = buf->data[1];
205
 
 
206
 
        g_netbuf_pull(buf, 2);
207
 
 
208
 
        switch(opcode) {
209
 
        case GET_INFO_BASE:
210
 
                g_print("%s() Sorry, GET_INFO_BASE not implemented!\n", __FUNCTION__);
211
 
                break;
212
 
        case GET_VALUE_BY_CLASS:
213
 
                g_string_append(str, "GET_VALUE_BY_CLASS: ");
214
 
                break;
215
 
        default:
216
 
                g_print("%s() Sorry, not implemented!\n", __FUNCTION__);
217
 
                break;
218
 
        }
219
 
 
220
 
        switch(rsp) {
221
 
        case IAS_SUCCESS:
222
 
                g_string_append(str, "Success ");
223
 
                parse_irias_value(buf, str);
224
 
                break;
225
 
        case IAS_CLASS_UNKNOWN:
226
 
                g_string_append(str, "No such class ");
227
 
                break;
228
 
        case IAS_ATTRIB_UNKNOWN:
229
 
                g_string_append(str, "No such attribute ");
230
 
                break;
231
 
        default:
232
 
                g_string_append(str, "Unknown response code ");
233
 
        }
234
 
}
235
 
 
236
 
/*
237
 
 * Function parse_irttp (buf, str)
238
 
 *
239
 
 *    Parse IrTTP data frame
240
 
 *
241
 
 */
242
 
inline void parse_irnet(GNetBuf *buf, GString *str) 
243
 
{
244
 
        /* Kill "unused" warning */
245
 
        buf = buf;
246
 
 
247
 
        g_string_sprintfa(str, "IrNET ");
248
 
        /* If you want to hook PPP frame decoding, you can do it here.
249
 
         * I don't think it's worth it, PPP has debug capabilities.
250
 
         * Jean II */
251
 
}
252
 
 
253
 
/*
254
 
 * Function parse_irttp (buf, str)
255
 
 *
256
 
 *    Parse IrTTP data frame
257
 
 *
258
 
 */
259
 
inline void parse_irttp(GNetBuf *buf, GString *str) 
260
 
{
261
 
        g_string_sprintfa(str, "TTP credits=%d ", buf->data[0] & 0x7f);
262
 
 
263
 
        if (buf->data[0] & 0x80)
264
 
                g_string_append(str, "More ");
265
 
 
266
 
        /* Remove TTP header */
267
 
        g_netbuf_pull(buf, 1);
268
 
}
269
 
 
270
 
/*
271
 
 * Function parse_irttp_connect (buf, str)
272
 
 *
273
 
 *    Parse IrTTP connect frame
274
 
 *
275
 
 */
276
 
inline void parse_irttp_connect(GNetBuf *buf, GString *str)
277
 
{
278
 
        guint8 plen, pi, pl;
279
 
        guint16 tmp_cpu;
280
 
 
281
 
        g_string_sprintfa(str, "TTP credits=%d ", buf->data[0] & 0x7f);
282
 
 
283
 
        if (buf->data[0] & 0x80) {
284
 
                plen = buf->data[1];
285
 
                pi   = buf->data[2];
286
 
                pl   = buf->data[3];
287
 
 
288
 
                memcpy(&tmp_cpu, buf->data+4, 2); /* Align value */
289
 
                tmp_cpu = GUINT16_FROM_BE(tmp_cpu); /* Convert to host order */
290
 
 
291
 
                /* Remove TTP parameters */
292
 
                g_netbuf_pull(buf, plen);
293
 
 
294
 
                g_string_sprintfa(str, "MaxSduSize=%d ", tmp_cpu);
295
 
        }
296
 
        /* Remove TTP header */
297
 
        g_netbuf_pull(buf, 1);
298
 
}
299
 
 
300
 
/*
301
 
 * Function parse_irlmp (buf)
302
 
 *
303
 
 *    Parse IrLMP frame
304
 
 *
305
 
 */
306
 
inline void parse_irlmp(GNetBuf *buf, GString *str,
307
 
                        guint8 caddr, int type, int cmd)
308
 
{
309
 
        guint8 slsap_sel, dlsap_sel;
310
 
        int ctrl;
311
 
        int rsvd;
312
 
        int i;
313
 
        
314
 
        ctrl = buf->data[0] & CONTROL_BIT;
315
 
        dlsap_sel = buf->data[0] & LSAP_MASK;
316
 
        slsap_sel = buf->data[1];
317
 
        
318
 
        /* Remove IrLMP header */
319
 
        g_netbuf_pull(buf, 2);
320
 
 
321
 
        g_string_sprintfa(str, "LM slsap=%02x dlsap=%02x ", slsap_sel, 
322
 
                          dlsap_sel);
323
 
        
324
 
        /* Control or data? */
325
 
        if (ctrl) {
326
 
                ctrl = buf->data[0];
327
 
                rsvd = buf->data[1];    /* reason/status/rsvd */
328
 
 
329
 
                /* Remove IrLMP control header (2 bytes) */
330
 
                g_netbuf_pull(buf, 2);
331
 
 
332
 
                switch (ctrl) {
333
 
                case CONNECT_CMD:
334
 
                        g_string_append(str, "CONN_CMD ");
335
 
                        if (dlsap_sel == last_ias.lsap_sel) {
336
 
                                i = find_free_connection();
337
 
                                if (i == -1) {
338
 
                                        printf("No more space for connection!\n");
339
 
                                        return;
340
 
                                }
341
 
                                conn[i].valid = 1;
342
 
                                conn[i].caddr = caddr;
343
 
                                if (type) {
344
 
                                        conn[i].slsap_sel = slsap_sel;
345
 
                                        conn[i].dlsap_sel = dlsap_sel;
346
 
                                } else {
347
 
                                        conn[i].slsap_sel = dlsap_sel;
348
 
                                        conn[i].dlsap_sel = slsap_sel;
349
 
                                }
350
 
                                conn[i].ttp = last_ias.ttp;
351
 
                                conn[i].obex = last_ias.obex;
352
 
                                conn[i].ircomm = last_ias.ircomm;
353
 
                                conn[i].irnet = last_ias.irnet;
354
 
 
355
 
                                if (conn[i].ttp)
356
 
                                        parse_irttp_connect(buf, str);
357
 
                                if (conn[i].ircomm)
358
 
                                        parse_ircomm_connect(buf, str);
359
 
                                if (conn[i].irnet)
360
 
                                        parse_irnet(buf, str);
361
 
                        } else {
362
 
                                if((config_force_ttp) && (dlsap_sel != 0x00))
363
 
                                        parse_irttp_connect(buf, str);
364
 
                        }
365
 
                        break;
366
 
                case CONNECT_RSP:
367
 
                        g_string_append(str, "CONN_RSP ");
368
 
                        if (type)
369
 
                                i = find_connection(slsap_sel, dlsap_sel);
370
 
                        else
371
 
                                i = find_connection(dlsap_sel, slsap_sel);
372
 
                        if (i != -1) {
373
 
                                if (conn[i].ttp)
374
 
                                        parse_irttp_connect(buf, str);
375
 
                                if (conn[i].ircomm)
376
 
                                        parse_ircomm_connect(buf, str);
377
 
                                if (conn[i].irnet)
378
 
                                        parse_irnet(buf, str);
379
 
                        } else {
380
 
                                if((config_force_ttp) && (slsap_sel != 0x00))
381
 
                                        parse_irttp_connect(buf, str);
382
 
                        }
383
 
                        break;
384
 
                case DISCONNECT:
385
 
                        g_string_append(str, "DISC ");
386
 
 
387
 
                        /* Mark this connection as invalid */
388
 
                        if (type)
389
 
                                i = find_connection(slsap_sel, dlsap_sel);
390
 
                        else
391
 
                                i = find_connection(dlsap_sel, slsap_sel);
392
 
                        if (i != -1) {
393
 
                                conn[i].valid = 0;
394
 
                        }
395
 
                        break;
396
 
                default:
397
 
                        break;
398
 
                }
399
 
        } else {
400
 
                /* Check if this is a LM-IAS query */
401
 
                if (dlsap_sel == 0x00)
402
 
                        parse_iriap_command(buf, str, slsap_sel);
403
 
                else if (slsap_sel == 0x00)
404
 
                        parse_iriap_response(buf, str, dlsap_sel);
405
 
                
406
 
                /* Find connection */
407
 
                if (type)
408
 
                        i = find_connection(slsap_sel, dlsap_sel);
409
 
                else
410
 
                        i = find_connection(dlsap_sel, slsap_sel);
411
 
                if (i != -1) {
412
 
                        if (conn[i].valid && conn[i].ttp)
413
 
                                parse_irttp(buf, str);
414
 
                        if (conn[i].valid && conn[i].obex)
415
 
                                parse_obex(buf, str, cmd);
416
 
                        if (conn[i].valid && conn[i].ircomm) {
417
 
                                if (conn[i].ttp)
418
 
                                        parse_ircomm_ttp(buf, str);
419
 
                                else
420
 
                                        parse_ircomm_lmp(buf, str);
421
 
                        }
422
 
                        if (conn[i].valid && conn[i].irnet)
423
 
                                parse_irnet(buf, str);
424
 
                }
425
 
                /* Not IAS and unknow connection */
426
 
                if((dlsap_sel != 0x00) && (slsap_sel != 0x00) && (i == -1)) {
427
 
                        if(config_force_ttp)
428
 
                                parse_irttp(buf, str);
429
 
                }
430
 
        }
431
 
}
432
 
 
433
 
 
434
 
/*
435
 
 * Function parse_ui_irlmp (buf)
436
 
 *
437
 
 *    Parse IrLMP frame in UI frame
438
 
 *
439
 
 */
440
 
inline void parse_ui_irlmp(GNetBuf *buf, GString *str, int type)
441
 
{
442
 
        guint8 slsap_sel, dlsap_sel;
443
 
        int ctrl;
444
 
        
445
 
        /* Kill "unused" warning */
446
 
        type = type;
447
 
 
448
 
        ctrl = buf->data[0] & CONTROL_BIT;
449
 
        dlsap_sel = buf->data[0] & LSAP_MASK;
450
 
        slsap_sel = buf->data[1];
451
 
        
452
 
        /* Remove IrLMP header */
453
 
        g_netbuf_pull(buf, 2);
454
 
 
455
 
        g_string_sprintfa(str, "LM slsap=%02x dlsap=%02x ", slsap_sel, 
456
 
                          dlsap_sel);
457
 
 
458
 
        /* Let's see if it's Ultra, and decode it - Jean II */
459
 
        if((slsap_sel == 0x70) && (dlsap_sel == 0x70))
460
 
          {
461
 
            int upid = buf->data[0] & 0x7F;
462
 
            g_netbuf_pull(buf, 1);
463
 
 
464
 
            g_string_sprintfa(str, " Ultra-PID=%02x ", upid);
465
 
 
466
 
            /* Check Obex over Ultra */
467
 
            if(upid == 0x01)
468
 
              {
469
 
                /* Remove SAR field */
470
 
                g_netbuf_pull(buf, 1);
471
 
 
472
 
                /* Decode Obex stuff - no connection, always a command */
473
 
                parse_obex(buf, str, 1);
474
 
              }
475
 
          }
476
 
}