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