unified socket application interface (for asterisk and maybe other apps)
[lcr.git] / joinpbx.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** join 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 join_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 (join_id && notify_off)
106         {
107                 message = message_create(join_id, epoint_id, JOIN_TO_EPOINT, MESSAGE_NOTIFY);
108                 message->param.notifyinfo.notify = notify_off;
109                 message_put(message);
110         }
111
112         if (join_id && notify_on)
113         {
114                 message = message_create(join_id, epoint_id, JOIN_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 join */
124 void joinpbx_debug(class JoinPBX *joinpbx, char *function)
125 {
126         struct join_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_JOIN))
133                 return;
134
135         PDEBUG(DEBUG_JOIN, "join(%d) start (called from %s)\n", joinpbx->j_serial, function);
136
137         relation = joinpbx->j_relation;
138
139         if (!relation)
140                 PDEBUG(DEBUG_JOIN, "join has no relations\n");
141         while(relation)
142         {
143                 epoint = find_epoint_id(relation->epoint_id);
144                 if (!epoint)
145                 {
146                         PDEBUG(DEBUG_JOIN, "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_join_id==joinpbx->j_serial)?"yes":"no", (epoint->get_hold_id()==joinpbx->j_serial)?"yes":"no");
164                 UPRINT(strchr(buffer,0), " endpoint=%d on=%s", epoint->ep_serial, (epoint->ep_join_id==joinpbx->j_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_JOIN, "%s\n", buffer);
229                 relation = relation->next;
230         }
231
232         PDEBUG(DEBUG_JOIN, "end\n");
233 }
234
235
236 /*
237  * constructor for a new join 
238  * the join will have a relation to the calling endpoint
239  */
240 JoinPBX::JoinPBX(class Endpoint *epoint) : Join()
241 {
242         struct join_relation *relation;
243 //      char filename[256];
244
245         if (!epoint)
246                 FATAL("epoint is NULL.\n");
247
248         PDEBUG(DEBUG_JOIN, "creating new join and connecting it to the endpoint.\n");
249
250         j_type = JOIN_TYPE_PBX;
251         j_caller[0] = '\0';
252         j_caller_id[0] = '\0';
253         j_dialed[0] = '\0';
254         j_todial[0] = '\0';
255         j_pid = getpid();
256         j_updatebridge = 0;
257         j_partyline = 0;
258         j_multicause = CAUSE_NOUSER;
259         j_multilocation = LOCATION_PRIVATE_LOCAL;
260
261         /* initialize a relation only to the calling interface */
262         relation = j_relation = (struct join_relation *)MALLOC(sizeof(struct join_relation));
263         cmemuse++;
264         relation->type = RELATION_TYPE_CALLING;
265         relation->channel_state = CHANNEL_STATE_HOLD; /* audio is assumed on a new join */
266         relation->tx_state = NOTIFY_STATE_ACTIVE; /* new joins always assumed to be active */
267         relation->rx_state = NOTIFY_STATE_ACTIVE; /* new joins always assumed to be active */
268         relation->epoint_id = epoint->ep_serial;
269
270
271         if (options.deb & DEBUG_JOIN)
272                 joinpbx_debug(this, "JoinPBX::Constructor(new join)");
273 }
274
275
276 /*
277  * join descructor
278  */
279 JoinPBX::~JoinPBX()
280 {
281         struct join_relation *relation, *rtemp;
282
283         relation = j_relation;
284         while(relation)
285         {
286                 rtemp = relation->next;
287                 FREE(relation, sizeof(struct join_relation));
288                 cmemuse--;
289                 relation = rtemp;
290         }
291 }
292
293
294 /* bridge sets the audio flow of all bchannels assiociated to 'this' join
295  * also it changes and notifies active/hold/conference states
296  */
297 void JoinPBX::bridge(void)
298 {
299         struct join_relation *relation;
300         struct message *message;
301         int numconnect = 0, relations = 0;
302         class Endpoint *epoint;
303         struct port_list *portlist;
304         class Port *port;
305 #ifdef DEBUG_COREBRIDGE
306         int allmISDN = 0; // never set for debug purpose
307 #else
308         int allmISDN = 1; // set until a non-mISDN relation is found
309 #endif
310
311         relation = j_relation;
312         while(relation)
313         {
314                 /* count all relations */
315                 relations++;
316
317                 /* check for relation's objects */
318                 epoint = find_epoint_id(relation->epoint_id);
319                 if (!epoint)
320                 {
321                         PERROR("software error: relation without existing endpoints.\n");
322                         relation = relation->next;
323                         continue;
324                 }
325                 portlist = epoint->ep_portlist;
326                 if (!portlist)
327                 {
328                         PDEBUG(DEBUG_JOIN, "join%d ignoring relation without port object.\n", j_serial);
329 //#warning testing: keep on hold until single audio stream available
330                         relation->channel_state = CHANNEL_STATE_HOLD;
331                         relation = relation->next;
332                         continue;
333                 }
334                 if (portlist->next)
335                 {
336                         PDEBUG(DEBUG_JOIN, "join%d ignoring relation with ep%d due to port_list.\n", j_serial, epoint->ep_serial);
337 //#warning testing: keep on hold until single audio stream available
338                         relation->channel_state = CHANNEL_STATE_HOLD;
339                         relation = relation->next;
340                         continue;
341                 }
342                 port = find_port_id(portlist->port_id);
343                 if (!port)
344                 {
345                         PDEBUG(DEBUG_JOIN, "join%d ignoring relation without existing port object.\n", j_serial);
346                         relation = relation->next;
347                         continue;
348                 }
349                 if ((port->p_type&PORT_CLASS_MASK)!=PORT_CLASS_mISDN)
350                 {
351                         PDEBUG(DEBUG_JOIN, "join%d ignoring relation ep%d because it's port is not mISDN.\n", j_serial, epoint->ep_serial);
352                         if (allmISDN)
353                         {
354                                 PDEBUG(DEBUG_JOIN, "join%d not all endpoints are mISDN.\n", j_serial);
355                                 allmISDN = 0;
356                         }
357                         relation = relation->next;
358                         continue;
359                 }
360                 
361                 relation = relation->next;
362         }
363
364         PDEBUG(DEBUG_JOIN, "join%d members=%d %s\n", j_serial, relations, (allmISDN)?"(all are mISDN-members)":"(not all are mISDN-members)");
365         /* we notify all relations about rxdata. */
366         relation = j_relation;
367         while(relation)
368         {
369                 /* count connected relations */
370                 if ((relation->channel_state == CHANNEL_STATE_CONNECT)
371                  && (relation->rx_state != NOTIFY_STATE_SUSPEND)
372                  && (relation->rx_state != NOTIFY_STATE_HOLD))
373                         numconnect ++;
374
375                 /* remove unconnected parties from conference, also remove remotely disconnected parties so conference will not be disturbed. */
376                 if (relation->channel_state == CHANNEL_STATE_CONNECT
377                  && relation->rx_state != NOTIFY_STATE_HOLD
378                  && relation->rx_state != NOTIFY_STATE_SUSPEND
379                  && relations>1 // no conf with one member
380                  && allmISDN) // no conf if any member is not mISDN
381                 {
382                         message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_mISDNSIGNAL);
383                         message->param.mISDNsignal.message = mISDNSIGNAL_CONF;
384                         message->param.mISDNsignal.conf = j_serial<<16 | j_pid;
385                         PDEBUG(DEBUG_JOIN, "join%d EP%d +on+ id: 0x%08x\n", j_serial, relation->epoint_id, message->param.mISDNsignal.conf);
386                         message_put(message);
387                 } else
388                 {
389                         message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_mISDNSIGNAL);
390                         message->param.mISDNsignal.message = mISDNSIGNAL_CONF;
391                         message->param.mISDNsignal.conf = 0;
392                         PDEBUG(DEBUG_JOIN, "join%d EP%d +off+ id: 0x%08x\n", j_serial, relation->epoint_id, message->param.mISDNsignal.conf);
393                         message_put(message);
394                 }
395
396                 /*
397                  * request data from endpoint/port if:
398                  * - two relations
399                  * - any without mISDN
400                  * in this case we bridge
401                  */
402                 message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_mISDNSIGNAL);
403                 message->param.mISDNsignal.message = mISDNSIGNAL_JOINDATA;
404                 message->param.mISDNsignal.joindata = (relations==2 && !allmISDN);
405                 PDEBUG(DEBUG_JOIN, "join%d EP%d set joindata=%d\n", j_serial, relation->epoint_id, message->param.mISDNsignal.joindata);
406                 message_put(message);
407
408                 relation = relation->next;
409         }
410
411         /* two people just exchange their states */
412         if (relations==2 && !j_partyline)
413         {
414                 PDEBUG(DEBUG_JOIN, "join%d 2 relations / no partyline\n", j_serial);
415                 relation = j_relation;
416                 relation->tx_state = notify_state_change(j_serial, relation->epoint_id, relation->tx_state, relation->next->rx_state);
417                 relation->next->tx_state = notify_state_change(j_serial, relation->next->epoint_id, relation->next->tx_state, relation->rx_state);
418         } else
419         /* one member in a join, so we put her on hold */
420         if (relations==1 || numconnect==1)
421         {
422                 PDEBUG(DEBUG_JOIN, "join%d 1 member or only 1 connected, put on hold\n");
423                 relation = j_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(j_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                 PDEBUG(DEBUG_JOIN, "join%d %d members, %d connected, signal conference\n", relations, numconnect);
436                 relation = j_relation;
437                 while(relation)
438                 {
439                         if ((relation->channel_state == CHANNEL_STATE_CONNECT)
440                          && (relation->rx_state != NOTIFY_STATE_SUSPEND)
441                          && (relation->rx_state != NOTIFY_STATE_HOLD))
442                                 relation->tx_state = notify_state_change(j_serial, relation->epoint_id, relation->tx_state, NOTIFY_STATE_CONFERENCE);
443                         relation = relation->next;
444                 }
445         }
446 }
447
448 /*
449  * bridging is only possible with two connected endpoints
450  */
451 void JoinPBX::bridge_data(unsigned long epoint_from, struct join_relation *relation_from, union parameter *param)
452 {
453         struct join_relation *relation_to;
454
455         /* if we are alone */
456         if (!j_relation->next)
457                 return;
458
459         /* if we are more than two */
460         if (j_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 = j_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 destination 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 //printf("from %d, to %d\n", relation_from->epoint_id, relation_to->epoint_id);
483         message_forward(j_serial, relation_to->epoint_id, JOIN_TO_EPOINT, param);
484 }
485
486 /* release join from endpoint
487  * if the join has two relations, all relations are freed and the join will be
488  * destroyed
489  * on outgoing relations, the cause is collected, if not connected
490  * returns if join has been destroyed
491  */
492 int JoinPBX::release(struct join_relation *relation, int location, int cause)
493 {
494         struct join_relation *reltemp, **relationpointer;
495         struct message *message;
496         class Join *join;
497         int destroy = 0;
498
499         /* remove from bridge */
500         if (relation->channel_state != CHANNEL_STATE_HOLD)
501         {
502                 relation->channel_state = CHANNEL_STATE_HOLD;
503                 j_updatebridge = 1; /* update bridge flag */
504                 // note: if join is not released, bridge must be updated
505         }
506
507         /* detach given interface */
508         reltemp = j_relation;
509         relationpointer = &j_relation;
510         while(reltemp)
511         {
512                 /* endpoint of function call */
513                 if (relation == reltemp)
514                         break;
515                 relationpointer = &reltemp->next;
516                 reltemp = reltemp->next;
517         }
518         if (!reltemp)
519                 FATAL("relation not in list of our relations. this must not happen.\n");
520         *relationpointer = reltemp->next;
521         FREE(reltemp, sizeof(struct join_relation));
522         cmemuse--;
523         relation = reltemp = NULL; // just in case of reuse fault;
524
525         /* if no more relation */
526         if (!j_relation)
527         {
528                 PDEBUG(DEBUG_JOIN, "join is completely removed.\n");
529                 /* there is no more endpoint related to the join */
530                 destroy = 1;
531                 delete this;
532                 // end of join object!
533                 PDEBUG(DEBUG_JOIN, "join completely removed!\n");
534         } else
535         /* if join is a party line */
536         if (j_partyline)
537         {
538                 PDEBUG(DEBUG_JOIN, "join is a conference room, so we keep it alive until the last party left.\n");
539         } else
540         /* if only one relation left */
541         if (!j_relation->next)
542         {
543                 PDEBUG(DEBUG_JOIN, "join has one relation left, so we send it a release with the given cause %d.\n", cause);
544                 message = message_create(j_serial, j_relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_RELEASE);
545                 message->param.disconnectinfo.cause = cause;
546                 message->param.disconnectinfo.location = location;
547                 message_put(message);
548                 destroy = 1;
549                 delete this;
550                 // end of join object!
551                 PDEBUG(DEBUG_JOIN, "join completely removed!\n");
552         }
553
554         join = join_first;
555         while(join)
556         {
557                 if (options.deb & DEBUG_JOIN && join->j_type==JOIN_TYPE_PBX)
558                         joinpbx_debug((class JoinPBX *)join, "join_release{all joins left}");
559                 join = join->next;
560         }
561         PDEBUG(DEBUG_JOIN, "join_release(): ended.\n");
562         return(destroy);
563 }
564
565 /* count number of relations in a join
566  */
567 int joinpbx_countrelations(unsigned long join_id)
568 {
569         struct join_relation *relation;
570         int i;
571         class Join *join;
572         class JoinPBX *joinpbx;
573
574         join = find_join_id(join_id);
575
576         if (!join)
577                 return(0);
578
579         if (join->j_type != JOIN_TYPE_REMOTE)
580                 return(2);
581
582         if (join->j_type != JOIN_TYPE_PBX)
583                 return(0);
584         joinpbx = (class JoinPBX *)join;
585
586         i = 0;
587         relation = joinpbx->j_relation;
588         while(relation)
589         {
590                 i++;
591                 relation = relation->next;
592         }
593
594         return(i);
595 }
596
597 void JoinPBX::remove_relation(struct join_relation *relation)
598 {
599         struct join_relation *temp, **tempp;
600
601         if (!relation)
602                 return;
603
604         temp = j_relation;
605         tempp = &j_relation;
606         while(temp)
607         {
608                 if (temp == relation)
609                         break;
610                 tempp = &temp->next;
611                 temp = temp->next;
612         }
613         if (!temp)
614         {
615                 PERROR("relation not in join.\n");
616                 return;
617         }
618
619         PDEBUG(DEBUG_JOIN, "removing relation.\n");
620         *tempp = relation->next;
621         FREE(temp, sizeof(struct join_relation));
622         cmemuse--;
623 }       
624
625
626 struct join_relation *JoinPBX::add_relation(void)
627 {
628         struct join_relation *relation;
629
630         if (!j_relation)
631         {
632                 PERROR("there is no first relation to this join\n");
633                 return(NULL);
634         }
635         relation = j_relation;
636         while(relation->next)
637                 relation = relation->next;
638
639         relation->next = (struct join_relation *)MALLOC(sizeof(struct join_relation));
640         cmemuse++;
641         /* the record pointer is set at the first time the data is received for the relation */
642
643 //      if (options.deb & DEBUG_JOIN)
644 //              joinpbx_debug(join, "add_relation");
645         return(relation->next);
646 }
647
648 /* epoint sends a message to a join
649  *
650  */
651 void JoinPBX::message_epoint(unsigned long epoint_id, int message_type, union parameter *param)
652 {
653         class Join *cl;
654         struct join_relation *relation, *reltemp;
655         int num;
656         int new_state;
657         struct message *message;
658 //      int size, writesize, oldpointer;
659         char *number, *numbers;
660
661         if (!epoint_id)
662         {
663                 PERROR("software error, epoint == NULL\n");
664                 return;
665         }
666
667 //      if (options.deb & DEBUG_JOIN)
668 //      {
669 //              PDEBUG(DEBUG_JOIN, "message %d received from ep%d.\n", message, epoint->ep_serial);
670 //              joinpbx_debug(join,"Join::message_epoint");
671 //      }
672         if (options.deb & DEBUG_JOIN)
673         {
674                 if (message_type != MESSAGE_DATA)
675                 {
676                         cl = join_first;
677                         while(cl)
678                         {
679                                 if (cl->j_type == JOIN_TYPE_PBX)
680                                         joinpbx_debug((class JoinPBX *)cl, "Join::message_epoint{all joins before processing}");
681                                 cl = cl->next;
682                         }
683                 }
684         }
685
686         /* check relation */
687         relation = j_relation;
688         while(relation)
689         {
690                 if (relation->epoint_id == epoint_id)
691                         break;
692                 relation = relation->next;
693         }
694         if (!relation)
695         {
696                 PDEBUG(DEBUG_JOIN, "no relation back to the endpoint found, ignoring (join=%d, endpoint=%d)\n", j_serial, epoint_id);
697                 return;
698         }
699
700         switch(message_type)
701         {
702                 /* process audio path message */
703                 case MESSAGE_AUDIOPATH:
704                 PDEBUG(DEBUG_JOIN, "join received channel message: %d.\n", param->audiopath);
705                 if (relation->channel_state != param->audiopath)
706                 {
707                         relation->channel_state = param->audiopath;
708                         j_updatebridge = 1; /* update bridge flag */
709                         if (options.deb & DEBUG_JOIN)
710                                 joinpbx_debug(this, "Join::message_epoint{after setting new channel state}");
711                 }
712                 return;
713
714                 /* track notify */
715                 case MESSAGE_NOTIFY:
716                 switch(param->notifyinfo.notify)
717                 {
718                         case INFO_NOTIFY_USER_SUSPENDED:
719                         case INFO_NOTIFY_USER_RESUMED:
720                         case INFO_NOTIFY_REMOTE_HOLD:
721                         case INFO_NOTIFY_REMOTE_RETRIEVAL:
722                         case INFO_NOTIFY_CONFERENCE_ESTABLISHED:
723                         case INFO_NOTIFY_CONFERENCE_DISCONNECTED:
724                         new_state = track_notify(relation->rx_state, param->notifyinfo.notify);
725                         if (new_state != relation->rx_state)
726                         {
727                                 relation->rx_state = new_state;
728                                 j_updatebridge = 1;
729                                 if (options.deb & DEBUG_JOIN)
730                                         joinpbx_debug(this, "Join::message_epoint{after setting new rx state}");
731                         }
732                         break;
733
734                         default:
735                         /* send notification to all other endpoints */
736                         reltemp = j_relation;
737                         while(reltemp)
738                         {
739                                 if (reltemp->epoint_id!=epoint_id && reltemp->epoint_id)
740                                 {
741                                         message = message_create(j_serial, reltemp->epoint_id, JOIN_TO_EPOINT, MESSAGE_NOTIFY);
742                                         memcpy(&message->param, param, sizeof(union parameter));
743                                         message_put(message);
744                                 }
745                                 reltemp = reltemp->next;
746                         }
747                 }
748                 return;
749
750                 /* audio data */
751                 case MESSAGE_DATA:
752                 /* now send audio data to the other endpoint */
753                 bridge_data(epoint_id, relation, param);
754                 return;
755
756                 /* relations sends a connect */
757                 case MESSAGE_CONNECT:
758                 /* outgoing setup type becomes connected */
759                 if (relation->type == RELATION_TYPE_SETUP)
760                         relation->type = RELATION_TYPE_CONNECT;
761                 /* release other relations in setup state */
762                 release_again:
763                 relation = j_relation;
764                 while(relation)
765                 {
766                         if (relation->type == RELATION_TYPE_SETUP)
767                         {
768                                 if (release(relation, LOCATION_PRIVATE_LOCAL, CAUSE_NONSELECTED))
769                                         return; // must return, because join IS destroyed
770                                 goto release_again;
771                         }
772                         relation = relation->next;
773                 }
774                 break; // continue with our message
775
776                 /* release is sent by endpoint */
777                 case MESSAGE_RELEASE:
778                 if (relation->type == RELATION_TYPE_SETUP)
779                 {
780                         /* collect cause and send collected cause */
781                         collect_cause(&j_multicause, &j_multilocation, param->disconnectinfo.cause, param->disconnectinfo.location);
782                         release(relation, j_multilocation, j_multicause);
783                 } else
784                 {
785                         /* send current cause */
786                         release(relation, param->disconnectinfo.location, param->disconnectinfo.cause);
787                 }
788                 return; // must return, because join may be destroyed
789         }
790
791         /* process party line */
792         if (message_type == MESSAGE_SETUP) if (param->setup.partyline)
793         {
794                 PDEBUG(DEBUG_JOIN, "respsone with connect in partyline mode.\n");
795                 j_partyline = param->setup.partyline;
796                 message = message_create(j_serial, epoint_id, JOIN_TO_EPOINT, MESSAGE_CONNECT);
797                 message->param.setup.partyline = j_partyline;
798                 message_put(message);
799                 j_updatebridge = 1; /* update bridge flag */
800         }
801         if (j_partyline)
802         {
803                 if (message_type == MESSAGE_DISCONNECT)
804                 {
805                         PDEBUG(DEBUG_JOIN, "releasing after receiving disconnect, because join in partyline mode.\n");
806                         message = message_create(j_serial, epoint_id, JOIN_TO_EPOINT, MESSAGE_RELEASE);
807                         message->param.disconnectinfo.cause = CAUSE_NORMAL;
808                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
809                         message_put(message);
810                         return;
811                 }
812         }
813         if (j_partyline)
814         {
815                 PDEBUG(DEBUG_JOIN, "ignoring message, because join in partyline mode.\n");
816                 return;
817         }
818
819         /* count relations */
820         num=joinpbx_countrelations(j_serial);
821
822         /* check number of relations */
823         if (num > 2)
824         {
825                 PDEBUG(DEBUG_JOIN, "join has more than two relations so there is no need to send a message.\n");
826                 return;
827         }
828
829         /* find interfaces not related to calling epoint */
830         relation = j_relation;
831         while(relation)
832         {
833                 if (relation->epoint_id != epoint_id)
834                         break;
835                 relation = relation->next;
836         }
837         if (!relation)
838         {
839                 switch(message_type)
840                 {
841                         case MESSAGE_SETUP:
842                         if (param->setup.dialinginfo.itype == INFO_ITYPE_ISDN_EXTENSION)
843                         {
844                                 numbers = param->setup.dialinginfo.id;
845                                 while((number = strsep(&numbers, ",")))
846                                 {
847                                         if (out_setup(epoint_id, message_type, param, number))
848                                                 return; // join destroyed
849                                 }
850                                 break;
851                         }
852                         if (out_setup(epoint_id, message_type, param, NULL))
853                                 return; // join destroyed
854                         break;
855
856                         default:
857                         PDEBUG(DEBUG_JOIN, "no need to send a message because there is no other endpoint than the calling one.\n");
858                 }
859         } else
860         {
861                 PDEBUG(DEBUG_JOIN, "sending message ep%ld -> ep%ld.\n", epoint_id, relation->epoint_id);
862                 message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, message_type);
863                 memcpy(&message->param, param, sizeof(union parameter));
864                 message_put(message);
865                 PDEBUG(DEBUG_JOIN, "message sent.\n");
866         }
867 }
868
869
870 /* join process is called from the main loop
871  * it processes the current calling state.
872  * returns 0 if join nothing was done
873  */
874 int JoinPBX::handler(void)
875 {
876 //      struct join_relation *relation;
877 //      char dialing[32][32];
878 //      int port[32];
879 //      int found;
880 //      int i, j;
881 //      char *p;
882
883         /* the bridge must be updated */
884         if (j_updatebridge)
885         {
886                 bridge();
887                 j_updatebridge = 0;
888                 return(1);
889         }
890
891         return(0);
892 }
893
894
895 int track_notify(int oldstate, int notify)
896 {
897         int newstate = oldstate;
898
899         switch(notify)
900         {
901                 case INFO_NOTIFY_USER_RESUMED:
902                 case INFO_NOTIFY_REMOTE_RETRIEVAL:
903                 case INFO_NOTIFY_CONFERENCE_DISCONNECTED:
904                 case INFO_NOTIFY_RESERVED_CT_1:
905                 case INFO_NOTIFY_RESERVED_CT_2:
906                 case INFO_NOTIFY_CALL_IS_DIVERTING:
907                 newstate = NOTIFY_STATE_ACTIVE;
908                 break;
909
910                 case INFO_NOTIFY_USER_SUSPENDED:
911                 newstate = NOTIFY_STATE_SUSPEND;
912                 break;
913
914                 case INFO_NOTIFY_REMOTE_HOLD:
915                 newstate = NOTIFY_STATE_HOLD;
916                 break;
917
918                 case INFO_NOTIFY_CONFERENCE_ESTABLISHED:
919                 newstate = NOTIFY_STATE_CONFERENCE;
920                 break;
921         }
922
923         return(newstate);
924 }
925
926
927 /*
928  * setup to exactly one endpoint
929  * if it fails, the calling endpoint is released.
930  * if other outgoing endpoints already exists, they are release as well.
931  * note: if this functions fails, it will destroy its own join object!
932  */
933 int JoinPBX::out_setup(unsigned long epoint_id, int message_type, union parameter *param, char *newnumber)
934 {
935         struct join_relation *relation;
936         struct message *message;
937         class Endpoint *epoint;
938
939         PDEBUG(DEBUG_JOIN, "no endpoint found, so we will create an endpoint and send the setup message we have.\n");
940         /* create a new relation */
941         if (!(relation=add_relation()))
942                 FATAL("No memory for relation.\n");
943         relation->type = RELATION_TYPE_SETUP;
944         relation->channel_state = CHANNEL_STATE_HOLD; /* audio is assumed on a new join */
945         relation->tx_state = NOTIFY_STATE_ACTIVE; /* new joins always assumed to be active */
946         relation->rx_state = NOTIFY_STATE_ACTIVE; /* new joins always assumed to be active */
947         /* create a new endpoint */
948         epoint = new Endpoint(0, j_serial, 0);
949         if (!epoint)
950                 FATAL("No memory for Endpoint instance\n");
951         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint)))
952                 FATAL("No memory for Endpoint Application instance\n");
953         relation->epoint_id = epoint->ep_serial;
954         /* send setup message to new endpoint */
955 //printf("JOLLY DEBUG: %d\n",join_countrelations(j_serial));
956 //i                     if (options.deb & DEBUG_JOIN)
957 //                              joinpbx_debug(join, "Join::message_epoint");
958         message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, message_type);
959         memcpy(&message->param, param, sizeof(union parameter));
960         if (newnumber)
961                 SCPY(message->param.setup.dialinginfo.id, newnumber);
962         PDEBUG(DEBUG_JOIN, "setup message sent to ep %d with number='%s'.\n", relation->epoint_id, message->param.setup.dialinginfo.id);
963         message_put(message);
964         return(0);
965 }
966
967