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 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
395 add_trace("cause", "location", "1");
396 add_trace("cause", "value", "47");
397 add_trace("reason", NULL, "callref already in use");
399 mncc = create_mncc(MNCC_REJ_REQ, callref);
401 mncc->cause_location = 1;
402 mncc->cause_value = 47;
403 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
404 new_state(PORT_STATE_RELEASE);
408 p_m_g_callref = callref;
411 /* if blocked, release call with MT_RELEASE_COMPLETE */
412 if (p_m_mISDNport->ifport->block) {
413 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
414 add_trace("cause", "location", "1");
415 add_trace("cause", "value", "27");
416 add_trace("reason", NULL, "port blocked");
418 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
420 mncc->cause_location = 1;
421 mncc->cause_value = 27;
422 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
423 new_state(PORT_STATE_RELEASE);
430 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
432 p_callerinfo.present = INFO_PRESENT_ALLOWED;
433 if (mncc->calling_number[0])
434 SCPY(p_callerinfo.id, mncc->calling_number);
436 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
437 p_callerinfo.screen = INFO_SCREEN_NETWORK;
438 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
439 p_callerinfo.isdn_port = p_m_portnum;
440 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
442 /* dialing information */
443 SCAT(p_dialinginfo.id, mncc->called_number);
444 switch (mncc->called_type) {
446 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
449 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
452 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
455 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
458 if (mncc->emergency) {
459 SCPY(p_dialinginfo.id, "emergency");
461 p_dialinginfo.sending_complete = 1;
463 /* bearer capability */
465 p_capainfo.bearer_capa = INFO_BC_SPEECH;
466 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
467 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
468 p_capainfo.source_mode = B_MODE_TRANSPARENT;
469 p_m_g_mode = p_capainfo.source_mode;
474 ret = channel = hunt_bchannel();
479 ret = seize_bchannel(channel, 1);
482 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
483 add_trace("cause", "location", "1");
484 add_trace("cause", "value", "34");
485 add_trace("reason", NULL, "no channel");
487 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
489 mncc->cause_location = 1;
490 mncc->cause_value = 34;
491 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
492 new_state(PORT_STATE_RELEASE);
496 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
497 if (bchannel_open(p_m_b_index))
500 /* what infos did we got ... */
501 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
502 add_trace("subscr", "number", "%s", p_callerinfo.id);
503 add_trace("dialing", "number", "%s", p_dialinginfo.id);
506 /* create endpoint */
508 FATAL("Incoming call but already got an endpoint.\n");
509 if (!(epoint = new Endpoint(p_serial, 0)))
510 FATAL("No memory for Endpoint instance\n");
511 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
512 FATAL("No memory for Endpoint Application instance\n");
513 epointlist_new(epoint->ep_serial);
515 /* modify lchan to GSM codec V1 */
516 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
518 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
519 mode->lchan_mode = 0x01; /* GSM V1 */
520 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
521 send_and_free_mncc(gsm->network, mode->msg_type, mode);
523 /* send call proceeding */
524 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
525 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
526 if (p_m_mISDNport->tones) {
527 proceeding->progress = 1;
528 proceeding->progress_coding = 3; /* GSM */
529 proceeding->progress_location = 1;
530 proceeding->progress_descr = 8;
531 add_trace("progress", "coding", "%d", proceeding->progress_coding);
532 add_trace("progress", "location", "%d", proceeding->progress_location);
533 add_trace("progress", "descr", "%d", proceeding->progress_descr);
535 send_and_free_mncc(gsm->network, proceeding->msg_type, proceeding);
538 new_state(PORT_STATE_IN_PROCEEDING);
540 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
541 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
543 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
544 send_and_free_mncc(gsm->network, frame->msg_type, frame);
545 p_m_g_tch_connected = 1;
548 /* send setup message to endpoit */
549 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
550 message->param.setup.isdn_port = p_m_portnum;
551 message->param.setup.port_type = p_type;
552 // message->param.setup.dtmf = 0;
553 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
554 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
555 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
556 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser_info);
557 message->param.setup.useruser.len = strlen(mncc->useruser_info);
558 message->param.setup.useruser.protocol = mncc->useruser_proto;
559 message_put(message);
562 /* DTMF INDICATION */
563 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
565 struct lcr_msg *message;
566 struct gsm_mncc *resp;
568 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
569 add_trace("keypad", NULL, "%c", mncc->keypad);
571 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
572 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
575 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
576 add_trace("keypad", NULL, "%c", mncc->keypad);
578 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
579 resp->keypad = mncc->keypad;
580 send_and_free_mncc(gsm->network, resp->msg_type, resp);
582 /* send dialing information */
583 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
584 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
585 message_put(message);
587 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
589 struct gsm_mncc *resp;
591 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
592 add_trace("keypad", NULL, "%c", mncc->keypad);
596 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
597 add_trace("keypad", NULL, "%c", mncc->keypad);
599 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
600 resp->keypad = mncc->keypad;
601 send_and_free_mncc(gsm->network, resp->msg_type, resp);
604 /* PROCEEDING INDICATION */
605 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
607 struct gsm_mncc *mode;
609 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
611 add_trace("cause", "location", "%", mncc->cause_location);
612 add_trace("cause", "value", "%", mncc->cause_value);
616 /* modify lchan to GSM codec V1 */
617 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
618 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
619 mode->lchan_mode = 0x01; /* GSM V1 */
620 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
621 send_and_free_mncc(gsm->network, mode->msg_type, mode);
626 /* ALERTING INDICATION */
627 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
629 struct lcr_msg *message;
631 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
634 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
635 message_put(message);
637 new_state(PORT_STATE_OUT_ALERTING);
641 /* CONNECT INDICATION */
642 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
644 struct gsm_mncc *resp, *frame;
645 struct lcr_msg *message;
647 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
651 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
652 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
654 send_and_free_mncc(gsm->network, resp->msg_type, resp);
656 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
657 message_put(message);
659 new_state(PORT_STATE_CONNECT);
661 if (!p_m_g_tch_connected) { /* only if ... */
662 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
664 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
665 send_and_free_mncc(gsm->network, frame->msg_type, frame);
666 p_m_g_tch_connected = 1;
670 /* CONNECT ACK INDICATION */
671 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
673 struct gsm_mncc *frame;
675 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
678 new_state(PORT_STATE_CONNECT);
680 if (!p_m_g_tch_connected) { /* only if ... */
681 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
683 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
684 send_and_free_mncc(gsm->network, frame->msg_type, frame);
685 p_m_g_tch_connected = 1;
689 /* DISCONNECT INDICATION */
690 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
692 struct lcr_msg *message;
693 int cause = 16, location = 0;
694 struct gsm_mncc *resp;
696 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
698 location = mncc->cause_location;
699 cause = mncc->cause_value;
700 add_trace("cause", "location", "%d", location);
701 add_trace("cause", "value", "%d", cause);
706 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
707 add_trace("cause", "location", "%d", 1);
708 add_trace("cause", "value", "%d", cause);
710 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
712 resp->cause_location = 1;
713 resp->cause_value = cause;
714 send_and_free_mncc(gsm->network, resp->msg_type, resp);
716 /* sending release to endpoint */
717 while(p_epointlist) {
718 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
719 message->param.disconnectinfo.cause = cause;
720 message->param.disconnectinfo.location = location;
721 message_put(message);
723 free_epointlist(p_epointlist);
725 new_state(PORT_STATE_RELEASE);
729 /* CC_RELEASE INDICATION */
730 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
732 int location = 0, cause = 16;
733 struct lcr_msg *message;
735 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
737 location = mncc->cause_location;
738 cause = mncc->cause_value;
739 add_trace("cause", "location", "%d", location);
740 add_trace("cause", "value", "%d", cause);
744 /* sending release to endpoint */
745 while(p_epointlist) {
746 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
747 message->param.disconnectinfo.cause = cause;
748 message->param.disconnectinfo.location = location;
749 message_put(message);
751 free_epointlist(p_epointlist);
753 new_state(PORT_STATE_RELEASE);
757 /* NOTIFY INDICATION */
758 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
760 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
765 /* HOLD INDICATION */
766 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
768 struct lcr_msg *message;
769 struct gsm_mncc *resp, *frame;
771 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
774 /* notify the hold of call */
775 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
776 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
777 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
778 message_put(message);
780 /* acknowledge hold */
781 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
783 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
784 send_and_free_mncc(gsm->network, resp->msg_type, resp);
787 if (p_m_g_tch_connected) { /* it should be true */
788 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
790 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
791 send_and_free_mncc(gsm->network, frame->msg_type, frame);
792 p_m_g_tch_connected = 0;
797 /* RETRIEVE INDICATION */
798 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
800 struct lcr_msg *message;
801 struct gsm_mncc *resp, *frame;
803 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
806 /* notify the retrieve of call */
807 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
808 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
809 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
810 message_put(message);
812 /* acknowledge retr */
813 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
815 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
816 send_and_free_mncc(gsm->network, resp->msg_type, resp);
819 if (!p_m_g_tch_connected) { /* it should be true */
820 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
822 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
823 send_and_free_mncc(gsm->network, frame->msg_type, frame);
824 p_m_g_tch_connected = 1;
829 * BSC sends message to port
831 static int message_bcs(void *net, int msg_type, void *arg)
833 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
834 unsigned int callref = mncc->callref;
836 class Pgsm *pgsm = NULL;
838 struct mISDNport *mISDNport;
840 /* Special messages */
845 callref = mncc->callref;
848 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
849 pgsm = (class Pgsm *)port;
850 if (pgsm->p_m_g_callref == callref) {
857 if (msg_type == GSM_TRAU_FRAME) {
859 pgsm->trau_receive((struct gsm_trau_frame *)arg);
864 if (msg_type != MNCC_SETUP_IND)
867 mISDNport = mISDNport_first;
871 mISDNport = mISDNport->next;
874 struct gsm_mncc *rej;
876 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
877 add_trace("cause", "location", "1");
878 add_trace("cause", "value", "27");
879 add_trace("reason", NULL, "GSM port not loaded");
881 rej = create_mncc(MNCC_REJ_REQ, callref);
883 rej->cause_location = 1;
884 rej->cause_value = 27;
885 send_and_free_mncc(gsm->network, rej->msg_type, rej);
888 /* creating port object, transparent until setup with hdlc */
889 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
890 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
892 FATAL("Cannot create Port instance.\n");
897 pgsm->setup_ind(msg_type, callref, mncc);
900 case MNCC_START_DTMF_IND:
901 pgsm->start_dtmf_ind(msg_type, callref, mncc);
904 case MNCC_STOP_DTMF_IND:
905 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
908 case MNCC_CALL_CONF_IND:
909 pgsm->call_conf_ind(msg_type, callref, mncc);
913 pgsm->alert_ind(msg_type, callref, mncc);
917 pgsm->setup_cnf(msg_type, callref, mncc);
920 case MNCC_SETUP_COMPL_IND:
921 pgsm->setup_compl_ind(msg_type, callref, mncc);
925 pgsm->disc_ind(msg_type, callref, mncc);
931 pgsm->rel_ind(msg_type, callref, mncc);
934 case MNCC_NOTIFY_IND:
935 pgsm->notify_ind(msg_type, callref, mncc);
939 pgsm->hold_ind(msg_type, callref, mncc);
942 case MNCC_RETRIEVE_IND:
943 pgsm->retr_ind(msg_type, callref, mncc);
947 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);
953 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
955 struct lcr_msg *message;
957 struct epoint_list *epointlist;
958 struct gsm_mncc *mncc;
961 /* copy setup infos to port */
962 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
963 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
964 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
965 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
968 if (!p_dialinginfo.id[0]) {
969 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
970 add_trace("failure", NULL, "No dialed subscriber given.");
972 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
973 message->param.disconnectinfo.cause = 28;
974 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
975 message_put(message);
976 new_state(PORT_STATE_RELEASE);
981 /* release if port is blocked */
982 if (p_m_mISDNport->ifport->block) {
983 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
984 add_trace("failure", NULL, "Port blocked.");
986 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
987 message->param.disconnectinfo.cause = 27; // temp. unavail.
988 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
989 message_put(message);
990 new_state(PORT_STATE_RELEASE);
996 ret = channel = hunt_bchannel();
1000 ret = seize_bchannel(channel, 1);
1003 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1004 add_trace("failure", NULL, "No internal audio channel available.");
1006 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1007 message->param.disconnectinfo.cause = 34;
1008 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1009 message_put(message);
1010 new_state(PORT_STATE_RELEASE);
1014 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1015 if (bchannel_open(p_m_b_index))
1018 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1019 /* screen outgoing caller id */
1020 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1021 do_screen(1, p_callerinfo.id2, sizeof(p_callerinfo.id2), &p_callerinfo.ntype2, &p_callerinfo.present2, p_m_mISDNport->ifport->interface);
1023 /* attach only if not already */
1024 epointlist = p_epointlist;
1026 if (epointlist->epoint_id == epoint_id)
1028 epointlist = epointlist->next;
1031 epointlist_new(epoint_id);
1034 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1035 p_m_g_callref = new_callref++;
1036 add_trace("callref", "new", "0x%x", p_m_g_callref);
1039 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1040 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1041 /* caller information */
1043 mncc->calling_plan = 1;
1044 switch (p_callerinfo.ntype) {
1045 case INFO_NTYPE_UNKNOWN:
1046 mncc->calling_type = 0x0;
1048 case INFO_NTYPE_INTERNATIONAL:
1049 mncc->calling_type = 0x1;
1051 case INFO_NTYPE_NATIONAL:
1052 mncc->calling_type = 0x2;
1054 case INFO_NTYPE_SUBSCRIBER:
1055 mncc->calling_type = 0x4;
1057 default: /* INFO_NTYPE_NOTPRESENT */
1061 switch (p_callerinfo.screen) {
1062 case INFO_SCREEN_USER:
1063 mncc->calling_screen = 0;
1065 default: /* INFO_SCREEN_NETWORK */
1066 mncc->calling_screen = 3;
1069 switch (p_callerinfo.present) {
1070 case INFO_PRESENT_ALLOWED:
1071 mncc->calling_present = 0;
1073 case INFO_PRESENT_RESTRICTED:
1074 mncc->calling_present = 1;
1076 default: /* INFO_PRESENT_NOTAVAIL */
1077 mncc->calling_present = 2;
1080 if (mncc->calling) {
1081 SCPY(mncc->calling_number, p_callerinfo.id);
1082 add_trace("calling", "type", "%d", mncc->calling_type);
1083 add_trace("calling", "plan", "%d", mncc->calling_plan);
1084 add_trace("calling", "present", "%d", mncc->calling_present);
1085 add_trace("calling", "screen", "%d", mncc->calling_screen);
1086 add_trace("calling", "number", "%s", mncc->calling_number);
1088 /* dialing information */
1090 SCPY(mncc->called_number, p_dialinginfo.id);
1091 add_trace("dialing", "number", "%s", mncc->calling_number);
1093 /* sending user-user */
1095 /* redirecting number */
1096 mncc->redirecting = 1;
1097 mncc->redirecting_plan = 1;
1098 switch (p_redirinfo.ntype) {
1099 case INFO_NTYPE_UNKNOWN:
1100 mncc->redirecting_type = 0x0;
1102 case INFO_NTYPE_INTERNATIONAL:
1103 mncc->redirecting_type = 0x1;
1105 case INFO_NTYPE_NATIONAL:
1106 mncc->redirecting_type = 0x2;
1108 case INFO_NTYPE_SUBSCRIBER:
1109 mncc->redirecting_type = 0x4;
1111 default: /* INFO_NTYPE_NOTPRESENT */
1112 mncc->redirecting = 0;
1115 switch (p_redirinfo.screen) {
1116 case INFO_SCREEN_USER:
1117 mncc->redirecting_screen = 0;
1119 default: /* INFO_SCREE_NETWORK */
1120 mncc->redirecting_screen = 3;
1123 switch (p_redirinfo.present) {
1124 case INFO_PRESENT_ALLOWED:
1125 mncc->redirecting_present = 0;
1127 case INFO_PRESENT_RESTRICTED:
1128 mncc->redirecting_present = 1;
1130 default: /* INFO_PRESENT_NOTAVAIL */
1131 mncc->redirecting_present = 2;
1134 /* sending redirecting number only in ntmode */
1135 if (mncc->redirecting) {
1136 SCPY(mncc->redirecting_number, p_redirinfo.id);
1137 add_trace("redir", "type", "%d", mncc->redirecting_type);
1138 add_trace("redir", "plan", "%d", mncc->redirecting_plan);
1139 add_trace("redir", "present", "%d", mncc->redirecting_present);
1140 add_trace("redir", "screen", "%d", mncc->redirecting_screen);
1141 add_trace("redir", "number", "%s", mncc->redirecting_number);
1143 /* bearer capability */
1146 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1149 new_state(PORT_STATE_OUT_SETUP);
1151 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1152 message_put(message);
1154 new_state(PORT_STATE_OUT_PROCEEDING);
1157 /* MESSAGE_NOTIFY */
1158 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1160 struct gsm_mncc *mncc;
1163 printf("if = %d\n", param->notifyinfo.notify);
1164 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1165 notify = param->notifyinfo.notify & 0x7f;
1166 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1167 /* queue notification */
1168 if (p_m_g_notify_pending)
1169 message_free(p_m_g_notify_pending);
1170 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1171 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1173 /* sending notification */
1174 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1175 add_trace("notify", NULL, "%d", notify);
1177 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1178 mncc->notify = notify;
1179 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1184 /* MESSAGE_ALERTING */
1185 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1187 struct gsm_mncc *mncc;
1190 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1191 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1192 if (p_m_mISDNport->tones) {
1194 mncc->progress_coding = 3; /* GSM */
1195 mncc->progress_location = 1;
1196 mncc->progress_descr = 8;
1197 add_trace("progress", "coding", "%d", mncc->progress_coding);
1198 add_trace("progress", "location", "%d", mncc->progress_location);
1199 add_trace("progress", "descr", "%d", mncc->progress_descr);
1201 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1204 new_state(PORT_STATE_IN_ALERTING);
1206 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1207 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1209 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1210 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1211 p_m_g_tch_connected = 1;
1215 /* MESSAGE_CONNECT */
1216 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1218 struct gsm_mncc *mncc;
1220 /* copy connected information */
1221 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
1222 /* screen outgoing caller id */
1223 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1226 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1227 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1228 /* caller information */
1229 mncc->connected = 1;
1230 mncc->connected_plan = 1;
1231 switch (p_callerinfo.ntype) {
1232 case INFO_NTYPE_UNKNOWN:
1233 mncc->connected_type = 0x0;
1235 case INFO_NTYPE_INTERNATIONAL:
1236 mncc->connected_type = 0x1;
1238 case INFO_NTYPE_NATIONAL:
1239 mncc->connected_type = 0x2;
1241 case INFO_NTYPE_SUBSCRIBER:
1242 mncc->connected_type = 0x4;
1244 default: /* INFO_NTYPE_NOTPRESENT */
1245 mncc->connected = 0;
1248 switch (p_callerinfo.screen) {
1249 case INFO_SCREEN_USER:
1250 mncc->connected_screen = 0;
1252 default: /* INFO_SCREEN_NETWORK */
1253 mncc->connected_screen = 3;
1256 switch (p_callerinfo.present) {
1257 case INFO_PRESENT_ALLOWED:
1258 mncc->connected_present = 0;
1260 case INFO_PRESENT_RESTRICTED:
1261 mncc->connected_present = 1;
1263 default: /* INFO_PRESENT_NOTAVAIL */
1264 mncc->connected_present = 2;
1267 if (mncc->connected) {
1268 SCPY(mncc->connected_number, p_connectinfo.id);
1269 add_trace("connected", "type", "%d", mncc->connected_type);
1270 add_trace("connected", "plan", "%d", mncc->connected_plan);
1271 add_trace("connected", "present", "%d", mncc->connected_present);
1272 add_trace("connected", "screen", "%d", mncc->connected_screen);
1273 add_trace("connected", "number", "%s", mncc->connected_number);
1276 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1278 new_state(PORT_STATE_CONNECT_WAITING);
1281 /* MESSAGE_DISCONNECT */
1282 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1284 struct gsm_mncc *mncc;
1286 /* send disconnect */
1287 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1288 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1289 if (p_m_mISDNport->tones) {
1291 mncc->progress_coding = 3; /* GSM */
1292 mncc->progress_location = 1;
1293 mncc->progress_descr = 8;
1294 add_trace("progress", "coding", "%d", mncc->progress_coding);
1295 add_trace("progress", "location", "%d", mncc->progress_location);
1296 add_trace("progress", "descr", "%d", mncc->progress_descr);
1299 mncc->cause_location = param->disconnectinfo.location;
1300 mncc->cause_value = param->disconnectinfo.cause;
1301 add_trace("cause", "location", "%d", mncc->cause_location);
1302 add_trace("cause", "value", "%d", mncc->cause_value);
1304 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1306 new_state(PORT_STATE_OUT_DISCONNECT);
1308 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1309 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1311 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1312 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1313 p_m_g_tch_connected = 1;
1318 /* MESSAGE_RELEASE */
1319 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1321 struct gsm_mncc *mncc;
1324 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1325 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1327 mncc->cause_location = param->disconnectinfo.location;
1328 mncc->cause_value = param->disconnectinfo.cause;
1329 add_trace("cause", "location", "%d", mncc->cause_location);
1330 add_trace("cause", "value", "%d", mncc->cause_value);
1332 send_and_free_mncc(gsm->network, mncc->msg_type, mncc);
1334 new_state(PORT_STATE_RELEASE);
1341 * endpoint sends messages to the port
1343 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1345 if (PmISDN::message_epoint(epoint_id, message_id, param))
1348 switch(message_id) {
1349 case MESSAGE_SETUP: /* dial-out command received from epoint */
1350 if (p_state!=PORT_STATE_IDLE)
1352 message_setup(epoint_id, message_id, param);
1355 case MESSAGE_NOTIFY: /* display and notifications */
1356 message_notify(epoint_id, message_id, param);
1359 // case MESSAGE_FACILITY: /* facility message */
1360 // message_facility(epoint_id, message_id, param);
1363 case MESSAGE_PROCEEDING: /* message not handles */
1366 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1367 if (p_state!=PORT_STATE_IN_PROCEEDING)
1369 message_alerting(epoint_id, message_id, param);
1370 if (p_m_g_notify_pending) {
1371 /* send pending notify message during connect */
1372 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1373 message_free(p_m_g_notify_pending);
1374 p_m_g_notify_pending = NULL;
1378 case MESSAGE_CONNECT: /* call of endpoint is connected */
1379 if (p_state!=PORT_STATE_IN_PROCEEDING
1380 && p_state!=PORT_STATE_IN_ALERTING)
1382 message_connect(epoint_id, message_id, param);
1383 if (p_m_g_notify_pending) {
1384 /* send pending notify message during connect */
1385 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1386 message_free(p_m_g_notify_pending);
1387 p_m_g_notify_pending = NULL;
1391 case MESSAGE_DISCONNECT: /* call has been disconnected */
1392 if (p_state!=PORT_STATE_IN_PROCEEDING
1393 && p_state!=PORT_STATE_IN_ALERTING
1394 && p_state!=PORT_STATE_OUT_SETUP
1395 && p_state!=PORT_STATE_OUT_OVERLAP
1396 && p_state!=PORT_STATE_OUT_PROCEEDING
1397 && p_state!=PORT_STATE_OUT_ALERTING
1398 && p_state!=PORT_STATE_CONNECT
1399 && p_state!=PORT_STATE_CONNECT_WAITING)
1401 message_disconnect(epoint_id, message_id, param);
1404 case MESSAGE_RELEASE: /* release isdn port */
1405 if (p_state==PORT_STATE_RELEASE)
1407 message_release(epoint_id, message_id, param);
1411 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1421 int Pgsm::handler(void)
1425 unsigned char buffer[2048+MISDN_HEADER_LEN];
1426 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1428 if ((ret = PmISDN::handler()))
1431 /* handle destruction */
1437 /* handle message from bchannel */
1438 if (p_m_g_gsm_b_sock > -1) {
1439 ret = recv(p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1440 if (ret >= (int)MISDN_HEADER_LEN) {
1442 /* we don't care about confirms, we use rx data to sync tx */
1445 /* we receive audio data, we respond to it AND we send tones */
1447 bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1449 case PH_ACTIVATE_IND:
1450 p_m_g_gsm_b_active = 1;
1452 case PH_DEACTIVATE_IND:
1453 p_m_g_gsm_b_active = 0;
1458 if (ret < 0 && errno != EWOULDBLOCK)
1459 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1468 * handles bsc select function within LCR's main loop
1470 int handle_gsm(void)
1474 ret1 = bsc_upqueue(gsm->network);
1475 ret2 = bsc_select_main(1); /* polling */
1481 static void gsm_sock_close(void)
1483 if (gsm->gsm_sock > -1)
1484 close(gsm->gsm_sock);
1488 static int gsm_sock_open(char *portname)
1492 unsigned long on = 1;
1493 struct sockaddr_mISDN addr;
1494 struct mISDN_devinfo devinfo;
1497 /* check port counts */
1498 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1500 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1505 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1508 gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1509 if (gsm->gsm_port < 0) {
1510 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1511 return gsm->gsm_port;
1515 devinfo.id = gsm->gsm_port;
1516 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1518 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1521 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1524 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1528 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1531 if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_TE_S0)) < 0) {
1532 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1534 return gsm->gsm_sock;
1536 /* set nonblocking io */
1537 if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1538 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1542 /* bind socket to dchannel */
1543 memset(&addr, 0, sizeof(addr));
1544 addr.family = AF_ISDN;
1545 addr.dev = gsm->gsm_port;
1547 if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1548 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1557 int gsm_exit(int rc)
1559 /* free gsm instance */
1561 if (gsm->gsm_sock > -1)
1563 /* shutdown network */
1565 shutdown_net(gsm->network);
1568 free(gsm->network); /* TBD */
1581 /* create gsm instance */
1582 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1586 if (!gsm_conf(&gsm->conf)) {
1587 PERROR("%s", gsm_conf_error);
1588 return gsm_exit(-EINVAL);
1592 if (gsm->conf.hlr[0] == '/')
1593 SCPY(hlr, gsm->conf.hlr);
1595 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1597 // TODO multiple base stations
1598 if (gsm->conf.numbts < 1 || gsm->conf.numbts > 1) {
1599 PERROR("Expecting exactly one BTS. You defined %d.\n", gsm->conf.numbts);
1600 return gsm_exit(-1);
1602 /* bootstrap network */
1603 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);
1604 if (!gsm->network) {
1605 PERROR("Failed to bootstrap GSM network.\n");
1606 return gsm_exit(-1);
1608 /* open gsm loop interface */
1609 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1610 return gsm_exit(-1);