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_tch_connected = 0;
201 PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
209 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
211 del_work(&p_g_delete);
213 /* remove queued message */
214 if (p_g_notify_pending)
215 message_free(p_g_notify_pending);
216 if (p_g_setup_pending)
217 message_free(p_g_setup_pending);
218 if (p_g_connect_pending)
219 message_free(p_g_connect_pending);
224 gsm_fr_destroy(p_g_fr_encoder);
226 gsm_fr_destroy(p_g_fr_decoder);
231 /* receive encoded frame from gsm */
232 void Pgsm::frame_receive(void *arg)
234 struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
235 unsigned char data[160];
241 switch (frame->msg_type) {
243 if ((frame->data[0]>>4) != 0xd) {
244 PDEBUG(DEBUG_GSM, "received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
249 gsm_fr_decode(p_g_fr_decoder, frame->data, p_g_samples);
250 for (i = 0; i < 160; i++) {
251 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
259 /* beep on bad frame */
260 for (i = 0; i < 160; i++) {
262 p_g_samples[i] = 15000;
264 p_g_samples[i] = -15000;
265 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
268 /* repeat on bad frame */
269 for (i = 0; i < 160; i++) {
270 p_g_samples[i] = (p_g_samples[i] * 14) >> 4;
271 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
278 record(data, 160, 0); // from down
280 tap(data, 160, 0); // from down
284 bridge_rx(data, 160);
287 bridge_tx(data, 160);
290 /* send traffic to gsm */
291 int Pgsm::bridge_rx(unsigned char *data, int len)
296 return audio_send(data, len);
299 int Pgsm::audio_send(unsigned char *data, int len)
301 unsigned char frame[33];
305 record(data, len, 1); // from up
307 tap(data, len, 1); // from up
309 /* encoder init failed */
313 /* (currently) not connected, so don't flood tch! */
314 if (!p_g_tch_connected)
317 /* write to rx buffer */
319 p_g_rxdata[p_g_rxpos++] = audio_law_to_s32[*data++];
320 if (p_g_rxpos == 160) {
325 gsm_fr_encode(p_g_fr_encoder, p_g_rxdata, frame);
334 void Pgsm::frame_send(void *_frame)
336 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
337 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
339 frame->msg_type = GSM_TCHF_FRAME;
340 frame->callref = p_g_callref;
341 memcpy(frame->data, _frame, 33);
344 mncc_send(p_g_lcr_gsm, frame->msg_type, frame);
351 void gsm_trace_header(const char *interface_name, class Pgsm *port, unsigned int msg_type, int direction)
354 struct interface *interface = interface_first;
356 interface = getinterfacebyname(interface_name);
360 /* select message and primitive text */
361 SCPY(msgtext, mncc_name(msg_type));
365 switch(port->p_type) {
366 case PORT_TYPE_GSM_BS_OUT:
367 case PORT_TYPE_GSM_BS_IN:
368 SCAT(msgtext, " LCR<->BSC");
370 case PORT_TYPE_GSM_MS_OUT:
371 case PORT_TYPE_GSM_MS_IN:
372 SCAT(msgtext, " LCR<->MS");
376 SCAT(msgtext, " ----");
378 /* init trace with given values */
381 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
382 port?port->p_dialinginfo.id:NULL,
385 port?port->p_serial:0,
389 /* modify lchan to given payload type */
390 void Pgsm::modify_lchan(int media_type)
392 struct gsm_mncc *mode;
394 /* already modified to that payload type */
395 if (p_g_media_type == media_type)
398 p_g_media_type = media_type;
399 gsm_trace_header(p_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
400 mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
401 switch (media_type) {
402 case MEDIA_TYPE_GSM_EFR:
403 add_trace("speech", "version", "EFR given");
404 mode->lchan_mode = 0x21; /* GSM V2 */
407 add_trace("speech", "version", "AMR given");
408 mode->lchan_mode = 0x41; /* GSM V3 */
411 add_trace("speech", "version", "Full/Half Rate given");
412 mode->lchan_mode = 0x01; /* GSM V1 */
414 mode->lchan_type = 0x02; /* FIXME: unused */
415 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
417 send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
420 /* CALL PROCEEDING INDICATION (from network) */
421 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
423 struct lcr_msg *message;
424 struct gsm_mncc *frame;
426 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
429 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
430 message_put(message);
432 new_state(PORT_STATE_OUT_PROCEEDING);
434 if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
435 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
437 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
438 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
439 p_g_tch_connected = 1;
443 /* ALERTING INDICATION */
444 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
446 struct lcr_msg *message;
447 struct gsm_mncc *frame;
449 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
452 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
453 message_put(message);
455 new_state(PORT_STATE_OUT_ALERTING);
457 if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
458 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
460 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
461 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
462 p_g_tch_connected = 1;
466 /* CONNECT INDICATION */
467 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
469 struct gsm_mncc *resp, *frame;
470 struct lcr_msg *message;
472 SCPY(p_connectinfo.id, mncc->connected.number);
473 SCPY(p_connectinfo.imsi, mncc->imsi);
474 p_connectinfo.present = INFO_PRESENT_ALLOWED;
475 p_connectinfo.screen = INFO_SCREEN_NETWORK;
476 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
477 SCPY(p_connectinfo.interface, p_interface_name);
479 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
480 if (p_connectinfo.id[0])
481 add_trace("connect", "number", "%s", p_connectinfo.id);
482 else if (mncc->imsi[0])
483 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
485 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
489 gsm_trace_header(p_interface_name, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
490 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_g_callref);
492 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
494 new_state(PORT_STATE_CONNECT);
496 if (!p_g_tch_connected) { /* only if ... */
497 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
499 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
500 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
501 p_g_tch_connected = 1;
504 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
505 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
507 /* if we have a bridge, but not yet modified, the phone accepts out requested payload.
508 * we force the first in list */
509 if (p_g_rtp_bridge) {
510 if (!p_g_media_type) {
511 /* modify to first given type */
512 modify_lchan(p_g_rtp_media_types[0]);
513 /* also set payload type */
514 p_g_payload_type = p_g_rtp_payload_types[0];
516 message->param.connectinfo.rtpinfo.media_types[0] = p_g_media_type;
517 message->param.connectinfo.rtpinfo.payload_types[0] = p_g_payload_type;
518 message->param.connectinfo.rtpinfo.payloads = 1;
521 if (p_g_rtp_bridge) {
522 struct gsm_mncc_rtp *rtp;
524 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding connect msg\n");
525 p_g_connect_pending = message;
526 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
527 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
529 message_put(message);
532 /* CONNECT ACK INDICATION */
533 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
535 struct gsm_mncc *frame;
537 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
540 new_state(PORT_STATE_CONNECT);
542 if (!p_g_tch_connected) { /* only if ... */
543 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
545 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
546 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
547 p_g_tch_connected = 1;
551 /* DISCONNECT INDICATION */
552 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
554 struct lcr_msg *message;
555 int cause = 16, location = 0;
556 struct gsm_mncc *resp;
558 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
559 if (mncc->fields & MNCC_F_CAUSE) {
560 location = mncc->cause.location;
561 cause = mncc->cause.value;
562 add_trace("cause", "coding", "%d", mncc->cause.coding);
563 add_trace("cause", "location", "%d", location);
564 add_trace("cause", "value", "%d", cause);
569 resp = create_mncc(MNCC_REL_REQ, p_g_callref);
570 gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
572 resp->fields |= MNCC_F_CAUSE;
573 resp->cause.coding = 3;
574 resp->cause.location = 1;
575 resp->cause.value = cause;
576 add_trace("cause", "coding", "%d", resp->cause.coding);
577 add_trace("cause", "location", "%d", resp->cause.location);
578 add_trace("cause", "value", "%d", resp->cause.value);
581 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
583 /* sending release to endpoint */
584 while(p_epointlist) {
585 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
586 message->param.disconnectinfo.cause = cause;
587 message->param.disconnectinfo.location = location;
588 message_put(message);
590 free_epointlist(p_epointlist);
592 new_state(PORT_STATE_RELEASE);
593 trigger_work(&p_g_delete);
596 /* CC_RELEASE INDICATION */
597 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
599 int location = 0, cause = 16;
600 struct lcr_msg *message;
602 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
603 if (mncc->fields & MNCC_F_CAUSE) {
604 location = mncc->cause.location;
605 cause = mncc->cause.value;
606 add_trace("cause", "coding", "%d", mncc->cause.coding);
607 add_trace("cause", "location", "%d", mncc->cause.location);
608 add_trace("cause", "value", "%d", mncc->cause.value);
612 /* sending release to endpoint */
613 while(p_epointlist) {
614 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
615 message->param.disconnectinfo.cause = cause;
616 message->param.disconnectinfo.location = location;
617 message_put(message);
619 free_epointlist(p_epointlist);
621 new_state(PORT_STATE_RELEASE);
622 trigger_work(&p_g_delete);
625 /* NOTIFY INDICATION */
626 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
628 struct lcr_msg *message;
630 gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
631 add_trace("notify", NULL, "%d", mncc->notify);
634 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
635 message->param.notifyinfo.notify = mncc->notify;
636 message_put(message);
640 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
642 struct gsm_mncc *mncc;
645 // printf("if = %d\n", param->notifyinfo.notify);
646 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
647 notify = param->notifyinfo.notify & 0x7f;
648 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
649 /* queue notification */
650 if (p_g_notify_pending)
651 message_free(p_g_notify_pending);
652 p_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
653 memcpy(&p_g_notify_pending->param, param, sizeof(union parameter));
655 /* sending notification */
656 gsm_trace_header(p_interface_name, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
657 add_trace("notify", NULL, "%d", notify);
659 mncc = create_mncc(MNCC_NOTIFY_REQ, p_g_callref);
660 mncc->notify = notify;
661 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
666 /* RTP create indication */
667 void Pgsm::rtp_create_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
669 struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
671 /* send queued setup, as we received remote RTP info */
672 if (p_g_setup_pending) {
673 struct lcr_msg *message;
675 message = p_g_setup_pending;
676 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding setup\n", rtp->ip, rtp->port);
677 message->param.setup.rtpinfo.ip = rtp->ip;
678 message->param.setup.rtpinfo.port = rtp->port;
679 message_put(message);
680 p_g_setup_pending = NULL;
682 if (p_g_connect_pending) {
683 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) connecting RTP... \n", rtp->ip, rtp->port);
684 send_mncc_rtp_connect();
688 /* RTP connect indication */
689 void Pgsm::rtp_connect_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
691 struct lcr_msg *message;
692 struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
694 if (p_g_connect_pending) {
695 message = p_g_connect_pending;
696 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding connect\n", rtp->ip, rtp->port);
697 message->param.connectinfo.rtpinfo.ip = rtp->ip;
698 message->param.connectinfo.rtpinfo.port = rtp->port;
699 message_put(message);
700 p_g_connect_pending = NULL;
704 /* MESSAGE_PROGRESS */
705 void Pgsm::message_progress(unsigned int epoint_id, int message_id, union parameter *param)
707 if (param->progressinfo.progress == 8) {
708 PDEBUG(DEBUG_GSM, "Remote provides tones for us\n");
712 if (param->progressinfo.rtpinfo.port) {
713 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]);
715 /* modify channel to givne type, also sets media type */
716 modify_lchan(param->progressinfo.rtpinfo.media_types[0]);
719 p_g_rtp_ip_remote = param->progressinfo.rtpinfo.ip;
720 p_g_rtp_port_remote = param->progressinfo.rtpinfo.port;
721 /* p_g_media_type is already set by modify_lchan() */
722 p_g_payload_type = param->progressinfo.rtpinfo.payload_types[0];
723 send_mncc_rtp_connect();
727 /* MESSAGE_ALERTING */
728 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
730 struct gsm_mncc *mncc;
733 gsm_trace_header(p_interface_name, this, MNCC_ALERT_REQ, DIRECTION_OUT);
734 mncc = create_mncc(MNCC_ALERT_REQ, p_g_callref);
736 mncc->fields |= MNCC_F_PROGRESS;
737 mncc->progress.coding = 3; /* GSM */
738 mncc->progress.location = 1;
739 mncc->progress.descr = 8;
740 add_trace("progress", "coding", "%d", mncc->progress.coding);
741 add_trace("progress", "location", "%d", mncc->progress.location);
742 add_trace("progress", "descr", "%d", mncc->progress.descr);
745 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
747 new_state(PORT_STATE_IN_ALERTING);
749 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
750 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
752 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
753 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
754 p_g_tch_connected = 1;
758 /* MESSAGE_CONNECT */
759 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
761 struct gsm_mncc *mncc;
763 /* copy connected information */
764 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
765 /* screen outgoing caller id */
766 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_interface_name);
769 mncc = create_mncc(MNCC_SETUP_RSP, p_g_callref);
770 gsm_trace_header(p_interface_name, this, MNCC_SETUP_RSP, DIRECTION_OUT);
771 /* caller information */
772 mncc->fields |= MNCC_F_CONNECTED;
773 mncc->connected.plan = 1;
774 switch (p_callerinfo.ntype) {
775 case INFO_NTYPE_UNKNOWN:
776 mncc->connected.type = 0x0;
778 case INFO_NTYPE_INTERNATIONAL:
779 mncc->connected.type = 0x1;
781 case INFO_NTYPE_NATIONAL:
782 mncc->connected.type = 0x2;
784 case INFO_NTYPE_SUBSCRIBER:
785 mncc->connected.type = 0x4;
787 default: /* INFO_NTYPE_NOTPRESENT */
788 mncc->fields &= ~MNCC_F_CONNECTED;
791 switch (p_callerinfo.screen) {
792 case INFO_SCREEN_USER:
793 mncc->connected.screen = 0;
795 default: /* INFO_SCREEN_NETWORK */
796 mncc->connected.screen = 3;
799 switch (p_callerinfo.present) {
800 case INFO_PRESENT_ALLOWED:
801 mncc->connected.present = 0;
803 case INFO_PRESENT_RESTRICTED:
804 mncc->connected.present = 1;
806 default: /* INFO_PRESENT_NOTAVAIL */
807 mncc->connected.present = 2;
810 if (mncc->fields & MNCC_F_CONNECTED) {
811 SCPY(mncc->connected.number, p_connectinfo.id);
812 add_trace("connected", "type", "%d", mncc->connected.type);
813 add_trace("connected", "plan", "%d", mncc->connected.plan);
814 add_trace("connected", "present", "%d", mncc->connected.present);
815 add_trace("connected", "screen", "%d", mncc->connected.screen);
816 add_trace("connected", "number", "%s", mncc->connected.number);
819 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
821 new_state(PORT_STATE_CONNECT_WAITING);
823 if (param->connectinfo.rtpinfo.port) {
824 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->connectinfo.rtpinfo.ip, param->connectinfo.rtpinfo.port);
826 /* modify channel to givne type, also sets media type */
827 modify_lchan(param->connectinfo.rtpinfo.media_types[0]);
830 p_g_rtp_ip_remote = param->connectinfo.rtpinfo.ip;
831 p_g_rtp_port_remote = param->connectinfo.rtpinfo.port;
832 /* p_g_media_type is already set by modify_lchan() */
833 p_g_payload_type = param->connectinfo.rtpinfo.payload_types[0];
834 send_mncc_rtp_connect();
838 /* MESSAGE_DISCONNECT */
839 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
841 struct gsm_mncc *mncc;
843 /* send disconnect */
844 mncc = create_mncc(MNCC_DISC_REQ, p_g_callref);
845 gsm_trace_header(p_interface_name, this, MNCC_DISC_REQ, DIRECTION_OUT);
847 mncc->fields |= MNCC_F_PROGRESS;
848 mncc->progress.coding = 3; /* GSM */
849 mncc->progress.location = 1;
850 mncc->progress.descr = 8;
851 add_trace("progress", "coding", "%d", mncc->progress.coding);
852 add_trace("progress", "location", "%d", mncc->progress.location);
853 add_trace("progress", "descr", "%d", mncc->progress.descr);
855 mncc->fields |= MNCC_F_CAUSE;
856 mncc->cause.coding = 3;
857 mncc->cause.location = param->disconnectinfo.location;
858 mncc->cause.value = param->disconnectinfo.cause;
859 add_trace("cause", "coding", "%d", mncc->cause.coding);
860 add_trace("cause", "location", "%d", mncc->cause.location);
861 add_trace("cause", "value", "%d", mncc->cause.value);
863 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
865 new_state(PORT_STATE_OUT_DISCONNECT);
867 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
868 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
870 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
871 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
872 p_g_tch_connected = 1;
877 /* MESSAGE_RELEASE */
878 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
880 struct gsm_mncc *mncc;
883 mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
884 gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
885 mncc->fields |= MNCC_F_CAUSE;
886 mncc->cause.coding = 3;
887 mncc->cause.location = param->disconnectinfo.location;
888 mncc->cause.value = param->disconnectinfo.cause;
889 add_trace("cause", "coding", "%d", mncc->cause.coding);
890 add_trace("cause", "location", "%d", mncc->cause.location);
891 add_trace("cause", "value", "%d", mncc->cause.value);
893 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
895 new_state(PORT_STATE_RELEASE);
896 trigger_work(&p_g_delete);
901 * endpoint sends messages to the port
903 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
907 if (Port::message_epoint(epoint_id, message_id, param))
911 case MESSAGE_NOTIFY: /* display and notifications */
913 message_notify(epoint_id, message_id, param);
916 // case MESSAGE_FACILITY: /* facility message */
917 // message_facility(epoint_id, message_id, param);
920 case MESSAGE_PROCEEDING: /* message not handles */
924 case MESSAGE_PROGRESS:
926 message_progress(epoint_id, message_id, param);
929 case MESSAGE_ALERTING: /* call of endpoint is ringing */
931 if (p_state!=PORT_STATE_IN_PROCEEDING)
933 message_alerting(epoint_id, message_id, param);
934 if (p_g_notify_pending) {
935 /* send pending notify message during connect */
936 message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
937 message_free(p_g_notify_pending);
938 p_g_notify_pending = NULL;
942 case MESSAGE_CONNECT: /* call of endpoint is connected */
944 if (p_state!=PORT_STATE_IN_PROCEEDING
945 && p_state!=PORT_STATE_IN_ALERTING)
947 message_connect(epoint_id, message_id, param);
948 if (p_g_notify_pending) {
949 /* send pending notify message during connect */
950 message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
951 message_free(p_g_notify_pending);
952 p_g_notify_pending = NULL;
956 case MESSAGE_DISCONNECT: /* call has been disconnected */
958 if (p_state!=PORT_STATE_IN_PROCEEDING
959 && p_state!=PORT_STATE_IN_ALERTING
960 && p_state!=PORT_STATE_OUT_SETUP
961 && p_state!=PORT_STATE_OUT_OVERLAP
962 && p_state!=PORT_STATE_OUT_PROCEEDING
963 && p_state!=PORT_STATE_OUT_ALERTING
964 && p_state!=PORT_STATE_CONNECT
965 && p_state!=PORT_STATE_CONNECT_WAITING)
967 message_disconnect(epoint_id, message_id, param);
970 case MESSAGE_RELEASE: /* release isdn port */
972 if (p_state==PORT_STATE_RELEASE)
974 message_release(epoint_id, message_id, param);
982 /* deletes only if l3id is release, otherwhise it will be triggered then */
983 static int delete_event(struct lcr_work *work, void *instance, int index)
985 class Pgsm *gsmport = (class Pgsm *)instance;
1009 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
1011 struct mncc_q_entry *qe;
1013 qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
1019 memcpy(qe->data, mncc, len);
1021 /* in case of empty list ... */
1022 if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
1023 /* the list head and tail both point to the new qe */
1024 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
1026 /* append to tail of list */
1027 lcr_gsm->mncc_q_tail->next = qe;
1028 lcr_gsm->mncc_q_tail = qe;
1031 lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
1036 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
1038 struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
1042 /* dequeue the successfully sent message */
1043 lcr_gsm->mncc_q_hd = qe->next;
1046 if (qe == lcr_gsm->mncc_q_tail)
1047 lcr_gsm->mncc_q_tail = NULL;
1052 /* routine called by LCR code if it wants to send a message to OpenBSC */
1053 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
1057 /* FIXME: the caller should provide this */
1059 case GSM_TCHF_FRAME:
1060 len = sizeof(struct gsm_data_frame) + 33;
1063 len = sizeof(struct gsm_mncc);
1067 return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
1070 /* close MNCC socket */
1071 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
1074 class Pgsm *pgsm = NULL;
1075 struct lcr_msg *message;
1077 PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
1082 /* free all the calls that were running through the MNCC interface */
1085 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_GSM) {
1086 pgsm = (class Pgsm *)port;
1087 if (pgsm->p_g_lcr_gsm == lcr_gsm) {
1088 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1089 message->param.disconnectinfo.cause = 27; // temp. unavail.
1090 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1091 message_put(message);
1092 pgsm->new_state(PORT_STATE_RELEASE);
1093 trigger_work(&pgsm->p_g_delete);
1099 /* flush the queue */
1100 while (mncc_q_dequeue(lcr_gsm))
1103 /* start the re-connect timer */
1104 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1109 /* write to OpenBSC via MNCC socket */
1110 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1112 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1113 struct mncc_q_entry *qe, *qe2;
1117 qe = lcr_gsm->mncc_q_hd;
1119 lfd->when &= ~LCR_FD_WRITE;
1122 rc = write(lfd->fd, qe->data, qe->len);
1124 return mncc_fd_close(lcr_gsm, lfd);
1127 if (rc < (int)qe->len)
1129 /* dequeue the successfully sent message */
1130 qe2 = mncc_q_dequeue(lcr_gsm);
1137 /* read from OpenBSC via MNCC socket */
1138 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1140 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1142 static char buf[sizeof(struct gsm_mncc)+1024];
1143 struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1145 memset(buf, 0, sizeof(buf));
1146 rc = recv(lfd->fd, buf, sizeof(buf), 0);
1148 return mncc_fd_close(lcr_gsm, lfd);
1152 /* Hand the MNCC message into LCR */
1153 switch (lcr_gsm->type) {
1155 case LCR_GSM_TYPE_NETWORK:
1156 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1159 case LCR_GSM_TYPE_MS:
1160 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1167 /* file descriptor callback if we can read or write form MNCC socket */
1168 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1172 if (what & LCR_FD_READ)
1173 rc = mncc_fd_read(lfd, inst, idx);
1177 if (what & LCR_FD_WRITE)
1178 rc = mncc_fd_write(lfd, inst, idx);
1183 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1185 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1188 lcr_gsm->mncc_lfd.fd = -1;
1190 fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1192 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1196 rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1197 sizeof(lcr_gsm->sun));
1199 PERROR("Could not connect to MNCC socket %s, "
1200 "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1201 SOCKET_RETRY_TIMER);
1203 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1205 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1206 lcr_gsm->mncc_lfd.fd = fd;
1207 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);