Use variables set by configure script instead of installing files to a
[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                  * Bridge between port instances if:
371                  * - two relations
372                  * - one or all are not mISDN
373                  */
374                 message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_BRIDGE);
375                 message->param.bridge_id = (relations==2 && !allmISDN) ? j_serial : 0;
376                 PDEBUG(DEBUG_JOIN, "join%u EP%u requests bridge=%u\n", j_serial, relation->epoint_id, message->param.bridge_id);
377                 message_put(message);
378
379                 relation = relation->next;
380         }
381
382         /* two people just exchange their states */
383         if (relations==2 && !j_partyline) {
384                 PDEBUG(DEBUG_JOIN, "join%d 2 relations / no partyline\n", j_serial);
385                 relation = j_relation;
386                 relation->tx_state = notify_state_change(j_serial, relation->epoint_id, relation->tx_state, relation->next->rx_state);
387                 relation->next->tx_state = notify_state_change(j_serial, relation->next->epoint_id, relation->next->tx_state, relation->rx_state);
388         } else
389         /* one member in a join, so we put her on hold */
390         if ((relations==1 || numconnect==1)/* && !j_partyline_jingle*/) {
391                 PDEBUG(DEBUG_JOIN, "join%d 1 member or only 1 connected, put on hold\n", j_serial);
392                 relation = j_relation;
393                 while(relation) {
394                         if ((relation->channel_state == 1)
395                          && (relation->rx_state != NOTIFY_STATE_SUSPEND)
396                          && (relation->rx_state != NOTIFY_STATE_HOLD))
397                                 relation->tx_state = notify_state_change(j_serial, relation->epoint_id, relation->tx_state, NOTIFY_STATE_HOLD);
398                         relation = relation->next;
399                 }
400         } else {
401         /* if conference/partyline (or more than two members and more than one is connected), so we set conference state */ 
402                 PDEBUG(DEBUG_JOIN, "join%d %d members, %d connected, signal conference\n", j_serial, relations, numconnect);
403                 relation = j_relation;
404                 while(relation) {
405                         if ((relation->channel_state == 1)
406                          && (relation->rx_state != NOTIFY_STATE_SUSPEND)
407                          && (relation->rx_state != NOTIFY_STATE_HOLD))
408                                 relation->tx_state = notify_state_change(j_serial, relation->epoint_id, relation->tx_state, NOTIFY_STATE_CONFERENCE);
409                         relation = relation->next;
410                 }
411         }
412 }
413
414 /* release join from endpoint
415  * if the join has two relations, all relations are freed and the join will be
416  * destroyed
417  * on outgoing relations, the cause is collected, if not connected
418  * returns if join has been destroyed
419  */
420 int JoinPBX::release(struct join_relation *relation, int location, int cause)
421 {
422         struct join_relation *reltemp, **relationpointer;
423         struct lcr_msg *message;
424         class Join *join;
425         int destroy = 0;
426
427         /* remove from bridge */
428         if (relation->channel_state != 0) {
429                 relation->channel_state = 0;
430                 trigger_work(&j_updatebridge);
431                 // note: if join is not released, bridge must be updated
432         }
433
434         /* detach given interface */
435         reltemp = j_relation;
436         relationpointer = &j_relation;
437         while(reltemp) {
438                 /* endpoint of function call */
439                 if (relation == reltemp)
440                         break;
441                 relationpointer = &reltemp->next;
442                 reltemp = reltemp->next;
443         }
444         if (!reltemp)
445                 FATAL("relation not in list of our relations. this must not happen.\n");
446 //printf("releasing relation %d\n", reltemp->epoint_id);
447         *relationpointer = reltemp->next;
448         FREE(reltemp, sizeof(struct join_relation));
449         cmemuse--;
450         relation = reltemp = NULL; // just in case of reuse fault;
451
452         /* if no more relation */
453         if (!j_relation) {
454                 PDEBUG(DEBUG_JOIN, "join is completely removed.\n");
455                 /* there is no more endpoint related to the join */
456                 destroy = 1;
457                 delete this;
458                 // end of join object!
459                 PDEBUG(DEBUG_JOIN, "join completely removed!\n");
460         } else
461         /* if join is a party line */
462         if (j_partyline) {
463                 PDEBUG(DEBUG_JOIN, "join is a conference room, so we keep it alive until the last party left.\n");
464         } else
465         /* if only one relation left */
466         if (!j_relation->next) {
467                 PDEBUG(DEBUG_JOIN, "join has one relation left, so we send it a release with the given cause %d.\n", cause);
468                 message = message_create(j_serial, j_relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_RELEASE);
469                 message->param.disconnectinfo.cause = cause;
470                 message->param.disconnectinfo.location = location;
471                 message_put(message);
472                 destroy = 1;
473                 delete this;
474                 // end of join object!
475                 PDEBUG(DEBUG_JOIN, "join completely removed!\n");
476         }
477
478         join = join_first;
479         while(join) {
480                 if (options.deb & DEBUG_JOIN && join->j_type==JOIN_TYPE_PBX)
481                         joinpbx_debug((class JoinPBX *)join, "join_release{all joins left}");
482                 join = join->next;
483         }
484         PDEBUG(DEBUG_JOIN, "join_release(): ended.\n");
485         return(destroy);
486 }
487
488 /* count number of relations in a join
489  */
490 int joinpbx_countrelations(unsigned int join_id)
491 {
492         struct join_relation *relation;
493         int i;
494         class Join *join;
495         class JoinPBX *joinpbx;
496
497         join = find_join_id(join_id);
498
499         if (!join)
500                 return(0);
501
502         if (join->j_type == JOIN_TYPE_REMOTE)
503                 return(2);
504
505         if (join->j_type != JOIN_TYPE_PBX)
506                 return(0);
507         joinpbx = (class JoinPBX *)join;
508
509         i = 0;
510         relation = joinpbx->j_relation;
511         while(relation) {
512                 i++;
513                 relation = relation->next;
514         }
515
516         return(i);
517 }
518
519 /* check if one is calling and all other relations are setup-realations */
520 int joinpbx_onecalling_othersetup(struct join_relation *relation)
521 {
522         int calling = 0, other = 0;
523
524         while(relation) {
525                 switch(relation->type) {
526                 case RELATION_TYPE_CALLING:
527                         calling++;
528                         break;
529                 case RELATION_TYPE_SETUP:
530                         break;
531                 default:
532                         other++;
533                         break;
534                 }
535
536                 relation = relation->next;
537         }
538
539         if (calling == 1 && other == 0)
540                 return(1);
541         return(0);
542 }
543
544 void JoinPBX::remove_relation(struct join_relation *relation)
545 {
546         struct join_relation *temp, **tempp;
547
548         if (!relation)
549                 return;
550
551         temp = j_relation;
552         tempp = &j_relation;
553         while(temp) {
554                 if (temp == relation)
555                         break;
556                 tempp = &temp->next;
557                 temp = temp->next;
558         }
559         if (!temp) {
560                 PERROR("relation not in join.\n");
561                 return;
562         }
563
564         PDEBUG(DEBUG_JOIN, "removing relation.\n");
565         *tempp = relation->next;
566         FREE(temp, sizeof(struct join_relation));
567         cmemuse--;
568 }       
569
570
571 struct join_relation *JoinPBX::add_relation(void)
572 {
573         struct join_relation *relation;
574
575         if (!j_relation) {
576                 PERROR("there is no first relation to this join\n");
577                 return(NULL);
578         }
579         relation = j_relation;
580         while(relation->next)
581                 relation = relation->next;
582
583         relation->next = (struct join_relation *)MALLOC(sizeof(struct join_relation));
584         cmemuse++;
585         /* the record pointer is set at the first time the data is received for the relation */
586
587 //      if (options.deb & DEBUG_JOIN)
588 //              joinpbx_debug(join, "add_relation");
589         return(relation->next);
590 }
591
592 /* epoint sends a message to a join
593  *
594  */
595 void JoinPBX::message_epoint(unsigned int epoint_id, int message_type, union parameter *param)
596 {
597         class Join *cl;
598         struct join_relation *relation, *reltemp;
599         int num;
600         int new_state;
601         struct lcr_msg *message;
602 //      int size, writesize, oldpointer;
603         char *number, *numbers;
604
605         if (!epoint_id) {
606                 PERROR("software error, epoint == NULL\n");
607                 return;
608         }
609
610 //      if (options.deb & DEBUG_JOIN) {
611 //              PDEBUG(DEBUG_JOIN, "message %d received from ep%d.\n", message, epoint->ep_serial);
612 //              joinpbx_debug(join,"Join::message_epoint");
613 //      }
614         if (options.deb & DEBUG_JOIN) {
615                 if (message_type) {
616                         cl = join_first;
617                         while(cl) {
618                                 if (cl->j_type == JOIN_TYPE_PBX)
619                                         joinpbx_debug((class JoinPBX *)cl, "Join::message_epoint{all joins before processing}");
620                                 cl = cl->next;
621                         }
622                 }
623         }
624
625         /* check relation */
626         relation = j_relation;
627         while(relation) {
628                 if (relation->epoint_id == epoint_id)
629                         break;
630                 relation = relation->next;
631         }
632         if (!relation) {
633                 PDEBUG(DEBUG_JOIN, "no relation back to the endpoint found, ignoring (join=%d, endpoint=%d)\n", j_serial, epoint_id);
634                 return;
635         }
636
637         /* count relations */
638         num=joinpbx_countrelations(j_serial);
639
640         /* process party line */
641         if (message_type == MESSAGE_SETUP) if (param->setup.partyline && !j_partyline) {
642                 j_partyline = param->setup.partyline;
643                 j_partyline_jingle = param->setup.partyline_jingle;
644         }
645         if (j_partyline) {
646                 switch(message_type) {
647                         case MESSAGE_SETUP:
648                         PDEBUG(DEBUG_JOIN, "respsone with connect in partyline mode.\n");
649                         relation->type = RELATION_TYPE_CONNECT;
650                         message = message_create(j_serial, epoint_id, JOIN_TO_EPOINT, MESSAGE_CONNECT);
651                         SPRINT(message->param.connectinfo.id, "%d", j_partyline);
652                         message->param.connectinfo.ntype = INFO_NTYPE_UNKNOWN;
653                         message_put(message);
654                         trigger_work(&j_updatebridge);
655                         if (j_partyline_jingle)
656                                play_jingle(1);
657                         break;
658                         
659                         case MESSAGE_AUDIOPATH:
660                         PDEBUG(DEBUG_JOIN, "join received channel message: %d.\n", param->audiopath);
661                         if (relation->channel_state != param->audiopath) {
662                                 relation->channel_state = param->audiopath;
663                                 trigger_work(&j_updatebridge);
664                                 if (options.deb & DEBUG_JOIN)
665                                         joinpbx_debug(this, "Join::message_epoint{after setting new channel state}");
666                         }
667                         break;
668
669                         case MESSAGE_DISCONNECT:
670                         PDEBUG(DEBUG_JOIN, "releasing after receiving disconnect, because join in partyline mode.\n");
671                         message = message_create(j_serial, epoint_id, JOIN_TO_EPOINT, MESSAGE_RELEASE);
672                         message->param.disconnectinfo.cause = CAUSE_NORMAL;
673                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
674                         message_put(message);
675                         // fall through
676
677                         case MESSAGE_RELEASE:
678                         PDEBUG(DEBUG_JOIN, "releasing from join\n");
679                         release(relation, 0, 0);
680                         if (j_partyline_jingle)
681                                play_jingle(0);
682                         break;
683
684                         default:
685                         PDEBUG(DEBUG_JOIN, "ignoring message, because join in partyline mode.\n");
686                 }
687                 return;
688         }
689
690
691         /* process messages */
692         switch(message_type) {
693                 /* process audio path message */
694                 case MESSAGE_AUDIOPATH:
695                 PDEBUG(DEBUG_JOIN, "join received channel message: audiopath=%d, current relation's channel_state=%d\n", param->audiopath, relation->channel_state);
696                 if (relation->channel_state != param->audiopath) {
697                         relation->channel_state = param->audiopath;
698                         trigger_work(&j_updatebridge);
699                         if (options.deb & DEBUG_JOIN)
700                                 joinpbx_debug(this, "Join::message_epoint{after setting new channel state}");
701                 }
702                 return;
703
704                 /* track notify */
705                 case MESSAGE_NOTIFY:
706                 switch(param->notifyinfo.notify) {
707                         case INFO_NOTIFY_USER_SUSPENDED:
708                         case INFO_NOTIFY_USER_RESUMED:
709                         case INFO_NOTIFY_REMOTE_HOLD:
710                         case INFO_NOTIFY_REMOTE_RETRIEVAL:
711                         case INFO_NOTIFY_CONFERENCE_ESTABLISHED:
712                         case INFO_NOTIFY_CONFERENCE_DISCONNECTED:
713                         new_state = track_notify(relation->rx_state, param->notifyinfo.notify);
714                         if (new_state != relation->rx_state) {
715                                 relation->rx_state = new_state;
716                                 trigger_work(&j_updatebridge);
717                                 if (options.deb & DEBUG_JOIN)
718                                         joinpbx_debug(this, "Join::message_epoint{after setting new rx state}");
719                         }
720                         break;
721
722                         default:
723                         /* send notification to all other endpoints */
724                         reltemp = j_relation;
725                         while(reltemp) {
726                                 if (reltemp->epoint_id!=epoint_id && reltemp->epoint_id) {
727                                         message = message_create(j_serial, reltemp->epoint_id, JOIN_TO_EPOINT, MESSAGE_NOTIFY);
728                                         memcpy(&message->param, param, sizeof(union parameter));
729                                         message_put(message);
730                                 }
731                                 reltemp = reltemp->next;
732                         }
733                 }
734                 return;
735
736                 /* relations sends a connect */
737                 case MESSAGE_CONNECT:
738                 /* outgoing setup type becomes connected */
739                 if (relation->type == RELATION_TYPE_SETUP)
740                         relation->type = RELATION_TYPE_CONNECT;
741                 /* release other relations in setup state */
742                 release_again:
743                 reltemp = j_relation;
744                 while(reltemp) {
745 //printf("connect, checking relation %d\n", reltemp->epoint_id);
746                         if (reltemp->type == RELATION_TYPE_SETUP) {
747 //printf("relation %d is of type setup, releasing\n", reltemp->epoint_id);
748                                 /* send release to endpoint */
749                                 message = message_create(j_serial, reltemp->epoint_id, JOIN_TO_EPOINT, MESSAGE_RELEASE);
750                                 message->param.disconnectinfo.cause = CAUSE_NONSELECTED;
751                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
752                                 message_put(message);
753
754                                 if (release(reltemp, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL)) // dummy cause, should not be used, since calling and connected endpoint still exist afterwards.
755                                         return; // must return, because join IS destroyed
756                                 goto release_again;
757                         }
758                         if (reltemp->type == RELATION_TYPE_CALLING)
759                                 reltemp->type = RELATION_TYPE_CONNECT;
760                         reltemp = reltemp->next;
761                 }
762                 break; // continue with our message
763
764                 /* release is sent by endpoint */
765                 case MESSAGE_RELEASE:
766                 switch(relation->type) {
767                         case RELATION_TYPE_SETUP: /* by called */
768                         /* collect cause and send collected cause */
769                         collect_cause(&j_multicause, &j_multilocation, param->disconnectinfo.cause, param->disconnectinfo.location);
770                         if (j_multicause)
771                                 release(relation, j_multilocation, j_multicause);
772                         else
773                                 release(relation, LOCATION_PRIVATE_LOCAL, CAUSE_UNSPECIFIED);
774                         break;
775
776                         case RELATION_TYPE_CALLING: /* by calling */
777                         /* remove us, if we don't have a called releation yet */
778                         if (!j_relation->next) {
779                                 release(j_relation, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
780                                 return; // must return, because join IS destroyed
781                         }
782                         /* in a conf, we don't kill the other members */
783                         if (num > 2 && !joinpbx_onecalling_othersetup(j_relation)) {
784                                 release(relation, 0, 0);
785                                 return;
786                         }
787                         /* remove all relations that are of called type */
788                         release_again2:
789                         reltemp = j_relation;
790                         while(reltemp) {
791                                 if (reltemp->type == RELATION_TYPE_SETUP) {
792                                         /* send release to endpoint */
793                                         message = message_create(j_serial, reltemp->epoint_id, JOIN_TO_EPOINT, message_type);
794                                         memcpy(&message->param, param, sizeof(union parameter));
795                                         message_put(message);
796
797                                         if (release(reltemp, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL))
798                                                 return; // must return, because join IS destroyed
799                                         goto release_again2;
800                                 }
801                                 reltemp = reltemp->next;
802                         }
803                         PERROR("we are still here, this should not happen\n");
804                         break;
805
806                         default: /* by connected */
807                         /* send current cause */
808                         release(relation, param->disconnectinfo.location, param->disconnectinfo.cause);
809                 }
810                 return; // must return, because join may be destroyed
811         }
812
813         /* check number of relations */
814         if (num > 2 && !joinpbx_onecalling_othersetup(j_relation) && message_type != MESSAGE_CONNECT) {
815                 PDEBUG(DEBUG_JOIN, "we are in a conference, so we ignore the messages, except MESSAGE_CONNECT.\n");
816                 return;
817         }
818
819         /* if join has no other relation, we process the setup message */
820         if (num == 1) {
821                 switch(message_type) {
822                         case MESSAGE_SETUP:
823                         if (param->setup.dialinginfo.itype == INFO_ITYPE_ISDN_EXTENSION) {
824                                 /* in case of keypad */
825                                 numbers = param->setup.dialinginfo.keypad;
826                                 if (numbers[0]) {
827                                         while((number = strsep(&numbers, ","))) {
828                                                 if (out_setup(epoint_id, message_type, param, NULL, number))
829                                                         return; // join destroyed
830                                         }
831                                         /* after keypad finish dialing */
832                                         break;
833                                 }
834                                 /* dialed number */
835                                 numbers = param->setup.dialinginfo.id;
836                                 while((number = strsep(&numbers, ","))) {
837                                         if (out_setup(epoint_id, message_type, param, number, NULL))
838                                                 return; // join destroyed
839                                 }
840                                 break;
841                         }
842                         if (out_setup(epoint_id, message_type, param, param->setup.dialinginfo.id, param->setup.dialinginfo.keypad))
843                                 return; // join destroyed
844                         break;
845
846                         default:
847                         PDEBUG(DEBUG_JOIN, "no need to send a message because there is no other endpoint than the calling one.\n");
848                 }
849         } else {
850                 /* sending message to other relation(s) */
851                 relation = j_relation;
852                 while(relation) {
853                         if (relation->epoint_id != epoint_id) {
854                                 PDEBUG(DEBUG_JOIN, "sending message ep%ld -> ep%ld.\n", epoint_id, relation->epoint_id);
855                                 message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, message_type);
856                                 memcpy(&message->param, param, sizeof(union parameter));
857                                 message_put(message);
858                                 PDEBUG(DEBUG_JOIN, "message sent.\n");
859                         }
860                         relation = relation->next;
861                 }
862         }
863 }
864
865
866 int track_notify(int oldstate, int notify)
867 {
868         int newstate = oldstate;
869
870         switch(notify) {
871                 case INFO_NOTIFY_USER_RESUMED:
872                 case INFO_NOTIFY_REMOTE_RETRIEVAL:
873                 case INFO_NOTIFY_CONFERENCE_DISCONNECTED:
874                 case INFO_NOTIFY_RESERVED_CT_1:
875                 case INFO_NOTIFY_RESERVED_CT_2:
876                 case INFO_NOTIFY_CALL_IS_DIVERTING:
877                 newstate = NOTIFY_STATE_ACTIVE;
878                 break;
879
880                 case INFO_NOTIFY_USER_SUSPENDED:
881                 newstate = NOTIFY_STATE_SUSPEND;
882                 break;
883
884                 case INFO_NOTIFY_REMOTE_HOLD:
885                 newstate = NOTIFY_STATE_HOLD;
886                 break;
887
888                 case INFO_NOTIFY_CONFERENCE_ESTABLISHED:
889                 newstate = NOTIFY_STATE_CONFERENCE;
890                 break;
891         }
892
893         return(newstate);
894 }
895
896
897 /*
898  * setup to exactly one endpoint
899  * if it fails, the calling endpoint is released.
900  * if other outgoing endpoints already exists, they are release as well.
901  * note: if this functions fails, it will destroy its own join object!
902  */
903 int JoinPBX::out_setup(unsigned int epoint_id, int message_type, union parameter *param, char *newnumber, char *newkeypad)
904 {
905         struct join_relation *relation;
906         struct lcr_msg *message;
907         class Endpoint *epoint;
908
909         PDEBUG(DEBUG_JOIN, "no endpoint found, so we will create an endpoint and send the setup message we have.\n");
910         /* create a new relation */
911         if (!(relation=add_relation()))
912                 FATAL("No memory for relation.\n");
913         relation->type = RELATION_TYPE_SETUP;
914         relation->channel_state = 0; /* audio is assumed on a new join */
915         relation->tx_state = NOTIFY_STATE_ACTIVE; /* new joins always assumed to be active */
916         relation->rx_state = NOTIFY_STATE_ACTIVE; /* new joins always assumed to be active */
917         /* create a new endpoint */
918         epoint = new Endpoint(0, j_serial);
919         if (!epoint)
920                 FATAL("No memory for Endpoint instance\n");
921         epoint->ep_app = new_endpointapp(epoint, 1, EAPP_TYPE_PBX); // outgoing
922         relation->epoint_id = epoint->ep_serial;
923         /* send setup message to new endpoint */
924 //printf("JOLLY DEBUG: %d\n",join_countrelations(j_serial));
925 //i                     if (options.deb & DEBUG_JOIN)
926 //                              joinpbx_debug(join, "Join::message_epoint");
927         message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, message_type);
928         memcpy(&message->param, param, sizeof(union parameter));
929         if (newnumber)
930                 SCPY(message->param.setup.dialinginfo.id, newnumber);
931         else
932                 message->param.setup.dialinginfo.id[0] = '\0';
933         if (newkeypad)
934                 SCPY(message->param.setup.dialinginfo.keypad, newkeypad);
935         else
936                 message->param.setup.dialinginfo.keypad[0] = '\0';
937         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);
938         message_put(message);
939         return(0);
940 }
941
942
943 /* send play message to all members to play join/release jingle */
944 void JoinPBX::play_jingle(int in)
945 {
946         struct join_relation *relation;
947         struct lcr_msg *message;
948
949         relation = j_relation;
950
951         if (!relation)
952                 return;
953         if (!relation->next)
954                 return;
955         while(relation) {
956                 message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_TONE);
957                 SCPY(message->param.tone.name, (char *)((in)?"joined":"left"));
958                 message_put(message);
959                 relation = relation->next;
960         }
961 }
962
963