1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** mISDN gsm (BS mode) **
10 \*****************************************************************************/
22 #include <sys/socket.h>
25 #include <openbsc/mncc.h>
26 #include <openbsc/trau_frame.h>
29 #define SOCKET_RETRY_TIMER 5
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 };
38 void generate_dtmf(void)
40 double fx, fy, sample;
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];
62 unsigned char dtmf_samples[16][8000];
63 static int dtmf_x[4] = { 1209, 1336, 1477, 1633 };
64 static int dtmf_y[4] = { 697, 770, 852, 941 };
66 void generate_dtmf(void)
68 double fx, fy, sample;
72 for (y = 0; y < 4; y++) {
73 fy = 2 * 3.1415927 * ((double)dtmf_y[y]) / 8000.0;
74 for (x = 0; x < 4; x++) {
75 fx = 2 * 3.1415927 * ((double)dtmf_x[x]) / 8000.0;
76 law = dtmf_samples[y << 2 | x];
77 for (i = 0; i < 8000; i++) {
78 sample = sin(fy * ((double)i)) * 0.251 * 32767.0; /* -6 dB */
79 sample += sin(fx * ((double)i)) * 0.158 * 32767.0; /* -8 dB */
80 *law++ = audio_s16_to_law[(int)sample & 0xffff];
90 Pgsm_bs::Pgsm_bs(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : Pgsm(type, mISDNport, portname, settings, channel, exclusive, mode)
92 p_m_g_instance = gsm->network;
96 PDEBUG(DEBUG_GSM, "Created new GSMBSPort(%s).\n", portname);
104 PDEBUG(DEBUG_GSM, "Destroyed GSM BS process(%s).\n", p_name);
107 /* DTMF INDICATION */
108 void Pgsm_bs::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
110 // struct lcr_msg *message;
111 struct gsm_mncc *resp;
113 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
114 add_trace("keypad", NULL, "%c", mncc->keypad);
116 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
117 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
120 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
121 add_trace("keypad", NULL, "%c", mncc->keypad);
123 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
124 resp->fields |= MNCC_F_KEYPAD;
125 resp->keypad = mncc->keypad;
126 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
129 /* send dialing information */
130 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
131 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
132 message_put(message);
135 /* generate DTMF tones */
136 switch (mncc->keypad) {
137 case '1': p_m_g_dtmf = dtmf_samples[0]; break;
138 case '2': p_m_g_dtmf = dtmf_samples[1]; break;
139 case '3': p_m_g_dtmf = dtmf_samples[2]; break;
141 case 'A': p_m_g_dtmf = dtmf_samples[3]; break;
142 case '4': p_m_g_dtmf = dtmf_samples[4]; break;
143 case '5': p_m_g_dtmf = dtmf_samples[5]; break;
144 case '6': p_m_g_dtmf = dtmf_samples[6]; break;
146 case 'B': p_m_g_dtmf = dtmf_samples[7]; break;
147 case '7': p_m_g_dtmf = dtmf_samples[8]; break;
148 case '8': p_m_g_dtmf = dtmf_samples[9]; break;
149 case '9': p_m_g_dtmf = dtmf_samples[10]; break;
151 case 'C': p_m_g_dtmf = dtmf_samples[11]; break;
152 case '*': p_m_g_dtmf = dtmf_samples[12]; break;
153 case '0': p_m_g_dtmf = dtmf_samples[13]; break;
154 case '#': p_m_g_dtmf = dtmf_samples[14]; break;
156 case 'D': p_m_g_dtmf = dtmf_samples[15]; break;
158 p_m_g_dtmf_index = 0;
160 void Pgsm_bs::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
162 struct gsm_mncc *resp;
164 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
165 add_trace("keypad", NULL, "%c", mncc->keypad);
169 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
170 add_trace("keypad", NULL, "%c", mncc->keypad);
172 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
173 resp->keypad = mncc->keypad;
174 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
180 /* HOLD INDICATION */
181 void Pgsm_bs::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
183 struct lcr_msg *message;
184 struct gsm_mncc *resp, *frame;
186 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
189 /* notify the hold of call */
190 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
191 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
192 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
193 message_put(message);
195 /* acknowledge hold */
196 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
198 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
199 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
202 if (p_m_g_tch_connected) { /* it should be true */
203 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
205 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
206 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
207 p_m_g_tch_connected = 0;
212 /* RETRIEVE INDICATION */
213 void Pgsm_bs::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
215 struct lcr_msg *message;
216 struct gsm_mncc *resp, *frame;
218 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
221 /* notify the retrieve of call */
222 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
223 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
224 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
225 message_put(message);
227 /* acknowledge retr */
228 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
230 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
231 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
234 if (!p_m_g_tch_connected) { /* it should be true */
235 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
237 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
238 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
239 p_m_g_tch_connected = 1;
244 * handles all indications
246 /* SETUP INDICATION */
247 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
250 class Endpoint *epoint;
251 struct lcr_msg *message;
253 struct gsm_mncc *mode, *proceeding, *frame;
255 /* process given callref */
256 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
257 add_trace("callref", "new", "0x%x", callref);
259 /* release in case the ID is already in use */
260 add_trace("error", NULL, "callref already in use");
262 mncc = create_mncc(MNCC_REJ_REQ, callref);
263 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
264 mncc->fields |= MNCC_F_CAUSE;
265 mncc->cause.coding = 3;
266 mncc->cause.location = 1;
267 mncc->cause.value = 47;
268 add_trace("cause", "coding", "%d", mncc->cause.coding);
269 add_trace("cause", "location", "%d", mncc->cause.location);
270 add_trace("cause", "value", "%d", mncc->cause.value);
271 add_trace("reason", NULL, "callref already in use");
273 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
274 new_state(PORT_STATE_RELEASE);
275 trigger_work(&p_m_g_delete);
278 p_m_g_callref = callref;
281 /* if blocked, release call with MT_RELEASE_COMPLETE */
282 if (p_m_mISDNport->ifport->block) {
283 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
284 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
285 mncc->fields |= MNCC_F_CAUSE;
286 mncc->cause.coding = 3;
287 mncc->cause.location = 1;
288 mncc->cause.value = 27;
289 add_trace("cause", "coding", "%d", mncc->cause.coding);
290 add_trace("cause", "location", "%d", mncc->cause.location);
291 add_trace("cause", "value", "%d", mncc->cause.value);
292 add_trace("reason", NULL, "port is blocked");
294 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
295 new_state(PORT_STATE_RELEASE);
296 trigger_work(&p_m_g_delete);
302 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
304 p_callerinfo.present = INFO_PRESENT_ALLOWED;
305 if (mncc->calling.number[0])
306 SCPY(p_callerinfo.id, mncc->calling.number);
308 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
309 SCPY(p_callerinfo.imsi, mncc->imsi);
310 p_callerinfo.screen = INFO_SCREEN_NETWORK;
311 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
312 p_callerinfo.isdn_port = p_m_portnum;
313 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
315 /* dialing information */
316 SCAT(p_dialinginfo.id, mncc->called.number);
317 switch (mncc->called.type) {
319 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
322 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
325 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
328 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
331 if (mncc->emergency) {
332 SCPY(p_dialinginfo.id, "emergency");
334 p_dialinginfo.sending_complete = 1;
336 /* bearer capability */
338 p_capainfo.bearer_capa = INFO_BC_SPEECH;
339 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
340 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
341 p_capainfo.source_mode = B_MODE_TRANSPARENT;
342 p_m_g_mode = p_capainfo.source_mode;
347 ret = channel = hunt_bchannel();
352 ret = seize_bchannel(channel, 1);
355 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
356 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
357 mncc->fields |= MNCC_F_CAUSE;
358 mncc->cause.coding = 3;
359 mncc->cause.location = 1;
360 mncc->cause.value = 34;
361 add_trace("cause", "coding", "%d", mncc->cause.coding);
362 add_trace("cause", "location", "%d", mncc->cause.location);
363 add_trace("cause", "value", "%d", mncc->cause.value);
364 add_trace("reason", NULL, "no channel");
366 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
367 new_state(PORT_STATE_RELEASE);
368 trigger_work(&p_m_g_delete);
371 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
372 if (bchannel_open(p_m_b_index))
375 /* what infos did we got ... */
376 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
377 if (p_callerinfo.id[0])
378 add_trace("calling", "number", "%s", p_callerinfo.id);
380 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
381 add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
382 add_trace("dialing", "number", "%s", p_dialinginfo.id);
385 /* create endpoint */
387 FATAL("Incoming call but already got an endpoint.\n");
388 if (!(epoint = new Endpoint(p_serial, 0)))
389 FATAL("No memory for Endpoint instance\n");
390 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
391 FATAL("No memory for Endpoint Application instance\n");
392 epointlist_new(epoint->ep_serial);
394 /* modify lchan to GSM codec V1 */
395 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
396 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
397 mode->lchan_mode = 0x01; /* GSM V1 */
398 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
400 send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
402 /* send call proceeding */
403 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
404 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
405 if (p_m_mISDNport->tones) {
406 proceeding->fields |= MNCC_F_PROGRESS;
407 proceeding->progress.coding = 3; /* GSM */
408 proceeding->progress.location = 1;
409 proceeding->progress.descr = 8;
410 add_trace("progress", "coding", "%d", proceeding->progress.coding);
411 add_trace("progress", "location", "%d", proceeding->progress.location);
412 add_trace("progress", "descr", "%d", proceeding->progress.descr);
415 send_and_free_mncc(p_m_g_instance, proceeding->msg_type, proceeding);
417 new_state(PORT_STATE_IN_PROCEEDING);
419 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
420 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
422 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
423 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
424 p_m_g_tch_connected = 1;
427 /* send setup message to endpoit */
428 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
429 message->param.setup.isdn_port = p_m_portnum;
430 message->param.setup.port_type = p_type;
431 // message->param.setup.dtmf = 0;
432 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
433 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
434 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
435 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
436 message->param.setup.useruser.len = strlen(mncc->useruser.info);
437 message->param.setup.useruser.protocol = mncc->useruser.proto;
438 message_put(message);
442 * BSC sends message to port
444 static int message_bsc(struct gsm_network *net, int msg_type, void *arg)
446 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
447 unsigned int callref = mncc->callref;
449 class Pgsm_bs *pgsm_bs = NULL;
451 struct mISDNport *mISDNport;
453 /* Special messages */
458 callref = mncc->callref;
461 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
462 pgsm_bs = (class Pgsm_bs *)port;
463 if (pgsm_bs->p_m_g_callref == callref) {
470 if (msg_type == GSM_TCHF_FRAME) {
472 /* inject DTMF, if enabled */
473 if (pgsm_bs->p_m_g_dtmf) {
474 unsigned char data[160];
477 for (i = 0; i < 160; i++) {
478 data[i] = pgsm_bs->p_m_g_dtmf[pgsm_bs->p_m_g_dtmf_index++];
479 if (pgsm_bs->p_m_g_dtmf_index == 8000)
480 pgsm_bs->p_m_g_dtmf_index = 0;
483 pgsm_bs->bchannel_send(PH_DATA_REQ, 0, data, 160);
485 pgsm_bs->frame_receive(arg);
491 if (msg_type != MNCC_SETUP_IND)
494 mISDNport = mISDNport_first;
496 if (mISDNport->gsm_bs)
498 mISDNport = mISDNport->next;
501 struct gsm_mncc *rej;
503 rej = create_mncc(MNCC_REJ_REQ, callref);
504 rej->fields |= MNCC_F_CAUSE;
505 rej->cause.coding = 3;
506 rej->cause.location = 1;
507 rej->cause.value = 27;
508 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
509 add_trace("cause", "coding", "%d", rej->cause.coding);
510 add_trace("cause", "location", "%d", rej->cause.location);
511 add_trace("cause", "value", "%d", rej->cause.value);
513 send_and_free_mncc(gsm->network, rej->msg_type, rej);
516 /* creating port object, transparent until setup with hdlc */
517 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
518 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
520 FATAL("Cannot create Port instance.\n");
525 pgsm_bs->setup_ind(msg_type, callref, mncc);
528 case MNCC_START_DTMF_IND:
529 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
532 case MNCC_STOP_DTMF_IND:
533 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
536 case MNCC_CALL_CONF_IND:
537 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
541 pgsm_bs->alert_ind(msg_type, callref, mncc);
545 pgsm_bs->setup_cnf(msg_type, callref, mncc);
548 case MNCC_SETUP_COMPL_IND:
549 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
553 pgsm_bs->disc_ind(msg_type, callref, mncc);
559 pgsm_bs->rel_ind(msg_type, callref, mncc);
562 case MNCC_NOTIFY_IND:
563 pgsm_bs->notify_ind(msg_type, callref, mncc);
567 pgsm_bs->hold_ind(msg_type, callref, mncc);
570 case MNCC_RETRIEVE_IND:
571 pgsm_bs->retr_ind(msg_type, callref, mncc);
575 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: 0x%x\n", pgsm_bs->p_name, pgsm_bs->p_callerinfo.id, msg_type);
581 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
583 struct lcr_msg *message;
585 struct epoint_list *epointlist;
586 struct gsm_mncc *mncc;
589 /* copy setup infos to port */
590 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
591 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
592 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
593 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
595 /* no GSM MNCC connection */
596 if (gsm->mncc_lfd.fd < 0) {
597 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
598 add_trace("failure", NULL, "No MNCC connection.");
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);
610 if (!p_dialinginfo.id[0]) {
611 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
612 add_trace("failure", NULL, "No dialed subscriber given.");
614 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
615 message->param.disconnectinfo.cause = 28;
616 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
617 message_put(message);
618 new_state(PORT_STATE_RELEASE);
619 trigger_work(&p_m_g_delete);
623 /* release if port is blocked */
624 if (p_m_mISDNport->ifport->block) {
625 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
626 add_trace("failure", NULL, "Port blocked.");
628 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
629 message->param.disconnectinfo.cause = 27; // temp. unavail.
630 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
631 message_put(message);
632 new_state(PORT_STATE_RELEASE);
633 trigger_work(&p_m_g_delete);
638 ret = channel = hunt_bchannel();
642 ret = seize_bchannel(channel, 1);
645 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
646 add_trace("failure", NULL, "No internal audio channel available.");
648 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
649 message->param.disconnectinfo.cause = 34;
650 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
651 message_put(message);
652 new_state(PORT_STATE_RELEASE);
653 trigger_work(&p_m_g_delete);
656 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
657 if (bchannel_open(p_m_b_index))
660 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
661 /* screen outgoing caller id */
662 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
664 /* attach only if not already */
665 epointlist = p_epointlist;
667 if (epointlist->epoint_id == epoint_id)
669 epointlist = epointlist->next;
672 epointlist_new(epoint_id);
675 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
676 p_m_g_callref = new_callref++;
677 add_trace("callref", "new", "0x%x", p_m_g_callref);
680 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
681 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
682 /* caller information */
683 mncc->fields |= MNCC_F_CALLING;
684 mncc->calling.plan = 1;
685 switch (p_callerinfo.ntype) {
686 case INFO_NTYPE_UNKNOWN:
687 mncc->calling.type = 0x0;
689 case INFO_NTYPE_INTERNATIONAL:
690 mncc->calling.type = 0x1;
692 case INFO_NTYPE_NATIONAL:
693 mncc->calling.type = 0x2;
695 case INFO_NTYPE_SUBSCRIBER:
696 mncc->calling.type = 0x4;
698 default: /* INFO_NTYPE_NOTPRESENT */
699 mncc->fields &= ~MNCC_F_CALLING;
702 switch (p_callerinfo.screen) {
703 case INFO_SCREEN_USER:
704 mncc->calling.screen = 0;
706 default: /* INFO_SCREEN_NETWORK */
707 mncc->calling.screen = 3;
710 switch (p_callerinfo.present) {
711 case INFO_PRESENT_ALLOWED:
712 mncc->calling.present = 0;
714 case INFO_PRESENT_RESTRICTED:
715 mncc->calling.present = 1;
717 default: /* INFO_PRESENT_NOTAVAIL */
718 mncc->calling.present = 2;
721 if (mncc->fields & MNCC_F_CALLING) {
722 SCPY(mncc->calling.number, p_callerinfo.id);
723 add_trace("calling", "type", "%d", mncc->calling.type);
724 add_trace("calling", "plan", "%d", mncc->calling.plan);
725 add_trace("calling", "present", "%d", mncc->calling.present);
726 add_trace("calling", "screen", "%d", mncc->calling.screen);
727 add_trace("calling", "number", "%s", mncc->calling.number);
729 /* dialing information */
730 mncc->fields |= MNCC_F_CALLED;
731 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
732 SCPY(mncc->imsi, p_dialinginfo.id+5);
733 add_trace("dialing", "imsi", "%s", mncc->imsi);
735 SCPY(mncc->called.number, p_dialinginfo.id);
736 add_trace("dialing", "number", "%s", mncc->called.number);
739 /* sending user-user */
741 /* redirecting number */
742 mncc->fields |= MNCC_F_REDIRECTING;
743 mncc->redirecting.plan = 1;
744 switch (p_redirinfo.ntype) {
745 case INFO_NTYPE_UNKNOWN:
746 mncc->redirecting.type = 0x0;
748 case INFO_NTYPE_INTERNATIONAL:
749 mncc->redirecting.type = 0x1;
751 case INFO_NTYPE_NATIONAL:
752 mncc->redirecting.type = 0x2;
754 case INFO_NTYPE_SUBSCRIBER:
755 mncc->redirecting.type = 0x4;
757 default: /* INFO_NTYPE_NOTPRESENT */
758 mncc->fields &= ~MNCC_F_REDIRECTING;
761 switch (p_redirinfo.screen) {
762 case INFO_SCREEN_USER:
763 mncc->redirecting.screen = 0;
765 default: /* INFO_SCREE_NETWORK */
766 mncc->redirecting.screen = 3;
769 switch (p_redirinfo.present) {
770 case INFO_PRESENT_ALLOWED:
771 mncc->redirecting.present = 0;
773 case INFO_PRESENT_RESTRICTED:
774 mncc->redirecting.present = 1;
776 default: /* INFO_PRESENT_NOTAVAIL */
777 mncc->redirecting.present = 2;
780 /* sending redirecting number only in ntmode */
781 if (mncc->fields & MNCC_F_REDIRECTING) {
782 SCPY(mncc->redirecting.number, p_redirinfo.id);
783 add_trace("redir", "type", "%d", mncc->redirecting.type);
784 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
785 add_trace("redir", "present", "%d", mncc->redirecting.present);
786 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
787 add_trace("redir", "number", "%s", mncc->redirecting.number);
789 /* bearer capability */
793 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
795 new_state(PORT_STATE_OUT_SETUP);
797 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
798 message_put(message);
800 new_state(PORT_STATE_OUT_PROCEEDING);
804 * endpoint sends messages to the port
806 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
808 if (Pgsm::message_epoint(epoint_id, message_id, param))
812 case MESSAGE_SETUP: /* dial-out command received from epoint */
813 if (p_state!=PORT_STATE_IDLE)
815 message_setup(epoint_id, message_id, param);
819 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
825 int gsm_bs_exit(int rc)
828 /* free gsm instance */
830 /* shutdown network */
832 bsc_shutdown_net((struct gsm_network *)gsm->network);
834 // if (gsm->network) {
835 // free((struct gsm_network *)gsm->network); /* TBD */
844 static int mncc_q_enqueue(struct gsm_mncc *mncc, unsigned int len)
846 struct mncc_q_entry *qe;
848 qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
854 memcpy(qe->data, mncc, len);
856 /* in case of empty list ... */
857 if (!gsm->mncc_q_hd && !gsm->mncc_q_tail) {
858 /* the list head and tail both point to the new qe */
859 gsm->mncc_q_hd = gsm->mncc_q_tail = qe;
861 /* append to tail of list */
862 gsm->mncc_q_tail->next = qe;
863 gsm->mncc_q_tail = qe;
866 gsm->mncc_lfd.when |= LCR_FD_WRITE;
871 static struct mncc_q_entry *mncc_q_dequeue(void)
873 struct mncc_q_entry *qe = gsm->mncc_q_hd;
877 /* dequeue the successfully sent message */
878 gsm->mncc_q_hd = qe->next;
881 if (qe == gsm->mncc_q_tail)
882 gsm->mncc_q_tail = NULL;
887 /* routine called by LCR code if it wants to send a message to OpenBSC */
888 int mncc_send(struct gsm_network *instance, int msg_type, void *data)
892 /* FIXME: the caller should provide this */
895 len = sizeof(struct gsm_data_frame) + 33;
898 len = sizeof(struct gsm_mncc);
902 return mncc_q_enqueue((struct gsm_mncc *)data, len);
907 /* close MNCC socket */
908 static int mncc_fd_close(struct lcr_fd *lfd)
911 class Pgsm_bs *pgsm_bs = NULL;
912 struct lcr_msg *message;
914 PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
919 /* free all the calls that were running through the MNCC interface */
922 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
923 pgsm_bs = (class Pgsm_bs *)port;
924 message = message_create(pgsm_bs->p_serial, ACTIVE_EPOINT(pgsm_bs->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
925 message->param.disconnectinfo.cause = 27; // temp. unavail.
926 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
927 message_put(message);
928 pgsm_bs->new_state(PORT_STATE_RELEASE);
929 trigger_work(&pgsm_bs->p_m_g_delete);
934 /* flush the queue */
935 while (mncc_q_dequeue())
938 /* start the re-connect timer */
939 schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
946 /* read from OpenBSC via MNCC socket */
947 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
949 struct mncc_q_entry *qe, *qe2;
955 lfd->when &= ~LCR_FD_WRITE;
958 rc = write(lfd->fd, qe->data, qe->len);
960 return mncc_fd_close(lfd);
963 if (rc < (int)qe->len)
965 /* dequeue the successfully sent message */
966 qe2 = mncc_q_dequeue();
973 /* read from OpenBSC via MNCC socket */
974 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
977 static char buf[sizeof(struct gsm_mncc)+1024];
978 struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
980 memset(buf, 0, sizeof(buf));
981 rc = recv(lfd->fd, buf, sizeof(buf), 0);
983 return mncc_fd_close(lfd);
987 /* Hand the MNCC message into LCR */
988 return message_bsc(NULL, mncc_prim->msg_type, mncc_prim);
991 /* file descriptor callback if we can read or write form MNCC socket */
992 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *instance, int idx)
996 if (what & LCR_FD_READ)
997 rc = mncc_fd_read(lfd, instance, idx);
1001 if (what & LCR_FD_WRITE)
1002 rc = mncc_fd_write(lfd, instance, idx);
1007 static int socket_retry_cb(struct lcr_timer *timer, void *instance, int index)
1011 fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1013 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1017 rc = connect(fd, (struct sockaddr *) &gsm->sun,
1020 PERROR("Could not connect to MNCC socket, "
1021 "retrying in %u seconds\n", SOCKET_RETRY_TIMER);
1023 schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1025 PDEBUG(DEBUG_GSM, "Connected to MNCC socket!\n");
1026 gsm->mncc_lfd.fd = fd;
1027 register_fd(&gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, NULL, 0);
1033 int gsm_bs_init(void)
1035 gsm->sun.sun_family = AF_UNIX;
1036 SCPY(gsm->sun.sun_path, "/tmp/bsc_mncc");
1038 memset(&gsm->socket_retry, 0, sizeof(gsm->socket_retry));
1039 add_timer(&gsm->socket_retry, socket_retry_cb, NULL, 0);
1041 /* do the initial connect */
1042 socket_retry_cb(&gsm->socket_retry, NULL, 0);