1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
14 #include "openbsc/gsm_data.h"
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
29 struct lcr_gsm *gsm = NULL;
31 static unsigned int new_callref = 1;
35 * create and send mncc message
37 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
39 struct gsm_mncc *mncc;
41 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
42 mncc->msg_type = msg_type;
43 mncc->callref = callref;
46 static int send_and_free_mncc(struct gsm_network *net, unsigned int msg_type, void *data)
50 ret = mncc_send(net, msg_type, data);
60 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)
62 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
65 p_m_g_gsm_b_sock = -1;
66 p_m_g_gsm_b_index = -1;
67 p_m_g_gsm_b_active = 0;
68 p_m_g_notify_pending = NULL;
69 p_m_g_decoder = gsm_audio_create();
70 p_m_g_encoder = gsm_audio_create();
71 if (!p_m_g_encoder || !p_m_g_decoder) {
72 PERROR("Failed to create GSM audio codec instance\n");
76 p_m_g_tch_connected = 0;
78 PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
86 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
88 /* remove queued message */
89 if (p_m_g_notify_pending)
90 message_free(p_m_g_notify_pending);
92 /* close audio transfer socket */
93 if (p_m_g_gsm_b_sock > -1)
98 gsm_audio_destroy(p_m_g_encoder);
100 gsm_audio_destroy(p_m_g_decoder);
104 /* close bsc side bchannel */
105 void Pgsm::bchannel_close(void)
107 if (p_m_g_gsm_b_sock > -1)
108 close(p_m_g_gsm_b_sock);
109 p_m_g_gsm_b_sock = -1;
110 p_m_g_gsm_b_index = -1;
111 p_m_g_gsm_b_active = 0;
114 /* open bsc side bchannel */
115 int Pgsm::bchannel_open(int index)
119 struct sockaddr_mISDN addr;
120 struct mISDNhead act;
122 if (p_m_g_gsm_b_sock > -1) {
123 PERROR("Socket already created for index %d\n", index);
128 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
130 PERROR("Failed to open bchannel-socket for index %d\n", index);
135 /* set nonblocking io */
136 ret = ioctl(p_m_g_gsm_b_sock, FIONBIO, &on);
138 PERROR("Failed to set bchannel-socket index %d into nonblocking IO\n", index);
143 /* bind socket to bchannel */
144 addr.family = AF_ISDN;
145 addr.dev = gsm->gsm_port;
146 addr.channel = index+1+(index>15);
147 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
149 PERROR("Failed to bind bchannel-socket for index %d\n", index);
153 /* activate bchannel */
154 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
155 act.prim = PH_ACTIVATE_REQ;
157 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
159 PERROR("Failed to activate index %d\n", index);
164 p_m_g_gsm_b_index = index;
169 /* receive from bchannel */
170 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
173 unsigned char frame[33];
174 struct decoded_trau_frame tf;
176 /* encoder init failed */
180 /* (currently) not connected, so don't flood tch! */
181 if (!p_m_g_tch_connected)
184 /* write to rx buffer */
186 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
187 if (p_m_g_rxpos == 160) {
191 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
193 /* set c-bits and t-bits */
199 memset(&tf.c_bits[5], 0, 6);
200 memset(&tf.c_bits[11], 1, 10);
201 memset(&tf.t_bits[0], 1, 4);
203 /* reassemble d-bits */
208 tf.d_bits[i] = (frame[j] >> k) & 1;
221 /* transmit to bchannel */
222 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
224 unsigned char buf[MISDN_HEADER_LEN+len];
225 struct mISDNhead *hh = (struct mISDNhead *)buf;
228 if (!p_m_g_gsm_b_active)
231 /* make and send frame */
232 hh->prim = PH_DATA_REQ;
234 memcpy(buf+MISDN_HEADER_LEN, data, len);
235 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
237 PERROR("Failed to send to socket index %d\n", index);
240 void Pgsm::trau_send(void *_tf)
242 struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_tf;
243 unsigned char data[sizeof(struct gsm_trau_frame) + sizeof(struct decoded_trau_frame)];
244 struct gsm_trau_frame *frame = (struct gsm_trau_frame *)data;
246 frame->msg_type = GSM_TRAU_FRAME;
247 frame->callref = p_m_g_callref;
248 memcpy(frame->data, tf, sizeof(struct decoded_trau_frame));
249 mncc_send((struct gsm_network *)gsm->network, frame->msg_type, frame);
253 void Pgsm::trau_receive(void *_frame)
255 struct gsm_trau_frame *frm = (struct gsm_trau_frame *)_frame;
256 struct decoded_trau_frame *tf = (struct decoded_trau_frame *)frm->data;
257 //struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_frame;
258 unsigned char frame[33];
259 signed short samples[160];
260 unsigned char data[160];
266 // 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]);
267 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)
268 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]);
271 memset(&frame, 0, sizeof(frame));
272 // frame[0] = 0xd << 4;
274 /* reassemble bits */
279 if (tf->d_bits[i] > 1)
281 frame[j] |= (tf->d_bits[i] << k);
290 gsm_audio_decode(p_m_g_decoder, frame, samples);
291 for (i = 0; i < 160; i++) {
292 data[i] = audio_s16_to_law[samples[i] & 0xffff];
296 bchannel_send(PH_DATA_REQ, 0, data, 160);
303 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
307 /* select message and primitive text */
308 SCPY(msgtext, get_mncc_name(msg_type));
311 if (direction == DIRECTION_OUT)
312 SCAT(msgtext, " MSC->BSC");
314 SCAT(msgtext, " MSC<-BSC");
316 /* init trace with given values */
317 start_trace(mISDNport?mISDNport->portnum:-1,
318 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
319 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
320 port?port->p_dialinginfo.id:NULL,
323 port?port->p_serial:0,
329 /* select bchannel */
330 int Pgsm::hunt_bchannel(void)
335 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
336 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
337 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
338 add_trace("conclusion", NULL, "all channels are reserved");
340 return(-34); // no channel
345 while(i < p_m_mISDNport->b_num) {
346 if (p_m_mISDNport->b_port[i] == NULL) {
347 channel = i+1+(i>=15);
353 add_trace("conclusion", NULL, "no channel available");
355 return(-6); // channel unacceptable
357 add_trace("conclusion", NULL, "channel available");
358 add_trace("connect", "channel", "%d", channel);
365 * handles all indications
367 /* SETUP INDICATION */
368 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
371 class Endpoint *epoint;
372 struct lcr_msg *message;
374 struct gsm_mncc *mode, *proceeding, *frame;
376 /* emergency shutdown */
377 printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
378 if (mncc->emergency && !gsm->conf.noemergshut) {
379 start_trace(p_m_mISDNport->portnum,
380 p_m_mISDNport->ifport->interface,
386 "EMERGENCY SHUTDOWN (due to received emergency call)");
390 /* process given callref */
391 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
392 add_trace("callref", "new", "0x%x", callref);
394 /* release in case the ID is already in use */
395 add_trace("error", NULL, "callref already in use");
397 mncc = create_mncc(MNCC_REJ_REQ, callref);
398 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
400 mncc->cause_coding = 3;
401 mncc->cause_location = 1;
402 mncc->cause_value = 47;
403 add_trace("cause", "coding", "%d", mncc->cause_coding);
404 add_trace("cause", "location", "%d", mncc->cause_location);
405 add_trace("cause", "value", "%d", mncc->cause_value);
406 add_trace("reason", NULL, "callref already in use");
408 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
409 new_state(PORT_STATE_RELEASE);
413 p_m_g_callref = callref;
416 /* if blocked, release call with MT_RELEASE_COMPLETE */
417 if (p_m_mISDNport->ifport->block) {
418 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
419 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
421 mncc->cause_coding = 3;
422 mncc->cause_location = 1;
423 mncc->cause_value = 27;
424 add_trace("cause", "coding", "%d", mncc->cause_coding);
425 add_trace("cause", "location", "%d", mncc->cause_location);
426 add_trace("cause", "value", "%d", mncc->cause_value);
427 add_trace("reason", NULL, "port is blocked");
429 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
430 new_state(PORT_STATE_RELEASE);
437 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
439 p_callerinfo.present = INFO_PRESENT_ALLOWED;
440 if (mncc->calling_number[0])
441 SCPY(p_callerinfo.id, mncc->calling_number);
443 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
444 SCPY(p_callerinfo.imsi, mncc->imsi);
445 p_callerinfo.screen = INFO_SCREEN_NETWORK;
446 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
447 p_callerinfo.isdn_port = p_m_portnum;
448 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
450 /* dialing information */
451 SCAT(p_dialinginfo.id, mncc->called_number);
452 switch (mncc->called_type) {
454 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
457 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
460 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
463 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
466 if (mncc->emergency) {
467 SCPY(p_dialinginfo.id, "emergency");
469 p_dialinginfo.sending_complete = 1;
471 /* bearer capability */
473 p_capainfo.bearer_capa = INFO_BC_SPEECH;
474 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
475 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
476 p_capainfo.source_mode = B_MODE_TRANSPARENT;
477 p_m_g_mode = p_capainfo.source_mode;
482 ret = channel = hunt_bchannel();
487 ret = seize_bchannel(channel, 1);
490 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
491 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
493 mncc->cause_coding = 3;
494 mncc->cause_location = 1;
495 mncc->cause_value = 34;
496 add_trace("cause", "coding", "%d", mncc->cause_coding);
497 add_trace("cause", "location", "%d", mncc->cause_location);
498 add_trace("cause", "value", "%d", mncc->cause_value);
499 add_trace("reason", NULL, "no channel");
501 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
502 new_state(PORT_STATE_RELEASE);
506 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
507 if (bchannel_open(p_m_b_index))
510 /* what infos did we got ... */
511 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
512 if (p_callerinfo.id[0])
513 add_trace("calling", "number", "%s", p_callerinfo.id);
515 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
516 add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
517 add_trace("dialing", "number", "%s", p_dialinginfo.id);
520 /* create endpoint */
522 FATAL("Incoming call but already got an endpoint.\n");
523 if (!(epoint = new Endpoint(p_serial, 0)))
524 FATAL("No memory for Endpoint instance\n");
525 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
526 FATAL("No memory for Endpoint Application instance\n");
527 epointlist_new(epoint->ep_serial);
529 /* modify lchan to GSM codec V1 */
530 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
531 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
532 mode->lchan_mode = 0x01; /* GSM V1 */
533 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
535 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
537 /* send call proceeding */
538 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
539 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
540 if (p_m_mISDNport->tones) {
541 proceeding->progress = 1;
542 proceeding->progress_coding = 3; /* GSM */
543 proceeding->progress_location = 1;
544 proceeding->progress_descr = 8;
545 add_trace("progress", "coding", "%d", proceeding->progress_coding);
546 add_trace("progress", "location", "%d", proceeding->progress_location);
547 add_trace("progress", "descr", "%d", proceeding->progress_descr);
550 send_and_free_mncc((struct gsm_network *)gsm->network, proceeding->msg_type, proceeding);
552 new_state(PORT_STATE_IN_PROCEEDING);
554 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
555 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
557 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
558 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
559 p_m_g_tch_connected = 1;
562 /* send setup message to endpoit */
563 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
564 message->param.setup.isdn_port = p_m_portnum;
565 message->param.setup.port_type = p_type;
566 // message->param.setup.dtmf = 0;
567 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
568 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
569 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
570 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser_info);
571 message->param.setup.useruser.len = strlen(mncc->useruser_info);
572 message->param.setup.useruser.protocol = mncc->useruser_proto;
573 message_put(message);
576 /* DTMF INDICATION */
577 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
579 struct lcr_msg *message;
580 struct gsm_mncc *resp;
582 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
583 add_trace("keypad", NULL, "%c", mncc->keypad);
585 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
586 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
589 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
590 add_trace("keypad", NULL, "%c", mncc->keypad);
592 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
593 resp->keypad = mncc->keypad;
594 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
596 /* send dialing information */
597 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
598 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
599 message_put(message);
601 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
603 struct gsm_mncc *resp;
605 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
606 add_trace("keypad", NULL, "%c", mncc->keypad);
610 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
611 add_trace("keypad", NULL, "%c", mncc->keypad);
613 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
614 resp->keypad = mncc->keypad;
615 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
618 /* PROCEEDING INDICATION */
619 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
621 struct gsm_mncc *mode;
623 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
625 add_trace("cause", "coding", "%d", mncc->cause_coding);
626 add_trace("cause", "location", "%", mncc->cause_location);
627 add_trace("cause", "value", "%", mncc->cause_value);
631 /* modify lchan to GSM codec V1 */
632 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
633 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
634 mode->lchan_mode = 0x01; /* GSM V1 */
635 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
637 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
641 /* ALERTING INDICATION */
642 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
644 struct lcr_msg *message;
646 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
649 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
650 message_put(message);
652 new_state(PORT_STATE_OUT_ALERTING);
656 /* CONNECT INDICATION */
657 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
659 struct gsm_mncc *resp, *frame;
660 struct lcr_msg *message;
662 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
665 SCPY(p_connectinfo.id, mncc->calling_number);
666 SCPY(p_connectinfo.imsi, mncc->imsi);
667 p_connectinfo.present = INFO_PRESENT_ALLOWED;
668 p_connectinfo.screen = INFO_SCREEN_NETWORK;
669 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
670 p_connectinfo.isdn_port = p_m_portnum;
671 SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
673 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
674 if (p_connectinfo.id[0])
675 add_trace("connect", "number", "%s", p_connectinfo.id);
677 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
678 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
679 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
681 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
683 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
684 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
685 message_put(message);
687 new_state(PORT_STATE_CONNECT);
689 if (!p_m_g_tch_connected) { /* only if ... */
690 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
692 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
693 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
694 p_m_g_tch_connected = 1;
698 /* CONNECT ACK INDICATION */
699 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
701 struct gsm_mncc *frame;
703 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
706 new_state(PORT_STATE_CONNECT);
708 if (!p_m_g_tch_connected) { /* only if ... */
709 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
711 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
712 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
713 p_m_g_tch_connected = 1;
717 /* DISCONNECT INDICATION */
718 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
720 struct lcr_msg *message;
721 int cause = 16, location = 0;
722 struct gsm_mncc *resp;
724 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
726 location = mncc->cause_location;
727 cause = mncc->cause_value;
728 add_trace("cause", "coding", "%d", mncc->cause_coding);
729 add_trace("cause", "location", "%d", location);
730 add_trace("cause", "value", "%d", cause);
735 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
736 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
739 resp->cause_coding = 3;
740 resp->cause_location = 1;
741 resp->cause_value = cause;
742 add_trace("cause", "coding", "%d", resp->cause_coding);
743 add_trace("cause", "location", "%d", resp->cause_location);
744 add_trace("cause", "value", "%d", resp->cause_value);
747 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
749 /* sending release to endpoint */
750 while(p_epointlist) {
751 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
752 message->param.disconnectinfo.cause = cause;
753 message->param.disconnectinfo.location = location;
754 message_put(message);
756 free_epointlist(p_epointlist);
758 new_state(PORT_STATE_RELEASE);
762 /* CC_RELEASE INDICATION */
763 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
765 int location = 0, cause = 16;
766 struct lcr_msg *message;
768 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
770 location = mncc->cause_location;
771 cause = mncc->cause_value;
772 add_trace("cause", "coding", "%d", mncc->cause_coding);
773 add_trace("cause", "location", "%d", mncc->cause_location);
774 add_trace("cause", "value", "%d", mncc->cause_value);
778 /* sending release to endpoint */
779 while(p_epointlist) {
780 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
781 message->param.disconnectinfo.cause = cause;
782 message->param.disconnectinfo.location = location;
783 message_put(message);
785 free_epointlist(p_epointlist);
787 new_state(PORT_STATE_RELEASE);
791 /* NOTIFY INDICATION */
792 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
794 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
799 /* HOLD INDICATION */
800 void Pgsm::hold_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 hold 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_HOLD;
811 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
812 message_put(message);
814 /* acknowledge hold */
815 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
817 resp = create_mncc(MNCC_HOLD_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_DROP, DIRECTION_OUT);
824 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
825 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
826 p_m_g_tch_connected = 0;
831 /* RETRIEVE INDICATION */
832 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
834 struct lcr_msg *message;
835 struct gsm_mncc *resp, *frame;
837 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
840 /* notify the retrieve of call */
841 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
842 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
843 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
844 message_put(message);
846 /* acknowledge retr */
847 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
849 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
850 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
853 if (!p_m_g_tch_connected) { /* it should be true */
854 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
856 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
857 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
858 p_m_g_tch_connected = 1;
863 * BSC sends message to port
865 static int message_bcs(struct gsm_network *net, int msg_type, void *arg)
867 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
868 unsigned int callref = mncc->callref;
870 class Pgsm *pgsm = NULL;
872 struct mISDNport *mISDNport;
874 /* Special messages */
879 callref = mncc->callref;
882 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
883 pgsm = (class Pgsm *)port;
884 if (pgsm->p_m_g_callref == callref) {
891 if (msg_type == GSM_TRAU_FRAME) {
893 pgsm->trau_receive((struct gsm_trau_frame *)arg);
898 if (msg_type != MNCC_SETUP_IND)
901 mISDNport = mISDNport_first;
905 mISDNport = mISDNport->next;
908 struct gsm_mncc *rej;
910 rej = create_mncc(MNCC_REJ_REQ, callref);
912 rej->cause_coding = 3;
913 rej->cause_location = 1;
914 rej->cause_value = 27;
915 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
916 add_trace("cause", "coding", "%d", rej->cause_coding);
917 add_trace("cause", "location", "%d", rej->cause_location);
918 add_trace("cause", "value", "%d", rej->cause_value);
920 send_and_free_mncc((struct gsm_network *)gsm->network, rej->msg_type, rej);
923 /* creating port object, transparent until setup with hdlc */
924 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
925 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
927 FATAL("Cannot create Port instance.\n");
932 pgsm->setup_ind(msg_type, callref, mncc);
935 case MNCC_START_DTMF_IND:
936 pgsm->start_dtmf_ind(msg_type, callref, mncc);
939 case MNCC_STOP_DTMF_IND:
940 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
943 case MNCC_CALL_CONF_IND:
944 pgsm->call_conf_ind(msg_type, callref, mncc);
948 pgsm->alert_ind(msg_type, callref, mncc);
952 pgsm->setup_cnf(msg_type, callref, mncc);
955 case MNCC_SETUP_COMPL_IND:
956 pgsm->setup_compl_ind(msg_type, callref, mncc);
960 pgsm->disc_ind(msg_type, callref, mncc);
966 pgsm->rel_ind(msg_type, callref, mncc);
969 case MNCC_NOTIFY_IND:
970 pgsm->notify_ind(msg_type, callref, mncc);
974 pgsm->hold_ind(msg_type, callref, mncc);
977 case MNCC_RETRIEVE_IND:
978 pgsm->retr_ind(msg_type, callref, mncc);
982 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);
988 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
990 struct lcr_msg *message;
992 struct epoint_list *epointlist;
993 struct gsm_mncc *mncc;
996 /* copy setup infos to port */
997 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
998 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
999 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
1000 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1003 if (!p_dialinginfo.id[0]) {
1004 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1005 add_trace("failure", NULL, "No dialed subscriber given.");
1007 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1008 message->param.disconnectinfo.cause = 28;
1009 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1010 message_put(message);
1011 new_state(PORT_STATE_RELEASE);
1016 /* release if port is blocked */
1017 if (p_m_mISDNport->ifport->block) {
1018 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1019 add_trace("failure", NULL, "Port blocked.");
1021 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1022 message->param.disconnectinfo.cause = 27; // temp. unavail.
1023 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1024 message_put(message);
1025 new_state(PORT_STATE_RELEASE);
1031 ret = channel = hunt_bchannel();
1035 ret = seize_bchannel(channel, 1);
1038 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1039 add_trace("failure", NULL, "No internal audio channel available.");
1041 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1042 message->param.disconnectinfo.cause = 34;
1043 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1044 message_put(message);
1045 new_state(PORT_STATE_RELEASE);
1049 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1050 if (bchannel_open(p_m_b_index))
1053 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1054 /* screen outgoing caller id */
1055 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1057 /* attach only if not already */
1058 epointlist = p_epointlist;
1060 if (epointlist->epoint_id == epoint_id)
1062 epointlist = epointlist->next;
1065 epointlist_new(epoint_id);
1068 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1069 p_m_g_callref = new_callref++;
1070 add_trace("callref", "new", "0x%x", p_m_g_callref);
1073 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1074 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1075 /* caller information */
1077 mncc->calling_plan = 1;
1078 switch (p_callerinfo.ntype) {
1079 case INFO_NTYPE_UNKNOWN:
1080 mncc->calling_type = 0x0;
1082 case INFO_NTYPE_INTERNATIONAL:
1083 mncc->calling_type = 0x1;
1085 case INFO_NTYPE_NATIONAL:
1086 mncc->calling_type = 0x2;
1088 case INFO_NTYPE_SUBSCRIBER:
1089 mncc->calling_type = 0x4;
1091 default: /* INFO_NTYPE_NOTPRESENT */
1095 switch (p_callerinfo.screen) {
1096 case INFO_SCREEN_USER:
1097 mncc->calling_screen = 0;
1099 default: /* INFO_SCREEN_NETWORK */
1100 mncc->calling_screen = 3;
1103 switch (p_callerinfo.present) {
1104 case INFO_PRESENT_ALLOWED:
1105 mncc->calling_present = 0;
1107 case INFO_PRESENT_RESTRICTED:
1108 mncc->calling_present = 1;
1110 default: /* INFO_PRESENT_NOTAVAIL */
1111 mncc->calling_present = 2;
1114 if (mncc->calling) {
1115 SCPY(mncc->calling_number, p_callerinfo.id);
1116 add_trace("calling", "type", "%d", mncc->calling_type);
1117 add_trace("calling", "plan", "%d", mncc->calling_plan);
1118 add_trace("calling", "present", "%d", mncc->calling_present);
1119 add_trace("calling", "screen", "%d", mncc->calling_screen);
1120 add_trace("calling", "number", "%s", mncc->calling_number);
1122 /* dialing information */
1124 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
1125 SCPY(mncc->imsi, p_dialinginfo.id+5);
1126 add_trace("dialing", "imsi", "%s", mncc->imsi);
1128 SCPY(mncc->called_number, p_dialinginfo.id);
1129 add_trace("dialing", "number", "%s", mncc->called_number);
1132 /* sending user-user */
1134 /* redirecting number */
1135 mncc->redirecting = 1;
1136 mncc->redirecting_plan = 1;
1137 switch (p_redirinfo.ntype) {
1138 case INFO_NTYPE_UNKNOWN:
1139 mncc->redirecting_type = 0x0;
1141 case INFO_NTYPE_INTERNATIONAL:
1142 mncc->redirecting_type = 0x1;
1144 case INFO_NTYPE_NATIONAL:
1145 mncc->redirecting_type = 0x2;
1147 case INFO_NTYPE_SUBSCRIBER:
1148 mncc->redirecting_type = 0x4;
1150 default: /* INFO_NTYPE_NOTPRESENT */
1151 mncc->redirecting = 0;
1154 switch (p_redirinfo.screen) {
1155 case INFO_SCREEN_USER:
1156 mncc->redirecting_screen = 0;
1158 default: /* INFO_SCREE_NETWORK */
1159 mncc->redirecting_screen = 3;
1162 switch (p_redirinfo.present) {
1163 case INFO_PRESENT_ALLOWED:
1164 mncc->redirecting_present = 0;
1166 case INFO_PRESENT_RESTRICTED:
1167 mncc->redirecting_present = 1;
1169 default: /* INFO_PRESENT_NOTAVAIL */
1170 mncc->redirecting_present = 2;
1173 /* sending redirecting number only in ntmode */
1174 if (mncc->redirecting) {
1175 SCPY(mncc->redirecting_number, p_redirinfo.id);
1176 add_trace("redir", "type", "%d", mncc->redirecting_type);
1177 add_trace("redir", "plan", "%d", mncc->redirecting_plan);
1178 add_trace("redir", "present", "%d", mncc->redirecting_present);
1179 add_trace("redir", "screen", "%d", mncc->redirecting_screen);
1180 add_trace("redir", "number", "%s", mncc->redirecting_number);
1182 /* bearer capability */
1186 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1188 new_state(PORT_STATE_OUT_SETUP);
1190 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1191 message_put(message);
1193 new_state(PORT_STATE_OUT_PROCEEDING);
1196 /* MESSAGE_NOTIFY */
1197 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1199 struct gsm_mncc *mncc;
1202 printf("if = %d\n", param->notifyinfo.notify);
1203 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1204 notify = param->notifyinfo.notify & 0x7f;
1205 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1206 /* queue notification */
1207 if (p_m_g_notify_pending)
1208 message_free(p_m_g_notify_pending);
1209 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1210 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1212 /* sending notification */
1213 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1214 add_trace("notify", NULL, "%d", notify);
1216 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1217 mncc->notify = notify;
1218 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1223 /* MESSAGE_ALERTING */
1224 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1226 struct gsm_mncc *mncc;
1229 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1230 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1231 if (p_m_mISDNport->tones) {
1233 mncc->progress_coding = 3; /* GSM */
1234 mncc->progress_location = 1;
1235 mncc->progress_descr = 8;
1236 add_trace("progress", "coding", "%d", mncc->progress_coding);
1237 add_trace("progress", "location", "%d", mncc->progress_location);
1238 add_trace("progress", "descr", "%d", mncc->progress_descr);
1241 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1243 new_state(PORT_STATE_IN_ALERTING);
1245 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1246 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1248 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1249 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1250 p_m_g_tch_connected = 1;
1254 /* MESSAGE_CONNECT */
1255 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1257 struct gsm_mncc *mncc;
1259 /* copy connected information */
1260 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
1261 /* screen outgoing caller id */
1262 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1265 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1266 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1267 /* caller information */
1268 mncc->connected = 1;
1269 mncc->connected_plan = 1;
1270 switch (p_callerinfo.ntype) {
1271 case INFO_NTYPE_UNKNOWN:
1272 mncc->connected_type = 0x0;
1274 case INFO_NTYPE_INTERNATIONAL:
1275 mncc->connected_type = 0x1;
1277 case INFO_NTYPE_NATIONAL:
1278 mncc->connected_type = 0x2;
1280 case INFO_NTYPE_SUBSCRIBER:
1281 mncc->connected_type = 0x4;
1283 default: /* INFO_NTYPE_NOTPRESENT */
1284 mncc->connected = 0;
1287 switch (p_callerinfo.screen) {
1288 case INFO_SCREEN_USER:
1289 mncc->connected_screen = 0;
1291 default: /* INFO_SCREEN_NETWORK */
1292 mncc->connected_screen = 3;
1295 switch (p_callerinfo.present) {
1296 case INFO_PRESENT_ALLOWED:
1297 mncc->connected_present = 0;
1299 case INFO_PRESENT_RESTRICTED:
1300 mncc->connected_present = 1;
1302 default: /* INFO_PRESENT_NOTAVAIL */
1303 mncc->connected_present = 2;
1306 if (mncc->connected) {
1307 SCPY(mncc->connected_number, p_connectinfo.id);
1308 add_trace("connected", "type", "%d", mncc->connected_type);
1309 add_trace("connected", "plan", "%d", mncc->connected_plan);
1310 add_trace("connected", "present", "%d", mncc->connected_present);
1311 add_trace("connected", "screen", "%d", mncc->connected_screen);
1312 add_trace("connected", "number", "%s", mncc->connected_number);
1315 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1317 new_state(PORT_STATE_CONNECT_WAITING);
1320 /* MESSAGE_DISCONNECT */
1321 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1323 struct gsm_mncc *mncc;
1325 /* send disconnect */
1326 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1327 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1328 if (p_m_mISDNport->tones) {
1330 mncc->progress_coding = 3; /* GSM */
1331 mncc->progress_location = 1;
1332 mncc->progress_descr = 8;
1333 add_trace("progress", "coding", "%d", mncc->progress_coding);
1334 add_trace("progress", "location", "%d", mncc->progress_location);
1335 add_trace("progress", "descr", "%d", mncc->progress_descr);
1338 mncc->cause_coding = 3;
1339 mncc->cause_location = param->disconnectinfo.location;
1340 mncc->cause_value = param->disconnectinfo.cause;
1341 add_trace("cause", "coding", "%d", mncc->cause_coding);
1342 add_trace("cause", "location", "%d", mncc->cause_location);
1343 add_trace("cause", "value", "%d", mncc->cause_value);
1345 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1347 new_state(PORT_STATE_OUT_DISCONNECT);
1349 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1350 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1352 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1353 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1354 p_m_g_tch_connected = 1;
1359 /* MESSAGE_RELEASE */
1360 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1362 struct gsm_mncc *mncc;
1365 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1366 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1368 mncc->cause_coding = 3;
1369 mncc->cause_location = param->disconnectinfo.location;
1370 mncc->cause_value = param->disconnectinfo.cause;
1371 add_trace("cause", "coding", "%d", mncc->cause_coding);
1372 add_trace("cause", "location", "%d", mncc->cause_location);
1373 add_trace("cause", "value", "%d", mncc->cause_value);
1375 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1377 new_state(PORT_STATE_RELEASE);
1384 * endpoint sends messages to the port
1386 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1388 if (PmISDN::message_epoint(epoint_id, message_id, param))
1391 switch(message_id) {
1392 case MESSAGE_SETUP: /* dial-out command received from epoint */
1393 if (p_state!=PORT_STATE_IDLE)
1395 message_setup(epoint_id, message_id, param);
1398 case MESSAGE_NOTIFY: /* display and notifications */
1399 message_notify(epoint_id, message_id, param);
1402 // case MESSAGE_FACILITY: /* facility message */
1403 // message_facility(epoint_id, message_id, param);
1406 case MESSAGE_PROCEEDING: /* message not handles */
1409 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1410 if (p_state!=PORT_STATE_IN_PROCEEDING)
1412 message_alerting(epoint_id, message_id, param);
1413 if (p_m_g_notify_pending) {
1414 /* send pending notify message during connect */
1415 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1416 message_free(p_m_g_notify_pending);
1417 p_m_g_notify_pending = NULL;
1421 case MESSAGE_CONNECT: /* call of endpoint is connected */
1422 if (p_state!=PORT_STATE_IN_PROCEEDING
1423 && p_state!=PORT_STATE_IN_ALERTING)
1425 message_connect(epoint_id, message_id, param);
1426 if (p_m_g_notify_pending) {
1427 /* send pending notify message during connect */
1428 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1429 message_free(p_m_g_notify_pending);
1430 p_m_g_notify_pending = NULL;
1434 case MESSAGE_DISCONNECT: /* call has been disconnected */
1435 if (p_state!=PORT_STATE_IN_PROCEEDING
1436 && p_state!=PORT_STATE_IN_ALERTING
1437 && p_state!=PORT_STATE_OUT_SETUP
1438 && p_state!=PORT_STATE_OUT_OVERLAP
1439 && p_state!=PORT_STATE_OUT_PROCEEDING
1440 && p_state!=PORT_STATE_OUT_ALERTING
1441 && p_state!=PORT_STATE_CONNECT
1442 && p_state!=PORT_STATE_CONNECT_WAITING)
1444 message_disconnect(epoint_id, message_id, param);
1447 case MESSAGE_RELEASE: /* release isdn port */
1448 if (p_state==PORT_STATE_RELEASE)
1450 message_release(epoint_id, message_id, param);
1454 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1464 int Pgsm::handler(void)
1468 unsigned char buffer[2048+MISDN_HEADER_LEN];
1469 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1471 if ((ret = PmISDN::handler()))
1474 /* handle destruction */
1480 /* handle message from bchannel */
1481 if (p_m_g_gsm_b_sock > -1) {
1482 ret = recv(p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1483 if (ret >= (int)MISDN_HEADER_LEN) {
1485 /* we don't care about confirms, we use rx data to sync tx */
1488 /* we receive audio data, we respond to it AND we send tones */
1490 bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1492 case PH_ACTIVATE_IND:
1493 p_m_g_gsm_b_active = 1;
1495 case PH_DEACTIVATE_IND:
1496 p_m_g_gsm_b_active = 0;
1501 if (ret < 0 && errno != EWOULDBLOCK)
1502 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1511 * handles bsc select function within LCR's main loop
1513 int handle_gsm(void)
1517 ret1 = bsc_upqueue((struct gsm_network *)gsm->network);
1518 ret2 = bsc_select_main(1); /* polling */
1524 static void gsm_sock_close(void)
1526 if (gsm->gsm_sock > -1)
1527 close(gsm->gsm_sock);
1531 static int gsm_sock_open(char *portname)
1535 unsigned long on = 1;
1536 struct sockaddr_mISDN addr;
1537 struct mISDN_devinfo devinfo;
1540 /* check port counts */
1541 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1543 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1548 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1551 gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1552 if (gsm->gsm_port < 0) {
1553 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1554 return gsm->gsm_port;
1558 devinfo.id = gsm->gsm_port;
1559 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1561 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1564 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1567 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1571 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1574 if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_TE_S0)) < 0) {
1575 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1577 return gsm->gsm_sock;
1579 /* set nonblocking io */
1580 if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1581 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1585 /* bind socket to dchannel */
1586 memset(&addr, 0, sizeof(addr));
1587 addr.family = AF_ISDN;
1588 addr.dev = gsm->gsm_port;
1590 if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1591 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1600 int gsm_exit(int rc)
1602 /* free gsm instance */
1604 if (gsm->gsm_sock > -1)
1606 /* shutdown network */
1608 shutdown_net((struct gsm_network *)gsm->network);
1611 free((struct gsm_network *)gsm->network); /* TBD */
1622 char hlr[128], filename[128];
1623 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1626 /* create gsm instance */
1627 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1631 if (!gsm_conf(&gsm->conf)) {
1632 PERROR("%s", gsm_conf_error);
1633 return gsm_exit(-EINVAL);
1637 debug_parse_category_mask(gsm->conf.debug);
1640 if (gsm->conf.hlr[0] == '/')
1641 SCPY(hlr, gsm->conf.hlr);
1643 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1645 // TODO multiple base stations
1646 if (gsm->conf.numbts < 1 || gsm->conf.numbts > 1) {
1647 PERROR("Expecting exactly one BTS. You defined %d.\n", gsm->conf.numbts);
1648 return gsm_exit(-1);
1651 /* bootstrap network */
1652 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);
1653 if (!gsm->network) {
1654 PERROR("Failed to bootstrap GSM network.\n");
1655 return gsm_exit(-1);
1658 /* open gsm loop interface */
1659 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1660 return gsm_exit(-1);
1663 /* open pcap file */
1664 if (gsm->conf.pcapfile[0]) {
1665 if (gsm->conf.pcapfile[0] == '/')
1666 SCPY(filename, gsm->conf.pcapfile);
1668 SPRINT(filename, "%s/%s", CONFIG_DATA, gsm->conf.pcapfile);
1669 pcapfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode);
1671 PERROR("Failed to open file for pcap\n");
1672 return gsm_exit(-1);
1674 e1_set_pcap_fd(pcapfd);