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