Fixed audio bridge to mISDN ports
[lcr.git] / gsm_bs.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN gsm (BS mode)                                                       **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13 #include "mncc.h"
14
15 struct lcr_gsm *gsm_bs = NULL;
16
17 /*
18  * DTMF stuff
19  */
20 unsigned char dtmf_samples[16][8000];
21 static int dtmf_x[4] = { 1209, 1336, 1477, 1633 };
22 static int dtmf_y[4] = { 697, 770, 852, 941 };
23
24 void generate_dtmf(void)
25 {
26         double fx, fy, sample;
27         int i, x, y;
28         unsigned char *law;
29
30         for (y = 0; y < 4; y++) {
31                 fy = 2 * 3.1415927 * ((double)dtmf_y[y]) / 8000.0;
32                 for (x = 0; x < 4; x++) {
33                         fx = 2 * 3.1415927 * ((double)dtmf_x[x]) / 8000.0;
34                         law = dtmf_samples[y << 2 | x];
35                         for (i = 0; i < 8000; i++) {
36                                 sample = sin(fy * ((double)i)) * 0.251 * 32767.0; /* -6 dB */
37                                 sample += sin(fx * ((double)i)) * 0.158 * 32767.0; /* -8 dB */
38                                 *law++ = audio_s16_to_law[(int)sample & 0xffff];
39                         }
40                 }
41         }
42 }
43
44
45 /*
46  * constructor
47  */
48 Pgsm_bs::Pgsm_bs(int type, char *portname, struct port_settings *settings, struct interface *interface) : Pgsm(type, portname, settings, interface)
49 {
50         p_g_lcr_gsm = gsm_bs;
51         p_g_dtmf = NULL;
52         p_g_dtmf_index = 0;
53
54         PDEBUG(DEBUG_GSM, "Created new GSMBSPort(%s).\n", portname);
55 }
56
57 /*
58  * destructor
59  */
60 Pgsm_bs::~Pgsm_bs()
61 {
62         PDEBUG(DEBUG_GSM, "Destroyed GSM BS process(%s).\n", p_name);
63 }
64
65 /* DTMF INDICATION */
66 void Pgsm_bs::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
67 {
68         struct gsm_mncc *resp;
69
70         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
71         add_trace("keypad", NULL, "%c", mncc->keypad);
72         end_trace();
73         SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
74         p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
75
76         /* send resp */
77         gsm_trace_header(p_g_interface_name, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
78         add_trace("keypad", NULL, "%c", mncc->keypad);
79         end_trace();
80         resp = create_mncc(MNCC_START_DTMF_RSP, p_g_callref);
81         resp->fields |= MNCC_F_KEYPAD;
82         resp->keypad = mncc->keypad;
83         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
84
85         if (p_g_rtp_bridge) {
86                 class Port *remote = bridge_remote();
87
88                 if (remote) {
89                         struct lcr_msg *message;
90
91                         /* send dtmf information, because we bridge RTP directly */
92                         message = message_create(0, remote->p_serial, EPOINT_TO_PORT, MESSAGE_DTMF);
93                         message->param.dtmf = mncc->keypad;
94                         message_put(message);
95                 }
96         } else {
97                 /* generate DTMF tones, since we do audio forwarding inside LCR */
98                 switch (mncc->keypad) {
99                         case '1': p_g_dtmf = dtmf_samples[0]; break;
100                         case '2': p_g_dtmf = dtmf_samples[1]; break;
101                         case '3': p_g_dtmf = dtmf_samples[2]; break;
102                         case 'a':
103                         case 'A': p_g_dtmf = dtmf_samples[3]; break;
104                         case '4': p_g_dtmf = dtmf_samples[4]; break;
105                         case '5': p_g_dtmf = dtmf_samples[5]; break;
106                         case '6': p_g_dtmf = dtmf_samples[6]; break;
107                         case 'b':
108                         case 'B': p_g_dtmf = dtmf_samples[7]; break;
109                         case '7': p_g_dtmf = dtmf_samples[8]; break;
110                         case '8': p_g_dtmf = dtmf_samples[9]; break;
111                         case '9': p_g_dtmf = dtmf_samples[10]; break;
112                         case 'c':
113                         case 'C': p_g_dtmf = dtmf_samples[11]; break;
114                         case '*': p_g_dtmf = dtmf_samples[12]; break;
115                         case '0': p_g_dtmf = dtmf_samples[13]; break;
116                         case '#': p_g_dtmf = dtmf_samples[14]; break;
117                         case 'd':
118                         case 'D': p_g_dtmf = dtmf_samples[15]; break;
119                 }
120                 p_g_dtmf_index = 0;
121         }
122 }
123 void Pgsm_bs::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
124 {
125         struct gsm_mncc *resp;
126
127         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
128         add_trace("keypad", NULL, "%c", mncc->keypad);
129         end_trace();
130
131         /* send resp */
132         gsm_trace_header(p_g_interface_name, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
133         add_trace("keypad", NULL, "%c", mncc->keypad);
134         end_trace();
135         resp = create_mncc(MNCC_STOP_DTMF_RSP, p_g_callref);
136         resp->keypad = mncc->keypad;
137         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
138         
139         /* stop DTMF */
140         p_g_dtmf = NULL;
141 }
142
143 /* HOLD INDICATION */
144 void Pgsm_bs::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
145 {
146         struct lcr_msg *message;
147         struct gsm_mncc *resp, *frame;
148
149         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
150         end_trace();
151
152         /* notify the hold of call */
153         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
154         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
155         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
156         message_put(message);
157
158         /* acknowledge hold */
159         gsm_trace_header(p_g_interface_name, this, MNCC_HOLD_CNF, DIRECTION_OUT);
160         end_trace();
161         resp = create_mncc(MNCC_HOLD_CNF, p_g_callref);
162         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
163
164         /* disable audio */
165         if (p_g_tch_connected) { /* it should be true */
166                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_DROP, DIRECTION_OUT);
167                 end_trace();
168                 frame = create_mncc(MNCC_FRAME_DROP, p_g_callref);
169                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
170                 p_g_tch_connected = 0;
171         }
172 }
173
174
175 /* RETRIEVE INDICATION */
176 void Pgsm_bs::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
177 {
178         struct lcr_msg *message;
179         struct gsm_mncc *resp, *frame;
180
181         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
182         end_trace();
183
184         /* notify the retrieve of call */
185         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
186         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
187         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
188         message_put(message);
189
190         /* acknowledge retr */
191         gsm_trace_header(p_g_interface_name, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
192         end_trace();
193         resp = create_mncc(MNCC_RETRIEVE_CNF, p_g_callref);
194         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
195
196         /* enable audio */
197         if (!p_g_tch_connected) { /* it should be true */
198                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
199                 end_trace();
200                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
201                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
202                 p_g_tch_connected = 1;
203         }
204 }
205
206 /*
207  * handles all indications
208  */
209 /* SETUP INDICATION */
210 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
211 {
212         class Endpoint *epoint;
213         struct lcr_msg *message;
214         struct gsm_mncc *mode, *proceeding, *frame;
215         struct interface *interface = interface_first;
216
217         while (interface) {
218                 if (!strcmp(interface->name, p_g_interface_name))
219                         break;
220                 interface = interface->next;
221         }
222         if (!interface) {
223                 PERROR("Cannot find interface %s.\n", p_g_interface_name);
224                 return;
225         }
226
227         /* process given callref */
228         gsm_trace_header(p_g_interface_name, this, 0, DIRECTION_IN);
229         add_trace("callref", "new", "0x%x", callref);
230         if (p_g_callref) {
231                 /* release in case the ID is already in use */
232                 add_trace("error", NULL, "callref already in use");
233                 end_trace();
234                 mncc = create_mncc(MNCC_REJ_REQ, callref);
235                 gsm_trace_header(p_g_interface_name, this, MNCC_REJ_REQ, DIRECTION_OUT);
236                 mncc->fields |= MNCC_F_CAUSE;
237                 mncc->cause.coding = 3;
238                 mncc->cause.location = 1;
239                 mncc->cause.value = 47;
240                 add_trace("cause", "coding", "%d", mncc->cause.coding);
241                 add_trace("cause", "location", "%d", mncc->cause.location);
242                 add_trace("cause", "value", "%d", mncc->cause.value);
243                 add_trace("reason", NULL, "callref already in use");
244                 end_trace();
245                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
246                 new_state(PORT_STATE_RELEASE);
247                 trigger_work(&p_g_delete);
248                 return;
249         }
250         p_g_callref = callref;
251         end_trace();
252
253         /* caller info */
254         if (mncc->clir.inv)
255                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
256         else
257                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
258         if (mncc->calling.number[0])
259                 SCPY(p_callerinfo.id, mncc->calling.number);
260         else
261                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
262         SCPY(p_callerinfo.imsi, mncc->imsi);
263         p_callerinfo.screen = INFO_SCREEN_NETWORK;
264         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
265         SCPY(p_callerinfo.interface, p_g_interface_name);
266
267         /* dialing information */
268         SCAT(p_dialinginfo.id, mncc->called.number);
269         switch (mncc->called.type) {
270                 case 0x1:
271                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
272                 break;
273                 case 0x2:
274                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
275                 break;
276                 case 0x4:
277                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
278                 break;
279                 default:
280                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
281                 break;
282         }
283         if (mncc->emergency) {
284                 SCPY(p_dialinginfo.id, "emergency");
285         }
286         p_dialinginfo.sending_complete = 1;
287
288         /* bearer capability */
289         // todo
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         p_capainfo.source_mode = B_MODE_TRANSPARENT;
294         p_g_mode = p_capainfo.source_mode;
295
296         /* useruser */
297
298         /* what infos did we got ... */
299         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
300         if (p_callerinfo.id[0])
301                 add_trace("calling", "number", "%s", p_callerinfo.id);
302         else
303                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
304         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
305         add_trace("dialing", "number", "%s", p_dialinginfo.id);
306         end_trace();
307
308         /* create endpoint */
309         if (p_epointlist)
310                 FATAL("Incoming call but already got an endpoint.\n");
311         if (!(epoint = new Endpoint(p_serial, 0)))
312                 FATAL("No memory for Endpoint instance\n");
313         epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
314         epointlist_new(epoint->ep_serial);
315
316         /* modify lchan to GSM codec V1 */
317         gsm_trace_header(p_g_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
318         mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
319         mode->lchan_mode = 0x01; /* GSM V1 */
320         mode->lchan_type = 0x02;
321         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
322         end_trace();
323         send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
324
325         /* send call proceeding */
326         gsm_trace_header(p_g_interface_name, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
327         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_g_callref);
328         if (p_g_tones) {
329                 proceeding->fields |= MNCC_F_PROGRESS;
330                 proceeding->progress.coding = 3; /* GSM */
331                 proceeding->progress.location = 1;
332                 proceeding->progress.descr = 8;
333                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
334                 add_trace("progress", "location", "%d", proceeding->progress.location);
335                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
336         }
337         end_trace();
338         send_and_free_mncc(p_g_lcr_gsm, proceeding->msg_type, proceeding);
339
340         new_state(PORT_STATE_IN_PROCEEDING);
341
342         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
343                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
344                 end_trace();
345                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
346                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
347                 p_g_tch_connected = 1;
348         }
349
350         /* send setup message to endpoit */
351         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
352         message->param.setup.port_type = p_type;
353 //      message->param.setup.dtmf = 0;
354         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
355         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
356         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
357         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
358         message->param.setup.useruser.len = strlen(mncc->useruser.info);
359         message->param.setup.useruser.protocol = mncc->useruser.proto;
360         message->param.setup.rtpinfo.payload_type = 3; /* FIXME: receive payload type from peer */
361
362         if (p_g_rtp_bridge) {
363                 struct gsm_mncc_rtp *rtp;
364
365                 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding setup\n");
366                 p_g_setup_pending = message;
367                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
368                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
369         } else
370                 message_put(message);
371
372 }
373
374 /*
375  * BSC sends message to port
376  */
377 int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
378 {
379         struct interface *interface = lcr_gsm->interface;
380         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
381         unsigned int callref = mncc->callref;
382         class Port *port;
383         class Pgsm_bs *pgsm_bs = NULL;
384         char name[64];
385 //      struct mISDNport *mISDNport;
386
387         /* Special messages */
388         switch(msg_type) {
389         }
390
391         /* find callref */
392         callref = mncc->callref;
393         port = port_first;
394         while(port) {
395                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
396                         pgsm_bs = (class Pgsm_bs *)port;
397                         if (pgsm_bs->p_g_callref == callref) {
398                                 break;
399                         }
400                 }
401                 port = port->next;
402         }
403
404         if (msg_type == GSM_TCHF_FRAME) {
405                 if (port) {
406                         /* inject DTMF, if enabled */
407                         if (pgsm_bs->p_g_dtmf) {
408                                 unsigned char data[160];
409                                 int i;
410
411                                 for (i = 0; i < 160; i++) {
412                                         data[i] = pgsm_bs->p_g_dtmf[pgsm_bs->p_g_dtmf_index++];
413                                         if (pgsm_bs->p_g_dtmf_index == 8000)
414                                                 pgsm_bs->p_g_dtmf_index = 0;
415                                 }
416                                 /* send */
417                                 pgsm_bs->bridge_tx(data, 160);
418                         } else
419                                 pgsm_bs->frame_receive(arg);
420                         /* if we do not bridge we need to inject audio, if available */
421                         if (!pgsm_bs->p_bridge) {
422                                 unsigned char data[160];
423                                 int i;
424
425                                 i = pgsm_bs->read_audio(data, 160);
426                                 if (i)
427                                         pgsm_bs->bridge_rx(data, i);
428                         }
429                 }
430                 return 0;
431         }
432
433         if (!port) {
434                 if (msg_type != MNCC_SETUP_IND)
435                         return(0);
436 #if 0
437                 /* find gsm port */
438                 mISDNport = mISDNport_first;
439                 while(mISDNport) {
440                         if (mISDNport->gsm_bs)
441                                 break;
442                         mISDNport = mISDNport->next;
443                 }
444                 if (!mISDNport) {
445                         struct gsm_mncc *rej;
446
447                         rej = create_mncc(MNCC_REJ_REQ, callref);
448                         rej->fields |= MNCC_F_CAUSE;
449                         rej->cause.coding = 3;
450                         rej->cause.location = 1;
451                         rej->cause.value = 27;
452                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
453                         add_trace("cause", "coding", "%d", rej->cause.coding);
454                         add_trace("cause", "location", "%d", rej->cause.location);
455                         add_trace("cause", "value", "%d", rej->cause.value);
456                         end_trace();
457                         send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
458                         return 0;
459                 }
460 #endif
461                 /* creating port object, transparent until setup with hdlc */
462                 SPRINT(name, "%s-%d-in", interface->name, 0);
463                 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, name, NULL, interface)))
464                         FATAL("Cannot create Port instance.\n");
465         }
466
467         switch(msg_type) {
468                 case MNCC_SETUP_IND:
469                 pgsm_bs->setup_ind(msg_type, callref, mncc);
470                 break;
471
472                 case MNCC_RTP_CREATE:
473                 pgsm_bs->rtp_create_ind(msg_type, callref, mncc);
474                 break;
475
476                 case MNCC_RTP_CONNECT:
477                 pgsm_bs->rtp_connect_ind(msg_type, callref, mncc);
478                 break;
479
480                 case MNCC_START_DTMF_IND:
481                 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
482                 break;
483
484                 case MNCC_STOP_DTMF_IND:
485                 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
486                 break;
487
488                 case MNCC_CALL_CONF_IND:
489                 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
490                 break;
491
492                 case MNCC_ALERT_IND:
493                 pgsm_bs->alert_ind(msg_type, callref, mncc);
494                 break;
495
496                 case MNCC_SETUP_CNF:
497                 pgsm_bs->setup_cnf(msg_type, callref, mncc);
498                 break;
499
500                 case MNCC_SETUP_COMPL_IND:
501                 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
502                 break;
503
504                 case MNCC_DISC_IND:
505                 pgsm_bs->disc_ind(msg_type, callref, mncc);
506                 break;
507
508                 case MNCC_REL_IND:
509                 case MNCC_REL_CNF:
510                 case MNCC_REJ_IND:
511                 pgsm_bs->rel_ind(msg_type, callref, mncc);
512                 break;
513
514                 case MNCC_NOTIFY_IND:
515                 pgsm_bs->notify_ind(msg_type, callref, mncc);
516                 break;
517
518                 case MNCC_HOLD_IND:
519                 pgsm_bs->hold_ind(msg_type, callref, mncc);
520                 break;
521
522                 case MNCC_RETRIEVE_IND:
523                 pgsm_bs->retr_ind(msg_type, callref, mncc);
524                 break;
525
526                 default:
527                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: 0x%x\n", pgsm_bs->p_name, pgsm_bs->p_callerinfo.id, msg_type);
528         }
529         return(0);
530 }
531
532 /* MESSAGE_SETUP */
533 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
534 {
535         struct lcr_msg *message;
536         struct epoint_list *epointlist;
537         struct gsm_mncc *mncc;
538
539         /* copy setup infos to port */
540         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
541         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
542         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
543         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
544
545         /* no GSM MNCC connection */
546         if (p_g_lcr_gsm->mncc_lfd.fd < 0) {
547                 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
548                 add_trace("failure", NULL, "No MNCC connection.");
549                 end_trace();
550                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
551                 message->param.disconnectinfo.cause = 41; // temp. failure.
552                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
553                 message_put(message);
554                 new_state(PORT_STATE_RELEASE);
555                 trigger_work(&p_g_delete);
556                 return;
557         }
558
559         /* no number */
560         if (!p_dialinginfo.id[0]) {
561                 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
562                 add_trace("failure", NULL, "No dialed subscriber given.");
563                 end_trace();
564                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
565                 message->param.disconnectinfo.cause = 28;
566                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
567                 message_put(message);
568                 new_state(PORT_STATE_RELEASE);
569                 trigger_work(&p_g_delete);
570                 return;
571         }
572
573 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
574         /* screen outgoing caller id */
575         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_g_interface_name);
576
577         /* attach only if not already */
578         epointlist = p_epointlist;
579         while(epointlist) {
580                 if (epointlist->epoint_id == epoint_id)
581                         break;
582                 epointlist = epointlist->next;
583         }
584         if (!epointlist)
585                 epointlist_new(epoint_id);
586
587         /* creating l3id */
588         gsm_trace_header(p_g_interface_name, this, 0, DIRECTION_OUT);
589         p_g_callref = new_callref++;
590         add_trace("callref", "new", "0x%x", p_g_callref);
591         end_trace();
592
593         gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
594         mncc = create_mncc(MNCC_SETUP_REQ, p_g_callref);
595         /* caller information */
596         mncc->fields |= MNCC_F_CALLING;
597         mncc->calling.plan = 1;
598         switch (p_callerinfo.ntype) {
599                 case INFO_NTYPE_UNKNOWN:
600                 mncc->calling.type = 0x0;
601                 break;
602                 case INFO_NTYPE_INTERNATIONAL:
603                 mncc->calling.type = 0x1;
604                 break;
605                 case INFO_NTYPE_NATIONAL:
606                 mncc->calling.type = 0x2;
607                 break;
608                 case INFO_NTYPE_SUBSCRIBER:
609                 mncc->calling.type = 0x4;
610                 break;
611                 default: /* INFO_NTYPE_NOTPRESENT */
612                 mncc->fields &= ~MNCC_F_CALLING;
613                 break;
614         }
615         switch (p_callerinfo.screen) {
616                 case INFO_SCREEN_USER:
617                 mncc->calling.screen = 0;
618                 break;
619                 default: /* INFO_SCREEN_NETWORK */
620                 mncc->calling.screen = 3;
621                 break;
622         }
623         switch (p_callerinfo.present) {
624                 case INFO_PRESENT_ALLOWED:
625                 mncc->calling.present = 0;
626                 break;
627                 case INFO_PRESENT_RESTRICTED:
628                 mncc->calling.present = 1;
629                 break;
630                 default: /* INFO_PRESENT_NOTAVAIL */
631                 mncc->calling.present = 2;
632                 break;
633         }
634         if (mncc->fields & MNCC_F_CALLING) {
635                 SCPY(mncc->calling.number, p_callerinfo.id);
636                 add_trace("calling", "type", "%d", mncc->calling.type);
637                 add_trace("calling", "plan", "%d", mncc->calling.plan);
638                 add_trace("calling", "present", "%d", mncc->calling.present);
639                 add_trace("calling", "screen", "%d", mncc->calling.screen);
640                 add_trace("calling", "number", "%s", mncc->calling.number);
641         }
642         /* dialing information */
643         mncc->fields |= MNCC_F_CALLED;
644         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
645                 SCPY(mncc->imsi, p_dialinginfo.id+5);
646                 add_trace("dialing", "imsi", "%s", mncc->imsi);
647         } else {
648                 SCPY(mncc->called.number, p_dialinginfo.id);
649                 add_trace("dialing", "number", "%s", mncc->called.number);
650         }
651         
652         /* sending user-user */
653
654         /* redirecting number */
655         mncc->fields |= MNCC_F_REDIRECTING;
656         mncc->redirecting.plan = 1;
657         switch (p_redirinfo.ntype) {
658                 case INFO_NTYPE_UNKNOWN:
659                 mncc->redirecting.type = 0x0;
660                 break;
661                 case INFO_NTYPE_INTERNATIONAL:
662                 mncc->redirecting.type = 0x1;
663                 break;
664                 case INFO_NTYPE_NATIONAL:
665                 mncc->redirecting.type = 0x2;
666                 break;
667                 case INFO_NTYPE_SUBSCRIBER:
668                 mncc->redirecting.type = 0x4;
669                 break;
670                 default: /* INFO_NTYPE_NOTPRESENT */
671                 mncc->fields &= ~MNCC_F_REDIRECTING;
672                 break;
673         }
674         switch (p_redirinfo.screen) {
675                 case INFO_SCREEN_USER:
676                 mncc->redirecting.screen = 0;
677                 break;
678                 default: /* INFO_SCREE_NETWORK */
679                 mncc->redirecting.screen = 3;
680                 break;
681         }
682         switch (p_redirinfo.present) {
683                 case INFO_PRESENT_ALLOWED:
684                 mncc->redirecting.present = 0;
685                 break;
686                 case INFO_PRESENT_RESTRICTED:
687                 mncc->redirecting.present = 1;
688                 break;
689                 default: /* INFO_PRESENT_NOTAVAIL */
690                 mncc->redirecting.present = 2;
691                 break;
692         }
693         /* sending redirecting number only in ntmode */
694         if (mncc->fields & MNCC_F_REDIRECTING) {
695                 SCPY(mncc->redirecting.number, p_redirinfo.id);
696                 add_trace("redir", "type", "%d", mncc->redirecting.type);
697                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
698                 add_trace("redir", "present", "%d", mncc->redirecting.present);
699                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
700                 add_trace("redir", "number", "%s", mncc->redirecting.number);
701         }
702         /* bearer capability */
703         //todo
704
705         end_trace();
706         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
707
708         new_state(PORT_STATE_OUT_SETUP);
709
710         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
711         message_put(message);
712
713         new_state(PORT_STATE_OUT_PROCEEDING);
714
715         /* RTP bridge */
716         if (param->setup.rtpinfo.port) {
717                 p_g_rtp_bridge = 1;
718                 p_g_rtp_ip_remote = param->setup.rtpinfo.ip;
719                 p_g_rtp_port_remote = param->setup.rtpinfo.port;
720         } else
721                 p_g_rtp_bridge = 0;
722 }
723
724 /*
725  * endpoint sends messages to the port
726  */
727 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
728 {
729         if (Pgsm::message_epoint(epoint_id, message_id, param))
730                 return(1);
731
732         switch(message_id) {
733                 case MESSAGE_SETUP: /* dial-out command received from epoint */
734                 if (p_state!=PORT_STATE_IDLE)
735                         break;
736                 message_setup(epoint_id, message_id, param);
737                 break;
738
739                 default:
740                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
741         }
742
743         return(1);
744 }
745
746 int gsm_bs_exit(int rc)
747 {
748         /* free gsm instance */
749         if (gsm_bs) {
750                 if (gsm_bs->mncc_lfd.fd > -1) {
751                         close(gsm_bs->mncc_lfd.fd);
752                         unregister_fd(&gsm_bs->mncc_lfd);
753                 }
754
755                 del_timer(&gsm_bs->socket_retry);
756                 free(gsm_bs);
757                 gsm_bs = NULL;
758         }
759
760
761         return(rc);
762 }
763
764 int gsm_bs_init(struct interface *interface)
765 {
766         /* create gsm instance */
767         gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
768
769         gsm_bs->interface = interface;
770         gsm_bs->type = LCR_GSM_TYPE_NETWORK;
771         gsm_bs->sun.sun_family = AF_UNIX;
772         SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
773
774         memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
775         add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
776
777         /* do the initial connect */
778         mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);
779
780         generate_dtmf();
781
782         return 0;
783 }