Add FXS support
[lcr.git] / fxs.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN fxs                                                                 **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 /* Procedures:
13
14
15 *****TBD: THIS TEXT IS OLD ******
16
17 off-hook indication:
18         no port instance:
19                 Create port/endpoint instance and send SETUP message to endpoint.
20         port instance active:
21                 Send CONNECT message to endpoint.
22         port instance inactive:
23                 Put inactive port instance active. Send RETRIEVE message to endpoint.
24 on-hook indication:
25         Release active port instance. Send RELEASE message to endpoint if exists.
26         inactive port instance:
27                 Send ring request. Use caller ID on incomming call or connected ID on outgoing call.
28 hookflash indication:
29         active port instance not connected:
30                 Release active port instance. Send RELEASE message to endpoint if exists.
31         active port instance connected:
32                 Put active port instance inactive. Send HOLD MESSAGE to endpoint.
33         inactive port instance:
34                 Put inactive port instance active. Send RETRIEVE message to endpoint.
35         no inactive port instance:
36                 Create port/endpoint instance and send SETUP message to endpoint.
37 keypulse indication:
38         active port instance in incomming overlap state:
39                 Send INFORMATION message to endpoint.
40         active port instance in other state:
41                 Send KEYPAD message to endpoint, if exists.
42 SETUP message:
43         no instance:
44                 Create port instance and send ALERTING message to endpoint.
45                 Send ring request. Use caller ID.
46         only one instance active:
47                 Create port instance and send ALERTING message to endpoint.
48                 Send knock sound. Send ALERTING message to endpoint.
49         one instance on hold:
50                 Send RELEASE message (cause = BUSY) to endpoint.
51 PROCEEDING / ALERTING / CONNECT message:
52         (change state only)
53 DISCONNECT message:
54         is inactive port instance:
55                 Release port instance. Send RELEASE message to endpoint.
56 RELEASE message:
57         is active port instance:
58                 Create hangup tone (release tone)
59         is inactive port instance:
60                 Release port instance.
61 */
62
63 #include "main.h"
64 #include "myisdn.h"
65 // socket mISDN
66 //#include <sys/socket.h>
67 extern "C" {
68 }
69
70 #ifdef ISDN_P_FXS_POTS
71
72 static int fxs_age = 0;
73
74 static int delete_event(struct lcr_work *work, void *instance, int index);
75
76 static int dtmf_timeout(struct lcr_timer *timer, void *instance, int index)
77 {
78         class Pfxs *pfxs = (class Pfxs *)instance;
79
80         /* allow DTMF dialing now */
81         PDEBUG(DEBUG_ISDN, "%s: allow DTMF now\n", pfxs->p_name);
82         pfxs->p_m_fxs_allow_dtmf = 1;
83
84         return 0;
85 }
86
87 /*
88  * constructor
89  */
90 Pfxs::Pfxs(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, struct interface *interface, int mode) : PmISDN(type, mISDNport, portname, settings, interface, 0, 0, mode)
91 {
92         p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
93
94         memset(&p_m_fxs_delete, 0, sizeof(p_m_fxs_delete));
95         add_work(&p_m_fxs_delete, delete_event, this, 0);
96         p_m_fxs_allow_dtmf = 0;
97         memset(&p_m_fxs_dtmf_timer, 0, sizeof(p_m_fxs_dtmf_timer));
98         add_timer(&p_m_fxs_dtmf_timer, dtmf_timeout, this, 0);
99         p_m_fxs_age = fxs_age++;
100         p_m_fxs_knocking = 0;
101
102         PDEBUG(DEBUG_ISDN, "Created new FXSPort(%s). Currently %d objects use, FXS port #%d\n", portname, mISDNport->use, p_m_portnum);
103 }
104
105
106 /*
107  * destructor
108  */
109 Pfxs::~Pfxs()
110 {
111         del_timer(&p_m_fxs_dtmf_timer);
112         del_work(&p_m_fxs_delete);
113 }
114
115 /* deletes only if l3id is release, otherwhise it will be triggered then */
116 static int delete_event(struct lcr_work *work, void *instance, int index)
117 {
118         class Pfxs *pots = (class Pfxs *)instance;
119
120         delete pots;
121
122         return 0;
123 }
124
125
126 int Pfxs::hunt_bchannel(void)
127 {
128         if (p_m_mISDNport->b_num < 1)
129                 return -47;
130         if (p_m_mISDNport->b_port[0])
131                 return -17;
132         return 1;
133 }
134
135 int Pfxs::ph_control_pots(unsigned int cont, unsigned char *data, int len)
136 {
137         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+len];
138         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
139         unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
140         int ret;
141
142         ctrl->prim = PH_CONTROL_REQ;
143         ctrl->id = 0;
144         *d++ = cont;
145         if (len)
146                 memcpy(d, data, len);
147         ret = sendto(p_m_mISDNport->pots_sock.fd, buffer, MISDN_HEADER_LEN+sizeof(int)+len, 0, NULL, 0);
148         if (ret <= 0)
149                 PERROR("Failed to send to socket %d\n", p_m_mISDNport->pots_sock.fd);
150
151         return ret;
152 }
153
154 void Pfxs::pickup_ind(unsigned int cont)
155 {
156         struct interface *interface = p_m_mISDNport->ifport->interface;
157         class Endpoint *epoint;
158         struct lcr_msg *message;
159         int ret, channel;
160
161         p_m_fxs_age = fxs_age++;
162
163         if (p_m_fxs_knocking) {
164                 ph_control_pots(POTS_CW_OFF, NULL, 0);
165                 p_m_fxs_knocking = 0;
166         }
167
168         chan_trace_header(p_m_mISDNport, this, "PICKUP", DIRECTION_NONE);
169
170         if (interface->ifmsn && interface->ifmsn->msn[0]) {
171                 SCPY(p_callerinfo.id, interface->ifmsn->msn);
172                 add_trace("caller", "ID", "%s", p_callerinfo.id);
173         }
174         p_callerinfo.present = INFO_PRESENT_ALLOWED;
175         p_callerinfo.isdn_port = p_m_portnum;
176         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
177         p_capainfo.source_mode = B_MODE_TRANSPARENT;
178         p_capainfo.bearer_capa = INFO_BC_AUDIO;
179         p_capainfo.bearer_info1 = 0x80 + ((options.law=='a')?3:2);
180         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
181
182         if ((cont & (~POTS_KP_MASK)) == POTS_KP_VAL) {
183                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
184                 p_dialinginfo.id[0] = cont & POTS_KP_MASK;
185         }
186
187         if (!p_m_b_channel) {
188                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) alloc bchannel\n", p_name);
189                 /* hunt bchannel */
190                 ret = channel = hunt_bchannel();
191                 if (ret < 0)
192                         goto no_channel;
193
194                 /* open channel */
195                 ret = seize_bchannel(channel, 1);
196                 if (ret < 0) {
197 no_channel:
198                         /*
199                          * NOTE: we send MT_RELEASE_COMPLETE to "REJECT" the channel
200                          * in response to the setup
201                          */
202                         add_trace("error", NULL, "no b-channel");
203                         end_trace();
204                         new_state(PORT_STATE_RELEASE);
205                         trigger_work(&p_m_fxs_delete);
206                         return;
207                 }
208                 end_trace();
209                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
210         } else {
211                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) bchannel is already active\n", p_name);
212         }
213
214         /* create endpoint */
215         if (p_epointlist)
216                 FATAL("Incoming call but already got an endpoint.\n");
217         if (!(epoint = new Endpoint(p_serial, 0)))
218                 FATAL("No memory for Endpoint instance\n");
219         epoint->ep_app = new_endpointapp(epoint, 0, p_m_mISDNport->ifport->interface->app); //incoming
220         epointlist_new(epoint->ep_serial);
221
222         /* indicate flash control */
223         if (cont == POTS_HOOK_FLASH || cont == POTS_EARTH_KEY)
224                 p_dialinginfo.flash = 1;
225
226         /* send setup message to endpoit */
227         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
228         message->param.setup.isdn_port = p_m_portnum;
229         message->param.setup.port_type = p_type;
230 //      message->param.setup.dtmf = !p_m_mISDNport->ifport->nodtmf;
231         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
232         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
233         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
234         message_put(message);
235
236         new_state(PORT_STATE_IN_OVERLAP);
237
238         schedule_timer(&p_m_fxs_dtmf_timer, 0, 500000);
239 }
240
241 void Pfxs::hangup_ind(unsigned int cont)
242 {
243         struct lcr_msg *message;
244
245         /* deactivate bchannel */
246         chan_trace_header(p_m_mISDNport, this, "HANGUP", DIRECTION_NONE);
247         end_trace();
248         drop_bchannel();
249         PDEBUG(DEBUG_ISDN, "Pfxs(%s) drop bchannel\n", p_name);
250
251         /* send release message, if not already */
252         if (p_epointlist) {
253                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
254                 message->param.disconnectinfo.cause = 16;
255                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
256                 message_put(message);
257         }
258         new_state(PORT_STATE_RELEASE);
259         trigger_work(&p_m_fxs_delete);
260 }
261
262 void Pfxs::answer_ind(unsigned int cont)
263 {
264         struct lcr_msg *message;
265         int ret, channel;
266
267         if (p_m_fxs_knocking) {
268                 ph_control_pots(POTS_CW_OFF, NULL, 0);
269                 p_m_fxs_knocking = 0;
270         }
271
272         chan_trace_header(p_m_mISDNport, this, "ANSWER", DIRECTION_NONE);
273         if (!p_m_b_channel) {
274                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) alloc bchannel\n", p_name);
275                 /* hunt bchannel */
276                 ret = channel = hunt_bchannel();
277                 if (ret < 0)
278                         goto no_channel;
279
280                 /* open channel */
281                 ret = seize_bchannel(channel, 1);
282                 if (ret < 0) {
283 no_channel:
284                         /*
285                          * NOTE: we send MT_RELEASE_COMPLETE to "REJECT" the channel
286                          * in response to the setup
287                          */
288                         add_trace("error", NULL, "no b-channel");
289                         end_trace();
290                         new_state(PORT_STATE_RELEASE);
291                         trigger_work(&p_m_fxs_delete);
292                         return;
293                 }
294                 end_trace();
295                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
296         } else {
297                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) bchannel is already active\n", p_name);
298         }
299
300         if (p_m_hold) {
301                 p_m_hold = 0;
302                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
303                 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
304                 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
305                 message_put(message);
306         } else {
307                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
308                 message_put(message);
309         }
310
311         new_state(PORT_STATE_CONNECT);
312 }
313
314 void Pfxs::hold_ind(unsigned int cont)
315 {
316         struct lcr_msg *message;
317
318         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
319         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
320         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
321         message_put(message);
322
323         /* deactivate bchannel */
324         chan_trace_header(p_m_mISDNport, this, "HOLD", DIRECTION_NONE);
325         end_trace();
326         drop_bchannel();
327         PDEBUG(DEBUG_ISDN, "Pfxs(%s) drop bchannel\n", p_name);
328
329         p_m_hold = 1;
330 }
331
332 void Pfxs::retrieve_ind(unsigned int cont)
333 {
334         struct lcr_msg *message;
335         int ret, channel;
336
337         p_m_fxs_age = fxs_age++;
338
339         if (p_m_fxs_knocking) {
340                 ph_control_pots(POTS_CW_OFF, NULL, 0);
341                 p_m_fxs_knocking = 0;
342         }
343
344         if (cont == POTS_ON_HOOK) {
345                 const char *callerid;
346
347                 if (p_state == PORT_STATE_CONNECT) {
348                         new_state(PORT_STATE_OUT_ALERTING);
349 #if 0
350                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
351                         message_put(message);
352 #endif
353                         chan_trace_header(p_m_mISDNport, this, "RING (retrieve)", DIRECTION_NONE);
354                 } else
355                         chan_trace_header(p_m_mISDNport, this, "RING (after knocking)", DIRECTION_NONE);
356                 if (p_type == PORT_TYPE_POTS_FXS_IN) {
357                         if (p_connectinfo.id[0]) {
358                                 callerid = numberrize_callerinfo(p_connectinfo.id, p_connectinfo.ntype, options.national, options.international);
359                                 add_trace("connect", "number", callerid);
360                         } else {
361                                 callerid = p_dialinginfo.id;
362                                 add_trace("dialing", "number", callerid);
363                         }
364                 } else {
365                         callerid = numberrize_callerinfo(p_callerinfo.id, p_callerinfo.ntype, options.national, options.international);
366                         add_trace("caller", "id", callerid);
367                 }
368                 ph_control_pots(POTS_RING_ON, (unsigned char *)callerid, strlen(callerid));
369                 end_trace();
370                 return;
371         }
372
373         chan_trace_header(p_m_mISDNport, this, "RETRIEVE", DIRECTION_NONE);
374         if (!p_m_b_channel) {
375                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) alloc bchannel\n", p_name);
376                 /* hunt bchannel */
377                 ret = channel = hunt_bchannel();
378                 if (ret < 0)
379                         goto no_channel;
380
381                 /* open channel */
382                 ret = seize_bchannel(channel, 1);
383                 if (ret < 0) {
384 no_channel:
385                         /*
386                          * NOTE: we send MT_RELEASE_COMPLETE to "REJECT" the channel
387                          * in response to the setup
388                          */
389                         add_trace("error", NULL, "no b-channel");
390                         end_trace();
391                         new_state(PORT_STATE_RELEASE);
392                         trigger_work(&p_m_fxs_delete);
393                         return;
394                 }
395                 end_trace();
396                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
397         } else {
398                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) bchannel is already active\n", p_name);
399         }
400
401         p_m_hold = 0;
402         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
403         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
404         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
405         message_put(message);
406 }
407
408 void Pfxs::keypulse_ind(unsigned int cont)
409 {
410         struct lcr_msg *message;
411
412         chan_trace_header(p_m_mISDNport, this, "PULSE", DIRECTION_NONE);
413         add_trace("KP", NULL, "%c", cont & DTMF_TONE_MASK);
414         end_trace();
415         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
416         message->param.information.id[0] = cont & POTS_KP_MASK;
417         PDEBUG(DEBUG_ISDN, "Pfxs(%s) PH_CONTROL INDICATION  DTMF digit '%c'\n", p_name, message->param.dtmf);
418         message_put(message);
419 }
420
421 void Pfxs::reject_ind(unsigned int cont)
422 {
423         struct lcr_msg *message;
424
425         if (p_m_fxs_knocking) {
426                 ph_control_pots(POTS_CW_OFF, NULL, 0);
427                 p_m_fxs_knocking = 0;
428         }
429
430         /* deactivate bchannel */
431         chan_trace_header(p_m_mISDNport, this, "REJECT", DIRECTION_NONE);
432         end_trace();
433         drop_bchannel();
434         PDEBUG(DEBUG_ISDN, "Pfxs(%s) drop bchannel\n", p_name);
435
436         /* send release message, if not already */
437         if (p_epointlist) {
438                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
439                 message->param.disconnectinfo.cause = 16;
440                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
441                 message_put(message);
442         }
443         new_state(PORT_STATE_RELEASE);
444         trigger_work(&p_m_fxs_delete);
445 }
446
447
448 void Pfxs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
449 {
450         struct lcr_msg *message;
451         class Port *port;
452         class Pfxs *pots;
453         struct epoint_list *epointlist;
454         const char *callerid;
455         int any_call = 0;
456
457         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
458         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
459
460         message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_ALERTING);
461         message_put(message);
462
463         new_state(PORT_STATE_OUT_ALERTING);
464
465         /* attach only if not already */
466         epointlist = p_epointlist;
467         while(epointlist) {
468                 if (epointlist->epoint_id == epoint_id)
469                         break;
470                 epointlist = epointlist->next;
471         }
472         if (!epointlist)
473                 epointlist_new(epoint_id);
474
475         /* find port in connected active state */
476         port = port_first;
477         while(port) {
478                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
479                         pots = (class Pfxs *)port;
480                         if (pots->p_m_mISDNport == p_m_mISDNport) {
481                                 if (pots != this)
482                                         any_call = 1;
483                                 if (pots->p_state == PORT_STATE_CONNECT && !pots->p_m_hold)
484                                         break; // found
485                         }
486                 }
487                 port = port->next;
488         }
489
490         if (port) {
491                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) knock because there is an ongoing active call\n", p_name);
492                 chan_trace_header(p_m_mISDNport, this, "KNOCK", DIRECTION_NONE);
493                 callerid = numberrize_callerinfo(p_callerinfo.id, p_callerinfo.ntype, options.national, options.international);
494                 add_trace("caller", "id", callerid);
495                 end_trace();
496                 ph_control_pots(POTS_CW_ON, (unsigned char *)callerid, strlen(callerid));
497                 p_m_fxs_knocking = 1;
498                 return;
499         }
500         if (any_call) {
501                 /* reject call, because we have a call, but we are not connected */
502                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) reject because there is an ongoing and incomplete call\n", p_name);
503                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
504                 message->param.disconnectinfo.cause = 17; // busy
505                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
506                 message_put(message);
507                 new_state(PORT_STATE_RELEASE);
508                 trigger_work(&p_m_fxs_delete);
509                 return;
510         }
511         PDEBUG(DEBUG_ISDN, "Pfxs(%s) ring because there is not calll\n", p_name);
512         chan_trace_header(p_m_mISDNport, this, "RING", DIRECTION_NONE);
513         callerid = numberrize_callerinfo(p_callerinfo.id, p_callerinfo.ntype, options.national, options.international);
514         add_trace("caller", "id", callerid);
515         end_trace();
516         ph_control_pots(POTS_RING_ON, (unsigned char *)callerid, strlen(callerid));
517 }
518
519 void Pfxs::message_proceeding(unsigned int epoint_id, int message_id, union parameter *param)
520 {
521         new_state(PORT_STATE_IN_PROCEEDING);
522 }
523
524 void Pfxs::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
525 {
526         new_state(PORT_STATE_IN_ALERTING);
527 }
528
529 void Pfxs::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
530 {
531         new_state(PORT_STATE_CONNECT);
532
533         memcpy(&p_connectinfo, &param->connectinfo, sizeof(struct connect_info));
534 }
535
536 void Pfxs::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
537 {
538         if (p_state == PORT_STATE_OUT_ALERTING) {
539                 if (p_m_fxs_knocking) {
540                         ph_control_pots(POTS_CW_OFF, NULL, 0);
541                         p_m_fxs_knocking = 0;
542                 } else {
543                         ph_control_pots(POTS_RING_OFF, NULL, 0);
544                 }
545                 if (p_epointlist) {
546                         struct lcr_msg *message;
547                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
548                         message->param.disconnectinfo.cause = 16;
549                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
550                         message_put(message);
551                 }
552                 free_epointid(epoint_id);
553                 new_state(PORT_STATE_RELEASE);
554                 trigger_work(&p_m_fxs_delete);
555                 return;
556         }
557
558         new_state(PORT_STATE_OUT_DISCONNECT);
559 }
560
561 void Pfxs::message_release(unsigned int epoint_id, int message_id, union parameter *param)
562 {
563         chan_trace_header(p_m_mISDNport, this, "CLEAR", DIRECTION_NONE);
564         end_trace();
565
566         if (p_state == PORT_STATE_OUT_ALERTING) {
567                 if (p_m_fxs_knocking) {
568                         ph_control_pots(POTS_CW_OFF, NULL, 0);
569                         p_m_fxs_knocking = 0;
570                 } else {
571                         ph_control_pots(POTS_RING_OFF, NULL, 0);
572                 }
573                 trigger_work(&p_m_fxs_delete);
574         }
575         if (p_state == PORT_STATE_CONNECT) {
576                 if (!p_m_hold)
577                         set_tone("", "release");
578                 else
579                         trigger_work(&p_m_fxs_delete);
580         }
581
582         new_state(PORT_STATE_RELEASE);
583
584         free_epointid(epoint_id);
585 }
586
587 /*
588  * endpoint sends messages to the port
589  */
590 int Pfxs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
591 {
592         if (PmISDN::message_epoint(epoint_id, message_id, param))
593                 return(1);
594
595         switch(message_id) {
596                 case MESSAGE_SETUP: /* dial-out command received from epoint */
597                 message_setup(epoint_id, message_id, param);
598                 break;
599
600                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
601                 message_proceeding(epoint_id, message_id, param);
602                 break;
603
604                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
605                 message_alerting(epoint_id, message_id, param);
606                 break;
607
608                 case MESSAGE_CONNECT: /* call of endpoint is connected */
609                 message_connect(epoint_id, message_id, param);
610                 break;
611
612                 case MESSAGE_DISCONNECT: /* call has been disconnected */
613                 message_disconnect(epoint_id, message_id, param);
614                 break;
615
616                 case MESSAGE_RELEASE: /* release isdn port */
617                 if (p_state==PORT_STATE_RELEASE) {
618                         break;
619                 }
620                 message_release(epoint_id, message_id, param);
621                 break;
622         }
623
624         return(1);
625 }
626
627 /*
628  * data from isdn-stack (layer-1) to pbx (port class)
629  */
630 int stack2manager_fxs(struct mISDNport *mISDNport, unsigned int cont)
631 {
632         class Port *port;
633         class Pfxs *pots, *latest_pots = NULL, *alerting_pots = NULL;
634         int latest = -1;
635         char name[32];
636
637         PDEBUG(DEBUG_ISDN, "cont(0x%x)\n", cont);
638
639         if ((cont & (~POTS_KP_MASK)) == POTS_KP_VAL) {
640                 /* find port in dialing state */
641                 port = port_first;
642                 while(port) {
643                         if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
644                                 pots = (class Pfxs *)port;
645                                 if (pots->p_m_mISDNport == mISDNport
646                                  && pots->p_state == PORT_STATE_IN_OVERLAP)
647                                         break; // found
648                         }
649                         port = port->next;
650                 }
651                 if (port) {
652                         pots->keypulse_ind(cont);
653                         return 0;
654                 }
655                 goto flash;
656         }
657
658         switch (cont) {
659         case POTS_OFF_HOOK:
660                 /* find ringing */
661                 port = port_first;
662                 while(port) {
663                         if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
664                                 pots = (class Pfxs *)port;
665                                 if (pots->p_m_mISDNport == mISDNport
666                                  && pots->p_state == PORT_STATE_OUT_ALERTING)
667                                         break; // found
668                         }
669                         port = port->next;
670                 }
671                 if (port) {
672                         pots->answer_ind(cont);
673                         break;
674                 }
675
676 setup:
677                 /* creating port object */
678                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
679                 pots = new Pfxs(PORT_TYPE_POTS_FXS_IN, mISDNport, name, NULL, mISDNport->ifport->interface, B_MODE_TRANSPARENT);
680                 if (!pots)
681                         FATAL("Failed to create Port instance\n");
682                 pots->pickup_ind(cont);
683                 break;
684
685         case POTS_ON_HOOK:
686                 if (mISDNport->ifport->pots_transfer) {
687                         struct lcr_msg *message;
688                         class Pfxs *pots1 = NULL, *pots2 = NULL;
689                         int count = 0;
690                         port = port_first;
691                         while(port) {
692                                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
693                                         pots = (class Pfxs *)port;
694                                         if (pots->p_m_mISDNport == mISDNport) {
695                                                 if (pots->p_state == PORT_STATE_CONNECT
696                                                  || pots->p_state == PORT_STATE_IN_PROCEEDING
697                                                  || pots->p_state == PORT_STATE_IN_ALERTING) {
698                                                         if (count == 0)
699                                                                 pots1 = pots;
700                                                         if (count == 1)
701                                                                 pots2 = pots;
702                                                         count++;
703                                                 }
704                                         }
705                                 }
706                                 port = port->next;
707                         }
708
709                         if (count == 2) {
710                                 if (pots1->p_state == PORT_STATE_CONNECT) {
711                                         message = message_create(pots1->p_serial, ACTIVE_EPOINT(pots1->p_epointlist), PORT_TO_EPOINT, MESSAGE_TRANSFER);
712                                         message_put(message);
713                                 }
714                                 else if (pots2->p_state == PORT_STATE_CONNECT) {
715                                         message = message_create(pots2->p_serial, ACTIVE_EPOINT(pots2->p_epointlist), PORT_TO_EPOINT, MESSAGE_TRANSFER);
716                                         message_put(message);
717                                 }
718                                 pots1->hangup_ind(cont);
719                                 pots2->hangup_ind(cont);
720                                 break;
721                         }
722                 }
723                 if (mISDNport->ifport->pots_ring) {
724                         /* release all except calls on hold, let the latest call ring */
725                         port = port_first;
726                         while(port) {
727                                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
728                                         pots = (class Pfxs *)port;
729                                         if (pots->p_m_mISDNport == mISDNport) {
730                                                 if (pots->p_state == PORT_STATE_CONNECT && pots->p_m_hold) {
731                                                         if (pots->p_m_fxs_age > latest) {
732                                                                 latest = pots->p_m_fxs_age;
733                                                                 latest_pots = pots;
734                                                         }
735                                                 }
736                                                 if (pots->p_state == PORT_STATE_OUT_ALERTING)
737                                                         alerting_pots = pots;
738                                         }
739                                 }
740                                 port = port->next;
741                         }
742                         port = port_first;
743                         while(port) {
744                                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
745                                         pots = (class Pfxs *)port;
746                                         if (pots->p_m_mISDNport == mISDNport) {
747                                                 if ((pots->p_state != PORT_STATE_CONNECT || !pots->p_m_hold) && pots != alerting_pots) {
748                                                         PDEBUG(DEBUG_ISDN, "Pfxs(%s) release because pots-ring-after-hangup set and call not on hold / alerting\n", pots->p_name);
749                                                         pots->hangup_ind(cont);
750                                                 }
751                                         }
752                                 }
753                                 port = port->next;
754                         }
755                         if (alerting_pots) {
756                                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) answer because pots-ring-after-hangup set and call is alerting (knocking)\n", alerting_pots->p_name);
757                                 alerting_pots->retrieve_ind(cont);
758                                 break;
759                         }
760                         if (latest_pots) {
761                                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) retrieve because pots-ring-after-hangup set and call is latest on hold\n", latest_pots->p_name);
762                                 latest_pots->retrieve_ind(cont);
763                                 break;
764                         }
765                 } else {
766                         /* release all pots */
767                         port = port_first;
768                         while(port) {
769                                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
770                                         pots = (class Pfxs *)port;
771                                         if (pots->p_m_mISDNport == mISDNport) {
772                                                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) release because pots-ring-after-hangup not set\n", pots->p_name);
773                                                 pots->hangup_ind(cont);
774                                         }
775                                 }
776                                 port = port->next;
777                         }
778                 }
779                 break;
780         case POTS_HOOK_FLASH:
781         case POTS_EARTH_KEY:
782 flash:
783                 if (!mISDNport->ifport->pots_flash) {
784                         PDEBUG(DEBUG_ISDN, "Pfxs flash key is disabled\n");
785                         break;
786                 }
787                 /* hold active pots / release not active pots */
788                 port = port_first;
789                 while(port) {
790                         if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
791                                 pots = (class Pfxs *)port;
792                                 if (pots->p_m_mISDNport == mISDNport) {
793                                         if (pots->p_state == PORT_STATE_CONNECT) {
794                                                 if (pots->p_m_hold) {
795                                                         if (pots->p_m_fxs_age > latest) {
796                                                                 latest = pots->p_m_fxs_age;
797                                                                 latest_pots = pots;
798                                                         }
799                                                 } else {
800                                                         PDEBUG(DEBUG_ISDN, "Pfxs(%s) hold, because flash on active call\n", pots->p_name);
801                                                         pots->hold_ind(cont);
802                                                 }
803                                         } else if (pots->p_state == PORT_STATE_OUT_ALERTING) {
804                                                 alerting_pots = pots;
805                                         } else {
806                                                 PDEBUG(DEBUG_ISDN, "Pfxs(%s) hangup, because flash on incomplete/released call\n", pots->p_name);
807                                                 pots->hangup_ind(cont);
808                                         }
809                                 }
810                         }
811                         port = port->next;
812                 }
813 #if 0
814                 /* now we have our bchannel available, so we can look alerting port to answer */
815                 if (alerting_pots) {
816                         PDEBUG(DEBUG_ISDN, "Pfxs(%s) answer because call is alerting (knocking)\n", alerting_pots->p_name);
817                         alerting_pots->answer_ind(cont);
818                         break;
819                 }
820                 if (latest_pots) {
821                         PDEBUG(DEBUG_ISDN, "Pfxs(%s) retrieve because call is latest on hold\n", latest_pots->p_name);
822                         latest_pots->retrieve_ind(cont);
823                         break;
824                 }
825 #endif
826                 goto setup;
827
828                 default:
829                 PERROR("unhandled message: xontrol(0x%x)\n", cont);
830                 return(-EINVAL);
831         }
832         return(0);
833 }
834
835 #endif /* ISDN_P_FXS_POTS */