Merged revisions 63534 via svnmerge from
[asterisk/asterisk.git] / channels / misdn / isdn_msg_parser.c
1 /*
2  * Chan_Misdn -- Channel Driver for Asterisk
3  *
4  * Interface to mISDN
5  *
6  * Copyright (C) 2004, Christian Richter
7  *
8  * Christian Richter <crich@beronet.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14
15 #include "isdn_lib_intern.h"
16
17
18 #include "isdn_lib.h"
19
20 #include "ie.c"
21
22
23 static void set_channel(struct misdn_bchannel *bc, int channel)
24 {
25
26         cb_log(3,bc->port,"set_channel: bc->channel:%d channel:%d\n", bc->channel, channel);
27         
28         
29         if (channel==0xff) {
30                 /* any channel */
31                 channel=-1;
32         }
33         
34         /*  ALERT: is that everytime true ?  */
35         if (channel > 0 && bc->nt ) {
36                 
37                 if (bc->channel && ( bc->channel != 0xff) ) {
38                         cb_log(0,bc->port,"We already have a channel (%d)\n", bc->channel);
39                 } else {
40                         bc->channel = channel;
41                         cb_event(EVENT_NEW_CHANNEL,bc,NULL);
42                 }
43         }
44         
45         if (channel > 0 && !bc->nt ) {
46                 bc->channel = channel;
47                 cb_event(EVENT_NEW_CHANNEL,bc,NULL);
48         }
49 }
50
51 static void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
52 {
53         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
54         CALL_PROCEEDING_t *proceeding=(CALL_PROCEEDING_t*)((unsigned long)msg->data+ HEADER_LEN);
55         //struct misdn_stack *stack=get_stack_by_bc(bc);
56         
57         {
58                 int  exclusive, channel;
59                 dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)proceeding, &exclusive, &channel, nt,bc);
60
61                 set_channel(bc,channel);
62                 
63         }
64         
65         dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)proceeding, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
66         
67         
68 #ifdef DEBUG 
69         printf("Parsing PROCEEDING Msg\n"); 
70 #endif
71 }
72 static msg_t *build_proceeding (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
73 {
74         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
75         CALL_PROCEEDING_t *proceeding;
76         msg_t *msg =(msg_t*)create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING,  bc?bc->l3_id:-1, sizeof(CALL_PROCEEDING_t) ,nt); 
77   
78         proceeding=(CALL_PROCEEDING_t*)((msg->data+HEADER_LEN));
79
80         enc_ie_channel_id(&proceeding->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
81   
82         if (nt) 
83                 enc_ie_progress(&proceeding->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
84   
85
86 #ifdef DEBUG 
87         printf("Building PROCEEDING Msg\n"); 
88 #endif
89         return msg; 
90 }
91
92 static void parse_alerting (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
93 {
94         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; 
95         ALERTING_t *alerting=(ALERTING_t*)((unsigned long)(msg->data+HEADER_LEN));
96         //Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN);
97         
98         dec_ie_progress(alerting->PROGRESS, (Q931_info_t *)alerting, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
99         
100 #ifdef DEBUG 
101         printf("Parsing ALERTING Msg\n"); 
102 #endif
103
104  
105 }
106
107 static msg_t *build_alerting (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
108 {
109         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
110         ALERTING_t *alerting;
111         msg_t *msg =(msg_t*)create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING,  bc?bc->l3_id:-1, sizeof(ALERTING_t) ,nt); 
112   
113         alerting=(ALERTING_t*)((msg->data+HEADER_LEN)); 
114   
115         enc_ie_channel_id(&alerting->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
116         
117         if (nt) 
118                 enc_ie_progress(&alerting->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
119 #ifdef DEBUG 
120         printf("Building ALERTING Msg\n"); 
121 #endif
122         return msg; 
123 }
124
125
126 static void parse_progress (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
127 {
128         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
129         PROGRESS_t *progress=(PROGRESS_t*)((unsigned long)(msg->data+HEADER_LEN)); 
130         //Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN);  
131         
132         dec_ie_progress(progress->PROGRESS, (Q931_info_t *)progress, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
133         
134 #ifdef DEBUG 
135         printf("Parsing PROGRESS Msg\n"); 
136 #endif
137 }
138
139 static msg_t *build_progress (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
140 {
141         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
142         PROGRESS_t *progress;
143         msg_t *msg =(msg_t*)create_l3msg(CC_PROGRESS | REQUEST, MT_PROGRESS,  bc?bc->l3_id:-1, sizeof(PROGRESS_t) ,nt); 
144  
145         progress=(PROGRESS_t*)((msg->data+HEADER_LEN)); 
146
147 #ifdef DEBUG 
148         printf("Building PROGRESS Msg\n"); 
149 #endif
150         return msg; 
151 }
152
153 static void parse_setup (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
154
155         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
156         SETUP_t *setup= (SETUP_t*)((unsigned long)msg->data+HEADER_LEN);
157         Q931_info_t *qi=(Q931_info_t*)((unsigned long)msg->data+HEADER_LEN);
158
159 #ifdef DEBUG 
160         printf("Parsing SETUP Msg\n"); 
161 #endif
162         {
163                 int type,plan,present, screen;
164                 char id[32];
165                 dec_ie_calling_pn(setup->CALLING_PN, qi, &type, &plan, &present, &screen, id, sizeof(id)-1, nt,bc);
166
167                 bc->onumplan=type; 
168                 strcpy(bc->oad, id);
169                 switch (present) {
170                 case 0:
171                         bc->pres=0; /* screened */
172                         break;
173                 case 1:
174                         bc->pres=1; /* not screened */
175                         break;
176                 default:
177                         bc->pres=0;
178                 }
179                 switch (screen) {
180                 case 0:
181                         break;
182                 default:
183                         ;
184                 } 
185         }
186         {
187                 int  type, plan;
188                 char number[32]; 
189                 dec_ie_called_pn(setup->CALLED_PN, (Q931_info_t *)setup, &type, &plan, number, sizeof(number)-1, nt,bc);
190                 strcpy(bc->dad, number);
191                 bc->dnumplan=type; 
192         }
193         {
194                 char keypad[32];
195                 dec_ie_keypad(setup->KEYPAD, (Q931_info_t *)setup, keypad, sizeof(keypad)-1, nt,bc);
196                 strcpy(bc->keypad, keypad);
197         }
198
199         {
200                 dec_ie_complete(setup->COMPLETE, (Q931_info_t *)setup, &bc->sending_complete, nt,bc);
201                 
202         }
203   
204         {
205                 int  type, plan, present, screen, reason;
206                 char id[32]; 
207                 dec_ie_redir_nr(setup->REDIR_NR, (Q931_info_t *)setup, &type, &plan, &present, &screen, &reason, id, sizeof(id)-1, nt,bc);
208     
209                 strcpy(bc->rad, id);
210                 bc->rnumplan=type; 
211         }
212         {
213                 int  coding, capability, mode, rate, multi, user, async, urate, stopbits, dbits, parity;
214                 dec_ie_bearer(setup->BEARER, (Q931_info_t *)setup, &coding, &capability, &mode, &rate, &multi, &user, &async, &urate, &stopbits, &dbits, &parity, nt,bc);
215                 switch (capability) {
216                 case -1: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; 
217                         break;
218                 case 0: bc->capability=INFO_CAPABILITY_SPEECH;
219                         break;
220                 case 18: bc->capability=INFO_CAPABILITY_VIDEO;
221                         break;
222                 case 8: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
223                         bc->user1 = user;
224                         bc->urate = urate;
225                         
226                         bc->rate = rate;
227                         bc->mode = mode;
228                         break;
229                 case 9: bc->capability=INFO_CAPABILITY_DIGITAL_RESTRICTED;
230                         break;
231                 default:
232                         break;
233                 }
234                 
235                 switch(user) {
236                 case 2:
237                         bc->law=INFO_CODEC_ULAW;
238                         break;
239                 case 3:
240                         bc->law=INFO_CODEC_ALAW;
241                         break;
242                 default:
243                         bc->law=INFO_CODEC_ALAW;
244                         
245                 }
246                 
247                 bc->capability=capability; 
248         }
249         {
250                 int  exclusive, channel;
251                 dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)setup, &exclusive, &channel, nt,bc);
252                 
253                 set_channel(bc,channel);
254         }
255
256         {
257                 int  protocol ;
258                 dec_ie_useruser(setup->USER_USER, (Q931_info_t *)setup, &protocol, bc->uu, &bc->uulen, nt,bc);
259                 if (bc->uulen) cb_log(1,bc->port,"USERUESRINFO:%s\n",bc->uu);
260                 else
261                 cb_log(1,bc->port,"NO USERUESRINFO\n");
262         }
263         
264         dec_ie_progress(setup->PROGRESS, (Q931_info_t *)setup, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
265         
266 }
267
268 #define ANY_CHANNEL 0xff /* IE attribut for 'any channel' */
269 static msg_t *build_setup (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
270 {
271         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
272         SETUP_t *setup;
273         msg_t *msg =(msg_t*)create_l3msg(CC_SETUP | REQUEST, MT_SETUP,  bc?bc->l3_id:-1, sizeof(SETUP_t) ,nt); 
274   
275         setup=(SETUP_t*)((msg->data+HEADER_LEN)); 
276   
277         if (bc->channel == 0 || bc->channel == ANY_CHANNEL || bc->channel==-1)
278                 enc_ie_channel_id(&setup->CHANNEL_ID, msg, 0, bc->channel, nt,bc);
279         else 
280                 enc_ie_channel_id(&setup->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
281         
282         
283         {
284                 int type=bc->onumplan,plan=1,present=bc->pres,screen=bc->screen;
285                 enc_ie_calling_pn(&setup->CALLING_PN, msg, type, plan, present,
286                                   screen, bc->oad, nt, bc);
287         }
288   
289         {
290                 if (bc->dad[0])
291                         enc_ie_called_pn(&setup->CALLED_PN, msg, bc->dnumplan, 1, bc->dad, nt,bc);
292         }
293
294         {
295                 if (bc->rad[0])
296                         enc_ie_redir_nr(&setup->REDIR_NR, msg, 1, 1,  bc->pres, bc->screen, 0, bc->rad, nt,bc);
297         }
298
299         {
300                 if (bc->keypad[0])
301                         enc_ie_keypad(&setup->KEYPAD, msg, bc->keypad, nt,bc);
302         }
303         
304   
305         if (*bc->display) {
306                 enc_ie_display(&setup->DISPLAY, msg, bc->display, nt,bc);
307         }
308   
309         {
310                 int coding=0, capability, mode=0 /*  2 for packet ! */
311                         ,user, rate=0x10;
312
313                 switch (bc->law) {
314                 case INFO_CODEC_ULAW: user=2;
315                         break;
316                 case INFO_CODEC_ALAW: user=3;
317                         break;
318                 default:
319                         user=3;
320                 }
321                 
322                 switch (bc->capability) {
323                 case INFO_CAPABILITY_SPEECH: capability = 0;
324                         break;
325                 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: capability = 8;
326                         user=-1;
327                         mode=bc->mode;
328                         rate=bc->rate;
329                         break;
330                 case INFO_CAPABILITY_DIGITAL_RESTRICTED: capability = 9;
331                         user=-1;
332                         break;
333                 default:
334                         capability=bc->capability; 
335                 }
336                 
337                 
338     
339                 enc_ie_bearer(&setup->BEARER, msg, coding, capability, mode, rate, -1, user, nt,bc);
340         }
341
342         if (bc->sending_complete) {
343                 enc_ie_complete(&setup->BEARER,msg, bc->sending_complete, nt, bc);
344         }
345   
346         {
347                 int  protocol=4;
348                 enc_ie_useruser(&setup->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
349                 if (bc->uulen) cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
350                 else
351                 cb_log(1,bc->port,"NO USERUESRINFO ENCODED\n");
352         }
353
354 #if DEBUG 
355         printf("Building SETUP Msg\n"); 
356 #endif
357         return msg; 
358 }
359
360 static void parse_connect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
361 {
362         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
363         CONNECT_t *connect=(CONNECT_t*)((unsigned long)(msg->data+HEADER_LEN));
364   
365         int plan,pres,screen;
366         
367         bc->ces = connect->ces;
368         bc->ces = connect->ces;
369
370         dec_ie_progress(connect->PROGRESS, (Q931_info_t *)connect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
371
372         dec_ie_connected_pn(connect->CONNECT_PN,(Q931_info_t *)connect, &bc->cpnnumplan, &plan, &pres, &screen, bc->cad, 31, nt, bc);
373
374         /*
375                 cb_log(1,bc->port,"CONNETED PN: %s cpn_dialplan:%d\n", connected_pn, type);
376         */
377         
378 #ifdef DEBUG 
379         printf("Parsing CONNECT Msg\n"); 
380 #endif
381 }
382
383 static msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
384 {
385         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
386         CONNECT_t *connect;
387         msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_t) ,nt); 
388         
389         cb_log(6,bc->port,"BUILD_CONNECT: bc:%p bc->l3id:%d, nt:%d\n",bc,bc->l3_id,nt);
390
391         connect=(CONNECT_t*)((msg->data+HEADER_LEN)); 
392
393         if (nt) {
394                 time_t now;
395                 time(&now);
396                 enc_ie_date(&connect->DATE, msg, now, nt,bc);
397         }
398   
399         {
400                 int type=bc->cpnnumplan, plan=1, present=2, screen=0;
401                 enc_ie_connected_pn(&connect->CONNECT_PN, msg, type,plan, present, screen, bc->cad, nt , bc);
402         }
403
404 #ifdef DEBUG 
405         printf("Building CONNECT Msg\n"); 
406 #endif
407         return msg; 
408 }
409
410 static void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
411 {
412         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
413         SETUP_ACKNOWLEDGE_t *setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((unsigned long)(msg->data+HEADER_LEN));
414
415         {
416                 int  exclusive, channel;
417                 dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)setup_acknowledge, &exclusive, &channel, nt,bc);
418
419
420                 set_channel(bc, channel);
421         }
422         
423         dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)setup_acknowledge, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
424 #ifdef DEBUG 
425         printf("Parsing SETUP_ACKNOWLEDGE Msg\n"); 
426 #endif
427
428  
429 }
430
431 static msg_t *build_setup_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
432 {
433         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
434         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
435         msg_t *msg =(msg_t*)create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SETUP_ACKNOWLEDGE_t) ,nt); 
436  
437         setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
438   
439         enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
440   
441         if (nt) 
442                 enc_ie_progress(&setup_acknowledge->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
443   
444 #ifdef DEBUG 
445         printf("Building SETUP_ACKNOWLEDGE Msg\n"); 
446 #endif
447         return msg; 
448 }
449
450 static void parse_connect_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
451 {
452 #ifdef DEBUG 
453         printf("Parsing CONNECT_ACKNOWLEDGE Msg\n"); 
454 #endif
455
456  
457 }
458
459 static msg_t *build_connect_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
460 {
461         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
462         CONNECT_ACKNOWLEDGE_t *connect_acknowledge;
463         msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_ACKNOWLEDGE_t) ,nt); 
464  
465         connect_acknowledge=(CONNECT_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
466   
467         enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
468   
469 #ifdef DEBUG 
470         printf("Building CONNECT_ACKNOWLEDGE Msg\n"); 
471 #endif
472         return msg; 
473 }
474
475 static void parse_user_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
476 {
477 #ifdef DEBUG 
478         printf("Parsing USER_INFORMATION Msg\n"); 
479 #endif
480
481  
482 }
483
484 static msg_t *build_user_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
485 {
486         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
487         USER_INFORMATION_t *user_information;
488         msg_t *msg =(msg_t*)create_l3msg(CC_USER_INFORMATION | REQUEST, MT_USER_INFORMATION,  bc?bc->l3_id:-1, sizeof(USER_INFORMATION_t) ,nt); 
489  
490         user_information=(USER_INFORMATION_t*)((msg->data+HEADER_LEN)); 
491
492 #ifdef DEBUG 
493         printf("Building USER_INFORMATION Msg\n"); 
494 #endif
495         return msg; 
496 }
497
498 static void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
499 {
500 #ifdef DEBUG 
501         printf("Parsing SUSPEND_REJECT Msg\n"); 
502 #endif
503
504  
505 }
506
507 static msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
508 {
509         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
510         SUSPEND_REJECT_t *suspend_reject;
511         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT,  bc?bc->l3_id:-1, sizeof(SUSPEND_REJECT_t) ,nt); 
512  
513         suspend_reject=(SUSPEND_REJECT_t*)((msg->data+HEADER_LEN)); 
514
515 #ifdef DEBUG 
516         printf("Building SUSPEND_REJECT Msg\n"); 
517 #endif
518         return msg; 
519 }
520
521 static void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
522 {
523 #ifdef DEBUG 
524         printf("Parsing RESUME_REJECT Msg\n"); 
525 #endif
526
527  
528 }
529
530 static msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
531 {
532         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
533         RESUME_REJECT_t *resume_reject;
534         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT,  bc?bc->l3_id:-1, sizeof(RESUME_REJECT_t) ,nt); 
535  
536         resume_reject=(RESUME_REJECT_t*)((msg->data+HEADER_LEN)); 
537
538 #ifdef DEBUG 
539         printf("Building RESUME_REJECT Msg\n"); 
540 #endif
541         return msg; 
542 }
543
544 static void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
545 {
546 #ifdef DEBUG 
547         printf("Parsing HOLD Msg\n"); 
548 #endif
549
550  
551 }
552
553 static msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
554 {
555         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
556         HOLD_t *hold;
557         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD | REQUEST, MT_HOLD,  bc?bc->l3_id:-1, sizeof(HOLD_t) ,nt); 
558  
559         hold=(HOLD_t*)((msg->data+HEADER_LEN)); 
560
561 #ifdef DEBUG 
562         printf("Building HOLD Msg\n"); 
563 #endif
564         return msg; 
565 }
566
567 static void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
568 {
569 #ifdef DEBUG 
570         printf("Parsing SUSPEND Msg\n"); 
571 #endif
572
573  
574 }
575
576 static msg_t *build_suspend (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
577 {
578         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
579         SUSPEND_t *suspend;
580         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND | REQUEST, MT_SUSPEND,  bc?bc->l3_id:-1, sizeof(SUSPEND_t) ,nt); 
581  
582         suspend=(SUSPEND_t*)((msg->data+HEADER_LEN)); 
583
584 #ifdef DEBUG 
585         printf("Building SUSPEND Msg\n"); 
586 #endif
587         return msg; 
588 }
589
590 static void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
591 {
592 #ifdef DEBUG 
593         printf("Parsing RESUME Msg\n"); 
594 #endif
595
596  
597 }
598
599 static msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
600 {
601         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
602         RESUME_t *resume;
603         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME | REQUEST, MT_RESUME,  bc?bc->l3_id:-1, sizeof(RESUME_t) ,nt); 
604  
605         resume=(RESUME_t*)((msg->data+HEADER_LEN)); 
606
607 #ifdef DEBUG 
608         printf("Building RESUME Msg\n"); 
609 #endif
610         return msg; 
611 }
612
613 static void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
614 {
615 #ifdef DEBUG 
616         printf("Parsing HOLD_ACKNOWLEDGE Msg\n"); 
617 #endif
618
619  
620 }
621
622 static msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
623 {
624         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
625         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
626         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(HOLD_ACKNOWLEDGE_t) ,nt); 
627  
628         hold_acknowledge=(HOLD_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
629
630 #ifdef DEBUG 
631         printf("Building HOLD_ACKNOWLEDGE Msg\n"); 
632 #endif
633         return msg; 
634 }
635
636 static void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
637 {
638 #ifdef DEBUG 
639         printf("Parsing SUSPEND_ACKNOWLEDGE Msg\n"); 
640 #endif
641
642  
643 }
644
645 static msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
646 {
647         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
648         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
649         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SUSPEND_ACKNOWLEDGE_t) ,nt); 
650  
651         suspend_acknowledge=(SUSPEND_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
652
653 #ifdef DEBUG 
654         printf("Building SUSPEND_ACKNOWLEDGE Msg\n"); 
655 #endif
656         return msg; 
657 }
658
659 static void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
660 {
661 #ifdef DEBUG 
662         printf("Parsing RESUME_ACKNOWLEDGE Msg\n"); 
663 #endif
664
665  
666 }
667
668 static msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
669 {
670         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
671         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
672         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RESUME_ACKNOWLEDGE_t) ,nt); 
673  
674         resume_acknowledge=(RESUME_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
675
676 #ifdef DEBUG 
677         printf("Building RESUME_ACKNOWLEDGE Msg\n"); 
678 #endif
679         return msg; 
680 }
681
682 static void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
683 {
684 #ifdef DEBUG 
685         printf("Parsing HOLD_REJECT Msg\n"); 
686 #endif
687
688  
689 }
690
691 static msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
692 {
693         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
694         HOLD_REJECT_t *hold_reject;
695         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT,  bc?bc->l3_id:-1, sizeof(HOLD_REJECT_t) ,nt); 
696  
697         hold_reject=(HOLD_REJECT_t*)((msg->data+HEADER_LEN)); 
698
699 #ifdef DEBUG 
700         printf("Building HOLD_REJECT Msg\n"); 
701 #endif
702         return msg; 
703 }
704
705 static void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
706 {
707 #ifdef DEBUG 
708         printf("Parsing RETRIEVE Msg\n"); 
709 #endif
710
711  
712 }
713
714 static msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
715 {
716         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
717         RETRIEVE_t *retrieve;
718         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE | REQUEST, MT_RETRIEVE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_t) ,nt); 
719  
720         retrieve=(RETRIEVE_t*)((msg->data+HEADER_LEN)); 
721
722 #ifdef DEBUG 
723         printf("Building RETRIEVE Msg\n"); 
724 #endif
725         return msg; 
726 }
727
728 static void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
729 {
730 #ifdef DEBUG 
731         printf("Parsing RETRIEVE_ACKNOWLEDGE Msg\n"); 
732 #endif
733
734  
735 }
736
737 static msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
738 {
739         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
740         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
741         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_ACKNOWLEDGE_t) ,nt); 
742  
743         retrieve_acknowledge=(RETRIEVE_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
744
745         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
746 #ifdef DEBUG 
747         printf("Building RETRIEVE_ACKNOWLEDGE Msg\n"); 
748 #endif
749         return msg; 
750 }
751
752 static void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
753 {
754 #ifdef DEBUG 
755         printf("Parsing RETRIEVE_REJECT Msg\n"); 
756 #endif
757
758  
759 }
760
761 static msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
762 {
763         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
764         RETRIEVE_REJECT_t *retrieve_reject;
765         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT,  bc?bc->l3_id:-1, sizeof(RETRIEVE_REJECT_t) ,nt); 
766  
767         retrieve_reject=(RETRIEVE_REJECT_t*)((msg->data+HEADER_LEN)); 
768
769 #ifdef DEBUG 
770         printf("Building RETRIEVE_REJECT Msg\n"); 
771 #endif
772         return msg; 
773 }
774
775 static void parse_disconnect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
776 {
777         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
778         DISCONNECT_t *disconnect=(DISCONNECT_t*)((unsigned long)(msg->data+HEADER_LEN));
779         int location;
780         int cause; 
781         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &cause, nt,bc);
782         if (cause>0) bc->cause=cause;
783
784         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)disconnect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
785 #ifdef DEBUG 
786         printf("Parsing DISCONNECT Msg\n"); 
787 #endif
788
789  
790 }
791
792 static msg_t *build_disconnect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
793 {
794         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
795         DISCONNECT_t *disconnect;
796         msg_t *msg =(msg_t*)create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT,  bc?bc->l3_id:-1, sizeof(DISCONNECT_t) ,nt); 
797         
798         disconnect=(DISCONNECT_t*)((msg->data+HEADER_LEN)); 
799         
800         enc_ie_cause(&disconnect->CAUSE, msg, (nt)?1:0, bc->out_cause,nt,bc);
801         if (nt) enc_ie_progress(&disconnect->PROGRESS, msg, 0, nt?1:5, 8 ,nt,bc);
802   
803 #ifdef DEBUG 
804         printf("Building DISCONNECT Msg\n"); 
805 #endif
806         return msg; 
807 }
808
809 static void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
810 {
811         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
812         RESTART_t *restart=(RESTART_t*)((unsigned long)(msg->data+HEADER_LEN));
813
814         struct misdn_stack *stack=get_stack_by_bc(bc);
815         
816 #ifdef DEBUG 
817         printf("Parsing RESTART Msg\n");
818 #endif
819   
820         {
821                 int  exclusive, channel = 0;
822                 dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &bc->restart_channel, nt,bc);
823                 /* XXX: this is broken... channel is not used */
824                 if (channel==0xff) /* any channel */
825                         channel=-1;
826                 cb_log(3, stack->port, "CC_RESTART Request on channel:%d on this port.\n");
827         }
828   
829  
830 }
831
832 static msg_t *build_restart (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
833 {
834         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
835         RESTART_t *restart;
836         msg_t *msg =(msg_t*)create_l3msg(CC_RESTART | REQUEST, MT_RESTART,  bc?bc->l3_id:-1, sizeof(RESTART_t) ,nt); 
837  
838         restart=(RESTART_t*)((msg->data+HEADER_LEN)); 
839
840 #ifdef DEBUG 
841         printf("Building RESTART Msg\n"); 
842 #endif
843         return msg; 
844 }
845
846 static void parse_release (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
847 {
848         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
849         RELEASE_t *release=(RELEASE_t*)((unsigned long)(msg->data+HEADER_LEN));
850         int location;
851         int cause;
852   
853         dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &cause, nt,bc);
854         if (cause>0) bc->cause=cause;
855 #ifdef DEBUG 
856         printf("Parsing RELEASE Msg\n"); 
857 #endif
858
859  
860 }
861
862 static msg_t *build_release (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
863 {
864         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
865         RELEASE_t *release;
866         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE,  bc?bc->l3_id:-1, sizeof(RELEASE_t) ,nt); 
867  
868         release=(RELEASE_t*)((msg->data+HEADER_LEN)); 
869   
870         if (bc->out_cause>= 0)
871                 enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
872   
873 #ifdef DEBUG 
874         printf("Building RELEASE Msg\n"); 
875 #endif
876         return msg; 
877 }
878
879 static void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
880 {
881         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
882         RELEASE_COMPLETE_t *release_complete=(RELEASE_COMPLETE_t*)((unsigned long)(msg->data+HEADER_LEN));
883         int location;
884         int cause;
885         iframe_t *frm = (iframe_t*) msg->data;
886
887         struct misdn_stack *stack=get_stack_by_bc(bc);
888         mISDNuser_head_t *hh;
889         hh=(mISDNuser_head_t*)msg->data;
890
891         /*hh=(mISDN_head_t*)msg->data;
892         mISDN_head_t *hh;*/
893
894         if (nt) {
895                 if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
896                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] \n");
897                         return;
898                 }
899         } else {
900                 if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
901                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] \n");
902                         return;
903                 }
904         }
905         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &cause, nt,bc);
906         if (cause>0) bc->cause=cause;
907
908 #ifdef DEBUG 
909         printf("Parsing RELEASE_COMPLETE Msg\n"); 
910 #endif
911 }
912
913 static msg_t *build_release_complete (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
914 {
915         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
916         RELEASE_COMPLETE_t *release_complete;
917         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE,  bc?bc->l3_id:-1, sizeof(RELEASE_COMPLETE_t) ,nt); 
918  
919         release_complete=(RELEASE_COMPLETE_t*)((msg->data+HEADER_LEN)); 
920         
921         enc_ie_cause(&release_complete->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
922   
923 #ifdef DEBUG 
924         printf("Building RELEASE_COMPLETE Msg\n"); 
925 #endif
926         return msg; 
927 }
928
929 static void parse_facility (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
930 {
931         int HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
932         FACILITY_t *facility = (FACILITY_t*)(msg->data+HEADER_LEN); 
933         Q931_info_t *qi = (Q931_info_t*)(msg->data+HEADER_LEN);  
934         unsigned char *p = NULL;
935         int err;
936
937 #ifdef DEBUG 
938         printf("Parsing FACILITY Msg\n"); 
939 #endif
940
941         if (!bc->nt) {
942                 if (qi->QI_ELEMENT(facility))
943                         p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
944         } else {
945                 p = facility->FACILITY;
946         }
947         if (!p)
948                 return;
949         
950         err = decodeFac(p, &(bc->fac_in));
951         if (err) {
952                 cb_log(1, bc->port, "Decoding FACILITY failed! (%d)\n", err);
953         }
954 }
955
956 static msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
957 {
958         int len,
959                 HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
960         unsigned char *ie_fac,
961                                   fac_tmp[256];
962         msg_t *msg =(msg_t*)create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY,  bc?bc->l3_id:-1, sizeof(FACILITY_t) ,nt);
963         FACILITY_t *facility = (FACILITY_t*)(msg->data+HEADER_LEN); 
964         Q931_info_t *qi;
965
966 #ifdef DEBUG 
967         printf("Building FACILITY Msg\n"); 
968 #endif
969         
970         len = encodeFac(fac_tmp, &(bc->fac_out));
971         if (len <= 0)
972                 return NULL;
973
974         ie_fac = msg_put(msg, len);
975         if (bc->nt) {
976                 facility->FACILITY = ie_fac + 1;
977         } else {
978                 qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
979                 qi->QI_ELEMENT(facility) = ie_fac - (unsigned char *)qi - sizeof(Q931_info_t);
980         }
981
982         memcpy(ie_fac, fac_tmp, len);
983
984         if (*bc->display) {
985                 printf("Sending %s as Display\n", bc->display);
986                 enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc);
987         }
988
989         return msg; 
990 }
991
992 static void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
993 {
994 #ifdef DEBUG 
995         printf("Parsing NOTIFY Msg\n"); 
996 #endif
997 }
998
999 static msg_t *build_notify (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1000 {
1001         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1002         NOTIFY_t *notify;
1003         msg_t *msg =(msg_t*)create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY,  bc?bc->l3_id:-1, sizeof(NOTIFY_t) ,nt); 
1004  
1005         notify=(NOTIFY_t*)((msg->data+HEADER_LEN)); 
1006
1007 #ifdef DEBUG 
1008         printf("Building NOTIFY Msg\n"); 
1009 #endif
1010         return msg; 
1011 }
1012
1013 static void parse_status_enquiry (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1014 {
1015 #ifdef DEBUG 
1016         printf("Parsing STATUS_ENQUIRY Msg\n"); 
1017 #endif
1018 }
1019
1020 static msg_t *build_status_enquiry (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1021 {
1022         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1023         STATUS_ENQUIRY_t *status_enquiry;
1024         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS_ENQUIRY | REQUEST, MT_STATUS_ENQUIRY,  bc?bc->l3_id:-1, sizeof(STATUS_ENQUIRY_t) ,nt); 
1025  
1026         status_enquiry=(STATUS_ENQUIRY_t*)((msg->data+HEADER_LEN)); 
1027
1028 #ifdef DEBUG 
1029         printf("Building STATUS_ENQUIRY Msg\n"); 
1030 #endif
1031         return msg; 
1032 }
1033
1034 static void parse_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1035 {
1036         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1037         INFORMATION_t *information=(INFORMATION_t*)((unsigned long)(msg->data+HEADER_LEN));
1038         {
1039                 int  type, plan;
1040                 char number[32];
1041                 char keypad[32];
1042                 dec_ie_called_pn(information->CALLED_PN, (Q931_info_t *)information, &type, &plan, number, sizeof(number)-1, nt, bc);
1043                 dec_ie_keypad(information->KEYPAD, (Q931_info_t *)information, keypad, sizeof(keypad)-1, nt, bc);
1044                 strcpy(bc->info_dad, number);
1045                 strcpy(bc->keypad,keypad);
1046         }
1047 #ifdef DEBUG 
1048         printf("Parsing INFORMATION Msg\n"); 
1049 #endif
1050 }
1051
1052 static msg_t *build_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1053 {
1054         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1055         INFORMATION_t *information;
1056         msg_t *msg =(msg_t*)create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION,  bc?bc->l3_id:-1, sizeof(INFORMATION_t) ,nt); 
1057  
1058         information=(INFORMATION_t*)((msg->data+HEADER_LEN)); 
1059   
1060         {
1061                 enc_ie_called_pn(&information->CALLED_PN, msg, 0, 1, bc->info_dad, nt,bc);
1062         }
1063
1064         {
1065                 if (*bc->display) {
1066                         printf("Sending %s as Display\n", bc->display);
1067                         enc_ie_display(&information->DISPLAY, msg, bc->display, nt,bc);
1068                 }
1069         }
1070   
1071 #ifdef DEBUG 
1072         printf("Building INFORMATION Msg\n"); 
1073 #endif
1074         return msg; 
1075 }
1076
1077 static void parse_status (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1078 {
1079         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1080         STATUS_t *status=(STATUS_t*)((unsigned long)(msg->data+HEADER_LEN));
1081         int location;
1082         int cause;
1083   
1084         dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &cause, nt,bc);
1085         if (cause>0) bc->cause=cause;
1086         ;
1087
1088 #ifdef DEBUG 
1089         printf("Parsing STATUS Msg\n"); 
1090 #endif
1091 }
1092
1093 static msg_t *build_status (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1094 {
1095         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1096         STATUS_t *status;
1097         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt); 
1098  
1099         status=(STATUS_t*)((msg->data+HEADER_LEN)); 
1100
1101 #ifdef DEBUG 
1102         printf("Building STATUS Msg\n"); 
1103 #endif
1104         return msg; 
1105 }
1106
1107 static void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1108 {
1109 #ifdef DEBUG 
1110         printf("Parsing STATUS Msg\n"); 
1111 #endif 
1112 }
1113
1114 static msg_t *build_timeout (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1115 {
1116         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1117         STATUS_t *status;
1118         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt); 
1119  
1120         status=(STATUS_t*)((msg->data+HEADER_LEN)); 
1121
1122 #ifdef DEBUG 
1123         printf("Building STATUS Msg\n"); 
1124 #endif
1125         return msg; 
1126 }
1127
1128
1129 /************************************/
1130
1131
1132
1133
1134 /** Msg Array **/
1135
1136 struct isdn_msg msgs_g[] = {
1137         {CC_PROCEEDING,L3,EVENT_PROCEEDING,
1138          parse_proceeding,build_proceeding,
1139          "PROCEEDING"},
1140         {CC_ALERTING,L3,EVENT_ALERTING,
1141          parse_alerting,build_alerting,
1142          "ALERTING"},
1143         {CC_PROGRESS,L3,EVENT_PROGRESS,
1144          parse_progress,build_progress,
1145          "PROGRESS"},
1146         {CC_SETUP,L3,EVENT_SETUP,
1147          parse_setup,build_setup,
1148          "SETUP"},
1149         {CC_CONNECT,L3,EVENT_CONNECT,
1150          parse_connect,build_connect,
1151          "CONNECT"},
1152         {CC_SETUP_ACKNOWLEDGE,L3,EVENT_SETUP_ACKNOWLEDGE,
1153          parse_setup_acknowledge,build_setup_acknowledge,
1154          "SETUP_ACKNOWLEDGE"},
1155         {CC_CONNECT_ACKNOWLEDGE ,L3,EVENT_CONNECT_ACKNOWLEDGE ,
1156          parse_connect_acknowledge ,build_connect_acknowledge,
1157          "CONNECT_ACKNOWLEDGE "},
1158         {CC_USER_INFORMATION,L3,EVENT_USER_INFORMATION,
1159          parse_user_information,build_user_information,
1160          "USER_INFORMATION"},
1161         {CC_SUSPEND_REJECT,L3,EVENT_SUSPEND_REJECT,
1162          parse_suspend_reject,build_suspend_reject,
1163          "SUSPEND_REJECT"},
1164         {CC_RESUME_REJECT,L3,EVENT_RESUME_REJECT,
1165          parse_resume_reject,build_resume_reject,
1166          "RESUME_REJECT"},
1167         {CC_HOLD,L3,EVENT_HOLD,
1168          parse_hold,build_hold,
1169          "HOLD"},
1170         {CC_SUSPEND,L3,EVENT_SUSPEND,
1171          parse_suspend,build_suspend,
1172          "SUSPEND"},
1173         {CC_RESUME,L3,EVENT_RESUME,
1174          parse_resume,build_resume,
1175          "RESUME"},
1176         {CC_HOLD_ACKNOWLEDGE,L3,EVENT_HOLD_ACKNOWLEDGE,
1177          parse_hold_acknowledge,build_hold_acknowledge,
1178          "HOLD_ACKNOWLEDGE"},
1179         {CC_SUSPEND_ACKNOWLEDGE,L3,EVENT_SUSPEND_ACKNOWLEDGE,
1180          parse_suspend_acknowledge,build_suspend_acknowledge,
1181          "SUSPEND_ACKNOWLEDGE"},
1182         {CC_RESUME_ACKNOWLEDGE,L3,EVENT_RESUME_ACKNOWLEDGE,
1183          parse_resume_acknowledge,build_resume_acknowledge,
1184          "RESUME_ACKNOWLEDGE"},
1185         {CC_HOLD_REJECT,L3,EVENT_HOLD_REJECT,
1186          parse_hold_reject,build_hold_reject,
1187          "HOLD_REJECT"},
1188         {CC_RETRIEVE,L3,EVENT_RETRIEVE,
1189          parse_retrieve,build_retrieve,
1190          "RETRIEVE"},
1191         {CC_RETRIEVE_ACKNOWLEDGE,L3,EVENT_RETRIEVE_ACKNOWLEDGE,
1192          parse_retrieve_acknowledge,build_retrieve_acknowledge,
1193          "RETRIEVE_ACKNOWLEDGE"},
1194         {CC_RETRIEVE_REJECT,L3,EVENT_RETRIEVE_REJECT,
1195          parse_retrieve_reject,build_retrieve_reject,
1196          "RETRIEVE_REJECT"},
1197         {CC_DISCONNECT,L3,EVENT_DISCONNECT,
1198          parse_disconnect,build_disconnect,
1199          "DISCONNECT"},
1200         {CC_RESTART,L3,EVENT_RESTART,
1201          parse_restart,build_restart,
1202          "RESTART"},
1203         {CC_RELEASE,L3,EVENT_RELEASE,
1204          parse_release,build_release,
1205          "RELEASE"},
1206         {CC_RELEASE_COMPLETE,L3,EVENT_RELEASE_COMPLETE,
1207          parse_release_complete,build_release_complete,
1208          "RELEASE_COMPLETE"},
1209         {CC_FACILITY,L3,EVENT_FACILITY,
1210          parse_facility,build_facility,
1211          "FACILITY"},
1212         {CC_NOTIFY,L3,EVENT_NOTIFY,
1213          parse_notify,build_notify,
1214          "NOTIFY"},
1215         {CC_STATUS_ENQUIRY,L3,EVENT_STATUS_ENQUIRY,
1216          parse_status_enquiry,build_status_enquiry,
1217          "STATUS_ENQUIRY"},
1218         {CC_INFORMATION,L3,EVENT_INFORMATION,
1219          parse_information,build_information,
1220          "INFORMATION"},
1221         {CC_STATUS,L3,EVENT_STATUS,
1222          parse_status,build_status,
1223          "STATUS"},
1224         {CC_TIMEOUT,L3,EVENT_TIMEOUT,
1225          parse_timeout,build_timeout,
1226          "TIMEOUT"},
1227         {0,0,0,NULL,NULL,NULL}
1228 };
1229
1230 #define msgs_max (sizeof(msgs_g)/sizeof(struct isdn_msg))
1231
1232 /** INTERFACE FCTS ***/
1233 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *msg, int nt)
1234 {
1235         int i;
1236
1237         if (nt){
1238                 mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data;
1239                 
1240                 for (i=0; i< msgs_max -1; i++) {
1241                         if ( (hh->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1242                 }
1243                 
1244         } else {
1245                 iframe_t *frm = (iframe_t*)msg->data;
1246     
1247                 for (i=0; i< msgs_max -1; i++) 
1248                         if ( (frm->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1249         }
1250
1251         return -1;
1252 }
1253
1254 int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt)
1255 {
1256         int i;
1257         for (i=0; i< msgs_max; i++) 
1258                 if ( event == msgs[i].event) return i;
1259
1260         cb_log(10,0, "get_index: event not found!\n");
1261         
1262         return -1;
1263 }
1264
1265 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *msg, int nt)
1266 {
1267         int i=isdn_msg_get_index(msgs, msg, nt);
1268         if(i>=0) return msgs[i].event;
1269         return EVENT_UNKNOWN;
1270 }
1271
1272 char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt)
1273 {
1274         int i=isdn_msg_get_index(msgs, msg, nt);
1275         if(i>=0) return msgs[i].info;
1276         return NULL;
1277 }
1278
1279
1280 char EVENT_CLEAN_INFO[] = "CLEAN_UP";
1281 char EVENT_DTMF_TONE_INFO[] = "DTMF_TONE";
1282 char EVENT_NEW_L3ID_INFO[] = "NEW_L3ID";
1283 char EVENT_NEW_BC_INFO[] = "NEW_BC";
1284 char EVENT_PORT_ALARM_INFO[] = "ALARM";
1285 char EVENT_NEW_CHANNEL_INFO[] = "NEW_CHANNEL";
1286 char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA";
1287 char EVENT_BCHAN_ACTIVATED_INFO[] = "BCHAN_ACTIVATED";
1288 char EVENT_TONE_GENERATE_INFO[] = "TONE_GENERATE";
1289 char EVENT_BCHAN_ERROR_INFO[] = "BCHAN_ERROR";
1290
1291 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
1292 {
1293         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1294         
1295         if(i>=0) return msgs[i].info;
1296         
1297         if (event == EVENT_CLEANUP) return EVENT_CLEAN_INFO;
1298         if (event == EVENT_DTMF_TONE) return EVENT_DTMF_TONE_INFO;
1299         if (event == EVENT_NEW_L3ID) return EVENT_NEW_L3ID_INFO;
1300         if (event == EVENT_NEW_BC) return EVENT_NEW_BC_INFO;
1301         if (event == EVENT_NEW_CHANNEL) return EVENT_NEW_CHANNEL_INFO;
1302         if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO;
1303         if (event == EVENT_BCHAN_ACTIVATED) return EVENT_BCHAN_ACTIVATED_INFO;
1304         if (event == EVENT_TONE_GENERATE) return EVENT_TONE_GENERATE_INFO;
1305         if (event == EVENT_PORT_ALARM) return EVENT_PORT_ALARM_INFO;
1306         if (event == EVENT_BCHAN_ERROR) return EVENT_BCHAN_ERROR_INFO;
1307         
1308         return NULL;
1309 }
1310
1311 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1312 {
1313         int i=isdn_msg_get_index(msgs, msg, nt);
1314         if(i<0) return -1;
1315
1316         msgs[i].msg_parser(msgs, msg, bc, nt);
1317         return 0;
1318 }
1319
1320 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt)
1321 {
1322         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1323         if(i<0) return NULL;
1324   
1325         return  msgs[i].msg_builder(msgs, bc, nt);
1326 }