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