added owner / group options to options.conf
[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
206 /*
207  * constructor for a new join 
208  * the join will have a relation to the calling endpoint
209  */
210 JoinPBX::JoinPBX(class Endpoint *epoint) : Join()
211 {
212         struct join_relation *relation;
213 //      char filename[256];
214
215         if (!epoint)
216                 FATAL("epoint is NULL.\n");
217
218         PDEBUG(DEBUG_JOIN, "creating new join and connecting it to the endpoint.\n");
219
220         j_type = JOIN_TYPE_PBX;
221         j_caller[0] = '\0';
222         j_caller_id[0] = '\0';
223         j_dialed[0] = '\0';
224         j_todial[0] = '\0';
225         j_pid = getpid();
226         j_updatebridge = 0;
227         j_partyline = 0;
228         j_partyline_jingle = 0;
229         j_multicause = 0;
230         j_multilocation = 0;
231
232         /* initialize a relation only to the calling interface */
233         relation = j_relation = (struct join_relation *)MALLOC(sizeof(struct join_relation));
234         cmemuse++;
235         relation->type = RELATION_TYPE_CALLING;
236         relation->channel_state = 0; /* audio is assumed on a new join */
237         relation->tx_state = NOTIFY_STATE_ACTIVE; /* new joins always assumed to be active */
238         relation->rx_state = NOTIFY_STATE_ACTIVE; /* new joins always assumed to be active */
239         relation->epoint_id = epoint->ep_serial;
240
241
242         if (options.deb & DEBUG_JOIN)
243                 joinpbx_debug(this, "JoinPBX::Constructor(new join)");
244 }
245
246
247 /*
248  * join descructor
249  */
250 JoinPBX::~JoinPBX()
251 {
252         struct join_relation *relation, *rtemp;
253
254         relation = j_relation;
255         while(relation) {
256                 rtemp = relation->next;
257                 FREE(relation, sizeof(struct join_relation));
258                 cmemuse--;
259                 relation = rtemp;
260         }
261 }
262
263
264 /* bridge sets the audio flow of all bchannels assiociated to 'this' join
265  * also it changes and notifies active/hold/conference states
266  */
267 void JoinPBX::bridge(void)
268 {
269         struct join_relation *relation;
270         struct lcr_msg *message;
271         int numconnect = 0, relations = 0;
272         class Endpoint *epoint;
273         struct port_list *portlist;
274         class Port *port;
275 #ifdef DEBUG_COREBRIDGE
276         int allmISDN = 0; // never set for debug purpose
277 #else
278         int allmISDN = 1; // set until a non-mISDN relation is found
279 #endif
280
281         relation = j_relation;
282         while(relation) {
283                 /* count all relations */
284                 relations++;
285
286                 /* check for relation's objects */
287                 epoint = find_epoint_id(relation->epoint_id);
288                 if (!epoint) {
289                         PERROR("software error: relation without existing endpoints.\n");
290                         relation = relation->next;
291                         continue;
292                 }
293                 portlist = epoint->ep_portlist;
294                 if (!portlist) {
295                         PDEBUG(DEBUG_JOIN, "join%d ignoring relation without port object.\n", j_serial);
296 //#warning testing: keep on hold until single audio stream available
297                         relation->channel_state = 0;
298                         relation = relation->next;
299                         continue;
300                 }
301                 if (portlist->next) {
302                         PDEBUG(DEBUG_JOIN, "join%d ignoring relation with ep%d due to port_list.\n", j_serial, epoint->ep_serial);
303 //#warning testing: keep on hold until single audio stream available
304                         relation->channel_state = 0;
305                         relation = relation->next;
306                         continue;
307                 }
308                 port = find_port_id(portlist->port_id);
309                 if (!port) {
310                         PDEBUG(DEBUG_JOIN, "join%d ignoring relation without existing port object.\n", j_serial);
311                         relation = relation->next;
312                         continue;
313                 }
314                 if ((port->p_type&PORT_CLASS_MASK)!=PORT_CLASS_mISDN) {
315                         PDEBUG(DEBUG_JOIN, "join%d ignoring relation ep%d because it's port is not mISDN.\n", j_serial, epoint->ep_serial);
316                         if (allmISDN) {
317                                 PDEBUG(DEBUG_JOIN, "join%d not all endpoints are mISDN.\n", j_serial);
318                                 allmISDN = 0;
319                         }
320                         relation = relation->next;
321                         continue;
322                 }
323                 
324                 relation = relation->next;
325         }
326
327         PDEBUG(DEBUG_JOIN, "join%d members=%d %s\n", j_serial, relations, (allmISDN)?"(all are mISDN-members)":"(not all are mISDN-members)");
328         /* we notify all relations about rxdata. */
329         relation = j_relation;
330         while(relation) {
331                 /* count connected relations */
332                 if ((relation->channel_state == 1)
333                  && (relation->rx_state != NOTIFY_STATE_SUSPEND)
334                  && (relation->rx_state != NOTIFY_STATE_HOLD))
335                         numconnect ++;
336
337                 /* remove unconnected parties from conference, also remove remotely disconnected parties so conference will not be disturbed. */
338                 if (relation->channel_state == 1
339                  && relation->rx_state != NOTIFY_STATE_HOLD
340                  && relation->rx_state != NOTIFY_STATE_SUSPEND
341                  && relations>1 // no conf with one member
342                  && allmISDN) { // no conf if any member is not mISDN
343                         message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_mISDNSIGNAL);
344                         message->param.mISDNsignal.message = mISDNSIGNAL_CONF;
345                         message->param.mISDNsignal.conf = j_serial<<16 | j_pid;
346                         PDEBUG(DEBUG_JOIN, "join%d EP%d +on+ id: 0x%08x\n", j_serial, relation->epoint_id, message->param.mISDNsignal.conf);
347                         message_put(message);
348                 } else {
349                         message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_mISDNSIGNAL);
350                         message->param.mISDNsignal.message = mISDNSIGNAL_CONF;
351                         message->param.mISDNsignal.conf = 0;
352                         PDEBUG(DEBUG_JOIN, "join%d EP%d +off+ id: 0x%08x\n", j_serial, relation->epoint_id, message->param.mISDNsignal.conf);
353                         message_put(message);
354                 }
355
356                 /*
357                  * request data from endpoint/port if:
358                  * - two relations
359                  * - any without mISDN
360                  * in this case we bridge
361                  */
362                 message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_mISDNSIGNAL);
363                 message->param.mISDNsignal.message = mISDNSIGNAL_JOINDATA;
364                 message->param.mISDNsignal.joindata = (relations==2 && !allmISDN);
365                 PDEBUG(DEBUG_JOIN, "join%d EP%d set joindata=%d\n", j_serial, relation->epoint_id, message->param.mISDNsignal.joindata);
366                 message_put(message);
367
368                 relation = relation->next;
369         }
370
371         /* two people just exchange their states */
372         if (relations==2 && !j_partyline) {
373                 PDEBUG(DEBUG_JOIN, "join%d 2 relations / no partyline\n", j_serial);
374                 relation = j_relation;
375                 relation->tx_state = notify_state_change(j_serial, relation->epoint_id, relation->tx_state, relation->next->rx_state);
376                 relation->next->tx_state = notify_state_change(j_serial, relation->next->epoint_id, relation->next->tx_state, relation->rx_state);
377         } else
378         /* one member in a join, so we put her on hold */
379         if ((relations==1 || numconnect==1)/* && !j_partyline_jingle*/) {
380                 PDEBUG(DEBUG_JOIN, "join%d 1 member or only 1 connected, put on hold\n", j_serial);
381                 relation = j_relation;
382                 while(relation) {
383                         if ((relation->channel_state == 1)
384                          && (relation->rx_state != NOTIFY_STATE_SUSPEND)
385                          && (relation->rx_state != NOTIFY_STATE_HOLD))
386                                 relation->tx_state = notify_state_change(j_serial, relation->epoint_id, relation->tx_state, NOTIFY_STATE_HOLD);
387                         relation = relation->next;
388                 }
389         } else {
390         /* if conference/partyline (or more than two members and more than one is connected), so we set conference state */ 
391                 PDEBUG(DEBUG_JOIN, "join%d %d members, %d connected, signal conference\n", j_serial, relations, numconnect);
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_CONFERENCE);
398                         relation = relation->next;
399                 }
400         }
401 }
402
403 /*
404  * bridging is only possible with two connected endpoints
405  */
406 void JoinPBX::bridge_data(unsigned int epoint_from, struct join_relation *relation_from, union parameter *param)
407 {
408         struct join_relation *relation_to;
409
410         /* if we are alone */
411         if (!j_relation->next)
412                 return;
413
414         /* if we are more than two */
415         if (j_relation->next->next)
416                 return;
417
418         /* skip if source endpoint has NOT audio mode CONNECT */
419         if (relation_from->channel_state != 1)
420                 return;
421
422         /* get destination relation */
423         relation_to = j_relation;
424         if (relation_to == relation_from) {
425                 /* oops, we are the first, so destination is: */
426                 relation_to = relation_to->next;
427         }
428
429         /* skip if destination endpoint has NOT audio mode CONNECT */
430         if (relation_to->channel_state != 1)
431                 return;
432
433         /* now we may send our data to the endpoint where it
434          * will be delivered to the port
435          */
436 //printf("from %d, to %d\n", relation_from->epoint_id, relation_to->epoint_id);
437         message_forward(j_serial, relation_to->epoint_id, JOIN_TO_EPOINT, param);
438 }
439
440 /* release join from endpoint
441  * if the join has two relations, all relations are freed and the join will be
442  * destroyed
443  * on outgoing relations, the cause is collected, if not connected
444  * returns if join has been destroyed
445  */
446 int JoinPBX::release(struct join_relation *relation, int location, int cause)
447 {
448         struct join_relation *reltemp, **relationpointer;
449         struct lcr_msg *message;
450         class Join *join;
451         int destroy = 0;
452
453         /* remove from bridge */
454         if (relation->channel_state != 0) {
455                 relation->channel_state = 0;
456                 j_updatebridge = 1; /* update bridge flag */
457                 // note: if join is not released, bridge must be updated
458         }
459
460         /* detach given interface */
461         reltemp = j_relation;
462         relationpointer = &j_relation;
463         while(reltemp) {
464                 /* endpoint of function call */
465                 if (relation == reltemp)
466                         break;
467                 relationpointer = &reltemp->next;
468                 reltemp = reltemp->next;
469         }
470         if (!reltemp)
471                 FATAL("relation not in list of our relations. this must not happen.\n");
472 //printf("releasing relation %d\n", reltemp->epoint_id);
473         *relationpointer = reltemp->next;
474         FREE(reltemp, sizeof(struct join_relation));
475         cmemuse--;
476         relation = reltemp = NULL; // just in case of reuse fault;
477
478         /* if no more relation */
479         if (!j_relation) {
480                 PDEBUG(DEBUG_JOIN, "join is completely removed.\n");
481                 /* there is no more endpoint related to the join */
482                 destroy = 1;
483                 delete this;
484                 // end of join object!
485                 PDEBUG(DEBUG_JOIN, "join completely removed!\n");
486         } else
487         /* if join is a party line */
488         if (j_partyline) {
489                 PDEBUG(DEBUG_JOIN, "join is a conference room, so we keep it alive until the last party left.\n");
490         } else
491         /* if only one relation left */
492         if (!j_relation->next) {
493                 PDEBUG(DEBUG_JOIN, "join has one relation left, so we send it a release with the given cause %d.\n", cause);
494                 message = message_create(j_serial, j_relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_RELEASE);
495                 message->param.disconnectinfo.cause = cause;
496                 message->param.disconnectinfo.location = location;
497                 message_put(message);
498                 destroy = 1;
499                 delete this;
500                 // end of join object!
501                 PDEBUG(DEBUG_JOIN, "join completely removed!\n");
502         }
503
504         join = join_first;
505         while(join) {
506                 if (options.deb & DEBUG_JOIN && join->j_type==JOIN_TYPE_PBX)
507                         joinpbx_debug((class JoinPBX *)join, "join_release{all joins left}");
508                 join = join->next;
509         }
510         PDEBUG(DEBUG_JOIN, "join_release(): ended.\n");
511         return(destroy);
512 }
513
514 /* count number of relations in a join
515  */
516 int joinpbx_countrelations(unsigned int join_id)
517 {
518         struct join_relation *relation;
519         int i;
520         class Join *join;
521         class JoinPBX *joinpbx;
522
523         join = find_join_id(join_id);
524
525         if (!join)
526                 return(0);
527
528         if (join->j_type == JOIN_TYPE_REMOTE)
529                 return(2);
530
531         if (join->j_type != JOIN_TYPE_PBX)
532                 return(0);
533         joinpbx = (class JoinPBX *)join;
534
535         i = 0;
536         relation = joinpbx->j_relation;
537         while(relation) {
538                 i++;
539                 relation = relation->next;
540         }
541
542         return(i);
543 }
544
545 /* check if one is calling and all other relations are setup-realations */
546 int joinpbx_onecalling_othersetup(struct join_relation *relation)
547 {
548         int calling = 0, other = 0;
549
550         while(relation) {
551                 switch(relation->type) {
552                 case RELATION_TYPE_CALLING:
553                         calling++;
554                         break;
555                 case RELATION_TYPE_SETUP:
556                         break;
557                 default:
558                         other++;
559                         break;
560                 }
561
562                 relation = relation->next;
563         }
564
565         if (calling == 1 && other == 0)
566                 return(1);
567         return(0);
568 }
569
570 void JoinPBX::remove_relation(struct join_relation *relation)
571 {
572         struct join_relation *temp, **tempp;
573
574         if (!relation)
575                 return;
576
577         temp = j_relation;
578         tempp = &j_relation;
579         while(temp) {
580                 if (temp == relation)
581                         break;
582                 tempp = &temp->next;
583                 temp = temp->next;
584         }
585         if (!temp) {
586                 PERROR("relation not in join.\n");
587                 return;
588         }
589
590         PDEBUG(DEBUG_JOIN, "removing relation.\n");
591         *tempp = relation->next;
592         FREE(temp, sizeof(struct join_relation));
593         cmemuse--;
594 }       
595
596
597 struct join_relation *JoinPBX::add_relation(void)
598 {
599         struct join_relation *relation;
600
601         if (!j_relation) {
602                 PERROR("there is no first relation to this join\n");
603                 return(NULL);
604         }
605         relation = j_relation;
606         while(relation->next)
607                 relation = relation->next;
608
609         relation->next = (struct join_relation *)MALLOC(sizeof(struct join_relation));
610         cmemuse++;
611         /* the record pointer is set at the first time the data is received for the relation */
612
613 //      if (options.deb & DEBUG_JOIN)
614 //              joinpbx_debug(join, "add_relation");
615         return(relation->next);
616 }
617
618 /* epoint sends a message to a join
619  *
620  */
621 void JoinPBX::message_epoint(unsigned int epoint_id, int message_type, union parameter *param)
622 {
623         class Join *cl;
624         struct join_relation *relation, *reltemp;
625         int num;
626         int new_state;
627         struct lcr_msg *message;
628 //      int size, writesize, oldpointer;
629         char *number, *numbers;
630
631         if (!epoint_id) {
632                 PERROR("software error, epoint == NULL\n");
633                 return;
634         }
635
636 //      if (options.deb & DEBUG_JOIN) {
637 //              PDEBUG(DEBUG_JOIN, "message %d received from ep%d.\n", message, epoint->ep_serial);
638 //              joinpbx_debug(join,"Join::message_epoint");
639 //      }
640         if (options.deb & DEBUG_JOIN) {
641                 if (message_type != MESSAGE_DATA) {
642                         cl = join_first;
643                         while(cl) {
644                                 if (cl->j_type == JOIN_TYPE_PBX)
645                                         joinpbx_debug((class JoinPBX *)cl, "Join::message_epoint{all joins before processing}");
646                                 cl = cl->next;
647                         }
648                 }
649         }
650
651         /* check relation */
652         relation = j_relation;
653         while(relation) {
654                 if (relation->epoint_id == epoint_id)
655                         break;
656                 relation = relation->next;
657         }
658         if (!relation) {
659                 PDEBUG(DEBUG_JOIN, "no relation back to the endpoint found, ignoring (join=%d, endpoint=%d)\n", j_serial, epoint_id);
660                 return;
661         }
662
663         /* count relations */
664         num=joinpbx_countrelations(j_serial);
665
666         /* process party line */
667         if (message_type == MESSAGE_SETUP) if (param->setup.partyline && !j_partyline) {
668                 j_partyline = param->setup.partyline;
669                 j_partyline_jingle = param->setup.partyline_jingle;
670         }
671         if (j_partyline) {
672                 switch(message_type) {
673                         case MESSAGE_SETUP:
674                         PDEBUG(DEBUG_JOIN, "respsone with connect in partyline mode.\n");
675                         relation->type = RELATION_TYPE_CONNECT;
676                         message = message_create(j_serial, epoint_id, JOIN_TO_EPOINT, MESSAGE_CONNECT);
677                         SPRINT(message->param.connectinfo.id, "%d", j_partyline);
678                         message->param.connectinfo.ntype = INFO_NTYPE_UNKNOWN;
679                         message_put(message);
680                         j_updatebridge = 1; /* update bridge flag */
681                         if (j_partyline_jingle)
682                                play_jingle(1);
683                         break;
684                         
685                         case MESSAGE_AUDIOPATH:
686                         PDEBUG(DEBUG_JOIN, "join received channel message: %d.\n", param->audiopath);
687                         if (relation->channel_state != param->audiopath) {
688                                 relation->channel_state = param->audiopath;
689                                 j_updatebridge = 1; /* update bridge flag */
690                                 if (options.deb & DEBUG_JOIN)
691                                         joinpbx_debug(this, "Join::message_epoint{after setting new channel state}");
692                         }
693                         break;
694
695                         case MESSAGE_DISCONNECT:
696                         PDEBUG(DEBUG_JOIN, "releasing after receiving disconnect, because join in partyline mode.\n");
697                         message = message_create(j_serial, epoint_id, JOIN_TO_EPOINT, MESSAGE_RELEASE);
698                         message->param.disconnectinfo.cause = CAUSE_NORMAL;
699                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
700                         message_put(message);
701                         // fall through
702
703                         case MESSAGE_RELEASE:
704                         PDEBUG(DEBUG_JOIN, "releasing from join\n");
705                         release(relation, 0, 0);
706                         if (j_partyline_jingle)
707                                play_jingle(0);
708                         break;
709
710                         default:
711                         PDEBUG(DEBUG_JOIN, "ignoring message, because join in partyline mode.\n");
712                 }
713                 return;
714         }
715
716
717         /* process messages */
718         switch(message_type) {
719                 /* process audio path message */
720                 case MESSAGE_AUDIOPATH:
721                 PDEBUG(DEBUG_JOIN, "join received channel message: %d.\n", param->audiopath);
722                 if (relation->channel_state != param->audiopath) {
723                         relation->channel_state = param->audiopath;
724                         j_updatebridge = 1; /* update bridge flag */
725                         if (options.deb & DEBUG_JOIN)
726                                 joinpbx_debug(this, "Join::message_epoint{after setting new channel state}");
727                 }
728                 return;
729
730                 /* track notify */
731                 case MESSAGE_NOTIFY:
732                 switch(param->notifyinfo.notify) {
733                         case INFO_NOTIFY_USER_SUSPENDED:
734                         case INFO_NOTIFY_USER_RESUMED:
735                         case INFO_NOTIFY_REMOTE_HOLD:
736                         case INFO_NOTIFY_REMOTE_RETRIEVAL:
737                         case INFO_NOTIFY_CONFERENCE_ESTABLISHED:
738                         case INFO_NOTIFY_CONFERENCE_DISCONNECTED:
739                         new_state = track_notify(relation->rx_state, param->notifyinfo.notify);
740                         if (new_state != relation->rx_state) {
741                                 relation->rx_state = new_state;
742                                 j_updatebridge = 1;
743                                 if (options.deb & DEBUG_JOIN)
744                                         joinpbx_debug(this, "Join::message_epoint{after setting new rx state}");
745                         }
746                         break;
747
748                         default:
749                         /* send notification to all other endpoints */
750                         reltemp = j_relation;
751                         while(reltemp) {
752                                 if (reltemp->epoint_id!=epoint_id && reltemp->epoint_id) {
753                                         message = message_create(j_serial, reltemp->epoint_id, JOIN_TO_EPOINT, MESSAGE_NOTIFY);
754                                         memcpy(&message->param, param, sizeof(union parameter));
755                                         message_put(message);
756                                 }
757                                 reltemp = reltemp->next;
758                         }
759                 }
760                 return;
761
762                 /* audio data */
763                 case MESSAGE_DATA:
764                 /* now send audio data to the other endpoint */
765                 bridge_data(epoint_id, relation, param);
766                 return;
767
768                 /* relations sends a connect */
769                 case MESSAGE_CONNECT:
770                 /* outgoing setup type becomes connected */
771                 if (relation->type == RELATION_TYPE_SETUP)
772                         relation->type = RELATION_TYPE_CONNECT;
773                 /* release other relations in setup state */
774                 release_again:
775                 reltemp = j_relation;
776                 while(reltemp) {
777 //printf("connect, checking relation %d\n", reltemp->epoint_id);
778                         if (reltemp->type == RELATION_TYPE_SETUP) {
779 //printf("relation %d is of type setup, releasing\n", reltemp->epoint_id);
780                                 /* send release to endpoint */
781                                 message = message_create(j_serial, reltemp->epoint_id, JOIN_TO_EPOINT, MESSAGE_RELEASE);
782                                 message->param.disconnectinfo.cause = CAUSE_NONSELECTED;
783                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
784                                 message_put(message);
785
786                                 if (release(reltemp, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL)) // dummy cause, should not be used, since calling and connected endpoint still exist afterwards.
787                                         return; // must return, because join IS destroyed
788                                 goto release_again;
789                         }
790                         if (reltemp->type == RELATION_TYPE_CALLING)
791                                 reltemp->type = RELATION_TYPE_CONNECT;
792                         reltemp = reltemp->next;
793                 }
794                 break; // continue with our message
795
796                 /* release is sent by endpoint */
797                 case MESSAGE_RELEASE:
798                 switch(relation->type) {
799                         case RELATION_TYPE_SETUP: /* by called */
800                         /* collect cause and send collected cause */
801                         collect_cause(&j_multicause, &j_multilocation, param->disconnectinfo.cause, param->disconnectinfo.location);
802                         if (j_multicause)
803                                 release(relation, j_multilocation, j_multicause);
804                         else
805                                 release(relation, LOCATION_PRIVATE_LOCAL, CAUSE_UNSPECIFIED);
806                         break;
807
808                         case RELATION_TYPE_CALLING: /* by calling */
809                         /* remove us, if we don't have a called releation yet */
810                         if (!j_relation->next) {
811                                 release(j_relation, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
812                                 return; // must return, because join IS destroyed
813                         }
814                         /* in a conf, we don't kill the other members */
815                         if (num > 2 && !joinpbx_onecalling_othersetup(j_relation)) {
816                                 release(relation, 0, 0);
817                                 return;
818                         }
819                         /* remove all relations that are of called type */
820                         release_again2:
821                         reltemp = j_relation;
822                         while(reltemp) {
823                                 if (reltemp->type == RELATION_TYPE_SETUP) {
824                                         /* send release to endpoint */
825                                         message = message_create(j_serial, reltemp->epoint_id, JOIN_TO_EPOINT, message_type);
826                                         memcpy(&message->param, param, sizeof(union parameter));
827                                         message_put(message);
828
829                                         if (release(reltemp, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL))
830                                                 return; // must return, because join IS destroyed
831                                         goto release_again2;
832                                 }
833                                 reltemp = reltemp->next;
834                         }
835                         PERROR("we are still here, this should not happen\n");
836                         break;
837
838                         default: /* by connected */
839                         /* send current cause */
840                         release(relation, param->disconnectinfo.location, param->disconnectinfo.cause);
841                 }
842                 return; // must return, because join may be destroyed
843         }
844
845         /* check number of relations */
846         if (num > 2 && !joinpbx_onecalling_othersetup(j_relation) && message_type != MESSAGE_CONNECT) {
847                 PDEBUG(DEBUG_JOIN, "we are in a conference, so we ignore the messages, except MESSAGE_CONNECT.\n");
848                 return;
849         }
850
851         /* if join has no other relation, we process the setup message */
852         if (num == 1) {
853                 switch(message_type) {
854                         case MESSAGE_SETUP:
855                         if (param->setup.dialinginfo.itype == INFO_ITYPE_ISDN_EXTENSION) {
856                                 numbers = param->setup.dialinginfo.id;
857                                 while((number = strsep(&numbers, ","))) {
858                                         if (out_setup(epoint_id, message_type, param, number))
859                                                 return; // join destroyed
860                                 }
861                                 break;
862                         }
863                         if (out_setup(epoint_id, message_type, param, NULL))
864                                 return; // join destroyed
865                         break;
866
867                         default:
868                         PDEBUG(DEBUG_JOIN, "no need to send a message because there is no other endpoint than the calling one.\n");
869                 }
870         } else {
871                 /* sending message to other relation(s) */
872                 relation = j_relation;
873                 while(relation) {
874                         if (relation->epoint_id != epoint_id) {
875                                 PDEBUG(DEBUG_JOIN, "sending message ep%ld -> ep%ld.\n", epoint_id, relation->epoint_id);
876                                 message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, message_type);
877                                 memcpy(&message->param, param, sizeof(union parameter));
878                                 message_put(message);
879                                 PDEBUG(DEBUG_JOIN, "message sent.\n");
880                         }
881                         relation = relation->next;
882                 }
883         }
884 }
885
886
887 /* join process is called from the main loop
888  * it processes the current calling state.
889  * returns 0 if join nothing was done
890  */
891 int JoinPBX::handler(void)
892 {
893 //      struct join_relation *relation;
894 //      char dialing[32][32];
895 //      int port[32];
896 //      int found;
897 //      int i, j;
898 //      char *p;
899
900         /* the bridge must be updated */
901         if (j_updatebridge) {
902                 bridge();
903                 j_updatebridge = 0;
904                 return(1);
905         }
906
907         return(0);
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)
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         PDEBUG(DEBUG_JOIN, "setup message sent to ep %d with number='%s'.\n", relation->epoint_id, message->param.setup.dialinginfo.id);
978         message_put(message);
979         return(0);
980 }
981
982
983 /* send play message to all members to play join/release jingle */
984 void JoinPBX::play_jingle(int in)
985 {
986         struct join_relation *relation;
987         struct lcr_msg *message;
988
989         relation = j_relation;
990
991         if (!relation)
992                 return;
993         if (!relation->next)
994                 return;
995         while(relation) {
996                 message = message_create(j_serial, relation->epoint_id, JOIN_TO_EPOINT, MESSAGE_TONE);
997                 SCPY(message->param.tone.name, (char *)((in)?"joined":"left"));
998                 message_put(message);
999                 relation = relation->next;
1000         }
1001 }
1002
1003