MNCC: Re-connect to the MNCC socket if connection was lost
[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 number */
567         if (!p_dialinginfo.id[0]) {
568                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
569                 add_trace("failure", NULL, "No dialed subscriber given.");
570                 end_trace();
571                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
572                 message->param.disconnectinfo.cause = 28;
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         /* release if port is blocked */
581         if (p_m_mISDNport->ifport->block) {
582                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
583                 add_trace("failure", NULL, "Port blocked.");
584                 end_trace();
585                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
586                 message->param.disconnectinfo.cause = 27; // temp. unavail.
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         /* hunt channel */
595         ret = channel = hunt_bchannel();
596         if (ret < 0)
597                 goto no_channel;
598         /* open channel */
599         ret = seize_bchannel(channel, 1);
600         if (ret < 0) {
601                 no_channel:
602                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
603                 add_trace("failure", NULL, "No internal audio channel available.");
604                 end_trace();
605                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
606                 message->param.disconnectinfo.cause = 34;
607                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
608                 message_put(message);
609                 new_state(PORT_STATE_RELEASE);
610                 trigger_work(&p_m_g_delete);
611                 return;
612         }
613         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
614         if (bchannel_open(p_m_b_index))
615                 goto no_channel;
616
617 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
618         /* screen outgoing caller id */
619         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
620
621         /* attach only if not already */
622         epointlist = p_epointlist;
623         while(epointlist) {
624                 if (epointlist->epoint_id == epoint_id)
625                         break;
626                 epointlist = epointlist->next;
627         }
628         if (!epointlist)
629                 epointlist_new(epoint_id);
630
631         /* creating l3id */
632         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
633         p_m_g_callref = new_callref++;
634         add_trace("callref", "new", "0x%x", p_m_g_callref);
635         end_trace();
636
637         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
638         mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
639         /* caller information */
640         mncc->fields |= MNCC_F_CALLING;
641         mncc->calling.plan = 1;
642         switch (p_callerinfo.ntype) {
643                 case INFO_NTYPE_UNKNOWN:
644                 mncc->calling.type = 0x0;
645                 break;
646                 case INFO_NTYPE_INTERNATIONAL:
647                 mncc->calling.type = 0x1;
648                 break;
649                 case INFO_NTYPE_NATIONAL:
650                 mncc->calling.type = 0x2;
651                 break;
652                 case INFO_NTYPE_SUBSCRIBER:
653                 mncc->calling.type = 0x4;
654                 break;
655                 default: /* INFO_NTYPE_NOTPRESENT */
656                 mncc->fields &= ~MNCC_F_CALLING;
657                 break;
658         }
659         switch (p_callerinfo.screen) {
660                 case INFO_SCREEN_USER:
661                 mncc->calling.screen = 0;
662                 break;
663                 default: /* INFO_SCREEN_NETWORK */
664                 mncc->calling.screen = 3;
665                 break;
666         }
667         switch (p_callerinfo.present) {
668                 case INFO_PRESENT_ALLOWED:
669                 mncc->calling.present = 0;
670                 break;
671                 case INFO_PRESENT_RESTRICTED:
672                 mncc->calling.present = 1;
673                 break;
674                 default: /* INFO_PRESENT_NOTAVAIL */
675                 mncc->calling.present = 2;
676                 break;
677         }
678         if (mncc->fields & MNCC_F_CALLING) {
679                 SCPY(mncc->calling.number, p_callerinfo.id);
680                 add_trace("calling", "type", "%d", mncc->calling.type);
681                 add_trace("calling", "plan", "%d", mncc->calling.plan);
682                 add_trace("calling", "present", "%d", mncc->calling.present);
683                 add_trace("calling", "screen", "%d", mncc->calling.screen);
684                 add_trace("calling", "number", "%s", mncc->calling.number);
685         }
686         /* dialing information */
687         mncc->fields |= MNCC_F_CALLED;
688         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
689                 SCPY(mncc->imsi, p_dialinginfo.id+5);
690                 add_trace("dialing", "imsi", "%s", mncc->imsi);
691         } else {
692                 SCPY(mncc->called.number, p_dialinginfo.id);
693                 add_trace("dialing", "number", "%s", mncc->called.number);
694         }
695         
696         /* sending user-user */
697
698         /* redirecting number */
699         mncc->fields |= MNCC_F_REDIRECTING;
700         mncc->redirecting.plan = 1;
701         switch (p_redirinfo.ntype) {
702                 case INFO_NTYPE_UNKNOWN:
703                 mncc->redirecting.type = 0x0;
704                 break;
705                 case INFO_NTYPE_INTERNATIONAL:
706                 mncc->redirecting.type = 0x1;
707                 break;
708                 case INFO_NTYPE_NATIONAL:
709                 mncc->redirecting.type = 0x2;
710                 break;
711                 case INFO_NTYPE_SUBSCRIBER:
712                 mncc->redirecting.type = 0x4;
713                 break;
714                 default: /* INFO_NTYPE_NOTPRESENT */
715                 mncc->fields &= ~MNCC_F_REDIRECTING;
716                 break;
717         }
718         switch (p_redirinfo.screen) {
719                 case INFO_SCREEN_USER:
720                 mncc->redirecting.screen = 0;
721                 break;
722                 default: /* INFO_SCREE_NETWORK */
723                 mncc->redirecting.screen = 3;
724                 break;
725         }
726         switch (p_redirinfo.present) {
727                 case INFO_PRESENT_ALLOWED:
728                 mncc->redirecting.present = 0;
729                 break;
730                 case INFO_PRESENT_RESTRICTED:
731                 mncc->redirecting.present = 1;
732                 break;
733                 default: /* INFO_PRESENT_NOTAVAIL */
734                 mncc->redirecting.present = 2;
735                 break;
736         }
737         /* sending redirecting number only in ntmode */
738         if (mncc->fields & MNCC_F_REDIRECTING) {
739                 SCPY(mncc->redirecting.number, p_redirinfo.id);
740                 add_trace("redir", "type", "%d", mncc->redirecting.type);
741                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
742                 add_trace("redir", "present", "%d", mncc->redirecting.present);
743                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
744                 add_trace("redir", "number", "%s", mncc->redirecting.number);
745         }
746         /* bearer capability */
747         //todo
748
749         end_trace();
750         send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
751
752         new_state(PORT_STATE_OUT_SETUP);
753
754         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
755         message_put(message);
756
757         new_state(PORT_STATE_OUT_PROCEEDING);
758 }
759
760 /*
761  * endpoint sends messages to the port
762  */
763 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
764 {
765         if (Pgsm::message_epoint(epoint_id, message_id, param))
766                 return(1);
767
768         switch(message_id) {
769                 case MESSAGE_SETUP: /* dial-out command received from epoint */
770                 if (p_state!=PORT_STATE_IDLE)
771                         break;
772                 message_setup(epoint_id, message_id, param);
773                 break;
774
775                 default:
776                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
777         }
778
779         return(1);
780 }
781
782 int gsm_bs_exit(int rc)
783 {
784 #if 0
785         /* free gsm instance */
786         if (gsm) {
787                 /* shutdown network */
788                 if (gsm->network)
789                         bsc_shutdown_net((struct gsm_network *)gsm->network);
790                 /* free network */
791 //              if (gsm->network) {
792 //                      free((struct gsm_network *)gsm->network); /* TBD */
793 //              }
794         }
795 #endif
796         return(rc);
797 }
798
799 extern "C" {
800
801 static int mncc_q_enqueue(struct gsm_mncc *mncc, unsigned int len)
802 {
803         struct mncc_q_entry *qe;
804
805         qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
806         if (!qe)
807                 return -ENOMEM;
808
809         qe->next = NULL;
810         qe->len = len;
811         memcpy(qe->data, mncc, len);
812
813         /* in case of empty list ... */
814         if (!gsm->mncc_q_hd && !gsm->mncc_q_tail) {
815                 /* the list head and tail both point to the new qe */
816                 gsm->mncc_q_hd = gsm->mncc_q_tail = qe;
817         } else {
818                 /* append to tail of list */
819                 gsm->mncc_q_tail->next = qe;
820                 gsm->mncc_q_tail = qe;
821         }
822
823         gsm->mncc_lfd.when |= LCR_FD_WRITE;
824
825         return 0;
826 }
827
828 static struct mncc_q_entry *mncc_q_dequeue(void)
829 {
830         struct mncc_q_entry *qe = gsm->mncc_q_hd;
831         if (!qe)
832                 return NULL;
833
834         /* dequeue the successfully sent message */
835         gsm->mncc_q_hd = qe->next;
836         if (!qe)
837                 return NULL;
838         if (qe == gsm->mncc_q_tail)
839                 gsm->mncc_q_tail = NULL;
840
841         return qe;
842 }
843
844 /* routine called by LCR code if it wants to send a message to OpenBSC */
845 int mncc_send(struct gsm_network *instance, int msg_type, void *data)
846 {
847         int len = 0;
848
849         /* FIXME: the caller should provide this */
850         switch (msg_type) {
851         case GSM_TCHF_FRAME:
852                 len = sizeof(struct gsm_data_frame) + 33;
853                 break;
854         default:
855                 len = sizeof(struct gsm_mncc);
856                 break;
857         }
858                 
859         return mncc_q_enqueue((struct gsm_mncc *)data, len);
860 }
861
862 } // extern "C"
863
864 /* close MNCC socket */
865 static int mncc_fd_close(struct lcr_fd *lfd)
866 {
867         class Port *port;
868         class Pgsm_bs *pgsm_bs = NULL;
869         struct lcr_msg *message;
870
871         PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
872         close(lfd->fd);
873         unregister_fd(lfd);
874         lfd->fd = -1;
875
876         /* free all the calls that were running through the MNCC interface */
877         port = port_first;
878         while(port) {
879                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
880                         pgsm_bs = (class Pgsm_bs *)port;
881                         message = message_create(pgsm_bs->p_serial, ACTIVE_EPOINT(pgsm_bs->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
882                         message->param.disconnectinfo.cause = 27; // temp. unavail.
883                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
884                         message_put(message);
885                         pgsm_bs->new_state(PORT_STATE_RELEASE);
886                         trigger_work(&pgsm_bs->p_m_g_delete);
887                 }
888                 port = port->next;
889         }
890
891         /* flush the queue */
892         while (mncc_q_dequeue())
893                 ;
894
895         /* start the re-connect timer */
896         schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
897
898         generate_dtmf();
899
900         return 0;
901 }
902
903 /* read from OpenBSC via MNCC socket */
904 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
905 {
906         struct mncc_q_entry *qe, *qe2;
907         int rc;
908
909         while (1) {
910                 qe = gsm->mncc_q_hd;
911                 if (!qe) {
912                         lfd->when &= ~LCR_FD_WRITE;
913                         break;
914                 }
915                 rc = write(lfd->fd, qe->data, qe->len);
916                 if (rc == 0)
917                         return mncc_fd_close(lfd);
918                 if (rc < 0)
919                         return rc;
920                 if (rc < qe->len)
921                         return -1;
922                 /* dequeue the successfully sent message */
923                 qe2 = mncc_q_dequeue();
924                 assert(qe == qe2);
925                 free(qe);
926         }
927         return 0;
928 }
929
930 /* read from OpenBSC via MNCC socket */
931 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
932 {
933         int rc;
934         static char buf[sizeof(struct gsm_mncc)+1024];
935         struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
936
937         memset(buf, 0, sizeof(buf));
938         rc = recv(lfd->fd, buf, sizeof(buf), 0);
939         if (rc == 0)
940                 return mncc_fd_close(lfd);
941         if (rc < 0)
942                 return rc;
943
944         /* Hand the MNCC message into LCR */
945         return message_bsc(NULL, mncc_prim->msg_type, mncc_prim);
946 }
947
948 /* file descriptor callback if we can read or write form MNCC socket */
949 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *instance, int idx)
950 {
951         int rc = 0;
952
953         if (what & LCR_FD_READ)
954                 rc = mncc_fd_read(lfd, instance, idx);
955         if (rc < 0)
956                 return rc;
957
958         if (what & LCR_FD_WRITE)
959                 rc = mncc_fd_write(lfd, instance, idx);
960
961         return rc;
962 }
963
964 static int socket_retry_cb(struct lcr_timer *timer, void *instance, int index)
965 {
966         int fd, rc;
967
968         fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
969         if (fd < 0) {
970                 PERROR("Cannot create SEQPACKET socket, giving up!\n");
971                 return fd;
972         }
973
974         rc = connect(fd, (struct sockaddr *) &gsm->sun,
975                      sizeof(gsm->sun));
976         if (rc < 0) {
977                 PERROR("Could not connect to MNCC socket, "
978                         "retrying in %u seconds\n", SOCKET_RETRY_TIMER);
979                 close(fd);
980                 schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
981         } else {
982                 PDEBUG(DEBUG_GSM, "Connected to MNCC socket!\n");
983                 gsm->mncc_lfd.fd = fd;
984                 register_fd(&gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, NULL, 0);
985         }
986
987         return 0;
988 }
989
990 int gsm_bs_init(void)
991 {
992         gsm->sun.sun_family = AF_UNIX;
993         strcpy(gsm->sun.sun_path, "/tmp/bsc_mncc");
994
995         memset(&gsm->socket_retry, 0, sizeof(gsm->socket_retry));
996         add_timer(&gsm->socket_retry, socket_retry_cb, NULL, 0);
997
998         /* do the initial connect */
999         socket_retry_cb(&gsm->socket_retry, NULL, 0);
1000
1001         return 0;
1002 }