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