backup
[lcr.git] / callpbx.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** call functions                                                            **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <poll.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include "main.h"
21 #define __u8 unsigned char
22 #define __u16 unsigned short
23 #define __u32 unsigned long
24 #include "linux/isdnif.h"
25
26
27 /* notify endpoint about state change (if any) */
28 static int notify_state_change(int call_id, int epoint_id, int old_state, int new_state)
29 {
30         int notify_off = 0, notify_on = 0;
31         struct message *message;
32
33         if (old_state == new_state)
34                 return(old_state);
35
36         switch(old_state)
37         {
38                 case NOTIFY_STATE_ACTIVE:
39                 switch(new_state)
40                 {
41                         case NOTIFY_STATE_HOLD:
42                         notify_on = INFO_NOTIFY_REMOTE_HOLD;
43                         break;
44                         case NOTIFY_STATE_SUSPEND:
45                         notify_on = INFO_NOTIFY_USER_SUSPENDED;
46                         break;
47                         case NOTIFY_STATE_CONFERENCE:
48                         notify_on = INFO_NOTIFY_CONFERENCE_ESTABLISHED;
49                         break;
50                 }
51                 break;
52
53                 case NOTIFY_STATE_HOLD:
54                 switch(new_state)
55                 {
56                         case NOTIFY_STATE_ACTIVE:
57                         notify_off = INFO_NOTIFY_REMOTE_RETRIEVAL;
58                         break;
59                         case NOTIFY_STATE_SUSPEND:
60                         notify_off = INFO_NOTIFY_REMOTE_RETRIEVAL;
61                         notify_on = INFO_NOTIFY_USER_SUSPENDED;
62                         break;
63                         case NOTIFY_STATE_CONFERENCE:
64                         notify_off = INFO_NOTIFY_REMOTE_RETRIEVAL;
65                         notify_on = INFO_NOTIFY_CONFERENCE_ESTABLISHED;
66                         break;
67                 }
68                 break;
69
70                 case NOTIFY_STATE_SUSPEND:
71                 switch(new_state)
72                 {
73                         case NOTIFY_STATE_ACTIVE:
74                         notify_off = INFO_NOTIFY_USER_RESUMED;
75                         break;
76                         case NOTIFY_STATE_HOLD:
77                         notify_off = INFO_NOTIFY_USER_RESUMED;
78                         notify_on = INFO_NOTIFY_REMOTE_HOLD;
79                         break;
80                         case NOTIFY_STATE_CONFERENCE:
81                         notify_off = INFO_NOTIFY_USER_RESUMED;
82                         notify_on = INFO_NOTIFY_CONFERENCE_ESTABLISHED;
83                         break;
84                 }
85                 break;
86
87                 case NOTIFY_STATE_CONFERENCE:
88                 switch(new_state)
89                 {
90                         case NOTIFY_STATE_ACTIVE:
91                         notify_off = INFO_NOTIFY_CONFERENCE_DISCONNECTED;
92                         break;
93                         case NOTIFY_STATE_HOLD:
94                         notify_off = INFO_NOTIFY_CONFERENCE_DISCONNECTED;
95                         notify_on = INFO_NOTIFY_REMOTE_HOLD;
96                         break;
97                         case NOTIFY_STATE_SUSPEND:
98                         notify_off = INFO_NOTIFY_CONFERENCE_DISCONNECTED;
99                         notify_on = INFO_NOTIFY_USER_SUSPENDED;
100                         break;
101                 }
102                 break;
103         }
104
105         if (call_id && notify_off)
106         {
107                 message = message_create(call_id, epoint_id, CALL_TO_EPOINT, MESSAGE_NOTIFY);
108                 message->param.notifyinfo.notify = notify_off;
109                 message_put(message);
110         }
111
112         if (call_id && notify_on)
113         {
114                 message = message_create(call_id, epoint_id, CALL_TO_EPOINT, MESSAGE_NOTIFY);
115                 message->param.notifyinfo.notify = notify_on;
116                 message_put(message);
117         }
118
119         return(new_state);
120 }
121
122
123 /* debug function for call */
124 void callpbx_debug(class CallPBX *callpbx, char *function)
125 {
126         struct call_relation *relation;
127         struct port_list *portlist;
128         class Endpoint *epoint;
129         class Port *port;
130         char buffer[512];
131
132         if (!(options.deb & DEBUG_CALL))
133                 return;
134
135         PDEBUG(DEBUG_CALL, "CALL(%d) start (called from %s)\n", callpbx->c_serial, function);
136
137         relation = callpbx->c_relation;
138
139         if (!relation)
140                 PDEBUG(DEBUG_CALL, "call has no relations\n");
141         while(relation)
142         {
143                 epoint = find_epoint_id(relation->epoint_id);
144                 if (!epoint)
145                 {
146                         PDEBUG(DEBUG_CALL, "warning: relations epoint id=%ld doesn't exists!\n", relation->epoint_id);
147                         relation = relation->next;
148                         continue;
149                 }
150                 buffer[0] = '\0';
151                 UPRINT(strchr(buffer,0), "*** ep%ld", relation->epoint_id);
152                 UPRINT(strchr(buffer,0), " ifs=");
153                 portlist = epoint->ep_portlist;
154                 while(portlist)
155                 {
156                         port = find_port_id(portlist->port_id);
157                         if (port)
158                                 UPRINT(strchr(buffer,0), "%s,", port->p_name);
159                         else
160                                 UPRINT(strchr(buffer,0), "<port %ld doesn't exist>,", portlist->port_id);
161                         portlist = portlist->next;
162                 }
163 //              UPRINT(strchr(buffer,0), " endpoint=%d on=%s hold=%s", epoint->ep_serial, (epoint->ep_call_id==callpbx->c_serial)?"yes":"no", (epoint->get_hold_id()==callpbx->c_serial)?"yes":"no");
164                 UPRINT(strchr(buffer,0), " endpoint=%d on=%s", epoint->ep_serial, (epoint->ep_call_id==callpbx->c_serial)?"yes":"no");
165                 switch(relation->type)
166                 {
167                         case RELATION_TYPE_CALLING:
168                         UPRINT(strchr(buffer,0), " type=CALLING");
169                         break;
170                         case RELATION_TYPE_SETUP:
171                         UPRINT(strchr(buffer,0), " type=SETUP");
172                         break;
173                         case RELATION_TYPE_CONNECT:
174                         UPRINT(strchr(buffer,0), " type=CONNECT");
175                         break;
176                         default:
177                         UPRINT(strchr(buffer,0), " type=unknown");
178                         break;
179                 }
180                 switch(relation->channel_state)
181                 {
182                         case CHANNEL_STATE_CONNECT:
183                         UPRINT(strchr(buffer,0), " channel=CONNECT");
184                         break;
185                         case CHANNEL_STATE_HOLD:
186                         UPRINT(strchr(buffer,0), " channel=HOLD");
187                         break;
188                         default:
189                         UPRINT(strchr(buffer,0), " channel=unknown");
190                         break;
191                 }
192                 switch(relation->tx_state)
193                 {
194                         case NOTIFY_STATE_ACTIVE:
195                         UPRINT(strchr(buffer,0), " tx_state=ACTIVE");
196                         break;
197                         case NOTIFY_STATE_HOLD:
198                         UPRINT(strchr(buffer,0), " tx_state=HOLD");
199                         break;
200                         case NOTIFY_STATE_SUSPEND:
201                         UPRINT(strchr(buffer,0), " tx_state=SUSPEND");
202                         break;
203                         case NOTIFY_STATE_CONFERENCE:
204                         UPRINT(strchr(buffer,0), " tx_state=CONFERENCE");
205                         break;
206                         default:
207                         UPRINT(strchr(buffer,0), " tx_state=unknown");
208                         break;
209                 }
210                 switch(relation->rx_state)
211                 {
212                         case NOTIFY_STATE_ACTIVE:
213                         UPRINT(strchr(buffer,0), " rx_state=ACTIVE");
214                         break;
215                         case NOTIFY_STATE_HOLD:
216                         UPRINT(strchr(buffer,0), " rx_state=HOLD");
217                         break;
218                         case NOTIFY_STATE_SUSPEND:
219                         UPRINT(strchr(buffer,0), " rx_state=SUSPEND");
220                         break;
221                         case NOTIFY_STATE_CONFERENCE:
222                         UPRINT(strchr(buffer,0), " rx_state=CONFERENCE");
223                         break;
224                         default:
225                         UPRINT(strchr(buffer,0), " rx_state=unknown");
226                         break;
227                 }
228                 PDEBUG(DEBUG_CALL, "%s\n", buffer);
229                 relation = relation->next;
230         }
231
232         PDEBUG(DEBUG_CALL, "end\n");
233 }
234
235
236 /*
237  * constructor for a new call 
238  * the call will have a relation to the calling endpoint
239  */
240 CallPBX::CallPBX(class Endpoint *epoint) : Call(epoint)
241 {
242         struct call_relation *relation;
243 //      char filename[256];
244
245         if (!epoint)
246         {
247                 PERROR("software error, epoint is NULL.\n");
248                 exit(-1);
249         }
250
251         PDEBUG(DEBUG_CALL, "creating new call and connecting it to the endpoint.\n");
252
253         c_type = CALL_TYPE_PBX;
254         c_caller[0] = '\0';
255         c_caller_id[0] = '\0';
256         c_dialed[0] = '\0';
257         c_todial[0] = '\0';
258         c_pid = getpid();
259         c_updatebridge = 0;
260         c_partyline = 0;
261
262         /* initialize a relation only to the calling interface */
263         relation = c_relation = (struct call_relation *)calloc(1, sizeof(struct call_relation));
264         if (!relation)
265         {
266                 PERROR("no memory, exitting..\n");
267                 exit(-1);
268         }
269         cmemuse++;
270         memset(relation, 0, sizeof(struct call_relation));
271         relation->type = RELATION_TYPE_CALLING;
272         relation->channel_state = CHANNEL_STATE_HOLD; /* audio is assumed on a new call */
273         relation->tx_state = NOTIFY_STATE_ACTIVE; /* new calls always assumed to be active */
274         relation->rx_state = NOTIFY_STATE_ACTIVE; /* new calls always assumed to be active */
275         relation->epoint_id = epoint->ep_serial;
276
277
278         if (options.deb & DEBUG_CALL)
279                 callpbx_debug(this, "CallPBX::Constructor(new call)");
280 }
281
282
283 /*
284  * call descructor
285  */
286 CallPBX::~CallPBX()
287 {
288         struct call_relation *relation, *rtemp;
289
290         relation = c_relation;
291         while(relation)
292         {
293                 rtemp = relation->next;
294                 memset(relation, 0, sizeof(struct call_relation));
295                 free(relation);
296                 cmemuse--;
297                 relation = rtemp;
298         }
299 }
300
301
302 /* bridge sets the audio flow of all bchannels assiociated to 'this' call
303  * also it changes and notifies active/hold/conference states
304  */
305 void CallPBX::bridge(void)
306 {
307         struct call_relation *relation;
308         struct message *message;
309         int numconnect = 0, relations = 0;
310         class Endpoint *epoint;
311         struct port_list *portlist;
312         class Port *port;
313         int allmISDN = 0; // relations that are no mISDN
314
315         relation = c_relation;
316         while(relation)
317         {
318                 /* count all relations */
319                 relations++;
320
321                 /* check for relation's objects */
322                 epoint = find_epoint_id(relation->epoint_id);
323                 if (!epoint)
324                 {
325                         PERROR("software error: relation without existing endpoints.\n");
326                         relation = relation->next;
327                         continue;
328                 }
329                 portlist = epoint->ep_portlist;
330                 if (!portlist)
331                 {
332                         PDEBUG((DEBUG_CALL|DEBUG_PORT), "ignoring relation without port object.\n");
333 //#warning testing: keep on hold until single audio stream available
334                         relation->channel_state = CHANNEL_STATE_HOLD;
335                         relation = relation->next;
336                         continue;
337                 }
338                 if (portlist->next)
339                 {
340                         PDEBUG((DEBUG_CALL|DEBUG_PORT), "ignoring relation with ep%d due to port_list.\n", epoint->ep_serial);
341 //#warning testing: keep on hold until single audio stream available
342                         relation->channel_state = CHANNEL_STATE_HOLD;
343                         relation = relation->next;
344                         continue;
345                 }
346                 port = find_port_id(portlist->port_id);
347                 if (!port)
348                 {
349                         PDEBUG((DEBUG_CALL|DEBUG_PORT), "ignoring relation without existing port object.\n");
350                         relation = relation->next;
351                         continue;
352                 }
353                 if ((port->p_type&PORT_CLASS_MASK)!=PORT_CLASS_mISDN)
354                 {
355                         PDEBUG((DEBUG_CALL|DEBUG_PORT), "ignoring relation ep%d because it's port is not mISDN.\n", epoint->ep_serial);
356                         if (allmISDN)
357                         {
358                                 PDEBUG((DEBUG_CALL|DEBUG_PORT), "not all endpoints are mISDN.\n");
359                                 allmISDN = 0;
360                         }
361                         relation = relation->next;
362                         continue;
363                 }
364                 relation = relation->next;
365         }
366
367         /* we notify all relations about rxdata. */
368         relation = c_relation;
369         while(relation)
370         {
371                 /* count connected relations */
372                 if ((relation->channel_state == CHANNEL_STATE_CONNECT)
373                  && (relation->rx_state != NOTIFY_STATE_SUSPEND)
374                  && (relation->rx_state != NOTIFY_STATE_HOLD))
375                         numconnect ++;
376
377                 /* remove unconnected parties from conference, also remove remotely disconnected parties so conference will not be disturbed. */
378                 if (relation->channel_state == CHANNEL_STATE_CONNECT
379                  && relation->rx_state != NOTIFY_STATE_HOLD
380                  && relation->rx_state != NOTIFY_STATE_SUSPEND
381                  && relations>1 // no conf with on party
382                  && allmISDN) // no conf if any member is not mISDN
383                 {
384                         message = message_create(c_serial, relation->epoint_id, CALL_TO_EPOINT, MESSAGE_mISDNSIGNAL);
385                         message->param.mISDNsignal.message = mISDNSIGNAL_CONF;
386                         message->param.mISDNsignal.conf = c_serial<<16 | c_pid;
387                         PDEBUG(DEBUG_CALL, "%s +on+ id: 0x%08x\n", port->p_name, message->param.mISDNsignal.conf);
388                         message_put(message);
389                 } else
390                 {
391                         message = message_create(c_serial, relation->epoint_id, CALL_TO_EPOINT, MESSAGE_mISDNSIGNAL);
392                         message->param.mISDNsignal.message = mISDNSIGNAL_CONF;
393                         message->param.mISDNsignal.conf = 0;
394                         PDEBUG(DEBUG_CALL, "%s +off+ id: 0x%08x\n", port->p_name, message->param.mISDNsignal.conf);
395                         message_put(message);
396                 }
397
398                 /*
399                  * request data from endpoint/port if:
400                  * - two relations
401                  * - any without mISDN
402                  * in this case we bridge
403                  */
404                 message = message_create(c_serial, relation->epoint_id, CALL_TO_EPOINT, MESSAGE_mISDNSIGNAL);
405                 message->param.mISDNsignal.message = mISDNSIGNAL_CALLDATA;
406                 message->param.mISDNsignal.calldata = (relnum==2 && !allmISDN);
407                 PDEBUG(DEBUG_CALL, "call %d sets 'calldata' on port %s to %d\n", c_serial, port->p_name, calldata);
408                 message_put(message);
409
410                 relation = relation->next;
411         }
412
413         /* two people just exchange their states */
414         if (relations==2 && !c_partyline)
415         {
416                 relation = c_relation;
417                 relation->tx_state = notify_state_change(c_serial, relation->epoint_id, relation->tx_state, relation->next->rx_state);
418                 relation->next->tx_state = notify_state_change(c_serial, relation->next->epoint_id, relation->next->tx_state, relation->rx_state);
419         } else
420         /* one member in a call, so we put her on hold */
421         if (relations==1 || numconnect==1)
422         {
423                 relation = c_relation;
424                 while(relation)
425                 {
426                         if ((relation->channel_state == CHANNEL_STATE_CONNECT)
427                          && (relation->rx_state != NOTIFY_STATE_SUSPEND)
428                          && (relation->rx_state != NOTIFY_STATE_HOLD))
429                                 relation->tx_state = notify_state_change(c_serial, relation->epoint_id, relation->tx_state, NOTIFY_STATE_HOLD);
430                         relation = relation->next;
431                 }
432         } else
433         /* if conference/partyline or (more than two members and more than one is connected), so we set conference state */ 
434         {
435                 relation = c_relation;
436                 while(relation)
437                 {
438                         if ((relation->channel_state == CHANNEL_STATE_CONNECT)
439                          && (relation->rx_state != NOTIFY_STATE_SUSPEND)
440                          && (relation->rx_state != NOTIFY_STATE_HOLD))
441                                 relation->tx_state = notify_state_change(c_serial, relation->epoint_id, relation->tx_state, NOTIFY_STATE_CONFERENCE);
442                         relation = relation->next;
443                 }
444         }
445 }
446
447 /*
448  * bridging is only possible with two connected endpoints
449  */
450 void CallPBX::bridge_data(unsigned long epoint_from, struct call_relation *relation_from, union parameter *param)
451 {
452         struct call_relation *relation_to;
453         struct message *message;
454
455         /* if we are alone */
456         if (!c_relation->next)
457                 return;
458
459         /* if we are more than two */
460         if (c_relation->next->next)
461                 return;
462
463         /* skip if source endpoint has NOT audio mode CONNECT */
464         if (relation_from->channel_state != CHANNEL_STATE_CONNECT)
465                 return;
466
467         /* get destination relation */
468         relation_to = c_relation;
469         if (relation_to == relation_from)
470         {
471                 /* oops, we are the first, so destination is: */
472                 relation_to = relation_to->next;
473         }
474
475         /* skip if destomatopm endpoint has NOT audio mode CONNECT */
476         if (relation_to->channel_state != CHANNEL_STATE_CONNECT)
477                 return;
478
479         /* now we may send our data to the endpoint where it
480          * will be delivered to the port
481          */
482 //PDEBUG(DEBUG_CALL, "mixing from %d to %d\n", epoint_from, relation_to->epoint_id);
483         message = message_create(c_serial, relation_to->epoint_id, CALL_TO_EPOINT, MESSAGE_DATA);
484         memcpy(&message->param, param, sizeof(union parameter));
485         message_put(message);
486 }
487
488
489 /* release call from endpoint
490  * if the call has two relations, all relations are freed and the call will be
491  * destroyed 
492  */
493 void CallPBX::release(unsigned long epoint_id, int hold, int location, int cause)
494 {
495         struct call_relation *relation, **relationpointer;
496         struct message *message;
497
498         class Call *call;
499
500         if (!epoint_id)
501         {
502                 PERROR("software error, epoint is NULL.\n");
503                 return;
504         }
505
506         if (options.deb & DEBUG_CALL)
507                 callpbx_debug(this, "call_release{before}");
508
509         /* find relation */
510         relation = c_relation;
511         while(relation)
512         {
513                 if (relation->epoint_id == epoint_id)
514                         break;
515                 relation = relation->next;
516         }
517         if (!relation)
518         {
519                 PERROR("software error, epoint has a call with no relation to the epoint.\n");
520                 return;
521         }
522
523         /* remove from bridge */
524         if (relation->channel_state != CHANNEL_STATE_HOLD)
525         {
526                 relation->channel_state = CHANNEL_STATE_HOLD;
527                 c_updatebridge = 1; /* update bridge flag */
528         }
529
530         /* detach given interface */
531         relation = c_relation;
532         relationpointer = &c_relation;
533         while(relation)
534         {
535                 /* endpoint of function call */
536                 if (relation->epoint_id == epoint_id)
537                 {
538                         *relationpointer = relation->next;
539                         memset(relation, 0, sizeof(struct call_relation));
540                         free(relation);
541                         cmemuse--;
542                         relation = *relationpointer;
543                         continue;
544                 }
545                 relationpointer = &relation->next;
546                 relation = relation->next;
547         }
548
549         /* if no more relation */
550         if (!c_relation)
551         {
552                 PDEBUG(DEBUG_CALL, "call is completely removed.\n");
553                 /* there is no more endpoint related to the call */
554                 delete this;
555                 // end of call object!
556                 PDEBUG(DEBUG_CALL, "call completely removed!\n");
557         } else
558         /* if call is a party line */
559         if (c_partyline)
560         {
561                 PDEBUG(DEBUG_CALL, "call is a conference room, so we keep it alive until the last party left.\n");
562         } else
563         /* if only one relation left */
564         if (!c_relation->next)
565         {
566                 PDEBUG(DEBUG_CALL, "call has one relation left, so we send it a release with the given cause %d.\n", cause);
567                 message = message_create(c_serial, c_relation->epoint_id, CALL_TO_EPOINT, MESSAGE_RELEASE);
568                 message->param.disconnectinfo.cause = cause;
569                 message->param.disconnectinfo.location = location;
570                 message_put(message);
571                 delete this;
572                 // end of call object!
573                 PDEBUG(DEBUG_CALL, "call completely removed!\n");
574         }
575
576         call = call_first;
577         while(call)
578         {
579                 if (options.deb & DEBUG_CALL && call->c_type==CALL_TYPE_PBX)
580                         callpbx_debug((class CallPBX *)call, "call_release{all calls left}");
581                 call = call->next;
582         }
583         PDEBUG(DEBUG_CALL, "call_release(): ended.\n");
584 }
585
586 /* count number of relations in a call
587  */
588 int callpbx_countrelations(unsigned long call_id)
589 {
590         struct call_relation *relation;
591         int i;
592         class Call *call;
593         class CallPBX *callpbx;
594
595         call = find_call_id(call_id);
596
597         if (!call)
598                 return(0);
599
600         if (call->c_type != CALL_TYPE_PBX)
601                 return(0);
602         callpbx = (class CallPBX *)call;
603
604         i = 0;
605         relation = callpbx->c_relation;
606         while(relation)
607         {
608                 i++;
609                 relation = relation->next;
610         }
611
612         return(i);
613 }
614
615 void CallPBX::remove_relation(struct call_relation *relation)
616 {
617         struct call_relation *temp, **tempp;
618
619         if (!relation)
620                 return;
621
622         temp = c_relation;
623         tempp = &c_relation;
624         while(temp)
625         {
626                 if (temp == relation)
627                         break;
628                 tempp = &temp->next;
629                 temp = temp->next;
630         }
631         if (!temp)
632         {
633                 PERROR("relation not in call.\n");
634                 return;
635         }
636
637         PDEBUG(DEBUG_CALL, "removing relation.\n");
638         *tempp = relation->next;
639         memset(temp, 0, sizeof(struct call_relation));
640         free(temp);
641         cmemuse--;
642 }       
643
644
645 struct call_relation *CallPBX::add_relation(void)
646 {
647         struct call_relation *relation;
648
649         if (!c_relation)
650         {
651                 PERROR("there is no first relation to this call\n");
652                 return(NULL);
653         }
654         relation = c_relation;
655         while(relation->next)
656                 relation = relation->next;
657
658         relation->next = (struct call_relation *)calloc(1, sizeof(struct call_relation));
659         if (!relation->next)
660         {
661                 PERROR("no memory\n");
662                 return(NULL);
663         }
664         cmemuse++;
665         memset(relation->next, 0, sizeof(struct call_relation));
666         /* the record pointer is set at the first time the data is received for the relation */
667
668 //      if (options.deb & DEBUG_CALL)
669 //              callpbx_debug(call, "add_relation");
670         return(relation->next);
671 }
672
673 /* epoint sends a message to a call
674  *
675  */
676 void CallPBX::message_epoint(unsigned long epoint_id, int message_type, union parameter *param)
677 {
678         class Call *cl;
679         struct call_relation *relation, *rel;
680         int num;
681         int new_state;
682         struct message *message;
683 //      int size, writesize, oldpointer;
684         class Endpoint *epoint;
685         char *number;
686
687         if (!epoint_id)
688         {
689                 PERROR("software error, epoint == NULL\n");
690                 return;
691         }
692
693 //      if (options.deb & DEBUG_CALL)
694 //      {
695 //              PDEBUG(DEBUG_CALL, "message %d received from ep%d.\n", message, epoint->ep_serial);
696 //              callpbx_debug(call,"Call::message_epoint");
697 //      }
698         if (options.deb & DEBUG_CALL)
699         {
700                 if (message_type != MESSAGE_DATA)
701                 {
702                         cl = call_first;
703                         while(cl)
704                         {
705                                 if (cl->c_type == CALL_TYPE_PBX)
706                                         callpbx_debug((class CallPBX *)cl, "Call::message_epoint{all calls before processing}");
707                                 cl = cl->next;
708                         }
709                 }
710         }
711
712         /* check relation */
713         relation = c_relation;
714         while(relation)
715         {
716                 if (relation->epoint_id == epoint_id)
717                         break;
718                 relation = relation->next;
719         }
720         if (!relation)
721         {
722 //              PERROR("no relation back to the endpoint found, ignoring (call=%d, endpoint=%d\n", c_serial, epoint_id);
723                 return;
724         }
725
726         switch(message_type)
727         {
728                 /* process channel message */
729                 case MESSAGE_CHANNEL:
730                 PDEBUG(DEBUG_CALL, "call received channel message: %d.\n", param->channel);
731                 if (relation->channel_state != param->channel)
732                 {
733                         relation->channel_state = param->channel;
734                         c_updatebridge = 1; /* update bridge flag */
735                         if (options.deb & DEBUG_CALL)
736                                 callpbx_debug(this, "Call::message_epoint{after setting new channel state}");
737                 }
738                 return;
739
740                 /* track notify */
741                 case MESSAGE_NOTIFY:
742                 switch(param->notifyinfo.notify)
743                 {
744                         case INFO_NOTIFY_USER_SUSPENDED:
745                         case INFO_NOTIFY_USER_RESUMED:
746                         case INFO_NOTIFY_REMOTE_HOLD:
747                         case INFO_NOTIFY_REMOTE_RETRIEVAL:
748                         case INFO_NOTIFY_CONFERENCE_ESTABLISHED:
749                         case INFO_NOTIFY_CONFERENCE_DISCONNECTED:
750                         new_state = track_notify(relation->rx_state, param->notifyinfo.notify);
751                         if (new_state != relation->rx_state)
752                         {
753                                 relation->rx_state = new_state;
754                                 c_updatebridge = 1;
755                                 if (options.deb & DEBUG_CALL)
756                                         callpbx_debug(this, "Call::message_epoint{after setting new rx state}");
757                         }
758                         break;
759
760                         default:
761                         /* send notification to all other endpoints */
762                         rel = c_relation;
763                         while(rel)
764                         {
765                                 if (rel->epoint_id!=epoint_id && rel->epoint_id)
766                                 {
767                                         message = message_create(c_serial, rel->epoint_id, CALL_TO_EPOINT, MESSAGE_NOTIFY);
768                                         memcpy(&message->param, param, sizeof(union parameter));
769                                         message_put(message);
770                                 }
771                                 rel = rel->next;
772                         }
773                 }
774                 return;
775
776                 /* audio data */
777                 case MESSAGE_DATA:
778                 /* now send audio data to the other endpoint */
779                 bridge_data(epoint_id, relation, param);
780                 return;
781         }
782
783         /* process party line */
784         if (message_type == MESSAGE_SETUP) if (param->setup.partyline)
785         {
786                 PDEBUG(DEBUG_CALL, "respsone with connect in partyline mode.\n");
787                 c_partyline = param->setup.partyline;
788                 message = message_create(c_serial, epoint_id, CALL_TO_EPOINT, MESSAGE_CONNECT);
789                 message->param.setup.partyline = c_partyline;
790                 message_put(message);
791                 c_updatebridge = 1; /* update bridge flag */
792         }
793         if (c_partyline)
794         {
795                 if (message_type == MESSAGE_DISCONNECT)
796                 {
797                         PDEBUG(DEBUG_CALL, "releasing after receiving disconnect, because call in partyline mode.\n");
798 //                      remove_relation(relation);
799                         message = message_create(c_serial, epoint_id, CALL_TO_EPOINT, MESSAGE_RELEASE);
800                         message->param.disconnectinfo.cause = CAUSE_NORMAL;
801                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
802                         message_put(message);
803 //                      c_updatebridge = 1; /* update bridge flag */
804                         return;
805                 }
806         }
807         if (c_partyline)
808         {
809                 PDEBUG(DEBUG_CALL, "ignoring message, because call in partyline mode.\n");
810                 return;
811         }
812
813         /* count relations */
814         num=callpbx_countrelations(c_serial);
815
816         /* check number of relations */
817         if (num > 2)
818         {
819                 PDEBUG(DEBUG_CALL, "call has more than two relations so there is no need to send a message.\n");
820                 return;
821         }
822
823         /* find interfaces not related to calling epoint */
824         relation = c_relation;
825         while(relation)
826         {
827                 if (relation->epoint_id != epoint_id)
828                         break;
829                 relation = relation->next;
830         }
831         if (!relation)
832         {
833                 switch(message_type)
834                 {
835                         case MESSAGE_SETUP:
836                         if (param->dialinginfo.itype == INFO_ITYPE_ISDN_EXTENSION)
837                         {
838                                 while(number = strsep(&param->dialinginfo.number, ','))
839                                 {
840                                         if (out_setup(epoint_id, message_type, param, number))
841                                                 return; // call destroyed
842                                 }
843                                 break;
844                         }
845                         if (out_setup(epoint_id, message_type, param, NULL))
846                                 return; // call destroyed
847                         break;
848
849                         default:
850                         PDEBUG(DEBUG_CALL, "no need to send a message because there is no other endpoint than the calling one.\n");
851                 }
852         } else
853         {
854                 PDEBUG(DEBUG_CALL, "sending message ep%ld -> ep%ld.\n", epoint_id, relation->epoint_id);
855                 message = message_create(c_serial, relation->epoint_id, CALL_TO_EPOINT, message_type);
856                 memcpy(&message->param, param, sizeof(union parameter));
857                 message_put(message);
858                 PDEBUG(DEBUG_CALL, "message sent.\n");
859         }
860 }
861
862
863 /* call process is called from the main loop
864  * it processes the current calling state.
865  * returns 0 if call nothing was done
866  */
867 int CallPBX::handler(void)
868 {
869 //      struct call_relation *relation;
870 //      char dialing[32][32];
871 //      int port[32];
872 //      int found;
873 //      int i, j;
874 //      char *p;
875
876         /* the bridge must be updated */
877         if (c_updatebridge)
878         {
879                 bridge();
880                 c_updatebridge = 0;
881                 return(1);
882         }
883
884         return(0);
885 }
886
887
888 int track_notify(int oldstate, int notify)
889 {
890         int newstate = oldstate;
891
892         switch(notify)
893         {
894                 case INFO_NOTIFY_USER_RESUMED:
895                 case INFO_NOTIFY_REMOTE_RETRIEVAL:
896                 case INFO_NOTIFY_CONFERENCE_DISCONNECTED:
897                 case INFO_NOTIFY_RESERVED_CT_1:
898                 case INFO_NOTIFY_RESERVED_CT_2:
899                 case INFO_NOTIFY_CALL_IS_DIVERTING:
900                 newstate = NOTIFY_STATE_ACTIVE;
901                 break;
902
903                 case INFO_NOTIFY_USER_SUSPENDED:
904                 newstate = NOTIFY_STATE_SUSPEND;
905                 break;
906
907                 case INFO_NOTIFY_REMOTE_HOLD:
908                 newstate = NOTIFY_STATE_HOLD;
909                 break;
910
911                 case INFO_NOTIFY_CONFERENCE_ESTABLISHED:
912                 newstate = NOTIFY_STATE_CONFERENCE;
913                 break;
914         }
915
916         return(newstate);
917 }
918
919
920 /*
921  * setup to exactly one endpoint
922  * if it fails, the calling endpoint is released.
923  * if other outgoing endpoints already exists, they are release as well.
924  * note: if this functions fails, it will destroy its own call object!
925  */
926 int CallPBX::out_setup(unsigned long epoint_id, int message_type, union parameter *param, char *newnumber)
927 {
928         struct call_relation *relation;
929         struct message *message;
930         class Endpoint *epoint;
931
932         PDEBUG(DEBUG_CALL, "no endpoint found, so we will create an endpoint and send the setup message we have.\n");
933         /* create a new relation */
934         if (!(relation=add_relation()))
935         {
936                 /* release due to error */
937                 ressource_error:
938                 relation = c_relation;
939                 while(relation)
940                 {
941                         message = message_create(c_serial, releation->epoint_id, CALL_TO_EPOINT, MESSAGE_RELEASE);
942                         message->param.disconnectinfo.cause = (relation->epoint_id==epoint_id)CAUSE_RESSOURCEUNAVAIL?:CAUSE_NORMAL; 
943                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
944                         message_put(message);
945                         relation = relation->next;
946                 }
947                 delete this;
948                 return(-ENOMEM);
949         }
950         relation->type = RELATION_TYPE_SETUP;
951         relation->channel_state = CHANNEL_STATE_HOLD; /* audio is assumed on a new call */
952         relation->tx_state = NOTIFY_STATE_ACTIVE; /* new calls always assumed to be active */
953         relation->rx_state = NOTIFY_STATE_ACTIVE; /* new calls always assumed to be active */
954         /* create a new endpoint */
955         epoint = new Endpoint(0, c_serial);
956         if (!epoint)
957         {
958                 remove_relation(relation);
959                 goto ressource_error;
960         }
961         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint)))
962         {
963                 PERROR("ERROR:No endpoint's app.\n");
964                 delete epoint;
965                 remove_relation(relation);
966                 goto ressource_error;
967         }
968         relation->epoint_id = epoint->ep_serial;
969         /* send setup message to new endpoint */
970 //printf("JOLLY DEBUG: %d\n",call_countrelations(c_serial));
971 //i                     if (options.deb & DEBUG_CALL)
972 //                              callpbx_debug(call, "Call::message_epoint");
973         message = message_create(c_serial, relation->epoint_id, CALL_TO_EPOINT, message_type);
974         memcpy(&message->param, param, sizeof(union parameter));
975         if (newnumber)
976                 SCPY(message->param.setup.dialinginfo.number, newnumber);
977         PDEBUG(DEBUG_CALL, "setup message sent to ep %d with number='%s'.\n", relation->epoint_id, message->param.setup.dialinginfo.number);
978         message_put(message);
979         return(0);
980 }
981
982 todo: beim release von einem relation_type_setup muss der cause gesammelt werden, bis keine weitere setup-relation mehr existiert
983 beim letzten den collected cause senden
984 bridge kann ruhig loslegen, das aber dokumentieren
985 bridge überdenken: wer sendet, welche töne verfügbar sind, u.s.w
986