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 "bootstrap.h"
20 #include "gsm_audio.h"
25 #define PF_ISDN AF_ISDN
28 struct lcr_gsm *gsm = NULL;
30 static unsigned int new_callref = 1;
34 * create and send mncc message
36 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
38 struct gsm_mncc *mncc;
40 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
41 mncc->msg_type = msg_type;
42 mncc->callref = callref;
45 static int send_and_free_mncc(struct gsm_network *net, unsigned int msg_type, void *data)
49 ret = mncc_send(net, msg_type, data);
59 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)
61 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
64 p_m_g_gsm_b_sock = -1;
65 p_m_g_gsm_b_index = -1;
66 p_m_g_gsm_b_active = 0;
67 p_m_g_notify_pending = NULL;
68 p_m_g_decoder = gsm_audio_create();
69 p_m_g_encoder = gsm_audio_create();
70 if (!p_m_g_encoder || !p_m_g_decoder) {
71 PERROR("Failed to create GSM audio codec instance\n");
75 p_m_g_tch_connected = 0;
77 PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
85 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
87 /* remove queued message */
88 if (p_m_g_notify_pending)
89 message_free(p_m_g_notify_pending);
91 /* close audio transfer socket */
92 if (p_m_g_gsm_b_sock > -1)
97 gsm_audio_destroy(p_m_g_encoder);
99 gsm_audio_destroy(p_m_g_decoder);
103 /* close bsc side bchannel */
104 void Pgsm::bchannel_close(void)
106 if (p_m_g_gsm_b_sock > -1)
107 close(p_m_g_gsm_b_sock);
108 p_m_g_gsm_b_sock = -1;
109 p_m_g_gsm_b_index = -1;
110 p_m_g_gsm_b_active = 0;
113 /* open bsc side bchannel */
114 int Pgsm::bchannel_open(int index)
118 struct sockaddr_mISDN addr;
119 struct mISDNhead act;
121 if (p_m_g_gsm_b_sock > -1) {
122 PERROR("Socket already created for index %d\n", index);
127 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
129 PERROR("Failed to open bchannel-socket for index %d\n", index);
134 /* set nonblocking io */
135 ret = ioctl(p_m_g_gsm_b_sock, FIONBIO, &on);
137 PERROR("Failed to set bchannel-socket index %d into nonblocking IO\n", index);
142 /* bind socket to bchannel */
143 addr.family = AF_ISDN;
144 addr.dev = gsm->gsm_port;
145 addr.channel = index+1+(index>15);
146 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
148 PERROR("Failed to bind bchannel-socket for index %d\n", index);
152 /* activate bchannel */
153 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
154 act.prim = PH_ACTIVATE_REQ;
156 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
158 PERROR("Failed to activate index %d\n", index);
163 p_m_g_gsm_b_index = index;
168 /* receive from bchannel */
169 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
172 unsigned char frame[33];
173 struct decoded_trau_frame tf;
175 /* encoder init failed */
179 /* (currently) not connected, so don't flood tch! */
180 if (!p_m_g_tch_connected)
183 /* write to rx buffer */
185 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
186 if (p_m_g_rxpos == 160) {
190 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
192 /* set c-bits and t-bits */
198 memset(&tf.c_bits[5], 0, 6);
199 memset(&tf.c_bits[11], 1, 10);
200 memset(&tf.t_bits[0], 1, 4);
202 /* reassemble d-bits */
207 tf.d_bits[i] = (frame[j] >> k) & 1;
220 /* transmit to bchannel */
221 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
223 unsigned char buf[MISDN_HEADER_LEN+len];
224 struct mISDNhead *hh = (struct mISDNhead *)buf;
227 if (!p_m_g_gsm_b_active)
230 /* make and send frame */
231 hh->prim = PH_DATA_REQ;
233 memcpy(buf+MISDN_HEADER_LEN, data, len);
234 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
236 PERROR("Failed to send to socket index %d\n", index);
239 void Pgsm::trau_send(void *_tf)
241 struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_tf;
242 unsigned char data[sizeof(struct gsm_trau_frame) + sizeof(struct decoded_trau_frame)];
243 struct gsm_trau_frame *frame = (struct gsm_trau_frame *)data;
245 frame->msg_type = GSM_TRAU_FRAME;
246 frame->callref = p_m_g_callref;
247 memcpy(frame->data, tf, sizeof(struct decoded_trau_frame));
248 mncc_send((struct gsm_network *)gsm->network, frame->msg_type, frame);
252 void Pgsm::trau_receive(void *_frame)
254 struct gsm_trau_frame *frm = (struct gsm_trau_frame *)_frame;
255 struct decoded_trau_frame *tf = (struct decoded_trau_frame *)frm->data;
256 //struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_frame;
257 unsigned char frame[33];
258 signed short samples[160];
259 unsigned char data[160];
265 // 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]);
266 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)
267 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]);
270 memset(&frame, 0, sizeof(frame));
271 // frame[0] = 0xd << 4;
273 /* reassemble bits */
278 if (tf->d_bits[i] > 1)
280 frame[j] |= (tf->d_bits[i] << k);
289 gsm_audio_decode(p_m_g_decoder, frame, samples);
290 for (i = 0; i < 160; i++) {
291 data[i] = audio_s16_to_law[samples[i] & 0xffff];
295 bchannel_send(PH_DATA_REQ, 0, data, 160);
302 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
306 /* select message and primitive text */
307 SCPY(msgtext, get_mncc_name(msg_type));
310 if (direction == DIRECTION_OUT)
311 SCAT(msgtext, " MSC->BSC");
313 SCAT(msgtext, " MSC<-BSC");
315 /* init trace with given values */
316 start_trace(mISDNport?mISDNport->portnum:-1,
317 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
318 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
319 port?port->p_dialinginfo.id:NULL,
322 port?port->p_serial:0,
328 /* select bchannel */
329 int Pgsm::hunt_bchannel(void)
334 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
335 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
336 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
337 add_trace("conclusion", NULL, "all channels are reserved");
339 return(-34); // no channel
344 while(i < p_m_mISDNport->b_num) {
345 if (p_m_mISDNport->b_port[i] == NULL) {
346 channel = i+1+(i>=15);
352 add_trace("conclusion", NULL, "no channel available");
354 return(-6); // channel unacceptable
356 add_trace("conclusion", NULL, "channel available");
357 add_trace("connect", "channel", "%d", channel);
364 * handles all indications
366 /* SETUP INDICATION */
367 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
370 class Endpoint *epoint;
371 struct lcr_msg *message;
373 struct gsm_mncc *mode, *proceeding, *frame;
375 /* emergency shutdown */
376 printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
377 if (mncc->emergency && !gsm->conf.noemergshut) {
378 start_trace(p_m_mISDNport->portnum,
379 p_m_mISDNport->ifport->interface,
385 "EMERGENCY SHUTDOWN (due to received emergency call)");
389 /* process given callref */
390 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
391 add_trace("callref", "new", "0x%x", callref);
393 /* release in case the ID is already in use */
394 add_trace("error", NULL, "callref already in use");
396 mncc = create_mncc(MNCC_REJ_REQ, callref);
397 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
399 mncc->cause_coding = 3;
400 mncc->cause_location = 1;
401 mncc->cause_value = 47;
402 add_trace("cause", "coding", "%d", mncc->cause_coding);
403 add_trace("cause", "location", "%d", mncc->cause_location);
404 add_trace("cause", "value", "%d", mncc->cause_value);
405 add_trace("reason", NULL, "callref already in use");
407 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
408 new_state(PORT_STATE_RELEASE);
412 p_m_g_callref = callref;
415 /* if blocked, release call with MT_RELEASE_COMPLETE */
416 if (p_m_mISDNport->ifport->block) {
417 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
418 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
420 mncc->cause_coding = 3;
421 mncc->cause_location = 1;
422 mncc->cause_value = 27;
423 add_trace("cause", "coding", "%d", mncc->cause_coding);
424 add_trace("cause", "location", "%d", mncc->cause_location);
425 add_trace("cause", "value", "%d", mncc->cause_value);
426 add_trace("reason", NULL, "port is blocked");
428 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
429 new_state(PORT_STATE_RELEASE);
436 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
438 p_callerinfo.present = INFO_PRESENT_ALLOWED;
439 if (mncc->calling_number[0])
440 SCPY(p_callerinfo.id, mncc->calling_number);
442 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
443 p_callerinfo.screen = INFO_SCREEN_NETWORK;
444 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
445 p_callerinfo.isdn_port = p_m_portnum;
446 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
448 /* dialing information */
449 SCAT(p_dialinginfo.id, mncc->called_number);
450 switch (mncc->called_type) {
452 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
455 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
458 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
461 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
464 if (mncc->emergency) {
465 SCPY(p_dialinginfo.id, "emergency");
467 p_dialinginfo.sending_complete = 1;
469 /* bearer capability */
471 p_capainfo.bearer_capa = INFO_BC_SPEECH;
472 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
473 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
474 p_capainfo.source_mode = B_MODE_TRANSPARENT;
475 p_m_g_mode = p_capainfo.source_mode;
480 ret = channel = hunt_bchannel();
485 ret = seize_bchannel(channel, 1);
488 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
489 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
491 mncc->cause_coding = 3;
492 mncc->cause_location = 1;
493 mncc->cause_value = 34;
494 add_trace("cause", "coding", "%d", mncc->cause_coding);
495 add_trace("cause", "location", "%d", mncc->cause_location);
496 add_trace("cause", "value", "%d", mncc->cause_value);
497 add_trace("reason", NULL, "no channel");
499 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
500 new_state(PORT_STATE_RELEASE);
504 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
505 if (bchannel_open(p_m_b_index))
508 /* what infos did we got ... */
509 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
510 add_trace("subscr", "number", "%s", p_callerinfo.id);
511 add_trace("dialing", "number", "%s", p_dialinginfo.id);
514 /* create endpoint */
516 FATAL("Incoming call but already got an endpoint.\n");
517 if (!(epoint = new Endpoint(p_serial, 0)))
518 FATAL("No memory for Endpoint instance\n");
519 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
520 FATAL("No memory for Endpoint Application instance\n");
521 epointlist_new(epoint->ep_serial);
523 /* modify lchan to GSM codec V1 */
524 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
525 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
526 mode->lchan_mode = 0x01; /* GSM V1 */
527 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
529 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
531 /* send call proceeding */
532 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
533 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
534 if (p_m_mISDNport->tones) {
535 proceeding->progress = 1;
536 proceeding->progress_coding = 3; /* GSM */
537 proceeding->progress_location = 1;
538 proceeding->progress_descr = 8;
539 add_trace("progress", "coding", "%d", proceeding->progress_coding);
540 add_trace("progress", "location", "%d", proceeding->progress_location);
541 add_trace("progress", "descr", "%d", proceeding->progress_descr);
544 send_and_free_mncc((struct gsm_network *)gsm->network, proceeding->msg_type, proceeding);
546 new_state(PORT_STATE_IN_PROCEEDING);
548 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
549 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
551 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
552 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
553 p_m_g_tch_connected = 1;
556 /* send setup message to endpoit */
557 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
558 message->param.setup.isdn_port = p_m_portnum;
559 message->param.setup.port_type = p_type;
560 // message->param.setup.dtmf = 0;
561 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
562 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
563 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
564 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser_info);
565 message->param.setup.useruser.len = strlen(mncc->useruser_info);
566 message->param.setup.useruser.protocol = mncc->useruser_proto;
567 message_put(message);
570 /* DTMF INDICATION */
571 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
573 struct lcr_msg *message;
574 struct gsm_mncc *resp;
576 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
577 add_trace("keypad", NULL, "%c", mncc->keypad);
579 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
580 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
583 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
584 add_trace("keypad", NULL, "%c", mncc->keypad);
586 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
587 resp->keypad = mncc->keypad;
588 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
590 /* send dialing information */
591 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
592 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
593 message_put(message);
595 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
597 struct gsm_mncc *resp;
599 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
600 add_trace("keypad", NULL, "%c", mncc->keypad);
604 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
605 add_trace("keypad", NULL, "%c", mncc->keypad);
607 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
608 resp->keypad = mncc->keypad;
609 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
612 /* PROCEEDING INDICATION */
613 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
615 struct gsm_mncc *mode;
617 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
619 add_trace("cause", "coding", "%d", mncc->cause_coding);
620 add_trace("cause", "location", "%", mncc->cause_location);
621 add_trace("cause", "value", "%", mncc->cause_value);
625 /* modify lchan to GSM codec V1 */
626 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
627 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
628 mode->lchan_mode = 0x01; /* GSM V1 */
629 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
631 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
635 /* ALERTING INDICATION */
636 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
638 struct lcr_msg *message;
640 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
643 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
644 message_put(message);
646 new_state(PORT_STATE_OUT_ALERTING);
650 /* CONNECT INDICATION */
651 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
653 struct gsm_mncc *resp, *frame;
654 struct lcr_msg *message;
656 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
660 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
661 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
663 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
665 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
666 message_put(message);
668 new_state(PORT_STATE_CONNECT);
670 if (!p_m_g_tch_connected) { /* only if ... */
671 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
673 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
674 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
675 p_m_g_tch_connected = 1;
679 /* CONNECT ACK INDICATION */
680 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
682 struct gsm_mncc *frame;
684 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
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 /* DISCONNECT INDICATION */
699 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
701 struct lcr_msg *message;
702 int cause = 16, location = 0;
703 struct gsm_mncc *resp;
705 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
707 location = mncc->cause_location;
708 cause = mncc->cause_value;
709 add_trace("cause", "coding", "%d", mncc->cause_coding);
710 add_trace("cause", "location", "%d", location);
711 add_trace("cause", "value", "%d", cause);
716 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
717 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
720 resp->cause_coding = 3;
721 resp->cause_location = 1;
722 resp->cause_value = cause;
723 add_trace("cause", "coding", "%d", resp->cause_coding);
724 add_trace("cause", "location", "%d", resp->cause_location);
725 add_trace("cause", "value", "%d", resp->cause_value);
728 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
730 /* sending release to endpoint */
731 while(p_epointlist) {
732 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
733 message->param.disconnectinfo.cause = cause;
734 message->param.disconnectinfo.location = location;
735 message_put(message);
737 free_epointlist(p_epointlist);
739 new_state(PORT_STATE_RELEASE);
743 /* CC_RELEASE INDICATION */
744 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
746 int location = 0, cause = 16;
747 struct lcr_msg *message;
749 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
751 location = mncc->cause_location;
752 cause = mncc->cause_value;
753 add_trace("cause", "coding", "%d", mncc->cause_coding);
754 add_trace("cause", "location", "%d", mncc->cause_location);
755 add_trace("cause", "value", "%d", mncc->cause_value);
759 /* sending release to endpoint */
760 while(p_epointlist) {
761 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
762 message->param.disconnectinfo.cause = cause;
763 message->param.disconnectinfo.location = location;
764 message_put(message);
766 free_epointlist(p_epointlist);
768 new_state(PORT_STATE_RELEASE);
772 /* NOTIFY INDICATION */
773 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
775 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
780 /* HOLD INDICATION */
781 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
783 struct lcr_msg *message;
784 struct gsm_mncc *resp, *frame;
786 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
789 /* notify the hold of call */
790 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
791 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
792 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
793 message_put(message);
795 /* acknowledge hold */
796 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
798 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
799 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
802 if (p_m_g_tch_connected) { /* it should be true */
803 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
805 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
806 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
807 p_m_g_tch_connected = 0;
812 /* RETRIEVE INDICATION */
813 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
815 struct lcr_msg *message;
816 struct gsm_mncc *resp, *frame;
818 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
821 /* notify the retrieve of call */
822 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
823 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
824 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
825 message_put(message);
827 /* acknowledge retr */
828 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
830 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
831 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
834 if (!p_m_g_tch_connected) { /* it should be true */
835 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
837 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
838 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
839 p_m_g_tch_connected = 1;
844 * BSC sends message to port
846 static int message_bcs(struct gsm_network *net, int msg_type, void *arg)
848 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
849 unsigned int callref = mncc->callref;
851 class Pgsm *pgsm = NULL;
853 struct mISDNport *mISDNport;
855 /* Special messages */
860 callref = mncc->callref;
863 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
864 pgsm = (class Pgsm *)port;
865 if (pgsm->p_m_g_callref == callref) {
872 if (msg_type == GSM_TRAU_FRAME) {
874 pgsm->trau_receive((struct gsm_trau_frame *)arg);
879 if (msg_type != MNCC_SETUP_IND)
882 mISDNport = mISDNport_first;
886 mISDNport = mISDNport->next;
889 struct gsm_mncc *rej;
891 rej = create_mncc(MNCC_REJ_REQ, callref);
893 rej->cause_coding = 3;
894 rej->cause_location = 1;
895 rej->cause_value = 27;
896 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
897 add_trace("cause", "coding", "%d", rej->cause_coding);
898 add_trace("cause", "location", "%d", rej->cause_location);
899 add_trace("cause", "value", "%d", rej->cause_value);
901 send_and_free_mncc((struct gsm_network *)gsm->network, rej->msg_type, rej);
904 /* creating port object, transparent until setup with hdlc */
905 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
906 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
908 FATAL("Cannot create Port instance.\n");
913 pgsm->setup_ind(msg_type, callref, mncc);
916 case MNCC_START_DTMF_IND:
917 pgsm->start_dtmf_ind(msg_type, callref, mncc);
920 case MNCC_STOP_DTMF_IND:
921 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
924 case MNCC_CALL_CONF_IND:
925 pgsm->call_conf_ind(msg_type, callref, mncc);
929 pgsm->alert_ind(msg_type, callref, mncc);
933 pgsm->setup_cnf(msg_type, callref, mncc);
936 case MNCC_SETUP_COMPL_IND:
937 pgsm->setup_compl_ind(msg_type, callref, mncc);
941 pgsm->disc_ind(msg_type, callref, mncc);
947 pgsm->rel_ind(msg_type, callref, mncc);
950 case MNCC_NOTIFY_IND:
951 pgsm->notify_ind(msg_type, callref, mncc);
955 pgsm->hold_ind(msg_type, callref, mncc);
958 case MNCC_RETRIEVE_IND:
959 pgsm->retr_ind(msg_type, callref, mncc);
963 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);
969 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
971 struct lcr_msg *message;
973 struct epoint_list *epointlist;
974 struct gsm_mncc *mncc;
977 /* copy setup infos to port */
978 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
979 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
980 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
981 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
984 if (!p_dialinginfo.id[0]) {
985 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
986 add_trace("failure", NULL, "No dialed subscriber given.");
988 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
989 message->param.disconnectinfo.cause = 28;
990 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
991 message_put(message);
992 new_state(PORT_STATE_RELEASE);
997 /* release if port is blocked */
998 if (p_m_mISDNport->ifport->block) {
999 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1000 add_trace("failure", NULL, "Port blocked.");
1002 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1003 message->param.disconnectinfo.cause = 27; // temp. unavail.
1004 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1005 message_put(message);
1006 new_state(PORT_STATE_RELEASE);
1012 ret = channel = hunt_bchannel();
1016 ret = seize_bchannel(channel, 1);
1019 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1020 add_trace("failure", NULL, "No internal audio channel available.");
1022 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1023 message->param.disconnectinfo.cause = 34;
1024 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1025 message_put(message);
1026 new_state(PORT_STATE_RELEASE);
1030 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1031 if (bchannel_open(p_m_b_index))
1034 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1035 /* screen outgoing caller id */
1036 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1038 /* attach only if not already */
1039 epointlist = p_epointlist;
1041 if (epointlist->epoint_id == epoint_id)
1043 epointlist = epointlist->next;
1046 epointlist_new(epoint_id);
1049 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1050 p_m_g_callref = new_callref++;
1051 add_trace("callref", "new", "0x%x", p_m_g_callref);
1054 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1055 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1056 /* caller information */
1058 mncc->calling_plan = 1;
1059 switch (p_callerinfo.ntype) {
1060 case INFO_NTYPE_UNKNOWN:
1061 mncc->calling_type = 0x0;
1063 case INFO_NTYPE_INTERNATIONAL:
1064 mncc->calling_type = 0x1;
1066 case INFO_NTYPE_NATIONAL:
1067 mncc->calling_type = 0x2;
1069 case INFO_NTYPE_SUBSCRIBER:
1070 mncc->calling_type = 0x4;
1072 default: /* INFO_NTYPE_NOTPRESENT */
1076 switch (p_callerinfo.screen) {
1077 case INFO_SCREEN_USER:
1078 mncc->calling_screen = 0;
1080 default: /* INFO_SCREEN_NETWORK */
1081 mncc->calling_screen = 3;
1084 switch (p_callerinfo.present) {
1085 case INFO_PRESENT_ALLOWED:
1086 mncc->calling_present = 0;
1088 case INFO_PRESENT_RESTRICTED:
1089 mncc->calling_present = 1;
1091 default: /* INFO_PRESENT_NOTAVAIL */
1092 mncc->calling_present = 2;
1095 if (mncc->calling) {
1096 SCPY(mncc->calling_number, p_callerinfo.id);
1097 add_trace("calling", "type", "%d", mncc->calling_type);
1098 add_trace("calling", "plan", "%d", mncc->calling_plan);
1099 add_trace("calling", "present", "%d", mncc->calling_present);
1100 add_trace("calling", "screen", "%d", mncc->calling_screen);
1101 add_trace("calling", "number", "%s", mncc->calling_number);
1103 /* dialing information */
1105 SCPY(mncc->called_number, p_dialinginfo.id);
1106 add_trace("dialing", "number", "%s", mncc->called_number);
1108 /* sending user-user */
1110 /* redirecting number */
1111 mncc->redirecting = 1;
1112 mncc->redirecting_plan = 1;
1113 switch (p_redirinfo.ntype) {
1114 case INFO_NTYPE_UNKNOWN:
1115 mncc->redirecting_type = 0x0;
1117 case INFO_NTYPE_INTERNATIONAL:
1118 mncc->redirecting_type = 0x1;
1120 case INFO_NTYPE_NATIONAL:
1121 mncc->redirecting_type = 0x2;
1123 case INFO_NTYPE_SUBSCRIBER:
1124 mncc->redirecting_type = 0x4;
1126 default: /* INFO_NTYPE_NOTPRESENT */
1127 mncc->redirecting = 0;
1130 switch (p_redirinfo.screen) {
1131 case INFO_SCREEN_USER:
1132 mncc->redirecting_screen = 0;
1134 default: /* INFO_SCREE_NETWORK */
1135 mncc->redirecting_screen = 3;
1138 switch (p_redirinfo.present) {
1139 case INFO_PRESENT_ALLOWED:
1140 mncc->redirecting_present = 0;
1142 case INFO_PRESENT_RESTRICTED:
1143 mncc->redirecting_present = 1;
1145 default: /* INFO_PRESENT_NOTAVAIL */
1146 mncc->redirecting_present = 2;
1149 /* sending redirecting number only in ntmode */
1150 if (mncc->redirecting) {
1151 SCPY(mncc->redirecting_number, p_redirinfo.id);
1152 add_trace("redir", "type", "%d", mncc->redirecting_type);
1153 add_trace("redir", "plan", "%d", mncc->redirecting_plan);
1154 add_trace("redir", "present", "%d", mncc->redirecting_present);
1155 add_trace("redir", "screen", "%d", mncc->redirecting_screen);
1156 add_trace("redir", "number", "%s", mncc->redirecting_number);
1158 /* bearer capability */
1162 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1164 new_state(PORT_STATE_OUT_SETUP);
1166 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1167 message_put(message);
1169 new_state(PORT_STATE_OUT_PROCEEDING);
1172 /* MESSAGE_NOTIFY */
1173 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1175 struct gsm_mncc *mncc;
1178 printf("if = %d\n", param->notifyinfo.notify);
1179 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1180 notify = param->notifyinfo.notify & 0x7f;
1181 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1182 /* queue notification */
1183 if (p_m_g_notify_pending)
1184 message_free(p_m_g_notify_pending);
1185 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1186 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1188 /* sending notification */
1189 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1190 add_trace("notify", NULL, "%d", notify);
1192 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1193 mncc->notify = notify;
1194 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1199 /* MESSAGE_ALERTING */
1200 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1202 struct gsm_mncc *mncc;
1205 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1206 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1207 if (p_m_mISDNport->tones) {
1209 mncc->progress_coding = 3; /* GSM */
1210 mncc->progress_location = 1;
1211 mncc->progress_descr = 8;
1212 add_trace("progress", "coding", "%d", mncc->progress_coding);
1213 add_trace("progress", "location", "%d", mncc->progress_location);
1214 add_trace("progress", "descr", "%d", mncc->progress_descr);
1217 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1219 new_state(PORT_STATE_IN_ALERTING);
1221 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1222 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1224 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1225 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1226 p_m_g_tch_connected = 1;
1230 /* MESSAGE_CONNECT */
1231 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1233 struct gsm_mncc *mncc;
1235 /* copy connected information */
1236 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
1237 /* screen outgoing caller id */
1238 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1241 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1242 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1243 /* caller information */
1244 mncc->connected = 1;
1245 mncc->connected_plan = 1;
1246 switch (p_callerinfo.ntype) {
1247 case INFO_NTYPE_UNKNOWN:
1248 mncc->connected_type = 0x0;
1250 case INFO_NTYPE_INTERNATIONAL:
1251 mncc->connected_type = 0x1;
1253 case INFO_NTYPE_NATIONAL:
1254 mncc->connected_type = 0x2;
1256 case INFO_NTYPE_SUBSCRIBER:
1257 mncc->connected_type = 0x4;
1259 default: /* INFO_NTYPE_NOTPRESENT */
1260 mncc->connected = 0;
1263 switch (p_callerinfo.screen) {
1264 case INFO_SCREEN_USER:
1265 mncc->connected_screen = 0;
1267 default: /* INFO_SCREEN_NETWORK */
1268 mncc->connected_screen = 3;
1271 switch (p_callerinfo.present) {
1272 case INFO_PRESENT_ALLOWED:
1273 mncc->connected_present = 0;
1275 case INFO_PRESENT_RESTRICTED:
1276 mncc->connected_present = 1;
1278 default: /* INFO_PRESENT_NOTAVAIL */
1279 mncc->connected_present = 2;
1282 if (mncc->connected) {
1283 SCPY(mncc->connected_number, p_connectinfo.id);
1284 add_trace("connected", "type", "%d", mncc->connected_type);
1285 add_trace("connected", "plan", "%d", mncc->connected_plan);
1286 add_trace("connected", "present", "%d", mncc->connected_present);
1287 add_trace("connected", "screen", "%d", mncc->connected_screen);
1288 add_trace("connected", "number", "%s", mncc->connected_number);
1291 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1293 new_state(PORT_STATE_CONNECT_WAITING);
1296 /* MESSAGE_DISCONNECT */
1297 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1299 struct gsm_mncc *mncc;
1301 /* send disconnect */
1302 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1303 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1304 if (p_m_mISDNport->tones) {
1306 mncc->progress_coding = 3; /* GSM */
1307 mncc->progress_location = 1;
1308 mncc->progress_descr = 8;
1309 add_trace("progress", "coding", "%d", mncc->progress_coding);
1310 add_trace("progress", "location", "%d", mncc->progress_location);
1311 add_trace("progress", "descr", "%d", mncc->progress_descr);
1314 mncc->cause_coding = 3;
1315 mncc->cause_location = param->disconnectinfo.location;
1316 mncc->cause_value = param->disconnectinfo.cause;
1317 add_trace("cause", "coding", "%d", mncc->cause_coding);
1318 add_trace("cause", "location", "%d", mncc->cause_location);
1319 add_trace("cause", "value", "%d", mncc->cause_value);
1321 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1323 new_state(PORT_STATE_OUT_DISCONNECT);
1325 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1326 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1328 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1329 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1330 p_m_g_tch_connected = 1;
1335 /* MESSAGE_RELEASE */
1336 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1338 struct gsm_mncc *mncc;
1341 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1342 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1344 mncc->cause_coding = 3;
1345 mncc->cause_location = param->disconnectinfo.location;
1346 mncc->cause_value = param->disconnectinfo.cause;
1347 add_trace("cause", "coding", "%d", mncc->cause_coding);
1348 add_trace("cause", "location", "%d", mncc->cause_location);
1349 add_trace("cause", "value", "%d", mncc->cause_value);
1351 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1353 new_state(PORT_STATE_RELEASE);
1360 * endpoint sends messages to the port
1362 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1364 if (PmISDN::message_epoint(epoint_id, message_id, param))
1367 switch(message_id) {
1368 case MESSAGE_SETUP: /* dial-out command received from epoint */
1369 if (p_state!=PORT_STATE_IDLE)
1371 message_setup(epoint_id, message_id, param);
1374 case MESSAGE_NOTIFY: /* display and notifications */
1375 message_notify(epoint_id, message_id, param);
1378 // case MESSAGE_FACILITY: /* facility message */
1379 // message_facility(epoint_id, message_id, param);
1382 case MESSAGE_PROCEEDING: /* message not handles */
1385 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1386 if (p_state!=PORT_STATE_IN_PROCEEDING)
1388 message_alerting(epoint_id, message_id, param);
1389 if (p_m_g_notify_pending) {
1390 /* send pending notify message during connect */
1391 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1392 message_free(p_m_g_notify_pending);
1393 p_m_g_notify_pending = NULL;
1397 case MESSAGE_CONNECT: /* call of endpoint is connected */
1398 if (p_state!=PORT_STATE_IN_PROCEEDING
1399 && p_state!=PORT_STATE_IN_ALERTING)
1401 message_connect(epoint_id, message_id, param);
1402 if (p_m_g_notify_pending) {
1403 /* send pending notify message during connect */
1404 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1405 message_free(p_m_g_notify_pending);
1406 p_m_g_notify_pending = NULL;
1410 case MESSAGE_DISCONNECT: /* call has been disconnected */
1411 if (p_state!=PORT_STATE_IN_PROCEEDING
1412 && p_state!=PORT_STATE_IN_ALERTING
1413 && p_state!=PORT_STATE_OUT_SETUP
1414 && p_state!=PORT_STATE_OUT_OVERLAP
1415 && p_state!=PORT_STATE_OUT_PROCEEDING
1416 && p_state!=PORT_STATE_OUT_ALERTING
1417 && p_state!=PORT_STATE_CONNECT
1418 && p_state!=PORT_STATE_CONNECT_WAITING)
1420 message_disconnect(epoint_id, message_id, param);
1423 case MESSAGE_RELEASE: /* release isdn port */
1424 if (p_state==PORT_STATE_RELEASE)
1426 message_release(epoint_id, message_id, param);
1430 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1440 int Pgsm::handler(void)
1444 unsigned char buffer[2048+MISDN_HEADER_LEN];
1445 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1447 if ((ret = PmISDN::handler()))
1450 /* handle destruction */
1456 /* handle message from bchannel */
1457 if (p_m_g_gsm_b_sock > -1) {
1458 ret = recv(p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1459 if (ret >= (int)MISDN_HEADER_LEN) {
1461 /* we don't care about confirms, we use rx data to sync tx */
1464 /* we receive audio data, we respond to it AND we send tones */
1466 bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1468 case PH_ACTIVATE_IND:
1469 p_m_g_gsm_b_active = 1;
1471 case PH_DEACTIVATE_IND:
1472 p_m_g_gsm_b_active = 0;
1477 if (ret < 0 && errno != EWOULDBLOCK)
1478 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1487 * handles bsc select function within LCR's main loop
1489 int handle_gsm(void)
1493 ret1 = bsc_upqueue((struct gsm_network *)gsm->network);
1494 ret2 = bsc_select_main(1); /* polling */
1500 static void gsm_sock_close(void)
1502 if (gsm->gsm_sock > -1)
1503 close(gsm->gsm_sock);
1507 static int gsm_sock_open(char *portname)
1511 unsigned long on = 1;
1512 struct sockaddr_mISDN addr;
1513 struct mISDN_devinfo devinfo;
1516 /* check port counts */
1517 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1519 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1524 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1527 gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1528 if (gsm->gsm_port < 0) {
1529 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1530 return gsm->gsm_port;
1534 devinfo.id = gsm->gsm_port;
1535 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1537 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1540 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1543 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1547 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1550 if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_TE_S0)) < 0) {
1551 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1553 return gsm->gsm_sock;
1555 /* set nonblocking io */
1556 if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1557 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1561 /* bind socket to dchannel */
1562 memset(&addr, 0, sizeof(addr));
1563 addr.family = AF_ISDN;
1564 addr.dev = gsm->gsm_port;
1566 if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1567 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1576 int gsm_exit(int rc)
1578 /* free gsm instance */
1580 if (gsm->gsm_sock > -1)
1582 /* shutdown network */
1584 shutdown_net((struct gsm_network *)gsm->network);
1587 free((struct gsm_network *)gsm->network); /* TBD */
1600 /* create gsm instance */
1601 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1605 if (!gsm_conf(&gsm->conf)) {
1606 PERROR("%s", gsm_conf_error);
1607 return gsm_exit(-EINVAL);
1611 debug_parse_category_mask(gsm->conf.debug);
1614 if (gsm->conf.hlr[0] == '/')
1615 SCPY(hlr, gsm->conf.hlr);
1617 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1619 // TODO multiple base stations
1620 if (gsm->conf.numbts < 1 || gsm->conf.numbts > 1) {
1621 PERROR("Expecting exactly one BTS. You defined %d.\n", gsm->conf.numbts);
1622 return gsm_exit(-1);
1624 /* bootstrap network */
1625 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);
1626 if (!gsm->network) {
1627 PERROR("Failed to bootstrap GSM network.\n");
1628 return gsm_exit(-1);
1630 /* open gsm loop interface */
1631 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1632 return gsm_exit(-1);