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 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)
64 p_m_g_instance = gsm->network;
68 PDEBUG(DEBUG_GSM, "Created new GSMBSPort(%s).\n", portname);
76 PDEBUG(DEBUG_GSM, "Destroyed GSM BS process(%s).\n", p_name);
80 void Pgsm_bs::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
82 // struct lcr_msg *message;
83 struct gsm_mncc *resp;
85 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
86 add_trace("keypad", NULL, "%c", mncc->keypad);
88 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
89 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
92 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
93 add_trace("keypad", NULL, "%c", mncc->keypad);
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);
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);
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;
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;
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;
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;
127 case 'D': p_m_g_dtmf = dtmf_samples[15]; break;
129 p_m_g_dtmf_index = 0;
131 void Pgsm_bs::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
133 struct gsm_mncc *resp;
135 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
136 add_trace("keypad", NULL, "%c", mncc->keypad);
140 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
141 add_trace("keypad", NULL, "%c", mncc->keypad);
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);
151 /* HOLD INDICATION */
152 void Pgsm_bs::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
154 struct lcr_msg *message;
155 struct gsm_mncc *resp, *frame;
157 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
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);
166 /* acknowledge hold */
167 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
169 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
170 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
173 if (p_m_g_tch_connected) { /* it should be true */
174 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
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;
183 /* RETRIEVE INDICATION */
184 void Pgsm_bs::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
186 struct lcr_msg *message;
187 struct gsm_mncc *resp, *frame;
189 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
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);
198 /* acknowledge retr */
199 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
201 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
202 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
205 if (!p_m_g_tch_connected) { /* it should be true */
206 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
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;
215 * handles all indications
217 /* SETUP INDICATION */
218 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
221 class Endpoint *epoint;
222 struct lcr_msg *message;
224 struct gsm_mncc *mode, *proceeding, *frame;
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);
230 /* release in case the ID is already in use */
231 add_trace("error", NULL, "callref already in use");
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");
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);
249 p_m_g_callref = callref;
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");
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);
273 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
275 p_callerinfo.present = INFO_PRESENT_ALLOWED;
276 if (mncc->calling.number[0])
277 SCPY(p_callerinfo.id, mncc->calling.number);
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);
286 /* dialing information */
287 SCAT(p_dialinginfo.id, mncc->called.number);
288 switch (mncc->called.type) {
290 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
293 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
296 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
299 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
302 if (mncc->emergency) {
303 SCPY(p_dialinginfo.id, "emergency");
305 p_dialinginfo.sending_complete = 1;
307 /* bearer capability */
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;
318 ret = channel = hunt_bchannel();
323 ret = seize_bchannel(channel, 1);
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");
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);
342 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
343 if (bchannel_open(p_m_b_index))
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);
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);
356 /* create endpoint */
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);
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);
371 send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
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);
386 send_and_free_mncc(p_m_g_instance, proceeding->msg_type, proceeding);
388 new_state(PORT_STATE_IN_PROCEEDING);
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);
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;
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);
413 * BSC sends message to port
415 static int message_bsc(struct gsm_network *net, int msg_type, void *arg)
417 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
418 unsigned int callref = mncc->callref;
420 class Pgsm_bs *pgsm_bs = NULL;
422 struct mISDNport *mISDNport;
424 /* Special messages */
429 callref = mncc->callref;
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) {
441 if (msg_type == GSM_TCHF_FRAME) {
443 /* inject DTMF, if enabled */
444 if (pgsm_bs->p_m_g_dtmf) {
445 unsigned char data[160];
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;
454 pgsm_bs->bchannel_send(PH_DATA_REQ, 0, data, 160);
456 pgsm_bs->frame_receive(arg);
462 if (msg_type != MNCC_SETUP_IND)
465 mISDNport = mISDNport_first;
467 if (mISDNport->gsm_bs)
469 mISDNport = mISDNport->next;
472 struct gsm_mncc *rej;
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);
484 send_and_free_mncc(gsm->network, rej->msg_type, rej);
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)))
491 FATAL("Cannot create Port instance.\n");
496 pgsm_bs->setup_ind(msg_type, callref, mncc);
499 case MNCC_START_DTMF_IND:
500 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
503 case MNCC_STOP_DTMF_IND:
504 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
507 case MNCC_CALL_CONF_IND:
508 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
512 pgsm_bs->alert_ind(msg_type, callref, mncc);
516 pgsm_bs->setup_cnf(msg_type, callref, mncc);
519 case MNCC_SETUP_COMPL_IND:
520 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
524 pgsm_bs->disc_ind(msg_type, callref, mncc);
530 pgsm_bs->rel_ind(msg_type, callref, mncc);
533 case MNCC_NOTIFY_IND:
534 pgsm_bs->notify_ind(msg_type, callref, mncc);
538 pgsm_bs->hold_ind(msg_type, callref, mncc);
541 case MNCC_RETRIEVE_IND:
542 pgsm_bs->retr_ind(msg_type, callref, mncc);
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);
552 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
554 struct lcr_msg *message;
556 struct epoint_list *epointlist;
557 struct gsm_mncc *mncc;
560 /* copy setup infos to port */
561 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
562 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
563 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
564 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
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.");
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);
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.");
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);
595 ret = channel = hunt_bchannel();
599 ret = seize_bchannel(channel, 1);
602 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
603 add_trace("failure", NULL, "No internal audio channel available.");
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);
613 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
614 if (bchannel_open(p_m_b_index))
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);
621 /* attach only if not already */
622 epointlist = p_epointlist;
624 if (epointlist->epoint_id == epoint_id)
626 epointlist = epointlist->next;
629 epointlist_new(epoint_id);
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);
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;
646 case INFO_NTYPE_INTERNATIONAL:
647 mncc->calling.type = 0x1;
649 case INFO_NTYPE_NATIONAL:
650 mncc->calling.type = 0x2;
652 case INFO_NTYPE_SUBSCRIBER:
653 mncc->calling.type = 0x4;
655 default: /* INFO_NTYPE_NOTPRESENT */
656 mncc->fields &= ~MNCC_F_CALLING;
659 switch (p_callerinfo.screen) {
660 case INFO_SCREEN_USER:
661 mncc->calling.screen = 0;
663 default: /* INFO_SCREEN_NETWORK */
664 mncc->calling.screen = 3;
667 switch (p_callerinfo.present) {
668 case INFO_PRESENT_ALLOWED:
669 mncc->calling.present = 0;
671 case INFO_PRESENT_RESTRICTED:
672 mncc->calling.present = 1;
674 default: /* INFO_PRESENT_NOTAVAIL */
675 mncc->calling.present = 2;
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);
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);
692 SCPY(mncc->called.number, p_dialinginfo.id);
693 add_trace("dialing", "number", "%s", mncc->called.number);
696 /* sending user-user */
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;
705 case INFO_NTYPE_INTERNATIONAL:
706 mncc->redirecting.type = 0x1;
708 case INFO_NTYPE_NATIONAL:
709 mncc->redirecting.type = 0x2;
711 case INFO_NTYPE_SUBSCRIBER:
712 mncc->redirecting.type = 0x4;
714 default: /* INFO_NTYPE_NOTPRESENT */
715 mncc->fields &= ~MNCC_F_REDIRECTING;
718 switch (p_redirinfo.screen) {
719 case INFO_SCREEN_USER:
720 mncc->redirecting.screen = 0;
722 default: /* INFO_SCREE_NETWORK */
723 mncc->redirecting.screen = 3;
726 switch (p_redirinfo.present) {
727 case INFO_PRESENT_ALLOWED:
728 mncc->redirecting.present = 0;
730 case INFO_PRESENT_RESTRICTED:
731 mncc->redirecting.present = 1;
733 default: /* INFO_PRESENT_NOTAVAIL */
734 mncc->redirecting.present = 2;
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);
746 /* bearer capability */
750 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
752 new_state(PORT_STATE_OUT_SETUP);
754 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
755 message_put(message);
757 new_state(PORT_STATE_OUT_PROCEEDING);
761 * endpoint sends messages to the port
763 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
765 if (Pgsm::message_epoint(epoint_id, message_id, param))
769 case MESSAGE_SETUP: /* dial-out command received from epoint */
770 if (p_state!=PORT_STATE_IDLE)
772 message_setup(epoint_id, message_id, param);
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);
782 int gsm_bs_exit(int rc)
785 /* free gsm instance */
787 /* shutdown network */
789 bsc_shutdown_net((struct gsm_network *)gsm->network);
791 // if (gsm->network) {
792 // free((struct gsm_network *)gsm->network); /* TBD */
801 static int mncc_q_enqueue(struct gsm_mncc *mncc, unsigned int len)
803 struct mncc_q_entry *qe;
805 qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
811 memcpy(qe->data, mncc, len);
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;
818 /* append to tail of list */
819 gsm->mncc_q_tail->next = qe;
820 gsm->mncc_q_tail = qe;
823 gsm->mncc_lfd.when |= LCR_FD_WRITE;
828 static struct mncc_q_entry *mncc_q_dequeue(void)
830 struct mncc_q_entry *qe = gsm->mncc_q_hd;
834 /* dequeue the successfully sent message */
835 gsm->mncc_q_hd = qe->next;
838 if (qe == gsm->mncc_q_tail)
839 gsm->mncc_q_tail = NULL;
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)
849 /* FIXME: the caller should provide this */
852 len = sizeof(struct gsm_data_frame) + 33;
855 len = sizeof(struct gsm_mncc);
859 return mncc_q_enqueue((struct gsm_mncc *)data, len);
864 /* close MNCC socket */
865 static int mncc_fd_close(struct lcr_fd *lfd)
868 class Pgsm_bs *pgsm_bs = NULL;
869 struct lcr_msg *message;
871 PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
876 /* free all the calls that were running through the MNCC interface */
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);
891 /* flush the queue */
892 while (mncc_q_dequeue())
895 /* start the re-connect timer */
896 schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
903 /* read from OpenBSC via MNCC socket */
904 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
906 struct mncc_q_entry *qe, *qe2;
912 lfd->when &= ~LCR_FD_WRITE;
915 rc = write(lfd->fd, qe->data, qe->len);
917 return mncc_fd_close(lfd);
922 /* dequeue the successfully sent message */
923 qe2 = mncc_q_dequeue();
930 /* read from OpenBSC via MNCC socket */
931 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
934 static char buf[sizeof(struct gsm_mncc)+1024];
935 struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
937 memset(buf, 0, sizeof(buf));
938 rc = recv(lfd->fd, buf, sizeof(buf), 0);
940 return mncc_fd_close(lfd);
944 /* Hand the MNCC message into LCR */
945 return message_bsc(NULL, mncc_prim->msg_type, mncc_prim);
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)
953 if (what & LCR_FD_READ)
954 rc = mncc_fd_read(lfd, instance, idx);
958 if (what & LCR_FD_WRITE)
959 rc = mncc_fd_write(lfd, instance, idx);
964 static int socket_retry_cb(struct lcr_timer *timer, void *instance, int index)
968 fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
970 PERROR("Cannot create SEQPACKET socket, giving up!\n");
974 rc = connect(fd, (struct sockaddr *) &gsm->sun,
977 PERROR("Could not connect to MNCC socket, "
978 "retrying in %u seconds\n", SOCKET_RETRY_TIMER);
980 schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
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);
990 int gsm_bs_init(void)
992 gsm->sun.sun_family = AF_UNIX;
993 strcpy(gsm->sun.sun_path, "/tmp/bsc_mncc");
995 memset(&gsm->socket_retry, 0, sizeof(gsm->socket_retry));
996 add_timer(&gsm->socket_retry, socket_retry_cb, NULL, 0);
998 /* do the initial connect */
999 socket_retry_cb(&gsm->socket_retry, NULL, 0);