1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
20 #include <openbsc/db.h>
21 #include <openbsc/select.h>
22 #include <openbsc/debug.h>
23 #include <openbsc/e1_input.h>
24 #include <openbsc/talloc.h>
25 #include <openbsc/mncc.h>
26 #include <openbsc/trau_frame.h>
27 struct gsm_network *bsc_gsmnet = 0;
28 extern int ipacc_rtp_direct;
29 extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *),
30 const char *cfg_file);
31 extern int bsc_shutdown_net(struct gsm_network *net);
32 void talloc_ctx_init(void);
33 void on_dso_load_token(void);
34 void on_dso_load_rrlp(void);
36 #include "gsm_audio.h"
41 #define PF_ISDN AF_ISDN
45 struct lcr_gsm *gsm = NULL;
47 static unsigned int new_callref = 1;
51 * create and send mncc message
53 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
55 struct gsm_mncc *mncc;
57 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
58 mncc->msg_type = msg_type;
59 mncc->callref = callref;
62 static int send_and_free_mncc(struct gsm_network *net, unsigned int msg_type, void *data)
66 ret = mncc_send(net, msg_type, data);
76 Pgsm::Pgsm(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : PmISDN(type, mISDNport, portname, settings, channel, exclusive, mode)
78 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
81 p_m_g_gsm_b_sock = -1;
82 p_m_g_gsm_b_index = -1;
83 p_m_g_gsm_b_active = 0;
84 p_m_g_notify_pending = NULL;
85 p_m_g_decoder = gsm_audio_create();
86 p_m_g_encoder = gsm_audio_create();
87 if (!p_m_g_encoder || !p_m_g_decoder) {
88 PERROR("Failed to create GSM audio codec instance\n");
92 p_m_g_tch_connected = 0;
94 PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
102 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
104 /* remove queued message */
105 if (p_m_g_notify_pending)
106 message_free(p_m_g_notify_pending);
108 /* close audio transfer socket */
109 if (p_m_g_gsm_b_sock > -1)
114 gsm_audio_destroy(p_m_g_encoder);
116 gsm_audio_destroy(p_m_g_decoder);
120 /* close bsc side bchannel */
121 void Pgsm::bchannel_close(void)
123 if (p_m_g_gsm_b_sock > -1)
124 close(p_m_g_gsm_b_sock);
125 p_m_g_gsm_b_sock = -1;
126 p_m_g_gsm_b_index = -1;
127 p_m_g_gsm_b_active = 0;
130 /* open bsc side bchannel */
131 int Pgsm::bchannel_open(int index)
135 struct sockaddr_mISDN addr;
136 struct mISDNhead act;
138 if (p_m_g_gsm_b_sock > -1) {
139 PERROR("Socket already created for index %d\n", index);
144 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
146 PERROR("Failed to open bchannel-socket for index %d\n", index);
151 /* set nonblocking io */
152 ret = ioctl(p_m_g_gsm_b_sock, FIONBIO, &on);
154 PERROR("Failed to set bchannel-socket index %d into nonblocking IO\n", index);
159 /* bind socket to bchannel */
160 addr.family = AF_ISDN;
161 addr.dev = gsm->gsm_port;
162 addr.channel = index+1+(index>15);
163 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
165 PERROR("Failed to bind bchannel-socket for index %d\n", index);
169 /* activate bchannel */
170 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
171 act.prim = PH_ACTIVATE_REQ;
173 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
175 PERROR("Failed to activate index %d\n", index);
180 p_m_g_gsm_b_index = index;
185 /* receive from bchannel */
186 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
188 unsigned char frame[33];
190 /* encoder init failed */
194 /* (currently) not connected, so don't flood tch! */
195 if (!p_m_g_tch_connected)
198 /* write to rx buffer */
200 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
201 if (p_m_g_rxpos == 160) {
205 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
211 /* transmit to bchannel */
212 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
214 unsigned char buf[MISDN_HEADER_LEN+len];
215 struct mISDNhead *hh = (struct mISDNhead *)buf;
218 if (!p_m_g_gsm_b_active)
221 /* make and send frame */
222 hh->prim = PH_DATA_REQ;
224 memcpy(buf+MISDN_HEADER_LEN, data, len);
225 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
227 PERROR("Failed to send to socket index %d\n", index);
230 void Pgsm::frame_send(void *_frame)
232 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
233 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
235 frame->msg_type = GSM_TCHF_FRAME;
236 frame->callref = p_m_g_callref;
237 memcpy(frame->data, _frame, 33);
238 mncc_send((struct gsm_network *)gsm->network, frame->msg_type, frame);
242 void Pgsm::frame_receive(void *_frame)
244 struct gsm_data_frame *frame = (struct gsm_data_frame *)_frame;
245 signed short samples[160];
246 unsigned char data[160];
252 if ((frame->data[0]>>4) != 0xd)
253 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
256 gsm_audio_decode(p_m_g_decoder, frame->data, samples);
257 for (i = 0; i < 160; i++) {
258 data[i] = audio_s16_to_law[samples[i] & 0xffff];
262 bchannel_send(PH_DATA_REQ, 0, data, 160);
269 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
273 /* select message and primitive text */
274 SCPY(msgtext, get_mncc_name(msg_type));
277 if (direction == DIRECTION_OUT)
278 SCAT(msgtext, " MSC->BSC");
280 SCAT(msgtext, " MSC<-BSC");
282 /* init trace with given values */
283 start_trace(mISDNport?mISDNport->portnum:-1,
284 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
285 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
286 port?port->p_dialinginfo.id:NULL,
289 port?port->p_serial:0,
295 /* select bchannel */
296 int Pgsm::hunt_bchannel(void)
301 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
302 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
303 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
304 add_trace("conclusion", NULL, "all channels are reserved");
306 return(-34); // no channel
311 while(i < p_m_mISDNport->b_num) {
312 if (p_m_mISDNport->b_port[i] == NULL) {
313 channel = i+1+(i>=15);
319 add_trace("conclusion", NULL, "no channel available");
321 return(-6); // channel unacceptable
323 add_trace("conclusion", NULL, "channel available");
324 add_trace("connect", "channel", "%d", channel);
331 * handles all indications
333 /* SETUP INDICATION */
334 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
337 class Endpoint *epoint;
338 struct lcr_msg *message;
340 struct gsm_mncc *mode, *proceeding, *frame;
342 /* emergency shutdown */
343 printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
344 if (mncc->emergency && !gsm->conf.noemergshut) {
345 start_trace(p_m_mISDNport->portnum,
346 p_m_mISDNport->ifport->interface,
352 "EMERGENCY SHUTDOWN (due to received emergency call)");
356 /* process given callref */
357 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
358 add_trace("callref", "new", "0x%x", callref);
360 /* release in case the ID is already in use */
361 add_trace("error", NULL, "callref already in use");
363 mncc = create_mncc(MNCC_REJ_REQ, callref);
364 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
365 mncc->fields |= MNCC_F_CAUSE;
366 mncc->cause.coding = 3;
367 mncc->cause.location = 1;
368 mncc->cause.value = 47;
369 add_trace("cause", "coding", "%d", mncc->cause.coding);
370 add_trace("cause", "location", "%d", mncc->cause.location);
371 add_trace("cause", "value", "%d", mncc->cause.value);
372 add_trace("reason", NULL, "callref already in use");
374 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
375 new_state(PORT_STATE_RELEASE);
379 p_m_g_callref = callref;
382 /* if blocked, release call with MT_RELEASE_COMPLETE */
383 if (p_m_mISDNport->ifport->block) {
384 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
385 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
386 mncc->fields |= MNCC_F_CAUSE;
387 mncc->cause.coding = 3;
388 mncc->cause.location = 1;
389 mncc->cause.value = 27;
390 add_trace("cause", "coding", "%d", mncc->cause.coding);
391 add_trace("cause", "location", "%d", mncc->cause.location);
392 add_trace("cause", "value", "%d", mncc->cause.value);
393 add_trace("reason", NULL, "port is blocked");
395 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
396 new_state(PORT_STATE_RELEASE);
403 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
405 p_callerinfo.present = INFO_PRESENT_ALLOWED;
406 if (mncc->calling.number[0])
407 SCPY(p_callerinfo.id, mncc->calling.number);
409 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
410 SCPY(p_callerinfo.imsi, mncc->imsi);
411 p_callerinfo.screen = INFO_SCREEN_NETWORK;
412 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
413 p_callerinfo.isdn_port = p_m_portnum;
414 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
416 /* dialing information */
417 SCAT(p_dialinginfo.id, mncc->called.number);
418 switch (mncc->called.type) {
420 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
423 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
426 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
429 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
432 if (mncc->emergency) {
433 SCPY(p_dialinginfo.id, "emergency");
435 p_dialinginfo.sending_complete = 1;
437 /* bearer capability */
439 p_capainfo.bearer_capa = INFO_BC_SPEECH;
440 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
441 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
442 p_capainfo.source_mode = B_MODE_TRANSPARENT;
443 p_m_g_mode = p_capainfo.source_mode;
448 ret = channel = hunt_bchannel();
453 ret = seize_bchannel(channel, 1);
456 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
457 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
458 mncc->fields |= MNCC_F_CAUSE;
459 mncc->cause.coding = 3;
460 mncc->cause.location = 1;
461 mncc->cause.value = 34;
462 add_trace("cause", "coding", "%d", mncc->cause.coding);
463 add_trace("cause", "location", "%d", mncc->cause.location);
464 add_trace("cause", "value", "%d", mncc->cause.value);
465 add_trace("reason", NULL, "no channel");
467 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
468 new_state(PORT_STATE_RELEASE);
472 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
473 if (bchannel_open(p_m_b_index))
476 /* what infos did we got ... */
477 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
478 if (p_callerinfo.id[0])
479 add_trace("calling", "number", "%s", p_callerinfo.id);
481 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
482 add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
483 add_trace("dialing", "number", "%s", p_dialinginfo.id);
486 /* create endpoint */
488 FATAL("Incoming call but already got an endpoint.\n");
489 if (!(epoint = new Endpoint(p_serial, 0)))
490 FATAL("No memory for Endpoint instance\n");
491 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
492 FATAL("No memory for Endpoint Application instance\n");
493 epointlist_new(epoint->ep_serial);
495 /* modify lchan to GSM codec V1 */
496 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
497 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
498 mode->lchan_mode = 0x01; /* GSM V1 */
499 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
501 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
503 /* send call proceeding */
504 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
505 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
506 if (p_m_mISDNport->tones) {
507 proceeding->fields |= MNCC_F_PROGRESS;
508 proceeding->progress.coding = 3; /* GSM */
509 proceeding->progress.location = 1;
510 proceeding->progress.descr = 8;
511 add_trace("progress", "coding", "%d", proceeding->progress.coding);
512 add_trace("progress", "location", "%d", proceeding->progress.location);
513 add_trace("progress", "descr", "%d", proceeding->progress.descr);
516 send_and_free_mncc((struct gsm_network *)gsm->network, proceeding->msg_type, proceeding);
518 new_state(PORT_STATE_IN_PROCEEDING);
520 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
521 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
523 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
524 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
525 p_m_g_tch_connected = 1;
528 /* send setup message to endpoit */
529 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
530 message->param.setup.isdn_port = p_m_portnum;
531 message->param.setup.port_type = p_type;
532 // message->param.setup.dtmf = 0;
533 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
534 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
535 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
536 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
537 message->param.setup.useruser.len = strlen(mncc->useruser.info);
538 message->param.setup.useruser.protocol = mncc->useruser.proto;
539 message_put(message);
542 /* DTMF INDICATION */
543 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
545 struct lcr_msg *message;
546 struct gsm_mncc *resp;
548 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
549 add_trace("keypad", NULL, "%c", mncc->keypad);
551 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
552 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
555 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
556 add_trace("keypad", NULL, "%c", mncc->keypad);
558 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
559 resp->keypad = mncc->keypad;
560 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
562 /* send dialing information */
563 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
564 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
565 message_put(message);
567 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
569 struct gsm_mncc *resp;
571 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
572 add_trace("keypad", NULL, "%c", mncc->keypad);
576 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
577 add_trace("keypad", NULL, "%c", mncc->keypad);
579 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
580 resp->keypad = mncc->keypad;
581 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
584 /* PROCEEDING INDICATION */
585 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
587 struct gsm_mncc *mode;
589 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
590 if (mncc->fields & MNCC_F_CAUSE) {
591 add_trace("cause", "coding", "%d", mncc->cause.coding);
592 add_trace("cause", "location", "%", mncc->cause.location);
593 add_trace("cause", "value", "%", mncc->cause.value);
597 /* modify lchan to GSM codec V1 */
598 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
599 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
600 mode->lchan_mode = 0x01; /* GSM V1 */
601 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
603 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
607 /* ALERTING INDICATION */
608 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
610 struct lcr_msg *message;
612 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
615 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
616 message_put(message);
618 new_state(PORT_STATE_OUT_ALERTING);
622 /* CONNECT INDICATION */
623 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
625 struct gsm_mncc *resp, *frame;
626 struct lcr_msg *message;
628 SCPY(p_connectinfo.id, mncc->connected.number);
629 SCPY(p_connectinfo.imsi, mncc->imsi);
630 p_connectinfo.present = INFO_PRESENT_ALLOWED;
631 p_connectinfo.screen = INFO_SCREEN_NETWORK;
632 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
633 p_connectinfo.isdn_port = p_m_portnum;
634 SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
636 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
637 if (p_connectinfo.id[0])
638 add_trace("connect", "number", "%s", p_connectinfo.id);
639 else if (mncc->imsi[0])
640 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
642 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
646 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
647 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
649 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
651 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
652 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
653 message_put(message);
655 new_state(PORT_STATE_CONNECT);
657 if (!p_m_g_tch_connected) { /* only if ... */
658 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
660 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
661 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
662 p_m_g_tch_connected = 1;
666 /* CONNECT ACK INDICATION */
667 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
669 struct gsm_mncc *frame;
671 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
674 new_state(PORT_STATE_CONNECT);
676 if (!p_m_g_tch_connected) { /* only if ... */
677 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
679 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
680 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
681 p_m_g_tch_connected = 1;
685 /* DISCONNECT INDICATION */
686 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
688 struct lcr_msg *message;
689 int cause = 16, location = 0;
690 struct gsm_mncc *resp;
692 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
693 if (mncc->fields & MNCC_F_CAUSE) {
694 location = mncc->cause.location;
695 cause = mncc->cause.value;
696 add_trace("cause", "coding", "%d", mncc->cause.coding);
697 add_trace("cause", "location", "%d", location);
698 add_trace("cause", "value", "%d", cause);
703 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
704 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
706 resp->fields |= MNCC_F_CAUSE;
707 resp->cause.coding = 3;
708 resp->cause.location = 1;
709 resp->cause.value = cause;
710 add_trace("cause", "coding", "%d", resp->cause.coding);
711 add_trace("cause", "location", "%d", resp->cause.location);
712 add_trace("cause", "value", "%d", resp->cause.value);
715 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
717 /* sending release to endpoint */
718 while(p_epointlist) {
719 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
720 message->param.disconnectinfo.cause = cause;
721 message->param.disconnectinfo.location = location;
722 message_put(message);
724 free_epointlist(p_epointlist);
726 new_state(PORT_STATE_RELEASE);
730 /* CC_RELEASE INDICATION */
731 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
733 int location = 0, cause = 16;
734 struct lcr_msg *message;
736 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
737 if (mncc->fields & MNCC_F_CAUSE) {
738 location = mncc->cause.location;
739 cause = mncc->cause.value;
740 add_trace("cause", "coding", "%d", mncc->cause.coding);
741 add_trace("cause", "location", "%d", mncc->cause.location);
742 add_trace("cause", "value", "%d", mncc->cause.value);
746 /* sending release to endpoint */
747 while(p_epointlist) {
748 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
749 message->param.disconnectinfo.cause = cause;
750 message->param.disconnectinfo.location = location;
751 message_put(message);
753 free_epointlist(p_epointlist);
755 new_state(PORT_STATE_RELEASE);
759 /* NOTIFY INDICATION */
760 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
762 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
767 /* HOLD INDICATION */
768 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
770 struct lcr_msg *message;
771 struct gsm_mncc *resp, *frame;
773 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
776 /* notify the hold of call */
777 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
778 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
779 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
780 message_put(message);
782 /* acknowledge hold */
783 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
785 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
786 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
789 if (p_m_g_tch_connected) { /* it should be true */
790 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
792 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
793 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
794 p_m_g_tch_connected = 0;
799 /* RETRIEVE INDICATION */
800 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
802 struct lcr_msg *message;
803 struct gsm_mncc *resp, *frame;
805 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
808 /* notify the retrieve of call */
809 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
810 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
811 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
812 message_put(message);
814 /* acknowledge retr */
815 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
817 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
818 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
821 if (!p_m_g_tch_connected) { /* it should be true */
822 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
824 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
825 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
826 p_m_g_tch_connected = 1;
831 * BSC sends message to port
833 static int message_bsc(struct gsm_network *net, int msg_type, void *arg)
835 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
836 unsigned int callref = mncc->callref;
838 class Pgsm *pgsm = NULL;
840 struct mISDNport *mISDNport;
842 /* Special messages */
847 callref = mncc->callref;
850 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
851 pgsm = (class Pgsm *)port;
852 if (pgsm->p_m_g_callref == callref) {
859 if (msg_type == GSM_TCHF_FRAME) {
861 pgsm->frame_receive((struct gsm_trau_frame *)arg);
866 if (msg_type != MNCC_SETUP_IND)
869 mISDNport = mISDNport_first;
873 mISDNport = mISDNport->next;
876 struct gsm_mncc *rej;
878 rej = create_mncc(MNCC_REJ_REQ, callref);
879 rej->fields |= MNCC_F_CAUSE;
880 rej->cause.coding = 3;
881 rej->cause.location = 1;
882 rej->cause.value = 27;
883 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
884 add_trace("cause", "coding", "%d", rej->cause.coding);
885 add_trace("cause", "location", "%d", rej->cause.location);
886 add_trace("cause", "value", "%d", rej->cause.value);
888 send_and_free_mncc((struct gsm_network *)gsm->network, rej->msg_type, rej);
891 /* creating port object, transparent until setup with hdlc */
892 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
893 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
895 FATAL("Cannot create Port instance.\n");
900 pgsm->setup_ind(msg_type, callref, mncc);
903 case MNCC_START_DTMF_IND:
904 pgsm->start_dtmf_ind(msg_type, callref, mncc);
907 case MNCC_STOP_DTMF_IND:
908 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
911 case MNCC_CALL_CONF_IND:
912 pgsm->call_conf_ind(msg_type, callref, mncc);
916 pgsm->alert_ind(msg_type, callref, mncc);
920 pgsm->setup_cnf(msg_type, callref, mncc);
923 case MNCC_SETUP_COMPL_IND:
924 pgsm->setup_compl_ind(msg_type, callref, mncc);
928 pgsm->disc_ind(msg_type, callref, mncc);
934 pgsm->rel_ind(msg_type, callref, mncc);
937 case MNCC_NOTIFY_IND:
938 pgsm->notify_ind(msg_type, callref, mncc);
942 pgsm->hold_ind(msg_type, callref, mncc);
945 case MNCC_RETRIEVE_IND:
946 pgsm->retr_ind(msg_type, callref, mncc);
950 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: 0x%x\n", pgsm->p_name, pgsm->p_callerinfo.id, msg_type);
956 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
958 struct lcr_msg *message;
960 struct epoint_list *epointlist;
961 struct gsm_mncc *mncc;
964 /* copy setup infos to port */
965 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
966 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
967 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
968 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
971 if (!p_dialinginfo.id[0]) {
972 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
973 add_trace("failure", NULL, "No dialed subscriber given.");
975 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
976 message->param.disconnectinfo.cause = 28;
977 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
978 message_put(message);
979 new_state(PORT_STATE_RELEASE);
984 /* release if port is blocked */
985 if (p_m_mISDNport->ifport->block) {
986 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
987 add_trace("failure", NULL, "Port blocked.");
989 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
990 message->param.disconnectinfo.cause = 27; // temp. unavail.
991 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
992 message_put(message);
993 new_state(PORT_STATE_RELEASE);
999 ret = channel = hunt_bchannel();
1003 ret = seize_bchannel(channel, 1);
1006 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1007 add_trace("failure", NULL, "No internal audio channel available.");
1009 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1010 message->param.disconnectinfo.cause = 34;
1011 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1012 message_put(message);
1013 new_state(PORT_STATE_RELEASE);
1017 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1018 if (bchannel_open(p_m_b_index))
1021 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1022 /* screen outgoing caller id */
1023 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1025 /* attach only if not already */
1026 epointlist = p_epointlist;
1028 if (epointlist->epoint_id == epoint_id)
1030 epointlist = epointlist->next;
1033 epointlist_new(epoint_id);
1036 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1037 p_m_g_callref = new_callref++;
1038 add_trace("callref", "new", "0x%x", p_m_g_callref);
1041 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1042 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1043 /* caller information */
1044 mncc->fields |= MNCC_F_CALLING;
1045 mncc->calling.plan = 1;
1046 switch (p_callerinfo.ntype) {
1047 case INFO_NTYPE_UNKNOWN:
1048 mncc->calling.type = 0x0;
1050 case INFO_NTYPE_INTERNATIONAL:
1051 mncc->calling.type = 0x1;
1053 case INFO_NTYPE_NATIONAL:
1054 mncc->calling.type = 0x2;
1056 case INFO_NTYPE_SUBSCRIBER:
1057 mncc->calling.type = 0x4;
1059 default: /* INFO_NTYPE_NOTPRESENT */
1060 mncc->fields &= ~MNCC_F_CALLING;
1063 switch (p_callerinfo.screen) {
1064 case INFO_SCREEN_USER:
1065 mncc->calling.screen = 0;
1067 default: /* INFO_SCREEN_NETWORK */
1068 mncc->calling.screen = 3;
1071 switch (p_callerinfo.present) {
1072 case INFO_PRESENT_ALLOWED:
1073 mncc->calling.present = 0;
1075 case INFO_PRESENT_RESTRICTED:
1076 mncc->calling.present = 1;
1078 default: /* INFO_PRESENT_NOTAVAIL */
1079 mncc->calling.present = 2;
1082 if (mncc->fields & MNCC_F_CALLING) {
1083 SCPY(mncc->calling.number, p_callerinfo.id);
1084 add_trace("calling", "type", "%d", mncc->calling.type);
1085 add_trace("calling", "plan", "%d", mncc->calling.plan);
1086 add_trace("calling", "present", "%d", mncc->calling.present);
1087 add_trace("calling", "screen", "%d", mncc->calling.screen);
1088 add_trace("calling", "number", "%s", mncc->calling.number);
1090 /* dialing information */
1091 mncc->fields |= MNCC_F_CALLED;
1092 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
1093 SCPY(mncc->imsi, p_dialinginfo.id+5);
1094 add_trace("dialing", "imsi", "%s", mncc->imsi);
1096 SCPY(mncc->called.number, p_dialinginfo.id);
1097 add_trace("dialing", "number", "%s", mncc->called.number);
1100 /* sending user-user */
1102 /* redirecting number */
1103 mncc->fields |= MNCC_F_REDIRECTING;
1104 mncc->redirecting.plan = 1;
1105 switch (p_redirinfo.ntype) {
1106 case INFO_NTYPE_UNKNOWN:
1107 mncc->redirecting.type = 0x0;
1109 case INFO_NTYPE_INTERNATIONAL:
1110 mncc->redirecting.type = 0x1;
1112 case INFO_NTYPE_NATIONAL:
1113 mncc->redirecting.type = 0x2;
1115 case INFO_NTYPE_SUBSCRIBER:
1116 mncc->redirecting.type = 0x4;
1118 default: /* INFO_NTYPE_NOTPRESENT */
1119 mncc->fields &= ~MNCC_F_REDIRECTING;
1122 switch (p_redirinfo.screen) {
1123 case INFO_SCREEN_USER:
1124 mncc->redirecting.screen = 0;
1126 default: /* INFO_SCREE_NETWORK */
1127 mncc->redirecting.screen = 3;
1130 switch (p_redirinfo.present) {
1131 case INFO_PRESENT_ALLOWED:
1132 mncc->redirecting.present = 0;
1134 case INFO_PRESENT_RESTRICTED:
1135 mncc->redirecting.present = 1;
1137 default: /* INFO_PRESENT_NOTAVAIL */
1138 mncc->redirecting.present = 2;
1141 /* sending redirecting number only in ntmode */
1142 if (mncc->fields & MNCC_F_REDIRECTING) {
1143 SCPY(mncc->redirecting.number, p_redirinfo.id);
1144 add_trace("redir", "type", "%d", mncc->redirecting.type);
1145 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
1146 add_trace("redir", "present", "%d", mncc->redirecting.present);
1147 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
1148 add_trace("redir", "number", "%s", mncc->redirecting.number);
1150 /* bearer capability */
1154 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1156 new_state(PORT_STATE_OUT_SETUP);
1158 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1159 message_put(message);
1161 new_state(PORT_STATE_OUT_PROCEEDING);
1164 /* MESSAGE_NOTIFY */
1165 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1167 struct gsm_mncc *mncc;
1170 // printf("if = %d\n", param->notifyinfo.notify);
1171 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1172 notify = param->notifyinfo.notify & 0x7f;
1173 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1174 /* queue notification */
1175 if (p_m_g_notify_pending)
1176 message_free(p_m_g_notify_pending);
1177 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1178 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1180 /* sending notification */
1181 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1182 add_trace("notify", NULL, "%d", notify);
1184 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1185 mncc->notify = notify;
1186 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1191 /* MESSAGE_ALERTING */
1192 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1194 struct gsm_mncc *mncc;
1197 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1198 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1199 if (p_m_mISDNport->tones) {
1200 mncc->fields |= MNCC_F_PROGRESS;
1201 mncc->progress.coding = 3; /* GSM */
1202 mncc->progress.location = 1;
1203 mncc->progress.descr = 8;
1204 add_trace("progress", "coding", "%d", mncc->progress.coding);
1205 add_trace("progress", "location", "%d", mncc->progress.location);
1206 add_trace("progress", "descr", "%d", mncc->progress.descr);
1209 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1211 new_state(PORT_STATE_IN_ALERTING);
1213 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1214 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1216 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1217 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1218 p_m_g_tch_connected = 1;
1222 /* MESSAGE_CONNECT */
1223 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1225 struct gsm_mncc *mncc;
1227 /* copy connected information */
1228 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
1229 /* screen outgoing caller id */
1230 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1233 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1234 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1235 /* caller information */
1236 mncc->fields |= MNCC_F_CONNECTED;
1237 mncc->connected.plan = 1;
1238 switch (p_callerinfo.ntype) {
1239 case INFO_NTYPE_UNKNOWN:
1240 mncc->connected.type = 0x0;
1242 case INFO_NTYPE_INTERNATIONAL:
1243 mncc->connected.type = 0x1;
1245 case INFO_NTYPE_NATIONAL:
1246 mncc->connected.type = 0x2;
1248 case INFO_NTYPE_SUBSCRIBER:
1249 mncc->connected.type = 0x4;
1251 default: /* INFO_NTYPE_NOTPRESENT */
1252 mncc->fields &= ~MNCC_F_CONNECTED;
1255 switch (p_callerinfo.screen) {
1256 case INFO_SCREEN_USER:
1257 mncc->connected.screen = 0;
1259 default: /* INFO_SCREEN_NETWORK */
1260 mncc->connected.screen = 3;
1263 switch (p_callerinfo.present) {
1264 case INFO_PRESENT_ALLOWED:
1265 mncc->connected.present = 0;
1267 case INFO_PRESENT_RESTRICTED:
1268 mncc->connected.present = 1;
1270 default: /* INFO_PRESENT_NOTAVAIL */
1271 mncc->connected.present = 2;
1274 if (mncc->fields & MNCC_F_CONNECTED) {
1275 SCPY(mncc->connected.number, p_connectinfo.id);
1276 add_trace("connected", "type", "%d", mncc->connected.type);
1277 add_trace("connected", "plan", "%d", mncc->connected.plan);
1278 add_trace("connected", "present", "%d", mncc->connected.present);
1279 add_trace("connected", "screen", "%d", mncc->connected.screen);
1280 add_trace("connected", "number", "%s", mncc->connected.number);
1283 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1285 new_state(PORT_STATE_CONNECT_WAITING);
1288 /* MESSAGE_DISCONNECT */
1289 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1291 struct gsm_mncc *mncc;
1293 /* send disconnect */
1294 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1295 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1296 if (p_m_mISDNport->tones) {
1297 mncc->fields |= MNCC_F_PROGRESS;
1298 mncc->progress.coding = 3; /* GSM */
1299 mncc->progress.location = 1;
1300 mncc->progress.descr = 8;
1301 add_trace("progress", "coding", "%d", mncc->progress.coding);
1302 add_trace("progress", "location", "%d", mncc->progress.location);
1303 add_trace("progress", "descr", "%d", mncc->progress.descr);
1305 mncc->fields |= MNCC_F_CAUSE;
1306 mncc->cause.coding = 3;
1307 mncc->cause.location = param->disconnectinfo.location;
1308 mncc->cause.value = param->disconnectinfo.cause;
1309 add_trace("cause", "coding", "%d", mncc->cause.coding);
1310 add_trace("cause", "location", "%d", mncc->cause.location);
1311 add_trace("cause", "value", "%d", mncc->cause.value);
1313 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1315 new_state(PORT_STATE_OUT_DISCONNECT);
1317 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1318 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1320 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1321 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1322 p_m_g_tch_connected = 1;
1327 /* MESSAGE_RELEASE */
1328 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1330 struct gsm_mncc *mncc;
1333 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1334 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1335 mncc->fields |= MNCC_F_CAUSE;
1336 mncc->cause.coding = 3;
1337 mncc->cause.location = param->disconnectinfo.location;
1338 mncc->cause.value = param->disconnectinfo.cause;
1339 add_trace("cause", "coding", "%d", mncc->cause.coding);
1340 add_trace("cause", "location", "%d", mncc->cause.location);
1341 add_trace("cause", "value", "%d", mncc->cause.value);
1343 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1345 new_state(PORT_STATE_RELEASE);
1352 * endpoint sends messages to the port
1354 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1356 if (PmISDN::message_epoint(epoint_id, message_id, param))
1359 switch(message_id) {
1360 case MESSAGE_SETUP: /* dial-out command received from epoint */
1361 if (p_state!=PORT_STATE_IDLE)
1363 message_setup(epoint_id, message_id, param);
1366 case MESSAGE_NOTIFY: /* display and notifications */
1367 message_notify(epoint_id, message_id, param);
1370 // case MESSAGE_FACILITY: /* facility message */
1371 // message_facility(epoint_id, message_id, param);
1374 case MESSAGE_PROCEEDING: /* message not handles */
1377 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1378 if (p_state!=PORT_STATE_IN_PROCEEDING)
1380 message_alerting(epoint_id, message_id, param);
1381 if (p_m_g_notify_pending) {
1382 /* send pending notify message during connect */
1383 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1384 message_free(p_m_g_notify_pending);
1385 p_m_g_notify_pending = NULL;
1389 case MESSAGE_CONNECT: /* call of endpoint is connected */
1390 if (p_state!=PORT_STATE_IN_PROCEEDING
1391 && p_state!=PORT_STATE_IN_ALERTING)
1393 message_connect(epoint_id, message_id, param);
1394 if (p_m_g_notify_pending) {
1395 /* send pending notify message during connect */
1396 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1397 message_free(p_m_g_notify_pending);
1398 p_m_g_notify_pending = NULL;
1402 case MESSAGE_DISCONNECT: /* call has been disconnected */
1403 if (p_state!=PORT_STATE_IN_PROCEEDING
1404 && p_state!=PORT_STATE_IN_ALERTING
1405 && p_state!=PORT_STATE_OUT_SETUP
1406 && p_state!=PORT_STATE_OUT_OVERLAP
1407 && p_state!=PORT_STATE_OUT_PROCEEDING
1408 && p_state!=PORT_STATE_OUT_ALERTING
1409 && p_state!=PORT_STATE_CONNECT
1410 && p_state!=PORT_STATE_CONNECT_WAITING)
1412 message_disconnect(epoint_id, message_id, param);
1415 case MESSAGE_RELEASE: /* release isdn port */
1416 if (p_state==PORT_STATE_RELEASE)
1418 message_release(epoint_id, message_id, param);
1422 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1432 int Pgsm::handler(void)
1436 unsigned char buffer[2048+MISDN_HEADER_LEN];
1437 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1439 if ((ret = PmISDN::handler()))
1442 /* handle destruction */
1448 /* handle message from bchannel */
1449 if (p_m_g_gsm_b_sock > -1) {
1450 ret = recv(p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1451 if (ret >= (int)MISDN_HEADER_LEN) {
1453 /* we don't care about confirms, we use rx data to sync tx */
1456 /* we receive audio data, we respond to it AND we send tones */
1458 bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1460 case PH_ACTIVATE_IND:
1461 p_m_g_gsm_b_active = 1;
1463 case PH_DEACTIVATE_IND:
1464 p_m_g_gsm_b_active = 0;
1469 if (ret < 0 && errno != EWOULDBLOCK)
1470 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1479 * handles bsc select function within LCR's main loop
1481 int handle_gsm(void)
1485 ret1 = bsc_upqueue((struct gsm_network *)gsm->network);
1486 ret2 = bsc_select_main(1); /* polling */
1492 static void gsm_sock_close(void)
1494 if (gsm->gsm_sock > -1)
1495 close(gsm->gsm_sock);
1499 static int gsm_sock_open(char *portname)
1503 unsigned long on = 1;
1504 struct sockaddr_mISDN addr;
1505 struct mISDN_devinfo devinfo;
1508 /* check port counts */
1509 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1511 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1516 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1519 gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1520 if (gsm->gsm_port < 0) {
1521 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1522 return gsm->gsm_port;
1526 devinfo.id = gsm->gsm_port;
1527 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1529 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1532 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1535 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1539 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1542 if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_TE_S0)) < 0) {
1543 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1545 return gsm->gsm_sock;
1547 /* set nonblocking io */
1548 if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1549 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1553 /* bind socket to dchannel */
1554 memset(&addr, 0, sizeof(addr));
1555 addr.family = AF_ISDN;
1556 addr.dev = gsm->gsm_port;
1558 if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1559 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1568 int gsm_exit(int rc)
1570 /* free gsm instance */
1572 if (gsm->gsm_sock > -1)
1574 /* shutdown network */
1576 bsc_shutdown_net((struct gsm_network *)gsm->network);
1578 // if (gsm->network) {
1579 // free((struct gsm_network *)gsm->network); /* TBD */
1590 char hlr[128], cfg[128], filename[128];
1591 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1594 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
1596 on_dso_load_token();
1602 /* create gsm instance */
1603 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1607 if (!gsm_conf(&gsm->conf)) {
1608 PERROR("%s", gsm_conf_error);
1609 return gsm_exit(-EINVAL);
1613 if (gsm->conf.debug[0])
1614 debug_parse_category_mask(gsm->conf.debug);
1616 /* open pcap file */
1617 if (gsm->conf.pcapfile[0]) {
1618 if (gsm->conf.pcapfile[0] == '/')
1619 SCPY(filename, gsm->conf.pcapfile);
1621 SPRINT(filename, "%s/%s", CONFIG_DATA, gsm->conf.pcapfile);
1622 pcapfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode);
1624 PERROR("Failed to open file for pcap\n");
1625 return gsm_exit(-1);
1627 e1_set_pcap_fd(pcapfd);
1630 /* use RTP proxy for audio streaming */
1631 ipacc_rtp_direct = 0;
1634 if (gsm->conf.hlr[0] == '/')
1635 SCPY(hlr, gsm->conf.hlr);
1637 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1639 PERROR("GSM DB: Failed to init database '%s'. Please check the option settings.\n", hlr);
1640 return gsm_exit(-1);
1642 printf("DB: Database initialized.\n");
1644 PERROR("GSM DB: Failed to prepare database.\n");
1645 return gsm_exit(-1);
1647 printf("DB: Database prepared.\n");
1649 /* bootstrap network */
1650 if (gsm->conf.openbsc_cfg[0] == '/')
1651 SCPY(cfg, gsm->conf.openbsc_cfg);
1653 SPRINT(cfg, "%s/%s", CONFIG_DATA, gsm->conf.openbsc_cfg);
1654 rc = bsc_bootstrap_network(&message_bsc, cfg);
1656 PERROR("Failed to bootstrap GSM network.\n");
1657 return gsm_exit(-1);
1659 gsm->network = bsc_gsmnet;
1661 /* open gsm loop interface */
1662 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1663 return gsm_exit(-1);