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