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