GSM now receives tones during bridge
[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          || msg_type == GSM_TCHF_BAD_FRAME) {
401                 if (port) {
402                         /* inject DTMF, if enabled */
403                         if (pgsm_bs->p_g_dtmf) {
404                                 unsigned char data[160];
405                                 int i;
406
407                                 for (i = 0; i < 160; i++) {
408                                         data[i] = pgsm_bs->p_g_dtmf[pgsm_bs->p_g_dtmf_index++];
409                                         if (pgsm_bs->p_g_dtmf_index == 8000)
410                                                 pgsm_bs->p_g_dtmf_index = 0;
411                                 }
412                                 /* send */
413                                 pgsm_bs->bridge_tx(data, 160);
414                         } else
415                                 pgsm_bs->frame_receive(arg);
416                         /* if we do not bridge we need to inject audio, if available */
417                         if (!pgsm_bs->p_bridge || pgsm_bs->p_tone_name[0]) {
418                                 unsigned char data[160];
419                                 int i;
420
421                                 i = pgsm_bs->read_audio(data, 160);
422                                 if (i)
423                                         pgsm_bs->audio_send(data, i);
424                         }
425                 }
426                 return 0;
427         }
428
429         if (!port) {
430                 struct interface *interface;
431
432                 if (msg_type != MNCC_SETUP_IND)
433                         return(0);
434
435                 interface = getinterfacebyname(lcr_gsm->interface_name);
436                 if (!interface) {
437                         struct gsm_mncc *rej;
438
439                         rej = create_mncc(MNCC_REJ_REQ, callref);
440                         rej->fields |= MNCC_F_CAUSE;
441                         rej->cause.coding = 3;
442                         rej->cause.location = 1;
443                         rej->cause.value = 27;
444                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
445                         add_trace("cause", "coding", "%d", rej->cause.coding);
446                         add_trace("cause", "location", "%d", rej->cause.location);
447                         add_trace("cause", "value", "%d", rej->cause.value);
448                         add_trace("reason", NULL, "interface %s not found", lcr_gsm->interface_name);
449                         end_trace();
450                         send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
451                         return 0;
452                 }
453                 /* creating port object, transparent until setup with hdlc */
454                 SPRINT(name, "%s-%d-in", interface->name, 0);
455                 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, name, NULL, interface)))
456                         FATAL("Cannot create Port instance.\n");
457         }
458
459         switch(msg_type) {
460                 case MNCC_SETUP_IND:
461                 pgsm_bs->setup_ind(msg_type, callref, mncc);
462                 break;
463
464                 case MNCC_RTP_CREATE:
465                 pgsm_bs->rtp_create_ind(msg_type, callref, mncc);
466                 break;
467
468                 case MNCC_RTP_CONNECT:
469                 pgsm_bs->rtp_connect_ind(msg_type, callref, mncc);
470                 break;
471
472                 case MNCC_START_DTMF_IND:
473                 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
474                 break;
475
476                 case MNCC_STOP_DTMF_IND:
477                 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
478                 break;
479
480                 case MNCC_CALL_CONF_IND:
481                 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
482                 break;
483
484                 case MNCC_ALERT_IND:
485                 pgsm_bs->alert_ind(msg_type, callref, mncc);
486                 break;
487
488                 case MNCC_SETUP_CNF:
489                 pgsm_bs->setup_cnf(msg_type, callref, mncc);
490                 break;
491
492                 case MNCC_SETUP_COMPL_IND:
493                 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
494                 break;
495
496                 case MNCC_DISC_IND:
497                 pgsm_bs->disc_ind(msg_type, callref, mncc);
498                 break;
499
500                 case MNCC_REL_IND:
501                 case MNCC_REL_CNF:
502                 case MNCC_REJ_IND:
503                 pgsm_bs->rel_ind(msg_type, callref, mncc);
504                 break;
505
506                 case MNCC_NOTIFY_IND:
507                 pgsm_bs->notify_ind(msg_type, callref, mncc);
508                 break;
509
510                 case MNCC_HOLD_IND:
511                 pgsm_bs->hold_ind(msg_type, callref, mncc);
512                 break;
513
514                 case MNCC_RETRIEVE_IND:
515                 pgsm_bs->retr_ind(msg_type, callref, mncc);
516                 break;
517
518                 default:
519                 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);
520         }
521         return(0);
522 }
523
524 /* MESSAGE_SETUP */
525 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
526 {
527         struct lcr_msg *message;
528         struct epoint_list *epointlist;
529         struct gsm_mncc *mncc;
530
531         /* copy setup infos to port */
532         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
533         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
534         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
535         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
536
537         /* no GSM MNCC connection */
538         if (p_g_lcr_gsm->mncc_lfd.fd < 0) {
539                 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
540                 add_trace("failure", NULL, "No MNCC connection.");
541                 end_trace();
542                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
543                 message->param.disconnectinfo.cause = 41; // temp. failure.
544                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
545                 message_put(message);
546                 new_state(PORT_STATE_RELEASE);
547                 trigger_work(&p_g_delete);
548                 return;
549         }
550
551         /* no number */
552         if (!p_dialinginfo.id[0]) {
553                 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
554                 add_trace("failure", NULL, "No dialed subscriber given.");
555                 end_trace();
556                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
557                 message->param.disconnectinfo.cause = 28;
558                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
559                 message_put(message);
560                 new_state(PORT_STATE_RELEASE);
561                 trigger_work(&p_g_delete);
562                 return;
563         }
564
565 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
566         /* screen outgoing caller id */
567         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_g_interface_name);
568
569         /* attach only if not already */
570         epointlist = p_epointlist;
571         while(epointlist) {
572                 if (epointlist->epoint_id == epoint_id)
573                         break;
574                 epointlist = epointlist->next;
575         }
576         if (!epointlist)
577                 epointlist_new(epoint_id);
578
579         /* creating l3id */
580         gsm_trace_header(p_g_interface_name, this, 0, DIRECTION_OUT);
581         p_g_callref = new_callref++;
582         add_trace("callref", "new", "0x%x", p_g_callref);
583         end_trace();
584
585         gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
586         mncc = create_mncc(MNCC_SETUP_REQ, p_g_callref);
587         /* caller information */
588         mncc->fields |= MNCC_F_CALLING;
589         mncc->calling.plan = 1;
590         switch (p_callerinfo.ntype) {
591                 case INFO_NTYPE_UNKNOWN:
592                 mncc->calling.type = 0x0;
593                 break;
594                 case INFO_NTYPE_INTERNATIONAL:
595                 mncc->calling.type = 0x1;
596                 break;
597                 case INFO_NTYPE_NATIONAL:
598                 mncc->calling.type = 0x2;
599                 break;
600                 case INFO_NTYPE_SUBSCRIBER:
601                 mncc->calling.type = 0x4;
602                 break;
603                 default: /* INFO_NTYPE_NOTPRESENT */
604                 mncc->fields &= ~MNCC_F_CALLING;
605                 break;
606         }
607         switch (p_callerinfo.screen) {
608                 case INFO_SCREEN_USER:
609                 mncc->calling.screen = 0;
610                 break;
611                 default: /* INFO_SCREEN_NETWORK */
612                 mncc->calling.screen = 3;
613                 break;
614         }
615         switch (p_callerinfo.present) {
616                 case INFO_PRESENT_ALLOWED:
617                 mncc->calling.present = 0;
618                 break;
619                 case INFO_PRESENT_RESTRICTED:
620                 mncc->calling.present = 1;
621                 break;
622                 default: /* INFO_PRESENT_NOTAVAIL */
623                 mncc->calling.present = 2;
624                 break;
625         }
626         if (mncc->fields & MNCC_F_CALLING) {
627                 SCPY(mncc->calling.number, p_callerinfo.id);
628                 add_trace("calling", "type", "%d", mncc->calling.type);
629                 add_trace("calling", "plan", "%d", mncc->calling.plan);
630                 add_trace("calling", "present", "%d", mncc->calling.present);
631                 add_trace("calling", "screen", "%d", mncc->calling.screen);
632                 add_trace("calling", "number", "%s", mncc->calling.number);
633         }
634         /* dialing information */
635         mncc->fields |= MNCC_F_CALLED;
636         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
637                 SCPY(mncc->imsi, p_dialinginfo.id+5);
638                 add_trace("dialing", "imsi", "%s", mncc->imsi);
639         } else {
640                 SCPY(mncc->called.number, p_dialinginfo.id);
641                 add_trace("dialing", "number", "%s", mncc->called.number);
642         }
643         
644         /* sending user-user */
645
646         /* redirecting number */
647         mncc->fields |= MNCC_F_REDIRECTING;
648         mncc->redirecting.plan = 1;
649         switch (p_redirinfo.ntype) {
650                 case INFO_NTYPE_UNKNOWN:
651                 mncc->redirecting.type = 0x0;
652                 break;
653                 case INFO_NTYPE_INTERNATIONAL:
654                 mncc->redirecting.type = 0x1;
655                 break;
656                 case INFO_NTYPE_NATIONAL:
657                 mncc->redirecting.type = 0x2;
658                 break;
659                 case INFO_NTYPE_SUBSCRIBER:
660                 mncc->redirecting.type = 0x4;
661                 break;
662                 default: /* INFO_NTYPE_NOTPRESENT */
663                 mncc->fields &= ~MNCC_F_REDIRECTING;
664                 break;
665         }
666         switch (p_redirinfo.screen) {
667                 case INFO_SCREEN_USER:
668                 mncc->redirecting.screen = 0;
669                 break;
670                 default: /* INFO_SCREE_NETWORK */
671                 mncc->redirecting.screen = 3;
672                 break;
673         }
674         switch (p_redirinfo.present) {
675                 case INFO_PRESENT_ALLOWED:
676                 mncc->redirecting.present = 0;
677                 break;
678                 case INFO_PRESENT_RESTRICTED:
679                 mncc->redirecting.present = 1;
680                 break;
681                 default: /* INFO_PRESENT_NOTAVAIL */
682                 mncc->redirecting.present = 2;
683                 break;
684         }
685         /* sending redirecting number only in ntmode */
686         if (mncc->fields & MNCC_F_REDIRECTING) {
687                 SCPY(mncc->redirecting.number, p_redirinfo.id);
688                 add_trace("redir", "type", "%d", mncc->redirecting.type);
689                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
690                 add_trace("redir", "present", "%d", mncc->redirecting.present);
691                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
692                 add_trace("redir", "number", "%s", mncc->redirecting.number);
693         }
694         /* bearer capability */
695         //todo
696
697         end_trace();
698         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
699
700         new_state(PORT_STATE_OUT_SETUP);
701
702         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
703         message_put(message);
704
705         new_state(PORT_STATE_OUT_PROCEEDING);
706
707         /* RTP bridge */
708         if (param->setup.rtpinfo.port) {
709                 p_g_rtp_bridge = 1;
710                 p_g_rtp_ip_remote = param->setup.rtpinfo.ip;
711                 p_g_rtp_port_remote = param->setup.rtpinfo.port;
712         } else
713                 p_g_rtp_bridge = 0;
714 }
715
716 /*
717  * endpoint sends messages to the port
718  */
719 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
720 {
721         if (Pgsm::message_epoint(epoint_id, message_id, param))
722                 return(1);
723
724         switch(message_id) {
725                 case MESSAGE_SETUP: /* dial-out command received from epoint */
726                 if (p_state!=PORT_STATE_IDLE)
727                         break;
728                 message_setup(epoint_id, message_id, param);
729                 break;
730
731                 default:
732                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
733         }
734
735         return(1);
736 }
737
738 int gsm_bs_exit(int rc)
739 {
740         /* free gsm instance */
741         if (gsm_bs) {
742                 if (gsm_bs->mncc_lfd.fd > -1) {
743                         close(gsm_bs->mncc_lfd.fd);
744                         unregister_fd(&gsm_bs->mncc_lfd);
745                 }
746
747                 del_timer(&gsm_bs->socket_retry);
748                 free(gsm_bs);
749                 gsm_bs = NULL;
750         }
751
752
753         return(rc);
754 }
755
756 int gsm_bs_init(struct interface *interface)
757 {
758         /* create gsm instance */
759         gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
760
761         SCPY(gsm_bs->interface_name, interface->name);
762         gsm_bs->type = LCR_GSM_TYPE_NETWORK;
763         gsm_bs->sun.sun_family = AF_UNIX;
764         SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
765
766         memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
767         add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
768
769         /* do the initial connect */
770         mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);
771
772         generate_dtmf();
773
774         return 0;
775 }