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