1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
7 ** MNCC-Interface: Harald Welte **
11 \*****************************************************************************/
20 #include "gsm_audio.h"
23 #include <mISDN/mISDNcompat.h>
26 #define SOCKET_RETRY_TIMER 5
28 //struct lcr_gsm *gsm = NULL;
32 /* names of MNCC-SAP */
33 static const struct _value_string {
37 { 0, "New call ref" },
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" },
88 const char *mncc_name(int value)
92 while (mncc_names[i].name) {
93 if (mncc_names[i].msg_type == value)
94 return mncc_names[i].name;
100 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data);
103 * create and send mncc message
105 struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
107 struct gsm_mncc *mncc;
109 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
110 mncc->msg_type = msg_type;
111 mncc->callref = callref;
114 int send_and_free_mncc(struct lcr_gsm *lcr_gsm, unsigned int msg_type, void *data)
119 ret = mncc_send(lcr_gsm, msg_type, data);
126 static int delete_event(struct lcr_work *work, void *instance, int index);
131 Pgsm::Pgsm(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings)
134 if (interface->is_tones == IS_YES)
137 if (interface->is_earlyb == IS_YES)
140 if (interface->rtp_bridge)
142 memset(&p_g_samples, 0, sizeof(p_g_samples));
143 SCPY(p_g_interface_name, interface->name);
144 p_callerinfo.itype = (interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
145 memset(&p_g_delete, 0, sizeof(p_g_delete));
146 add_work(&p_g_delete, delete_event, this, 0);
151 p_g_gsm_b_index = -1;
152 p_g_gsm_b_active = 0;
153 p_g_notify_pending = NULL;
154 p_g_setup_pending = NULL;
155 p_g_connect_pending = NULL;
156 p_g_decoder = gsm_audio_create();
157 p_g_encoder = gsm_audio_create();
158 if (!p_g_encoder || !p_g_decoder) {
159 PERROR("Failed to create GSM audio codec instance\n");
160 trigger_work(&p_g_delete);
163 p_g_tch_connected = 0;
165 PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
173 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
175 del_work(&p_g_delete);
177 /* remove queued message */
178 if (p_g_notify_pending)
179 message_free(p_g_notify_pending);
180 if (p_g_setup_pending)
181 message_free(p_g_setup_pending);
182 if (p_g_connect_pending)
183 message_free(p_g_connect_pending);
187 gsm_audio_destroy(p_g_encoder);
189 gsm_audio_destroy(p_g_decoder);
193 /* receive encoded frame from gsm */
194 void Pgsm::frame_receive(void *arg)
196 struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
197 unsigned char data[160];
203 if (frame->msg_type != GSM_TCHF_BAD_FRAME) {
204 if ((frame->data[0]>>4) != 0xd)
205 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
208 gsm_audio_decode(p_g_decoder, frame->data, p_g_samples);
209 for (i = 0; i < 160; i++) {
210 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
212 } else if (p_echotest) {
213 /* beep on bad frame */
214 for (i = 0; i < 160; i++) {
216 p_g_samples[i] = 15000;
218 p_g_samples[i] = -15000;
219 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
222 /* repeat on bad frame */
223 for (i = 0; i < 160; i++) {
224 p_g_samples[i] = (p_g_samples[i] * 14) >> 4;
225 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
231 bridge_rx(data, 160);
234 bridge_tx(data, 160);
237 /* send traffic to gsm */
238 int Pgsm::bridge_rx(unsigned char *data, int len)
240 unsigned char frame[33];
242 /* encoder init failed */
246 /* (currently) not connected, so don't flood tch! */
247 if (!p_g_tch_connected)
250 /* write to rx buffer */
252 p_g_rxdata[p_g_rxpos++] = audio_law_to_s32[*data++];
253 if (p_g_rxpos == 160) {
257 gsm_audio_encode(p_g_encoder, p_g_rxdata, frame);
265 void Pgsm::frame_send(void *_frame)
267 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
268 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
270 frame->msg_type = GSM_TCHF_FRAME;
271 frame->callref = p_g_callref;
272 memcpy(frame->data, _frame, 33);
275 mncc_send(p_g_lcr_gsm, frame->msg_type, frame);
282 void gsm_trace_header(const char *interface_name, class Pgsm *port, unsigned int msg_type, int direction)
285 struct interface *interface = interface_first;
287 interface = getinterfacebyname(interface_name);
291 /* select message and primitive text */
292 SCPY(msgtext, mncc_name(msg_type));
296 switch(port->p_type) {
297 case PORT_TYPE_GSM_BS_OUT:
298 case PORT_TYPE_GSM_BS_IN:
299 SCAT(msgtext, " LCR<->BSC");
301 case PORT_TYPE_GSM_MS_OUT:
302 case PORT_TYPE_GSM_MS_IN:
303 SCAT(msgtext, " LCR<->MS");
307 SCAT(msgtext, " ----");
309 /* init trace with given values */
312 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
313 port?port->p_dialinginfo.id:NULL,
316 port?port->p_serial:0,
320 /* PROCEEDING INDICATION */
321 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
323 struct gsm_mncc *mode;
325 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
326 if (mncc->fields & MNCC_F_CAUSE) {
327 add_trace("cause", "coding", "%d", mncc->cause.coding);
328 add_trace("cause", "location", "%", mncc->cause.location);
329 add_trace("cause", "value", "%", mncc->cause.value);
333 /* modify lchan to GSM codec V1 */
334 gsm_trace_header(p_g_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
335 mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
336 mode->lchan_mode = 0x01; /* GSM V1 */
337 mode->lchan_type = 0x02;
338 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
340 send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
344 /* CALL PROCEEDING INDICATION */
345 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
347 struct lcr_msg *message;
348 struct gsm_mncc *frame;
350 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
353 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
354 message_put(message);
356 new_state(PORT_STATE_OUT_PROCEEDING);
358 if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
359 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
361 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
362 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
363 p_g_tch_connected = 1;
367 /* ALERTING INDICATION */
368 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
370 struct lcr_msg *message;
371 struct gsm_mncc *frame;
373 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
376 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
377 message_put(message);
379 new_state(PORT_STATE_OUT_ALERTING);
381 if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
382 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
384 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
385 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
386 p_g_tch_connected = 1;
390 /* CONNECT INDICATION */
391 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
393 struct gsm_mncc *resp, *frame;
394 struct lcr_msg *message;
396 SCPY(p_connectinfo.id, mncc->connected.number);
397 SCPY(p_connectinfo.imsi, mncc->imsi);
398 p_connectinfo.present = INFO_PRESENT_ALLOWED;
399 p_connectinfo.screen = INFO_SCREEN_NETWORK;
400 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
401 SCPY(p_connectinfo.interface, p_g_interface_name);
403 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
404 if (p_connectinfo.id[0])
405 add_trace("connect", "number", "%s", p_connectinfo.id);
406 else if (mncc->imsi[0])
407 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
409 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
413 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
414 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_g_callref);
416 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
418 new_state(PORT_STATE_CONNECT);
420 if (!p_g_tch_connected) { /* only if ... */
421 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
423 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
424 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
425 p_g_tch_connected = 1;
428 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
429 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
430 message->param.connectinfo.rtpinfo.payload_type = 3; /* FIXME: receive payload type from peer */
432 if (p_g_rtp_bridge) {
433 struct gsm_mncc_rtp *rtp;
435 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding connect msg\n");
436 p_g_connect_pending = message;
437 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
438 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
440 message_put(message);
443 /* CONNECT ACK INDICATION */
444 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
446 struct gsm_mncc *frame;
448 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
451 new_state(PORT_STATE_CONNECT);
453 if (!p_g_tch_connected) { /* only if ... */
454 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
456 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
457 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
458 p_g_tch_connected = 1;
462 /* DISCONNECT INDICATION */
463 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
465 struct lcr_msg *message;
466 int cause = 16, location = 0;
467 struct gsm_mncc *resp;
469 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
470 if (mncc->fields & MNCC_F_CAUSE) {
471 location = mncc->cause.location;
472 cause = mncc->cause.value;
473 add_trace("cause", "coding", "%d", mncc->cause.coding);
474 add_trace("cause", "location", "%d", location);
475 add_trace("cause", "value", "%d", cause);
480 resp = create_mncc(MNCC_REL_REQ, p_g_callref);
481 gsm_trace_header(p_g_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
483 resp->fields |= MNCC_F_CAUSE;
484 resp->cause.coding = 3;
485 resp->cause.location = 1;
486 resp->cause.value = cause;
487 add_trace("cause", "coding", "%d", resp->cause.coding);
488 add_trace("cause", "location", "%d", resp->cause.location);
489 add_trace("cause", "value", "%d", resp->cause.value);
492 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
494 /* sending release to endpoint */
495 while(p_epointlist) {
496 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
497 message->param.disconnectinfo.cause = cause;
498 message->param.disconnectinfo.location = location;
499 message_put(message);
501 free_epointlist(p_epointlist);
503 new_state(PORT_STATE_RELEASE);
504 trigger_work(&p_g_delete);
507 /* CC_RELEASE INDICATION */
508 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
510 int location = 0, cause = 16;
511 struct lcr_msg *message;
513 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
514 if (mncc->fields & MNCC_F_CAUSE) {
515 location = mncc->cause.location;
516 cause = mncc->cause.value;
517 add_trace("cause", "coding", "%d", mncc->cause.coding);
518 add_trace("cause", "location", "%d", mncc->cause.location);
519 add_trace("cause", "value", "%d", mncc->cause.value);
523 /* sending release to endpoint */
524 while(p_epointlist) {
525 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
526 message->param.disconnectinfo.cause = cause;
527 message->param.disconnectinfo.location = location;
528 message_put(message);
530 free_epointlist(p_epointlist);
532 new_state(PORT_STATE_RELEASE);
533 trigger_work(&p_g_delete);
536 /* NOTIFY INDICATION */
537 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
539 struct lcr_msg *message;
541 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
542 add_trace("notify", NULL, "%d", mncc->notify);
545 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
546 message->param.notifyinfo.notify = mncc->notify;
547 message_put(message);
551 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
553 struct gsm_mncc *mncc;
556 // printf("if = %d\n", param->notifyinfo.notify);
557 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
558 notify = param->notifyinfo.notify & 0x7f;
559 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
560 /* queue notification */
561 if (p_g_notify_pending)
562 message_free(p_g_notify_pending);
563 p_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
564 memcpy(&p_g_notify_pending->param, param, sizeof(union parameter));
566 /* sending notification */
567 gsm_trace_header(p_g_interface_name, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
568 add_trace("notify", NULL, "%d", notify);
570 mncc = create_mncc(MNCC_NOTIFY_REQ, p_g_callref);
571 mncc->notify = notify;
572 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
577 /* RTP create indication */
578 void Pgsm::rtp_create_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
580 struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
582 /* send queued setup, as we received remote RTP info */
583 if (p_g_setup_pending) {
584 struct lcr_msg *message;
586 message = p_g_setup_pending;
587 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding setup\n", rtp->ip, rtp->port);
588 message->param.setup.rtpinfo.ip = rtp->ip;
589 message->param.setup.rtpinfo.port = rtp->port;
590 message_put(message);
591 p_g_setup_pending = NULL;
593 if (p_g_connect_pending) {
594 struct gsm_mncc_rtp *nrtp;
596 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) connecting RTP... \n", rtp->ip, rtp->port);
597 nrtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
598 nrtp->ip = p_g_rtp_ip_remote;
599 nrtp->port = p_g_rtp_port_remote;
600 send_and_free_mncc(p_g_lcr_gsm, nrtp->msg_type, nrtp);
604 /* RTP connect indication */
605 void Pgsm::rtp_connect_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
607 struct lcr_msg *message;
608 struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
610 if (p_g_connect_pending) {
611 message = p_g_connect_pending;
612 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding connect\n", rtp->ip, rtp->port);
613 message->param.connectinfo.rtpinfo.ip = rtp->ip;
614 message->param.connectinfo.rtpinfo.port = rtp->port;
615 message_put(message);
616 p_g_connect_pending = NULL;
620 /* MESSAGE_PROGRESS */
621 void Pgsm::message_progress(unsigned int epoint_id, int message_id, union parameter *param)
623 if (param->progressinfo.progress == 8) {
624 PDEBUG(DEBUG_GSM, "Remote provides tones for us\n");
628 if (param->progressinfo.rtpinfo.port) {
629 struct gsm_mncc_rtp *rtp;
631 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->progressinfo.rtpinfo.ip, param->progressinfo.rtpinfo.port);
632 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
633 rtp->ip = param->progressinfo.rtpinfo.ip;
634 rtp->port = param->progressinfo.rtpinfo.port;
635 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
639 /* MESSAGE_ALERTING */
640 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
642 struct gsm_mncc *mncc;
645 gsm_trace_header(p_g_interface_name, this, MNCC_ALERT_REQ, DIRECTION_OUT);
646 mncc = create_mncc(MNCC_ALERT_REQ, p_g_callref);
648 mncc->fields |= MNCC_F_PROGRESS;
649 mncc->progress.coding = 3; /* GSM */
650 mncc->progress.location = 1;
651 mncc->progress.descr = 8;
652 add_trace("progress", "coding", "%d", mncc->progress.coding);
653 add_trace("progress", "location", "%d", mncc->progress.location);
654 add_trace("progress", "descr", "%d", mncc->progress.descr);
657 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
659 new_state(PORT_STATE_IN_ALERTING);
661 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
662 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
664 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
665 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
666 p_g_tch_connected = 1;
670 /* MESSAGE_CONNECT */
671 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
673 struct gsm_mncc *mncc;
675 /* copy connected information */
676 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
677 /* screen outgoing caller id */
678 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_g_interface_name);
681 mncc = create_mncc(MNCC_SETUP_RSP, p_g_callref);
682 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_RSP, DIRECTION_OUT);
683 /* caller information */
684 mncc->fields |= MNCC_F_CONNECTED;
685 mncc->connected.plan = 1;
686 switch (p_callerinfo.ntype) {
687 case INFO_NTYPE_UNKNOWN:
688 mncc->connected.type = 0x0;
690 case INFO_NTYPE_INTERNATIONAL:
691 mncc->connected.type = 0x1;
693 case INFO_NTYPE_NATIONAL:
694 mncc->connected.type = 0x2;
696 case INFO_NTYPE_SUBSCRIBER:
697 mncc->connected.type = 0x4;
699 default: /* INFO_NTYPE_NOTPRESENT */
700 mncc->fields &= ~MNCC_F_CONNECTED;
703 switch (p_callerinfo.screen) {
704 case INFO_SCREEN_USER:
705 mncc->connected.screen = 0;
707 default: /* INFO_SCREEN_NETWORK */
708 mncc->connected.screen = 3;
711 switch (p_callerinfo.present) {
712 case INFO_PRESENT_ALLOWED:
713 mncc->connected.present = 0;
715 case INFO_PRESENT_RESTRICTED:
716 mncc->connected.present = 1;
718 default: /* INFO_PRESENT_NOTAVAIL */
719 mncc->connected.present = 2;
722 if (mncc->fields & MNCC_F_CONNECTED) {
723 SCPY(mncc->connected.number, p_connectinfo.id);
724 add_trace("connected", "type", "%d", mncc->connected.type);
725 add_trace("connected", "plan", "%d", mncc->connected.plan);
726 add_trace("connected", "present", "%d", mncc->connected.present);
727 add_trace("connected", "screen", "%d", mncc->connected.screen);
728 add_trace("connected", "number", "%s", mncc->connected.number);
731 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
733 new_state(PORT_STATE_CONNECT_WAITING);
735 if (param->connectinfo.rtpinfo.port) {
736 struct gsm_mncc_rtp *rtp;
738 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->connectinfo.rtpinfo.ip, param->connectinfo.rtpinfo.port);
739 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
740 rtp->ip = param->connectinfo.rtpinfo.ip;
741 rtp->port = param->connectinfo.rtpinfo.port;
742 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
747 /* MESSAGE_DISCONNECT */
748 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
750 struct gsm_mncc *mncc;
752 /* send disconnect */
753 mncc = create_mncc(MNCC_DISC_REQ, p_g_callref);
754 gsm_trace_header(p_g_interface_name, this, MNCC_DISC_REQ, DIRECTION_OUT);
756 mncc->fields |= MNCC_F_PROGRESS;
757 mncc->progress.coding = 3; /* GSM */
758 mncc->progress.location = 1;
759 mncc->progress.descr = 8;
760 add_trace("progress", "coding", "%d", mncc->progress.coding);
761 add_trace("progress", "location", "%d", mncc->progress.location);
762 add_trace("progress", "descr", "%d", mncc->progress.descr);
764 mncc->fields |= MNCC_F_CAUSE;
765 mncc->cause.coding = 3;
766 mncc->cause.location = param->disconnectinfo.location;
767 mncc->cause.value = param->disconnectinfo.cause;
768 add_trace("cause", "coding", "%d", mncc->cause.coding);
769 add_trace("cause", "location", "%d", mncc->cause.location);
770 add_trace("cause", "value", "%d", mncc->cause.value);
772 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
774 new_state(PORT_STATE_OUT_DISCONNECT);
776 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
777 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
779 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
780 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
781 p_g_tch_connected = 1;
786 /* MESSAGE_RELEASE */
787 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
789 struct gsm_mncc *mncc;
792 mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
793 gsm_trace_header(p_g_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
794 mncc->fields |= MNCC_F_CAUSE;
795 mncc->cause.coding = 3;
796 mncc->cause.location = param->disconnectinfo.location;
797 mncc->cause.value = param->disconnectinfo.cause;
798 add_trace("cause", "coding", "%d", mncc->cause.coding);
799 add_trace("cause", "location", "%d", mncc->cause.location);
800 add_trace("cause", "value", "%d", mncc->cause.value);
802 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
804 new_state(PORT_STATE_RELEASE);
805 trigger_work(&p_g_delete);
810 * endpoint sends messages to the port
812 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
816 if (Port::message_epoint(epoint_id, message_id, param))
820 case MESSAGE_NOTIFY: /* display and notifications */
822 message_notify(epoint_id, message_id, param);
825 // case MESSAGE_FACILITY: /* facility message */
826 // message_facility(epoint_id, message_id, param);
829 case MESSAGE_PROCEEDING: /* message not handles */
833 case MESSAGE_PROGRESS:
835 message_progress(epoint_id, message_id, param);
838 case MESSAGE_ALERTING: /* call of endpoint is ringing */
840 if (p_state!=PORT_STATE_IN_PROCEEDING)
842 message_alerting(epoint_id, message_id, param);
843 if (p_g_notify_pending) {
844 /* send pending notify message during connect */
845 message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
846 message_free(p_g_notify_pending);
847 p_g_notify_pending = NULL;
851 case MESSAGE_CONNECT: /* call of endpoint is connected */
853 if (p_state!=PORT_STATE_IN_PROCEEDING
854 && p_state!=PORT_STATE_IN_ALERTING)
856 message_connect(epoint_id, message_id, param);
857 if (p_g_notify_pending) {
858 /* send pending notify message during connect */
859 message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
860 message_free(p_g_notify_pending);
861 p_g_notify_pending = NULL;
865 case MESSAGE_DISCONNECT: /* call has been disconnected */
867 if (p_state!=PORT_STATE_IN_PROCEEDING
868 && p_state!=PORT_STATE_IN_ALERTING
869 && p_state!=PORT_STATE_OUT_SETUP
870 && p_state!=PORT_STATE_OUT_OVERLAP
871 && p_state!=PORT_STATE_OUT_PROCEEDING
872 && p_state!=PORT_STATE_OUT_ALERTING
873 && p_state!=PORT_STATE_CONNECT
874 && p_state!=PORT_STATE_CONNECT_WAITING)
876 message_disconnect(epoint_id, message_id, param);
879 case MESSAGE_RELEASE: /* release isdn port */
881 if (p_state==PORT_STATE_RELEASE)
883 message_release(epoint_id, message_id, param);
891 /* deletes only if l3id is release, otherwhise it will be triggered then */
892 static int delete_event(struct lcr_work *work, void *instance, int index)
894 class Pgsm *gsmport = (class Pgsm *)instance;
918 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
920 struct mncc_q_entry *qe;
922 qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
928 memcpy(qe->data, mncc, len);
930 /* in case of empty list ... */
931 if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
932 /* the list head and tail both point to the new qe */
933 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
935 /* append to tail of list */
936 lcr_gsm->mncc_q_tail->next = qe;
937 lcr_gsm->mncc_q_tail = qe;
940 lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
945 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
947 struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
951 /* dequeue the successfully sent message */
952 lcr_gsm->mncc_q_hd = qe->next;
955 if (qe == lcr_gsm->mncc_q_tail)
956 lcr_gsm->mncc_q_tail = NULL;
961 /* routine called by LCR code if it wants to send a message to OpenBSC */
962 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
966 /* FIXME: the caller should provide this */
969 len = sizeof(struct gsm_data_frame) + 33;
972 len = sizeof(struct gsm_mncc);
976 return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
979 /* close MNCC socket */
980 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
983 class Pgsm *pgsm = NULL;
984 struct lcr_msg *message;
986 PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
991 /* free all the calls that were running through the MNCC interface */
994 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_GSM) {
995 pgsm = (class Pgsm *)port;
996 if (pgsm->p_g_lcr_gsm == lcr_gsm) {
997 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
998 message->param.disconnectinfo.cause = 27; // temp. unavail.
999 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1000 message_put(message);
1001 pgsm->new_state(PORT_STATE_RELEASE);
1002 trigger_work(&pgsm->p_g_delete);
1008 /* flush the queue */
1009 while (mncc_q_dequeue(lcr_gsm))
1012 /* start the re-connect timer */
1013 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1018 /* write to OpenBSC via MNCC socket */
1019 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1021 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1022 struct mncc_q_entry *qe, *qe2;
1026 qe = lcr_gsm->mncc_q_hd;
1028 lfd->when &= ~LCR_FD_WRITE;
1031 rc = write(lfd->fd, qe->data, qe->len);
1033 return mncc_fd_close(lcr_gsm, lfd);
1036 if (rc < (int)qe->len)
1038 /* dequeue the successfully sent message */
1039 qe2 = mncc_q_dequeue(lcr_gsm);
1046 /* read from OpenBSC via MNCC socket */
1047 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1049 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1051 static char buf[sizeof(struct gsm_mncc)+1024];
1052 struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1054 memset(buf, 0, sizeof(buf));
1055 rc = recv(lfd->fd, buf, sizeof(buf), 0);
1057 return mncc_fd_close(lcr_gsm, lfd);
1061 /* Hand the MNCC message into LCR */
1062 switch (lcr_gsm->type) {
1064 case LCR_GSM_TYPE_NETWORK:
1065 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1068 case LCR_GSM_TYPE_MS:
1069 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1076 /* file descriptor callback if we can read or write form MNCC socket */
1077 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1081 if (what & LCR_FD_READ)
1082 rc = mncc_fd_read(lfd, inst, idx);
1086 if (what & LCR_FD_WRITE)
1087 rc = mncc_fd_write(lfd, inst, idx);
1092 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1094 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1097 lcr_gsm->mncc_lfd.fd = -1;
1099 fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1101 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1105 rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1106 sizeof(lcr_gsm->sun));
1108 PERROR("Could not connect to MNCC socket %s, "
1109 "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1110 SOCKET_RETRY_TIMER);
1112 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1114 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1115 lcr_gsm->mncc_lfd.fd = fd;
1116 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);