1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** mISDN gsm (BS mode) **
10 \*****************************************************************************/
15 struct lcr_gsm *gsm_bs = NULL;
17 #define PAYLOAD_TYPE_GSM 3
22 unsigned char dtmf_samples[16][8000];
23 static int dtmf_x[4] = { 1209, 1336, 1477, 1633 };
24 static int dtmf_y[4] = { 697, 770, 852, 941 };
26 void generate_dtmf(void)
28 double fx, fy, sample;
32 for (y = 0; y < 4; y++) {
33 fy = 2 * 3.1415927 * ((double)dtmf_y[y]) / 8000.0;
34 for (x = 0; x < 4; x++) {
35 fx = 2 * 3.1415927 * ((double)dtmf_x[x]) / 8000.0;
36 law = dtmf_samples[y << 2 | x];
37 for (i = 0; i < 8000; i++) {
38 sample = sin(fy * ((double)i)) * 0.251 * 32767.0; /* -6 dB */
39 sample += sin(fx * ((double)i)) * 0.158 * 32767.0; /* -8 dB */
40 *law++ = audio_s16_to_law[(int)sample & 0xffff];
50 Pgsm_bs::Pgsm_bs(int type, char *portname, struct port_settings *settings, struct interface *interface) : Pgsm(type, portname, settings, interface)
56 PDEBUG(DEBUG_GSM, "Created new GSMBSPort(%s).\n", portname);
64 PDEBUG(DEBUG_GSM, "Destroyed GSM BS process(%s).\n", p_name);
67 static const char *media_type2name(unsigned char media_type) {
75 case MEDIA_TYPE_GSM_HR:
77 case MEDIA_TYPE_GSM_EFR:
86 /* PROCEEDING INDICATION (from MS) */
87 void Pgsm_bs::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
90 unsigned char payload_types[8];
93 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
94 if (mncc->fields & MNCC_F_CAUSE) {
95 add_trace("cause", "coding", "%d", mncc->cause.coding);
96 add_trace("cause", "location", "%", mncc->cause.location);
97 add_trace("cause", "value", "%", mncc->cause.value);
101 new_state(PORT_STATE_OUT_PROCEEDING);
103 /* get list of offered payload types
104 * if list ist empty, the FR V1 is selected */
105 select_payload_type(mncc, payload_types, media_types, &payloads, sizeof(payload_types));
106 /* if no given payload type is supported, we select from channel type */
108 switch (mncc->lchan_type) {
109 case GSM_LCHAN_TCH_F:
110 media_types[0] = MEDIA_TYPE_GSM;
111 payload_types[0] = PAYLOAD_TYPE_GSM;
114 case GSM_LCHAN_TCH_H:
115 media_types[0] = MEDIA_TYPE_GSM_HR;
116 payload_types[0] = 96; /* dynamic */
120 mncc = create_mncc(MNCC_REL_REQ, callref);
121 gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
122 mncc->fields |= MNCC_F_CAUSE;
123 mncc->cause.coding = 3;
124 mncc->cause.location = 1;
125 mncc->cause.value = 65;
126 add_trace("cause", "coding", "%d", mncc->cause.coding);
127 add_trace("cause", "location", "%d", mncc->cause.location);
128 add_trace("cause", "value", "%d", mncc->cause.value);
129 add_trace("reason", NULL, "Given lchan not supported");
131 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
132 new_state(PORT_STATE_RELEASE);
133 trigger_work(&p_g_delete);
138 /* select first payload type that matches the rtp list */
139 if (p_g_rtp_bridge) {
142 for (i = 0; i < p_g_rtp_payloads; i++) {
143 for (j = 0; j < payloads; j++) {
144 if (p_g_rtp_media_types[i] == media_types[j])
150 if (i == p_g_rtp_payloads) {
151 struct lcr_msg *message;
153 /* payload offered by remote RTP is not supported */
154 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
155 message->param.disconnectinfo.cause = 65;
156 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
157 message_put(message);
159 mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
160 gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
161 mncc->fields |= MNCC_F_CAUSE;
162 mncc->cause.coding = 3;
163 mncc->cause.location = LOCATION_PRIVATE_LOCAL;
164 mncc->cause.value = 65;
165 add_trace("cause", "coding", "%d", mncc->cause.coding);
166 add_trace("cause", "location", "%d", mncc->cause.location);
167 add_trace("cause", "value", "%d", mncc->cause.value);
168 add_trace("reason", NULL, "None of the payload types are supported by MS");
170 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
171 new_state(PORT_STATE_RELEASE);
172 trigger_work(&p_g_delete);
176 modify_lchan(p_g_rtp_media_types[i]);
177 /* use the payload type from received rtp list, not from locally generated payload types */
178 p_g_payload_type = p_g_rtp_payload_types[i];
180 /* modify to first given payload */
181 modify_lchan(media_types[0]);
182 p_g_payload_type = payload_types[0];
186 /* DTMF INDICATION */
187 void Pgsm_bs::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
189 struct gsm_mncc *resp;
191 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
192 add_trace("keypad", NULL, "%c", mncc->keypad);
194 SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
195 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
198 gsm_trace_header(p_interface_name, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
199 add_trace("keypad", NULL, "%c", mncc->keypad);
201 resp = create_mncc(MNCC_START_DTMF_RSP, p_g_callref);
202 resp->fields |= MNCC_F_KEYPAD;
203 resp->keypad = mncc->keypad;
204 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
206 if (p_g_rtp_bridge) {
207 /* if two members are bridged */
208 if (p_bridge && p_bridge->first && p_bridge->first->next && !p_bridge->first->next->next) {
209 class Port *remote = NULL;
211 /* select other member */
212 if (p_bridge->first->port == this)
213 remote = p_bridge->first->next->port;
214 if (p_bridge->first->next->port == this)
215 remote = p_bridge->first->port;
218 struct lcr_msg *message;
220 /* send dtmf information, because we bridge RTP directly */
221 message = message_create(0, remote->p_serial, EPOINT_TO_PORT, MESSAGE_DTMF);
222 message->param.dtmf = mncc->keypad;
223 message_put(message);
227 /* generate DTMF tones, since we do audio forwarding inside LCR */
228 switch (mncc->keypad) {
229 case '1': p_g_dtmf = dtmf_samples[0]; break;
230 case '2': p_g_dtmf = dtmf_samples[1]; break;
231 case '3': p_g_dtmf = dtmf_samples[2]; break;
233 case 'A': p_g_dtmf = dtmf_samples[3]; break;
234 case '4': p_g_dtmf = dtmf_samples[4]; break;
235 case '5': p_g_dtmf = dtmf_samples[5]; break;
236 case '6': p_g_dtmf = dtmf_samples[6]; break;
238 case 'B': p_g_dtmf = dtmf_samples[7]; break;
239 case '7': p_g_dtmf = dtmf_samples[8]; break;
240 case '8': p_g_dtmf = dtmf_samples[9]; break;
241 case '9': p_g_dtmf = dtmf_samples[10]; break;
243 case 'C': p_g_dtmf = dtmf_samples[11]; break;
244 case '*': p_g_dtmf = dtmf_samples[12]; break;
245 case '0': p_g_dtmf = dtmf_samples[13]; break;
246 case '#': p_g_dtmf = dtmf_samples[14]; break;
248 case 'D': p_g_dtmf = dtmf_samples[15]; break;
253 void Pgsm_bs::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
255 struct gsm_mncc *resp;
257 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
258 add_trace("keypad", NULL, "%c", mncc->keypad);
262 gsm_trace_header(p_interface_name, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
263 add_trace("keypad", NULL, "%c", mncc->keypad);
265 resp = create_mncc(MNCC_STOP_DTMF_RSP, p_g_callref);
266 resp->keypad = mncc->keypad;
267 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
273 /* HOLD INDICATION */
274 void Pgsm_bs::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
276 struct lcr_msg *message;
277 struct gsm_mncc *resp, *frame;
279 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
282 /* notify the hold of call */
283 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
284 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
285 message->param.notifyinfo.local = 1; /* call is held by supplementary service */
286 message_put(message);
288 /* acknowledge hold */
289 gsm_trace_header(p_interface_name, this, MNCC_HOLD_CNF, DIRECTION_OUT);
291 resp = create_mncc(MNCC_HOLD_CNF, p_g_callref);
292 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
295 if (p_g_tch_connected) { /* it should be true */
296 gsm_trace_header(p_interface_name, this, MNCC_FRAME_DROP, DIRECTION_OUT);
298 frame = create_mncc(MNCC_FRAME_DROP, p_g_callref);
299 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
300 p_g_tch_connected = 0;
305 /* RETRIEVE INDICATION */
306 void Pgsm_bs::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
308 struct lcr_msg *message;
309 struct gsm_mncc *resp, *frame;
311 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
314 /* notify the retrieve of call */
315 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
316 message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
317 message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
318 message_put(message);
320 /* acknowledge retr */
321 gsm_trace_header(p_interface_name, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
323 resp = create_mncc(MNCC_RETRIEVE_CNF, p_g_callref);
324 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
327 if (!p_g_tch_connected) { /* it should be true */
328 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
330 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
331 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
332 p_g_tch_connected = 1;
337 * select payload type by given list or GSM V1 FR
338 * return the payload type or 0 if not given
341 void Pgsm_bs::select_payload_type(struct gsm_mncc *mncc, unsigned char *payload_types, int *media_types, int *payloads, int max_payloads)
344 unsigned char payload_type;
346 void *encoder, *decoder;
350 gsm_trace_header(p_interface_name, this, 1 /* codec negotioation */, DIRECTION_NONE);
351 if ((mncc->fields & MNCC_F_BEARER_CAP)) {
352 /* select preferred payload type from list */
354 unsigned char dynamic_type = 96;
356 add_trace("bearer", "capa", "given by MS");
357 for (i = 0; mncc->bearer_cap.speech_ver[i] >= 0; i++) {
359 /* select payload type we support */
360 switch (mncc->bearer_cap.speech_ver[i]) {
362 add_trace("speech", "version", "Full Rate given");
363 media_type = MEDIA_TYPE_GSM;
364 payload_type = PAYLOAD_TYPE_GSM;
365 encoder = p_g_fr_encoder;
366 decoder = p_g_fr_decoder;
369 add_trace("speech", "version", "EFR given");
370 media_type = MEDIA_TYPE_GSM_EFR;
371 payload_type = dynamic_type++;
372 encoder = p_g_amr_encoder;
373 decoder = p_g_amr_decoder;
376 add_trace("speech", "version", "AMR given");
377 media_type = MEDIA_TYPE_AMR;
378 payload_type = dynamic_type++;
379 encoder = p_g_amr_encoder;
380 decoder = p_g_amr_decoder;
383 add_trace("speech", "version", "Half Rate given");
384 media_type = MEDIA_TYPE_GSM_HR;
385 payload_type = dynamic_type++;
386 encoder = p_g_hr_encoder;
387 decoder = p_g_hr_decoder;
391 add_trace("speech", "version", "AMR Half Rate given");
392 media_type = MEDIA_TYPE_AMR;
393 payload_type = dynamic_type++;
394 encoder = p_g_amr_encoder;
395 decoder = p_g_amr_decoder;
399 add_trace("speech", "version", "Analog 8000Hz given");
400 media_type = MEDIA_TYPE_ANALOG;
401 payload_type = dynamic_type++;
406 add_trace("speech", "version", "%d given", mncc->bearer_cap.speech_ver[i]);
410 /* wen don't support it, so we check the next */
412 add_trace("speech", "ignored", "Not supported by LCR");
415 if (!half && mncc->lchan_type != GSM_LCHAN_TCH_F) {
416 add_trace("speech", "ignored", "Not TCH/F");
419 if (half && mncc->lchan_type != GSM_LCHAN_TCH_H) {
420 add_trace("speech", "ignored", "Not TCH/H");
423 if (!p_g_rtp_bridge) {
424 if (!encoder || !decoder) {
425 add_trace("speech", "ignored", "Codec not supported");
429 if (*payloads <= max_payloads) {
430 media_types[*payloads] = media_type;
431 payload_types[*payloads] = payload_type;
436 add_trace("bearer", "capa", "not given by MS");
437 add_trace("speech", "version", "Full Rate given");
438 media_types[0] = MEDIA_TYPE_GSM;
439 payload_types[0] = PAYLOAD_TYPE_GSM;
443 add_trace("error", "", "All given payload types unsupported");
448 * handles all indications
450 /* SETUP INDICATION */
451 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
453 class Endpoint *epoint;
454 struct lcr_msg *message;
455 struct gsm_mncc *proceeding, *frame;
456 struct interface *interface;
458 unsigned char payload_types[8];
461 interface = getinterfacebyname(p_interface_name);
463 PERROR("Cannot find interface %s.\n", p_interface_name);
467 /* process given callref */
468 gsm_trace_header(p_interface_name, this, 0, DIRECTION_IN);
469 add_trace("callref", "new", "0x%x", callref);
471 /* release in case the ID is already in use */
472 add_trace("error", NULL, "callref already in use");
475 mncc = create_mncc(MNCC_REJ_REQ, callref);
476 gsm_trace_header(p_interface_name, this, MNCC_REJ_REQ, DIRECTION_OUT);
477 mncc->fields |= MNCC_F_CAUSE;
478 mncc->cause.coding = 3;
479 mncc->cause.location = 1;
480 mncc->cause.value = 47;
481 add_trace("cause", "coding", "%d", mncc->cause.coding);
482 add_trace("cause", "location", "%d", mncc->cause.location);
483 add_trace("cause", "value", "%d", mncc->cause.value);
484 add_trace("reason", NULL, "callref already in use");
486 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
487 new_state(PORT_STATE_RELEASE);
488 trigger_work(&p_g_delete);
491 if (callref < 0x40000000) {
492 /* release in case the ID is invalid */
493 add_trace("error", NULL, "callref invalid, not of BSC type");
496 p_g_callref = callref;
501 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
503 p_callerinfo.present = INFO_PRESENT_ALLOWED;
504 if (mncc->calling.number[0])
505 SCPY(p_callerinfo.id, mncc->calling.number);
507 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
508 SCPY(p_callerinfo.imsi, mncc->imsi);
509 p_callerinfo.screen = INFO_SCREEN_NETWORK;
510 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
511 SCPY(p_callerinfo.interface, p_interface_name);
513 /* dialing information */
514 SCAT(p_dialinginfo.id, mncc->called.number);
515 switch (mncc->called.type) {
517 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
520 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
523 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
526 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
529 if (mncc->emergency) {
530 SCPY(p_dialinginfo.id, "emergency");
532 p_dialinginfo.sending_complete = 1;
534 /* bearer capability */
535 p_capainfo.bearer_capa = INFO_BC_SPEECH;
536 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
537 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
538 p_capainfo.source_mode = B_MODE_TRANSPARENT;
539 p_g_mode = p_capainfo.source_mode;
541 /* get list of offered payload types
542 * if list ist empty, the FR V1 is selected */
543 select_payload_type(mncc, payload_types, media_types, &payloads, sizeof(payload_types));
544 /* if no given payload type is supported, we select from channel type */
546 switch (mncc->lchan_type) {
547 case GSM_LCHAN_TCH_F:
548 media_types[0] = MEDIA_TYPE_GSM;
549 payload_types[0] = PAYLOAD_TYPE_GSM;
552 case GSM_LCHAN_TCH_H:
553 media_types[0] = MEDIA_TYPE_GSM_HR;
554 payload_types[0] = 96; /* dynamic */
558 mncc = create_mncc(MNCC_REJ_REQ, callref);
559 gsm_trace_header(p_interface_name, this, MNCC_REJ_REQ, DIRECTION_OUT);
560 mncc->fields |= MNCC_F_CAUSE;
561 mncc->cause.coding = 3;
562 mncc->cause.location = 1;
563 mncc->cause.value = 65;
564 add_trace("cause", "coding", "%d", mncc->cause.coding);
565 add_trace("cause", "location", "%d", mncc->cause.location);
566 add_trace("cause", "value", "%d", mncc->cause.value);
567 add_trace("reason", NULL, "Given lchan not supported");
569 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
570 new_state(PORT_STATE_RELEASE);
571 trigger_work(&p_g_delete);
576 /* if no given payload type is supported, we reject the call */
583 /* what infos did we got ... */
584 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
585 if (p_callerinfo.id[0])
586 add_trace("calling", "number", "%s", p_callerinfo.id);
587 else if (p_callerinfo.imsi[0])
588 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
589 add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
590 add_trace("dialing", "number", "%s", p_dialinginfo.id);
593 /* create endpoint */
595 FATAL("Incoming call but already got an endpoint.\n");
596 if (!(epoint = new Endpoint(p_serial, 0)))
597 FATAL("No memory for Endpoint instance\n");
598 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
599 epointlist_new(epoint->ep_serial);
601 /* modify lchan in case of no rtp bridge */
603 modify_lchan(media_types[0]);
605 /* send call proceeding */
606 gsm_trace_header(p_interface_name, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
607 proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_g_callref);
609 proceeding->fields |= MNCC_F_PROGRESS;
610 proceeding->progress.coding = 3; /* GSM */
611 proceeding->progress.location = 1;
612 proceeding->progress.descr = 8;
613 add_trace("progress", "coding", "%d", proceeding->progress.coding);
614 add_trace("progress", "location", "%d", proceeding->progress.location);
615 add_trace("progress", "descr", "%d", proceeding->progress.descr);
618 send_and_free_mncc(p_g_lcr_gsm, proceeding->msg_type, proceeding);
620 new_state(PORT_STATE_IN_PROCEEDING);
622 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
623 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
625 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
626 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
627 p_g_tch_connected = 1;
630 /* send setup message to endpoit */
631 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
632 message->param.setup.port_type = p_type;
633 // message->param.setup.dtmf = 0;
634 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
635 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
636 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
637 SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
638 message->param.setup.useruser.len = strlen(mncc->useruser.info);
639 message->param.setup.useruser.protocol = mncc->useruser.proto;
640 if (p_g_rtp_bridge) {
641 struct gsm_mncc_rtp *rtp;
644 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding setup\n");
645 p_g_setup_pending = message;
646 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
647 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
649 for (i = 0; i < (int)sizeof(message->param.setup.rtpinfo.payload_types) && i < payloads; i++) {
650 message->param.setup.rtpinfo.media_types[i] = media_types[i];
651 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
652 message->param.setup.rtpinfo.payloads++;
656 message_put(message);
661 * BSC sends message to port
663 int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
665 struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
666 unsigned int callref = mncc->callref;
668 class Pgsm_bs *pgsm_bs = NULL;
670 // struct mISDNport *mISDNport;
672 /* Special messages */
677 callref = mncc->callref;
680 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
681 pgsm_bs = (class Pgsm_bs *)port;
682 if (pgsm_bs->p_g_callref == callref) {
689 if (msg_type == GSM_TCHF_FRAME
690 || msg_type == GSM_TCHF_FRAME_EFR
691 || msg_type == GSM_TCHH_FRAME
692 || msg_type == GSM_TCH_FRAME_AMR
693 || msg_type == ANALOG_8000HZ
694 || msg_type == GSM_BAD_FRAME) {
696 /* inject DTMF, if enabled */
697 if (pgsm_bs->p_g_dtmf) {
698 unsigned char data[160];
701 for (i = 0; i < 160; i++) {
702 data[i] = pgsm_bs->p_g_dtmf[pgsm_bs->p_g_dtmf_index++];
703 if (pgsm_bs->p_g_dtmf_index == 8000)
704 pgsm_bs->p_g_dtmf_index = 0;
707 pgsm_bs->bridge_tx(data, 160);
709 pgsm_bs->frame_receive(arg);
710 /* if we do not bridge we need to inject audio, if available */
711 if (!pgsm_bs->p_bridge || pgsm_bs->p_tone_name[0]) {
712 unsigned char data[160];
715 i = pgsm_bs->read_audio(data, 160);
717 pgsm_bs->audio_send(data, i);
724 struct interface *interface;
726 if (msg_type != MNCC_SETUP_IND)
729 interface = getinterfacebyname(lcr_gsm->interface_name);
731 struct gsm_mncc *rej;
733 rej = create_mncc(MNCC_REJ_REQ, callref);
734 rej->fields |= MNCC_F_CAUSE;
735 rej->cause.coding = 3;
736 rej->cause.location = 1;
737 rej->cause.value = 27;
738 gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
739 add_trace("cause", "coding", "%d", rej->cause.coding);
740 add_trace("cause", "location", "%d", rej->cause.location);
741 add_trace("cause", "value", "%d", rej->cause.value);
742 add_trace("reason", NULL, "interface %s not found", lcr_gsm->interface_name);
744 send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
747 /* creating port object, transparent until setup with hdlc */
748 SPRINT(name, "%s-%d-in", interface->name, 0);
749 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, name, NULL, interface)))
750 FATAL("Cannot create Port instance.\n");
755 pgsm_bs->setup_ind(msg_type, callref, mncc);
758 case MNCC_RTP_CREATE:
759 pgsm_bs->rtp_create_ind(msg_type, callref, mncc);
762 case MNCC_RTP_CONNECT:
763 pgsm_bs->rtp_connect_ind(msg_type, callref, mncc);
766 case MNCC_START_DTMF_IND:
767 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
770 case MNCC_STOP_DTMF_IND:
771 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
774 case MNCC_CALL_CONF_IND:
775 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
779 pgsm_bs->alert_ind(msg_type, callref, mncc);
783 pgsm_bs->setup_cnf(msg_type, callref, mncc);
786 case MNCC_SETUP_COMPL_IND:
787 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
791 pgsm_bs->disc_ind(msg_type, callref, mncc);
797 pgsm_bs->rel_ind(msg_type, callref, mncc);
800 case MNCC_NOTIFY_IND:
801 pgsm_bs->notify_ind(msg_type, callref, mncc);
805 pgsm_bs->hold_ind(msg_type, callref, mncc);
808 case MNCC_RETRIEVE_IND:
809 pgsm_bs->retr_ind(msg_type, callref, mncc);
813 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: 0x%x\n", pgsm_bs->p_name, pgsm_bs->p_callerinfo.id, msg_type);
819 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
821 struct lcr_msg *message;
822 struct epoint_list *epointlist;
823 struct gsm_mncc *mncc;
824 struct interface *interface;
826 interface = getinterfacebyname(p_interface_name);
828 PERROR("Cannot find interface %s.\n", p_interface_name);
832 /* copy setup infos to port */
833 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
834 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
835 memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
836 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
838 /* no GSM MNCC connection */
839 if (p_g_lcr_gsm->mncc_lfd.fd < 0) {
840 gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
841 add_trace("failure", NULL, "No MNCC connection.");
843 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
844 message->param.disconnectinfo.cause = 41; // temp. failure.
845 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
846 message_put(message);
847 new_state(PORT_STATE_RELEASE);
848 trigger_work(&p_g_delete);
853 if (!p_dialinginfo.id[0]) {
854 gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
855 add_trace("failure", NULL, "No dialed subscriber given.");
857 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
858 message->param.disconnectinfo.cause = 28;
859 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
860 message_put(message);
861 new_state(PORT_STATE_RELEASE);
862 trigger_work(&p_g_delete);
866 /* unsupported codec for RTP bridge */
867 if (param->setup.rtpinfo.port) {
870 p_g_rtp_payloads = 0;
871 gsm_trace_header(p_interface_name, this, 1 /* codec negotioation */, DIRECTION_NONE);
872 for (i = 0; i < param->setup.rtpinfo.payloads; i++) {
873 switch (param->setup.rtpinfo.media_types[i]) {
875 case MEDIA_TYPE_GSM_EFR:
877 case MEDIA_TYPE_GSM_HR:
878 add_trace("rtp", "payload", "%s:%d supported", media_type2name(param->setup.rtpinfo.media_types[i]), param->setup.rtpinfo.payload_types[i]);
879 if (p_g_rtp_payloads < (int)sizeof(p_g_rtp_payload_types)) {
880 p_g_rtp_media_types[p_g_rtp_payloads] = param->setup.rtpinfo.media_types[i];
881 p_g_rtp_payload_types[p_g_rtp_payloads] = param->setup.rtpinfo.payload_types[i];
886 add_trace("rtp", "payload", "%s:%d unsupported", media_type2name(param->setup.rtpinfo.media_types[i]), param->setup.rtpinfo.payload_types[i]);
890 if (!p_g_rtp_payloads) {
891 gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
892 add_trace("failure", NULL, "No payload given that is supported by GSM");
894 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
895 message->param.disconnectinfo.cause = 65;
896 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
897 message_put(message);
898 new_state(PORT_STATE_RELEASE);
899 trigger_work(&p_g_delete);
904 // SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
905 /* screen outgoing caller id */
906 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_interface_name);
908 /* attach only if not already */
909 epointlist = p_epointlist;
911 if (epointlist->epoint_id == epoint_id)
913 epointlist = epointlist->next;
916 epointlist_new(epoint_id);
919 gsm_trace_header(p_interface_name, this, 0, DIRECTION_OUT);
920 p_g_callref = new_callref++;
921 add_trace("callref", "new", "0x%x", p_g_callref);
924 gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
925 mncc = create_mncc(MNCC_SETUP_REQ, p_g_callref);
926 /* caller information */
927 mncc->fields |= MNCC_F_CALLING;
928 mncc->calling.plan = 1;
929 switch (p_callerinfo.ntype) {
930 case INFO_NTYPE_UNKNOWN:
931 mncc->calling.type = 0x0;
933 case INFO_NTYPE_INTERNATIONAL:
934 mncc->calling.type = 0x1;
936 case INFO_NTYPE_NATIONAL:
937 mncc->calling.type = 0x2;
939 case INFO_NTYPE_SUBSCRIBER:
940 mncc->calling.type = 0x4;
942 default: /* INFO_NTYPE_NOTPRESENT */
943 mncc->fields &= ~MNCC_F_CALLING;
946 switch (p_callerinfo.screen) {
947 case INFO_SCREEN_USER:
948 mncc->calling.screen = 0;
950 default: /* INFO_SCREEN_NETWORK */
951 mncc->calling.screen = 3;
954 switch (p_callerinfo.present) {
955 case INFO_PRESENT_ALLOWED:
956 mncc->calling.present = 0;
958 case INFO_PRESENT_RESTRICTED:
959 mncc->calling.present = 1;
961 default: /* INFO_PRESENT_NOTAVAIL */
962 mncc->calling.present = 2;
965 if (mncc->fields & MNCC_F_CALLING) {
966 SCPY(mncc->calling.number, p_callerinfo.id);
967 add_trace("calling", "type", "%d", mncc->calling.type);
968 add_trace("calling", "plan", "%d", mncc->calling.plan);
969 add_trace("calling", "present", "%d", mncc->calling.present);
970 add_trace("calling", "screen", "%d", mncc->calling.screen);
971 add_trace("calling", "number", "%s", mncc->calling.number);
973 /* dialing information */
974 mncc->fields |= MNCC_F_CALLED;
975 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
976 SCPY(mncc->imsi, p_dialinginfo.id+5);
977 add_trace("dialing", "imsi", "%s", mncc->imsi);
979 SCPY(mncc->called.number, p_dialinginfo.id);
980 add_trace("dialing", "number", "%s", mncc->called.number);
983 /* sending user-user */
985 /* redirecting number */
986 mncc->fields |= MNCC_F_REDIRECTING;
987 mncc->redirecting.plan = 1;
988 switch (p_redirinfo.ntype) {
989 case INFO_NTYPE_UNKNOWN:
990 mncc->redirecting.type = 0x0;
992 case INFO_NTYPE_INTERNATIONAL:
993 mncc->redirecting.type = 0x1;
995 case INFO_NTYPE_NATIONAL:
996 mncc->redirecting.type = 0x2;
998 case INFO_NTYPE_SUBSCRIBER:
999 mncc->redirecting.type = 0x4;
1001 default: /* INFO_NTYPE_NOTPRESENT */
1002 mncc->fields &= ~MNCC_F_REDIRECTING;
1005 switch (p_redirinfo.screen) {
1006 case INFO_SCREEN_USER:
1007 mncc->redirecting.screen = 0;
1009 default: /* INFO_SCREE_NETWORK */
1010 mncc->redirecting.screen = 3;
1013 switch (p_redirinfo.present) {
1014 case INFO_PRESENT_ALLOWED:
1015 mncc->redirecting.present = 0;
1017 case INFO_PRESENT_RESTRICTED:
1018 mncc->redirecting.present = 1;
1020 default: /* INFO_PRESENT_NOTAVAIL */
1021 mncc->redirecting.present = 2;
1024 /* sending redirecting number only in ntmode */
1025 if (mncc->fields & MNCC_F_REDIRECTING) {
1026 SCPY(mncc->redirecting.number, p_redirinfo.id);
1027 add_trace("redir", "type", "%d", mncc->redirecting.type);
1028 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
1029 add_trace("redir", "present", "%d", mncc->redirecting.present);
1030 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
1031 add_trace("redir", "number", "%s", mncc->redirecting.number);
1034 if (interface->gsm_bs_hr) {
1035 add_trace("lchan", "type", "TCH/H or TCH/F");
1036 mncc->lchan_type = GSM_LCHAN_TCH_H;
1038 add_trace("lchan", "type", "TCH/F");
1039 mncc->lchan_type = GSM_LCHAN_TCH_F;
1043 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
1045 new_state(PORT_STATE_OUT_SETUP);
1047 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1048 message_put(message);
1051 if (param->setup.rtpinfo.port) {
1053 p_g_rtp_ip_remote = param->setup.rtpinfo.ip;
1054 p_g_rtp_port_remote = param->setup.rtpinfo.port;
1060 * endpoint sends messages to the port
1062 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1064 if (Pgsm::message_epoint(epoint_id, message_id, param))
1067 switch(message_id) {
1068 case MESSAGE_SETUP: /* dial-out command received from epoint */
1069 if (p_state!=PORT_STATE_IDLE)
1071 message_setup(epoint_id, message_id, param);
1075 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1081 int gsm_bs_exit(int rc)
1083 /* free gsm instance */
1085 if (gsm_bs->mncc_lfd.fd > -1) {
1086 close(gsm_bs->mncc_lfd.fd);
1087 unregister_fd(&gsm_bs->mncc_lfd);
1090 del_timer(&gsm_bs->socket_retry);
1099 int gsm_bs_init(struct interface *interface)
1101 /* create gsm instance */
1102 gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1104 SCPY(gsm_bs->interface_name, interface->name);
1105 gsm_bs->type = LCR_GSM_TYPE_NETWORK;
1106 gsm_bs->sun.sun_family = AF_UNIX;
1107 SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
1109 memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
1110 add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
1112 /* do the initial connect */
1113 mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);