Forward DTMF as message directly from GSM BS to SIP.
[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
216         /* process given callref */
217         gsm_trace_header(p_g_interface_name, this, 0, DIRECTION_IN);
218         add_trace("callref", "new", "0x%x", callref);
219         if (p_g_callref) {
220                 /* release in case the ID is already in use */
221                 add_trace("error", NULL, "callref already in use");
222                 end_trace();
223                 mncc = create_mncc(MNCC_REJ_REQ, callref);
224                 gsm_trace_header(p_g_interface_name, this, MNCC_REJ_REQ, DIRECTION_OUT);
225                 mncc->fields |= MNCC_F_CAUSE;
226                 mncc->cause.coding = 3;
227                 mncc->cause.location = 1;
228                 mncc->cause.value = 47;
229                 add_trace("cause", "coding", "%d", mncc->cause.coding);
230                 add_trace("cause", "location", "%d", mncc->cause.location);
231                 add_trace("cause", "value", "%d", mncc->cause.value);
232                 add_trace("reason", NULL, "callref already in use");
233                 end_trace();
234                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
235                 new_state(PORT_STATE_RELEASE);
236                 trigger_work(&p_g_delete);
237                 return;
238         }
239         p_g_callref = callref;
240         end_trace();
241
242         /* caller info */
243         if (mncc->clir.inv)
244                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
245         else
246                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
247         if (mncc->calling.number[0])
248                 SCPY(p_callerinfo.id, mncc->calling.number);
249         else
250                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
251         SCPY(p_callerinfo.imsi, mncc->imsi);
252         p_callerinfo.screen = INFO_SCREEN_NETWORK;
253         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
254         SCPY(p_callerinfo.interface, p_g_interface_name);
255
256         /* dialing information */
257         SCAT(p_dialinginfo.id, mncc->called.number);
258         switch (mncc->called.type) {
259                 case 0x1:
260                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
261                 break;
262                 case 0x2:
263                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
264                 break;
265                 case 0x4:
266                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
267                 break;
268                 default:
269                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
270                 break;
271         }
272         if (mncc->emergency) {
273                 SCPY(p_dialinginfo.id, "emergency");
274         }
275         p_dialinginfo.sending_complete = 1;
276
277         /* bearer capability */
278         // todo
279         p_capainfo.bearer_capa = INFO_BC_SPEECH;
280         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
281         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
282         p_capainfo.source_mode = B_MODE_TRANSPARENT;
283         p_g_mode = p_capainfo.source_mode;
284
285         /* useruser */
286
287         /* what infos did we got ... */
288         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
289         if (p_callerinfo.id[0])
290                 add_trace("calling", "number", "%s", p_callerinfo.id);
291         else
292                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
293         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
294         add_trace("dialing", "number", "%s", p_dialinginfo.id);
295         end_trace();
296
297         /* create endpoint */
298         if (p_epointlist)
299                 FATAL("Incoming call but already got an endpoint.\n");
300         if (!(epoint = new Endpoint(p_serial, 0)))
301                 FATAL("No memory for Endpoint instance\n");
302         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
303                 FATAL("No memory for Endpoint Application instance\n");
304         epointlist_new(epoint->ep_serial);
305
306         /* modify lchan to GSM codec V1 */
307         gsm_trace_header(p_g_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
308         mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
309         mode->lchan_mode = 0x01; /* GSM V1 */
310         mode->lchan_type = 0x02;
311         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
312         end_trace();
313         send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
314
315         /* send call proceeding */
316         gsm_trace_header(p_g_interface_name, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
317         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_g_callref);
318         if (p_g_tones) {
319                 proceeding->fields |= MNCC_F_PROGRESS;
320                 proceeding->progress.coding = 3; /* GSM */
321                 proceeding->progress.location = 1;
322                 proceeding->progress.descr = 8;
323                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
324                 add_trace("progress", "location", "%d", proceeding->progress.location);
325                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
326         }
327         end_trace();
328         send_and_free_mncc(p_g_lcr_gsm, proceeding->msg_type, proceeding);
329
330         new_state(PORT_STATE_IN_PROCEEDING);
331
332         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
333                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
334                 end_trace();
335                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
336                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
337                 p_g_tch_connected = 1;
338         }
339
340         /* send setup message to endpoit */
341         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
342         message->param.setup.port_type = p_type;
343 //      message->param.setup.dtmf = 0;
344         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
345         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
346         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
347         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
348         message->param.setup.useruser.len = strlen(mncc->useruser.info);
349         message->param.setup.useruser.protocol = mncc->useruser.proto;
350         message->param.setup.rtpinfo.payload_type = 3; /* FIXME: receive payload type from peer */
351
352         if (p_g_rtp_bridge) {
353                 struct gsm_mncc_rtp *rtp;
354
355                 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding setup\n");
356                 p_g_setup_pending = message;
357                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
358                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
359         } else
360                 message_put(message);
361
362 }
363
364 /*
365  * BSC sends message to port
366  */
367 int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
368 {
369         struct interface *interface = lcr_gsm->interface;
370         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
371         unsigned int callref = mncc->callref;
372         class Port *port;
373         class Pgsm_bs *pgsm_bs = NULL;
374         char name[64];
375 //      struct mISDNport *mISDNport;
376
377         /* Special messages */
378         switch(msg_type) {
379         }
380
381         /* find callref */
382         callref = mncc->callref;
383         port = port_first;
384         while(port) {
385                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
386                         pgsm_bs = (class Pgsm_bs *)port;
387                         if (pgsm_bs->p_g_callref == callref) {
388                                 break;
389                         }
390                 }
391                 port = port->next;
392         }
393
394         if (msg_type == GSM_TCHF_FRAME) {
395                 if (port) {
396                         /* inject DTMF, if enabled */
397                         if (pgsm_bs->p_g_dtmf) {
398                                 unsigned char data[160];
399                                 int i;
400
401                                 for (i = 0; i < 160; i++) {
402                                         data[i] = pgsm_bs->p_g_dtmf[pgsm_bs->p_g_dtmf_index++];
403                                         if (pgsm_bs->p_g_dtmf_index == 8000)
404                                                 pgsm_bs->p_g_dtmf_index = 0;
405                                 }
406                                 /* send */
407                                 pgsm_bs->bridge_tx(data, 160);
408                         } else
409                                 pgsm_bs->frame_receive(arg);
410                         /* if we do not bridge we need to inject audio, if available */
411                         if (!pgsm_bs->p_bridge) {
412                                 unsigned char data[160];
413                                 int i;
414
415                                 i = pgsm_bs->read_audio(data, 160);
416                                 if (i)
417                                         pgsm_bs->bridge_rx(data, i);
418                         }
419                 }
420                 return 0;
421         }
422
423         if (!port) {
424                 if (msg_type != MNCC_SETUP_IND)
425                         return(0);
426 #if 0
427                 /* find gsm port */
428                 mISDNport = mISDNport_first;
429                 while(mISDNport) {
430                         if (mISDNport->gsm_bs)
431                                 break;
432                         mISDNport = mISDNport->next;
433                 }
434                 if (!mISDNport) {
435                         struct gsm_mncc *rej;
436
437                         rej = create_mncc(MNCC_REJ_REQ, callref);
438                         rej->fields |= MNCC_F_CAUSE;
439                         rej->cause.coding = 3;
440                         rej->cause.location = 1;
441                         rej->cause.value = 27;
442                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
443                         add_trace("cause", "coding", "%d", rej->cause.coding);
444                         add_trace("cause", "location", "%d", rej->cause.location);
445                         add_trace("cause", "value", "%d", rej->cause.value);
446                         end_trace();
447                         send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
448                         return 0;
449                 }
450 #endif
451                 /* creating port object, transparent until setup with hdlc */
452                 SPRINT(name, "%s-%d-in", interface->name, 0);
453                 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, name, NULL, interface)))
454                         FATAL("Cannot create Port instance.\n");
455         }
456
457         switch(msg_type) {
458                 case MNCC_SETUP_IND:
459                 pgsm_bs->setup_ind(msg_type, callref, mncc);
460                 break;
461
462                 case MNCC_RTP_CREATE:
463                 pgsm_bs->rtp_create_ind(msg_type, callref, mncc);
464                 break;
465
466                 case MNCC_RTP_CONNECT:
467                 pgsm_bs->rtp_connect_ind(msg_type, callref, mncc);
468                 break;
469
470                 case MNCC_START_DTMF_IND:
471                 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
472                 break;
473
474                 case MNCC_STOP_DTMF_IND:
475                 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
476                 break;
477
478                 case MNCC_CALL_CONF_IND:
479                 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
480                 break;
481
482                 case MNCC_ALERT_IND:
483                 pgsm_bs->alert_ind(msg_type, callref, mncc);
484                 break;
485
486                 case MNCC_SETUP_CNF:
487                 pgsm_bs->setup_cnf(msg_type, callref, mncc);
488                 break;
489
490                 case MNCC_SETUP_COMPL_IND:
491                 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
492                 break;
493
494                 case MNCC_DISC_IND:
495                 pgsm_bs->disc_ind(msg_type, callref, mncc);
496                 break;
497
498                 case MNCC_REL_IND:
499                 case MNCC_REL_CNF:
500                 case MNCC_REJ_IND:
501                 pgsm_bs->rel_ind(msg_type, callref, mncc);
502                 break;
503
504                 case MNCC_NOTIFY_IND:
505                 pgsm_bs->notify_ind(msg_type, callref, mncc);
506                 break;
507
508                 case MNCC_HOLD_IND:
509                 pgsm_bs->hold_ind(msg_type, callref, mncc);
510                 break;
511
512                 case MNCC_RETRIEVE_IND:
513                 pgsm_bs->retr_ind(msg_type, callref, mncc);
514                 break;
515
516                 default:
517                 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);
518         }
519         return(0);
520 }
521
522 /* MESSAGE_SETUP */
523 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
524 {
525         struct lcr_msg *message;
526         struct epoint_list *epointlist;
527         struct gsm_mncc *mncc;
528
529         /* copy setup infos to port */
530         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
531         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
532         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
533         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
534
535         /* no GSM MNCC connection */
536         if (p_g_lcr_gsm->mncc_lfd.fd < 0) {
537                 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
538                 add_trace("failure", NULL, "No MNCC connection.");
539                 end_trace();
540                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
541                 message->param.disconnectinfo.cause = 41; // temp. failure.
542                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
543                 message_put(message);
544                 new_state(PORT_STATE_RELEASE);
545                 trigger_work(&p_g_delete);
546                 return;
547         }
548
549         /* no number */
550         if (!p_dialinginfo.id[0]) {
551                 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
552                 add_trace("failure", NULL, "No dialed subscriber given.");
553                 end_trace();
554                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
555                 message->param.disconnectinfo.cause = 28;
556                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
557                 message_put(message);
558                 new_state(PORT_STATE_RELEASE);
559                 trigger_work(&p_g_delete);
560                 return;
561         }
562
563 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
564         /* screen outgoing caller id */
565         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_g_interface_name);
566
567         /* attach only if not already */
568         epointlist = p_epointlist;
569         while(epointlist) {
570                 if (epointlist->epoint_id == epoint_id)
571                         break;
572                 epointlist = epointlist->next;
573         }
574         if (!epointlist)
575                 epointlist_new(epoint_id);
576
577         /* creating l3id */
578         gsm_trace_header(p_g_interface_name, this, 0, DIRECTION_OUT);
579         p_g_callref = new_callref++;
580         add_trace("callref", "new", "0x%x", p_g_callref);
581         end_trace();
582
583         gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
584         mncc = create_mncc(MNCC_SETUP_REQ, p_g_callref);
585         /* caller information */
586         mncc->fields |= MNCC_F_CALLING;
587         mncc->calling.plan = 1;
588         switch (p_callerinfo.ntype) {
589                 case INFO_NTYPE_UNKNOWN:
590                 mncc->calling.type = 0x0;
591                 break;
592                 case INFO_NTYPE_INTERNATIONAL:
593                 mncc->calling.type = 0x1;
594                 break;
595                 case INFO_NTYPE_NATIONAL:
596                 mncc->calling.type = 0x2;
597                 break;
598                 case INFO_NTYPE_SUBSCRIBER:
599                 mncc->calling.type = 0x4;
600                 break;
601                 default: /* INFO_NTYPE_NOTPRESENT */
602                 mncc->fields &= ~MNCC_F_CALLING;
603                 break;
604         }
605         switch (p_callerinfo.screen) {
606                 case INFO_SCREEN_USER:
607                 mncc->calling.screen = 0;
608                 break;
609                 default: /* INFO_SCREEN_NETWORK */
610                 mncc->calling.screen = 3;
611                 break;
612         }
613         switch (p_callerinfo.present) {
614                 case INFO_PRESENT_ALLOWED:
615                 mncc->calling.present = 0;
616                 break;
617                 case INFO_PRESENT_RESTRICTED:
618                 mncc->calling.present = 1;
619                 break;
620                 default: /* INFO_PRESENT_NOTAVAIL */
621                 mncc->calling.present = 2;
622                 break;
623         }
624         if (mncc->fields & MNCC_F_CALLING) {
625                 SCPY(mncc->calling.number, p_callerinfo.id);
626                 add_trace("calling", "type", "%d", mncc->calling.type);
627                 add_trace("calling", "plan", "%d", mncc->calling.plan);
628                 add_trace("calling", "present", "%d", mncc->calling.present);
629                 add_trace("calling", "screen", "%d", mncc->calling.screen);
630                 add_trace("calling", "number", "%s", mncc->calling.number);
631         }
632         /* dialing information */
633         mncc->fields |= MNCC_F_CALLED;
634         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
635                 SCPY(mncc->imsi, p_dialinginfo.id+5);
636                 add_trace("dialing", "imsi", "%s", mncc->imsi);
637         } else {
638                 SCPY(mncc->called.number, p_dialinginfo.id);
639                 add_trace("dialing", "number", "%s", mncc->called.number);
640         }
641         
642         /* sending user-user */
643
644         /* redirecting number */
645         mncc->fields |= MNCC_F_REDIRECTING;
646         mncc->redirecting.plan = 1;
647         switch (p_redirinfo.ntype) {
648                 case INFO_NTYPE_UNKNOWN:
649                 mncc->redirecting.type = 0x0;
650                 break;
651                 case INFO_NTYPE_INTERNATIONAL:
652                 mncc->redirecting.type = 0x1;
653                 break;
654                 case INFO_NTYPE_NATIONAL:
655                 mncc->redirecting.type = 0x2;
656                 break;
657                 case INFO_NTYPE_SUBSCRIBER:
658                 mncc->redirecting.type = 0x4;
659                 break;
660                 default: /* INFO_NTYPE_NOTPRESENT */
661                 mncc->fields &= ~MNCC_F_REDIRECTING;
662                 break;
663         }
664         switch (p_redirinfo.screen) {
665                 case INFO_SCREEN_USER:
666                 mncc->redirecting.screen = 0;
667                 break;
668                 default: /* INFO_SCREE_NETWORK */
669                 mncc->redirecting.screen = 3;
670                 break;
671         }
672         switch (p_redirinfo.present) {
673                 case INFO_PRESENT_ALLOWED:
674                 mncc->redirecting.present = 0;
675                 break;
676                 case INFO_PRESENT_RESTRICTED:
677                 mncc->redirecting.present = 1;
678                 break;
679                 default: /* INFO_PRESENT_NOTAVAIL */
680                 mncc->redirecting.present = 2;
681                 break;
682         }
683         /* sending redirecting number only in ntmode */
684         if (mncc->fields & MNCC_F_REDIRECTING) {
685                 SCPY(mncc->redirecting.number, p_redirinfo.id);
686                 add_trace("redir", "type", "%d", mncc->redirecting.type);
687                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
688                 add_trace("redir", "present", "%d", mncc->redirecting.present);
689                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
690                 add_trace("redir", "number", "%s", mncc->redirecting.number);
691         }
692         /* bearer capability */
693         //todo
694
695         end_trace();
696         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
697
698         new_state(PORT_STATE_OUT_SETUP);
699
700         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
701         message_put(message);
702
703         new_state(PORT_STATE_OUT_PROCEEDING);
704
705         /* RTP bridge */
706         if (param->setup.rtpinfo.port) {
707                 p_g_rtp_bridge = 1;
708                 p_g_rtp_ip_remote = param->setup.rtpinfo.ip;
709                 p_g_rtp_port_remote = param->setup.rtpinfo.port;
710         } else
711                 p_g_rtp_bridge = 0;
712 }
713
714 /*
715  * endpoint sends messages to the port
716  */
717 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
718 {
719         if (Pgsm::message_epoint(epoint_id, message_id, param))
720                 return(1);
721
722         switch(message_id) {
723                 case MESSAGE_SETUP: /* dial-out command received from epoint */
724                 if (p_state!=PORT_STATE_IDLE)
725                         break;
726                 message_setup(epoint_id, message_id, param);
727                 break;
728
729                 default:
730                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
731         }
732
733         return(1);
734 }
735
736 int gsm_bs_exit(int rc)
737 {
738         /* free gsm instance */
739         if (gsm_bs) {
740                 if (gsm_bs->mncc_lfd.fd > -1) {
741                         close(gsm_bs->mncc_lfd.fd);
742                         unregister_fd(&gsm_bs->mncc_lfd);
743                 }
744
745                 del_timer(&gsm_bs->socket_retry);
746                 free(gsm_bs);
747                 gsm_bs = NULL;
748         }
749
750
751         return(rc);
752 }
753
754 int gsm_bs_init(struct interface *interface)
755 {
756         /* create gsm instance */
757         gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
758
759         gsm_bs->interface = interface;
760         gsm_bs->type = LCR_GSM_TYPE_NETWORK;
761         gsm_bs->sun.sun_family = AF_UNIX;
762         SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
763
764         memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
765         add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
766
767         /* do the initial connect */
768         mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);
769
770         generate_dtmf();
771
772         return 0;
773 }