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