1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
20 #include <openbsc/db.h>
21 #include <openbsc/select.h>
22 #include <openbsc/debug.h>
23 #include <openbsc/e1_input.h>
24 #include <openbsc/talloc.h>
25 #include <openbsc/mncc.h>
26 #include <openbsc/trau_frame.h>
27 struct gsm_network *bsc_gsmnet = 0;
28 extern int ipacc_rtp_direct;
29 extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *),
30 const char *cfg_file);
31 extern int bsc_shutdown_net(struct gsm_network *net);
32 void talloc_ctx_init(void);
33 void on_dso_load_token(void);
34 void on_dso_load_rrlp(void);
35 static struct debug_target *stderr_target;
37 #include "gsm_audio.h"
42 #define PF_ISDN AF_ISDN
46 struct lcr_gsm *gsm = NULL;
48 static unsigned int new_callref = 1;
52 * create and send mncc message
54 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
56 struct gsm_mncc *mncc;
58 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
59 mncc->msg_type = msg_type;
60 mncc->callref = callref;
63 static int send_and_free_mncc(struct gsm_network *net, unsigned int msg_type, void *data)
67 ret = mncc_send(net, msg_type, data);
77 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)
79 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
82 p_m_g_gsm_b_sock = -1;
83 p_m_g_gsm_b_index = -1;
84 p_m_g_gsm_b_active = 0;
85 p_m_g_notify_pending = NULL;
86 p_m_g_decoder = gsm_audio_create();
87 p_m_g_encoder = gsm_audio_create();
88 if (!p_m_g_encoder || !p_m_g_decoder) {
89 PERROR("Failed to create GSM audio codec instance\n");
93 p_m_g_tch_connected = 0;
95 PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
103 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
105 /* remove queued message */
106 if (p_m_g_notify_pending)
107 message_free(p_m_g_notify_pending);
109 /* close audio transfer socket */
110 if (p_m_g_gsm_b_sock > -1)
115 gsm_audio_destroy(p_m_g_encoder);
117 gsm_audio_destroy(p_m_g_decoder);
121 /* close bsc side bchannel */
122 void Pgsm::bchannel_close(void)
124 if (p_m_g_gsm_b_sock > -1)
125 close(p_m_g_gsm_b_sock);
126 p_m_g_gsm_b_sock = -1;
127 p_m_g_gsm_b_index = -1;
128 p_m_g_gsm_b_active = 0;
131 /* open bsc side bchannel */
132 int Pgsm::bchannel_open(int index)
136 struct sockaddr_mISDN addr;
137 struct mISDNhead act;
139 if (p_m_g_gsm_b_sock > -1) {
140 PERROR("Socket already created for index %d\n", index);
145 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
147 PERROR("Failed to open bchannel-socket for index %d\n", index);
152 /* set nonblocking io */
153 ret = ioctl(p_m_g_gsm_b_sock, FIONBIO, &on);
155 PERROR("Failed to set bchannel-socket index %d into nonblocking IO\n", index);
160 /* bind socket to bchannel */
161 addr.family = AF_ISDN;
162 addr.dev = gsm->gsm_port;
163 addr.channel = index+1+(index>15);
164 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
166 PERROR("Failed to bind bchannel-socket for index %d\n", index);
170 /* activate bchannel */
171 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
172 act.prim = PH_ACTIVATE_REQ;
174 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
176 PERROR("Failed to activate index %d\n", index);
181 p_m_g_gsm_b_index = index;
186 /* receive from bchannel */
187 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
189 unsigned char frame[33];
191 /* encoder init failed */
195 /* (currently) not connected, so don't flood tch! */
196 if (!p_m_g_tch_connected)
199 /* write to rx buffer */
201 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
202 if (p_m_g_rxpos == 160) {
206 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
212 /* transmit to bchannel */
213 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
215 unsigned char buf[MISDN_HEADER_LEN+len];
216 struct mISDNhead *hh = (struct mISDNhead *)buf;
219 if (!p_m_g_gsm_b_active)
222 /* make and send frame */
223 hh->prim = PH_DATA_REQ;
225 memcpy(buf+MISDN_HEADER_LEN, data, len);
226 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
228 PERROR("Failed to send to socket index %d\n", index);
231 void Pgsm::frame_send(void *_frame)
233 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
234 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
236 frame->msg_type = GSM_TCHF_FRAME;
237 frame->callref = p_m_g_callref;
238 memcpy(frame->data, _frame, 33);
239 mncc_send((struct gsm_network *)gsm->network, frame->msg_type, frame);
243 void Pgsm::frame_receive(void *_frame)
245 struct gsm_data_frame *frame = (struct gsm_data_frame *)_frame;
246 signed short samples[160];
247 unsigned char data[160];
253 if ((frame->data[0]>>4) != 0xd)
254 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
257 gsm_audio_decode(p_m_g_decoder, frame->data, samples);
258 for (i = 0; i < 160; i++) {
259 data[i] = audio_s16_to_law[samples[i] & 0xffff];
263 bchannel_send(PH_DATA_REQ, 0, data, 160);
270 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
274 /* select message and primitive text */
275 SCPY(msgtext, get_mncc_name(msg_type));
278 if (direction == DIRECTION_OUT)
279 SCAT(msgtext, " MSC->BSC");
281 SCAT(msgtext, " MSC<-BSC");
283 /* init trace with given values */
284 start_trace(mISDNport?mISDNport->portnum:-1,
285 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
286 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
287 port?port->p_dialinginfo.id:NULL,
290 port?port->p_serial:0,
296 /* select bchannel */
297 int Pgsm::hunt_bchannel(void)
302 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
303 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
304 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
305 add_trace("conclusion", NULL, "all channels are reserved");
307 return(-34); // no channel
312 while(i < p_m_mISDNport->b_num) {
313 if (p_m_mISDNport->b_port[i] == NULL) {
314 channel = i+1+(i>=15);
320 add_trace("conclusion", NULL, "no channel available");
322 return(-6); // channel unacceptable
324 add_trace("conclusion", NULL, "channel available");
325 add_trace("connect", "channel", "%d", channel);
332 * handles all indications
334 /* SETUP INDICATION */
335 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
338 class Endpoint *epoint;
339 struct lcr_msg *message;
341 struct gsm_mncc *mode, *proceeding, *frame;
343 /* emergency shutdown */
344 printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
345 if (mncc->emergency && !gsm->conf.noemergshut) {
346 start_trace(p_m_mISDNport->portnum,
347 p_m_mISDNport->ifport->interface,
353 "EMERGENCY SHUTDOWN (due to received emergency call)");
357 /* process given callref */
358 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
359 add_trace("callref", "new", "0x%x", callref);
361 /* release in case the ID is already in use */
362 add_trace("error", NULL, "callref already in use");
364 mncc = create_mncc(MNCC_REJ_REQ, callref);
365 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
366 mncc->fields |= MNCC_F_CAUSE;
367 mncc->cause.coding = 3;
368 mncc->cause.location = 1;
369 mncc->cause.value = 47;
370 add_trace("cause", "coding", "%d", mncc->cause.coding);
371 add_trace("cause", "location", "%d", mncc->cause.location);
372 add_trace("cause", "value", "%d", mncc->cause.value);
373 add_trace("reason", NULL, "callref already in use");
375 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
376 new_state(PORT_STATE_RELEASE);
380 p_m_g_callref = callref;
383 /* if blocked, release call with MT_RELEASE_COMPLETE */
384 if (p_m_mISDNport->ifport->block) {
385 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
386 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
387 mncc->fields |= MNCC_F_CAUSE;
388 mncc->cause.coding = 3;
389 mncc->cause.location = 1;
390 mncc->cause.value = 27;
391 add_trace("cause", "coding", "%d", mncc->cause.coding);
392 add_trace("cause", "location", "%d", mncc->cause.location);
393 add_trace("cause", "value", "%d", mncc->cause.value);
394 add_trace("reason", NULL, "port is blocked");
396 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
397 new_state(PORT_STATE_RELEASE);
404 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
406 p_callerinfo.present = INFO_PRESENT_ALLOWED;
407 if (mncc->calling.number[0])
408 SCPY(p_callerinfo.id, mncc->calling.number);
410 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
411 SCPY(p_callerinfo.imsi, mncc->imsi);
412 p_callerinfo.screen = INFO_SCREEN_NETWORK;
413 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
414 p_callerinfo.isdn_port = p_m_portnum;
415 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
417 /* dialing information */
418 SCAT(p_dialinginfo.id, mncc->called.number);
419 switch (mncc->called.type) {
421 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
424 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
427 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
430 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
433 if (mncc->emergency) {
434 SCPY(p_dialinginfo.id, "emergency");
436 p_dialinginfo.sending_complete = 1;
438 /* bearer capability */
440 p_capainfo.bearer_capa = INFO_BC_SPEECH;
441 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
442 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
443 p_capainfo.source_mode = B_MODE_TRANSPARENT;
444 p_m_g_mode = p_capainfo.source_mode;
449 ret = channel = hunt_bchannel();
454 ret = seize_bchannel(channel, 1);
457 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
458 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
459 mncc->fields |= MNCC_F_CAUSE;
460 mncc->cause.coding = 3;
461 mncc->cause.location = 1;
462 mncc->cause.value = 34;
463 add_trace("cause", "coding", "%d", mncc->cause.coding);
464 add_trace("cause", "location", "%d", mncc->cause.location);
465 add_trace("cause", "value", "%d", mncc->cause.value);
466 add_trace("reason", NULL, "no channel");
468 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
469 new_state(PORT_STATE_RELEASE);
473 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
474 if (bchannel_open(p_m_b_index))
477 /* what infos did we got ... */
478 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
479 if (p_callerinfo.id[0])
480 add_trace("calling", "number", "%s", p_callerinfo.id);
482 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
483 add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
484 add_trace("dialing", "number", "%s", p_dialinginfo.id);
487 /* create endpoint */
489 FATAL("Incoming call but already got an endpoint.\n");
490 if (!(epoint = new Endpoint(p_serial, 0)))
491 FATAL("No memory for Endpoint instance\n");
492 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
493 FATAL("No memory for Endpoint Application instance\n");
494 epointlist_new(epoint->ep_serial);
496 /* modify lchan to GSM codec V1 */
497 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
498 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
499 mode->lchan_mode = 0x01; /* GSM V1 */
500 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
502 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
504 /* send call proceeding */
505 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
506 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
507 if (p_m_mISDNport->tones) {
508 proceeding->fields |= MNCC_F_PROGRESS;
509 proceeding->progress.coding = 3; /* GSM */
510 proceeding->progress.location = 1;
511 proceeding->progress.descr = 8;
512 add_trace("progress", "coding", "%d", proceeding->progress.coding);
513 add_trace("progress", "location", "%d", proceeding->progress.location);
514 add_trace("progress", "descr", "%d", proceeding->progress.descr);
517 send_and_free_mncc((struct gsm_network *)gsm->network, proceeding->msg_type, proceeding);
519 new_state(PORT_STATE_IN_PROCEEDING);
521 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
522 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
524 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
525 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
526 p_m_g_tch_connected = 1;
529 /* send setup message to endpoit */
530 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
531 message->param.setup.isdn_port = p_m_portnum;
532 message->param.setup.port_type = p_type;
533 // message->param.setup.dtmf = 0;
534 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
535 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
536 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
537 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
538 message->param.setup.useruser.len = strlen(mncc->useruser.info);
539 message->param.setup.useruser.protocol = mncc->useruser.proto;
540 message_put(message);
543 /* DTMF INDICATION */
544 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
546 struct lcr_msg *message;
547 struct gsm_mncc *resp;
549 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
550 add_trace("keypad", NULL, "%c", mncc->keypad);
552 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
553 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
556 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
557 add_trace("keypad", NULL, "%c", mncc->keypad);
559 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
560 resp->keypad = mncc->keypad;
561 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
563 /* send dialing information */
564 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
565 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
566 message_put(message);
568 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
570 struct gsm_mncc *resp;
572 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
573 add_trace("keypad", NULL, "%c", mncc->keypad);
577 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
578 add_trace("keypad", NULL, "%c", mncc->keypad);
580 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
581 resp->keypad = mncc->keypad;
582 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
585 /* PROCEEDING INDICATION */
586 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
588 struct gsm_mncc *mode;
590 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
591 if (mncc->fields & MNCC_F_CAUSE) {
592 add_trace("cause", "coding", "%d", mncc->cause.coding);
593 add_trace("cause", "location", "%", mncc->cause.location);
594 add_trace("cause", "value", "%", mncc->cause.value);
598 /* modify lchan to GSM codec V1 */
599 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
600 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
601 mode->lchan_mode = 0x01; /* GSM V1 */
602 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
604 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
608 /* ALERTING INDICATION */
609 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
611 struct lcr_msg *message;
613 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
616 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
617 message_put(message);
619 new_state(PORT_STATE_OUT_ALERTING);
623 /* CONNECT INDICATION */
624 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
626 struct gsm_mncc *resp, *frame;
627 struct lcr_msg *message;
629 SCPY(p_connectinfo.id, mncc->connected.number);
630 SCPY(p_connectinfo.imsi, mncc->imsi);
631 p_connectinfo.present = INFO_PRESENT_ALLOWED;
632 p_connectinfo.screen = INFO_SCREEN_NETWORK;
633 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
634 p_connectinfo.isdn_port = p_m_portnum;
635 SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
637 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
638 if (p_connectinfo.id[0])
639 add_trace("connect", "number", "%s", p_connectinfo.id);
640 else if (mncc->imsi[0])
641 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
643 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
647 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
648 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
650 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
652 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
653 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
654 message_put(message);
656 new_state(PORT_STATE_CONNECT);
658 if (!p_m_g_tch_connected) { /* only if ... */
659 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
661 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
662 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
663 p_m_g_tch_connected = 1;
667 /* CONNECT ACK INDICATION */
668 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
670 struct gsm_mncc *frame;
672 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
675 new_state(PORT_STATE_CONNECT);
677 if (!p_m_g_tch_connected) { /* only if ... */
678 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
680 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
681 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
682 p_m_g_tch_connected = 1;
686 /* DISCONNECT INDICATION */
687 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
689 struct lcr_msg *message;
690 int cause = 16, location = 0;
691 struct gsm_mncc *resp;
693 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
694 if (mncc->fields & MNCC_F_CAUSE) {
695 location = mncc->cause.location;
696 cause = mncc->cause.value;
697 add_trace("cause", "coding", "%d", mncc->cause.coding);
698 add_trace("cause", "location", "%d", location);
699 add_trace("cause", "value", "%d", cause);
704 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
705 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
707 resp->fields |= MNCC_F_CAUSE;
708 resp->cause.coding = 3;
709 resp->cause.location = 1;
710 resp->cause.value = cause;
711 add_trace("cause", "coding", "%d", resp->cause.coding);
712 add_trace("cause", "location", "%d", resp->cause.location);
713 add_trace("cause", "value", "%d", resp->cause.value);
716 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
718 /* sending release to endpoint */
719 while(p_epointlist) {
720 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
721 message->param.disconnectinfo.cause = cause;
722 message->param.disconnectinfo.location = location;
723 message_put(message);
725 free_epointlist(p_epointlist);
727 new_state(PORT_STATE_RELEASE);
731 /* CC_RELEASE INDICATION */
732 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
734 int location = 0, cause = 16;
735 struct lcr_msg *message;
737 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
738 if (mncc->fields & MNCC_F_CAUSE) {
739 location = mncc->cause.location;
740 cause = mncc->cause.value;
741 add_trace("cause", "coding", "%d", mncc->cause.coding);
742 add_trace("cause", "location", "%d", mncc->cause.location);
743 add_trace("cause", "value", "%d", mncc->cause.value);
747 /* sending release to endpoint */
748 while(p_epointlist) {
749 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
750 message->param.disconnectinfo.cause = cause;
751 message->param.disconnectinfo.location = location;
752 message_put(message);
754 free_epointlist(p_epointlist);
756 new_state(PORT_STATE_RELEASE);
760 /* NOTIFY INDICATION */
761 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
763 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
768 /* HOLD INDICATION */
769 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
771 struct lcr_msg *message;
772 struct gsm_mncc *resp, *frame;
774 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
777 /* notify the hold of call */
778 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
779 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
780 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
781 message_put(message);
783 /* acknowledge hold */
784 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
786 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
787 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
790 if (p_m_g_tch_connected) { /* it should be true */
791 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
793 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
794 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
795 p_m_g_tch_connected = 0;
800 /* RETRIEVE INDICATION */
801 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
803 struct lcr_msg *message;
804 struct gsm_mncc *resp, *frame;
806 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
809 /* notify the retrieve of call */
810 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
811 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
812 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
813 message_put(message);
815 /* acknowledge retr */
816 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
818 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
819 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
822 if (!p_m_g_tch_connected) { /* it should be true */
823 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
825 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
826 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
827 p_m_g_tch_connected = 1;
832 * BSC sends message to port
834 static int message_bsc(struct gsm_network *net, int msg_type, void *arg)
836 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
837 unsigned int callref = mncc->callref;
839 class Pgsm *pgsm = NULL;
841 struct mISDNport *mISDNport;
843 /* Special messages */
848 callref = mncc->callref;
851 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
852 pgsm = (class Pgsm *)port;
853 if (pgsm->p_m_g_callref == callref) {
860 if (msg_type == GSM_TCHF_FRAME) {
862 pgsm->frame_receive((struct gsm_trau_frame *)arg);
867 if (msg_type != MNCC_SETUP_IND)
870 mISDNport = mISDNport_first;
874 mISDNport = mISDNport->next;
877 struct gsm_mncc *rej;
879 rej = create_mncc(MNCC_REJ_REQ, callref);
880 rej->fields |= MNCC_F_CAUSE;
881 rej->cause.coding = 3;
882 rej->cause.location = 1;
883 rej->cause.value = 27;
884 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
885 add_trace("cause", "coding", "%d", rej->cause.coding);
886 add_trace("cause", "location", "%d", rej->cause.location);
887 add_trace("cause", "value", "%d", rej->cause.value);
889 send_and_free_mncc((struct gsm_network *)gsm->network, rej->msg_type, rej);
892 /* creating port object, transparent until setup with hdlc */
893 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
894 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
896 FATAL("Cannot create Port instance.\n");
901 pgsm->setup_ind(msg_type, callref, mncc);
904 case MNCC_START_DTMF_IND:
905 pgsm->start_dtmf_ind(msg_type, callref, mncc);
908 case MNCC_STOP_DTMF_IND:
909 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
912 case MNCC_CALL_CONF_IND:
913 pgsm->call_conf_ind(msg_type, callref, mncc);
917 pgsm->alert_ind(msg_type, callref, mncc);
921 pgsm->setup_cnf(msg_type, callref, mncc);
924 case MNCC_SETUP_COMPL_IND:
925 pgsm->setup_compl_ind(msg_type, callref, mncc);
929 pgsm->disc_ind(msg_type, callref, mncc);
935 pgsm->rel_ind(msg_type, callref, mncc);
938 case MNCC_NOTIFY_IND:
939 pgsm->notify_ind(msg_type, callref, mncc);
943 pgsm->hold_ind(msg_type, callref, mncc);
946 case MNCC_RETRIEVE_IND:
947 pgsm->retr_ind(msg_type, callref, mncc);
951 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);
957 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
959 struct lcr_msg *message;
961 struct epoint_list *epointlist;
962 struct gsm_mncc *mncc;
965 /* copy setup infos to port */
966 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
967 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
968 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
969 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
972 if (!p_dialinginfo.id[0]) {
973 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
974 add_trace("failure", NULL, "No dialed subscriber given.");
976 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
977 message->param.disconnectinfo.cause = 28;
978 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
979 message_put(message);
980 new_state(PORT_STATE_RELEASE);
985 /* release if port is blocked */
986 if (p_m_mISDNport->ifport->block) {
987 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
988 add_trace("failure", NULL, "Port blocked.");
990 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
991 message->param.disconnectinfo.cause = 27; // temp. unavail.
992 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
993 message_put(message);
994 new_state(PORT_STATE_RELEASE);
1000 ret = channel = hunt_bchannel();
1004 ret = seize_bchannel(channel, 1);
1007 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1008 add_trace("failure", NULL, "No internal audio channel available.");
1010 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1011 message->param.disconnectinfo.cause = 34;
1012 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1013 message_put(message);
1014 new_state(PORT_STATE_RELEASE);
1018 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1019 if (bchannel_open(p_m_b_index))
1022 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1023 /* screen outgoing caller id */
1024 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1026 /* attach only if not already */
1027 epointlist = p_epointlist;
1029 if (epointlist->epoint_id == epoint_id)
1031 epointlist = epointlist->next;
1034 epointlist_new(epoint_id);
1037 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1038 p_m_g_callref = new_callref++;
1039 add_trace("callref", "new", "0x%x", p_m_g_callref);
1042 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1043 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1044 /* caller information */
1045 mncc->fields |= MNCC_F_CALLING;
1046 mncc->calling.plan = 1;
1047 switch (p_callerinfo.ntype) {
1048 case INFO_NTYPE_UNKNOWN:
1049 mncc->calling.type = 0x0;
1051 case INFO_NTYPE_INTERNATIONAL:
1052 mncc->calling.type = 0x1;
1054 case INFO_NTYPE_NATIONAL:
1055 mncc->calling.type = 0x2;
1057 case INFO_NTYPE_SUBSCRIBER:
1058 mncc->calling.type = 0x4;
1060 default: /* INFO_NTYPE_NOTPRESENT */
1061 mncc->fields &= ~MNCC_F_CALLING;
1064 switch (p_callerinfo.screen) {
1065 case INFO_SCREEN_USER:
1066 mncc->calling.screen = 0;
1068 default: /* INFO_SCREEN_NETWORK */
1069 mncc->calling.screen = 3;
1072 switch (p_callerinfo.present) {
1073 case INFO_PRESENT_ALLOWED:
1074 mncc->calling.present = 0;
1076 case INFO_PRESENT_RESTRICTED:
1077 mncc->calling.present = 1;
1079 default: /* INFO_PRESENT_NOTAVAIL */
1080 mncc->calling.present = 2;
1083 if (mncc->fields & MNCC_F_CALLING) {
1084 SCPY(mncc->calling.number, p_callerinfo.id);
1085 add_trace("calling", "type", "%d", mncc->calling.type);
1086 add_trace("calling", "plan", "%d", mncc->calling.plan);
1087 add_trace("calling", "present", "%d", mncc->calling.present);
1088 add_trace("calling", "screen", "%d", mncc->calling.screen);
1089 add_trace("calling", "number", "%s", mncc->calling.number);
1091 /* dialing information */
1092 mncc->fields |= MNCC_F_CALLED;
1093 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
1094 SCPY(mncc->imsi, p_dialinginfo.id+5);
1095 add_trace("dialing", "imsi", "%s", mncc->imsi);
1097 SCPY(mncc->called.number, p_dialinginfo.id);
1098 add_trace("dialing", "number", "%s", mncc->called.number);
1101 /* sending user-user */
1103 /* redirecting number */
1104 mncc->fields |= MNCC_F_REDIRECTING;
1105 mncc->redirecting.plan = 1;
1106 switch (p_redirinfo.ntype) {
1107 case INFO_NTYPE_UNKNOWN:
1108 mncc->redirecting.type = 0x0;
1110 case INFO_NTYPE_INTERNATIONAL:
1111 mncc->redirecting.type = 0x1;
1113 case INFO_NTYPE_NATIONAL:
1114 mncc->redirecting.type = 0x2;
1116 case INFO_NTYPE_SUBSCRIBER:
1117 mncc->redirecting.type = 0x4;
1119 default: /* INFO_NTYPE_NOTPRESENT */
1120 mncc->fields &= ~MNCC_F_REDIRECTING;
1123 switch (p_redirinfo.screen) {
1124 case INFO_SCREEN_USER:
1125 mncc->redirecting.screen = 0;
1127 default: /* INFO_SCREE_NETWORK */
1128 mncc->redirecting.screen = 3;
1131 switch (p_redirinfo.present) {
1132 case INFO_PRESENT_ALLOWED:
1133 mncc->redirecting.present = 0;
1135 case INFO_PRESENT_RESTRICTED:
1136 mncc->redirecting.present = 1;
1138 default: /* INFO_PRESENT_NOTAVAIL */
1139 mncc->redirecting.present = 2;
1142 /* sending redirecting number only in ntmode */
1143 if (mncc->fields & MNCC_F_REDIRECTING) {
1144 SCPY(mncc->redirecting.number, p_redirinfo.id);
1145 add_trace("redir", "type", "%d", mncc->redirecting.type);
1146 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
1147 add_trace("redir", "present", "%d", mncc->redirecting.present);
1148 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
1149 add_trace("redir", "number", "%s", mncc->redirecting.number);
1151 /* bearer capability */
1155 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1157 new_state(PORT_STATE_OUT_SETUP);
1159 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1160 message_put(message);
1162 new_state(PORT_STATE_OUT_PROCEEDING);
1165 /* MESSAGE_NOTIFY */
1166 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1168 struct gsm_mncc *mncc;
1171 // printf("if = %d\n", param->notifyinfo.notify);
1172 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1173 notify = param->notifyinfo.notify & 0x7f;
1174 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1175 /* queue notification */
1176 if (p_m_g_notify_pending)
1177 message_free(p_m_g_notify_pending);
1178 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1179 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1181 /* sending notification */
1182 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1183 add_trace("notify", NULL, "%d", notify);
1185 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1186 mncc->notify = notify;
1187 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1192 /* MESSAGE_ALERTING */
1193 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1195 struct gsm_mncc *mncc;
1198 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1199 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1200 if (p_m_mISDNport->tones) {
1201 mncc->fields |= MNCC_F_PROGRESS;
1202 mncc->progress.coding = 3; /* GSM */
1203 mncc->progress.location = 1;
1204 mncc->progress.descr = 8;
1205 add_trace("progress", "coding", "%d", mncc->progress.coding);
1206 add_trace("progress", "location", "%d", mncc->progress.location);
1207 add_trace("progress", "descr", "%d", mncc->progress.descr);
1210 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1212 new_state(PORT_STATE_IN_ALERTING);
1214 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1215 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1217 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1218 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1219 p_m_g_tch_connected = 1;
1223 /* MESSAGE_CONNECT */
1224 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1226 struct gsm_mncc *mncc;
1228 /* copy connected information */
1229 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
1230 /* screen outgoing caller id */
1231 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1234 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1235 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1236 /* caller information */
1237 mncc->fields |= MNCC_F_CONNECTED;
1238 mncc->connected.plan = 1;
1239 switch (p_callerinfo.ntype) {
1240 case INFO_NTYPE_UNKNOWN:
1241 mncc->connected.type = 0x0;
1243 case INFO_NTYPE_INTERNATIONAL:
1244 mncc->connected.type = 0x1;
1246 case INFO_NTYPE_NATIONAL:
1247 mncc->connected.type = 0x2;
1249 case INFO_NTYPE_SUBSCRIBER:
1250 mncc->connected.type = 0x4;
1252 default: /* INFO_NTYPE_NOTPRESENT */
1253 mncc->fields &= ~MNCC_F_CONNECTED;
1256 switch (p_callerinfo.screen) {
1257 case INFO_SCREEN_USER:
1258 mncc->connected.screen = 0;
1260 default: /* INFO_SCREEN_NETWORK */
1261 mncc->connected.screen = 3;
1264 switch (p_callerinfo.present) {
1265 case INFO_PRESENT_ALLOWED:
1266 mncc->connected.present = 0;
1268 case INFO_PRESENT_RESTRICTED:
1269 mncc->connected.present = 1;
1271 default: /* INFO_PRESENT_NOTAVAIL */
1272 mncc->connected.present = 2;
1275 if (mncc->fields & MNCC_F_CONNECTED) {
1276 SCPY(mncc->connected.number, p_connectinfo.id);
1277 add_trace("connected", "type", "%d", mncc->connected.type);
1278 add_trace("connected", "plan", "%d", mncc->connected.plan);
1279 add_trace("connected", "present", "%d", mncc->connected.present);
1280 add_trace("connected", "screen", "%d", mncc->connected.screen);
1281 add_trace("connected", "number", "%s", mncc->connected.number);
1284 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1286 new_state(PORT_STATE_CONNECT_WAITING);
1289 /* MESSAGE_DISCONNECT */
1290 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1292 struct gsm_mncc *mncc;
1294 /* send disconnect */
1295 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1296 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1297 if (p_m_mISDNport->tones) {
1298 mncc->fields |= MNCC_F_PROGRESS;
1299 mncc->progress.coding = 3; /* GSM */
1300 mncc->progress.location = 1;
1301 mncc->progress.descr = 8;
1302 add_trace("progress", "coding", "%d", mncc->progress.coding);
1303 add_trace("progress", "location", "%d", mncc->progress.location);
1304 add_trace("progress", "descr", "%d", mncc->progress.descr);
1306 mncc->fields |= MNCC_F_CAUSE;
1307 mncc->cause.coding = 3;
1308 mncc->cause.location = param->disconnectinfo.location;
1309 mncc->cause.value = param->disconnectinfo.cause;
1310 add_trace("cause", "coding", "%d", mncc->cause.coding);
1311 add_trace("cause", "location", "%d", mncc->cause.location);
1312 add_trace("cause", "value", "%d", mncc->cause.value);
1314 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1316 new_state(PORT_STATE_OUT_DISCONNECT);
1318 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1319 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1321 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1322 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1323 p_m_g_tch_connected = 1;
1328 /* MESSAGE_RELEASE */
1329 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1331 struct gsm_mncc *mncc;
1334 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1335 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1336 mncc->fields |= MNCC_F_CAUSE;
1337 mncc->cause.coding = 3;
1338 mncc->cause.location = param->disconnectinfo.location;
1339 mncc->cause.value = param->disconnectinfo.cause;
1340 add_trace("cause", "coding", "%d", mncc->cause.coding);
1341 add_trace("cause", "location", "%d", mncc->cause.location);
1342 add_trace("cause", "value", "%d", mncc->cause.value);
1344 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1346 new_state(PORT_STATE_RELEASE);
1353 * endpoint sends messages to the port
1355 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1357 if (PmISDN::message_epoint(epoint_id, message_id, param))
1360 switch(message_id) {
1361 case MESSAGE_SETUP: /* dial-out command received from epoint */
1362 if (p_state!=PORT_STATE_IDLE)
1364 message_setup(epoint_id, message_id, param);
1367 case MESSAGE_NOTIFY: /* display and notifications */
1368 message_notify(epoint_id, message_id, param);
1371 // case MESSAGE_FACILITY: /* facility message */
1372 // message_facility(epoint_id, message_id, param);
1375 case MESSAGE_PROCEEDING: /* message not handles */
1378 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1379 if (p_state!=PORT_STATE_IN_PROCEEDING)
1381 message_alerting(epoint_id, message_id, param);
1382 if (p_m_g_notify_pending) {
1383 /* send pending notify message during connect */
1384 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1385 message_free(p_m_g_notify_pending);
1386 p_m_g_notify_pending = NULL;
1390 case MESSAGE_CONNECT: /* call of endpoint is connected */
1391 if (p_state!=PORT_STATE_IN_PROCEEDING
1392 && p_state!=PORT_STATE_IN_ALERTING)
1394 message_connect(epoint_id, message_id, param);
1395 if (p_m_g_notify_pending) {
1396 /* send pending notify message during connect */
1397 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1398 message_free(p_m_g_notify_pending);
1399 p_m_g_notify_pending = NULL;
1403 case MESSAGE_DISCONNECT: /* call has been disconnected */
1404 if (p_state!=PORT_STATE_IN_PROCEEDING
1405 && p_state!=PORT_STATE_IN_ALERTING
1406 && p_state!=PORT_STATE_OUT_SETUP
1407 && p_state!=PORT_STATE_OUT_OVERLAP
1408 && p_state!=PORT_STATE_OUT_PROCEEDING
1409 && p_state!=PORT_STATE_OUT_ALERTING
1410 && p_state!=PORT_STATE_CONNECT
1411 && p_state!=PORT_STATE_CONNECT_WAITING)
1413 message_disconnect(epoint_id, message_id, param);
1416 case MESSAGE_RELEASE: /* release isdn port */
1417 if (p_state==PORT_STATE_RELEASE)
1419 message_release(epoint_id, message_id, param);
1423 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1433 int Pgsm::handler(void)
1437 unsigned char buffer[2048+MISDN_HEADER_LEN];
1438 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1440 if ((ret = PmISDN::handler()))
1443 /* handle destruction */
1449 /* handle message from bchannel */
1450 if (p_m_g_gsm_b_sock > -1) {
1451 ret = recv(p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1452 if (ret >= (int)MISDN_HEADER_LEN) {
1454 /* we don't care about confirms, we use rx data to sync tx */
1457 /* we receive audio data, we respond to it AND we send tones */
1459 bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1461 case PH_ACTIVATE_IND:
1462 p_m_g_gsm_b_active = 1;
1464 case PH_DEACTIVATE_IND:
1465 p_m_g_gsm_b_active = 0;
1470 if (ret < 0 && errno != EWOULDBLOCK)
1471 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1480 * handles bsc select function within LCR's main loop
1482 int handle_gsm(void)
1486 ret1 = bsc_upqueue((struct gsm_network *)gsm->network);
1487 ret2 = bsc_select_main(1); /* polling */
1493 static void gsm_sock_close(void)
1495 if (gsm->gsm_sock > -1)
1496 close(gsm->gsm_sock);
1500 static int gsm_sock_open(char *portname)
1504 unsigned long on = 1;
1505 struct sockaddr_mISDN addr;
1506 struct mISDN_devinfo devinfo;
1509 /* check port counts */
1510 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1512 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1517 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1520 gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1521 if (gsm->gsm_port < 0) {
1522 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1523 return gsm->gsm_port;
1527 devinfo.id = gsm->gsm_port;
1528 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1530 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1533 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1536 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1540 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1543 if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, (pri)?ISDN_P_TE_E1:ISDN_P_TE_S0)) < 0) {
1544 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1546 return gsm->gsm_sock;
1548 /* set nonblocking io */
1549 if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1550 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1554 /* bind socket to dchannel */
1555 memset(&addr, 0, sizeof(addr));
1556 addr.family = AF_ISDN;
1557 addr.dev = gsm->gsm_port;
1559 if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1560 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1569 int gsm_exit(int rc)
1571 /* free gsm instance */
1573 if (gsm->gsm_sock > -1)
1575 /* shutdown network */
1577 bsc_shutdown_net((struct gsm_network *)gsm->network);
1579 // if (gsm->network) {
1580 // free((struct gsm_network *)gsm->network); /* TBD */
1591 char hlr[128], cfg[128], filename[128];
1592 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1595 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
1597 on_dso_load_token();
1603 /* create gsm instance */
1604 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1608 if (!gsm_conf(&gsm->conf)) {
1609 PERROR("%s", gsm_conf_error);
1610 return gsm_exit(-EINVAL);
1614 stderr_target = debug_target_create_stderr();
1615 if (gsm->conf.debug[0])
1616 debug_parse_category_mask(stderr_target, gsm->conf.debug);
1618 /* open pcap file */
1619 if (gsm->conf.pcapfile[0]) {
1620 if (gsm->conf.pcapfile[0] == '/')
1621 SCPY(filename, gsm->conf.pcapfile);
1623 SPRINT(filename, "%s/%s", CONFIG_DATA, gsm->conf.pcapfile);
1624 pcapfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode);
1626 PERROR("Failed to open file for pcap\n");
1627 return gsm_exit(-1);
1629 e1_set_pcap_fd(pcapfd);
1632 /* use RTP proxy for audio streaming */
1633 ipacc_rtp_direct = 0;
1636 if (gsm->conf.hlr[0] == '/')
1637 SCPY(hlr, gsm->conf.hlr);
1639 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1641 PERROR("GSM DB: Failed to init database '%s'. Please check the option settings.\n", hlr);
1642 return gsm_exit(-1);
1644 printf("DB: Database initialized.\n");
1646 PERROR("GSM DB: Failed to prepare database.\n");
1647 return gsm_exit(-1);
1649 printf("DB: Database prepared.\n");
1651 /* bootstrap network */
1652 if (gsm->conf.openbsc_cfg[0] == '/')
1653 SCPY(cfg, gsm->conf.openbsc_cfg);
1655 SPRINT(cfg, "%s/%s", CONFIG_DATA, gsm->conf.openbsc_cfg);
1656 rc = bsc_bootstrap_network(&message_bsc, cfg);
1658 PERROR("Failed to bootstrap GSM network.\n");
1659 return gsm_exit(-1);
1661 gsm->network = bsc_gsmnet;
1663 /* open gsm loop interface */
1664 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1665 return gsm_exit(-1);