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