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