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