Make LCR compile with latest osmocomBB code.
[lcr.git] / gsm_ms.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN gsm (MS mode)                                                       **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 #ifndef _GNU_SOURCE
15 #define _GNU_SOURCE
16 #endif
17 extern "C" {
18 #include <getopt.h>
19 #include <arpa/inet.h>
20
21 #include <osmocom/core/select.h>
22 #include <osmocom/core/talloc.h>
23 #include <osmocom/core/gsmtap_util.h>
24
25 #include <osmocom/bb/common/osmocom_data.h>
26 #include <osmocom/bb/common/logging.h>
27 #include <osmocom/bb/common/l1l2_interface.h>
28 #include <osmocom/bb/mobile/app_mobile.h>
29 }
30
31 static const char *config_file = "/etc/osmocom/osmocom.cfg";
32 short vty_port = 4247;
33
34 struct llist_head ms_list;
35 struct log_target *stderr_target;
36 void *l23_ctx = NULL;
37 struct gsmtap_inst *gsmtap_inst;
38
39 static int dtmf_timeout(struct lcr_timer *timer, void *instance, int index);
40
41 /*
42  * constructor
43  */
44 Pgsm_ms::Pgsm_ms(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : Pgsm(type, mISDNport, portname, settings, channel, exclusive, mode)
45 {
46         struct osmocom_ms *ms = NULL;
47         char *ms_name = mISDNport->ifport->gsm_ms_name;
48
49         p_m_g_instance = NULL;
50
51         llist_for_each_entry(ms, &ms_list, entity) {
52                 if (!strcmp(ms->name, ms_name)) {
53                         p_m_g_instance = ms;
54                         break;
55                 }
56         }
57
58         p_m_g_dtmf_state = DTMF_ST_IDLE;
59         p_m_g_dtmf_index = 0;
60         p_m_g_dtmf[0] = '\0';
61         memset(&p_m_g_dtmf_timer, 0, sizeof(p_m_g_dtmf_timer));
62         add_timer(&p_m_g_dtmf_timer, dtmf_timeout, this, 0);
63
64         PDEBUG(DEBUG_GSM, "Created new GSMMSPort(%s %s).\n", portname, ms_name);
65 }
66
67 /*
68  * destructor
69  */
70 Pgsm_ms::~Pgsm_ms()
71 {
72         PDEBUG(DEBUG_GSM, "Destroyed GSM MS process(%s).\n", p_name);
73         del_timer(&p_m_g_dtmf_timer);
74 }
75
76 /*
77  * handles all indications
78  */
79 /* SETUP INDICATION */
80 void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
81 {
82         int ret;
83         class Endpoint *epoint;
84         struct lcr_msg *message;
85         int channel;
86         struct gsm_mncc *mode, *proceeding, *frame;
87
88         /* process given callref */
89         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
90         add_trace("callref", "new", "0x%x", callref);
91         if (p_m_g_callref) {
92                 /* release in case the ID is already in use */
93                 add_trace("error", NULL, "callref already in use");
94                 end_trace();
95                 mncc = create_mncc(MNCC_REJ_REQ, callref);
96                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
97                 mncc->fields |= MNCC_F_CAUSE;
98                 mncc->cause.coding = 3;
99                 mncc->cause.location = 1;
100                 mncc->cause.value = 47;
101                 add_trace("cause", "coding", "%d", mncc->cause.coding);
102                 add_trace("cause", "location", "%d", mncc->cause.location);
103                 add_trace("cause", "value", "%d", mncc->cause.value);
104                 add_trace("reason", NULL, "callref already in use");
105                 end_trace();
106                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
107                 new_state(PORT_STATE_RELEASE);
108                 trigger_work(&p_m_g_delete);
109                 return;
110         }
111         p_m_g_callref = callref;
112         end_trace();
113
114         /* if blocked, release call with MT_RELEASE_COMPLETE */
115         if (p_m_mISDNport->ifport->block) {
116                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
117                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
118                 mncc->fields |= MNCC_F_CAUSE;
119                 mncc->cause.coding = 3;
120                 mncc->cause.location = 1;
121                 mncc->cause.value = 27;
122                 add_trace("cause", "coding", "%d", mncc->cause.coding);
123                 add_trace("cause", "location", "%d", mncc->cause.location);
124                 add_trace("cause", "value", "%d", mncc->cause.value);
125                 add_trace("reason", NULL, "port is blocked");
126                 end_trace();
127                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
128                 new_state(PORT_STATE_RELEASE);
129                 trigger_work(&p_m_g_delete);
130                 return;
131         }
132
133         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_IND, DIRECTION_IN);
134         /* caller information */
135         p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
136         if (mncc->fields & MNCC_F_CALLING) {
137                 switch (mncc->calling.present) {
138                         case 1:
139                         p_callerinfo.present = INFO_PRESENT_RESTRICTED;
140                         break;
141                         case 2:
142                         p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
143                         break;
144                         default:
145                         p_callerinfo.present = INFO_PRESENT_ALLOWED;
146                         break;
147                 }
148                 switch (mncc->calling.screen) {
149                         case 0:
150                         p_callerinfo.screen = INFO_SCREEN_USER;
151                         break;
152                         case 1:
153                         p_callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
154                         break;
155                         case 2:
156                         p_callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
157                         break;
158                         default:
159                         p_callerinfo.screen = INFO_SCREEN_NETWORK;
160                         break;
161                 }
162                 switch (mncc->calling.type) {
163                         case 0x0:
164                         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
165                         break;
166                         case 0x1:
167                         p_callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
168                         break;
169                         case 0x2:
170                         p_callerinfo.ntype = INFO_NTYPE_NATIONAL;
171                         break;
172                         case 0x4:
173                         p_callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
174                         break;
175                         default:
176                         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
177                         break;
178                 }
179                 SCPY(p_callerinfo.id, mncc->calling.number);
180                 add_trace("calling", "type", "%d", mncc->calling.type);
181                 add_trace("calling", "plan", "%d", mncc->calling.plan);
182                 add_trace("calling", "present", "%d", mncc->calling.present);
183                 add_trace("calling", "screen", "%d", mncc->calling.screen);
184                 add_trace("calling", "number", "%s", mncc->calling.number);
185         }
186         p_callerinfo.isdn_port = p_m_portnum;
187         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
188         /* dialing information */
189         if (mncc->fields & MNCC_F_CALLED) {
190                 SCAT(p_dialinginfo.id, mncc->called.number);
191                 switch (mncc->called.type) {
192                         case 0x1:
193                         p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
194                         break;
195                         case 0x2:
196                         p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
197                         break;
198                         case 0x4:
199                         p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
200                         break;
201                         default:
202                         p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
203                         break;
204                 }
205                 add_trace("dialing", "type", "%d", mncc->called.type);
206                 add_trace("dialing", "plan", "%d", mncc->called.plan);
207                 add_trace("dialing", "number", "%s", mncc->called.number);
208         }
209         p_dialinginfo.sending_complete = 1;
210         /* redir info */
211         p_redirinfo.ntype = INFO_NTYPE_NOTPRESENT;
212         if (mncc->fields & MNCC_F_REDIRECTING) {
213                 switch (mncc->redirecting.present) {
214                         case 1:
215                         p_redirinfo.present = INFO_PRESENT_RESTRICTED;
216                         break;
217                         case 2:
218                         p_redirinfo.present = INFO_PRESENT_NOTAVAIL;
219                         break;
220                         default:
221                         p_redirinfo.present = INFO_PRESENT_ALLOWED;
222                         break;
223                 }
224                 switch (mncc->redirecting.screen) {
225                         case 0:
226                         p_redirinfo.screen = INFO_SCREEN_USER;
227                         break;
228                         case 1:
229                         p_redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
230                         break;
231                         case 2:
232                         p_redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
233                         break;
234                         default:
235                         p_redirinfo.screen = INFO_SCREEN_NETWORK;
236                         break;
237                 }
238                 switch (mncc->redirecting.type) {
239                         case 0x0:
240                         p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
241                         break;
242                         case 0x1:
243                         p_redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
244                         break;
245                         case 0x2:
246                         p_redirinfo.ntype = INFO_NTYPE_NATIONAL;
247                         break;
248                         case 0x4:
249                         p_redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
250                         break;
251                         default:
252                         p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
253                         break;
254                 }
255                 SCPY(p_redirinfo.id, mncc->redirecting.number);
256                 add_trace("redir", "type", "%d", mncc->redirecting.type);
257                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
258                 add_trace("redir", "present", "%d", mncc->redirecting.present);
259                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
260                 add_trace("redir", "number", "%s", mncc->redirecting.number);
261                 p_redirinfo.isdn_port = p_m_portnum;
262         }
263         /* bearer capability */
264         if (mncc->fields & MNCC_F_BEARER_CAP) {
265                 switch (mncc->bearer_cap.transfer) {
266                         case 1:
267                         p_capainfo.bearer_capa = INFO_BC_DATAUNRESTRICTED;
268                         break;
269                         case 2:
270                         case 3:
271                         p_capainfo.bearer_capa = INFO_BC_AUDIO;
272                         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
273                         break;
274                         default:
275                         p_capainfo.bearer_capa = INFO_BC_SPEECH;
276                         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
277                         break;
278                 }
279                 switch (mncc->bearer_cap.mode) {
280                         case 1:
281                         p_capainfo.bearer_mode = INFO_BMODE_PACKET;
282                         break;
283                         default:
284                         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
285                         break;
286                 }
287                 add_trace("bearer", "transfer", "%d", mncc->bearer_cap.transfer);
288                 add_trace("bearer", "mode", "%d", mncc->bearer_cap.mode);
289         } else {
290                 p_capainfo.bearer_capa = INFO_BC_SPEECH;
291                 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
292                 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
293         }
294         /* if packet mode works some day, see dss1.cpp for conditions */
295         p_capainfo.source_mode = B_MODE_TRANSPARENT;
296
297         end_trace();
298
299         /* hunt channel */
300         ret = channel = hunt_bchannel();
301         if (ret < 0)
302                 goto no_channel;
303
304         /* open channel */
305         ret = seize_bchannel(channel, 1);
306         if (ret < 0) {
307                 no_channel:
308                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
309                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
310                 mncc->fields |= MNCC_F_CAUSE;
311                 mncc->cause.coding = 3;
312                 mncc->cause.location = 1;
313                 mncc->cause.value = 34;
314                 add_trace("cause", "coding", "%d", mncc->cause.coding);
315                 add_trace("cause", "location", "%d", mncc->cause.location);
316                 add_trace("cause", "value", "%d", mncc->cause.value);
317                 add_trace("reason", NULL, "no channel");
318                 end_trace();
319                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
320                 new_state(PORT_STATE_RELEASE);
321                 trigger_work(&p_m_g_delete);
322                 return;
323         }
324         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
325         if (bchannel_open(p_m_b_index))
326                 goto no_channel;
327
328         /* create endpoint */
329         if (p_epointlist)
330                 FATAL("Incoming call but already got an endpoint.\n");
331         if (!(epoint = new Endpoint(p_serial, 0)))
332                 FATAL("No memory for Endpoint instance\n");
333         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
334                 FATAL("No memory for Endpoint Application instance\n");
335         epointlist_new(epoint->ep_serial);
336
337         /* modify lchan to GSM codec V1 */
338         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
339         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
340         mode->lchan_mode = 0x01; /* GSM V1 */
341         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
342         end_trace();
343         send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
344
345         /* send call proceeding */
346         gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_CONF_REQ, DIRECTION_OUT);
347         proceeding = create_mncc(MNCC_CALL_CONF_REQ, p_m_g_callref);
348         // FIXME: bearer
349         /* DTMF supported */
350         proceeding->fields |= MNCC_F_CCCAP;
351         proceeding->cccap.dtmf = 1;
352         end_trace();
353         send_and_free_mncc(p_m_g_instance, proceeding->msg_type, proceeding);
354
355         new_state(PORT_STATE_IN_PROCEEDING);
356
357         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
358                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
359                 end_trace();
360                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
361                 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
362                 p_m_g_tch_connected = 1;
363         }
364
365         /* send setup message to endpoit */
366         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
367         message->param.setup.isdn_port = p_m_portnum;
368         message->param.setup.port_type = p_type;
369 //      message->param.setup.dtmf = 0;
370         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
371         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
372         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
373         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
374         message->param.setup.useruser.len = strlen(mncc->useruser.info);
375         message->param.setup.useruser.protocol = mncc->useruser.proto;
376         message_put(message);
377 }
378
379 /*
380  * MS sends message to port
381  */
382 static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
383 {
384         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
385         unsigned int callref = mncc->callref;
386         class Port *port;
387         class Pgsm_ms *pgsm_ms = NULL;
388         char name[64];
389         struct mISDNport *mISDNport;
390
391         /* Special messages */
392         switch (msg_type) {
393         case MS_NEW:
394                 PDEBUG(DEBUG_GSM, "MS %s comes available\n", ms->name);
395                 return 0;
396         case MS_DELETE:
397                 PDEBUG(DEBUG_GSM, "MS %s is removed\n", ms->name);
398                 port = port_first;
399                 while(port) {
400                         if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_MS) {
401                                 pgsm_ms = (class Pgsm_ms *)port;
402                                 if (pgsm_ms->p_m_g_instance == ms) {
403                                         struct lcr_msg *message;
404
405                                         pgsm_ms->p_m_g_instance = 0;
406                                         message = message_create(pgsm_ms->p_serial, ACTIVE_EPOINT(pgsm_ms->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
407                                         message->param.disconnectinfo.cause = 27;
408                                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
409                                         message_put(message);
410                                         pgsm_ms->new_state(PORT_STATE_RELEASE);
411                                         trigger_work(&pgsm_ms->p_m_g_delete);
412                                 }
413                         }
414                         port = port->next;
415                 }
416                 return 0;
417         }
418
419         /* find callref */
420         callref = mncc->callref;
421         port = port_first;
422         while(port) {
423                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_MS) {
424                         pgsm_ms = (class Pgsm_ms *)port;
425                         if (pgsm_ms->p_m_g_callref == callref) {
426                                 break;
427                         }
428                 }
429                 port = port->next;
430         }
431
432         if (msg_type == GSM_TCHF_FRAME) {
433                 if (port)
434                         pgsm_ms->frame_receive(arg);
435                 return 0;
436         }
437
438         if (!port) {
439                 if (msg_type != MNCC_SETUP_IND)
440                         return(0);
441                 /* find gsm ms port */
442                 mISDNport = mISDNport_first;
443                 while(mISDNport) {
444                         if (mISDNport->gsm_ms && !strcmp(mISDNport->ifport->gsm_ms_name, ms->name))
445                                 break;
446                         mISDNport = mISDNport->next;
447                 }
448                 if (!mISDNport) {
449                         struct gsm_mncc *rej;
450
451                         rej = create_mncc(MNCC_REJ_REQ, callref);
452                         rej->fields |= MNCC_F_CAUSE;
453                         rej->cause.coding = 3;
454                         rej->cause.location = 1;
455                         rej->cause.value = 27;
456                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
457                         add_trace("cause", "coding", "%d", rej->cause.coding);
458                         add_trace("cause", "location", "%d", rej->cause.location);
459                         add_trace("cause", "value", "%d", rej->cause.value);
460                         end_trace();
461                         send_and_free_mncc(ms, rej->msg_type, rej);
462                         return 0;
463                 }
464                 /* creating port object, transparent until setup with hdlc */
465                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
466                 if (!(pgsm_ms = new Pgsm_ms(PORT_TYPE_GSM_MS_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
467
468                         FATAL("Cannot create Port instance.\n");
469         }
470
471         switch(msg_type) {
472                 case MNCC_SETUP_IND:
473                 pgsm_ms->setup_ind(msg_type, callref, mncc);
474                 break;
475
476                 case MNCC_CALL_PROC_IND:
477                 pgsm_ms->call_proc_ind(msg_type, callref, mncc);
478                 break;
479
480                 case MNCC_ALERT_IND:
481                 pgsm_ms->alert_ind(msg_type, callref, mncc);
482                 break;
483
484                 case MNCC_SETUP_CNF:
485                 pgsm_ms->setup_cnf(msg_type, callref, mncc);
486                 break;
487
488                 case MNCC_SETUP_COMPL_IND:
489                 pgsm_ms->setup_compl_ind(msg_type, callref, mncc);
490                 break;
491
492                 case MNCC_DISC_IND:
493                 pgsm_ms->disc_ind(msg_type, callref, mncc);
494                 break;
495
496                 case MNCC_REL_IND:
497                 case MNCC_REL_CNF:
498                 case MNCC_REJ_IND:
499                 pgsm_ms->rel_ind(msg_type, callref, mncc);
500                 break;
501
502                 case MNCC_NOTIFY_IND:
503                 pgsm_ms->notify_ind(msg_type, callref, mncc);
504                 break;
505
506                 case MNCC_START_DTMF_RSP:
507                 case MNCC_START_DTMF_REJ:
508                 case MNCC_STOP_DTMF_RSP:
509                 pgsm_ms->dtmf_statemachine(mncc);
510                 break;
511
512                 default:
513                 ;
514         }
515         return(0);
516 }
517
518 /* MESSAGE_SETUP */
519 void Pgsm_ms::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
520 {
521         struct lcr_msg *message;
522         int ret;
523         struct epoint_list *epointlist;
524         struct gsm_mncc *mncc;
525         int channel;
526
527         /* copy setup infos to port */
528         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
529         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
530         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
531         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
532
533         /* no instance */
534         if (!p_m_g_instance) {
535                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
536                 add_trace("failure", NULL, "MS %s instance is unavailable", p_m_mISDNport->ifport->gsm_ms_name);
537                 end_trace();
538                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
539                 message->param.disconnectinfo.cause = 27;
540                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
541                 message_put(message);
542                 new_state(PORT_STATE_RELEASE);
543                 trigger_work(&p_m_g_delete);
544                 return;
545         }
546         
547         /* no number */
548         if (!p_dialinginfo.id[0]) {
549                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
550                 add_trace("failure", NULL, "No dialed subscriber given.");
551                 end_trace();
552                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
553                 message->param.disconnectinfo.cause = 28;
554                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
555                 message_put(message);
556                 new_state(PORT_STATE_RELEASE);
557                 trigger_work(&p_m_g_delete);
558                 return;
559         }
560         
561         /* release if port is blocked */
562         if (p_m_mISDNport->ifport->block) {
563                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
564                 add_trace("failure", NULL, "Port blocked.");
565                 end_trace();
566                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
567                 message->param.disconnectinfo.cause = 27; // temp. unavail.
568                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
569                 message_put(message);
570                 new_state(PORT_STATE_RELEASE);
571                 trigger_work(&p_m_g_delete);
572                 return;
573         }
574
575         /* hunt channel */
576         ret = channel = hunt_bchannel();
577         if (ret < 0)
578                 goto no_channel;
579         /* open channel */
580         ret = seize_bchannel(channel, 1);
581         if (ret < 0) {
582                 no_channel:
583                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
584                 add_trace("failure", NULL, "No internal audio channel available.");
585                 end_trace();
586                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
587                 message->param.disconnectinfo.cause = 34;
588                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
589                 message_put(message);
590                 new_state(PORT_STATE_RELEASE);
591                 trigger_work(&p_m_g_delete);
592                 return;
593         }
594         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
595         if (bchannel_open(p_m_b_index))
596                 goto no_channel;
597
598         /* attach only if not already */
599         epointlist = p_epointlist;
600         while(epointlist) {
601                 if (epointlist->epoint_id == epoint_id)
602                         break;
603                 epointlist = epointlist->next;
604         }
605         if (!epointlist)
606                 epointlist_new(epoint_id);
607
608         /* creating l3id */
609         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
610         p_m_g_callref = new_callref++;
611         add_trace("callref", "new", "0x%x", p_m_g_callref);
612         end_trace();
613
614         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
615         mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
616         if (!strncasecmp(p_dialinginfo.id, "emerg", 5)) {
617                 mncc->emergency = 1;
618         } else {
619                 /* caller info (only clir) */
620                 switch (p_callerinfo.present) {
621                         case INFO_PRESENT_ALLOWED:
622                         mncc->clir.inv = 1;
623                         break;
624                         default:
625                         mncc->clir.sup = 1;
626                 }
627                 /* dialing information (mandatory) */
628                 mncc->fields |= MNCC_F_CALLED;
629                 mncc->called.type = 0; /* unknown */
630                 mncc->called.plan = 1; /* isdn */
631                 SCPY(mncc->called.number, p_dialinginfo.id);
632                 add_trace("dialing", "number", "%s", mncc->called.number);
633                 
634                 /* bearer capability (mandatory) */
635                 mncc->fields |= MNCC_F_BEARER_CAP;
636                 mncc->bearer_cap.coding = 0;
637                 mncc->bearer_cap.radio = 1;
638                 mncc->bearer_cap.speech_ctm = 0;
639                 mncc->bearer_cap.speech_ver[0] = 0;
640                 mncc->bearer_cap.speech_ver[1] = -1; /* end of list */
641                 switch (p_capainfo.bearer_capa) {
642                         case INFO_BC_DATAUNRESTRICTED:
643                         case INFO_BC_DATARESTRICTED:
644                         mncc->bearer_cap.transfer = 1;
645                         break;
646                         case INFO_BC_SPEECH:
647                         mncc->bearer_cap.transfer = 0;
648                         break;
649                         default:
650                         mncc->bearer_cap.transfer = 2;
651                         break;
652                 }
653                 switch (p_capainfo.bearer_mode) {
654                         case INFO_BMODE_PACKET:
655                         mncc->bearer_cap.mode = 1;
656                         break;
657                         default:
658                         mncc->bearer_cap.mode = 0;
659                         break;
660                 }
661                 /* DTMF supported */
662                 mncc->fields |= MNCC_F_CCCAP;
663                 mncc->cccap.dtmf = 1;
664
665                 /* request keypad from remote */
666                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ENABLEKEYPAD);
667                 message_put(message);
668         }
669
670         end_trace();
671         send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
672
673         new_state(PORT_STATE_OUT_SETUP);
674
675         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
676         message_put(message);
677
678         new_state(PORT_STATE_OUT_PROCEEDING);
679 }
680
681 void Pgsm_ms::dtmf_statemachine(struct gsm_mncc *mncc)
682 {
683         struct gsm_mncc *dtmf;
684
685         switch (p_m_g_dtmf_state) {
686         case DTMF_ST_SPACE:
687         case DTMF_ST_IDLE:
688                 /* end of string */
689                 if (!p_m_g_dtmf[p_m_g_dtmf_index]) {
690                         PDEBUG(DEBUG_GSM, "done with DTMF\n");
691                         p_m_g_dtmf_state = DTMF_ST_IDLE;
692                         return;
693                 }
694                 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_REQ, DIRECTION_OUT);
695                 dtmf = create_mncc(MNCC_START_DTMF_REQ, p_m_g_callref);
696                 dtmf->keypad = p_m_g_dtmf[p_m_g_dtmf_index++];
697                 p_m_g_dtmf_state = DTMF_ST_START;
698                 PDEBUG(DEBUG_GSM, "start DTMF (keypad %c)\n",
699                         dtmf->keypad);
700                 end_trace();
701                 send_and_free_mncc(p_m_g_instance, dtmf->msg_type, dtmf);
702                 return;
703         case DTMF_ST_START:
704                 if (mncc->msg_type != MNCC_START_DTMF_RSP) {
705                         PDEBUG(DEBUG_GSM, "DTMF was rejected\n");
706                         return;
707                 }
708                 schedule_timer(&p_m_g_dtmf_timer, 0, 70 * 1000);
709                 p_m_g_dtmf_state = DTMF_ST_MARK;
710                 PDEBUG(DEBUG_GSM, "DTMF is on\n");
711                 break;
712         case DTMF_ST_MARK:
713                 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_REQ, DIRECTION_OUT);
714                 dtmf = create_mncc(MNCC_STOP_DTMF_REQ, p_m_g_callref);
715                 p_m_g_dtmf_state = DTMF_ST_STOP;
716                 end_trace();
717                 send_and_free_mncc(p_m_g_instance, dtmf->msg_type, dtmf);
718                 return;
719         case DTMF_ST_STOP:
720                 schedule_timer(&p_m_g_dtmf_timer, 0, 120 * 1000);
721                 p_m_g_dtmf_state = DTMF_ST_SPACE;
722                 PDEBUG(DEBUG_GSM, "DTMF is off\n");
723                 break;
724         }
725 }
726
727 static int dtmf_timeout(struct lcr_timer *timer, void *instance, int index)
728 {
729         class Pgsm_ms *pgsm_ms = (class Pgsm_ms *)instance;
730
731         PDEBUG(DEBUG_GSM, "DTMF timer has fired\n");
732         pgsm_ms->dtmf_statemachine(NULL);
733
734         return 0;
735 }
736
737 /* MESSAGE_DTMF */
738 void Pgsm_ms::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
739 {
740         char digit = param->dtmf;
741
742         if (digit >= 'a' && digit <= 'c')
743                 digit = digit - 'a' + 'A';
744         if (!strchr("01234567890*#ABC", digit))
745                 return;
746
747         /* schedule */
748         if (p_m_g_dtmf_state == DTMF_ST_IDLE) {
749                 p_m_g_dtmf_index = 0;
750                 p_m_g_dtmf[0] = '\0';
751         }
752         SCCAT(p_m_g_dtmf, digit);
753         if (p_m_g_dtmf_state == DTMF_ST_IDLE)
754                 dtmf_statemachine(NULL);
755 }
756
757 /* MESSAGE_INFORMATION */
758 void Pgsm_ms::message_information(unsigned int epoint_id, int message_id, union parameter *param)
759 {
760         char digit;
761         int i;
762
763         for (i = 0; i < (int)strlen(param->information.id); i++) {
764                 digit = param->information.id[i];
765                 if (digit >= 'a' && digit <= 'c')
766                         digit = digit - 'a' + 'A';
767                 if (!strchr("01234567890*#ABC", digit))
768                         continue;
769
770                 /* schedule */
771                 if (p_m_g_dtmf_state == DTMF_ST_IDLE) {
772                         p_m_g_dtmf_index = 0;
773                         p_m_g_dtmf[0] = '\0';
774                 }
775                 SCCAT(p_m_g_dtmf, digit);
776                 if (p_m_g_dtmf_state == DTMF_ST_IDLE)
777                         dtmf_statemachine(NULL);
778         }
779 }
780
781 /*
782  * endpoint sends messages to the port
783  */
784 int Pgsm_ms::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
785 {
786         struct lcr_msg *message;
787
788         if (message_id == MESSAGE_CONNECT) {
789                 /* request keypad from remote */
790                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ENABLEKEYPAD);
791                 message_put(message);
792         }
793
794         if (Pgsm::message_epoint(epoint_id, message_id, param))
795                 return(1);
796
797         switch(message_id) {
798                 case MESSAGE_SETUP: /* dial-out command received from epoint */
799                 if (p_state!=PORT_STATE_IDLE)
800                         break;
801                 message_setup(epoint_id, message_id, param);
802                 break;
803
804                 case MESSAGE_DTMF:
805                 message_dtmf(epoint_id, message_id, param);
806                 break;
807
808                 case MESSAGE_INFORMATION:
809                 message_information(epoint_id, message_id, param);
810                 break;
811
812                 default:
813                 PDEBUG(DEBUG_GSM, "Pgsm_ms(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
814         }
815
816         return(1);
817 }
818
819 int gsm_ms_exit(int rc)
820 {
821         l23_app_exit();
822
823         return(rc);
824 }
825
826 int gsm_ms_init(void)
827 {
828         INIT_LLIST_HEAD(&ms_list);
829         log_init(&log_info);
830         stderr_target = log_target_create_stderr();
831         log_add_target(stderr_target);
832         log_set_all_filter(stderr_target, 1);
833
834         l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
835
836         log_parse_category_mask(stderr_target, "DCS:DPLMN:DRR:DMM:DSIM:DCC:DMNCC:DPAG:DSUM");
837         log_set_log_level(stderr_target, LOGL_INFO);
838
839 #if 0
840         if (gsmtap_ip) {
841                 rc = gsmtap_init(gsmtap_ip);
842                 if (rc < 0) {
843                         fprintf(stderr, "Failed during gsmtap_init()\n");
844                         exit(1);
845                 }
846         }
847 #endif
848
849         l23_app_init(message_ms, config_file, vty_port);
850
851         return 0;
852 }
853
854 /* add a new GSM mobile instance */
855 int gsm_ms_new(const char *name)
856 {
857         PDEBUG(DEBUG_GSM, "GSM: interface for MS '%s' is up\n", name);
858
859         return 0;
860 }
861
862 int gsm_ms_delete(const char *name)
863 {
864         struct osmocom_ms *ms;
865         int found = 0;
866         class Port *port;
867         class Pgsm_ms *pgsm_ms = NULL;
868
869         PDEBUG(DEBUG_GSM, "GSM: interface for MS '%s' is down\n", name);
870
871         llist_for_each_entry(ms, &ms_list, entity) {
872                 if (!strcmp(ms->name, name)) {
873                         found = 1;
874                         break;
875                 }
876         }
877
878         if (!found)
879                 return 0;
880
881         port = port_first;
882         while(port) {
883                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_MS) {
884                         pgsm_ms = (class Pgsm_ms *)port;
885                         if (pgsm_ms->p_m_g_instance == ms && pgsm_ms->p_m_g_callref) {
886                                 struct gsm_mncc *rej;
887
888                                 rej = create_mncc(MNCC_REL_REQ, pgsm_ms->p_m_g_callref);
889                                 rej->fields |= MNCC_F_CAUSE;
890                                 rej->cause.coding = 3;
891                                 rej->cause.location = 1;
892                                 rej->cause.value = 27;
893                                 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
894                                 add_trace("cause", "coding", "%d", rej->cause.coding);
895                                 add_trace("cause", "location", "%d", rej->cause.location);
896                                 add_trace("cause", "value", "%d", rej->cause.value);
897                                 end_trace();
898                         }
899                 }
900         }
901
902         return 0;
903 }
904
905 /*
906  * handles bsc select function within LCR's main loop
907  */
908 int handle_gsm_ms(int *_quit)
909 {
910         int work = 0, quit = 0;
911
912         if (l23_app_work(&quit))
913                 work = 1;
914         if (quit && llist_empty(&ms_list))
915                 *_quit = 1;
916 //      debug_reset_context();
917         if (osmo_select_main(1)) /* polling */
918                 work = 1;
919
920         return work;
921 }
922