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 struct lcr_msg *message;
765 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
766 add_trace("notify", NULL, "%d", mncc->notify);
769 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
770 message->param.notifyinfo.notify = mncc->notify;
771 message_put(message);
775 /* HOLD INDICATION */
776 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
778 struct lcr_msg *message;
779 struct gsm_mncc *resp, *frame;
781 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
784 /* notify the hold of call */
785 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
786 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
787 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
788 message_put(message);
790 /* acknowledge hold */
791 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
793 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
794 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
797 if (p_m_g_tch_connected) { /* it should be true */
798 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
800 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
801 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
802 p_m_g_tch_connected = 0;
807 /* RETRIEVE INDICATION */
808 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
810 struct lcr_msg *message;
811 struct gsm_mncc *resp, *frame;
813 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
816 /* notify the retrieve of call */
817 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
818 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
819 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
820 message_put(message);
822 /* acknowledge retr */
823 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
825 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
826 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
829 if (!p_m_g_tch_connected) { /* it should be true */
830 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
832 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
833 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
834 p_m_g_tch_connected = 1;
839 * BSC sends message to port
841 static int message_bsc(struct gsm_network *net, int msg_type, void *arg)
843 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
844 unsigned int callref = mncc->callref;
846 class Pgsm *pgsm = NULL;
848 struct mISDNport *mISDNport;
850 /* Special messages */
855 callref = mncc->callref;
858 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
859 pgsm = (class Pgsm *)port;
860 if (pgsm->p_m_g_callref == callref) {
867 if (msg_type == GSM_TCHF_FRAME) {
869 pgsm->frame_receive((struct gsm_trau_frame *)arg);
874 if (msg_type != MNCC_SETUP_IND)
877 mISDNport = mISDNport_first;
881 mISDNport = mISDNport->next;
884 struct gsm_mncc *rej;
886 rej = create_mncc(MNCC_REJ_REQ, callref);
887 rej->fields |= MNCC_F_CAUSE;
888 rej->cause.coding = 3;
889 rej->cause.location = 1;
890 rej->cause.value = 27;
891 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
892 add_trace("cause", "coding", "%d", rej->cause.coding);
893 add_trace("cause", "location", "%d", rej->cause.location);
894 add_trace("cause", "value", "%d", rej->cause.value);
896 send_and_free_mncc((struct gsm_network *)gsm->network, rej->msg_type, rej);
899 /* creating port object, transparent until setup with hdlc */
900 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
901 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
903 FATAL("Cannot create Port instance.\n");
908 pgsm->setup_ind(msg_type, callref, mncc);
911 case MNCC_START_DTMF_IND:
912 pgsm->start_dtmf_ind(msg_type, callref, mncc);
915 case MNCC_STOP_DTMF_IND:
916 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
919 case MNCC_CALL_CONF_IND:
920 pgsm->call_conf_ind(msg_type, callref, mncc);
924 pgsm->alert_ind(msg_type, callref, mncc);
928 pgsm->setup_cnf(msg_type, callref, mncc);
931 case MNCC_SETUP_COMPL_IND:
932 pgsm->setup_compl_ind(msg_type, callref, mncc);
936 pgsm->disc_ind(msg_type, callref, mncc);
942 pgsm->rel_ind(msg_type, callref, mncc);
945 case MNCC_NOTIFY_IND:
946 pgsm->notify_ind(msg_type, callref, mncc);
950 pgsm->hold_ind(msg_type, callref, mncc);
953 case MNCC_RETRIEVE_IND:
954 pgsm->retr_ind(msg_type, callref, mncc);
958 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);
964 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
966 struct lcr_msg *message;
968 struct epoint_list *epointlist;
969 struct gsm_mncc *mncc;
972 /* copy setup infos to port */
973 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
974 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
975 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
976 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
979 if (!p_dialinginfo.id[0]) {
980 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
981 add_trace("failure", NULL, "No dialed subscriber given.");
983 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
984 message->param.disconnectinfo.cause = 28;
985 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
986 message_put(message);
987 new_state(PORT_STATE_RELEASE);
992 /* release if port is blocked */
993 if (p_m_mISDNport->ifport->block) {
994 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
995 add_trace("failure", NULL, "Port blocked.");
997 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
998 message->param.disconnectinfo.cause = 27; // temp. unavail.
999 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1000 message_put(message);
1001 new_state(PORT_STATE_RELEASE);
1007 ret = channel = hunt_bchannel();
1011 ret = seize_bchannel(channel, 1);
1014 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1015 add_trace("failure", NULL, "No internal audio channel available.");
1017 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1018 message->param.disconnectinfo.cause = 34;
1019 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1020 message_put(message);
1021 new_state(PORT_STATE_RELEASE);
1025 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1026 if (bchannel_open(p_m_b_index))
1029 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1030 /* screen outgoing caller id */
1031 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1033 /* attach only if not already */
1034 epointlist = p_epointlist;
1036 if (epointlist->epoint_id == epoint_id)
1038 epointlist = epointlist->next;
1041 epointlist_new(epoint_id);
1044 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1045 p_m_g_callref = new_callref++;
1046 add_trace("callref", "new", "0x%x", p_m_g_callref);
1049 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1050 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1051 /* caller information */
1052 mncc->fields |= MNCC_F_CALLING;
1053 mncc->calling.plan = 1;
1054 switch (p_callerinfo.ntype) {
1055 case INFO_NTYPE_UNKNOWN:
1056 mncc->calling.type = 0x0;
1058 case INFO_NTYPE_INTERNATIONAL:
1059 mncc->calling.type = 0x1;
1061 case INFO_NTYPE_NATIONAL:
1062 mncc->calling.type = 0x2;
1064 case INFO_NTYPE_SUBSCRIBER:
1065 mncc->calling.type = 0x4;
1067 default: /* INFO_NTYPE_NOTPRESENT */
1068 mncc->fields &= ~MNCC_F_CALLING;
1071 switch (p_callerinfo.screen) {
1072 case INFO_SCREEN_USER:
1073 mncc->calling.screen = 0;
1075 default: /* INFO_SCREEN_NETWORK */
1076 mncc->calling.screen = 3;
1079 switch (p_callerinfo.present) {
1080 case INFO_PRESENT_ALLOWED:
1081 mncc->calling.present = 0;
1083 case INFO_PRESENT_RESTRICTED:
1084 mncc->calling.present = 1;
1086 default: /* INFO_PRESENT_NOTAVAIL */
1087 mncc->calling.present = 2;
1090 if (mncc->fields & MNCC_F_CALLING) {
1091 SCPY(mncc->calling.number, p_callerinfo.id);
1092 add_trace("calling", "type", "%d", mncc->calling.type);
1093 add_trace("calling", "plan", "%d", mncc->calling.plan);
1094 add_trace("calling", "present", "%d", mncc->calling.present);
1095 add_trace("calling", "screen", "%d", mncc->calling.screen);
1096 add_trace("calling", "number", "%s", mncc->calling.number);
1098 /* dialing information */
1099 mncc->fields |= MNCC_F_CALLED;
1100 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
1101 SCPY(mncc->imsi, p_dialinginfo.id+5);
1102 add_trace("dialing", "imsi", "%s", mncc->imsi);
1104 SCPY(mncc->called.number, p_dialinginfo.id);
1105 add_trace("dialing", "number", "%s", mncc->called.number);
1108 /* sending user-user */
1110 /* redirecting number */
1111 mncc->fields |= MNCC_F_REDIRECTING;
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->fields &= ~MNCC_F_REDIRECTING;
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->fields & MNCC_F_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) {
1208 mncc->fields |= MNCC_F_PROGRESS;
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->fields |= MNCC_F_CONNECTED;
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->fields &= ~MNCC_F_CONNECTED;
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->fields & MNCC_F_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) {
1305 mncc->fields |= MNCC_F_PROGRESS;
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);
1313 mncc->fields |= MNCC_F_CAUSE;
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);
1343 mncc->fields |= MNCC_F_CAUSE;
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, (pri)?ISDN_P_TE_E1: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 bsc_shutdown_net((struct gsm_network *)gsm->network);
1586 // if (gsm->network) {
1587 // free((struct gsm_network *)gsm->network); /* TBD */
1598 char hlr[128], cfg[128], filename[128];
1599 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1602 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
1604 on_dso_load_token();
1610 /* create gsm instance */
1611 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1615 if (!gsm_conf(&gsm->conf)) {
1616 PERROR("%s", gsm_conf_error);
1617 return gsm_exit(-EINVAL);
1621 stderr_target = debug_target_create_stderr();
1622 if (gsm->conf.debug[0])
1623 debug_parse_category_mask(stderr_target, gsm->conf.debug);
1625 /* open pcap file */
1626 if (gsm->conf.pcapfile[0]) {
1627 if (gsm->conf.pcapfile[0] == '/')
1628 SCPY(filename, gsm->conf.pcapfile);
1630 SPRINT(filename, "%s/%s", CONFIG_DATA, gsm->conf.pcapfile);
1631 pcapfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode);
1633 PERROR("Failed to open file for pcap\n");
1634 return gsm_exit(-1);
1636 e1_set_pcap_fd(pcapfd);
1639 /* use RTP proxy for audio streaming */
1640 ipacc_rtp_direct = 0;
1643 if (gsm->conf.hlr[0] == '/')
1644 SCPY(hlr, gsm->conf.hlr);
1646 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1648 PERROR("GSM DB: Failed to init database '%s'. Please check the option settings.\n", hlr);
1649 return gsm_exit(-1);
1651 printf("DB: Database initialized.\n");
1653 PERROR("GSM DB: Failed to prepare database.\n");
1654 return gsm_exit(-1);
1656 printf("DB: Database prepared.\n");
1658 /* bootstrap network */
1659 if (gsm->conf.openbsc_cfg[0] == '/')
1660 SCPY(cfg, gsm->conf.openbsc_cfg);
1662 SPRINT(cfg, "%s/%s", CONFIG_DATA, gsm->conf.openbsc_cfg);
1663 rc = bsc_bootstrap_network(&message_bsc, cfg);
1665 PERROR("Failed to bootstrap GSM network.\n");
1666 return gsm_exit(-1);
1668 gsm->network = bsc_gsmnet;
1670 /* open gsm loop interface */
1671 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1672 return gsm_exit(-1);