Merge branch 'develop'
[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 "config.h"
14
15 #ifndef _GNU_SOURCE
16 #define _GNU_SOURCE
17 #endif
18 extern "C" {
19 #include <assert.h>
20 #include <getopt.h>
21
22 #include <sys/socket.h>
23 #include <sys/un.h>
24
25 #include <openbsc/mncc.h>
26 #include <openbsc/trau_frame.h>
27 }
28
29 #define SOCKET_RETRY_TIMER      5
30
31 /*
32  * DTMF stuff
33  */
34 unsigned char dtmf_samples[16][8000];
35 static int dtmf_x[4] = { 1209, 1336, 1477, 1633 };
36 static int dtmf_y[4] = { 697, 770, 852, 941 };
37
38 void generate_dtmf(void)
39 {
40         double fx, fy, sample;
41         int i, x, y;
42         unsigned char *law;
43
44         for (y = 0; y < 4; y++) {
45                 fy = 2 * 3.1415927 * ((double)dtmf_y[y]) / 8000.0;
46                 for (x = 0; x < 4; x++) {
47                         fx = 2 * 3.1415927 * ((double)dtmf_x[x]) / 8000.0;
48                         law = dtmf_samples[y << 2 | x];
49                         for (i = 0; i < 8000; i++) {
50                                 sample = sin(fy * ((double)i)) * 0.251 * 32767.0; /* -6 dB */
51                                 sample += sin(fx * ((double)i)) * 0.158 * 32767.0; /* -8 dB */
52                                 *law++ = audio_s16_to_law[(int)sample & 0xffff];
53                         }
54                 }
55         }
56 }
57
58
59 /*
60  * DTMF stuff
61  */
62 unsigned char dtmf_samples[16][8000];
63 static int dtmf_x[4] = { 1209, 1336, 1477, 1633 };
64 static int dtmf_y[4] = { 697, 770, 852, 941 };
65
66 void generate_dtmf(void)
67 {
68         double fx, fy, sample;
69         int i, x, y;
70         unsigned char *law;
71
72         for (y = 0; y < 4; y++) {
73                 fy = 2 * 3.1415927 * ((double)dtmf_y[y]) / 8000.0;
74                 for (x = 0; x < 4; x++) {
75                         fx = 2 * 3.1415927 * ((double)dtmf_x[x]) / 8000.0;
76                         law = dtmf_samples[y << 2 | x];
77                         for (i = 0; i < 8000; i++) {
78                                 sample = sin(fy * ((double)i)) * 0.251 * 32767.0; /* -6 dB */
79                                 sample += sin(fx * ((double)i)) * 0.158 * 32767.0; /* -8 dB */
80                                 *law++ = audio_s16_to_law[(int)sample & 0xffff];
81                         }
82                 }
83         }
84 }
85
86
87 /*
88  * constructor
89  */
90 Pgsm_bs::Pgsm_bs(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : Pgsm(type, mISDNport, portname, settings, channel, exclusive, mode)
91 {
92         p_m_g_instance = gsm->network;
93         p_m_g_dtmf = NULL;
94         p_m_g_dtmf_index = 0;
95
96         PDEBUG(DEBUG_GSM, "Created new GSMBSPort(%s).\n", portname);
97 }
98
99 /*
100  * destructor
101  */
102 Pgsm_bs::~Pgsm_bs()
103 {
104         PDEBUG(DEBUG_GSM, "Destroyed GSM BS process(%s).\n", p_name);
105 }
106
107 /* DTMF INDICATION */
108 void Pgsm_bs::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
109 {
110 //      struct lcr_msg *message;
111         struct gsm_mncc *resp;
112
113         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
114         add_trace("keypad", NULL, "%c", mncc->keypad);
115         end_trace();
116         SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
117         p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
118
119         /* send resp */
120         gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
121         add_trace("keypad", NULL, "%c", mncc->keypad);
122         end_trace();
123         resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
124         resp->fields |= MNCC_F_KEYPAD;
125         resp->keypad = mncc->keypad;
126         send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
127
128 #if 0
129         /* send dialing information */
130         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
131         memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
132         message_put(message);
133 #endif
134
135         /* generate DTMF tones */
136         switch (mncc->keypad) {
137                 case '1': p_m_g_dtmf = dtmf_samples[0]; break;
138                 case '2': p_m_g_dtmf = dtmf_samples[1]; break;
139                 case '3': p_m_g_dtmf = dtmf_samples[2]; break;
140                 case 'a':
141                 case 'A': p_m_g_dtmf = dtmf_samples[3]; break;
142                 case '4': p_m_g_dtmf = dtmf_samples[4]; break;
143                 case '5': p_m_g_dtmf = dtmf_samples[5]; break;
144                 case '6': p_m_g_dtmf = dtmf_samples[6]; break;
145                 case 'b':
146                 case 'B': p_m_g_dtmf = dtmf_samples[7]; break;
147                 case '7': p_m_g_dtmf = dtmf_samples[8]; break;
148                 case '8': p_m_g_dtmf = dtmf_samples[9]; break;
149                 case '9': p_m_g_dtmf = dtmf_samples[10]; break;
150                 case 'c':
151                 case 'C': p_m_g_dtmf = dtmf_samples[11]; break;
152                 case '*': p_m_g_dtmf = dtmf_samples[12]; break;
153                 case '0': p_m_g_dtmf = dtmf_samples[13]; break;
154                 case '#': p_m_g_dtmf = dtmf_samples[14]; break;
155                 case 'd':
156                 case 'D': p_m_g_dtmf = dtmf_samples[15]; break;
157         }
158         p_m_g_dtmf_index = 0;
159 }
160 void Pgsm_bs::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
161 {
162         struct gsm_mncc *resp;
163
164         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
165         add_trace("keypad", NULL, "%c", mncc->keypad);
166         end_trace();
167
168         /* send resp */
169         gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
170         add_trace("keypad", NULL, "%c", mncc->keypad);
171         end_trace();
172         resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
173         resp->keypad = mncc->keypad;
174         send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
175         
176         /* stop DTMF */
177         p_m_g_dtmf = NULL;
178 }
179
180 /* HOLD INDICATION */
181 void Pgsm_bs::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
182 {
183         struct lcr_msg *message;
184         struct gsm_mncc *resp, *frame;
185
186         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
187         end_trace();
188
189         /* notify the hold of call */
190         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
191         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
192         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
193         message_put(message);
194
195         /* acknowledge hold */
196         gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
197         end_trace();
198         resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
199         send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
200
201         /* disable audio */
202         if (p_m_g_tch_connected) { /* it should be true */
203                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
204                 end_trace();
205                 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
206                 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
207                 p_m_g_tch_connected = 0;
208         }
209 }
210
211
212 /* RETRIEVE INDICATION */
213 void Pgsm_bs::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
214 {
215         struct lcr_msg *message;
216         struct gsm_mncc *resp, *frame;
217
218         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
219         end_trace();
220
221         /* notify the retrieve of call */
222         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
223         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
224         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
225         message_put(message);
226
227         /* acknowledge retr */
228         gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
229         end_trace();
230         resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
231         send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
232
233         /* enable audio */
234         if (!p_m_g_tch_connected) { /* it should be true */
235                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
236                 end_trace();
237                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
238                 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
239                 p_m_g_tch_connected = 1;
240         }
241 }
242
243 /*
244  * handles all indications
245  */
246 /* SETUP INDICATION */
247 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
248 {
249         int ret;
250         class Endpoint *epoint;
251         struct lcr_msg *message;
252         int channel;
253         struct gsm_mncc *mode, *proceeding, *frame;
254
255         /* process given callref */
256         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
257         add_trace("callref", "new", "0x%x", callref);
258         if (p_m_g_callref) {
259                 /* release in case the ID is already in use */
260                 add_trace("error", NULL, "callref already in use");
261                 end_trace();
262                 mncc = create_mncc(MNCC_REJ_REQ, callref);
263                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
264                 mncc->fields |= MNCC_F_CAUSE;
265                 mncc->cause.coding = 3;
266                 mncc->cause.location = 1;
267                 mncc->cause.value = 47;
268                 add_trace("cause", "coding", "%d", mncc->cause.coding);
269                 add_trace("cause", "location", "%d", mncc->cause.location);
270                 add_trace("cause", "value", "%d", mncc->cause.value);
271                 add_trace("reason", NULL, "callref already in use");
272                 end_trace();
273                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
274                 new_state(PORT_STATE_RELEASE);
275                 trigger_work(&p_m_g_delete);
276                 return;
277         }
278         p_m_g_callref = callref;
279         end_trace();
280
281         /* if blocked, release call with MT_RELEASE_COMPLETE */
282         if (p_m_mISDNport->ifport->block) {
283                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
284                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
285                 mncc->fields |= MNCC_F_CAUSE;
286                 mncc->cause.coding = 3;
287                 mncc->cause.location = 1;
288                 mncc->cause.value = 27;
289                 add_trace("cause", "coding", "%d", mncc->cause.coding);
290                 add_trace("cause", "location", "%d", mncc->cause.location);
291                 add_trace("cause", "value", "%d", mncc->cause.value);
292                 add_trace("reason", NULL, "port is blocked");
293                 end_trace();
294                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
295                 new_state(PORT_STATE_RELEASE);
296                 trigger_work(&p_m_g_delete);
297                 return;
298         }
299
300         /* caller info */
301         if (mncc->clir.inv)
302                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
303         else
304                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
305         if (mncc->calling.number[0])
306                 SCPY(p_callerinfo.id, mncc->calling.number);
307         else
308                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
309         SCPY(p_callerinfo.imsi, mncc->imsi);
310         p_callerinfo.screen = INFO_SCREEN_NETWORK;
311         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
312         p_callerinfo.isdn_port = p_m_portnum;
313         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
314
315         /* dialing information */
316         SCAT(p_dialinginfo.id, mncc->called.number);
317         switch (mncc->called.type) {
318                 case 0x1:
319                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
320                 break;
321                 case 0x2:
322                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
323                 break;
324                 case 0x4:
325                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
326                 break;
327                 default:
328                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
329                 break;
330         }
331         if (mncc->emergency) {
332                 SCPY(p_dialinginfo.id, "emergency");
333         }
334         p_dialinginfo.sending_complete = 1;
335
336         /* bearer capability */
337         // todo
338         p_capainfo.bearer_capa = INFO_BC_SPEECH;
339         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
340         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
341         p_capainfo.source_mode = B_MODE_TRANSPARENT;
342         p_m_g_mode = p_capainfo.source_mode;
343
344         /* useruser */
345
346         /* hunt channel */
347         ret = channel = hunt_bchannel();
348         if (ret < 0)
349                 goto no_channel;
350
351         /* open channel */
352         ret = seize_bchannel(channel, 1);
353         if (ret < 0) {
354                 no_channel:
355                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
356                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
357                 mncc->fields |= MNCC_F_CAUSE;
358                 mncc->cause.coding = 3;
359                 mncc->cause.location = 1;
360                 mncc->cause.value = 34;
361                 add_trace("cause", "coding", "%d", mncc->cause.coding);
362                 add_trace("cause", "location", "%d", mncc->cause.location);
363                 add_trace("cause", "value", "%d", mncc->cause.value);
364                 add_trace("reason", NULL, "no channel");
365                 end_trace();
366                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
367                 new_state(PORT_STATE_RELEASE);
368                 trigger_work(&p_m_g_delete);
369                 return;
370         }
371         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
372         if (bchannel_open(p_m_b_index))
373                 goto no_channel;
374
375         /* what infos did we got ... */
376         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
377         if (p_callerinfo.id[0])
378                 add_trace("calling", "number", "%s", p_callerinfo.id);
379         else
380                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
381         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
382         add_trace("dialing", "number", "%s", p_dialinginfo.id);
383         end_trace();
384
385         /* create endpoint */
386         if (p_epointlist)
387                 FATAL("Incoming call but already got an endpoint.\n");
388         if (!(epoint = new Endpoint(p_serial, 0)))
389                 FATAL("No memory for Endpoint instance\n");
390         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
391                 FATAL("No memory for Endpoint Application instance\n");
392         epointlist_new(epoint->ep_serial);
393
394         /* modify lchan to GSM codec V1 */
395         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
396         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
397         mode->lchan_mode = 0x01; /* GSM V1 */
398         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
399         end_trace();
400         send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
401
402         /* send call proceeding */
403         gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
404         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
405         if (p_m_mISDNport->tones) {
406                 proceeding->fields |= MNCC_F_PROGRESS;
407                 proceeding->progress.coding = 3; /* GSM */
408                 proceeding->progress.location = 1;
409                 proceeding->progress.descr = 8;
410                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
411                 add_trace("progress", "location", "%d", proceeding->progress.location);
412                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
413         }
414         end_trace();
415         send_and_free_mncc(p_m_g_instance, proceeding->msg_type, proceeding);
416
417         new_state(PORT_STATE_IN_PROCEEDING);
418
419         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
420                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
421                 end_trace();
422                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
423                 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
424                 p_m_g_tch_connected = 1;
425         }
426
427         /* send setup message to endpoit */
428         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
429         message->param.setup.isdn_port = p_m_portnum;
430         message->param.setup.port_type = p_type;
431 //      message->param.setup.dtmf = 0;
432         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
433         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
434         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
435         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
436         message->param.setup.useruser.len = strlen(mncc->useruser.info);
437         message->param.setup.useruser.protocol = mncc->useruser.proto;
438         message_put(message);
439 }
440
441 /*
442  * BSC sends message to port
443  */
444 static int message_bsc(struct gsm_network *net, int msg_type, void *arg)
445 {
446         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
447         unsigned int callref = mncc->callref;
448         class Port *port;
449         class Pgsm_bs *pgsm_bs = NULL;
450         char name[64];
451         struct mISDNport *mISDNport;
452
453         /* Special messages */
454         switch(msg_type) {
455         }
456
457         /* find callref */
458         callref = mncc->callref;
459         port = port_first;
460         while(port) {
461                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
462                         pgsm_bs = (class Pgsm_bs *)port;
463                         if (pgsm_bs->p_m_g_callref == callref) {
464                                 break;
465                         }
466                 }
467                 port = port->next;
468         }
469
470         if (msg_type == GSM_TCHF_FRAME) {
471                 if (port) {
472                         /* inject DTMF, if enabled */
473                         if (pgsm_bs->p_m_g_dtmf) {
474                                 unsigned char data[160];
475                                 int i;
476
477                                 for (i = 0; i < 160; i++) {
478                                         data[i] = pgsm_bs->p_m_g_dtmf[pgsm_bs->p_m_g_dtmf_index++];
479                                         if (pgsm_bs->p_m_g_dtmf_index == 8000)
480                                                 pgsm_bs->p_m_g_dtmf_index = 0;
481                                 }
482                                 /* send */
483                                 pgsm_bs->bchannel_send(PH_DATA_REQ, 0, data, 160);
484                         } else
485                                 pgsm_bs->frame_receive(arg);
486                 }
487                 return 0;
488         }
489
490         if (!port) {
491                 if (msg_type != MNCC_SETUP_IND)
492                         return(0);
493                 /* find gsm port */
494                 mISDNport = mISDNport_first;
495                 while(mISDNport) {
496                         if (mISDNport->gsm_bs)
497                                 break;
498                         mISDNport = mISDNport->next;
499                 }
500                 if (!mISDNport) {
501                         struct gsm_mncc *rej;
502
503                         rej = create_mncc(MNCC_REJ_REQ, callref);
504                         rej->fields |= MNCC_F_CAUSE;
505                         rej->cause.coding = 3;
506                         rej->cause.location = 1;
507                         rej->cause.value = 27;
508                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
509                         add_trace("cause", "coding", "%d", rej->cause.coding);
510                         add_trace("cause", "location", "%d", rej->cause.location);
511                         add_trace("cause", "value", "%d", rej->cause.value);
512                         end_trace();
513                         send_and_free_mncc(gsm->network, rej->msg_type, rej);
514                         return 0;
515                 }
516                 /* creating port object, transparent until setup with hdlc */
517                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
518                 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
519
520                         FATAL("Cannot create Port instance.\n");
521         }
522
523         switch(msg_type) {
524                 case MNCC_SETUP_IND:
525                 pgsm_bs->setup_ind(msg_type, callref, mncc);
526                 break;
527
528                 case MNCC_START_DTMF_IND:
529                 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
530                 break;
531
532                 case MNCC_STOP_DTMF_IND:
533                 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
534                 break;
535
536                 case MNCC_CALL_CONF_IND:
537                 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
538                 break;
539
540                 case MNCC_ALERT_IND:
541                 pgsm_bs->alert_ind(msg_type, callref, mncc);
542                 break;
543
544                 case MNCC_SETUP_CNF:
545                 pgsm_bs->setup_cnf(msg_type, callref, mncc);
546                 break;
547
548                 case MNCC_SETUP_COMPL_IND:
549                 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
550                 break;
551
552                 case MNCC_DISC_IND:
553                 pgsm_bs->disc_ind(msg_type, callref, mncc);
554                 break;
555
556                 case MNCC_REL_IND:
557                 case MNCC_REL_CNF:
558                 case MNCC_REJ_IND:
559                 pgsm_bs->rel_ind(msg_type, callref, mncc);
560                 break;
561
562                 case MNCC_NOTIFY_IND:
563                 pgsm_bs->notify_ind(msg_type, callref, mncc);
564                 break;
565
566                 case MNCC_HOLD_IND:
567                 pgsm_bs->hold_ind(msg_type, callref, mncc);
568                 break;
569
570                 case MNCC_RETRIEVE_IND:
571                 pgsm_bs->retr_ind(msg_type, callref, mncc);
572                 break;
573
574                 default:
575                 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);
576         }
577         return(0);
578 }
579
580 /* MESSAGE_SETUP */
581 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
582 {
583         struct lcr_msg *message;
584         int ret;
585         struct epoint_list *epointlist;
586         struct gsm_mncc *mncc;
587         int channel;
588
589         /* copy setup infos to port */
590         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
591         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
592         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
593         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
594
595         /* no GSM MNCC connection */
596         if (gsm->mncc_lfd.fd < 0) {
597                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
598                 add_trace("failure", NULL, "No MNCC connection.");
599                 end_trace();
600                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
601                 message->param.disconnectinfo.cause = 27; // temp. unavail.
602                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
603                 message_put(message);
604                 new_state(PORT_STATE_RELEASE);
605                 trigger_work(&p_m_g_delete);
606                 return;
607         }
608
609         /* no number */
610         if (!p_dialinginfo.id[0]) {
611                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
612                 add_trace("failure", NULL, "No dialed subscriber given.");
613                 end_trace();
614                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
615                 message->param.disconnectinfo.cause = 28;
616                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
617                 message_put(message);
618                 new_state(PORT_STATE_RELEASE);
619                 trigger_work(&p_m_g_delete);
620                 return;
621         }
622         
623         /* release if port is blocked */
624         if (p_m_mISDNport->ifport->block) {
625                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
626                 add_trace("failure", NULL, "Port blocked.");
627                 end_trace();
628                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
629                 message->param.disconnectinfo.cause = 27; // temp. unavail.
630                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
631                 message_put(message);
632                 new_state(PORT_STATE_RELEASE);
633                 trigger_work(&p_m_g_delete);
634                 return;
635         }
636
637         /* hunt channel */
638         ret = channel = hunt_bchannel();
639         if (ret < 0)
640                 goto no_channel;
641         /* open channel */
642         ret = seize_bchannel(channel, 1);
643         if (ret < 0) {
644                 no_channel:
645                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
646                 add_trace("failure", NULL, "No internal audio channel available.");
647                 end_trace();
648                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
649                 message->param.disconnectinfo.cause = 34;
650                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
651                 message_put(message);
652                 new_state(PORT_STATE_RELEASE);
653                 trigger_work(&p_m_g_delete);
654                 return;
655         }
656         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
657         if (bchannel_open(p_m_b_index))
658                 goto no_channel;
659
660 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
661         /* screen outgoing caller id */
662         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
663
664         /* attach only if not already */
665         epointlist = p_epointlist;
666         while(epointlist) {
667                 if (epointlist->epoint_id == epoint_id)
668                         break;
669                 epointlist = epointlist->next;
670         }
671         if (!epointlist)
672                 epointlist_new(epoint_id);
673
674         /* creating l3id */
675         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
676         p_m_g_callref = new_callref++;
677         add_trace("callref", "new", "0x%x", p_m_g_callref);
678         end_trace();
679
680         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
681         mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
682         /* caller information */
683         mncc->fields |= MNCC_F_CALLING;
684         mncc->calling.plan = 1;
685         switch (p_callerinfo.ntype) {
686                 case INFO_NTYPE_UNKNOWN:
687                 mncc->calling.type = 0x0;
688                 break;
689                 case INFO_NTYPE_INTERNATIONAL:
690                 mncc->calling.type = 0x1;
691                 break;
692                 case INFO_NTYPE_NATIONAL:
693                 mncc->calling.type = 0x2;
694                 break;
695                 case INFO_NTYPE_SUBSCRIBER:
696                 mncc->calling.type = 0x4;
697                 break;
698                 default: /* INFO_NTYPE_NOTPRESENT */
699                 mncc->fields &= ~MNCC_F_CALLING;
700                 break;
701         }
702         switch (p_callerinfo.screen) {
703                 case INFO_SCREEN_USER:
704                 mncc->calling.screen = 0;
705                 break;
706                 default: /* INFO_SCREEN_NETWORK */
707                 mncc->calling.screen = 3;
708                 break;
709         }
710         switch (p_callerinfo.present) {
711                 case INFO_PRESENT_ALLOWED:
712                 mncc->calling.present = 0;
713                 break;
714                 case INFO_PRESENT_RESTRICTED:
715                 mncc->calling.present = 1;
716                 break;
717                 default: /* INFO_PRESENT_NOTAVAIL */
718                 mncc->calling.present = 2;
719                 break;
720         }
721         if (mncc->fields & MNCC_F_CALLING) {
722                 SCPY(mncc->calling.number, p_callerinfo.id);
723                 add_trace("calling", "type", "%d", mncc->calling.type);
724                 add_trace("calling", "plan", "%d", mncc->calling.plan);
725                 add_trace("calling", "present", "%d", mncc->calling.present);
726                 add_trace("calling", "screen", "%d", mncc->calling.screen);
727                 add_trace("calling", "number", "%s", mncc->calling.number);
728         }
729         /* dialing information */
730         mncc->fields |= MNCC_F_CALLED;
731         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
732                 SCPY(mncc->imsi, p_dialinginfo.id+5);
733                 add_trace("dialing", "imsi", "%s", mncc->imsi);
734         } else {
735                 SCPY(mncc->called.number, p_dialinginfo.id);
736                 add_trace("dialing", "number", "%s", mncc->called.number);
737         }
738         
739         /* sending user-user */
740
741         /* redirecting number */
742         mncc->fields |= MNCC_F_REDIRECTING;
743         mncc->redirecting.plan = 1;
744         switch (p_redirinfo.ntype) {
745                 case INFO_NTYPE_UNKNOWN:
746                 mncc->redirecting.type = 0x0;
747                 break;
748                 case INFO_NTYPE_INTERNATIONAL:
749                 mncc->redirecting.type = 0x1;
750                 break;
751                 case INFO_NTYPE_NATIONAL:
752                 mncc->redirecting.type = 0x2;
753                 break;
754                 case INFO_NTYPE_SUBSCRIBER:
755                 mncc->redirecting.type = 0x4;
756                 break;
757                 default: /* INFO_NTYPE_NOTPRESENT */
758                 mncc->fields &= ~MNCC_F_REDIRECTING;
759                 break;
760         }
761         switch (p_redirinfo.screen) {
762                 case INFO_SCREEN_USER:
763                 mncc->redirecting.screen = 0;
764                 break;
765                 default: /* INFO_SCREE_NETWORK */
766                 mncc->redirecting.screen = 3;
767                 break;
768         }
769         switch (p_redirinfo.present) {
770                 case INFO_PRESENT_ALLOWED:
771                 mncc->redirecting.present = 0;
772                 break;
773                 case INFO_PRESENT_RESTRICTED:
774                 mncc->redirecting.present = 1;
775                 break;
776                 default: /* INFO_PRESENT_NOTAVAIL */
777                 mncc->redirecting.present = 2;
778                 break;
779         }
780         /* sending redirecting number only in ntmode */
781         if (mncc->fields & MNCC_F_REDIRECTING) {
782                 SCPY(mncc->redirecting.number, p_redirinfo.id);
783                 add_trace("redir", "type", "%d", mncc->redirecting.type);
784                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
785                 add_trace("redir", "present", "%d", mncc->redirecting.present);
786                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
787                 add_trace("redir", "number", "%s", mncc->redirecting.number);
788         }
789         /* bearer capability */
790         //todo
791
792         end_trace();
793         send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
794
795         new_state(PORT_STATE_OUT_SETUP);
796
797         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
798         message_put(message);
799
800         new_state(PORT_STATE_OUT_PROCEEDING);
801 }
802
803 /*
804  * endpoint sends messages to the port
805  */
806 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
807 {
808         if (Pgsm::message_epoint(epoint_id, message_id, param))
809                 return(1);
810
811         switch(message_id) {
812                 case MESSAGE_SETUP: /* dial-out command received from epoint */
813                 if (p_state!=PORT_STATE_IDLE)
814                         break;
815                 message_setup(epoint_id, message_id, param);
816                 break;
817
818                 default:
819                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
820         }
821
822         return(1);
823 }
824
825 int gsm_bs_exit(int rc)
826 {
827 #if 0
828         /* free gsm instance */
829         if (gsm) {
830                 /* shutdown network */
831                 if (gsm->network)
832                         bsc_shutdown_net((struct gsm_network *)gsm->network);
833                 /* free network */
834 //              if (gsm->network) {
835 //                      free((struct gsm_network *)gsm->network); /* TBD */
836 //              }
837         }
838 #endif
839         return(rc);
840 }
841
842 extern "C" {
843
844 static int mncc_q_enqueue(struct gsm_mncc *mncc, unsigned int len)
845 {
846         struct mncc_q_entry *qe;
847
848         qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
849         if (!qe)
850                 return -ENOMEM;
851
852         qe->next = NULL;
853         qe->len = len;
854         memcpy(qe->data, mncc, len);
855
856         /* in case of empty list ... */
857         if (!gsm->mncc_q_hd && !gsm->mncc_q_tail) {
858                 /* the list head and tail both point to the new qe */
859                 gsm->mncc_q_hd = gsm->mncc_q_tail = qe;
860         } else {
861                 /* append to tail of list */
862                 gsm->mncc_q_tail->next = qe;
863                 gsm->mncc_q_tail = qe;
864         }
865
866         gsm->mncc_lfd.when |= LCR_FD_WRITE;
867
868         return 0;
869 }
870
871 static struct mncc_q_entry *mncc_q_dequeue(void)
872 {
873         struct mncc_q_entry *qe = gsm->mncc_q_hd;
874         if (!qe)
875                 return NULL;
876
877         /* dequeue the successfully sent message */
878         gsm->mncc_q_hd = qe->next;
879         if (!qe)
880                 return NULL;
881         if (qe == gsm->mncc_q_tail)
882                 gsm->mncc_q_tail = NULL;
883
884         return qe;
885 }
886
887 /* routine called by LCR code if it wants to send a message to OpenBSC */
888 int mncc_send(struct gsm_network *instance, int msg_type, void *data)
889 {
890         int len = 0;
891
892         /* FIXME: the caller should provide this */
893         switch (msg_type) {
894         case GSM_TCHF_FRAME:
895                 len = sizeof(struct gsm_data_frame) + 33;
896                 break;
897         default:
898                 len = sizeof(struct gsm_mncc);
899                 break;
900         }
901                 
902         return mncc_q_enqueue((struct gsm_mncc *)data, len);
903 }
904
905 } // extern "C"
906
907 /* close MNCC socket */
908 static int mncc_fd_close(struct lcr_fd *lfd)
909 {
910         class Port *port;
911         class Pgsm_bs *pgsm_bs = NULL;
912         struct lcr_msg *message;
913
914         PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
915         close(lfd->fd);
916         unregister_fd(lfd);
917         lfd->fd = -1;
918
919         /* free all the calls that were running through the MNCC interface */
920         port = port_first;
921         while(port) {
922                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
923                         pgsm_bs = (class Pgsm_bs *)port;
924                         message = message_create(pgsm_bs->p_serial, ACTIVE_EPOINT(pgsm_bs->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
925                         message->param.disconnectinfo.cause = 27; // temp. unavail.
926                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
927                         message_put(message);
928                         pgsm_bs->new_state(PORT_STATE_RELEASE);
929                         trigger_work(&pgsm_bs->p_m_g_delete);
930                 }
931                 port = port->next;
932         }
933
934         /* flush the queue */
935         while (mncc_q_dequeue())
936                 ;
937
938         /* start the re-connect timer */
939         schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
940
941         generate_dtmf();
942
943         return 0;
944 }
945
946 /* read from OpenBSC via MNCC socket */
947 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
948 {
949         struct mncc_q_entry *qe, *qe2;
950         int rc;
951
952         while (1) {
953                 qe = gsm->mncc_q_hd;
954                 if (!qe) {
955                         lfd->when &= ~LCR_FD_WRITE;
956                         break;
957                 }
958                 rc = write(lfd->fd, qe->data, qe->len);
959                 if (rc == 0)
960                         return mncc_fd_close(lfd);
961                 if (rc < 0)
962                         return rc;
963                 if (rc < (int)qe->len)
964                         return -1;
965                 /* dequeue the successfully sent message */
966                 qe2 = mncc_q_dequeue();
967                 assert(qe == qe2);
968                 free(qe);
969         }
970         return 0;
971 }
972
973 /* read from OpenBSC via MNCC socket */
974 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
975 {
976         int rc;
977         static char buf[sizeof(struct gsm_mncc)+1024];
978         struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
979
980         memset(buf, 0, sizeof(buf));
981         rc = recv(lfd->fd, buf, sizeof(buf), 0);
982         if (rc == 0)
983                 return mncc_fd_close(lfd);
984         if (rc < 0)
985                 return rc;
986
987         /* Hand the MNCC message into LCR */
988         return message_bsc(NULL, mncc_prim->msg_type, mncc_prim);
989 }
990
991 /* file descriptor callback if we can read or write form MNCC socket */
992 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *instance, int idx)
993 {
994         int rc = 0;
995
996         if (what & LCR_FD_READ)
997                 rc = mncc_fd_read(lfd, instance, idx);
998         if (rc < 0)
999                 return rc;
1000
1001         if (what & LCR_FD_WRITE)
1002                 rc = mncc_fd_write(lfd, instance, idx);
1003
1004         return rc;
1005 }
1006
1007 static int socket_retry_cb(struct lcr_timer *timer, void *instance, int index)
1008 {
1009         int fd, rc;
1010
1011         fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1012         if (fd < 0) {
1013                 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1014                 return fd;
1015         }
1016
1017         rc = connect(fd, (struct sockaddr *) &gsm->sun,
1018                      sizeof(gsm->sun));
1019         if (rc < 0) {
1020                 PERROR("Could not connect to MNCC socket, "
1021                         "retrying in %u seconds\n", SOCKET_RETRY_TIMER);
1022                 close(fd);
1023                 schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1024         } else {
1025                 PDEBUG(DEBUG_GSM, "Connected to MNCC socket!\n");
1026                 gsm->mncc_lfd.fd = fd;
1027                 register_fd(&gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, NULL, 0);
1028         }
1029
1030         return 0;
1031 }
1032
1033 int gsm_bs_init(void)
1034 {
1035         gsm->sun.sun_family = AF_UNIX;
1036         SCPY(gsm->sun.sun_path, "/tmp/bsc_mncc");
1037
1038         memset(&gsm->socket_retry, 0, sizeof(gsm->socket_retry));
1039         add_timer(&gsm->socket_retry, socket_retry_cb, NULL, 0);
1040
1041         /* do the initial connect */
1042         socket_retry_cb(&gsm->socket_retry, NULL, 0);
1043
1044         return 0;
1045 }