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