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