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 void on_dso_load_ho_dec(void);
36 int bts_model_unknown_init(void);
37 int bts_model_bs11_init(void);
38 int bts_model_nanobts_init(void);
39 static struct debug_target *stderr_target;
41 /* timer to store statistics */
42 #define DB_SYNC_INTERVAL 60, 0
43 static struct timer_list db_sync_timer;
45 #include "gsm_audio.h"
50 #define PF_ISDN AF_ISDN
54 struct lcr_gsm *gsm = NULL;
56 static unsigned int new_callref = 1;
59 static int _db_store_counter(struct counter *counter, void *data)
61 return db_store_counter(counter);
64 static void db_sync_timer_cb(void *data)
66 /* store counters to database and re-schedule */
67 counters_for_each(_db_store_counter, NULL);
68 bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL);
72 * create and send mncc message
74 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
76 struct gsm_mncc *mncc;
78 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
79 mncc->msg_type = msg_type;
80 mncc->callref = callref;
83 static int send_and_free_mncc(struct gsm_network *net, unsigned int msg_type, void *data)
87 ret = mncc_send(net, msg_type, data);
93 static int delete_event(struct lcr_work *work, void *instance, int index);
98 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)
100 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
101 memset(&p_m_g_delete, 0, sizeof(p_m_g_delete));
102 add_work(&p_m_g_delete, delete_event, this, 0);
105 p_m_g_gsm_b_sock = -1;
106 p_m_g_gsm_b_index = -1;
107 p_m_g_gsm_b_active = 0;
108 p_m_g_notify_pending = NULL;
109 p_m_g_decoder = gsm_audio_create();
110 p_m_g_encoder = gsm_audio_create();
111 if (!p_m_g_encoder || !p_m_g_decoder) {
112 PERROR("Failed to create GSM audio codec instance\n");
113 trigger_work(&p_m_g_delete);
116 p_m_g_tch_connected = 0;
118 PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
126 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
128 del_work(&p_m_g_delete);
130 /* remove queued message */
131 if (p_m_g_notify_pending)
132 message_free(p_m_g_notify_pending);
134 /* close audio transfer socket */
135 if (p_m_g_gsm_b_sock > -1)
140 gsm_audio_destroy(p_m_g_encoder);
142 gsm_audio_destroy(p_m_g_decoder);
146 /* close bsc side bchannel */
147 void Pgsm::bchannel_close(void)
149 if (p_m_g_gsm_b_sock > -1) {
150 unregister_fd(&p_m_g_gsm_b_fd);
151 close(p_m_g_gsm_b_sock);
153 p_m_g_gsm_b_sock = -1;
154 p_m_g_gsm_b_index = -1;
155 p_m_g_gsm_b_active = 0;
158 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index);
160 /* open bsc side bchannel */
161 int Pgsm::bchannel_open(int index)
164 struct sockaddr_mISDN addr;
165 struct mISDNhead act;
167 if (p_m_g_gsm_b_sock > -1) {
168 PERROR("Socket already created for index %d\n", index);
173 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
175 PERROR("Failed to open bchannel-socket for index %d\n", index);
179 memset(&p_m_g_gsm_b_fd, 0, sizeof(p_m_g_gsm_b_fd.fd));
180 p_m_g_gsm_b_fd.fd = p_m_g_gsm_b_sock;
181 register_fd(&p_m_g_gsm_b_fd, LCR_FD_READ, b_handler, this, 0);
184 /* bind socket to bchannel */
185 addr.family = AF_ISDN;
186 addr.dev = gsm->gsm_port;
187 addr.channel = index+1+(index>15);
188 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
190 PERROR("Failed to bind bchannel-socket for index %d\n", index);
194 /* activate bchannel */
195 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
196 act.prim = PH_ACTIVATE_REQ;
198 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
200 PERROR("Failed to activate index %d\n", index);
205 p_m_g_gsm_b_index = index;
210 /* receive from bchannel */
211 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
213 unsigned char frame[33];
215 /* encoder init failed */
219 /* (currently) not connected, so don't flood tch! */
220 if (!p_m_g_tch_connected)
223 /* write to rx buffer */
225 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
226 if (p_m_g_rxpos == 160) {
230 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
236 /* transmit to bchannel */
237 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
239 unsigned char buf[MISDN_HEADER_LEN+len];
240 struct mISDNhead *hh = (struct mISDNhead *)buf;
243 if (!p_m_g_gsm_b_active)
246 /* make and send frame */
247 hh->prim = PH_DATA_REQ;
249 memcpy(buf+MISDN_HEADER_LEN, data, len);
250 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
252 PERROR("Failed to send to socket index %d\n", index);
255 void Pgsm::frame_send(void *_frame)
257 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
258 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
260 frame->msg_type = GSM_TCHF_FRAME;
261 frame->callref = p_m_g_callref;
262 memcpy(frame->data, _frame, 33);
263 mncc_send((struct gsm_network *)gsm->network, frame->msg_type, frame);
267 void Pgsm::frame_receive(void *_frame)
269 struct gsm_data_frame *frame = (struct gsm_data_frame *)_frame;
270 signed short samples[160];
271 unsigned char data[160];
277 if ((frame->data[0]>>4) != 0xd)
278 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
281 gsm_audio_decode(p_m_g_decoder, frame->data, samples);
282 for (i = 0; i < 160; i++) {
283 data[i] = audio_s16_to_law[samples[i] & 0xffff];
287 bchannel_send(PH_DATA_REQ, 0, data, 160);
294 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
298 /* select message and primitive text */
299 SCPY(msgtext, get_mncc_name(msg_type));
302 if (direction == DIRECTION_OUT)
303 SCAT(msgtext, " MSC->BSC");
305 SCAT(msgtext, " MSC<-BSC");
307 /* init trace with given values */
308 start_trace(mISDNport?mISDNport->portnum:-1,
309 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
310 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
311 port?port->p_dialinginfo.id:NULL,
314 port?port->p_serial:0,
320 /* select bchannel */
321 int Pgsm::hunt_bchannel(void)
326 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
327 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
328 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
329 add_trace("conclusion", NULL, "all channels are reserved");
331 return(-34); // no channel
336 while(i < p_m_mISDNport->b_num) {
337 if (p_m_mISDNport->b_port[i] == NULL) {
338 channel = i+1+(i>=15);
344 add_trace("conclusion", NULL, "no channel available");
346 return(-6); // channel unacceptable
348 add_trace("conclusion", NULL, "channel available");
349 add_trace("connect", "channel", "%d", channel);
356 * handles all indications
358 /* SETUP INDICATION */
359 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
362 class Endpoint *epoint;
363 struct lcr_msg *message;
365 struct gsm_mncc *mode, *proceeding, *frame;
367 /* emergency shutdown */
368 printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
369 if (mncc->emergency && !gsm->conf.noemergshut) {
370 start_trace(p_m_mISDNport->portnum,
371 p_m_mISDNport->ifport->interface,
377 "EMERGENCY SHUTDOWN (due to received emergency call)");
381 /* process given callref */
382 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
383 add_trace("callref", "new", "0x%x", callref);
385 /* release in case the ID is already in use */
386 add_trace("error", NULL, "callref already in use");
388 mncc = create_mncc(MNCC_REJ_REQ, callref);
389 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
390 mncc->fields |= MNCC_F_CAUSE;
391 mncc->cause.coding = 3;
392 mncc->cause.location = 1;
393 mncc->cause.value = 47;
394 add_trace("cause", "coding", "%d", mncc->cause.coding);
395 add_trace("cause", "location", "%d", mncc->cause.location);
396 add_trace("cause", "value", "%d", mncc->cause.value);
397 add_trace("reason", NULL, "callref already in use");
399 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
400 new_state(PORT_STATE_RELEASE);
401 trigger_work(&p_m_g_delete);
404 p_m_g_callref = callref;
407 /* if blocked, release call with MT_RELEASE_COMPLETE */
408 if (p_m_mISDNport->ifport->block) {
409 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
410 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
411 mncc->fields |= MNCC_F_CAUSE;
412 mncc->cause.coding = 3;
413 mncc->cause.location = 1;
414 mncc->cause.value = 27;
415 add_trace("cause", "coding", "%d", mncc->cause.coding);
416 add_trace("cause", "location", "%d", mncc->cause.location);
417 add_trace("cause", "value", "%d", mncc->cause.value);
418 add_trace("reason", NULL, "port is blocked");
420 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
421 new_state(PORT_STATE_RELEASE);
422 trigger_work(&p_m_g_delete);
428 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
430 p_callerinfo.present = INFO_PRESENT_ALLOWED;
431 if (mncc->calling.number[0])
432 SCPY(p_callerinfo.id, mncc->calling.number);
434 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
435 SCPY(p_callerinfo.imsi, mncc->imsi);
436 p_callerinfo.screen = INFO_SCREEN_NETWORK;
437 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
438 p_callerinfo.isdn_port = p_m_portnum;
439 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
441 /* dialing information */
442 SCAT(p_dialinginfo.id, mncc->called.number);
443 switch (mncc->called.type) {
445 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
448 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
451 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
454 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
457 if (mncc->emergency) {
458 SCPY(p_dialinginfo.id, "emergency");
460 p_dialinginfo.sending_complete = 1;
462 /* bearer capability */
464 p_capainfo.bearer_capa = INFO_BC_SPEECH;
465 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
466 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
467 p_capainfo.source_mode = B_MODE_TRANSPARENT;
468 p_m_g_mode = p_capainfo.source_mode;
473 ret = channel = hunt_bchannel();
478 ret = seize_bchannel(channel, 1);
481 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
482 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
483 mncc->fields |= MNCC_F_CAUSE;
484 mncc->cause.coding = 3;
485 mncc->cause.location = 1;
486 mncc->cause.value = 34;
487 add_trace("cause", "coding", "%d", mncc->cause.coding);
488 add_trace("cause", "location", "%d", mncc->cause.location);
489 add_trace("cause", "value", "%d", mncc->cause.value);
490 add_trace("reason", NULL, "no channel");
492 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
493 new_state(PORT_STATE_RELEASE);
494 trigger_work(&p_m_g_delete);
497 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
498 if (bchannel_open(p_m_b_index))
501 /* what infos did we got ... */
502 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
503 if (p_callerinfo.id[0])
504 add_trace("calling", "number", "%s", p_callerinfo.id);
506 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
507 add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
508 add_trace("dialing", "number", "%s", p_dialinginfo.id);
511 /* create endpoint */
513 FATAL("Incoming call but already got an endpoint.\n");
514 if (!(epoint = new Endpoint(p_serial, 0)))
515 FATAL("No memory for Endpoint instance\n");
516 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
517 FATAL("No memory for Endpoint Application instance\n");
518 epointlist_new(epoint->ep_serial);
520 /* modify lchan to GSM codec V1 */
521 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
522 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
523 mode->lchan_mode = 0x01; /* GSM V1 */
524 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
526 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
528 /* send call proceeding */
529 gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
530 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
531 if (p_m_mISDNport->tones) {
532 proceeding->fields |= MNCC_F_PROGRESS;
533 proceeding->progress.coding = 3; /* GSM */
534 proceeding->progress.location = 1;
535 proceeding->progress.descr = 8;
536 add_trace("progress", "coding", "%d", proceeding->progress.coding);
537 add_trace("progress", "location", "%d", proceeding->progress.location);
538 add_trace("progress", "descr", "%d", proceeding->progress.descr);
541 send_and_free_mncc((struct gsm_network *)gsm->network, proceeding->msg_type, proceeding);
543 new_state(PORT_STATE_IN_PROCEEDING);
545 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
546 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
548 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
549 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
550 p_m_g_tch_connected = 1;
553 /* send setup message to endpoit */
554 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
555 message->param.setup.isdn_port = p_m_portnum;
556 message->param.setup.port_type = p_type;
557 // message->param.setup.dtmf = 0;
558 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
559 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
560 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
561 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
562 message->param.setup.useruser.len = strlen(mncc->useruser.info);
563 message->param.setup.useruser.protocol = mncc->useruser.proto;
564 message_put(message);
567 /* DTMF INDICATION */
568 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
570 struct lcr_msg *message;
571 struct gsm_mncc *resp;
573 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
574 add_trace("keypad", NULL, "%c", mncc->keypad);
576 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
577 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
580 gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
581 add_trace("keypad", NULL, "%c", mncc->keypad);
583 resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
584 resp->keypad = mncc->keypad;
585 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
587 /* send dialing information */
588 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
589 memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
590 message_put(message);
592 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
594 struct gsm_mncc *resp;
596 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
597 add_trace("keypad", NULL, "%c", mncc->keypad);
601 gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
602 add_trace("keypad", NULL, "%c", mncc->keypad);
604 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
605 resp->keypad = mncc->keypad;
606 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
609 /* PROCEEDING INDICATION */
610 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
612 struct gsm_mncc *mode;
614 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
615 if (mncc->fields & MNCC_F_CAUSE) {
616 add_trace("cause", "coding", "%d", mncc->cause.coding);
617 add_trace("cause", "location", "%", mncc->cause.location);
618 add_trace("cause", "value", "%", mncc->cause.value);
622 /* modify lchan to GSM codec V1 */
623 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
624 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
625 mode->lchan_mode = 0x01; /* GSM V1 */
626 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
628 send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
632 /* ALERTING INDICATION */
633 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
635 struct lcr_msg *message;
637 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
640 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
641 message_put(message);
643 new_state(PORT_STATE_OUT_ALERTING);
647 /* CONNECT INDICATION */
648 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
650 struct gsm_mncc *resp, *frame;
651 struct lcr_msg *message;
653 SCPY(p_connectinfo.id, mncc->connected.number);
654 SCPY(p_connectinfo.imsi, mncc->imsi);
655 p_connectinfo.present = INFO_PRESENT_ALLOWED;
656 p_connectinfo.screen = INFO_SCREEN_NETWORK;
657 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
658 p_connectinfo.isdn_port = p_m_portnum;
659 SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
661 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
662 if (p_connectinfo.id[0])
663 add_trace("connect", "number", "%s", p_connectinfo.id);
664 else if (mncc->imsi[0])
665 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
667 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
671 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
672 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
674 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
676 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
677 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
678 message_put(message);
680 new_state(PORT_STATE_CONNECT);
682 if (!p_m_g_tch_connected) { /* only if ... */
683 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
685 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
686 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
687 p_m_g_tch_connected = 1;
691 /* CONNECT ACK INDICATION */
692 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
694 struct gsm_mncc *frame;
696 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
699 new_state(PORT_STATE_CONNECT);
701 if (!p_m_g_tch_connected) { /* only if ... */
702 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
704 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
705 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
706 p_m_g_tch_connected = 1;
710 /* DISCONNECT INDICATION */
711 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
713 struct lcr_msg *message;
714 int cause = 16, location = 0;
715 struct gsm_mncc *resp;
717 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
718 if (mncc->fields & MNCC_F_CAUSE) {
719 location = mncc->cause.location;
720 cause = mncc->cause.value;
721 add_trace("cause", "coding", "%d", mncc->cause.coding);
722 add_trace("cause", "location", "%d", location);
723 add_trace("cause", "value", "%d", cause);
728 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
729 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
731 resp->fields |= MNCC_F_CAUSE;
732 resp->cause.coding = 3;
733 resp->cause.location = 1;
734 resp->cause.value = cause;
735 add_trace("cause", "coding", "%d", resp->cause.coding);
736 add_trace("cause", "location", "%d", resp->cause.location);
737 add_trace("cause", "value", "%d", resp->cause.value);
740 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
742 /* sending release to endpoint */
743 while(p_epointlist) {
744 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
745 message->param.disconnectinfo.cause = cause;
746 message->param.disconnectinfo.location = location;
747 message_put(message);
749 free_epointlist(p_epointlist);
751 new_state(PORT_STATE_RELEASE);
752 trigger_work(&p_m_g_delete);
755 /* CC_RELEASE INDICATION */
756 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
758 int location = 0, cause = 16;
759 struct lcr_msg *message;
761 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
762 if (mncc->fields & MNCC_F_CAUSE) {
763 location = mncc->cause.location;
764 cause = mncc->cause.value;
765 add_trace("cause", "coding", "%d", mncc->cause.coding);
766 add_trace("cause", "location", "%d", mncc->cause.location);
767 add_trace("cause", "value", "%d", mncc->cause.value);
771 /* sending release to endpoint */
772 while(p_epointlist) {
773 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
774 message->param.disconnectinfo.cause = cause;
775 message->param.disconnectinfo.location = location;
776 message_put(message);
778 free_epointlist(p_epointlist);
780 new_state(PORT_STATE_RELEASE);
781 trigger_work(&p_m_g_delete);
784 /* NOTIFY INDICATION */
785 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
787 struct lcr_msg *message;
789 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
790 add_trace("notify", NULL, "%d", mncc->notify);
793 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
794 message->param.notifyinfo.notify = mncc->notify;
795 message_put(message);
799 /* HOLD INDICATION */
800 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
802 struct lcr_msg *message;
803 struct gsm_mncc *resp, *frame;
805 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
808 /* notify the hold of call */
809 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
810 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
811 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
812 message_put(message);
814 /* acknowledge hold */
815 gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
817 resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
818 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
821 if (p_m_g_tch_connected) { /* it should be true */
822 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
824 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
825 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
826 p_m_g_tch_connected = 0;
831 /* RETRIEVE INDICATION */
832 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
834 struct lcr_msg *message;
835 struct gsm_mncc *resp, *frame;
837 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
840 /* notify the retrieve of call */
841 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
842 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
843 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
844 message_put(message);
846 /* acknowledge retr */
847 gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
849 resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
850 send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
853 if (!p_m_g_tch_connected) { /* it should be true */
854 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
856 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
857 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
858 p_m_g_tch_connected = 1;
863 * BSC sends message to port
865 static int message_bsc(struct gsm_network *net, int msg_type, void *arg)
867 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
868 unsigned int callref = mncc->callref;
870 class Pgsm *pgsm = NULL;
872 struct mISDNport *mISDNport;
874 /* Special messages */
879 callref = mncc->callref;
882 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
883 pgsm = (class Pgsm *)port;
884 if (pgsm->p_m_g_callref == callref) {
891 if (msg_type == GSM_TCHF_FRAME) {
893 pgsm->frame_receive((struct gsm_trau_frame *)arg);
898 if (msg_type != MNCC_SETUP_IND)
901 mISDNport = mISDNport_first;
905 mISDNport = mISDNport->next;
908 struct gsm_mncc *rej;
910 rej = create_mncc(MNCC_REJ_REQ, callref);
911 rej->fields |= MNCC_F_CAUSE;
912 rej->cause.coding = 3;
913 rej->cause.location = 1;
914 rej->cause.value = 27;
915 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
916 add_trace("cause", "coding", "%d", rej->cause.coding);
917 add_trace("cause", "location", "%d", rej->cause.location);
918 add_trace("cause", "value", "%d", rej->cause.value);
920 send_and_free_mncc((struct gsm_network *)gsm->network, rej->msg_type, rej);
923 /* creating port object, transparent until setup with hdlc */
924 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
925 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
927 FATAL("Cannot create Port instance.\n");
932 pgsm->setup_ind(msg_type, callref, mncc);
935 case MNCC_START_DTMF_IND:
936 pgsm->start_dtmf_ind(msg_type, callref, mncc);
939 case MNCC_STOP_DTMF_IND:
940 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
943 case MNCC_CALL_CONF_IND:
944 pgsm->call_conf_ind(msg_type, callref, mncc);
948 pgsm->alert_ind(msg_type, callref, mncc);
952 pgsm->setup_cnf(msg_type, callref, mncc);
955 case MNCC_SETUP_COMPL_IND:
956 pgsm->setup_compl_ind(msg_type, callref, mncc);
960 pgsm->disc_ind(msg_type, callref, mncc);
966 pgsm->rel_ind(msg_type, callref, mncc);
969 case MNCC_NOTIFY_IND:
970 pgsm->notify_ind(msg_type, callref, mncc);
974 pgsm->hold_ind(msg_type, callref, mncc);
977 case MNCC_RETRIEVE_IND:
978 pgsm->retr_ind(msg_type, callref, mncc);
982 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);
988 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
990 struct lcr_msg *message;
992 struct epoint_list *epointlist;
993 struct gsm_mncc *mncc;
996 /* copy setup infos to port */
997 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
998 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
999 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
1000 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1003 if (!p_dialinginfo.id[0]) {
1004 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1005 add_trace("failure", NULL, "No dialed subscriber given.");
1007 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1008 message->param.disconnectinfo.cause = 28;
1009 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1010 message_put(message);
1011 new_state(PORT_STATE_RELEASE);
1012 trigger_work(&p_m_g_delete);
1016 /* release if port is blocked */
1017 if (p_m_mISDNport->ifport->block) {
1018 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1019 add_trace("failure", NULL, "Port blocked.");
1021 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1022 message->param.disconnectinfo.cause = 27; // temp. unavail.
1023 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1024 message_put(message);
1025 new_state(PORT_STATE_RELEASE);
1026 trigger_work(&p_m_g_delete);
1031 ret = channel = hunt_bchannel();
1035 ret = seize_bchannel(channel, 1);
1038 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1039 add_trace("failure", NULL, "No internal audio channel available.");
1041 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1042 message->param.disconnectinfo.cause = 34;
1043 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1044 message_put(message);
1045 new_state(PORT_STATE_RELEASE);
1046 trigger_work(&p_m_g_delete);
1049 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1050 if (bchannel_open(p_m_b_index))
1053 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1054 /* screen outgoing caller id */
1055 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1057 /* attach only if not already */
1058 epointlist = p_epointlist;
1060 if (epointlist->epoint_id == epoint_id)
1062 epointlist = epointlist->next;
1065 epointlist_new(epoint_id);
1068 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1069 p_m_g_callref = new_callref++;
1070 add_trace("callref", "new", "0x%x", p_m_g_callref);
1073 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1074 mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1075 /* caller information */
1076 mncc->fields |= MNCC_F_CALLING;
1077 mncc->calling.plan = 1;
1078 switch (p_callerinfo.ntype) {
1079 case INFO_NTYPE_UNKNOWN:
1080 mncc->calling.type = 0x0;
1082 case INFO_NTYPE_INTERNATIONAL:
1083 mncc->calling.type = 0x1;
1085 case INFO_NTYPE_NATIONAL:
1086 mncc->calling.type = 0x2;
1088 case INFO_NTYPE_SUBSCRIBER:
1089 mncc->calling.type = 0x4;
1091 default: /* INFO_NTYPE_NOTPRESENT */
1092 mncc->fields &= ~MNCC_F_CALLING;
1095 switch (p_callerinfo.screen) {
1096 case INFO_SCREEN_USER:
1097 mncc->calling.screen = 0;
1099 default: /* INFO_SCREEN_NETWORK */
1100 mncc->calling.screen = 3;
1103 switch (p_callerinfo.present) {
1104 case INFO_PRESENT_ALLOWED:
1105 mncc->calling.present = 0;
1107 case INFO_PRESENT_RESTRICTED:
1108 mncc->calling.present = 1;
1110 default: /* INFO_PRESENT_NOTAVAIL */
1111 mncc->calling.present = 2;
1114 if (mncc->fields & MNCC_F_CALLING) {
1115 SCPY(mncc->calling.number, p_callerinfo.id);
1116 add_trace("calling", "type", "%d", mncc->calling.type);
1117 add_trace("calling", "plan", "%d", mncc->calling.plan);
1118 add_trace("calling", "present", "%d", mncc->calling.present);
1119 add_trace("calling", "screen", "%d", mncc->calling.screen);
1120 add_trace("calling", "number", "%s", mncc->calling.number);
1122 /* dialing information */
1123 mncc->fields |= MNCC_F_CALLED;
1124 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
1125 SCPY(mncc->imsi, p_dialinginfo.id+5);
1126 add_trace("dialing", "imsi", "%s", mncc->imsi);
1128 SCPY(mncc->called.number, p_dialinginfo.id);
1129 add_trace("dialing", "number", "%s", mncc->called.number);
1132 /* sending user-user */
1134 /* redirecting number */
1135 mncc->fields |= MNCC_F_REDIRECTING;
1136 mncc->redirecting.plan = 1;
1137 switch (p_redirinfo.ntype) {
1138 case INFO_NTYPE_UNKNOWN:
1139 mncc->redirecting.type = 0x0;
1141 case INFO_NTYPE_INTERNATIONAL:
1142 mncc->redirecting.type = 0x1;
1144 case INFO_NTYPE_NATIONAL:
1145 mncc->redirecting.type = 0x2;
1147 case INFO_NTYPE_SUBSCRIBER:
1148 mncc->redirecting.type = 0x4;
1150 default: /* INFO_NTYPE_NOTPRESENT */
1151 mncc->fields &= ~MNCC_F_REDIRECTING;
1154 switch (p_redirinfo.screen) {
1155 case INFO_SCREEN_USER:
1156 mncc->redirecting.screen = 0;
1158 default: /* INFO_SCREE_NETWORK */
1159 mncc->redirecting.screen = 3;
1162 switch (p_redirinfo.present) {
1163 case INFO_PRESENT_ALLOWED:
1164 mncc->redirecting.present = 0;
1166 case INFO_PRESENT_RESTRICTED:
1167 mncc->redirecting.present = 1;
1169 default: /* INFO_PRESENT_NOTAVAIL */
1170 mncc->redirecting.present = 2;
1173 /* sending redirecting number only in ntmode */
1174 if (mncc->fields & MNCC_F_REDIRECTING) {
1175 SCPY(mncc->redirecting.number, p_redirinfo.id);
1176 add_trace("redir", "type", "%d", mncc->redirecting.type);
1177 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
1178 add_trace("redir", "present", "%d", mncc->redirecting.present);
1179 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
1180 add_trace("redir", "number", "%s", mncc->redirecting.number);
1182 /* bearer capability */
1186 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1188 new_state(PORT_STATE_OUT_SETUP);
1190 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1191 message_put(message);
1193 new_state(PORT_STATE_OUT_PROCEEDING);
1196 /* MESSAGE_NOTIFY */
1197 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1199 struct gsm_mncc *mncc;
1202 // printf("if = %d\n", param->notifyinfo.notify);
1203 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1204 notify = param->notifyinfo.notify & 0x7f;
1205 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1206 /* queue notification */
1207 if (p_m_g_notify_pending)
1208 message_free(p_m_g_notify_pending);
1209 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1210 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1212 /* sending notification */
1213 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1214 add_trace("notify", NULL, "%d", notify);
1216 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1217 mncc->notify = notify;
1218 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1223 /* MESSAGE_ALERTING */
1224 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1226 struct gsm_mncc *mncc;
1229 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1230 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1231 if (p_m_mISDNport->tones) {
1232 mncc->fields |= MNCC_F_PROGRESS;
1233 mncc->progress.coding = 3; /* GSM */
1234 mncc->progress.location = 1;
1235 mncc->progress.descr = 8;
1236 add_trace("progress", "coding", "%d", mncc->progress.coding);
1237 add_trace("progress", "location", "%d", mncc->progress.location);
1238 add_trace("progress", "descr", "%d", mncc->progress.descr);
1241 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1243 new_state(PORT_STATE_IN_ALERTING);
1245 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1246 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1248 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1249 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1250 p_m_g_tch_connected = 1;
1254 /* MESSAGE_CONNECT */
1255 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1257 struct gsm_mncc *mncc;
1259 /* copy connected information */
1260 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
1261 /* screen outgoing caller id */
1262 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1265 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1266 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1267 /* caller information */
1268 mncc->fields |= MNCC_F_CONNECTED;
1269 mncc->connected.plan = 1;
1270 switch (p_callerinfo.ntype) {
1271 case INFO_NTYPE_UNKNOWN:
1272 mncc->connected.type = 0x0;
1274 case INFO_NTYPE_INTERNATIONAL:
1275 mncc->connected.type = 0x1;
1277 case INFO_NTYPE_NATIONAL:
1278 mncc->connected.type = 0x2;
1280 case INFO_NTYPE_SUBSCRIBER:
1281 mncc->connected.type = 0x4;
1283 default: /* INFO_NTYPE_NOTPRESENT */
1284 mncc->fields &= ~MNCC_F_CONNECTED;
1287 switch (p_callerinfo.screen) {
1288 case INFO_SCREEN_USER:
1289 mncc->connected.screen = 0;
1291 default: /* INFO_SCREEN_NETWORK */
1292 mncc->connected.screen = 3;
1295 switch (p_callerinfo.present) {
1296 case INFO_PRESENT_ALLOWED:
1297 mncc->connected.present = 0;
1299 case INFO_PRESENT_RESTRICTED:
1300 mncc->connected.present = 1;
1302 default: /* INFO_PRESENT_NOTAVAIL */
1303 mncc->connected.present = 2;
1306 if (mncc->fields & MNCC_F_CONNECTED) {
1307 SCPY(mncc->connected.number, p_connectinfo.id);
1308 add_trace("connected", "type", "%d", mncc->connected.type);
1309 add_trace("connected", "plan", "%d", mncc->connected.plan);
1310 add_trace("connected", "present", "%d", mncc->connected.present);
1311 add_trace("connected", "screen", "%d", mncc->connected.screen);
1312 add_trace("connected", "number", "%s", mncc->connected.number);
1315 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1317 new_state(PORT_STATE_CONNECT_WAITING);
1320 /* MESSAGE_DISCONNECT */
1321 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1323 struct gsm_mncc *mncc;
1325 /* send disconnect */
1326 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1327 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1328 if (p_m_mISDNport->tones) {
1329 mncc->fields |= MNCC_F_PROGRESS;
1330 mncc->progress.coding = 3; /* GSM */
1331 mncc->progress.location = 1;
1332 mncc->progress.descr = 8;
1333 add_trace("progress", "coding", "%d", mncc->progress.coding);
1334 add_trace("progress", "location", "%d", mncc->progress.location);
1335 add_trace("progress", "descr", "%d", mncc->progress.descr);
1337 mncc->fields |= MNCC_F_CAUSE;
1338 mncc->cause.coding = 3;
1339 mncc->cause.location = param->disconnectinfo.location;
1340 mncc->cause.value = param->disconnectinfo.cause;
1341 add_trace("cause", "coding", "%d", mncc->cause.coding);
1342 add_trace("cause", "location", "%d", mncc->cause.location);
1343 add_trace("cause", "value", "%d", mncc->cause.value);
1345 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1347 new_state(PORT_STATE_OUT_DISCONNECT);
1349 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1350 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1352 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1353 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1354 p_m_g_tch_connected = 1;
1359 /* MESSAGE_RELEASE */
1360 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1362 struct gsm_mncc *mncc;
1365 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1366 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1367 mncc->fields |= MNCC_F_CAUSE;
1368 mncc->cause.coding = 3;
1369 mncc->cause.location = param->disconnectinfo.location;
1370 mncc->cause.value = param->disconnectinfo.cause;
1371 add_trace("cause", "coding", "%d", mncc->cause.coding);
1372 add_trace("cause", "location", "%d", mncc->cause.location);
1373 add_trace("cause", "value", "%d", mncc->cause.value);
1375 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1377 new_state(PORT_STATE_RELEASE);
1378 trigger_work(&p_m_g_delete);
1384 * endpoint sends messages to the port
1386 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1388 if (PmISDN::message_epoint(epoint_id, message_id, param))
1391 switch(message_id) {
1392 case MESSAGE_SETUP: /* dial-out command received from epoint */
1393 if (p_state!=PORT_STATE_IDLE)
1395 message_setup(epoint_id, message_id, param);
1398 case MESSAGE_NOTIFY: /* display and notifications */
1399 message_notify(epoint_id, message_id, param);
1402 // case MESSAGE_FACILITY: /* facility message */
1403 // message_facility(epoint_id, message_id, param);
1406 case MESSAGE_PROCEEDING: /* message not handles */
1409 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1410 if (p_state!=PORT_STATE_IN_PROCEEDING)
1412 message_alerting(epoint_id, message_id, param);
1413 if (p_m_g_notify_pending) {
1414 /* send pending notify message during connect */
1415 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1416 message_free(p_m_g_notify_pending);
1417 p_m_g_notify_pending = NULL;
1421 case MESSAGE_CONNECT: /* call of endpoint is connected */
1422 if (p_state!=PORT_STATE_IN_PROCEEDING
1423 && p_state!=PORT_STATE_IN_ALERTING)
1425 message_connect(epoint_id, message_id, param);
1426 if (p_m_g_notify_pending) {
1427 /* send pending notify message during connect */
1428 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1429 message_free(p_m_g_notify_pending);
1430 p_m_g_notify_pending = NULL;
1434 case MESSAGE_DISCONNECT: /* call has been disconnected */
1435 if (p_state!=PORT_STATE_IN_PROCEEDING
1436 && p_state!=PORT_STATE_IN_ALERTING
1437 && p_state!=PORT_STATE_OUT_SETUP
1438 && p_state!=PORT_STATE_OUT_OVERLAP
1439 && p_state!=PORT_STATE_OUT_PROCEEDING
1440 && p_state!=PORT_STATE_OUT_ALERTING
1441 && p_state!=PORT_STATE_CONNECT
1442 && p_state!=PORT_STATE_CONNECT_WAITING)
1444 message_disconnect(epoint_id, message_id, param);
1447 case MESSAGE_RELEASE: /* release isdn port */
1448 if (p_state==PORT_STATE_RELEASE)
1450 message_release(epoint_id, message_id, param);
1454 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1461 /* deletes only if l3id is release, otherwhise it will be triggered then */
1462 static int delete_event(struct lcr_work *work, void *instance, int index)
1464 class Pgsm *gsmport = (class Pgsm *)instance;
1472 * handler of bchannel events
1474 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1476 class Pgsm *gsmport = (class Pgsm *)instance;
1478 unsigned char buffer[2048+MISDN_HEADER_LEN];
1479 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1481 /* handle message from bchannel */
1482 if (gsmport->p_m_g_gsm_b_sock > -1) {
1483 ret = recv(gsmport->p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1484 if (ret >= (int)MISDN_HEADER_LEN) {
1486 /* we don't care about confirms, we use rx data to sync tx */
1489 /* we receive audio data, we respond to it AND we send tones */
1491 gsmport->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1493 case PH_ACTIVATE_IND:
1494 gsmport->p_m_g_gsm_b_active = 1;
1496 case PH_DEACTIVATE_IND:
1497 gsmport->p_m_g_gsm_b_active = 0;
1501 if (ret < 0 && errno != EWOULDBLOCK)
1502 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1509 static void gsm_sock_close(void)
1511 if (gsm->gsm_sock > -1)
1512 close(gsm->gsm_sock);
1516 static int gsm_sock_open(char *portname)
1520 unsigned long on = 1;
1521 struct sockaddr_mISDN addr;
1522 struct mISDN_devinfo devinfo;
1525 /* check port counts */
1526 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1528 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1533 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1536 gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1537 if (gsm->gsm_port < 0) {
1538 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1539 return gsm->gsm_port;
1543 devinfo.id = gsm->gsm_port;
1544 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1546 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1549 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1552 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1556 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1559 if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, (pri)?ISDN_P_TE_E1:ISDN_P_TE_S0)) < 0) {
1560 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1562 return gsm->gsm_sock;
1564 /* set nonblocking io */
1565 if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1566 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1570 /* bind socket to dchannel */
1571 memset(&addr, 0, sizeof(addr));
1572 addr.family = AF_ISDN;
1573 addr.dev = gsm->gsm_port;
1575 if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1576 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1585 int gsm_exit(int rc)
1587 /* free gsm instance */
1589 if (gsm->gsm_sock > -1)
1591 /* shutdown network */
1593 bsc_shutdown_net((struct gsm_network *)gsm->network);
1595 // if (gsm->network) {
1596 // free((struct gsm_network *)gsm->network); /* TBD */
1607 char hlr[128], cfg[128], filename[128];
1608 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1612 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
1614 on_dso_load_token();
1616 on_dso_load_ho_dec();
1617 stderr_target = debug_target_create_stderr();
1618 debug_add_target(stderr_target);
1620 bts_model_unknown_init();
1621 bts_model_bs11_init();
1622 bts_model_nanobts_init();
1624 /* enable filters */
1625 debug_set_all_filter(stderr_target, 1);
1630 /* create gsm instance */
1631 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1635 if (!gsm_conf(&gsm->conf)) {
1636 PERROR("%s", gsm_conf_error);
1637 return gsm_exit(-EINVAL);
1641 if (gsm->conf.debug[0])
1642 debug_parse_category_mask(stderr_target, gsm->conf.debug);
1644 /* open pcap file */
1645 if (gsm->conf.pcapfile[0]) {
1646 if (gsm->conf.pcapfile[0] == '/')
1647 SCPY(filename, gsm->conf.pcapfile);
1649 SPRINT(filename, "%s/%s", CONFIG_DATA, gsm->conf.pcapfile);
1650 pcapfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode);
1652 PERROR("Failed to open file for pcap\n");
1653 return gsm_exit(-1);
1655 e1_set_pcap_fd(pcapfd);
1658 /* use RTP proxy for audio streaming */
1659 ipacc_rtp_direct = 0;
1662 if (gsm->conf.hlr[0] == '/')
1663 SCPY(hlr, gsm->conf.hlr);
1665 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1667 PERROR("GSM DB: Failed to init database '%s'. Please check the option settings.\n", hlr);
1668 return gsm_exit(-1);
1670 printf("DB: Database initialized.\n");
1672 PERROR("GSM DB: Failed to prepare database.\n");
1673 return gsm_exit(-1);
1675 printf("DB: Database prepared.\n");
1677 /* setup the timer */
1678 db_sync_timer.cb = db_sync_timer_cb;
1679 db_sync_timer.data = NULL;
1680 bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL);
1682 /* bootstrap network */
1683 if (gsm->conf.openbsc_cfg[0] == '/')
1684 SCPY(cfg, gsm->conf.openbsc_cfg);
1686 SPRINT(cfg, "%s/%s", CONFIG_DATA, gsm->conf.openbsc_cfg);
1687 rc = bsc_bootstrap_network(&message_bsc, cfg);
1689 PERROR("Failed to bootstrap GSM network.\n");
1690 return gsm_exit(-1);
1692 gsm->network = bsc_gsmnet;
1694 /* open gsm loop interface */
1695 if (gsm_sock_open(gsm->conf.interface_bsc)) {
1696 return gsm_exit(-1);
1703 * handles bsc select function within LCR's main loop
1705 int handle_gsm(void)
1709 ret1 = bsc_upqueue((struct gsm_network *)gsm->network);
1710 debug_reset_context();
1711 ret2 = bsc_select_main(1); /* polling */