ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / isdn / capi / capiutil.c
1 /* $Id: capiutil.c,v 1.13.6.4 2001/09/23 22:24:33 kai Exp $
2  *
3  * CAPI 2.0 convert capi message to capi message struct
4  *
5  * From CAPI 2.0 Development Kit AVM 1995 (msg.c)
6  * Rewritten for Linux 1996 by Carsten Paeth <calle@calle.de>
7  *
8  * This software may be used and distributed according to the terms
9  * of the GNU General Public License, incorporated herein by reference.
10  *
11  */
12
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
16 #include <linux/stddef.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/init.h>
20 #include <linux/config.h>
21 #include <linux/isdn/capiutil.h>
22
23 /* from CAPI2.0 DDK AVM Berlin GmbH */
24
25 #ifndef CONFIG_ISDN_DRV_AVMB1_VERBOSE_REASON
26 char *capi_info2str(u16 reason)
27 {
28     return "..";
29 }
30 #else
31 char *capi_info2str(u16 reason)
32 {
33     switch (reason) {
34
35 /*-- informative values (corresponding message was processed) -----*/
36         case 0x0001:
37            return "NCPI not supported by current protocol, NCPI ignored";
38         case 0x0002:
39            return "Flags not supported by current protocol, flags ignored";
40         case 0x0003:
41            return "Alert already sent by another application";
42
43 /*-- error information concerning CAPI_REGISTER -----*/
44         case 0x1001:
45            return "Too many applications";
46         case 0x1002:
47            return "Logical block size too small, must be at least 128 Bytes";
48         case 0x1003:
49            return "Buffer exceeds 64 kByte";
50         case 0x1004:
51            return "Message buffer size too small, must be at least 1024 Bytes";
52         case 0x1005:
53            return "Max. number of logical connections not supported";
54         case 0x1006:
55            return "Reserved";
56         case 0x1007:
57            return "The message could not be accepted because of an internal busy condition";
58         case 0x1008:
59            return "OS resource error (no memory ?)";
60         case 0x1009:
61            return "CAPI not installed";
62         case 0x100A:
63            return "Controller does not support external equipment";
64         case 0x100B:
65            return "Controller does only support external equipment";
66
67 /*-- error information concerning message exchange functions -----*/
68         case 0x1101:
69            return "Illegal application number";
70         case 0x1102:
71            return "Illegal command or subcommand or message length less than 12 bytes";
72         case 0x1103:
73            return "The message could not be accepted because of a queue full condition !! The error code does not imply that CAPI cannot receive messages directed to another controller, PLCI or NCCI";
74         case 0x1104:
75            return "Queue is empty";
76         case 0x1105:
77            return "Queue overflow, a message was lost !! This indicates a configuration error. The only recovery from this error is to perform a CAPI_RELEASE";
78         case 0x1106:
79            return "Unknown notification parameter";
80         case 0x1107:
81            return "The Message could not be accepted because of an internal busy condition";
82         case 0x1108:
83            return "OS Resource error (no memory ?)";
84         case 0x1109:
85            return "CAPI not installed";
86         case 0x110A:
87            return "Controller does not support external equipment";
88         case 0x110B:
89            return "Controller does only support external equipment";
90
91 /*-- error information concerning resource / coding problems -----*/
92         case 0x2001:
93            return "Message not supported in current state";
94         case 0x2002:
95            return "Illegal Controller / PLCI / NCCI";
96         case 0x2003:
97            return "Out of PLCI";
98         case 0x2004:
99            return "Out of NCCI";
100         case 0x2005:
101            return "Out of LISTEN";
102         case 0x2006:
103            return "Out of FAX resources (protocol T.30)";
104         case 0x2007:
105            return "Illegal message parameter coding";
106
107 /*-- error information concerning requested services  -----*/
108         case 0x3001:
109            return "B1 protocol not supported";
110         case 0x3002: 
111            return "B2 protocol not supported";
112         case 0x3003: 
113            return "B3 protocol not supported";
114         case 0x3004: 
115            return "B1 protocol parameter not supported";
116         case 0x3005: 
117            return "B2 protocol parameter not supported";
118         case 0x3006: 
119            return "B3 protocol parameter not supported";
120         case 0x3007: 
121            return "B protocol combination not supported";
122         case 0x3008: 
123            return "NCPI not supported";
124         case 0x3009: 
125            return "CIP Value unknown";
126         case 0x300A: 
127            return "Flags not supported (reserved bits)";
128         case 0x300B: 
129            return "Facility not supported";
130         case 0x300C: 
131            return "Data length not supported by current protocol";
132         case 0x300D: 
133            return "Reset procedure not supported by current protocol";
134
135 /*-- informations about the clearing of a physical connection -----*/
136         case 0x3301: 
137            return "Protocol error layer 1 (broken line or B-channel removed by signalling protocol)";
138         case 0x3302: 
139            return "Protocol error layer 2";
140         case 0x3303: 
141            return "Protocol error layer 3";
142         case 0x3304: 
143            return "Another application got that call";
144 /*-- T.30 specific reasons -----*/
145         case 0x3311: 
146            return "Connecting not successful (remote station is no FAX G3 machine)";
147         case 0x3312: 
148            return "Connecting not successful (training error)";
149         case 0x3313: 
150            return "Disconnected before transfer (remote station does not support transfer mode, e.g. resolution)";
151         case 0x3314: 
152            return "Disconnected during transfer (remote abort)";
153         case 0x3315: 
154            return "Disconnected during transfer (remote procedure error, e.g. unsuccessful repetition of T.30 commands)";
155         case 0x3316: 
156            return "Disconnected during transfer (local tx data underrun)";
157         case 0x3317: 
158            return "Disconnected during transfer (local rx data overflow)";
159         case 0x3318: 
160            return "Disconnected during transfer (local abort)";
161         case 0x3319: 
162            return "Illegal parameter coding (e.g. SFF coding error)";
163
164 /*-- disconnect causes from the network according to ETS 300 102-1/Q.931 -----*/
165         case 0x3481: return "Unallocated (unassigned) number";
166         case 0x3482: return "No route to specified transit network";
167         case 0x3483: return "No route to destination";
168         case 0x3486: return "Channel unacceptable";
169         case 0x3487: 
170            return "Call awarded and being delivered in an established channel";
171         case 0x3490: return "Normal call clearing";
172         case 0x3491: return "User busy";
173         case 0x3492: return "No user responding";
174         case 0x3493: return "No answer from user (user alerted)";
175         case 0x3495: return "Call rejected";
176         case 0x3496: return "Number changed";
177         case 0x349A: return "Non-selected user clearing";
178         case 0x349B: return "Destination out of order";
179         case 0x349C: return "Invalid number format";
180         case 0x349D: return "Facility rejected";
181         case 0x349E: return "Response to STATUS ENQUIRY";
182         case 0x349F: return "Normal, unspecified";
183         case 0x34A2: return "No circuit / channel available";
184         case 0x34A6: return "Network out of order";
185         case 0x34A9: return "Temporary failure";
186         case 0x34AA: return "Switching equipment congestion";
187         case 0x34AB: return "Access information discarded";
188         case 0x34AC: return "Requested circuit / channel not available";
189         case 0x34AF: return "Resources unavailable, unspecified";
190         case 0x34B1: return "Quality of service unavailable";
191         case 0x34B2: return "Requested facility not subscribed";
192         case 0x34B9: return "Bearer capability not authorized";
193         case 0x34BA: return "Bearer capability not presently available";
194         case 0x34BF: return "Service or option not available, unspecified";
195         case 0x34C1: return "Bearer capability not implemented";
196         case 0x34C2: return "Channel type not implemented";
197         case 0x34C5: return "Requested facility not implemented";
198         case 0x34C6: return "Only restricted digital information bearer capability is available";
199         case 0x34CF: return "Service or option not implemented, unspecified";
200         case 0x34D1: return "Invalid call reference value";
201         case 0x34D2: return "Identified channel does not exist";
202         case 0x34D3: return "A suspended call exists, but this call identity does not";
203         case 0x34D4: return "Call identity in use";
204         case 0x34D5: return "No call suspended";
205         case 0x34D6: return "Call having the requested call identity has been cleared";
206         case 0x34D8: return "Incompatible destination";
207         case 0x34DB: return "Invalid transit network selection";
208         case 0x34DF: return "Invalid message, unspecified";
209         case 0x34E0: return "Mandatory information element is missing";
210         case 0x34E1: return "Message type non-existent or not implemented";
211         case 0x34E2: return "Message not compatible with call state or message type non-existent or not implemented";
212         case 0x34E3: return "Information element non-existent or not implemented";
213         case 0x34E4: return "Invalid information element contents";
214         case 0x34E5: return "Message not compatible with call state";
215         case 0x34E6: return "Recovery on timer expiry";
216         case 0x34EF: return "Protocol error, unspecified";
217         case 0x34FF: return "Interworking, unspecified";
218
219         default: return "No additional information";
220     }
221 }
222 #endif
223
224 typedef struct {
225         int typ;
226         size_t off;
227 } _cdef;
228
229 #define _CBYTE         1
230 #define _CWORD         2
231 #define _CDWORD        3
232 #define _CSTRUCT       4
233 #define _CMSTRUCT      5
234 #define _CEND          6
235
236 static _cdef cdef[] =
237 {
238     /*00 */ 
239  {_CEND},
240     /*01 */ 
241  {_CEND},
242     /*02 */ 
243  {_CEND},
244     /*03 */ 
245  {_CDWORD, offsetof(_cmsg, adr.adrController)},
246     /*04 */ 
247  {_CMSTRUCT, offsetof(_cmsg, AdditionalInfo)},
248     /*05 */ 
249  {_CSTRUCT, offsetof(_cmsg, B1configuration)},
250     /*06 */ 
251  {_CWORD, offsetof(_cmsg, B1protocol)},
252     /*07 */ 
253  {_CSTRUCT, offsetof(_cmsg, B2configuration)},
254     /*08 */ 
255  {_CWORD, offsetof(_cmsg, B2protocol)},
256     /*09 */ 
257  {_CSTRUCT, offsetof(_cmsg, B3configuration)},
258     /*0a */ 
259  {_CWORD, offsetof(_cmsg, B3protocol)},
260     /*0b */ 
261  {_CSTRUCT, offsetof(_cmsg, BC)},
262     /*0c */ 
263  {_CSTRUCT, offsetof(_cmsg, BChannelinformation)},
264     /*0d */ 
265  {_CMSTRUCT, offsetof(_cmsg, BProtocol)},
266     /*0e */ 
267  {_CSTRUCT, offsetof(_cmsg, CalledPartyNumber)},
268     /*0f */ 
269  {_CSTRUCT, offsetof(_cmsg, CalledPartySubaddress)},
270     /*10 */ 
271  {_CSTRUCT, offsetof(_cmsg, CallingPartyNumber)},
272     /*11 */ 
273  {_CSTRUCT, offsetof(_cmsg, CallingPartySubaddress)},
274     /*12 */ 
275  {_CDWORD, offsetof(_cmsg, CIPmask)},
276     /*13 */ 
277  {_CDWORD, offsetof(_cmsg, CIPmask2)},
278     /*14 */ 
279  {_CWORD, offsetof(_cmsg, CIPValue)},
280     /*15 */ 
281  {_CDWORD, offsetof(_cmsg, Class)},
282     /*16 */ 
283  {_CSTRUCT, offsetof(_cmsg, ConnectedNumber)},
284     /*17 */ 
285  {_CSTRUCT, offsetof(_cmsg, ConnectedSubaddress)},
286     /*18 */ 
287  {_CDWORD, offsetof(_cmsg, Data)},
288     /*19 */ 
289  {_CWORD, offsetof(_cmsg, DataHandle)},
290     /*1a */ 
291  {_CWORD, offsetof(_cmsg, DataLength)},
292     /*1b */ 
293  {_CSTRUCT, offsetof(_cmsg, FacilityConfirmationParameter)},
294     /*1c */ 
295  {_CSTRUCT, offsetof(_cmsg, Facilitydataarray)},
296     /*1d */ 
297  {_CSTRUCT, offsetof(_cmsg, FacilityIndicationParameter)},
298     /*1e */ 
299  {_CSTRUCT, offsetof(_cmsg, FacilityRequestParameter)},
300     /*1f */ 
301  {_CWORD, offsetof(_cmsg, FacilitySelector)},
302     /*20 */ 
303  {_CWORD, offsetof(_cmsg, Flags)},
304     /*21 */ 
305  {_CDWORD, offsetof(_cmsg, Function)},
306     /*22 */ 
307  {_CSTRUCT, offsetof(_cmsg, HLC)},
308     /*23 */ 
309  {_CWORD, offsetof(_cmsg, Info)},
310     /*24 */ 
311  {_CSTRUCT, offsetof(_cmsg, InfoElement)},
312     /*25 */ 
313  {_CDWORD, offsetof(_cmsg, InfoMask)},
314     /*26 */ 
315  {_CWORD, offsetof(_cmsg, InfoNumber)},
316     /*27 */ 
317  {_CSTRUCT, offsetof(_cmsg, Keypadfacility)},
318     /*28 */ 
319  {_CSTRUCT, offsetof(_cmsg, LLC)},
320     /*29 */ 
321  {_CSTRUCT, offsetof(_cmsg, ManuData)},
322     /*2a */ 
323  {_CDWORD, offsetof(_cmsg, ManuID)},
324     /*2b */ 
325  {_CSTRUCT, offsetof(_cmsg, NCPI)},
326     /*2c */ 
327  {_CWORD, offsetof(_cmsg, Reason)},
328     /*2d */ 
329  {_CWORD, offsetof(_cmsg, Reason_B3)},
330     /*2e */ 
331  {_CWORD, offsetof(_cmsg, Reject)},
332     /*2f */ 
333  {_CSTRUCT, offsetof(_cmsg, Useruserdata)}
334 };
335
336 static unsigned char *cpars[] =
337 {
338     /*00 */ 0,
339     /*01 ALERT_REQ */ (unsigned char *) "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
340     /*02 CONNECT_REQ */ (unsigned char *) "\x03\x14\x0e\x10\x0f\x11\x0d\x06\x08\x0a\x05\x07\x09\x01\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
341     /*03 */ 0,
342     /*04 DISCONNECT_REQ */ (unsigned char *) "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
343     /*05 LISTEN_REQ */ (unsigned char *) "\x03\x25\x12\x13\x10\x11\x01",
344     /*06 */ 0,
345     /*07 */ 0,
346     /*08 INFO_REQ */ (unsigned char *) "\x03\x0e\x04\x0c\x27\x2f\x1c\x01\x01",
347     /*09 FACILITY_REQ */ (unsigned char *) "\x03\x1f\x1e\x01",
348     /*0a SELECT_B_PROTOCOL_REQ */ (unsigned char *) "\x03\x0d\x06\x08\x0a\x05\x07\x09\x01\x01",
349     /*0b CONNECT_B3_REQ */ (unsigned char *) "\x03\x2b\x01",
350     /*0c */ 0,
351     /*0d DISCONNECT_B3_REQ */ (unsigned char *) "\x03\x2b\x01",
352     /*0e */ 0,
353     /*0f DATA_B3_REQ */ (unsigned char *) "\x03\x18\x1a\x19\x20\x01",
354     /*10 RESET_B3_REQ */ (unsigned char *) "\x03\x2b\x01",
355     /*11 */ 0,
356     /*12 */ 0,
357     /*13 ALERT_CONF */ (unsigned char *) "\x03\x23\x01",
358     /*14 CONNECT_CONF */ (unsigned char *) "\x03\x23\x01",
359     /*15 */ 0,
360     /*16 DISCONNECT_CONF */ (unsigned char *) "\x03\x23\x01",
361     /*17 LISTEN_CONF */ (unsigned char *) "\x03\x23\x01",
362     /*18 MANUFACTURER_REQ */ (unsigned char *) "\x03\x2a\x15\x21\x29\x01",
363     /*19 */ 0,
364     /*1a INFO_CONF */ (unsigned char *) "\x03\x23\x01",
365     /*1b FACILITY_CONF */ (unsigned char *) "\x03\x23\x1f\x1b\x01",
366     /*1c SELECT_B_PROTOCOL_CONF */ (unsigned char *) "\x03\x23\x01",
367     /*1d CONNECT_B3_CONF */ (unsigned char *) "\x03\x23\x01",
368     /*1e */ 0,
369     /*1f DISCONNECT_B3_CONF */ (unsigned char *) "\x03\x23\x01",
370     /*20 */ 0,
371     /*21 DATA_B3_CONF */ (unsigned char *) "\x03\x19\x23\x01",
372     /*22 RESET_B3_CONF */ (unsigned char *) "\x03\x23\x01",
373     /*23 */ 0,
374     /*24 */ 0,
375     /*25 */ 0,
376     /*26 CONNECT_IND */ (unsigned char *) "\x03\x14\x0e\x10\x0f\x11\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
377     /*27 CONNECT_ACTIVE_IND */ (unsigned char *) "\x03\x16\x17\x28\x01",
378     /*28 DISCONNECT_IND */ (unsigned char *) "\x03\x2c\x01",
379     /*29 */ 0,
380     /*2a MANUFACTURER_CONF */ (unsigned char *) "\x03\x2a\x15\x21\x29\x01",
381     /*2b */ 0,
382     /*2c INFO_IND */ (unsigned char *) "\x03\x26\x24\x01",
383     /*2d FACILITY_IND */ (unsigned char *) "\x03\x1f\x1d\x01",
384     /*2e */ 0,
385     /*2f CONNECT_B3_IND */ (unsigned char *) "\x03\x2b\x01",
386     /*30 CONNECT_B3_ACTIVE_IND */ (unsigned char *) "\x03\x2b\x01",
387     /*31 DISCONNECT_B3_IND */ (unsigned char *) "\x03\x2d\x2b\x01",
388     /*32 */ 0,
389     /*33 DATA_B3_IND */ (unsigned char *) "\x03\x18\x1a\x19\x20\x01",
390     /*34 RESET_B3_IND */ (unsigned char *) "\x03\x2b\x01",
391     /*35 CONNECT_B3_T90_ACTIVE_IND */ (unsigned char *) "\x03\x2b\x01",
392     /*36 */ 0,
393     /*37 */ 0,
394     /*38 CONNECT_RESP */ (unsigned char *) "\x03\x2e\x0d\x06\x08\x0a\x05\x07\x09\x01\x16\x17\x28\x04\x0c\x27\x2f\x1c\x01\x01",
395     /*39 CONNECT_ACTIVE_RESP */ (unsigned char *) "\x03\x01",
396     /*3a DISCONNECT_RESP */ (unsigned char *) "\x03\x01",
397     /*3b */ 0,
398     /*3c MANUFACTURER_IND */ (unsigned char *) "\x03\x2a\x15\x21\x29\x01",
399     /*3d */ 0,
400     /*3e INFO_RESP */ (unsigned char *) "\x03\x01",
401     /*3f FACILITY_RESP */ (unsigned char *) "\x03\x1f\x01",
402     /*40 */ 0,
403     /*41 CONNECT_B3_RESP */ (unsigned char *) "\x03\x2e\x2b\x01",
404     /*42 CONNECT_B3_ACTIVE_RESP */ (unsigned char *) "\x03\x01",
405     /*43 DISCONNECT_B3_RESP */ (unsigned char *) "\x03\x01",
406     /*44 */ 0,
407     /*45 DATA_B3_RESP */ (unsigned char *) "\x03\x19\x01",
408     /*46 RESET_B3_RESP */ (unsigned char *) "\x03\x01",
409     /*47 CONNECT_B3_T90_ACTIVE_RESP */ (unsigned char *) "\x03\x01",
410     /*48 */ 0,
411     /*49 */ 0,
412     /*4a */ 0,
413     /*4b */ 0,
414     /*4c */ 0,
415     /*4d */ 0,
416     /*4e MANUFACTURER_RESP */ (unsigned char *) "\x03\x2a\x15\x21\x29\x01",
417 };
418
419 /*-------------------------------------------------------*/
420
421 #define byteTLcpy(x,y)        *(u8 *)(x)=*(u8 *)(y);
422 #define wordTLcpy(x,y)        *(u16 *)(x)=*(u16 *)(y);
423 #define dwordTLcpy(x,y)       memcpy(x,y,4);
424 #define structTLcpy(x,y,l)    memcpy (x,y,l)
425 #define structTLcpyovl(x,y,l) memmove (x,y,l)
426
427 #define byteTRcpy(x,y)        *(u8 *)(y)=*(u8 *)(x);
428 #define wordTRcpy(x,y)        *(u16 *)(y)=*(u16 *)(x);
429 #define dwordTRcpy(x,y)       memcpy(y,x,4);
430 #define structTRcpy(x,y,l)    memcpy (y,x,l)
431 #define structTRcpyovl(x,y,l) memmove (y,x,l)
432
433 /*-------------------------------------------------------*/
434 static unsigned command_2_index(unsigned c, unsigned sc)
435 {
436         if (c & 0x80)
437                 c = 0x9 + (c & 0x0f);
438         else if (c <= 0x0f);
439         else if (c == 0x41)
440                 c = 0x9 + 0x1;
441         else if (c == 0xff)
442                 c = 0x00;
443         return (sc & 3) * (0x9 + 0x9) + c;
444 }
445
446 /*-------------------------------------------------------*/
447 #define TYP (cdef[cmsg->par[cmsg->p]].typ)
448 #define OFF (((u8 *)cmsg)+cdef[cmsg->par[cmsg->p]].off)
449
450 static void jumpcstruct(_cmsg * cmsg)
451 {
452         unsigned layer;
453         for (cmsg->p++, layer = 1; layer;) {
454                 /* $$$$$ assert (cmsg->p); */
455                 cmsg->p++;
456                 switch (TYP) {
457                 case _CMSTRUCT:
458                         layer++;
459                         break;
460                 case _CEND:
461                         layer--;
462                         break;
463                 }
464         }
465 }
466 /*-------------------------------------------------------*/
467 static void pars_2_message(_cmsg * cmsg)
468 {
469
470         for (; TYP != _CEND; cmsg->p++) {
471                 switch (TYP) {
472                 case _CBYTE:
473                         byteTLcpy(cmsg->m + cmsg->l, OFF);
474                         cmsg->l++;
475                         break;
476                 case _CWORD:
477                         wordTLcpy(cmsg->m + cmsg->l, OFF);
478                         cmsg->l += 2;
479                         break;
480                 case _CDWORD:
481                         dwordTLcpy(cmsg->m + cmsg->l, OFF);
482                         cmsg->l += 4;
483                         break;
484                 case _CSTRUCT:
485                         if (*(u8 **) OFF == 0) {
486                                 *(cmsg->m + cmsg->l) = '\0';
487                                 cmsg->l++;
488                         } else if (**(_cstruct *) OFF != 0xff) {
489                                 structTLcpy(cmsg->m + cmsg->l, *(_cstruct *) OFF, 1 + **(_cstruct *) OFF);
490                                 cmsg->l += 1 + **(_cstruct *) OFF;
491                         } else {
492                                 _cstruct s = *(_cstruct *) OFF;
493                                 structTLcpy(cmsg->m + cmsg->l, s, 3 + *(u16 *) (s + 1));
494                                 cmsg->l += 3 + *(u16 *) (s + 1);
495                         }
496                         break;
497                 case _CMSTRUCT:
498 /*----- Metastruktur 0 -----*/
499                         if (*(_cmstruct *) OFF == CAPI_DEFAULT) {
500                                 *(cmsg->m + cmsg->l) = '\0';
501                                 cmsg->l++;
502                                 jumpcstruct(cmsg);
503                         }
504 /*----- Metastruktur wird composed -----*/
505                         else {
506                                 unsigned _l = cmsg->l;
507                                 unsigned _ls;
508                                 cmsg->l++;
509                                 cmsg->p++;
510                                 pars_2_message(cmsg);
511                                 _ls = cmsg->l - _l - 1;
512                                 if (_ls < 255)
513                                         (cmsg->m + _l)[0] = (u8) _ls;
514                                 else {
515                                         structTLcpyovl(cmsg->m + _l + 3, cmsg->m + _l + 1, _ls);
516                                         (cmsg->m + _l)[0] = 0xff;
517                                         wordTLcpy(cmsg->m + _l + 1, &_ls);
518                                 }
519                         }
520                         break;
521                 }
522         }
523 }
524
525 /*-------------------------------------------------------*/
526 unsigned capi_cmsg2message(_cmsg * cmsg, u8 * msg)
527 {
528         cmsg->m = msg;
529         cmsg->l = 8;
530         cmsg->p = 0;
531         cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
532
533         pars_2_message(cmsg);
534
535         wordTLcpy(msg + 0, &cmsg->l);
536         byteTLcpy(cmsg->m + 4, &cmsg->Command);
537         byteTLcpy(cmsg->m + 5, &cmsg->Subcommand);
538         wordTLcpy(cmsg->m + 2, &cmsg->ApplId);
539         wordTLcpy(cmsg->m + 6, &cmsg->Messagenumber);
540
541         return 0;
542 }
543
544 /*-------------------------------------------------------*/
545 static void message_2_pars(_cmsg * cmsg)
546 {
547         for (; TYP != _CEND; cmsg->p++) {
548
549                 switch (TYP) {
550                 case _CBYTE:
551                         byteTRcpy(cmsg->m + cmsg->l, OFF);
552                         cmsg->l++;
553                         break;
554                 case _CWORD:
555                         wordTRcpy(cmsg->m + cmsg->l, OFF);
556                         cmsg->l += 2;
557                         break;
558                 case _CDWORD:
559                         dwordTRcpy(cmsg->m + cmsg->l, OFF);
560                         cmsg->l += 4;
561                         break;
562                 case _CSTRUCT:
563                         *(u8 **) OFF = cmsg->m + cmsg->l;
564
565                         if (cmsg->m[cmsg->l] != 0xff)
566                                 cmsg->l += 1 + cmsg->m[cmsg->l];
567                         else
568                                 cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
569                         break;
570                 case _CMSTRUCT:
571 /*----- Metastruktur 0 -----*/
572                         if (cmsg->m[cmsg->l] == '\0') {
573                                 *(_cmstruct *) OFF = CAPI_DEFAULT;
574                                 cmsg->l++;
575                                 jumpcstruct(cmsg);
576                         } else {
577                                 unsigned _l = cmsg->l;
578                                 *(_cmstruct *) OFF = CAPI_COMPOSE;
579                                 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
580                                 cmsg->p++;
581                                 message_2_pars(cmsg);
582                         }
583                         break;
584                 }
585         }
586 }
587
588 /*-------------------------------------------------------*/
589 unsigned capi_message2cmsg(_cmsg * cmsg, u8 * msg)
590 {
591         memset(cmsg, 0, sizeof(_cmsg));
592         cmsg->m = msg;
593         cmsg->l = 8;
594         cmsg->p = 0;
595         byteTRcpy(cmsg->m + 4, &cmsg->Command);
596         byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
597         cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
598
599         message_2_pars(cmsg);
600
601         wordTRcpy(msg + 0, &cmsg->l);
602         wordTRcpy(cmsg->m + 2, &cmsg->ApplId);
603         wordTRcpy(cmsg->m + 6, &cmsg->Messagenumber);
604
605         return 0;
606 }
607
608 /*-------------------------------------------------------*/
609 unsigned capi_cmsg_header(_cmsg * cmsg, u16 _ApplId,
610                           u8 _Command, u8 _Subcommand,
611                           u16 _Messagenumber, u32 _Controller)
612 {
613         memset(cmsg, 0, sizeof(_cmsg));
614         cmsg->ApplId = _ApplId;
615         cmsg->Command = _Command;
616         cmsg->Subcommand = _Subcommand;
617         cmsg->Messagenumber = _Messagenumber;
618         cmsg->adr.adrController = _Controller;
619         return 0;
620 }
621
622 /*-------------------------------------------------------*/
623
624 static char *mnames[] =
625 {
626         0,
627         "ALERT_REQ",
628         "CONNECT_REQ",
629         0,
630         "DISCONNECT_REQ",
631         "LISTEN_REQ",
632         0,
633         0,
634         "INFO_REQ",
635         "FACILITY_REQ",
636         "SELECT_B_PROTOCOL_REQ",
637         "CONNECT_B3_REQ",
638         0,
639         "DISCONNECT_B3_REQ",
640         0,
641         "DATA_B3_REQ",
642         "RESET_B3_REQ",
643         0,
644         0,
645         "ALERT_CONF",
646         "CONNECT_CONF",
647         0,
648         "DISCONNECT_CONF",
649         "LISTEN_CONF",
650         "MANUFACTURER_REQ",
651         0,
652         "INFO_CONF",
653         "FACILITY_CONF",
654         "SELECT_B_PROTOCOL_CONF",
655         "CONNECT_B3_CONF",
656         0,
657         "DISCONNECT_B3_CONF",
658         0,
659         "DATA_B3_CONF",
660         "RESET_B3_CONF",
661         0,
662         0,
663         0,
664         "CONNECT_IND",
665         "CONNECT_ACTIVE_IND",
666         "DISCONNECT_IND",
667         0,
668         "MANUFACTURER_CONF",
669         0,
670         "INFO_IND",
671         "FACILITY_IND",
672         0,
673         "CONNECT_B3_IND",
674         "CONNECT_B3_ACTIVE_IND",
675         "DISCONNECT_B3_IND",
676         0,
677         "DATA_B3_IND",
678         "RESET_B3_IND",
679         "CONNECT_B3_T90_ACTIVE_IND",
680         0,
681         0,
682         "CONNECT_RESP",
683         "CONNECT_ACTIVE_RESP",
684         "DISCONNECT_RESP",
685         0,
686         "MANUFACTURER_IND",
687         0,
688         "INFO_RESP",
689         "FACILITY_RESP",
690         0,
691         "CONNECT_B3_RESP",
692         "CONNECT_B3_ACTIVE_RESP",
693         "DISCONNECT_B3_RESP",
694         0,
695         "DATA_B3_RESP",
696         "RESET_B3_RESP",
697         "CONNECT_B3_T90_ACTIVE_RESP",
698         0,
699         0,
700         0,
701         0,
702         0,
703         0,
704         "MANUFACTURER_RESP"
705 };
706
707 char *capi_cmd2str(u8 cmd, u8 subcmd)
708 {
709         return mnames[command_2_index(cmd, subcmd)];
710 }
711
712
713 /*-------------------------------------------------------*/
714 /*-------------------------------------------------------*/
715
716 static char *pnames[] =
717 {
718     /*00 */ 0,
719     /*01 */ 0,
720     /*02 */ 0,
721     /*03 */ "Controller/PLCI/NCCI",
722     /*04 */ "AdditionalInfo",
723     /*05 */ "B1configuration",
724     /*06 */ "B1protocol",
725     /*07 */ "B2configuration",
726     /*08 */ "B2protocol",
727     /*09 */ "B3configuration",
728     /*0a */ "B3protocol",
729     /*0b */ "BC",
730     /*0c */ "BChannelinformation",
731     /*0d */ "BProtocol",
732     /*0e */ "CalledPartyNumber",
733     /*0f */ "CalledPartySubaddress",
734     /*10 */ "CallingPartyNumber",
735     /*11 */ "CallingPartySubaddress",
736     /*12 */ "CIPmask",
737     /*13 */ "CIPmask2",
738     /*14 */ "CIPValue",
739     /*15 */ "Class",
740     /*16 */ "ConnectedNumber",
741     /*17 */ "ConnectedSubaddress",
742     /*18 */ "Data32",
743     /*19 */ "DataHandle",
744     /*1a */ "DataLength",
745     /*1b */ "FacilityConfirmationParameter",
746     /*1c */ "Facilitydataarray",
747     /*1d */ "FacilityIndicationParameter",
748     /*1e */ "FacilityRequestParameter",
749     /*1f */ "FacilitySelector",
750     /*20 */ "Flags",
751     /*21 */ "Function",
752     /*22 */ "HLC",
753     /*23 */ "Info",
754     /*24 */ "InfoElement",
755     /*25 */ "InfoMask",
756     /*26 */ "InfoNumber",
757     /*27 */ "Keypadfacility",
758     /*28 */ "LLC",
759     /*29 */ "ManuData",
760     /*2a */ "ManuID",
761     /*2b */ "NCPI",
762     /*2c */ "Reason",
763     /*2d */ "Reason_B3",
764     /*2e */ "Reject",
765     /*2f */ "Useruserdata"
766 };
767
768
769 static char buf[8192];
770 static char *p = 0;
771
772 #include <stdarg.h>
773
774 /*-------------------------------------------------------*/
775 static void bufprint(char *fmt,...)
776 {
777         va_list f;
778         va_start(f, fmt);
779         vsprintf(p, fmt, f);
780         va_end(f);
781         p += strlen(p);
782 }
783
784 static void printstructlen(u8 * m, unsigned len)
785 {
786         unsigned hex = 0;
787         for (; len; len--, m++)
788                 if (isalnum(*m) || *m == ' ') {
789                         if (hex)
790                                 bufprint(">");
791                         bufprint("%c", *m);
792                         hex = 0;
793                 } else {
794                         if (!hex)
795                                 bufprint("<%02x", *m);
796                         else
797                                 bufprint(" %02x", *m);
798                         hex = 1;
799                 }
800         if (hex)
801                 bufprint(">");
802 }
803
804 static void printstruct(u8 * m)
805 {
806         unsigned len;
807         if (m[0] != 0xff) {
808                 len = m[0];
809                 m += 1;
810         } else {
811                 len = ((u16 *) (m + 1))[0];
812                 m += 3;
813         }
814         printstructlen(m, len);
815 }
816
817 /*-------------------------------------------------------*/
818 #define NAME (pnames[cmsg->par[cmsg->p]])
819
820 static void protocol_message_2_pars(_cmsg * cmsg, int level)
821 {
822         for (; TYP != _CEND; cmsg->p++) {
823                 int slen = 29 + 3 - level;
824                 int i;
825
826                 bufprint("  ");
827                 for (i = 0; i < level - 1; i++)
828                         bufprint(" ");
829
830                 switch (TYP) {
831                 case _CBYTE:
832                         bufprint("%-*s = 0x%x\n", slen, NAME, *(u8 *) (cmsg->m + cmsg->l));
833                         cmsg->l++;
834                         break;
835                 case _CWORD:
836                         bufprint("%-*s = 0x%x\n", slen, NAME, *(u16 *) (cmsg->m + cmsg->l));
837                         cmsg->l += 2;
838                         break;
839                 case _CDWORD:
840                         bufprint("%-*s = 0x%lx\n", slen, NAME, *(u32 *) (cmsg->m + cmsg->l));
841                         cmsg->l += 4;
842                         break;
843                 case _CSTRUCT:
844                         bufprint("%-*s = ", slen, NAME);
845                         if (cmsg->m[cmsg->l] == '\0')
846                                 bufprint("default");
847                         else
848                                 printstruct(cmsg->m + cmsg->l);
849                         bufprint("\n");
850                         if (cmsg->m[cmsg->l] != 0xff)
851                                 cmsg->l += 1 + cmsg->m[cmsg->l];
852                         else
853                                 cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
854
855                         break;
856
857                 case _CMSTRUCT:
858 /*----- Metastruktur 0 -----*/
859                         if (cmsg->m[cmsg->l] == '\0') {
860                                 bufprint("%-*s = default\n", slen, NAME);
861                                 cmsg->l++;
862                                 jumpcstruct(cmsg);
863                         } else {
864                                 char *name = NAME;
865                                 unsigned _l = cmsg->l;
866                                 bufprint("%-*s\n", slen, name);
867                                 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
868                                 cmsg->p++;
869                                 protocol_message_2_pars(cmsg, level + 1);
870                         }
871                         break;
872                 }
873         }
874 }
875 /*-------------------------------------------------------*/
876 char *capi_message2str(u8 * msg)
877 {
878
879         _cmsg cmsg;
880         p = buf;
881         p[0] = 0;
882
883         cmsg.m = msg;
884         cmsg.l = 8;
885         cmsg.p = 0;
886         byteTRcpy(cmsg.m + 4, &cmsg.Command);
887         byteTRcpy(cmsg.m + 5, &cmsg.Subcommand);
888         cmsg.par = cpars[command_2_index(cmsg.Command, cmsg.Subcommand)];
889
890         bufprint("%-26s ID=%03d #0x%04x LEN=%04d\n",
891                  mnames[command_2_index(cmsg.Command, cmsg.Subcommand)],
892                  ((unsigned short *) msg)[1],
893                  ((unsigned short *) msg)[3],
894                  ((unsigned short *) msg)[0]);
895
896         protocol_message_2_pars(&cmsg, 1);
897         return buf;
898 }
899
900 char *capi_cmsg2str(_cmsg * cmsg)
901 {
902         p = buf;
903         p[0] = 0;
904         cmsg->l = 8;
905         cmsg->p = 0;
906         bufprint("%s ID=%03d #0x%04x LEN=%04d\n",
907                  mnames[command_2_index(cmsg->Command, cmsg->Subcommand)],
908                  ((u16 *) cmsg->m)[1],
909                  ((u16 *) cmsg->m)[3],
910                  ((u16 *) cmsg->m)[0]);
911         protocol_message_2_pars(cmsg, 1);
912         return buf;
913 }
914
915 EXPORT_SYMBOL(capi_cmsg2message);
916 EXPORT_SYMBOL(capi_message2cmsg);
917 EXPORT_SYMBOL(capi_cmsg_header);
918 EXPORT_SYMBOL(capi_cmd2str);
919 EXPORT_SYMBOL(capi_cmsg2str);
920 EXPORT_SYMBOL(capi_message2str);
921 EXPORT_SYMBOL(capi_info2str);