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