1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
7 ** MNCC-Interface: Harald Welte **
11 \*****************************************************************************/
20 #include "gsm_audio.h"
25 #define SOCKET_RETRY_TIMER 5
27 //struct lcr_gsm *gsm = NULL;
31 /* names of MNCC-SAP */
32 static const struct _value_string {
36 { 0, "New call ref" },
37 { 1, "Codec negotiation" },
38 { MNCC_SETUP_REQ, "MNCC_SETUP_REQ" },
39 { MNCC_SETUP_IND, "MNCC_SETUP_IND" },
40 { MNCC_SETUP_RSP, "MNCC_SETUP_RSP" },
41 { MNCC_SETUP_CNF, "MNCC_SETUP_CNF" },
42 { MNCC_SETUP_COMPL_REQ, "MNCC_SETUP_COMPL_REQ" },
43 { MNCC_SETUP_COMPL_IND, "MNCC_SETUP_COMPL_IND" },
44 { MNCC_CALL_CONF_IND, "MNCC_CALL_CONF_IND" },
45 { MNCC_CALL_PROC_REQ, "MNCC_CALL_PROC_REQ" },
46 { MNCC_PROGRESS_REQ, "MNCC_PROGRESS_REQ" },
47 { MNCC_ALERT_REQ, "MNCC_ALERT_REQ" },
48 { MNCC_ALERT_IND, "MNCC_ALERT_IND" },
49 { MNCC_NOTIFY_REQ, "MNCC_NOTIFY_REQ" },
50 { MNCC_NOTIFY_IND, "MNCC_NOTIFY_IND" },
51 { MNCC_DISC_REQ, "MNCC_DISC_REQ" },
52 { MNCC_DISC_IND, "MNCC_DISC_IND" },
53 { MNCC_REL_REQ, "MNCC_REL_REQ" },
54 { MNCC_REL_IND, "MNCC_REL_IND" },
55 { MNCC_REL_CNF, "MNCC_REL_CNF" },
56 { MNCC_FACILITY_REQ, "MNCC_FACILITY_REQ" },
57 { MNCC_FACILITY_IND, "MNCC_FACILITY_IND" },
58 { MNCC_START_DTMF_IND, "MNCC_START_DTMF_IND" },
59 { MNCC_START_DTMF_RSP, "MNCC_START_DTMF_RSP" },
60 { MNCC_START_DTMF_REJ, "MNCC_START_DTMF_REJ" },
61 { MNCC_STOP_DTMF_IND, "MNCC_STOP_DTMF_IND" },
62 { MNCC_STOP_DTMF_RSP, "MNCC_STOP_DTMF_RSP" },
63 { MNCC_MODIFY_REQ, "MNCC_MODIFY_REQ" },
64 { MNCC_MODIFY_IND, "MNCC_MODIFY_IND" },
65 { MNCC_MODIFY_RSP, "MNCC_MODIFY_RSP" },
66 { MNCC_MODIFY_CNF, "MNCC_MODIFY_CNF" },
67 { MNCC_MODIFY_REJ, "MNCC_MODIFY_REJ" },
68 { MNCC_HOLD_IND, "MNCC_HOLD_IND" },
69 { MNCC_HOLD_CNF, "MNCC_HOLD_CNF" },
70 { MNCC_HOLD_REJ, "MNCC_HOLD_REJ" },
71 { MNCC_RETRIEVE_IND, "MNCC_RETRIEVE_IND" },
72 { MNCC_RETRIEVE_CNF, "MNCC_RETRIEVE_CNF" },
73 { MNCC_RETRIEVE_REJ, "MNCC_RETRIEVE_REJ" },
74 { MNCC_USERINFO_REQ, "MNCC_USERINFO_REQ" },
75 { MNCC_USERINFO_IND, "MNCC_USERINFO_IND" },
76 { MNCC_REJ_REQ, "MNCC_REJ_REQ" },
77 { MNCC_REJ_IND, "MNCC_REJ_IND" },
78 { MNCC_PROGRESS_IND, "MNCC_PROGRESS_IND" },
79 { MNCC_CALL_PROC_IND, "MNCC_CALL_PROC_IND" },
80 { MNCC_CALL_CONF_REQ, "MNCC_CALL_CONF_REQ" },
81 { MNCC_START_DTMF_REQ, "MNCC_START_DTMF_REQ" },
82 { MNCC_STOP_DTMF_REQ, "MNCC_STOP_DTMF_REQ" },
83 { MNCC_HOLD_REQ, "MNCC_HOLD_REQ " },
84 { MNCC_RETRIEVE_REQ, "MNCC_RETRIEVE_REQ" },
85 { MNCC_LCHAN_MODIFY, "MNCC_LCHAN_MODIFY" },
89 const char *mncc_name(int value)
93 while (mncc_names[i].name) {
94 if (mncc_names[i].msg_type == value)
95 return mncc_names[i].name;
101 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data);
104 * create and send mncc message
106 struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
108 struct gsm_mncc *mncc;
110 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
111 mncc->msg_type = msg_type;
112 mncc->callref = callref;
115 int send_and_free_mncc(struct lcr_gsm *lcr_gsm, unsigned int msg_type, void *data)
120 ret = mncc_send(lcr_gsm, msg_type, data);
127 void Pgsm::send_mncc_rtp_connect(void)
129 struct gsm_mncc_rtp *nrtp;
131 nrtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
132 nrtp->ip = p_g_rtp_ip_remote;
133 nrtp->port = p_g_rtp_port_remote;
134 switch (p_g_media_type) {
136 nrtp->payload_msg_type = GSM_TCHF_FRAME;
138 case MEDIA_TYPE_GSM_EFR:
139 nrtp->payload_msg_type = GSM_TCHF_FRAME_EFR;
142 nrtp->payload_msg_type = GSM_TCH_FRAME_AMR;
144 case MEDIA_TYPE_GSM_HR:
145 nrtp->payload_msg_type = GSM_TCHH_FRAME;
148 nrtp->payload_type = p_g_payload_type;
149 PDEBUG(DEBUG_GSM, "sending MNCC RTP connect with payload_msg_type=%x, payload_type=%d\n", nrtp->payload_msg_type, nrtp->payload_type);
150 send_and_free_mncc(p_g_lcr_gsm, nrtp->msg_type, nrtp);
153 static int delete_event(struct lcr_work *work, void *instance, int index);
158 Pgsm::Pgsm(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
161 if (interface->is_tones == IS_YES)
164 if (interface->is_earlyb == IS_YES)
167 if (interface->rtp_bridge)
169 p_g_rtp_payloads = 0;
170 memset(&p_g_samples, 0, sizeof(p_g_samples));
171 p_callerinfo.itype = (interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
172 memset(&p_g_delete, 0, sizeof(p_g_delete));
173 add_work(&p_g_delete, delete_event, this, 0);
178 p_g_gsm_b_index = -1;
179 p_g_gsm_b_active = 0;
180 p_g_notify_pending = NULL;
181 p_g_setup_pending = NULL;
182 p_g_connect_pending = NULL;
183 p_g_fr_decoder = NULL;
184 p_g_fr_encoder = NULL;
185 p_g_hr_decoder = NULL;
186 p_g_hr_encoder = NULL;
187 p_g_amr_decoder = NULL;
188 p_g_amr_encoder = NULL;
190 p_g_fr_decoder = gsm_fr_create();
191 p_g_fr_encoder = gsm_fr_create();
192 if (!p_g_fr_encoder || !p_g_fr_decoder) {
193 PERROR("Failed to create GSM FR codec instance\n");
194 trigger_work(&p_g_delete);
198 p_g_amr_decoder = gsm_amr_create();
199 p_g_amr_encoder = gsm_amr_create();
200 if (!p_g_amr_encoder || !p_g_amr_decoder) {
201 PERROR("Failed to create GSM AMR codec instance\n");
202 trigger_work(&p_g_delete);
206 p_g_tch_connected = 0;
209 PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
217 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
219 del_work(&p_g_delete);
221 /* remove queued message */
222 if (p_g_notify_pending)
223 message_free(p_g_notify_pending);
224 if (p_g_setup_pending)
225 message_free(p_g_setup_pending);
226 if (p_g_connect_pending)
227 message_free(p_g_connect_pending);
232 gsm_fr_destroy(p_g_fr_encoder);
234 gsm_fr_destroy(p_g_fr_decoder);
239 gsm_amr_destroy(p_g_amr_encoder);
241 gsm_amr_destroy(p_g_amr_decoder);
246 /* receive encoded frame from gsm */
247 void Pgsm::frame_receive(void *arg)
249 struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
250 unsigned char data[160];
256 switch (frame->msg_type) {
258 if (p_g_media_type != MEDIA_TYPE_GSM) {
259 PERROR("FR frame, but current media type mismatches.\n");
262 if (!p_g_fr_decoder) {
263 PERROR("FR frame, but decoder not created.\n");
266 if ((frame->data[0]>>4) != 0xd) {
267 PDEBUG(DEBUG_GSM, "received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
272 gsm_fr_decode(p_g_fr_decoder, frame->data, p_g_samples);
273 for (i = 0; i < 160; i++) {
274 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
278 case GSM_TCHF_FRAME_EFR:
279 if (p_g_media_type != MEDIA_TYPE_GSM_EFR) {
280 PERROR("EFR frame, but current media type mismatches.\n");
283 if (!p_g_amr_decoder) {
284 PERROR("EFR frame, but decoder not created.\n");
287 if ((frame->data[0]>>4) != 0xc)
291 gsm_efr_decode(p_g_amr_decoder, frame->data, p_g_samples);
292 for (i = 0; i < 160; i++) {
293 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
301 /* beep on bad frame */
302 for (i = 0; i < 160; i++) {
304 p_g_samples[i] = 15000;
306 p_g_samples[i] = -15000;
307 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
310 /* repeat on bad frame */
311 for (i = 0; i < 160; i++) {
312 p_g_samples[i] = (p_g_samples[i] * 14) >> 4;
313 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
320 record(data, 160, 0); // from down
322 tap(data, 160, 0); // from down
326 bridge_rx(data, 160);
329 bridge_tx(data, 160);
332 /* send traffic to gsm */
333 int Pgsm::bridge_rx(unsigned char *data, int len)
338 return audio_send(data, len);
341 int Pgsm::audio_send(unsigned char *data, int len)
343 unsigned char frame[33];
347 record(data, len, 1); // from up
349 tap(data, len, 1); // from up
351 /* encoder init failed */
355 /* (currently) not connected, so don't flood tch! */
356 if (!p_g_tch_connected)
359 /* write to rx buffer */
361 p_g_rxdata[p_g_rxpos++] = audio_law_to_s32[*data++];
362 if (p_g_rxpos != 160)
366 switch (p_g_media_type) {
368 if (!p_g_fr_encoder) {
369 PERROR("FR frame, but encoder not created.\n");
374 gsm_fr_encode(p_g_fr_encoder, p_g_rxdata, frame);
375 frame_send(frame, 33, GSM_TCHF_FRAME);
378 case MEDIA_TYPE_GSM_EFR:
379 if (!p_g_amr_encoder) {
380 PERROR("EFR frame, but encoder not created.\n");
385 gsm_efr_encode(p_g_amr_encoder, p_g_rxdata, frame);
386 frame_send(frame, 31, GSM_TCHF_FRAME_EFR);
395 void Pgsm::frame_send(void *_frame, int len, int msg_type)
397 unsigned char buffer[sizeof(struct gsm_data_frame) + len];
398 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
400 frame->msg_type = msg_type;
401 frame->callref = p_g_callref;
402 memcpy(frame->data, _frame, len);
405 mncc_send(p_g_lcr_gsm, frame->msg_type, frame);
412 void gsm_trace_header(const char *interface_name, class Pgsm *port, unsigned int msg_type, int direction)
415 struct interface *interface = interface_first;
417 interface = getinterfacebyname(interface_name);
421 /* select message and primitive text */
422 SCPY(msgtext, mncc_name(msg_type));
426 switch(port->p_type) {
427 case PORT_TYPE_GSM_BS_OUT:
428 case PORT_TYPE_GSM_BS_IN:
429 SCAT(msgtext, " LCR<->BSC");
431 case PORT_TYPE_GSM_MS_OUT:
432 case PORT_TYPE_GSM_MS_IN:
433 SCAT(msgtext, " LCR<->MS");
437 SCAT(msgtext, " ----");
439 /* init trace with given values */
442 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
443 port?port->p_dialinginfo.id:NULL,
446 port?port->p_serial:0,
450 /* modify lchan to given payload type */
451 void Pgsm::modify_lchan(int media_type)
453 struct gsm_mncc *mode;
455 /* already modified to that media type */
456 if (p_g_media_type == media_type)
459 p_g_media_type = media_type;
460 gsm_trace_header(p_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
461 mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
462 switch (media_type) {
463 case MEDIA_TYPE_GSM_EFR:
464 add_trace("speech", "version", "EFR given");
465 mode->lchan_mode = 0x21; /* GSM V2 */
468 add_trace("speech", "version", "AMR given");
469 mode->lchan_mode = 0x41; /* GSM V3 */
472 add_trace("speech", "version", "Full/Half Rate given");
473 mode->lchan_mode = 0x01; /* GSM V1 */
475 mode->lchan_type = 0x02; /* FIXME: unused */
476 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
478 send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
481 /* CALL PROCEEDING INDICATION (from network) */
482 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
484 struct lcr_msg *message;
485 struct gsm_mncc *frame;
487 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
490 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
491 message_put(message);
493 new_state(PORT_STATE_OUT_PROCEEDING);
495 if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
496 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
498 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
499 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
500 p_g_tch_connected = 1;
503 /* modify to GSM FR (this is GSM user side only, so there is FR supported only) */
504 modify_lchan(MEDIA_TYPE_GSM);
507 /* ALERTING INDICATION */
508 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
510 struct lcr_msg *message;
511 struct gsm_mncc *frame;
513 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
516 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
517 message_put(message);
519 new_state(PORT_STATE_OUT_ALERTING);
521 if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
522 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
524 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
525 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
526 p_g_tch_connected = 1;
529 /* modify to GSM FR, if not already */
530 if (!p_g_media_type) {
531 modify_lchan(MEDIA_TYPE_GSM);
535 /* CONNECT INDICATION */
536 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
538 struct gsm_mncc *resp, *frame;
539 struct lcr_msg *message;
541 SCPY(p_connectinfo.id, mncc->connected.number);
542 SCPY(p_connectinfo.imsi, mncc->imsi);
543 p_connectinfo.present = INFO_PRESENT_ALLOWED;
544 p_connectinfo.screen = INFO_SCREEN_NETWORK;
545 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
546 SCPY(p_connectinfo.interface, p_interface_name);
548 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
549 if (p_connectinfo.id[0])
550 add_trace("connect", "number", "%s", p_connectinfo.id);
551 else if (mncc->imsi[0])
552 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
554 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
558 gsm_trace_header(p_interface_name, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
559 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_g_callref);
561 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
563 new_state(PORT_STATE_CONNECT);
565 if (!p_g_tch_connected) { /* only if ... */
566 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
568 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
569 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
570 p_g_tch_connected = 1;
573 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
574 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
576 /* if we have a bridge, but not yet modified, the phone accepts out requested payload.
577 * we force the first in list */
578 if (p_g_rtp_bridge) {
579 if (!p_g_media_type) {
580 /* modify to first given type */
581 modify_lchan(p_g_rtp_media_types[0]);
582 /* also set payload type */
583 p_g_payload_type = p_g_rtp_payload_types[0];
585 message->param.connectinfo.rtpinfo.media_types[0] = p_g_media_type;
586 message->param.connectinfo.rtpinfo.payload_types[0] = p_g_payload_type;
587 message->param.connectinfo.rtpinfo.payloads = 1;
589 /* modify to GSM FR, if not already
590 * for network side, this should have been already happened */
592 modify_lchan(MEDIA_TYPE_GSM);
596 if (p_g_rtp_bridge) {
597 struct gsm_mncc_rtp *rtp;
599 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding connect msg\n");
600 p_g_connect_pending = message;
601 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
602 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
604 message_put(message);
607 /* CONNECT ACK INDICATION */
608 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
610 struct gsm_mncc *frame;
612 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
615 new_state(PORT_STATE_CONNECT);
617 if (!p_g_tch_connected) { /* only if ... */
618 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
620 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
621 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
622 p_g_tch_connected = 1;
626 /* DISCONNECT INDICATION */
627 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
629 struct lcr_msg *message;
630 int cause = 16, location = 0;
631 struct gsm_mncc *resp;
633 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
634 if (mncc->fields & MNCC_F_CAUSE) {
635 location = mncc->cause.location;
636 cause = mncc->cause.value;
637 add_trace("cause", "coding", "%d", mncc->cause.coding);
638 add_trace("cause", "location", "%d", location);
639 add_trace("cause", "value", "%d", cause);
644 resp = create_mncc(MNCC_REL_REQ, p_g_callref);
645 gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
647 resp->fields |= MNCC_F_CAUSE;
648 resp->cause.coding = 3;
649 resp->cause.location = 1;
650 resp->cause.value = cause;
651 add_trace("cause", "coding", "%d", resp->cause.coding);
652 add_trace("cause", "location", "%d", resp->cause.location);
653 add_trace("cause", "value", "%d", resp->cause.value);
656 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
658 /* sending release to endpoint */
659 while(p_epointlist) {
660 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
661 message->param.disconnectinfo.cause = cause;
662 message->param.disconnectinfo.location = location;
663 message_put(message);
665 free_epointlist(p_epointlist);
667 new_state(PORT_STATE_RELEASE);
668 trigger_work(&p_g_delete);
671 /* CC_RELEASE INDICATION */
672 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
674 int location = 0, cause = 16;
675 struct lcr_msg *message;
677 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
678 if (mncc->fields & MNCC_F_CAUSE) {
679 location = mncc->cause.location;
680 cause = mncc->cause.value;
681 add_trace("cause", "coding", "%d", mncc->cause.coding);
682 add_trace("cause", "location", "%d", mncc->cause.location);
683 add_trace("cause", "value", "%d", mncc->cause.value);
687 /* sending release to endpoint */
688 while(p_epointlist) {
689 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
690 message->param.disconnectinfo.cause = cause;
691 message->param.disconnectinfo.location = location;
692 message_put(message);
694 free_epointlist(p_epointlist);
696 new_state(PORT_STATE_RELEASE);
697 trigger_work(&p_g_delete);
700 /* NOTIFY INDICATION */
701 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
703 struct lcr_msg *message;
705 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
706 add_trace("notify", NULL, "%d", mncc->notify);
709 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
710 message->param.notifyinfo.notify = mncc->notify;
711 message_put(message);
715 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
717 struct gsm_mncc *mncc;
720 // printf("if = %d\n", param->notifyinfo.notify);
721 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
722 notify = param->notifyinfo.notify & 0x7f;
723 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
724 /* queue notification */
725 if (p_g_notify_pending)
726 message_free(p_g_notify_pending);
727 p_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
728 memcpy(&p_g_notify_pending->param, param, sizeof(union parameter));
730 /* sending notification */
731 gsm_trace_header(p_interface_name, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
732 add_trace("notify", NULL, "%d", notify);
734 mncc = create_mncc(MNCC_NOTIFY_REQ, p_g_callref);
735 mncc->notify = notify;
736 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
741 /* RTP create indication */
742 void Pgsm::rtp_create_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
744 struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
746 /* send queued setup, as we received remote RTP info */
747 if (p_g_setup_pending) {
748 struct lcr_msg *message;
750 message = p_g_setup_pending;
751 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding setup\n", rtp->ip, rtp->port);
752 message->param.setup.rtpinfo.ip = rtp->ip;
753 message->param.setup.rtpinfo.port = rtp->port;
754 message_put(message);
755 p_g_setup_pending = NULL;
757 if (p_g_connect_pending) {
758 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) connecting RTP... \n", rtp->ip, rtp->port);
759 send_mncc_rtp_connect();
763 /* RTP connect indication */
764 void Pgsm::rtp_connect_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
766 struct lcr_msg *message;
767 struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
769 if (p_g_connect_pending) {
770 message = p_g_connect_pending;
771 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding connect\n", rtp->ip, rtp->port);
772 message->param.connectinfo.rtpinfo.ip = rtp->ip;
773 message->param.connectinfo.rtpinfo.port = rtp->port;
774 message_put(message);
775 p_g_connect_pending = NULL;
779 /* MESSAGE_PROGRESS */
780 void Pgsm::message_progress(unsigned int epoint_id, int message_id, union parameter *param)
782 if (param->progressinfo.progress == 8) {
783 PDEBUG(DEBUG_GSM, "Remote provides tones for us\n");
787 if (param->progressinfo.rtpinfo.port) {
788 PDEBUG(DEBUG_GSM, "PROGRESS with RTP peer info, sent to BSC (%08x,%d) with media %d, pt %d\n", param->progressinfo.rtpinfo.ip, param->progressinfo.rtpinfo.port, param->progressinfo.rtpinfo.media_types[0], param->progressinfo.rtpinfo.payload_types[0]);
790 /* modify channel to givne type, also sets media type */
791 modify_lchan(param->progressinfo.rtpinfo.media_types[0]);
794 p_g_rtp_ip_remote = param->progressinfo.rtpinfo.ip;
795 p_g_rtp_port_remote = param->progressinfo.rtpinfo.port;
796 /* p_g_media_type is already set by modify_lchan() */
797 p_g_payload_type = param->progressinfo.rtpinfo.payload_types[0];
798 send_mncc_rtp_connect();
802 /* MESSAGE_ALERTING */
803 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
805 struct gsm_mncc *mncc;
808 gsm_trace_header(p_interface_name, this, MNCC_ALERT_REQ, DIRECTION_OUT);
809 mncc = create_mncc(MNCC_ALERT_REQ, p_g_callref);
811 mncc->fields |= MNCC_F_PROGRESS;
812 mncc->progress.coding = 3; /* GSM */
813 mncc->progress.location = 1;
814 mncc->progress.descr = 8;
815 add_trace("progress", "coding", "%d", mncc->progress.coding);
816 add_trace("progress", "location", "%d", mncc->progress.location);
817 add_trace("progress", "descr", "%d", mncc->progress.descr);
820 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
822 new_state(PORT_STATE_IN_ALERTING);
824 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
825 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
827 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
828 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
829 p_g_tch_connected = 1;
833 /* MESSAGE_CONNECT */
834 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
836 struct gsm_mncc *mncc;
838 /* copy connected information */
839 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
840 /* screen outgoing caller id */
841 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_interface_name);
844 mncc = create_mncc(MNCC_SETUP_RSP, p_g_callref);
845 gsm_trace_header(p_interface_name, this, MNCC_SETUP_RSP, DIRECTION_OUT);
846 /* caller information */
847 mncc->fields |= MNCC_F_CONNECTED;
848 mncc->connected.plan = 1;
849 switch (p_callerinfo.ntype) {
850 case INFO_NTYPE_UNKNOWN:
851 mncc->connected.type = 0x0;
853 case INFO_NTYPE_INTERNATIONAL:
854 mncc->connected.type = 0x1;
856 case INFO_NTYPE_NATIONAL:
857 mncc->connected.type = 0x2;
859 case INFO_NTYPE_SUBSCRIBER:
860 mncc->connected.type = 0x4;
862 default: /* INFO_NTYPE_NOTPRESENT */
863 mncc->fields &= ~MNCC_F_CONNECTED;
866 switch (p_callerinfo.screen) {
867 case INFO_SCREEN_USER:
868 mncc->connected.screen = 0;
870 default: /* INFO_SCREEN_NETWORK */
871 mncc->connected.screen = 3;
874 switch (p_callerinfo.present) {
875 case INFO_PRESENT_ALLOWED:
876 mncc->connected.present = 0;
878 case INFO_PRESENT_RESTRICTED:
879 mncc->connected.present = 1;
881 default: /* INFO_PRESENT_NOTAVAIL */
882 mncc->connected.present = 2;
885 if (mncc->fields & MNCC_F_CONNECTED) {
886 SCPY(mncc->connected.number, p_connectinfo.id);
887 add_trace("connected", "type", "%d", mncc->connected.type);
888 add_trace("connected", "plan", "%d", mncc->connected.plan);
889 add_trace("connected", "present", "%d", mncc->connected.present);
890 add_trace("connected", "screen", "%d", mncc->connected.screen);
891 add_trace("connected", "number", "%s", mncc->connected.number);
894 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
896 new_state(PORT_STATE_CONNECT_WAITING);
898 if (param->connectinfo.rtpinfo.port) {
899 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->connectinfo.rtpinfo.ip, param->connectinfo.rtpinfo.port);
901 /* modify channel to givne type, also sets media type */
902 modify_lchan(param->connectinfo.rtpinfo.media_types[0]);
905 p_g_rtp_ip_remote = param->connectinfo.rtpinfo.ip;
906 p_g_rtp_port_remote = param->connectinfo.rtpinfo.port;
907 /* p_g_media_type is already set by modify_lchan() */
908 p_g_payload_type = param->connectinfo.rtpinfo.payload_types[0];
909 send_mncc_rtp_connect();
913 /* MESSAGE_DISCONNECT */
914 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
916 struct gsm_mncc *mncc;
918 /* send disconnect */
919 mncc = create_mncc(MNCC_DISC_REQ, p_g_callref);
920 gsm_trace_header(p_interface_name, this, MNCC_DISC_REQ, DIRECTION_OUT);
922 mncc->fields |= MNCC_F_PROGRESS;
923 mncc->progress.coding = 3; /* GSM */
924 mncc->progress.location = 1;
925 mncc->progress.descr = 8;
926 add_trace("progress", "coding", "%d", mncc->progress.coding);
927 add_trace("progress", "location", "%d", mncc->progress.location);
928 add_trace("progress", "descr", "%d", mncc->progress.descr);
930 mncc->fields |= MNCC_F_CAUSE;
931 mncc->cause.coding = 3;
932 mncc->cause.location = param->disconnectinfo.location;
933 mncc->cause.value = param->disconnectinfo.cause;
934 add_trace("cause", "coding", "%d", mncc->cause.coding);
935 add_trace("cause", "location", "%d", mncc->cause.location);
936 add_trace("cause", "value", "%d", mncc->cause.value);
938 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
940 new_state(PORT_STATE_OUT_DISCONNECT);
942 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
943 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
945 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
946 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
947 p_g_tch_connected = 1;
952 /* MESSAGE_RELEASE */
953 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
955 struct gsm_mncc *mncc;
958 mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
959 gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
960 mncc->fields |= MNCC_F_CAUSE;
961 mncc->cause.coding = 3;
962 mncc->cause.location = param->disconnectinfo.location;
963 mncc->cause.value = param->disconnectinfo.cause;
964 add_trace("cause", "coding", "%d", mncc->cause.coding);
965 add_trace("cause", "location", "%d", mncc->cause.location);
966 add_trace("cause", "value", "%d", mncc->cause.value);
968 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
970 new_state(PORT_STATE_RELEASE);
971 trigger_work(&p_g_delete);
976 * endpoint sends messages to the port
978 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
982 if (Port::message_epoint(epoint_id, message_id, param))
986 case MESSAGE_NOTIFY: /* display and notifications */
988 message_notify(epoint_id, message_id, param);
991 // case MESSAGE_FACILITY: /* facility message */
992 // message_facility(epoint_id, message_id, param);
995 case MESSAGE_PROCEEDING: /* message not handles */
999 case MESSAGE_PROGRESS:
1001 message_progress(epoint_id, message_id, param);
1004 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1006 if (p_state!=PORT_STATE_IN_PROCEEDING)
1008 message_alerting(epoint_id, message_id, param);
1009 if (p_g_notify_pending) {
1010 /* send pending notify message during connect */
1011 message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
1012 message_free(p_g_notify_pending);
1013 p_g_notify_pending = NULL;
1017 case MESSAGE_CONNECT: /* call of endpoint is connected */
1019 if (p_state!=PORT_STATE_IN_PROCEEDING
1020 && p_state!=PORT_STATE_IN_ALERTING)
1022 message_connect(epoint_id, message_id, param);
1023 if (p_g_notify_pending) {
1024 /* send pending notify message during connect */
1025 message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
1026 message_free(p_g_notify_pending);
1027 p_g_notify_pending = NULL;
1031 case MESSAGE_DISCONNECT: /* call has been disconnected */
1033 if (p_state!=PORT_STATE_IN_PROCEEDING
1034 && p_state!=PORT_STATE_IN_ALERTING
1035 && p_state!=PORT_STATE_OUT_SETUP
1036 && p_state!=PORT_STATE_OUT_OVERLAP
1037 && p_state!=PORT_STATE_OUT_PROCEEDING
1038 && p_state!=PORT_STATE_OUT_ALERTING
1039 && p_state!=PORT_STATE_CONNECT
1040 && p_state!=PORT_STATE_CONNECT_WAITING)
1042 message_disconnect(epoint_id, message_id, param);
1045 case MESSAGE_RELEASE: /* release isdn port */
1047 if (p_state==PORT_STATE_RELEASE)
1049 message_release(epoint_id, message_id, param);
1057 /* deletes only if l3id is release, otherwhise it will be triggered then */
1058 static int delete_event(struct lcr_work *work, void *instance, int index)
1060 class Pgsm *gsmport = (class Pgsm *)instance;
1067 int gsm_exit(int rc)
1084 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
1086 struct mncc_q_entry *qe;
1088 qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
1094 memcpy(qe->data, mncc, len);
1096 /* in case of empty list ... */
1097 if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
1098 /* the list head and tail both point to the new qe */
1099 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
1101 /* append to tail of list */
1102 lcr_gsm->mncc_q_tail->next = qe;
1103 lcr_gsm->mncc_q_tail = qe;
1106 lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
1111 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
1113 struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
1117 /* dequeue the successfully sent message */
1118 lcr_gsm->mncc_q_hd = qe->next;
1121 if (qe == lcr_gsm->mncc_q_tail)
1122 lcr_gsm->mncc_q_tail = NULL;
1127 /* routine called by LCR code if it wants to send a message to OpenBSC */
1128 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
1132 /* FIXME: the caller should provide this */
1134 case GSM_TCHF_FRAME:
1135 len = sizeof(struct gsm_data_frame) + 33;
1138 len = sizeof(struct gsm_mncc);
1142 return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
1145 /* close MNCC socket */
1146 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
1149 class Pgsm *pgsm = NULL;
1150 struct lcr_msg *message;
1152 PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
1157 /* free all the calls that were running through the MNCC interface */
1160 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_GSM) {
1161 pgsm = (class Pgsm *)port;
1162 if (pgsm->p_g_lcr_gsm == lcr_gsm) {
1163 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1164 message->param.disconnectinfo.cause = 27; // temp. unavail.
1165 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1166 message_put(message);
1167 pgsm->new_state(PORT_STATE_RELEASE);
1168 trigger_work(&pgsm->p_g_delete);
1174 /* flush the queue */
1175 while (mncc_q_dequeue(lcr_gsm))
1178 /* start the re-connect timer */
1179 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1184 /* write to OpenBSC via MNCC socket */
1185 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1187 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1188 struct mncc_q_entry *qe, *qe2;
1192 qe = lcr_gsm->mncc_q_hd;
1194 lfd->when &= ~LCR_FD_WRITE;
1197 rc = write(lfd->fd, qe->data, qe->len);
1199 return mncc_fd_close(lcr_gsm, lfd);
1202 if (rc < (int)qe->len)
1204 /* dequeue the successfully sent message */
1205 qe2 = mncc_q_dequeue(lcr_gsm);
1212 /* read from OpenBSC via MNCC socket */
1213 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1215 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1217 static char buf[sizeof(struct gsm_mncc)+1024];
1218 struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1220 memset(buf, 0, sizeof(buf));
1221 rc = recv(lfd->fd, buf, sizeof(buf), 0);
1223 return mncc_fd_close(lcr_gsm, lfd);
1227 /* Hand the MNCC message into LCR */
1228 switch (lcr_gsm->type) {
1230 case LCR_GSM_TYPE_NETWORK:
1231 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1234 case LCR_GSM_TYPE_MS:
1235 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1242 /* file descriptor callback if we can read or write form MNCC socket */
1243 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1247 if (what & LCR_FD_READ)
1248 rc = mncc_fd_read(lfd, inst, idx);
1252 if (what & LCR_FD_WRITE)
1253 rc = mncc_fd_write(lfd, inst, idx);
1258 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1260 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1263 lcr_gsm->mncc_lfd.fd = -1;
1265 fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1267 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1271 rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1272 sizeof(lcr_gsm->sun));
1274 PERROR("Could not connect to MNCC socket %s, "
1275 "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1276 SOCKET_RETRY_TIMER);
1278 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1280 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1281 lcr_gsm->mncc_lfd.fd = fd;
1282 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);