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