1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
15 #include "openbsc/mncc.h"
16 #include "openbsc/trau_frame.h"
17 #include "openbsc/select.h"
18 #include "openbsc/debug.h"
19 #include "openbsc/e1_input.h"
20 #include "bootstrap.h"
21 #include "gsm_audio.h"
26 #define PF_ISDN AF_ISDN
30 struct lcr_gsm *gsm = NULL;
32 static unsigned int new_callref = 1;
36 * create and send mncc message
38 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
40 struct gsm_mncc *mncc;
42 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
43 mncc->msg_type = msg_type;
44 mncc->callref = callref;
47 static int send_and_free_mncc(struct gsm_network *net, unsigned int msg_type, void *data)
51 ret = mncc_send(net, msg_type, data);
61 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)
63 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
66 p_m_g_gsm_b_sock = -1;
67 p_m_g_gsm_b_index = -1;
68 p_m_g_gsm_b_active = 0;
69 p_m_g_notify_pending = NULL;
70 p_m_g_decoder = gsm_audio_create();
71 p_m_g_encoder = gsm_audio_create();
72 if (!p_m_g_encoder || !p_m_g_decoder) {
73 PERROR("Failed to create GSM audio codec instance\n");
77 p_m_g_tch_connected = 0;
79 PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
87 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
89 /* remove queued message */
90 if (p_m_g_notify_pending)
91 message_free(p_m_g_notify_pending);
93 /* close audio transfer socket */
94 if (p_m_g_gsm_b_sock > -1)
99 gsm_audio_destroy(p_m_g_encoder);
101 gsm_audio_destroy(p_m_g_decoder);
105 /* close bsc side bchannel */
106 void Pgsm::bchannel_close(void)
108 if (p_m_g_gsm_b_sock > -1)
109 close(p_m_g_gsm_b_sock);
110 p_m_g_gsm_b_sock = -1;
111 p_m_g_gsm_b_index = -1;
112 p_m_g_gsm_b_active = 0;
115 /* open bsc side bchannel */
116 int Pgsm::bchannel_open(int index)
120 struct sockaddr_mISDN addr;
121 struct mISDNhead act;
123 if (p_m_g_gsm_b_sock > -1) {
124 PERROR("Socket already created for index %d\n", index);
129 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
131 PERROR("Failed to open bchannel-socket for index %d\n", index);
136 /* set nonblocking io */
137 ret = ioctl(p_m_g_gsm_b_sock, FIONBIO, &on);
139 PERROR("Failed to set bchannel-socket index %d into nonblocking IO\n", index);
144 /* bind socket to bchannel */
145 addr.family = AF_ISDN;
146 addr.dev = gsm->gsm_port;
147 addr.channel = index+1+(index>15);
148 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
150 PERROR("Failed to bind bchannel-socket for index %d\n", index);
154 /* activate bchannel */
155 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
156 act.prim = PH_ACTIVATE_REQ;
158 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
160 PERROR("Failed to activate index %d\n", index);
165 p_m_g_gsm_b_index = index;
170 /* receive from bchannel */
171 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
174 unsigned char frame[33];
175 struct decoded_trau_frame tf;
177 /* encoder init failed */
181 /* (currently) not connected, so don't flood tch! */
182 if (!p_m_g_tch_connected)
185 /* write to rx buffer */
187 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
188 if (p_m_g_rxpos == 160) {
192 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
194 /* set c-bits and t-bits */
200 memset(&tf.c_bits[5], 0, 6);
201 memset(&tf.c_bits[11], 1, 10);
202 memset(&tf.t_bits[0], 1, 4);
204 /* reassemble d-bits */
209 tf.d_bits[i] = (frame[j] >> k) & 1;
222 /* transmit to bchannel */
223 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
225 unsigned char buf[MISDN_HEADER_LEN+len];
226 struct mISDNhead *hh = (struct mISDNhead *)buf;
229 if (!p_m_g_gsm_b_active)
232 /* make and send frame */
233 hh->prim = PH_DATA_REQ;
235 memcpy(buf+MISDN_HEADER_LEN, data, len);
236 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
238 PERROR("Failed to send to socket index %d\n", index);
241 void Pgsm::trau_send(void *_tf)
243 struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_tf;
244 unsigned char data[sizeof(struct gsm_trau_frame) + sizeof(struct decoded_trau_frame)];
245 struct gsm_trau_frame *frame = (struct gsm_trau_frame *)data;
247 frame->msg_type = GSM_TRAU_FRAME;
248 frame->callref = p_m_g_callref;
249 memcpy(frame->data, tf, sizeof(struct decoded_trau_frame));
250 mncc_send((struct gsm_network *)gsm->network, frame->msg_type, frame);
254 void Pgsm::trau_receive(void *_frame)
256 struct gsm_trau_frame *frm = (struct gsm_trau_frame *)_frame;
257 struct decoded_trau_frame *tf = (struct decoded_trau_frame *)frm->data;
258 //struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_frame;
259 unsigned char frame[33];
260 signed short samples[160];
261 unsigned char data[160];
267 // printf("got trau %d %d %d %d %d\n", tf->c_bits[0], tf->c_bits[1], tf->c_bits[2], tf->c_bits[3], tf->c_bits[4]);
268 if (tf->c_bits[0]!=0 || tf->c_bits[1]!=0 || tf->c_bits[2]!=0 || tf->c_bits[3]!=1 || tf->c_bits[4]!=0)
269 PERROR("illegal trau (C1-C5) %d %d %d %d %d\n", tf->c_bits[0], tf->c_bits[1], tf->c_bits[2], tf->c_bits[3], tf->c_bits[4]);
272 memset(&frame, 0, sizeof(frame));
273 // frame[0] = 0xd << 4;
275 /* reassemble bits */
280 if (tf->d_bits[i] > 1)
282 frame[j] |= (tf->d_bits[i] << k);
291 gsm_audio_decode(p_m_g_decoder, frame, samples);
292 for (i = 0; i < 160; i++) {
293 data[i] = audio_s16_to_law[samples[i] & 0xffff];
297 bchannel_send(PH_DATA_REQ, 0, data, 160);
304 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
308 /* select message and primitive text */
309 SCPY(msgtext, get_mncc_name(msg_type));
312 if (direction == DIRECTION_OUT)
313 SCAT(msgtext, " MSC->BSC");
315 SCAT(msgtext, " MSC<-BSC");
317 /* init trace with given values */
318 start_trace(mISDNport?mISDNport->portnum:-1,
319 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
320 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
321 port?port->p_dialinginfo.id:NULL,
324 port?port->p_serial:0,
330 /* select bchannel */
331 int Pgsm::hunt_bchannel(void)
336 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
337 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
338 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
339 add_trace("conclusion", NULL, "all channels are reserved");
341 return(-34); // no channel
346 while(i < p_m_mISDNport->b_num) {
347 if (p_m_mISDNport->b_port[i] == NULL) {
348 channel = i+1+(i>=15);
354 add_trace("conclusion", NULL, "no channel available");
356 return(-6); // channel unacceptable
358 add_trace("conclusion", NULL, "channel available");
359 add_trace("connect", "channel", "%d", channel);
366 * handles all indications
368 /* SETUP INDICATION */
369 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
372 class Endpoint *epoint;
373 struct lcr_msg *message;
375 struct gsm_mncc *mode, *proceeding, *frame;
377 /* emergency shutdown */
378 printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
379 if (mncc->emergency && !gsm->conf.noemergshut) {
380 start_trace(p_m_mISDNport->portnum,
381 p_m_mISDNport->ifport->interface,
387 "EMERGENCY SHUTDOWN (due to received emergency call)");
391 /* process given callref */
392 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
393 add_trace("callref", "new", "0x%x", callref);
395 /* release in case the ID is already in use */
396 add_trace("error", NULL, "callref already in use");
398 mncc = create_mncc(MNCC_REJ_REQ, callref);
399 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
400 mncc->fields |= MNCC_F_CAUSE;
401 mncc->cause.coding = 3;
402 mncc->cause.location = 1;
403 mncc->cause.value = 47;
404 add_trace("cause", "coding", "%d", mncc->cause.coding);
405 add_trace("cause", "location", "%d", mncc->cause.location);
406 add_trace("cause", "value", "%d", mncc->cause.value);
407 add_trace("reason", NULL, "callref already in use");
409 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
410 new_state(PORT_STATE_RELEASE);
414 p_m_g_callref = callref;
417 /* if blocked, release call with MT_RELEASE_COMPLETE */
418 if (p_m_mISDNport->ifport->block) {
419 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
420 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
421 mncc->fields |= MNCC_F_CAUSE;
422 mncc->cause.coding = 3;
423 mncc->cause.location = 1;
424 mncc->cause.value = 27;
425 add_trace("cause", "coding", "%d", mncc->cause.coding);
426 add_trace("cause", "location", "%d", mncc->cause.location);
427 add_trace("cause", "value", "%d", mncc->cause.value);
428 add_trace("reason", NULL, "port is blocked");
430 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
431 new_state(PORT_STATE_RELEASE);
438 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
440 p_callerinfo.present = INFO_PRESENT_ALLOWED;
441 if (mncc->calling.number[0])
442 SCPY(p_callerinfo.id, mncc->calling.number);
444 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
445 SCPY(p_callerinfo.imsi, mncc->imsi);
446 p_callerinfo.screen = INFO_SCREEN_NETWORK;
447 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
448 p_callerinfo.isdn_port = p_m_portnum;
449 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
451 /* dialing information */
452 SCAT(p_dialinginfo.id, mncc->called.number);
453 switch (mncc->called.type) {
455 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
458 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
461 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
464 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
467 if (mncc->emergency) {
468 SCPY(p_dialinginfo.id, "emergency");
470 p_dialinginfo.sending_complete = 1;
472 /* bearer capability */
474 p_capainfo.bearer_capa = INFO_BC_SPEECH;
475 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
476 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
477 p_capainfo.source_mode = B_MODE_TRANSPARENT;
478 p_m_g_mode = p_capainfo.source_mode;
483 ret = channel = hunt_bchannel();
488 ret = seize_bchannel(channel, 1);
491 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
492 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
493 mncc->fields |= MNCC_F_CAUSE;
494 mncc->cause.coding = 3;
495 mncc->cause.location = 1;
496 mncc->cause.value = 34;
497 add_trace("cause", "coding", "%d", mncc->cause.coding);
498 add_trace("cause", "location", "%d", mncc->cause.location);
499 add_trace("cause", "value", "%d", mncc->cause.value);
500 add_trace("reason", NULL, "no channel");
502 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
503 new_state(PORT_STATE_RELEASE);
507 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
508 if (bchannel_open(p_m_b_index))
511 /* what infos did we got ... */
512 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
513 if (p_callerinfo.id[0])
514 add_trace("calling", "number", "%s", p_callerinfo.id);
516 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
517 add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
518 add_trace("dialing", "number", "%s", p_dialinginfo.id);
521 /* create endpoint */
523 FATAL("Incoming call but already got an endpoint.\n");
524 if (!(epoint = new Endpoint(p_serial, 0)))
525 FATAL("No memory for Endpoint instance\n");
526 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
527 FATAL("No memory for Endpoint Application instance\n");
528 epointlist_new(epoint->ep_serial);
530 /* modify lchan to GSM codec V1 */
531 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
532 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
533 mode->lchan_mode = 0x01; /* GSM V1 */
534 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
536 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
538 /* send call proceeding */
539 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
540 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
541 if (p_m_mISDNport->tones) {
542 proceeding->fields |= MNCC_F_PROGRESS;
543 proceeding->progress.coding = 3; /* GSM */
544 proceeding->progress.location = 1;
545 proceeding->progress.descr = 8;
546 add_trace("progress", "coding", "%d", proceeding->progress.coding);
547 add_trace("progress", "location", "%d", proceeding->progress.location);
548 add_trace("progress", "descr", "%d", proceeding->progress.descr);
551 send_and_free_mncc((struct gsm_network *)gsm->network, proceeding->msg_type, proceeding);
553 new_state(PORT_STATE_IN_PROCEEDING);
555 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
556 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
558 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
559 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
560 p_m_g_tch_connected = 1;
563 /* send setup message to endpoit */
564 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
565 message->param.setup.isdn_port = p_m_portnum;
566 message->param.setup.port_type = p_type;
567 // message->param.setup.dtmf = 0;
568 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
569 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
570 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
571 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
572 message->param.setup.useruser.len = strlen(mncc->useruser.info);
573 message->param.setup.useruser.protocol = mncc->useruser.proto;
574 message_put(message);
577 /* DTMF INDICATION */
578 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
580 struct lcr_msg *message;
581 struct gsm_mncc *resp;
583 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
584 add_trace("keypad", NULL, "%c", mncc->keypad);
586 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
587 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
590 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
591 add_trace("keypad", NULL, "%c", mncc->keypad);
593 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
594 resp->keypad = mncc->keypad;
595 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
597 /* send dialing information */
598 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
599 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
600 message_put(message);
602 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
604 struct gsm_mncc *resp;
606 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
607 add_trace("keypad", NULL, "%c", mncc->keypad);
611 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
612 add_trace("keypad", NULL, "%c", mncc->keypad);
614 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
615 resp->keypad = mncc->keypad;
616 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
619 /* PROCEEDING INDICATION */
620 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
622 struct gsm_mncc *mode;
624 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
625 if (mncc->fields & MNCC_F_CAUSE) {
626 add_trace("cause", "coding", "%d", mncc->cause.coding);
627 add_trace("cause", "location", "%", mncc->cause.location);
628 add_trace("cause", "value", "%", mncc->cause.value);
632 /* modify lchan to GSM codec V1 */
633 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
634 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
635 mode->lchan_mode = 0x01; /* GSM V1 */
636 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
638 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
642 /* ALERTING INDICATION */
643 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
645 struct lcr_msg *message;
647 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
650 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
651 message_put(message);
653 new_state(PORT_STATE_OUT_ALERTING);
657 /* CONNECT INDICATION */
658 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
660 struct gsm_mncc *resp, *frame;
661 struct lcr_msg *message;
663 SCPY(p_connectinfo.id, mncc->connected.number);
664 SCPY(p_connectinfo.imsi, mncc->imsi);
665 p_connectinfo.present = INFO_PRESENT_ALLOWED;
666 p_connectinfo.screen = INFO_SCREEN_NETWORK;
667 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
668 p_connectinfo.isdn_port = p_m_portnum;
669 SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
671 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
672 if (p_connectinfo.id[0])
673 add_trace("connect", "number", "%s", p_connectinfo.id);
674 else if (mncc->imsi[0])
675 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
677 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
681 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
682 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
684 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
686 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
687 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
688 message_put(message);
690 new_state(PORT_STATE_CONNECT);
692 if (!p_m_g_tch_connected) { /* only if ... */
693 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
695 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
696 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
697 p_m_g_tch_connected = 1;
701 /* CONNECT ACK INDICATION */
702 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
704 struct gsm_mncc *frame;
706 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
709 new_state(PORT_STATE_CONNECT);
711 if (!p_m_g_tch_connected) { /* only if ... */
712 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
714 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
715 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
716 p_m_g_tch_connected = 1;
720 /* DISCONNECT INDICATION */
721 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
723 struct lcr_msg *message;
724 int cause = 16, location = 0;
725 struct gsm_mncc *resp;
727 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
728 if (mncc->fields & MNCC_F_CAUSE) {
729 location = mncc->cause.location;
730 cause = mncc->cause.value;
731 add_trace("cause", "coding", "%d", mncc->cause.coding);
732 add_trace("cause", "location", "%d", location);
733 add_trace("cause", "value", "%d", cause);
738 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
739 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
741 resp->fields |= MNCC_F_CAUSE;
742 resp->cause.coding = 3;
743 resp->cause.location = 1;
744 resp->cause.value = cause;
745 add_trace("cause", "coding", "%d", resp->cause.coding);
746 add_trace("cause", "location", "%d", resp->cause.location);
747 add_trace("cause", "value", "%d", resp->cause.value);
750 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
752 /* sending release to endpoint */
753 while(p_epointlist) {
754 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
755 message->param.disconnectinfo.cause = cause;
756 message->param.disconnectinfo.location = location;
757 message_put(message);
759 free_epointlist(p_epointlist);
761 new_state(PORT_STATE_RELEASE);
765 /* CC_RELEASE INDICATION */
766 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
768 int location = 0, cause = 16;
769 struct lcr_msg *message;
771 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
772 if (mncc->fields & MNCC_F_CAUSE) {
773 location = mncc->cause.location;
774 cause = mncc->cause.value;
775 add_trace("cause", "coding", "%d", mncc->cause.coding);
776 add_trace("cause", "location", "%d", mncc->cause.location);
777 add_trace("cause", "value", "%d", mncc->cause.value);
781 /* sending release to endpoint */
782 while(p_epointlist) {
783 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
784 message->param.disconnectinfo.cause = cause;
785 message->param.disconnectinfo.location = location;
786 message_put(message);
788 free_epointlist(p_epointlist);
790 new_state(PORT_STATE_RELEASE);
794 /* NOTIFY INDICATION */
795 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
797 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
802 /* HOLD INDICATION */
803 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
805 struct lcr_msg *message;
806 struct gsm_mncc *resp, *frame;
808 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
811 /* notify the hold of call */
812 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
813 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
814 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
815 message_put(message);
817 /* acknowledge hold */
818 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
820 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
821 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
824 if (p_m_g_tch_connected) { /* it should be true */
825 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
827 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
828 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
829 p_m_g_tch_connected = 0;
834 /* RETRIEVE INDICATION */
835 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
837 struct lcr_msg *message;
838 struct gsm_mncc *resp, *frame;
840 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
843 /* notify the retrieve of call */
844 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
845 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
846 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
847 message_put(message);
849 /* acknowledge retr */
850 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
852 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
853 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
856 if (!p_m_g_tch_connected) { /* it should be true */
857 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
859 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
860 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
861 p_m_g_tch_connected = 1;
866 * BSC sends message to port
868 static int message_bcs(struct gsm_network *net, int msg_type, void *arg)
870 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
871 unsigned int callref = mncc->callref;
873 class Pgsm *pgsm = NULL;
875 struct mISDNport *mISDNport;
877 /* Special messages */
882 callref = mncc->callref;
885 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
886 pgsm = (class Pgsm *)port;
887 if (pgsm->p_m_g_callref == callref) {
894 if (msg_type == GSM_TRAU_FRAME) {
896 pgsm->trau_receive((struct gsm_trau_frame *)arg);
901 if (msg_type != MNCC_SETUP_IND)
904 mISDNport = mISDNport_first;
908 mISDNport = mISDNport->next;
911 struct gsm_mncc *rej;
913 rej = create_mncc(MNCC_REJ_REQ, callref);
914 rej->fields |= MNCC_F_CAUSE;
915 rej->cause.coding = 3;
916 rej->cause.location = 1;
917 rej->cause.value = 27;
918 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
919 add_trace("cause", "coding", "%d", rej->cause.coding);
920 add_trace("cause", "location", "%d", rej->cause.location);
921 add_trace("cause", "value", "%d", rej->cause.value);
923 send_and_free_mncc((struct gsm_network *)gsm->network, rej->msg_type, rej);
926 /* creating port object, transparent until setup with hdlc */
927 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
928 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
930 FATAL("Cannot create Port instance.\n");
935 pgsm->setup_ind(msg_type, callref, mncc);
938 case MNCC_START_DTMF_IND:
939 pgsm->start_dtmf_ind(msg_type, callref, mncc);
942 case MNCC_STOP_DTMF_IND:
943 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
946 case MNCC_CALL_CONF_IND:
947 pgsm->call_conf_ind(msg_type, callref, mncc);
951 pgsm->alert_ind(msg_type, callref, mncc);
955 pgsm->setup_cnf(msg_type, callref, mncc);
958 case MNCC_SETUP_COMPL_IND:
959 pgsm->setup_compl_ind(msg_type, callref, mncc);
963 pgsm->disc_ind(msg_type, callref, mncc);
969 pgsm->rel_ind(msg_type, callref, mncc);
972 case MNCC_NOTIFY_IND:
973 pgsm->notify_ind(msg_type, callref, mncc);
977 pgsm->hold_ind(msg_type, callref, mncc);
980 case MNCC_RETRIEVE_IND:
981 pgsm->retr_ind(msg_type, callref, mncc);
985 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);
991 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
993 struct lcr_msg *message;
995 struct epoint_list *epointlist;
996 struct gsm_mncc *mncc;
999 /* copy setup infos to port */
1000 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
1001 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
1002 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
1003 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1006 if (!p_dialinginfo.id[0]) {
1007 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1008 add_trace("failure", NULL, "No dialed subscriber given.");
1010 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1011 message->param.disconnectinfo.cause = 28;
1012 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1013 message_put(message);
1014 new_state(PORT_STATE_RELEASE);
1019 /* release if port is blocked */
1020 if (p_m_mISDNport->ifport->block) {
1021 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1022 add_trace("failure", NULL, "Port blocked.");
1024 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1025 message->param.disconnectinfo.cause = 27; // temp. unavail.
1026 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1027 message_put(message);
1028 new_state(PORT_STATE_RELEASE);
1034 ret = channel = hunt_bchannel();
1038 ret = seize_bchannel(channel, 1);
1041 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1042 add_trace("failure", NULL, "No internal audio channel available.");
1044 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1045 message->param.disconnectinfo.cause = 34;
1046 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1047 message_put(message);
1048 new_state(PORT_STATE_RELEASE);
1052 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1053 if (bchannel_open(p_m_b_index))
1056 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1057 /* screen outgoing caller id */
1058 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1060 /* attach only if not already */
1061 epointlist = p_epointlist;
1063 if (epointlist->epoint_id == epoint_id)
1065 epointlist = epointlist->next;
1068 epointlist_new(epoint_id);
1071 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1072 p_m_g_callref = new_callref++;
1073 add_trace("callref", "new", "0x%x", p_m_g_callref);
1076 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1077 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1078 /* caller information */
1079 mncc->fields |= MNCC_F_CALLING;
1080 mncc->calling.plan = 1;
1081 switch (p_callerinfo.ntype) {
1082 case INFO_NTYPE_UNKNOWN:
1083 mncc->calling.type = 0x0;
1085 case INFO_NTYPE_INTERNATIONAL:
1086 mncc->calling.type = 0x1;
1088 case INFO_NTYPE_NATIONAL:
1089 mncc->calling.type = 0x2;
1091 case INFO_NTYPE_SUBSCRIBER:
1092 mncc->calling.type = 0x4;
1094 default: /* INFO_NTYPE_NOTPRESENT */
1095 mncc->fields &= ~MNCC_F_CALLING;
1098 switch (p_callerinfo.screen) {
1099 case INFO_SCREEN_USER:
1100 mncc->calling.screen = 0;
1102 default: /* INFO_SCREEN_NETWORK */
1103 mncc->calling.screen = 3;
1106 switch (p_callerinfo.present) {
1107 case INFO_PRESENT_ALLOWED:
1108 mncc->calling.present = 0;
1110 case INFO_PRESENT_RESTRICTED:
1111 mncc->calling.present = 1;
1113 default: /* INFO_PRESENT_NOTAVAIL */
1114 mncc->calling.present = 2;
1117 if (mncc->fields & MNCC_F_CALLING) {
1118 SCPY(mncc->calling.number, p_callerinfo.id);
1119 add_trace("calling", "type", "%d", mncc->calling.type);
1120 add_trace("calling", "plan", "%d", mncc->calling.plan);
1121 add_trace("calling", "present", "%d", mncc->calling.present);
1122 add_trace("calling", "screen", "%d", mncc->calling.screen);
1123 add_trace("calling", "number", "%s", mncc->calling.number);
1125 /* dialing information */
1126 mncc->fields |= MNCC_F_CALLED;
1127 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
1128 SCPY(mncc->imsi, p_dialinginfo.id+5);
1129 add_trace("dialing", "imsi", "%s", mncc->imsi);
1131 SCPY(mncc->called.number, p_dialinginfo.id);
1132 add_trace("dialing", "number", "%s", mncc->called.number);
1135 /* sending user-user */
1137 /* redirecting number */
1138 mncc->fields |= MNCC_F_REDIRECTING;
1139 mncc->redirecting.plan = 1;
1140 switch (p_redirinfo.ntype) {
1141 case INFO_NTYPE_UNKNOWN:
1142 mncc->redirecting.type = 0x0;
1144 case INFO_NTYPE_INTERNATIONAL:
1145 mncc->redirecting.type = 0x1;
1147 case INFO_NTYPE_NATIONAL:
1148 mncc->redirecting.type = 0x2;
1150 case INFO_NTYPE_SUBSCRIBER:
1151 mncc->redirecting.type = 0x4;
1153 default: /* INFO_NTYPE_NOTPRESENT */
1154 mncc->fields &= ~MNCC_F_REDIRECTING;
1157 switch (p_redirinfo.screen) {
1158 case INFO_SCREEN_USER:
1159 mncc->redirecting.screen = 0;
1161 default: /* INFO_SCREE_NETWORK */
1162 mncc->redirecting.screen = 3;
1165 switch (p_redirinfo.present) {
1166 case INFO_PRESENT_ALLOWED:
1167 mncc->redirecting.present = 0;
1169 case INFO_PRESENT_RESTRICTED:
1170 mncc->redirecting.present = 1;
1172 default: /* INFO_PRESENT_NOTAVAIL */
1173 mncc->redirecting.present = 2;
1176 /* sending redirecting number only in ntmode */
1177 if (mncc->fields & MNCC_F_REDIRECTING) {
1178 SCPY(mncc->redirecting.number, p_redirinfo.id);
1179 add_trace("redir", "type", "%d", mncc->redirecting.type);
1180 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
1181 add_trace("redir", "present", "%d", mncc->redirecting.present);
1182 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
1183 add_trace("redir", "number", "%s", mncc->redirecting.number);
1185 /* bearer capability */
1189 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1191 new_state(PORT_STATE_OUT_SETUP);
1193 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1194 message_put(message);
1196 new_state(PORT_STATE_OUT_PROCEEDING);
1199 /* MESSAGE_NOTIFY */
1200 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1202 struct gsm_mncc *mncc;
1205 printf("if = %d\n", param->notifyinfo.notify);
1206 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1207 notify = param->notifyinfo.notify & 0x7f;
1208 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1209 /* queue notification */
1210 if (p_m_g_notify_pending)
1211 message_free(p_m_g_notify_pending);
1212 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1213 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1215 /* sending notification */
1216 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1217 add_trace("notify", NULL, "%d", notify);
1219 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1220 mncc->notify = notify;
1221 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1226 /* MESSAGE_ALERTING */
1227 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1229 struct gsm_mncc *mncc;
1232 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1233 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1234 if (p_m_mISDNport->tones) {
1235 mncc->fields |= MNCC_F_PROGRESS;
1236 mncc->progress.coding = 3; /* GSM */
1237 mncc->progress.location = 1;
1238 mncc->progress.descr = 8;
1239 add_trace("progress", "coding", "%d", mncc->progress.coding);
1240 add_trace("progress", "location", "%d", mncc->progress.location);
1241 add_trace("progress", "descr", "%d", mncc->progress.descr);
1244 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1246 new_state(PORT_STATE_IN_ALERTING);
1248 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1249 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1251 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1252 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1253 p_m_g_tch_connected = 1;
1257 /* MESSAGE_CONNECT */
1258 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1260 struct gsm_mncc *mncc;
1262 /* copy connected information */
1263 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
1264 /* screen outgoing caller id */
1265 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1268 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1269 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1270 /* caller information */
1271 mncc->fields |= MNCC_F_CONNECTED;
1272 mncc->connected.plan = 1;
1273 switch (p_callerinfo.ntype) {
1274 case INFO_NTYPE_UNKNOWN:
1275 mncc->connected.type = 0x0;
1277 case INFO_NTYPE_INTERNATIONAL:
1278 mncc->connected.type = 0x1;
1280 case INFO_NTYPE_NATIONAL:
1281 mncc->connected.type = 0x2;
1283 case INFO_NTYPE_SUBSCRIBER:
1284 mncc->connected.type = 0x4;
1286 default: /* INFO_NTYPE_NOTPRESENT */
1287 mncc->fields &= ~MNCC_F_CONNECTED;
1290 switch (p_callerinfo.screen) {
1291 case INFO_SCREEN_USER:
1292 mncc->connected.screen = 0;
1294 default: /* INFO_SCREEN_NETWORK */
1295 mncc->connected.screen = 3;
1298 switch (p_callerinfo.present) {
1299 case INFO_PRESENT_ALLOWED:
1300 mncc->connected.present = 0;
1302 case INFO_PRESENT_RESTRICTED:
1303 mncc->connected.present = 1;
1305 default: /* INFO_PRESENT_NOTAVAIL */
1306 mncc->connected.present = 2;
1309 if (mncc->fields & MNCC_F_CONNECTED) {
1310 SCPY(mncc->connected.number, p_connectinfo.id);
1311 add_trace("connected", "type", "%d", mncc->connected.type);
1312 add_trace("connected", "plan", "%d", mncc->connected.plan);
1313 add_trace("connected", "present", "%d", mncc->connected.present);
1314 add_trace("connected", "screen", "%d", mncc->connected.screen);
1315 add_trace("connected", "number", "%s", mncc->connected.number);
1318 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1320 new_state(PORT_STATE_CONNECT_WAITING);
1323 /* MESSAGE_DISCONNECT */
1324 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1326 struct gsm_mncc *mncc;
1328 /* send disconnect */
1329 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1330 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1331 if (p_m_mISDNport->tones) {
1332 mncc->fields |= MNCC_F_PROGRESS;
1333 mncc->progress.coding = 3; /* GSM */
1334 mncc->progress.location = 1;
1335 mncc->progress.descr = 8;
1336 add_trace("progress", "coding", "%d", mncc->progress.coding);
1337 add_trace("progress", "location", "%d", mncc->progress.location);
1338 add_trace("progress", "descr", "%d", mncc->progress.descr);
1340 mncc->fields |= MNCC_F_CAUSE;
1341 mncc->cause.coding = 3;
1342 mncc->cause.location = param->disconnectinfo.location;
1343 mncc->cause.value = param->disconnectinfo.cause;
1344 add_trace("cause", "coding", "%d", mncc->cause.coding);
1345 add_trace("cause", "location", "%d", mncc->cause.location);
1346 add_trace("cause", "value", "%d", mncc->cause.value);
1348 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1350 new_state(PORT_STATE_OUT_DISCONNECT);
1352 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1353 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1355 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1356 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1357 p_m_g_tch_connected = 1;
1362 /* MESSAGE_RELEASE */
1363 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1365 struct gsm_mncc *mncc;
1368 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1369 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1370 mncc->fields |= MNCC_F_CAUSE;
1371 mncc->cause.coding = 3;
1372 mncc->cause.location = param->disconnectinfo.location;
1373 mncc->cause.value = param->disconnectinfo.cause;
1374 add_trace("cause", "coding", "%d", mncc->cause.coding);
1375 add_trace("cause", "location", "%d", mncc->cause.location);
1376 add_trace("cause", "value", "%d", mncc->cause.value);
1378 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1380 new_state(PORT_STATE_RELEASE);
1387 * endpoint sends messages to the port
1389 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1391 if (PmISDN::message_epoint(epoint_id, message_id, param))
1394 switch(message_id) {
1395 case MESSAGE_SETUP: /* dial-out command received from epoint */
1396 if (p_state!=PORT_STATE_IDLE)
1398 message_setup(epoint_id, message_id, param);
1401 case MESSAGE_NOTIFY: /* display and notifications */
1402 message_notify(epoint_id, message_id, param);
1405 // case MESSAGE_FACILITY: /* facility message */
1406 // message_facility(epoint_id, message_id, param);
1409 case MESSAGE_PROCEEDING: /* message not handles */
1412 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1413 if (p_state!=PORT_STATE_IN_PROCEEDING)
1415 message_alerting(epoint_id, message_id, param);
1416 if (p_m_g_notify_pending) {
1417 /* send pending notify message during connect */
1418 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1419 message_free(p_m_g_notify_pending);
1420 p_m_g_notify_pending = NULL;
1424 case MESSAGE_CONNECT: /* call of endpoint is connected */
1425 if (p_state!=PORT_STATE_IN_PROCEEDING
1426 && p_state!=PORT_STATE_IN_ALERTING)
1428 message_connect(epoint_id, message_id, param);
1429 if (p_m_g_notify_pending) {
1430 /* send pending notify message during connect */
1431 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1432 message_free(p_m_g_notify_pending);
1433 p_m_g_notify_pending = NULL;
1437 case MESSAGE_DISCONNECT: /* call has been disconnected */
1438 if (p_state!=PORT_STATE_IN_PROCEEDING
1439 && p_state!=PORT_STATE_IN_ALERTING
1440 && p_state!=PORT_STATE_OUT_SETUP
1441 && p_state!=PORT_STATE_OUT_OVERLAP
1442 && p_state!=PORT_STATE_OUT_PROCEEDING
1443 && p_state!=PORT_STATE_OUT_ALERTING
1444 && p_state!=PORT_STATE_CONNECT
1445 && p_state!=PORT_STATE_CONNECT_WAITING)
1447 message_disconnect(epoint_id, message_id, param);
1450 case MESSAGE_RELEASE: /* release isdn port */
1451 if (p_state==PORT_STATE_RELEASE)
1453 message_release(epoint_id, message_id, param);
1457 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1467 int Pgsm::handler(void)
1471 unsigned char buffer[2048+MISDN_HEADER_LEN];
1472 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1474 if ((ret = PmISDN::handler()))
1477 /* handle destruction */
1483 /* handle message from bchannel */
1484 if (p_m_g_gsm_b_sock > -1) {
1485 ret = recv(p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1486 if (ret >= (int)MISDN_HEADER_LEN) {
1488 /* we don't care about confirms, we use rx data to sync tx */
1491 /* we receive audio data, we respond to it AND we send tones */
1493 bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1495 case PH_ACTIVATE_IND:
1496 p_m_g_gsm_b_active = 1;
1498 case PH_DEACTIVATE_IND:
1499 p_m_g_gsm_b_active = 0;
1504 if (ret < 0 && errno != EWOULDBLOCK)
1505 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1514 * handles bsc select function within LCR's main loop
1516 int handle_gsm(void)
1520 ret1 = bsc_upqueue((struct gsm_network *)gsm->network);
1521 ret2 = bsc_select_main(1); /* polling */
1527 static void gsm_sock_close(void)
1529 if (gsm->gsm_sock > -1)
1530 close(gsm->gsm_sock);
1534 static int gsm_sock_open(char *portname)
1538 unsigned long on = 1;
1539 struct sockaddr_mISDN addr;
1540 struct mISDN_devinfo devinfo;
1543 /* check port counts */
1544 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1546 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1551 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1554 gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1555 if (gsm->gsm_port < 0) {
1556 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1557 return gsm->gsm_port;
1561 devinfo.id = gsm->gsm_port;
1562 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1564 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1567 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1570 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1574 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1577 if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_TE_S0)) < 0) {
1578 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1580 return gsm->gsm_sock;
1582 /* set nonblocking io */
1583 if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1584 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1588 /* bind socket to dchannel */
1589 memset(&addr, 0, sizeof(addr));
1590 addr.family = AF_ISDN;
1591 addr.dev = gsm->gsm_port;
1593 if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1594 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1603 int gsm_exit(int rc)
1605 /* free gsm instance */
1607 if (gsm->gsm_sock > -1)
1609 /* shutdown network */
1611 shutdown_net((struct gsm_network *)gsm->network);
1613 // if (gsm->network) {
1614 // free((struct gsm_network *)gsm->network); /* TBD */
1625 char hlr[128], filename[128];
1626 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1629 /* create gsm instance */
1630 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1634 if (!gsm_conf(&gsm->conf)) {
1635 PERROR("%s", gsm_conf_error);
1636 return gsm_exit(-EINVAL);
1640 debug_parse_category_mask(gsm->conf.debug);
1643 if (gsm->conf.hlr[0] == '/')
1644 SCPY(hlr, gsm->conf.hlr);
1646 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1648 // TODO multiple base stations
1649 if (gsm->conf.numbts < 1 || gsm->conf.numbts > 1) {
1650 PERROR("Expecting exactly one BTS. You defined %d.\n", gsm->conf.numbts);
1651 return gsm_exit(-1);
1654 /* bootstrap network */
1655 gsm->network = bootstrap_network(&message_bcs, gsm->conf.bts[0].type, gsm->conf.mcc, gsm->conf.mnc, gsm->conf.lac, gsm->conf.bts[0].frequency[0], gsm->conf.bts[0].card, !gsm->conf.keep_l2, gsm->conf.short_name, gsm->conf.long_name, hlr, gsm->conf.allow_all);
1656 if (!gsm->network) {
1657 PERROR("Failed to bootstrap GSM network.\n");
1658 return gsm_exit(-1);
1661 /* open gsm loop interface */
1662 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1663 return gsm_exit(-1);
1666 /* open pcap file */
1667 if (gsm->conf.pcapfile[0]) {
1668 if (gsm->conf.pcapfile[0] == '/')
1669 SCPY(filename, gsm->conf.pcapfile);
1671 SPRINT(filename, "%s/%s", CONFIG_DATA, gsm->conf.pcapfile);
1672 pcapfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode);
1674 PERROR("Failed to open file for pcap\n");
1675 return gsm_exit(-1);
1677 e1_set_pcap_fd(pcapfd);