Merged revisions 78936 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->COMPLETE,msg, bc->sending_complete, nt, bc);
344         }
345   
346         if (bc->uulen) {
347                 int  protocol=4;
348                 enc_ie_useruser(&setup->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
349                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
350         }
351
352 #ifdef DEBUG 
353         printf("Building SETUP Msg\n"); 
354 #endif
355         return msg; 
356 }
357
358 static void parse_connect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
359 {
360         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
361         CONNECT_t *connect=(CONNECT_t*)((unsigned long)(msg->data+HEADER_LEN));
362   
363         int plan,pres,screen;
364         
365         bc->ces = connect->ces;
366         bc->ces = connect->ces;
367
368         dec_ie_progress(connect->PROGRESS, (Q931_info_t *)connect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
369
370         dec_ie_connected_pn(connect->CONNECT_PN,(Q931_info_t *)connect, &bc->cpnnumplan, &plan, &pres, &screen, bc->cad, 31, nt, bc);
371
372         /*
373                 cb_log(1,bc->port,"CONNETED PN: %s cpn_dialplan:%d\n", connected_pn, type);
374         */
375         
376 #ifdef DEBUG 
377         printf("Parsing CONNECT Msg\n"); 
378 #endif
379 }
380
381 static msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
382 {
383         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
384         CONNECT_t *connect;
385         msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_t) ,nt); 
386         
387         cb_log(6,bc->port,"BUILD_CONNECT: bc:%p bc->l3id:%d, nt:%d\n",bc,bc->l3_id,nt);
388
389         connect=(CONNECT_t*)((msg->data+HEADER_LEN)); 
390
391         if (nt) {
392                 time_t now;
393                 time(&now);
394                 enc_ie_date(&connect->DATE, msg, now, nt,bc);
395         }
396   
397         {
398                 int type=bc->cpnnumplan, plan=1, present=2, screen=0;
399                 enc_ie_connected_pn(&connect->CONNECT_PN, msg, type,plan, present, screen, bc->cad, nt , bc);
400         }
401
402 #ifdef DEBUG 
403         printf("Building CONNECT Msg\n"); 
404 #endif
405         return msg; 
406 }
407
408 static void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
409 {
410         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
411         SETUP_ACKNOWLEDGE_t *setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((unsigned long)(msg->data+HEADER_LEN));
412
413         {
414                 int  exclusive, channel;
415                 dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)setup_acknowledge, &exclusive, &channel, nt,bc);
416
417
418                 set_channel(bc, channel);
419         }
420         
421         dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)setup_acknowledge, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
422 #ifdef DEBUG 
423         printf("Parsing SETUP_ACKNOWLEDGE Msg\n"); 
424 #endif
425
426  
427 }
428
429 static msg_t *build_setup_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
430 {
431         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
432         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
433         msg_t *msg =(msg_t*)create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SETUP_ACKNOWLEDGE_t) ,nt); 
434  
435         setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
436   
437         enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
438   
439         if (nt) 
440                 enc_ie_progress(&setup_acknowledge->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
441   
442 #ifdef DEBUG 
443         printf("Building SETUP_ACKNOWLEDGE Msg\n"); 
444 #endif
445         return msg; 
446 }
447
448 static void parse_connect_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
449 {
450 #ifdef DEBUG 
451         printf("Parsing CONNECT_ACKNOWLEDGE Msg\n"); 
452 #endif
453
454  
455 }
456
457 static msg_t *build_connect_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
458 {
459         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
460         CONNECT_ACKNOWLEDGE_t *connect_acknowledge;
461         msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_ACKNOWLEDGE_t) ,nt); 
462  
463         connect_acknowledge=(CONNECT_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
464   
465         enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
466   
467 #ifdef DEBUG 
468         printf("Building CONNECT_ACKNOWLEDGE Msg\n"); 
469 #endif
470         return msg; 
471 }
472
473 static void parse_user_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
474 {
475 #ifdef DEBUG 
476         printf("Parsing USER_INFORMATION Msg\n"); 
477 #endif
478
479  
480 }
481
482 static msg_t *build_user_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
483 {
484         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
485         USER_INFORMATION_t *user_information;
486         msg_t *msg =(msg_t*)create_l3msg(CC_USER_INFORMATION | REQUEST, MT_USER_INFORMATION,  bc?bc->l3_id:-1, sizeof(USER_INFORMATION_t) ,nt); 
487  
488         user_information=(USER_INFORMATION_t*)((msg->data+HEADER_LEN)); 
489
490 #ifdef DEBUG 
491         printf("Building USER_INFORMATION Msg\n"); 
492 #endif
493         return msg; 
494 }
495
496 static void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
497 {
498 #ifdef DEBUG 
499         printf("Parsing SUSPEND_REJECT Msg\n"); 
500 #endif
501
502  
503 }
504
505 static msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
506 {
507         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
508         SUSPEND_REJECT_t *suspend_reject;
509         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT,  bc?bc->l3_id:-1, sizeof(SUSPEND_REJECT_t) ,nt); 
510  
511         suspend_reject=(SUSPEND_REJECT_t*)((msg->data+HEADER_LEN)); 
512
513 #ifdef DEBUG 
514         printf("Building SUSPEND_REJECT Msg\n"); 
515 #endif
516         return msg; 
517 }
518
519 static void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
520 {
521 #ifdef DEBUG 
522         printf("Parsing RESUME_REJECT Msg\n"); 
523 #endif
524
525  
526 }
527
528 static msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
529 {
530         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
531         RESUME_REJECT_t *resume_reject;
532         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT,  bc?bc->l3_id:-1, sizeof(RESUME_REJECT_t) ,nt); 
533  
534         resume_reject=(RESUME_REJECT_t*)((msg->data+HEADER_LEN)); 
535
536 #ifdef DEBUG 
537         printf("Building RESUME_REJECT Msg\n"); 
538 #endif
539         return msg; 
540 }
541
542 static void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
543 {
544 #ifdef DEBUG 
545         printf("Parsing HOLD Msg\n"); 
546 #endif
547
548  
549 }
550
551 static msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
552 {
553         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
554         HOLD_t *hold;
555         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD | REQUEST, MT_HOLD,  bc?bc->l3_id:-1, sizeof(HOLD_t) ,nt); 
556  
557         hold=(HOLD_t*)((msg->data+HEADER_LEN)); 
558
559 #ifdef DEBUG 
560         printf("Building HOLD Msg\n"); 
561 #endif
562         return msg; 
563 }
564
565 static void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
566 {
567 #ifdef DEBUG 
568         printf("Parsing SUSPEND Msg\n"); 
569 #endif
570
571  
572 }
573
574 static msg_t *build_suspend (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
575 {
576         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
577         SUSPEND_t *suspend;
578         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND | REQUEST, MT_SUSPEND,  bc?bc->l3_id:-1, sizeof(SUSPEND_t) ,nt); 
579  
580         suspend=(SUSPEND_t*)((msg->data+HEADER_LEN)); 
581
582 #ifdef DEBUG 
583         printf("Building SUSPEND Msg\n"); 
584 #endif
585         return msg; 
586 }
587
588 static void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
589 {
590 #ifdef DEBUG 
591         printf("Parsing RESUME Msg\n"); 
592 #endif
593
594  
595 }
596
597 static msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
598 {
599         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
600         RESUME_t *resume;
601         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME | REQUEST, MT_RESUME,  bc?bc->l3_id:-1, sizeof(RESUME_t) ,nt); 
602  
603         resume=(RESUME_t*)((msg->data+HEADER_LEN)); 
604
605 #ifdef DEBUG 
606         printf("Building RESUME Msg\n"); 
607 #endif
608         return msg; 
609 }
610
611 static void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
612 {
613 #ifdef DEBUG 
614         printf("Parsing HOLD_ACKNOWLEDGE Msg\n"); 
615 #endif
616
617  
618 }
619
620 static msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
621 {
622         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
623         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
624         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(HOLD_ACKNOWLEDGE_t) ,nt); 
625  
626         hold_acknowledge=(HOLD_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
627
628 #ifdef DEBUG 
629         printf("Building HOLD_ACKNOWLEDGE Msg\n"); 
630 #endif
631         return msg; 
632 }
633
634 static void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
635 {
636 #ifdef DEBUG 
637         printf("Parsing SUSPEND_ACKNOWLEDGE Msg\n"); 
638 #endif
639
640  
641 }
642
643 static msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
644 {
645         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
646         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
647         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SUSPEND_ACKNOWLEDGE_t) ,nt); 
648  
649         suspend_acknowledge=(SUSPEND_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
650
651 #ifdef DEBUG 
652         printf("Building SUSPEND_ACKNOWLEDGE Msg\n"); 
653 #endif
654         return msg; 
655 }
656
657 static void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
658 {
659 #ifdef DEBUG 
660         printf("Parsing RESUME_ACKNOWLEDGE Msg\n"); 
661 #endif
662
663  
664 }
665
666 static msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
667 {
668         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
669         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
670         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RESUME_ACKNOWLEDGE_t) ,nt); 
671  
672         resume_acknowledge=(RESUME_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
673
674 #ifdef DEBUG 
675         printf("Building RESUME_ACKNOWLEDGE Msg\n"); 
676 #endif
677         return msg; 
678 }
679
680 static void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
681 {
682 #ifdef DEBUG 
683         printf("Parsing HOLD_REJECT Msg\n"); 
684 #endif
685
686  
687 }
688
689 static msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
690 {
691         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
692         HOLD_REJECT_t *hold_reject;
693         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT,  bc?bc->l3_id:-1, sizeof(HOLD_REJECT_t) ,nt); 
694  
695         hold_reject=(HOLD_REJECT_t*)((msg->data+HEADER_LEN)); 
696
697 #ifdef DEBUG 
698         printf("Building HOLD_REJECT Msg\n"); 
699 #endif
700         return msg; 
701 }
702
703 static void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
704 {
705 #ifdef DEBUG 
706         printf("Parsing RETRIEVE Msg\n"); 
707 #endif
708
709  
710 }
711
712 static msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
713 {
714         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
715         RETRIEVE_t *retrieve;
716         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE | REQUEST, MT_RETRIEVE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_t) ,nt); 
717  
718         retrieve=(RETRIEVE_t*)((msg->data+HEADER_LEN)); 
719
720 #ifdef DEBUG 
721         printf("Building RETRIEVE Msg\n"); 
722 #endif
723         return msg; 
724 }
725
726 static void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
727 {
728 #ifdef DEBUG 
729         printf("Parsing RETRIEVE_ACKNOWLEDGE Msg\n"); 
730 #endif
731
732  
733 }
734
735 static msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
736 {
737         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
738         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
739         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_ACKNOWLEDGE_t) ,nt); 
740  
741         retrieve_acknowledge=(RETRIEVE_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); 
742
743         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
744 #ifdef DEBUG 
745         printf("Building RETRIEVE_ACKNOWLEDGE Msg\n"); 
746 #endif
747         return msg; 
748 }
749
750 static void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
751 {
752 #ifdef DEBUG 
753         printf("Parsing RETRIEVE_REJECT Msg\n"); 
754 #endif
755
756  
757 }
758
759 static msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
760 {
761         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
762         RETRIEVE_REJECT_t *retrieve_reject;
763         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT,  bc?bc->l3_id:-1, sizeof(RETRIEVE_REJECT_t) ,nt); 
764  
765         retrieve_reject=(RETRIEVE_REJECT_t*)((msg->data+HEADER_LEN)); 
766
767 #ifdef DEBUG 
768         printf("Building RETRIEVE_REJECT Msg\n"); 
769 #endif
770         return msg; 
771 }
772
773 static void parse_disconnect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
774 {
775         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
776         DISCONNECT_t *disconnect=(DISCONNECT_t*)((unsigned long)(msg->data+HEADER_LEN));
777         int location;
778         int cause; 
779         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &cause, nt,bc);
780         if (cause>0) bc->cause=cause;
781
782         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)disconnect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
783 #ifdef DEBUG 
784         printf("Parsing DISCONNECT Msg\n"); 
785 #endif
786
787  
788 }
789
790 static msg_t *build_disconnect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
791 {
792         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
793         DISCONNECT_t *disconnect;
794         msg_t *msg =(msg_t*)create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT,  bc?bc->l3_id:-1, sizeof(DISCONNECT_t) ,nt); 
795         
796         disconnect=(DISCONNECT_t*)((msg->data+HEADER_LEN)); 
797         
798         enc_ie_cause(&disconnect->CAUSE, msg, (nt)?1:0, bc->out_cause,nt,bc);
799         if (nt) enc_ie_progress(&disconnect->PROGRESS, msg, 0, nt?1:5, 8 ,nt,bc);
800
801         if (bc->uulen) {
802                 int  protocol=4;
803                 enc_ie_useruser(&disconnect->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
804                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
805         }
806   
807 #ifdef DEBUG 
808         printf("Building DISCONNECT Msg\n"); 
809 #endif
810         return msg; 
811 }
812
813 static void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
814 {
815         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
816         RESTART_t *restart=(RESTART_t*)((unsigned long)(msg->data+HEADER_LEN));
817
818         struct misdn_stack *stack=get_stack_by_bc(bc);
819         
820 #ifdef DEBUG 
821         printf("Parsing RESTART Msg\n");
822 #endif
823   
824         {
825                 int  exclusive;
826                 dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &bc->restart_channel, nt,bc);
827                 cb_log(3, stack->port, "CC_RESTART Request on channel:%d on this port.\n", bc->restart_channel);
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         enc_ie_channel_id(&restart->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
844         enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x80, nt, bc);
845
846         cb_log(0,bc->port, "Restarting channel %d\n", bc->channel);
847         return msg; 
848 }
849
850 static void parse_release (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
851 {
852         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
853         RELEASE_t *release=(RELEASE_t*)((unsigned long)(msg->data+HEADER_LEN));
854         int location;
855         int cause;
856   
857         dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &cause, nt,bc);
858         if (cause>0) bc->cause=cause;
859 #ifdef DEBUG 
860         printf("Parsing RELEASE Msg\n"); 
861 #endif
862
863  
864 }
865
866 static msg_t *build_release (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
867 {
868         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
869         RELEASE_t *release;
870         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE,  bc?bc->l3_id:-1, sizeof(RELEASE_t) ,nt); 
871  
872         release=(RELEASE_t*)((msg->data+HEADER_LEN)); 
873   
874         if (bc->out_cause>= 0)
875                 enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
876
877         if (bc->uulen) {
878                 int  protocol=4;
879                 enc_ie_useruser(&release->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
880                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
881         }
882   
883 #ifdef DEBUG 
884         printf("Building RELEASE Msg\n"); 
885 #endif
886         return msg; 
887 }
888
889 static void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
890 {
891         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
892         RELEASE_COMPLETE_t *release_complete=(RELEASE_COMPLETE_t*)((unsigned long)(msg->data+HEADER_LEN));
893         int location;
894         int cause;
895         iframe_t *frm = (iframe_t*) msg->data;
896
897         struct misdn_stack *stack=get_stack_by_bc(bc);
898         mISDNuser_head_t *hh;
899         hh=(mISDNuser_head_t*)msg->data;
900
901         /*hh=(mISDN_head_t*)msg->data;
902         mISDN_head_t *hh;*/
903
904         if (nt) {
905                 if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
906                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] \n");
907                         return;
908                 }
909         } else {
910                 if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
911                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] \n");
912                         return;
913                 }
914         }
915         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &cause, nt,bc);
916         if (cause>0) bc->cause=cause;
917
918 #ifdef DEBUG 
919         printf("Parsing RELEASE_COMPLETE Msg\n"); 
920 #endif
921 }
922
923 static msg_t *build_release_complete (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
924 {
925         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
926         RELEASE_COMPLETE_t *release_complete;
927         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE,  bc?bc->l3_id:-1, sizeof(RELEASE_COMPLETE_t) ,nt); 
928  
929         release_complete=(RELEASE_COMPLETE_t*)((msg->data+HEADER_LEN)); 
930         
931         enc_ie_cause(&release_complete->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
932
933         if (bc->uulen) {
934                 int  protocol=4;
935                 enc_ie_useruser(&release_complete->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
936                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
937         }
938   
939 #ifdef DEBUG 
940         printf("Building RELEASE_COMPLETE Msg\n"); 
941 #endif
942         return msg; 
943 }
944
945 static void parse_facility (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
946 {
947         int HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
948         FACILITY_t *facility = (FACILITY_t*)(msg->data+HEADER_LEN); 
949         Q931_info_t *qi = (Q931_info_t*)(msg->data+HEADER_LEN);  
950         unsigned char *p = NULL;
951         int err;
952
953 #ifdef DEBUG 
954         printf("Parsing FACILITY Msg\n"); 
955 #endif
956
957         if (!bc->nt) {
958                 if (qi->QI_ELEMENT(facility))
959                         p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
960         } else {
961                 p = facility->FACILITY;
962         }
963         if (!p)
964                 return;
965         
966         err = decodeFac(p, &(bc->fac_in));
967         if (err) {
968                 cb_log(1, bc->port, "Decoding FACILITY failed! (%d)\n", err);
969         }
970 }
971
972 static msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
973 {
974         int len,
975                 HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
976         unsigned char *ie_fac,
977                                   fac_tmp[256];
978         msg_t *msg =(msg_t*)create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY,  bc?bc->l3_id:-1, sizeof(FACILITY_t) ,nt);
979         FACILITY_t *facility = (FACILITY_t*)(msg->data+HEADER_LEN); 
980         Q931_info_t *qi;
981
982 #ifdef DEBUG 
983         printf("Building FACILITY Msg\n"); 
984 #endif
985         
986         len = encodeFac(fac_tmp, &(bc->fac_out));
987         if (len <= 0)
988                 return NULL;
989
990         ie_fac = msg_put(msg, len);
991         if (bc->nt) {
992                 facility->FACILITY = ie_fac + 1;
993         } else {
994                 qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
995                 qi->QI_ELEMENT(facility) = ie_fac - (unsigned char *)qi - sizeof(Q931_info_t);
996         }
997
998         memcpy(ie_fac, fac_tmp, len);
999
1000         if (*bc->display) {
1001                 printf("Sending %s as Display\n", bc->display);
1002                 enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc);
1003         }
1004
1005         return msg; 
1006 }
1007
1008 static void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1009 {
1010 #ifdef DEBUG 
1011         printf("Parsing NOTIFY Msg\n"); 
1012 #endif
1013 }
1014
1015 static msg_t *build_notify (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1016 {
1017         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1018         NOTIFY_t *notify;
1019         msg_t *msg =(msg_t*)create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY,  bc?bc->l3_id:-1, sizeof(NOTIFY_t) ,nt); 
1020  
1021         notify=(NOTIFY_t*)((msg->data+HEADER_LEN)); 
1022
1023 #ifdef DEBUG 
1024         printf("Building NOTIFY Msg\n"); 
1025 #endif
1026         return msg; 
1027 }
1028
1029 static void parse_status_enquiry (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1030 {
1031 #ifdef DEBUG 
1032         printf("Parsing STATUS_ENQUIRY Msg\n"); 
1033 #endif
1034 }
1035
1036 static msg_t *build_status_enquiry (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1037 {
1038         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1039         STATUS_ENQUIRY_t *status_enquiry;
1040         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS_ENQUIRY | REQUEST, MT_STATUS_ENQUIRY,  bc?bc->l3_id:-1, sizeof(STATUS_ENQUIRY_t) ,nt); 
1041  
1042         status_enquiry=(STATUS_ENQUIRY_t*)((msg->data+HEADER_LEN)); 
1043
1044 #ifdef DEBUG 
1045         printf("Building STATUS_ENQUIRY Msg\n"); 
1046 #endif
1047         return msg; 
1048 }
1049
1050 static void parse_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1051 {
1052         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1053         INFORMATION_t *information=(INFORMATION_t*)((unsigned long)(msg->data+HEADER_LEN));
1054         {
1055                 int  type, plan;
1056                 char number[32];
1057                 char keypad[32];
1058                 dec_ie_called_pn(information->CALLED_PN, (Q931_info_t *)information, &type, &plan, number, sizeof(number)-1, nt, bc);
1059                 dec_ie_keypad(information->KEYPAD, (Q931_info_t *)information, keypad, sizeof(keypad)-1, nt, bc);
1060                 strcpy(bc->info_dad, number);
1061                 strcpy(bc->keypad,keypad);
1062         }
1063 #ifdef DEBUG 
1064         printf("Parsing INFORMATION Msg\n"); 
1065 #endif
1066 }
1067
1068 static msg_t *build_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1069 {
1070         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1071         INFORMATION_t *information;
1072         msg_t *msg =(msg_t*)create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION,  bc?bc->l3_id:-1, sizeof(INFORMATION_t) ,nt); 
1073  
1074         information=(INFORMATION_t*)((msg->data+HEADER_LEN)); 
1075   
1076         {
1077                 enc_ie_called_pn(&information->CALLED_PN, msg, 0, 1, bc->info_dad, nt,bc);
1078         }
1079
1080         {
1081                 if (*bc->display) {
1082                         printf("Sending %s as Display\n", bc->display);
1083                         enc_ie_display(&information->DISPLAY, msg, bc->display, nt,bc);
1084                 }
1085         }
1086   
1087 #ifdef DEBUG 
1088         printf("Building INFORMATION Msg\n"); 
1089 #endif
1090         return msg; 
1091 }
1092
1093 static void parse_status (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1094 {
1095         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1096         STATUS_t *status=(STATUS_t*)((unsigned long)(msg->data+HEADER_LEN));
1097         int location;
1098         int cause;
1099   
1100         dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &cause, nt,bc);
1101         if (cause>0) bc->cause=cause;
1102         ;
1103
1104 #ifdef DEBUG 
1105         printf("Parsing STATUS Msg\n"); 
1106 #endif
1107 }
1108
1109 static msg_t *build_status (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1110 {
1111         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1112         STATUS_t *status;
1113         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt); 
1114  
1115         status=(STATUS_t*)((msg->data+HEADER_LEN)); 
1116
1117 #ifdef DEBUG 
1118         printf("Building STATUS Msg\n"); 
1119 #endif
1120         return msg; 
1121 }
1122
1123 static void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) 
1124 {
1125 #ifdef DEBUG 
1126         printf("Parsing STATUS Msg\n"); 
1127 #endif 
1128 }
1129
1130 static msg_t *build_timeout (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) 
1131 {
1132         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1133         STATUS_t *status;
1134         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt); 
1135  
1136         status=(STATUS_t*)((msg->data+HEADER_LEN)); 
1137
1138 #ifdef DEBUG 
1139         printf("Building STATUS Msg\n"); 
1140 #endif
1141         return msg; 
1142 }
1143
1144
1145 /************************************/
1146
1147
1148
1149
1150 /** Msg Array **/
1151
1152 struct isdn_msg msgs_g[] = {
1153         {CC_PROCEEDING,L3,EVENT_PROCEEDING,
1154          parse_proceeding,build_proceeding,
1155          "PROCEEDING"},
1156         {CC_ALERTING,L3,EVENT_ALERTING,
1157          parse_alerting,build_alerting,
1158          "ALERTING"},
1159         {CC_PROGRESS,L3,EVENT_PROGRESS,
1160          parse_progress,build_progress,
1161          "PROGRESS"},
1162         {CC_SETUP,L3,EVENT_SETUP,
1163          parse_setup,build_setup,
1164          "SETUP"},
1165         {CC_CONNECT,L3,EVENT_CONNECT,
1166          parse_connect,build_connect,
1167          "CONNECT"},
1168         {CC_SETUP_ACKNOWLEDGE,L3,EVENT_SETUP_ACKNOWLEDGE,
1169          parse_setup_acknowledge,build_setup_acknowledge,
1170          "SETUP_ACKNOWLEDGE"},
1171         {CC_CONNECT_ACKNOWLEDGE ,L3,EVENT_CONNECT_ACKNOWLEDGE ,
1172          parse_connect_acknowledge ,build_connect_acknowledge,
1173          "CONNECT_ACKNOWLEDGE "},
1174         {CC_USER_INFORMATION,L3,EVENT_USER_INFORMATION,
1175          parse_user_information,build_user_information,
1176          "USER_INFORMATION"},
1177         {CC_SUSPEND_REJECT,L3,EVENT_SUSPEND_REJECT,
1178          parse_suspend_reject,build_suspend_reject,
1179          "SUSPEND_REJECT"},
1180         {CC_RESUME_REJECT,L3,EVENT_RESUME_REJECT,
1181          parse_resume_reject,build_resume_reject,
1182          "RESUME_REJECT"},
1183         {CC_HOLD,L3,EVENT_HOLD,
1184          parse_hold,build_hold,
1185          "HOLD"},
1186         {CC_SUSPEND,L3,EVENT_SUSPEND,
1187          parse_suspend,build_suspend,
1188          "SUSPEND"},
1189         {CC_RESUME,L3,EVENT_RESUME,
1190          parse_resume,build_resume,
1191          "RESUME"},
1192         {CC_HOLD_ACKNOWLEDGE,L3,EVENT_HOLD_ACKNOWLEDGE,
1193          parse_hold_acknowledge,build_hold_acknowledge,
1194          "HOLD_ACKNOWLEDGE"},
1195         {CC_SUSPEND_ACKNOWLEDGE,L3,EVENT_SUSPEND_ACKNOWLEDGE,
1196          parse_suspend_acknowledge,build_suspend_acknowledge,
1197          "SUSPEND_ACKNOWLEDGE"},
1198         {CC_RESUME_ACKNOWLEDGE,L3,EVENT_RESUME_ACKNOWLEDGE,
1199          parse_resume_acknowledge,build_resume_acknowledge,
1200          "RESUME_ACKNOWLEDGE"},
1201         {CC_HOLD_REJECT,L3,EVENT_HOLD_REJECT,
1202          parse_hold_reject,build_hold_reject,
1203          "HOLD_REJECT"},
1204         {CC_RETRIEVE,L3,EVENT_RETRIEVE,
1205          parse_retrieve,build_retrieve,
1206          "RETRIEVE"},
1207         {CC_RETRIEVE_ACKNOWLEDGE,L3,EVENT_RETRIEVE_ACKNOWLEDGE,
1208          parse_retrieve_acknowledge,build_retrieve_acknowledge,
1209          "RETRIEVE_ACKNOWLEDGE"},
1210         {CC_RETRIEVE_REJECT,L3,EVENT_RETRIEVE_REJECT,
1211          parse_retrieve_reject,build_retrieve_reject,
1212          "RETRIEVE_REJECT"},
1213         {CC_DISCONNECT,L3,EVENT_DISCONNECT,
1214          parse_disconnect,build_disconnect,
1215          "DISCONNECT"},
1216         {CC_RESTART,L3,EVENT_RESTART,
1217          parse_restart,build_restart,
1218          "RESTART"},
1219         {CC_RELEASE,L3,EVENT_RELEASE,
1220          parse_release,build_release,
1221          "RELEASE"},
1222         {CC_RELEASE_COMPLETE,L3,EVENT_RELEASE_COMPLETE,
1223          parse_release_complete,build_release_complete,
1224          "RELEASE_COMPLETE"},
1225         {CC_FACILITY,L3,EVENT_FACILITY,
1226          parse_facility,build_facility,
1227          "FACILITY"},
1228         {CC_NOTIFY,L3,EVENT_NOTIFY,
1229          parse_notify,build_notify,
1230          "NOTIFY"},
1231         {CC_STATUS_ENQUIRY,L3,EVENT_STATUS_ENQUIRY,
1232          parse_status_enquiry,build_status_enquiry,
1233          "STATUS_ENQUIRY"},
1234         {CC_INFORMATION,L3,EVENT_INFORMATION,
1235          parse_information,build_information,
1236          "INFORMATION"},
1237         {CC_STATUS,L3,EVENT_STATUS,
1238          parse_status,build_status,
1239          "STATUS"},
1240         {CC_TIMEOUT,L3,EVENT_TIMEOUT,
1241          parse_timeout,build_timeout,
1242          "TIMEOUT"},
1243         {0,0,0,NULL,NULL,NULL}
1244 };
1245
1246 #define msgs_max (sizeof(msgs_g)/sizeof(struct isdn_msg))
1247
1248 /** INTERFACE FCTS ***/
1249 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *msg, int nt)
1250 {
1251         int i;
1252
1253         if (nt){
1254                 mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data;
1255                 
1256                 for (i=0; i< msgs_max -1; i++) {
1257                         if ( (hh->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1258                 }
1259                 
1260         } else {
1261                 iframe_t *frm = (iframe_t*)msg->data;
1262     
1263                 for (i=0; i< msgs_max -1; i++) 
1264                         if ( (frm->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1265         }
1266
1267         return -1;
1268 }
1269
1270 int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt)
1271 {
1272         int i;
1273         for (i=0; i< msgs_max; i++) 
1274                 if ( event == msgs[i].event) return i;
1275
1276         cb_log(10,0, "get_index: event not found!\n");
1277         
1278         return -1;
1279 }
1280
1281 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *msg, int nt)
1282 {
1283         int i=isdn_msg_get_index(msgs, msg, nt);
1284         if(i>=0) return msgs[i].event;
1285         return EVENT_UNKNOWN;
1286 }
1287
1288 char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt)
1289 {
1290         int i=isdn_msg_get_index(msgs, msg, nt);
1291         if(i>=0) return msgs[i].info;
1292         return NULL;
1293 }
1294
1295
1296 char EVENT_CLEAN_INFO[] = "CLEAN_UP";
1297 char EVENT_DTMF_TONE_INFO[] = "DTMF_TONE";
1298 char EVENT_NEW_L3ID_INFO[] = "NEW_L3ID";
1299 char EVENT_NEW_BC_INFO[] = "NEW_BC";
1300 char EVENT_PORT_ALARM_INFO[] = "ALARM";
1301 char EVENT_NEW_CHANNEL_INFO[] = "NEW_CHANNEL";
1302 char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA";
1303 char EVENT_BCHAN_ACTIVATED_INFO[] = "BCHAN_ACTIVATED";
1304 char EVENT_TONE_GENERATE_INFO[] = "TONE_GENERATE";
1305 char EVENT_BCHAN_ERROR_INFO[] = "BCHAN_ERROR";
1306
1307 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
1308 {
1309         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1310         
1311         if(i>=0) return msgs[i].info;
1312         
1313         if (event == EVENT_CLEANUP) return EVENT_CLEAN_INFO;
1314         if (event == EVENT_DTMF_TONE) return EVENT_DTMF_TONE_INFO;
1315         if (event == EVENT_NEW_L3ID) return EVENT_NEW_L3ID_INFO;
1316         if (event == EVENT_NEW_BC) return EVENT_NEW_BC_INFO;
1317         if (event == EVENT_NEW_CHANNEL) return EVENT_NEW_CHANNEL_INFO;
1318         if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO;
1319         if (event == EVENT_BCHAN_ACTIVATED) return EVENT_BCHAN_ACTIVATED_INFO;
1320         if (event == EVENT_TONE_GENERATE) return EVENT_TONE_GENERATE_INFO;
1321         if (event == EVENT_PORT_ALARM) return EVENT_PORT_ALARM_INFO;
1322         if (event == EVENT_BCHAN_ERROR) return EVENT_BCHAN_ERROR_INFO;
1323         
1324         return NULL;
1325 }
1326
1327 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1328 {
1329         int i=isdn_msg_get_index(msgs, msg, nt);
1330         if(i<0) return -1;
1331
1332         msgs[i].msg_parser(msgs, msg, bc, nt);
1333         return 0;
1334 }
1335
1336 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt)
1337 {
1338         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1339         if(i<0) return NULL;
1340   
1341         return  msgs[i].msg_builder(msgs, bc, nt);
1342 }