1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
14 #include "openbsc/openbsc.h"
15 #include "openbsc/mncc.h"
16 #include "openbsc/trau_frame.h"
17 #include "bootstrap.h"
18 #include "gsm_audio.h"
23 #define PF_ISDN AF_ISDN
26 struct lcr_gsm *gsm = NULL;
28 static unsigned int new_callref = 1;
32 * create and send mncc message
34 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
36 struct gsm_mncc *mncc;
38 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
39 mncc->msg_type = msg_type;
40 mncc->callref = callref;
43 static int send_and_free_mncc(void *net, unsigned int msg_type, void *data)
47 ret = mncc_send(net, msg_type, data);
57 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)
59 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
62 p_m_g_gsm_b_sock = -1;
63 p_m_g_gsm_b_index = -1;
64 p_m_g_gsm_b_active = 0;
65 p_m_g_notify_pending = NULL;
66 p_m_g_decoder = gsm_audio_create();
67 p_m_g_encoder = gsm_audio_create();
68 if (!p_m_g_encoder || !p_m_g_decoder) {
69 PERROR("Failed to create GSM audio codec instance\n");
73 p_m_g_tch_connected = 0;
75 PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
83 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
85 /* remove queued message */
86 if (p_m_g_notify_pending)
87 message_free(p_m_g_notify_pending);
89 /* close audio transfer socket */
90 if (p_m_g_gsm_b_sock > -1)
95 gsm_audio_destroy(p_m_g_encoder);
97 gsm_audio_destroy(p_m_g_decoder);
101 /* close bsc side bchannel */
102 void Pgsm::bchannel_close(void)
104 if (p_m_g_gsm_b_sock > -1)
105 close(p_m_g_gsm_b_sock);
106 p_m_g_gsm_b_sock = -1;
107 p_m_g_gsm_b_index = -1;
108 p_m_g_gsm_b_active = 0;
111 /* open bsc side bchannel */
112 int Pgsm::bchannel_open(int index)
116 struct sockaddr_mISDN addr;
117 struct mISDNhead act;
119 if (p_m_g_gsm_b_sock > -1) {
120 PERROR("Socket already created for index %d\n", index);
125 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
127 PERROR("Failed to open bchannel-socket for index %d\n", index);
132 /* set nonblocking io */
133 ret = ioctl(p_m_g_gsm_b_sock, FIONBIO, &on);
135 PERROR("Failed to set bchannel-socket index %d into nonblocking IO\n", index);
140 /* bind socket to bchannel */
141 addr.family = AF_ISDN;
142 addr.dev = gsm->gsm_port;
143 addr.channel = index+1+(index>15);
144 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
146 PERROR("Failed to bind bchannel-socket for index %d\n", index);
150 /* activate bchannel */
151 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
152 act.prim = PH_ACTIVATE_REQ;
154 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
156 PERROR("Failed to activate index %d\n", index);
161 p_m_g_gsm_b_index = index;
166 /* receive from bchannel */
167 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
170 unsigned char frame[33];
171 struct decoded_trau_frame tf;
173 /* encoder init failed */
177 /* (currently) not connected, so don't flood tch! */
178 if (!p_m_g_tch_connected)
181 /* write to rx buffer */
183 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
184 if (p_m_g_rxpos == 160) {
188 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
190 /* set c-bits and t-bits */
196 memset(&tf.c_bits[5], 0, 6);
197 memset(&tf.c_bits[11], 1, 10);
198 memset(&tf.t_bits[0], 1, 4);
200 /* reassemble d-bits */
205 tf.d_bits[i] = (frame[j] >> k) & 1;
218 /* transmit to bchannel */
219 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
221 unsigned char buf[MISDN_HEADER_LEN+len];
222 struct mISDNhead *hh = (struct mISDNhead *)buf;
225 if (!p_m_g_gsm_b_active)
228 /* make and send frame */
229 hh->prim = PH_DATA_REQ;
231 memcpy(buf+MISDN_HEADER_LEN, data, len);
232 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
234 PERROR("Failed to send to socket index %d\n", index);
237 void Pgsm::trau_send(void *_tf)
239 struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_tf;
240 unsigned char data[sizeof(struct gsm_trau_frame) + sizeof(struct decoded_trau_frame)];
241 struct gsm_trau_frame *frame = (struct gsm_trau_frame *)data;
243 frame->msg_type = GSM_TRAU_FRAME;
244 frame->callref = p_m_g_callref;
245 memcpy(frame->data, tf, sizeof(struct decoded_trau_frame));
246 mncc_send(gsm->network, frame->msg_type, frame);
250 void Pgsm::trau_receive(void *_frame)
252 struct gsm_trau_frame *frm = (struct gsm_trau_frame *)_frame;
253 struct decoded_trau_frame *tf = (struct decoded_trau_frame *)frm->data;
254 //struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_frame;
255 unsigned char frame[33];
256 signed short samples[160];
257 unsigned char data[160];
263 // 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]);
264 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)
265 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]);
268 memset(&frame, 0, sizeof(frame));
269 // frame[0] = 0xd << 4;
271 /* reassemble bits */
276 if (tf->d_bits[i] > 1)
278 frame[j] |= (tf->d_bits[i] << k);
287 gsm_audio_decode(p_m_g_decoder, frame, samples);
288 for (i = 0; i < 160; i++) {
289 data[i] = audio_s16_to_law[samples[i] & 0xffff];
293 bchannel_send(PH_DATA_REQ, 0, data, 160);
300 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
304 /* select message and primitive text */
305 SCPY(msgtext, get_mncc_name(msg_type));
308 if (direction == DIRECTION_OUT)
309 SCAT(msgtext, " MSC->BSC");
311 SCAT(msgtext, " MSC<-BSC");
313 /* init trace with given values */
314 start_trace(mISDNport?mISDNport->portnum:-1,
315 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
316 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
317 port?port->p_dialinginfo.id:NULL,
320 port?port->p_serial:0,
326 /* select bchannel */
327 int Pgsm::hunt_bchannel(void)
332 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
333 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
334 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
335 add_trace("conclusion", NULL, "all channels are reserved");
337 return(-34); // no channel
342 while(i < p_m_mISDNport->b_num) {
343 if (p_m_mISDNport->b_port[i] == NULL) {
344 channel = i+1+(i>=15);
350 add_trace("conclusion", NULL, "no channel available");
352 return(-6); // channel unacceptable
354 add_trace("conclusion", NULL, "channel available");
355 add_trace("connect", "channel", "%d", channel);
362 * handles all indications
364 /* SETUP INDICATION */
365 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
368 class Endpoint *epoint;
369 struct lcr_msg *message;
371 struct gsm_mncc *mode, *proceeding, *frame;
373 /* emergency shutdown */
374 printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
375 if (mncc->emergency && !gsm->conf.noemergshut) {
376 start_trace(p_m_mISDNport->portnum,
377 p_m_mISDNport->ifport->interface,
383 "EMERGENCY SHUTDOWN (due to received emergency call)");
387 /* process given callref */
388 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
389 add_trace("callref", "new", "0x%x", callref);
391 /* release in case the ID is already in use */
392 add_trace("error", NULL, "callref already in use");
394 mncc = create_mncc(MNCC_REJ_REQ, callref);
395 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
397 mncc->cause_coding = 3;
398 mncc->cause_location = 1;
399 mncc->cause_value = 47;
400 add_trace("cause", "coding", "%d", mncc->cause_coding);
401 add_trace("cause", "location", "%d", mncc->cause_location);
402 add_trace("cause", "value", "%d", mncc->cause_value);
403 add_trace("reason", NULL, "callref already in use");
405 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
406 new_state(PORT_STATE_RELEASE);
410 p_m_g_callref = callref;
413 /* if blocked, release call with MT_RELEASE_COMPLETE */
414 if (p_m_mISDNport->ifport->block) {
415 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
416 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
418 mncc->cause_coding = 3;
419 mncc->cause_location = 1;
420 mncc->cause_value = 27;
421 add_trace("cause", "coding", "%d", mncc->cause_coding);
422 add_trace("cause", "location", "%d", mncc->cause_location);
423 add_trace("cause", "value", "%d", mncc->cause_value);
424 add_trace("reason", NULL, "port is blocked");
426 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
427 new_state(PORT_STATE_RELEASE);
434 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
436 p_callerinfo.present = INFO_PRESENT_ALLOWED;
437 if (mncc->calling_number[0])
438 SCPY(p_callerinfo.id, mncc->calling_number);
440 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
441 p_callerinfo.screen = INFO_SCREEN_NETWORK;
442 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
443 p_callerinfo.isdn_port = p_m_portnum;
444 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
446 /* dialing information */
447 SCAT(p_dialinginfo.id, mncc->called_number);
448 switch (mncc->called_type) {
450 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
453 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
456 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
459 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
462 if (mncc->emergency) {
463 SCPY(p_dialinginfo.id, "emergency");
465 p_dialinginfo.sending_complete = 1;
467 /* bearer capability */
469 p_capainfo.bearer_capa = INFO_BC_SPEECH;
470 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
471 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
472 p_capainfo.source_mode = B_MODE_TRANSPARENT;
473 p_m_g_mode = p_capainfo.source_mode;
478 ret = channel = hunt_bchannel();
483 ret = seize_bchannel(channel, 1);
486 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
487 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
489 mncc->cause_coding = 3;
490 mncc->cause_location = 1;
491 mncc->cause_value = 34;
492 add_trace("cause", "coding", "%d", mncc->cause_coding);
493 add_trace("cause", "location", "%d", mncc->cause_location);
494 add_trace("cause", "value", "%d", mncc->cause_value);
495 add_trace("reason", NULL, "no channel");
497 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
498 new_state(PORT_STATE_RELEASE);
502 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
503 if (bchannel_open(p_m_b_index))
506 /* what infos did we got ... */
507 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
508 add_trace("subscr", "number", "%s", p_callerinfo.id);
509 add_trace("dialing", "number", "%s", p_dialinginfo.id);
512 /* create endpoint */
514 FATAL("Incoming call but already got an endpoint.\n");
515 if (!(epoint = new Endpoint(p_serial, 0)))
516 FATAL("No memory for Endpoint instance\n");
517 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
518 FATAL("No memory for Endpoint Application instance\n");
519 epointlist_new(epoint->ep_serial);
521 /* modify lchan to GSM codec V1 */
522 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
523 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
524 mode->lchan_mode = 0x01; /* GSM V1 */
525 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
527 send_and_free_mncc(gsm->network, mode->msg_type, mode);
529 /* send call proceeding */
530 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
531 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
532 if (p_m_mISDNport->tones) {
533 proceeding->progress = 1;
534 proceeding->progress_coding = 3; /* GSM */
535 proceeding->progress_location = 1;
536 proceeding->progress_descr = 8;
537 add_trace("progress", "coding", "%d", proceeding->progress_coding);
538 add_trace("progress", "location", "%d", proceeding->progress_location);
539 add_trace("progress", "descr", "%d", proceeding->progress_descr);
542 send_and_free_mncc(gsm->network, proceeding->msg_type, proceeding);
544 new_state(PORT_STATE_IN_PROCEEDING);
546 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
547 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
549 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
550 send_and_free_mncc(gsm->network, frame->msg_type, frame);
551 p_m_g_tch_connected = 1;
554 /* send setup message to endpoit */
555 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
556 message->param.setup.isdn_port = p_m_portnum;
557 message->param.setup.port_type = p_type;
558 // message->param.setup.dtmf = 0;
559 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
560 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
561 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
562 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser_info);
563 message->param.setup.useruser.len = strlen(mncc->useruser_info);
564 message->param.setup.useruser.protocol = mncc->useruser_proto;
565 message_put(message);
568 /* DTMF INDICATION */
569 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
571 struct lcr_msg *message;
572 struct gsm_mncc *resp;
574 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
575 add_trace("keypad", NULL, "%c", mncc->keypad);
577 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
578 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
581 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
582 add_trace("keypad", NULL, "%c", mncc->keypad);
584 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
585 resp->keypad = mncc->keypad;
586 send_and_free_mncc(gsm->network, resp->msg_type, resp);
588 /* send dialing information */
589 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
590 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
591 message_put(message);
593 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
595 struct gsm_mncc *resp;
597 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
598 add_trace("keypad", NULL, "%c", mncc->keypad);
602 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
603 add_trace("keypad", NULL, "%c", mncc->keypad);
605 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
606 resp->keypad = mncc->keypad;
607 send_and_free_mncc(gsm->network, resp->msg_type, resp);
610 /* PROCEEDING INDICATION */
611 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
613 struct gsm_mncc *mode;
615 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
617 add_trace("cause", "coding", "%d", mncc->cause_coding);
618 add_trace("cause", "location", "%", mncc->cause_location);
619 add_trace("cause", "value", "%", mncc->cause_value);
623 /* modify lchan to GSM codec V1 */
624 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
625 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
626 mode->lchan_mode = 0x01; /* GSM V1 */
627 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
629 send_and_free_mncc(gsm->network, mode->msg_type, mode);
633 /* ALERTING INDICATION */
634 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
636 struct lcr_msg *message;
638 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
641 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
642 message_put(message);
644 new_state(PORT_STATE_OUT_ALERTING);
648 /* CONNECT INDICATION */
649 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
651 struct gsm_mncc *resp, *frame;
652 struct lcr_msg *message;
654 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
658 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
659 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
661 send_and_free_mncc(gsm->network, resp->msg_type, resp);
663 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
664 message_put(message);
666 new_state(PORT_STATE_CONNECT);
668 if (!p_m_g_tch_connected) { /* only if ... */
669 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
671 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
672 send_and_free_mncc(gsm->network, frame->msg_type, frame);
673 p_m_g_tch_connected = 1;
677 /* CONNECT ACK INDICATION */
678 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
680 struct gsm_mncc *frame;
682 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
685 new_state(PORT_STATE_CONNECT);
687 if (!p_m_g_tch_connected) { /* only if ... */
688 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
690 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
691 send_and_free_mncc(gsm->network, frame->msg_type, frame);
692 p_m_g_tch_connected = 1;
696 /* DISCONNECT INDICATION */
697 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
699 struct lcr_msg *message;
700 int cause = 16, location = 0;
701 struct gsm_mncc *resp;
703 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
705 location = mncc->cause_location;
706 cause = mncc->cause_value;
707 add_trace("cause", "coding", "%d", mncc->cause_coding);
708 add_trace("cause", "location", "%d", location);
709 add_trace("cause", "value", "%d", cause);
714 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
715 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
718 resp->cause_coding = 3;
719 resp->cause_location = 1;
720 resp->cause_value = cause;
721 add_trace("cause", "coding", "%d", resp->cause_coding);
722 add_trace("cause", "location", "%d", resp->cause_location);
723 add_trace("cause", "value", "%d", resp->cause_value);
726 send_and_free_mncc(gsm->network, resp->msg_type, resp);
728 /* sending release to endpoint */
729 while(p_epointlist) {
730 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
731 message->param.disconnectinfo.cause = cause;
732 message->param.disconnectinfo.location = location;
733 message_put(message);
735 free_epointlist(p_epointlist);
737 new_state(PORT_STATE_RELEASE);
741 /* CC_RELEASE INDICATION */
742 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
744 int location = 0, cause = 16;
745 struct lcr_msg *message;
747 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
749 location = mncc->cause_location;
750 cause = mncc->cause_value;
751 add_trace("cause", "coding", "%d", mncc->cause_coding);
752 add_trace("cause", "location", "%d", mncc->cause_location);
753 add_trace("cause", "value", "%d", mncc->cause_value);
757 /* sending release to endpoint */
758 while(p_epointlist) {
759 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
760 message->param.disconnectinfo.cause = cause;
761 message->param.disconnectinfo.location = location;
762 message_put(message);
764 free_epointlist(p_epointlist);
766 new_state(PORT_STATE_RELEASE);
770 /* NOTIFY INDICATION */
771 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
773 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
778 /* HOLD INDICATION */
779 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
781 struct lcr_msg *message;
782 struct gsm_mncc *resp, *frame;
784 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
787 /* notify the hold of call */
788 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
789 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
790 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
791 message_put(message);
793 /* acknowledge hold */
794 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
796 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
797 send_and_free_mncc(gsm->network, resp->msg_type, resp);
800 if (p_m_g_tch_connected) { /* it should be true */
801 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
803 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
804 send_and_free_mncc(gsm->network, frame->msg_type, frame);
805 p_m_g_tch_connected = 0;
810 /* RETRIEVE INDICATION */
811 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
813 struct lcr_msg *message;
814 struct gsm_mncc *resp, *frame;
816 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
819 /* notify the retrieve of call */
820 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
821 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
822 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
823 message_put(message);
825 /* acknowledge retr */
826 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
828 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
829 send_and_free_mncc(gsm->network, resp->msg_type, resp);
832 if (!p_m_g_tch_connected) { /* it should be true */
833 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
835 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
836 send_and_free_mncc(gsm->network, frame->msg_type, frame);
837 p_m_g_tch_connected = 1;
842 * BSC sends message to port
844 static int message_bcs(void *net, int msg_type, void *arg)
846 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
847 unsigned int callref = mncc->callref;
849 class Pgsm *pgsm = NULL;
851 struct mISDNport *mISDNport;
853 /* Special messages */
858 callref = mncc->callref;
861 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
862 pgsm = (class Pgsm *)port;
863 if (pgsm->p_m_g_callref == callref) {
870 if (msg_type == GSM_TRAU_FRAME) {
872 pgsm->trau_receive((struct gsm_trau_frame *)arg);
877 if (msg_type != MNCC_SETUP_IND)
880 mISDNport = mISDNport_first;
884 mISDNport = mISDNport->next;
887 struct gsm_mncc *rej;
889 rej = create_mncc(MNCC_REJ_REQ, callref);
891 rej->cause_coding = 3;
892 rej->cause_location = 1;
893 rej->cause_value = 27;
894 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
895 add_trace("cause", "coding", "%d", rej->cause_coding);
896 add_trace("cause", "location", "%d", rej->cause_location);
897 add_trace("cause", "value", "%d", rej->cause_value);
899 send_and_free_mncc(gsm->network, rej->msg_type, rej);
902 /* creating port object, transparent until setup with hdlc */
903 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
904 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
906 FATAL("Cannot create Port instance.\n");
911 pgsm->setup_ind(msg_type, callref, mncc);
914 case MNCC_START_DTMF_IND:
915 pgsm->start_dtmf_ind(msg_type, callref, mncc);
918 case MNCC_STOP_DTMF_IND:
919 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
922 case MNCC_CALL_CONF_IND:
923 pgsm->call_conf_ind(msg_type, callref, mncc);
927 pgsm->alert_ind(msg_type, callref, mncc);
931 pgsm->setup_cnf(msg_type, callref, mncc);
934 case MNCC_SETUP_COMPL_IND:
935 pgsm->setup_compl_ind(msg_type, callref, mncc);
939 pgsm->disc_ind(msg_type, callref, mncc);
945 pgsm->rel_ind(msg_type, callref, mncc);
948 case MNCC_NOTIFY_IND:
949 pgsm->notify_ind(msg_type, callref, mncc);
953 pgsm->hold_ind(msg_type, callref, mncc);
956 case MNCC_RETRIEVE_IND:
957 pgsm->retr_ind(msg_type, callref, mncc);
961 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);
967 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
969 struct lcr_msg *message;
971 struct epoint_list *epointlist;
972 struct gsm_mncc *mncc;
975 /* copy setup infos to port */
976 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
977 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
978 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
979 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
982 if (!p_dialinginfo.id[0]) {
983 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
984 add_trace("failure", NULL, "No dialed subscriber given.");
986 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
987 message->param.disconnectinfo.cause = 28;
988 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
989 message_put(message);
990 new_state(PORT_STATE_RELEASE);
995 /* release if port is blocked */
996 if (p_m_mISDNport->ifport->block) {
997 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
998 add_trace("failure", NULL, "Port blocked.");
1000 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1001 message->param.disconnectinfo.cause = 27; // temp. unavail.
1002 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1003 message_put(message);
1004 new_state(PORT_STATE_RELEASE);
1010 ret = channel = hunt_bchannel();
1014 ret = seize_bchannel(channel, 1);
1017 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1018 add_trace("failure", NULL, "No internal audio channel available.");
1020 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1021 message->param.disconnectinfo.cause = 34;
1022 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1023 message_put(message);
1024 new_state(PORT_STATE_RELEASE);
1028 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1029 if (bchannel_open(p_m_b_index))
1032 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1033 /* screen outgoing caller id */
1034 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1036 /* attach only if not already */
1037 epointlist = p_epointlist;
1039 if (epointlist->epoint_id == epoint_id)
1041 epointlist = epointlist->next;
1044 epointlist_new(epoint_id);
1047 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1048 p_m_g_callref = new_callref++;
1049 add_trace("callref", "new", "0x%x", p_m_g_callref);
1052 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1053 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1054 /* caller information */
1056 mncc->calling_plan = 1;
1057 switch (p_callerinfo.ntype) {
1058 case INFO_NTYPE_UNKNOWN:
1059 mncc->calling_type = 0x0;
1061 case INFO_NTYPE_INTERNATIONAL:
1062 mncc->calling_type = 0x1;
1064 case INFO_NTYPE_NATIONAL:
1065 mncc->calling_type = 0x2;
1067 case INFO_NTYPE_SUBSCRIBER:
1068 mncc->calling_type = 0x4;
1070 default: /* INFO_NTYPE_NOTPRESENT */
1074 switch (p_callerinfo.screen) {
1075 case INFO_SCREEN_USER:
1076 mncc->calling_screen = 0;
1078 default: /* INFO_SCREEN_NETWORK */
1079 mncc->calling_screen = 3;
1082 switch (p_callerinfo.present) {
1083 case INFO_PRESENT_ALLOWED:
1084 mncc->calling_present = 0;
1086 case INFO_PRESENT_RESTRICTED:
1087 mncc->calling_present = 1;
1089 default: /* INFO_PRESENT_NOTAVAIL */
1090 mncc->calling_present = 2;
1093 if (mncc->calling) {
1094 SCPY(mncc->calling_number, p_callerinfo.id);
1095 add_trace("calling", "type", "%d", mncc->calling_type);
1096 add_trace("calling", "plan", "%d", mncc->calling_plan);
1097 add_trace("calling", "present", "%d", mncc->calling_present);
1098 add_trace("calling", "screen", "%d", mncc->calling_screen);
1099 add_trace("calling", "number", "%s", mncc->calling_number);
1101 /* dialing information */
1103 SCPY(mncc->called_number, p_dialinginfo.id);
1104 add_trace("dialing", "number", "%s", mncc->called_number);
1106 /* sending user-user */
1108 /* redirecting number */
1109 mncc->redirecting = 1;
1110 mncc->redirecting_plan = 1;
1111 switch (p_redirinfo.ntype) {
1112 case INFO_NTYPE_UNKNOWN:
1113 mncc->redirecting_type = 0x0;
1115 case INFO_NTYPE_INTERNATIONAL:
1116 mncc->redirecting_type = 0x1;
1118 case INFO_NTYPE_NATIONAL:
1119 mncc->redirecting_type = 0x2;
1121 case INFO_NTYPE_SUBSCRIBER:
1122 mncc->redirecting_type = 0x4;
1124 default: /* INFO_NTYPE_NOTPRESENT */
1125 mncc->redirecting = 0;
1128 switch (p_redirinfo.screen) {
1129 case INFO_SCREEN_USER:
1130 mncc->redirecting_screen = 0;
1132 default: /* INFO_SCREE_NETWORK */
1133 mncc->redirecting_screen = 3;
1136 switch (p_redirinfo.present) {
1137 case INFO_PRESENT_ALLOWED:
1138 mncc->redirecting_present = 0;
1140 case INFO_PRESENT_RESTRICTED:
1141 mncc->redirecting_present = 1;
1143 default: /* INFO_PRESENT_NOTAVAIL */
1144 mncc->redirecting_present = 2;
1147 /* sending redirecting number only in ntmode */
1148 if (mncc->redirecting) {
1149 SCPY(mncc->redirecting_number, p_redirinfo.id);
1150 add_trace("redir", "type", "%d", mncc->redirecting_type);
1151 add_trace("redir", "plan", "%d", mncc->redirecting_plan);
1152 add_trace("redir", "present", "%d", mncc->redirecting_present);
1153 add_trace("redir", "screen", "%d", mncc->redirecting_screen);
1154 add_trace("redir", "number", "%s", mncc->redirecting_number);
1156 /* bearer capability */
1160 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1162 new_state(PORT_STATE_OUT_SETUP);
1164 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1165 message_put(message);
1167 new_state(PORT_STATE_OUT_PROCEEDING);
1170 /* MESSAGE_NOTIFY */
1171 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1173 struct gsm_mncc *mncc;
1176 printf("if = %d\n", param->notifyinfo.notify);
1177 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1178 notify = param->notifyinfo.notify & 0x7f;
1179 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1180 /* queue notification */
1181 if (p_m_g_notify_pending)
1182 message_free(p_m_g_notify_pending);
1183 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1184 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1186 /* sending notification */
1187 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1188 add_trace("notify", NULL, "%d", notify);
1190 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1191 mncc->notify = notify;
1192 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1197 /* MESSAGE_ALERTING */
1198 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1200 struct gsm_mncc *mncc;
1203 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1204 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1205 if (p_m_mISDNport->tones) {
1207 mncc->progress_coding = 3; /* GSM */
1208 mncc->progress_location = 1;
1209 mncc->progress_descr = 8;
1210 add_trace("progress", "coding", "%d", mncc->progress_coding);
1211 add_trace("progress", "location", "%d", mncc->progress_location);
1212 add_trace("progress", "descr", "%d", mncc->progress_descr);
1215 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1217 new_state(PORT_STATE_IN_ALERTING);
1219 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1220 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1222 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1223 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1224 p_m_g_tch_connected = 1;
1228 /* MESSAGE_CONNECT */
1229 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1231 struct gsm_mncc *mncc;
1233 /* copy connected information */
1234 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
1235 /* screen outgoing caller id */
1236 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1239 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1240 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1241 /* caller information */
1242 mncc->connected = 1;
1243 mncc->connected_plan = 1;
1244 switch (p_callerinfo.ntype) {
1245 case INFO_NTYPE_UNKNOWN:
1246 mncc->connected_type = 0x0;
1248 case INFO_NTYPE_INTERNATIONAL:
1249 mncc->connected_type = 0x1;
1251 case INFO_NTYPE_NATIONAL:
1252 mncc->connected_type = 0x2;
1254 case INFO_NTYPE_SUBSCRIBER:
1255 mncc->connected_type = 0x4;
1257 default: /* INFO_NTYPE_NOTPRESENT */
1258 mncc->connected = 0;
1261 switch (p_callerinfo.screen) {
1262 case INFO_SCREEN_USER:
1263 mncc->connected_screen = 0;
1265 default: /* INFO_SCREEN_NETWORK */
1266 mncc->connected_screen = 3;
1269 switch (p_callerinfo.present) {
1270 case INFO_PRESENT_ALLOWED:
1271 mncc->connected_present = 0;
1273 case INFO_PRESENT_RESTRICTED:
1274 mncc->connected_present = 1;
1276 default: /* INFO_PRESENT_NOTAVAIL */
1277 mncc->connected_present = 2;
1280 if (mncc->connected) {
1281 SCPY(mncc->connected_number, p_connectinfo.id);
1282 add_trace("connected", "type", "%d", mncc->connected_type);
1283 add_trace("connected", "plan", "%d", mncc->connected_plan);
1284 add_trace("connected", "present", "%d", mncc->connected_present);
1285 add_trace("connected", "screen", "%d", mncc->connected_screen);
1286 add_trace("connected", "number", "%s", mncc->connected_number);
1289 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1291 new_state(PORT_STATE_CONNECT_WAITING);
1294 /* MESSAGE_DISCONNECT */
1295 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1297 struct gsm_mncc *mncc;
1299 /* send disconnect */
1300 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1301 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1302 if (p_m_mISDNport->tones) {
1304 mncc->progress_coding = 3; /* GSM */
1305 mncc->progress_location = 1;
1306 mncc->progress_descr = 8;
1307 add_trace("progress", "coding", "%d", mncc->progress_coding);
1308 add_trace("progress", "location", "%d", mncc->progress_location);
1309 add_trace("progress", "descr", "%d", mncc->progress_descr);
1312 mncc->cause_coding = 3;
1313 mncc->cause_location = param->disconnectinfo.location;
1314 mncc->cause_value = param->disconnectinfo.cause;
1315 add_trace("cause", "coding", "%d", mncc->cause_coding);
1316 add_trace("cause", "location", "%d", mncc->cause_location);
1317 add_trace("cause", "value", "%d", mncc->cause_value);
1319 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1321 new_state(PORT_STATE_OUT_DISCONNECT);
1323 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1324 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1326 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1327 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1328 p_m_g_tch_connected = 1;
1333 /* MESSAGE_RELEASE */
1334 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1336 struct gsm_mncc *mncc;
1339 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1340 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1342 mncc->cause_coding = 3;
1343 mncc->cause_location = param->disconnectinfo.location;
1344 mncc->cause_value = param->disconnectinfo.cause;
1345 add_trace("cause", "coding", "%d", mncc->cause_coding);
1346 add_trace("cause", "location", "%d", mncc->cause_location);
1347 add_trace("cause", "value", "%d", mncc->cause_value);
1349 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1351 new_state(PORT_STATE_RELEASE);
1358 * endpoint sends messages to the port
1360 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1362 if (PmISDN::message_epoint(epoint_id, message_id, param))
1365 switch(message_id) {
1366 case MESSAGE_SETUP: /* dial-out command received from epoint */
1367 if (p_state!=PORT_STATE_IDLE)
1369 message_setup(epoint_id, message_id, param);
1372 case MESSAGE_NOTIFY: /* display and notifications */
1373 message_notify(epoint_id, message_id, param);
1376 // case MESSAGE_FACILITY: /* facility message */
1377 // message_facility(epoint_id, message_id, param);
1380 case MESSAGE_PROCEEDING: /* message not handles */
1383 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1384 if (p_state!=PORT_STATE_IN_PROCEEDING)
1386 message_alerting(epoint_id, message_id, param);
1387 if (p_m_g_notify_pending) {
1388 /* send pending notify message during connect */
1389 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1390 message_free(p_m_g_notify_pending);
1391 p_m_g_notify_pending = NULL;
1395 case MESSAGE_CONNECT: /* call of endpoint is connected */
1396 if (p_state!=PORT_STATE_IN_PROCEEDING
1397 && p_state!=PORT_STATE_IN_ALERTING)
1399 message_connect(epoint_id, message_id, param);
1400 if (p_m_g_notify_pending) {
1401 /* send pending notify message during connect */
1402 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1403 message_free(p_m_g_notify_pending);
1404 p_m_g_notify_pending = NULL;
1408 case MESSAGE_DISCONNECT: /* call has been disconnected */
1409 if (p_state!=PORT_STATE_IN_PROCEEDING
1410 && p_state!=PORT_STATE_IN_ALERTING
1411 && p_state!=PORT_STATE_OUT_SETUP
1412 && p_state!=PORT_STATE_OUT_OVERLAP
1413 && p_state!=PORT_STATE_OUT_PROCEEDING
1414 && p_state!=PORT_STATE_OUT_ALERTING
1415 && p_state!=PORT_STATE_CONNECT
1416 && p_state!=PORT_STATE_CONNECT_WAITING)
1418 message_disconnect(epoint_id, message_id, param);
1421 case MESSAGE_RELEASE: /* release isdn port */
1422 if (p_state==PORT_STATE_RELEASE)
1424 message_release(epoint_id, message_id, param);
1428 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1438 int Pgsm::handler(void)
1442 unsigned char buffer[2048+MISDN_HEADER_LEN];
1443 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1445 if ((ret = PmISDN::handler()))
1448 /* handle destruction */
1454 /* handle message from bchannel */
1455 if (p_m_g_gsm_b_sock > -1) {
1456 ret = recv(p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1457 if (ret >= (int)MISDN_HEADER_LEN) {
1459 /* we don't care about confirms, we use rx data to sync tx */
1462 /* we receive audio data, we respond to it AND we send tones */
1464 bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1466 case PH_ACTIVATE_IND:
1467 p_m_g_gsm_b_active = 1;
1469 case PH_DEACTIVATE_IND:
1470 p_m_g_gsm_b_active = 0;
1475 if (ret < 0 && errno != EWOULDBLOCK)
1476 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1485 * handles bsc select function within LCR's main loop
1487 int handle_gsm(void)
1491 ret1 = bsc_upqueue(gsm->network);
1492 ret2 = bsc_select_main(1); /* polling */
1498 static void gsm_sock_close(void)
1500 if (gsm->gsm_sock > -1)
1501 close(gsm->gsm_sock);
1505 static int gsm_sock_open(char *portname)
1509 unsigned long on = 1;
1510 struct sockaddr_mISDN addr;
1511 struct mISDN_devinfo devinfo;
1514 /* check port counts */
1515 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1517 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1522 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1525 gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1526 if (gsm->gsm_port < 0) {
1527 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1528 return gsm->gsm_port;
1532 devinfo.id = gsm->gsm_port;
1533 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1535 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1538 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1541 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1545 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1548 if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_TE_S0)) < 0) {
1549 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1551 return gsm->gsm_sock;
1553 /* set nonblocking io */
1554 if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1555 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1559 /* bind socket to dchannel */
1560 memset(&addr, 0, sizeof(addr));
1561 addr.family = AF_ISDN;
1562 addr.dev = gsm->gsm_port;
1564 if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1565 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1574 int gsm_exit(int rc)
1576 /* free gsm instance */
1578 if (gsm->gsm_sock > -1)
1580 /* shutdown network */
1582 shutdown_net(gsm->network);
1585 free(gsm->network); /* TBD */
1598 /* create gsm instance */
1599 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1603 if (!gsm_conf(&gsm->conf)) {
1604 PERROR("%s", gsm_conf_error);
1605 return gsm_exit(-EINVAL);
1609 debug_parse_category_mask(gsm->conf.debug);
1612 if (gsm->conf.hlr[0] == '/')
1613 SCPY(hlr, gsm->conf.hlr);
1615 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1617 // TODO multiple base stations
1618 if (gsm->conf.numbts < 1 || gsm->conf.numbts > 1) {
1619 PERROR("Expecting exactly one BTS. You defined %d.\n", gsm->conf.numbts);
1620 return gsm_exit(-1);
1622 /* bootstrap network */
1623 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);
1624 if (!gsm->network) {
1625 PERROR("Failed to bootstrap GSM network.\n");
1626 return gsm_exit(-1);
1628 /* open gsm loop interface */
1629 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1630 return gsm_exit(-1);