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->fields |= MNCC_F_KEYPAD;
97 resp->keypad = mncc->keypad;
98 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
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);
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;
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;
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;
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;
128 case 'D': p_m_g_dtmf = dtmf_samples[15]; break;
130 p_m_g_dtmf_index = 0;
132 void Pgsm_bs::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
134 struct gsm_mncc *resp;
136 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
137 add_trace("keypad", NULL, "%c", mncc->keypad);
141 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
142 add_trace("keypad", NULL, "%c", mncc->keypad);
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);
152 /* HOLD INDICATION */
153 void Pgsm_bs::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
155 struct lcr_msg *message;
156 struct gsm_mncc *resp, *frame;
158 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
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);
167 /* acknowledge hold */
168 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
170 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
171 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
174 if (p_m_g_tch_connected) { /* it should be true */
175 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
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;
184 /* RETRIEVE INDICATION */
185 void Pgsm_bs::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
187 struct lcr_msg *message;
188 struct gsm_mncc *resp, *frame;
190 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
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);
199 /* acknowledge retr */
200 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
202 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
203 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
206 if (!p_m_g_tch_connected) { /* it should be true */
207 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
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;
216 * handles all indications
218 /* SETUP INDICATION */
219 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
222 class Endpoint *epoint;
223 struct lcr_msg *message;
225 struct gsm_mncc *mode, *proceeding, *frame;
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);
231 /* release in case the ID is already in use */
232 add_trace("error", NULL, "callref already in use");
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");
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);
250 p_m_g_callref = callref;
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");
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);
274 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
276 p_callerinfo.present = INFO_PRESENT_ALLOWED;
277 if (mncc->calling.number[0])
278 SCPY(p_callerinfo.id, mncc->calling.number);
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);
287 /* dialing information */
288 SCAT(p_dialinginfo.id, mncc->called.number);
289 switch (mncc->called.type) {
291 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
294 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
297 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
300 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
303 if (mncc->emergency) {
304 SCPY(p_dialinginfo.id, "emergency");
306 p_dialinginfo.sending_complete = 1;
308 /* bearer capability */
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;
319 ret = channel = hunt_bchannel();
324 ret = seize_bchannel(channel, 1);
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");
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);
343 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
344 if (bchannel_open(p_m_b_index))
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);
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);
357 /* create endpoint */
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);
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);
372 send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
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);
387 send_and_free_mncc(p_m_g_instance, proceeding->msg_type, proceeding);
389 new_state(PORT_STATE_IN_PROCEEDING);
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);
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;
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);
414 * BSC sends message to port
416 static int message_bsc(struct gsm_network *net, int msg_type, void *arg)
418 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
419 unsigned int callref = mncc->callref;
421 class Pgsm_bs *pgsm_bs = NULL;
423 struct mISDNport *mISDNport;
425 /* Special messages */
430 callref = mncc->callref;
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) {
442 if (msg_type == GSM_TCHF_FRAME) {
444 /* inject DTMF, if enabled */
445 if (pgsm_bs->p_m_g_dtmf) {
446 unsigned char data[160];
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;
455 pgsm_bs->bchannel_send(PH_DATA_REQ, 0, data, 160);
457 pgsm_bs->frame_receive(arg);
463 if (msg_type != MNCC_SETUP_IND)
466 mISDNport = mISDNport_first;
468 if (mISDNport->gsm_bs)
470 mISDNport = mISDNport->next;
473 struct gsm_mncc *rej;
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);
485 send_and_free_mncc(gsm->network, rej->msg_type, rej);
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)))
492 FATAL("Cannot create Port instance.\n");
497 pgsm_bs->setup_ind(msg_type, callref, mncc);
500 case MNCC_START_DTMF_IND:
501 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
504 case MNCC_STOP_DTMF_IND:
505 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
508 case MNCC_CALL_CONF_IND:
509 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
513 pgsm_bs->alert_ind(msg_type, callref, mncc);
517 pgsm_bs->setup_cnf(msg_type, callref, mncc);
520 case MNCC_SETUP_COMPL_IND:
521 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
525 pgsm_bs->disc_ind(msg_type, callref, mncc);
531 pgsm_bs->rel_ind(msg_type, callref, mncc);
534 case MNCC_NOTIFY_IND:
535 pgsm_bs->notify_ind(msg_type, callref, mncc);
539 pgsm_bs->hold_ind(msg_type, callref, mncc);
542 case MNCC_RETRIEVE_IND:
543 pgsm_bs->retr_ind(msg_type, callref, mncc);
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);
553 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
555 struct lcr_msg *message;
557 struct epoint_list *epointlist;
558 struct gsm_mncc *mncc;
561 /* copy setup infos to port */
562 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
563 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
564 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
565 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
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.");
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);
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.");
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);
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.");
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 ret = channel = hunt_bchannel();
614 ret = seize_bchannel(channel, 1);
617 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
618 add_trace("failure", NULL, "No internal audio channel available.");
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);
628 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
629 if (bchannel_open(p_m_b_index))
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);
636 /* attach only if not already */
637 epointlist = p_epointlist;
639 if (epointlist->epoint_id == epoint_id)
641 epointlist = epointlist->next;
644 epointlist_new(epoint_id);
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);
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;
661 case INFO_NTYPE_INTERNATIONAL:
662 mncc->calling.type = 0x1;
664 case INFO_NTYPE_NATIONAL:
665 mncc->calling.type = 0x2;
667 case INFO_NTYPE_SUBSCRIBER:
668 mncc->calling.type = 0x4;
670 default: /* INFO_NTYPE_NOTPRESENT */
671 mncc->fields &= ~MNCC_F_CALLING;
674 switch (p_callerinfo.screen) {
675 case INFO_SCREEN_USER:
676 mncc->calling.screen = 0;
678 default: /* INFO_SCREEN_NETWORK */
679 mncc->calling.screen = 3;
682 switch (p_callerinfo.present) {
683 case INFO_PRESENT_ALLOWED:
684 mncc->calling.present = 0;
686 case INFO_PRESENT_RESTRICTED:
687 mncc->calling.present = 1;
689 default: /* INFO_PRESENT_NOTAVAIL */
690 mncc->calling.present = 2;
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);
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);
707 SCPY(mncc->called.number, p_dialinginfo.id);
708 add_trace("dialing", "number", "%s", mncc->called.number);
711 /* sending user-user */
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;
720 case INFO_NTYPE_INTERNATIONAL:
721 mncc->redirecting.type = 0x1;
723 case INFO_NTYPE_NATIONAL:
724 mncc->redirecting.type = 0x2;
726 case INFO_NTYPE_SUBSCRIBER:
727 mncc->redirecting.type = 0x4;
729 default: /* INFO_NTYPE_NOTPRESENT */
730 mncc->fields &= ~MNCC_F_REDIRECTING;
733 switch (p_redirinfo.screen) {
734 case INFO_SCREEN_USER:
735 mncc->redirecting.screen = 0;
737 default: /* INFO_SCREE_NETWORK */
738 mncc->redirecting.screen = 3;
741 switch (p_redirinfo.present) {
742 case INFO_PRESENT_ALLOWED:
743 mncc->redirecting.present = 0;
745 case INFO_PRESENT_RESTRICTED:
746 mncc->redirecting.present = 1;
748 default: /* INFO_PRESENT_NOTAVAIL */
749 mncc->redirecting.present = 2;
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);
761 /* bearer capability */
765 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
767 new_state(PORT_STATE_OUT_SETUP);
769 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
770 message_put(message);
772 new_state(PORT_STATE_OUT_PROCEEDING);
776 * endpoint sends messages to the port
778 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
780 if (Pgsm::message_epoint(epoint_id, message_id, param))
784 case MESSAGE_SETUP: /* dial-out command received from epoint */
785 if (p_state!=PORT_STATE_IDLE)
787 message_setup(epoint_id, message_id, param);
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);
797 int gsm_bs_exit(int rc)
800 /* free gsm instance */
802 /* shutdown network */
804 bsc_shutdown_net((struct gsm_network *)gsm->network);
806 // if (gsm->network) {
807 // free((struct gsm_network *)gsm->network); /* TBD */
816 static int mncc_q_enqueue(struct gsm_mncc *mncc, unsigned int len)
818 struct mncc_q_entry *qe;
820 qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
826 memcpy(qe->data, mncc, len);
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;
833 /* append to tail of list */
834 gsm->mncc_q_tail->next = qe;
835 gsm->mncc_q_tail = qe;
838 gsm->mncc_lfd.when |= LCR_FD_WRITE;
843 static struct mncc_q_entry *mncc_q_dequeue(void)
845 struct mncc_q_entry *qe = gsm->mncc_q_hd;
849 /* dequeue the successfully sent message */
850 gsm->mncc_q_hd = qe->next;
853 if (qe == gsm->mncc_q_tail)
854 gsm->mncc_q_tail = NULL;
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)
864 /* FIXME: the caller should provide this */
867 len = sizeof(struct gsm_data_frame) + 33;
870 len = sizeof(struct gsm_mncc);
874 return mncc_q_enqueue((struct gsm_mncc *)data, len);
879 /* close MNCC socket */
880 static int mncc_fd_close(struct lcr_fd *lfd)
883 class Pgsm_bs *pgsm_bs = NULL;
884 struct lcr_msg *message;
886 PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
891 /* free all the calls that were running through the MNCC interface */
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);
906 /* flush the queue */
907 while (mncc_q_dequeue())
910 /* start the re-connect timer */
911 schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
918 /* read from OpenBSC via MNCC socket */
919 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
921 struct mncc_q_entry *qe, *qe2;
927 lfd->when &= ~LCR_FD_WRITE;
930 rc = write(lfd->fd, qe->data, qe->len);
932 return mncc_fd_close(lfd);
935 if (rc < (int)qe->len)
937 /* dequeue the successfully sent message */
938 qe2 = mncc_q_dequeue();
945 /* read from OpenBSC via MNCC socket */
946 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
949 static char buf[sizeof(struct gsm_mncc)+1024];
950 struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
952 memset(buf, 0, sizeof(buf));
953 rc = recv(lfd->fd, buf, sizeof(buf), 0);
955 return mncc_fd_close(lfd);
959 /* Hand the MNCC message into LCR */
960 return message_bsc(NULL, mncc_prim->msg_type, mncc_prim);
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)
968 if (what & LCR_FD_READ)
969 rc = mncc_fd_read(lfd, instance, idx);
973 if (what & LCR_FD_WRITE)
974 rc = mncc_fd_write(lfd, instance, idx);
979 static int socket_retry_cb(struct lcr_timer *timer, void *instance, int index)
983 fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
985 PERROR("Cannot create SEQPACKET socket, giving up!\n");
989 rc = connect(fd, (struct sockaddr *) &gsm->sun,
992 PERROR("Could not connect to MNCC socket, "
993 "retrying in %u seconds\n", SOCKET_RETRY_TIMER);
995 schedule_timer(&gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
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);
1005 int gsm_bs_init(void)
1007 gsm->sun.sun_family = AF_UNIX;
1008 strcpy(gsm->sun.sun_path, "/tmp/bsc_mncc");
1010 memset(&gsm->socket_retry, 0, sizeof(gsm->socket_retry));
1011 add_timer(&gsm->socket_retry, socket_retry_cb, NULL, 0);
1013 /* do the initial connect */
1014 socket_retry_cb(&gsm->socket_retry, NULL, 0);