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 SCPY(p_g_interface_name, interface->name);
143 p_callerinfo.itype = (interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
144 memset(&p_g_delete, 0, sizeof(p_g_delete));
145 add_work(&p_g_delete, delete_event, this, 0);
150 p_g_gsm_b_index = -1;
151 p_g_gsm_b_active = 0;
152 p_g_notify_pending = NULL;
153 p_g_setup_pending = NULL;
154 p_g_connect_pending = NULL;
155 p_g_decoder = gsm_audio_create();
156 p_g_encoder = gsm_audio_create();
157 if (!p_g_encoder || !p_g_decoder) {
158 PERROR("Failed to create GSM audio codec instance\n");
159 trigger_work(&p_g_delete);
162 p_g_tch_connected = 0;
164 PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
172 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
174 del_work(&p_g_delete);
176 /* remove queued message */
177 if (p_g_notify_pending)
178 message_free(p_g_notify_pending);
179 if (p_g_setup_pending)
180 message_free(p_g_setup_pending);
181 if (p_g_connect_pending)
182 message_free(p_g_connect_pending);
186 gsm_audio_destroy(p_g_encoder);
188 gsm_audio_destroy(p_g_decoder);
192 /* receive encoded frame from gsm */
193 void Pgsm::frame_receive(void *arg)
195 struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
196 signed short samples[160];
197 unsigned char data[160];
203 if ((frame->data[0]>>4) != 0xd)
204 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
207 gsm_audio_decode(p_g_decoder, frame->data, samples);
208 for (i = 0; i < 160; i++) {
209 data[i] = audio_s16_to_law[samples[i] & 0xffff];
213 bridge_tx(data, 160);
216 /* send traffic to gsm */
217 int Pgsm::bridge_rx(unsigned char *data, int len)
219 unsigned char frame[33];
221 /* encoder init failed */
225 /* (currently) not connected, so don't flood tch! */
226 if (!p_g_tch_connected)
229 /* write to rx buffer */
231 p_g_rxdata[p_g_rxpos++] = audio_law_to_s32[*data++];
232 if (p_g_rxpos == 160) {
236 gsm_audio_encode(p_g_encoder, p_g_rxdata, frame);
244 void Pgsm::frame_send(void *_frame)
246 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
247 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
249 frame->msg_type = GSM_TCHF_FRAME;
250 frame->callref = p_g_callref;
251 memcpy(frame->data, _frame, 33);
254 mncc_send(p_g_lcr_gsm, frame->msg_type, frame);
261 void gsm_trace_header(const char *interface_name, class Pgsm *port, unsigned int msg_type, int direction)
264 struct interface *interface = interface_first;
266 interface = getinterfacebyname(interface_name);
270 /* select message and primitive text */
271 SCPY(msgtext, mncc_name(msg_type));
275 switch(port->p_type) {
276 case PORT_TYPE_GSM_BS_OUT:
277 case PORT_TYPE_GSM_BS_IN:
278 SCAT(msgtext, " LCR<->BSC");
280 case PORT_TYPE_GSM_MS_OUT:
281 case PORT_TYPE_GSM_MS_IN:
282 SCAT(msgtext, " LCR<->MS");
286 SCAT(msgtext, " ----");
288 /* init trace with given values */
291 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
292 port?port->p_dialinginfo.id:NULL,
295 port?port->p_serial:0,
299 /* PROCEEDING INDICATION */
300 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
302 struct gsm_mncc *mode;
304 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
305 if (mncc->fields & MNCC_F_CAUSE) {
306 add_trace("cause", "coding", "%d", mncc->cause.coding);
307 add_trace("cause", "location", "%", mncc->cause.location);
308 add_trace("cause", "value", "%", mncc->cause.value);
312 /* modify lchan to GSM codec V1 */
313 gsm_trace_header(p_g_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
314 mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
315 mode->lchan_mode = 0x01; /* GSM V1 */
316 mode->lchan_type = 0x02;
317 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
319 send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
323 /* CALL PROCEEDING INDICATION */
324 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
326 struct lcr_msg *message;
327 struct gsm_mncc *frame;
329 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
332 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
333 message_put(message);
335 new_state(PORT_STATE_OUT_PROCEEDING);
337 if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
338 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
340 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
341 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
342 p_g_tch_connected = 1;
346 /* ALERTING INDICATION */
347 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
349 struct lcr_msg *message;
350 struct gsm_mncc *frame;
352 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
355 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
356 message_put(message);
358 new_state(PORT_STATE_OUT_ALERTING);
360 if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
361 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
363 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
364 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
365 p_g_tch_connected = 1;
369 /* CONNECT INDICATION */
370 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
372 struct gsm_mncc *resp, *frame;
373 struct lcr_msg *message;
375 SCPY(p_connectinfo.id, mncc->connected.number);
376 SCPY(p_connectinfo.imsi, mncc->imsi);
377 p_connectinfo.present = INFO_PRESENT_ALLOWED;
378 p_connectinfo.screen = INFO_SCREEN_NETWORK;
379 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
380 SCPY(p_connectinfo.interface, p_g_interface_name);
382 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
383 if (p_connectinfo.id[0])
384 add_trace("connect", "number", "%s", p_connectinfo.id);
385 else if (mncc->imsi[0])
386 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
388 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
392 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
393 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_g_callref);
395 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
397 new_state(PORT_STATE_CONNECT);
399 if (!p_g_tch_connected) { /* only if ... */
400 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
402 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
403 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
404 p_g_tch_connected = 1;
407 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
408 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
409 message->param.connectinfo.rtpinfo.payload_type = 3; /* FIXME: receive payload type from peer */
411 if (p_g_rtp_bridge) {
412 struct gsm_mncc_rtp *rtp;
414 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding connect msg\n");
415 p_g_connect_pending = message;
416 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
417 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
419 message_put(message);
422 /* CONNECT ACK INDICATION */
423 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
425 struct gsm_mncc *frame;
427 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
430 new_state(PORT_STATE_CONNECT);
432 if (!p_g_tch_connected) { /* only if ... */
433 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
435 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
436 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
437 p_g_tch_connected = 1;
441 /* DISCONNECT INDICATION */
442 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
444 struct lcr_msg *message;
445 int cause = 16, location = 0;
446 struct gsm_mncc *resp;
448 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
449 if (mncc->fields & MNCC_F_CAUSE) {
450 location = mncc->cause.location;
451 cause = mncc->cause.value;
452 add_trace("cause", "coding", "%d", mncc->cause.coding);
453 add_trace("cause", "location", "%d", location);
454 add_trace("cause", "value", "%d", cause);
459 resp = create_mncc(MNCC_REL_REQ, p_g_callref);
460 gsm_trace_header(p_g_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
462 resp->fields |= MNCC_F_CAUSE;
463 resp->cause.coding = 3;
464 resp->cause.location = 1;
465 resp->cause.value = cause;
466 add_trace("cause", "coding", "%d", resp->cause.coding);
467 add_trace("cause", "location", "%d", resp->cause.location);
468 add_trace("cause", "value", "%d", resp->cause.value);
471 send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
473 /* sending release to endpoint */
474 while(p_epointlist) {
475 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
476 message->param.disconnectinfo.cause = cause;
477 message->param.disconnectinfo.location = location;
478 message_put(message);
480 free_epointlist(p_epointlist);
482 new_state(PORT_STATE_RELEASE);
483 trigger_work(&p_g_delete);
486 /* CC_RELEASE INDICATION */
487 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
489 int location = 0, cause = 16;
490 struct lcr_msg *message;
492 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
493 if (mncc->fields & MNCC_F_CAUSE) {
494 location = mncc->cause.location;
495 cause = mncc->cause.value;
496 add_trace("cause", "coding", "%d", mncc->cause.coding);
497 add_trace("cause", "location", "%d", mncc->cause.location);
498 add_trace("cause", "value", "%d", mncc->cause.value);
502 /* sending release to endpoint */
503 while(p_epointlist) {
504 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
505 message->param.disconnectinfo.cause = cause;
506 message->param.disconnectinfo.location = location;
507 message_put(message);
509 free_epointlist(p_epointlist);
511 new_state(PORT_STATE_RELEASE);
512 trigger_work(&p_g_delete);
515 /* NOTIFY INDICATION */
516 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
518 struct lcr_msg *message;
520 gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
521 add_trace("notify", NULL, "%d", mncc->notify);
524 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
525 message->param.notifyinfo.notify = mncc->notify;
526 message_put(message);
530 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
532 struct gsm_mncc *mncc;
535 // printf("if = %d\n", param->notifyinfo.notify);
536 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
537 notify = param->notifyinfo.notify & 0x7f;
538 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
539 /* queue notification */
540 if (p_g_notify_pending)
541 message_free(p_g_notify_pending);
542 p_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
543 memcpy(&p_g_notify_pending->param, param, sizeof(union parameter));
545 /* sending notification */
546 gsm_trace_header(p_g_interface_name, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
547 add_trace("notify", NULL, "%d", notify);
549 mncc = create_mncc(MNCC_NOTIFY_REQ, p_g_callref);
550 mncc->notify = notify;
551 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
556 /* RTP create indication */
557 void Pgsm::rtp_create_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
559 struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
561 /* send queued setup, as we received remote RTP info */
562 if (p_g_setup_pending) {
563 struct lcr_msg *message;
565 message = p_g_setup_pending;
566 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding setup\n", rtp->ip, rtp->port);
567 message->param.setup.rtpinfo.ip = rtp->ip;
568 message->param.setup.rtpinfo.port = rtp->port;
569 message_put(message);
570 p_g_setup_pending = NULL;
572 if (p_g_connect_pending) {
573 struct gsm_mncc_rtp *nrtp;
575 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) connecting RTP... \n", rtp->ip, rtp->port);
576 nrtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
577 nrtp->ip = p_g_rtp_ip_remote;
578 nrtp->port = p_g_rtp_port_remote;
579 send_and_free_mncc(p_g_lcr_gsm, nrtp->msg_type, nrtp);
583 /* RTP connect indication */
584 void Pgsm::rtp_connect_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
586 struct lcr_msg *message;
587 struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
589 if (p_g_connect_pending) {
590 message = p_g_connect_pending;
591 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding connect\n", rtp->ip, rtp->port);
592 message->param.connectinfo.rtpinfo.ip = rtp->ip;
593 message->param.connectinfo.rtpinfo.port = rtp->port;
594 message_put(message);
595 p_g_connect_pending = NULL;
599 /* MESSAGE_PROGRESS */
600 void Pgsm::message_progress(unsigned int epoint_id, int message_id, union parameter *param)
602 if (param->progressinfo.progress == 8) {
603 PDEBUG(DEBUG_GSM, "Remote provides tones for us\n");
607 if (param->progressinfo.rtpinfo.port) {
608 struct gsm_mncc_rtp *rtp;
610 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->progressinfo.rtpinfo.ip, param->progressinfo.rtpinfo.port);
611 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
612 rtp->ip = param->progressinfo.rtpinfo.ip;
613 rtp->port = param->progressinfo.rtpinfo.port;
614 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
618 /* MESSAGE_ALERTING */
619 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
621 struct gsm_mncc *mncc;
624 gsm_trace_header(p_g_interface_name, this, MNCC_ALERT_REQ, DIRECTION_OUT);
625 mncc = create_mncc(MNCC_ALERT_REQ, p_g_callref);
627 mncc->fields |= MNCC_F_PROGRESS;
628 mncc->progress.coding = 3; /* GSM */
629 mncc->progress.location = 1;
630 mncc->progress.descr = 8;
631 add_trace("progress", "coding", "%d", mncc->progress.coding);
632 add_trace("progress", "location", "%d", mncc->progress.location);
633 add_trace("progress", "descr", "%d", mncc->progress.descr);
636 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
638 new_state(PORT_STATE_IN_ALERTING);
640 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
641 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
643 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
644 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
645 p_g_tch_connected = 1;
649 /* MESSAGE_CONNECT */
650 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
652 struct gsm_mncc *mncc;
654 /* copy connected information */
655 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
656 /* screen outgoing caller id */
657 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_g_interface_name);
660 mncc = create_mncc(MNCC_SETUP_RSP, p_g_callref);
661 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_RSP, DIRECTION_OUT);
662 /* caller information */
663 mncc->fields |= MNCC_F_CONNECTED;
664 mncc->connected.plan = 1;
665 switch (p_callerinfo.ntype) {
666 case INFO_NTYPE_UNKNOWN:
667 mncc->connected.type = 0x0;
669 case INFO_NTYPE_INTERNATIONAL:
670 mncc->connected.type = 0x1;
672 case INFO_NTYPE_NATIONAL:
673 mncc->connected.type = 0x2;
675 case INFO_NTYPE_SUBSCRIBER:
676 mncc->connected.type = 0x4;
678 default: /* INFO_NTYPE_NOTPRESENT */
679 mncc->fields &= ~MNCC_F_CONNECTED;
682 switch (p_callerinfo.screen) {
683 case INFO_SCREEN_USER:
684 mncc->connected.screen = 0;
686 default: /* INFO_SCREEN_NETWORK */
687 mncc->connected.screen = 3;
690 switch (p_callerinfo.present) {
691 case INFO_PRESENT_ALLOWED:
692 mncc->connected.present = 0;
694 case INFO_PRESENT_RESTRICTED:
695 mncc->connected.present = 1;
697 default: /* INFO_PRESENT_NOTAVAIL */
698 mncc->connected.present = 2;
701 if (mncc->fields & MNCC_F_CONNECTED) {
702 SCPY(mncc->connected.number, p_connectinfo.id);
703 add_trace("connected", "type", "%d", mncc->connected.type);
704 add_trace("connected", "plan", "%d", mncc->connected.plan);
705 add_trace("connected", "present", "%d", mncc->connected.present);
706 add_trace("connected", "screen", "%d", mncc->connected.screen);
707 add_trace("connected", "number", "%s", mncc->connected.number);
710 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
712 new_state(PORT_STATE_CONNECT_WAITING);
714 if (param->connectinfo.rtpinfo.port) {
715 struct gsm_mncc_rtp *rtp;
717 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->connectinfo.rtpinfo.ip, param->connectinfo.rtpinfo.port);
718 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
719 rtp->ip = param->connectinfo.rtpinfo.ip;
720 rtp->port = param->connectinfo.rtpinfo.port;
721 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
726 /* MESSAGE_DISCONNECT */
727 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
729 struct gsm_mncc *mncc;
731 /* send disconnect */
732 mncc = create_mncc(MNCC_DISC_REQ, p_g_callref);
733 gsm_trace_header(p_g_interface_name, this, MNCC_DISC_REQ, DIRECTION_OUT);
735 mncc->fields |= MNCC_F_PROGRESS;
736 mncc->progress.coding = 3; /* GSM */
737 mncc->progress.location = 1;
738 mncc->progress.descr = 8;
739 add_trace("progress", "coding", "%d", mncc->progress.coding);
740 add_trace("progress", "location", "%d", mncc->progress.location);
741 add_trace("progress", "descr", "%d", mncc->progress.descr);
743 mncc->fields |= MNCC_F_CAUSE;
744 mncc->cause.coding = 3;
745 mncc->cause.location = param->disconnectinfo.location;
746 mncc->cause.value = param->disconnectinfo.cause;
747 add_trace("cause", "coding", "%d", mncc->cause.coding);
748 add_trace("cause", "location", "%d", mncc->cause.location);
749 add_trace("cause", "value", "%d", mncc->cause.value);
751 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
753 new_state(PORT_STATE_OUT_DISCONNECT);
755 if (p_g_tones && !p_g_tch_connected) { /* only if ... */
756 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
758 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
759 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
760 p_g_tch_connected = 1;
765 /* MESSAGE_RELEASE */
766 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
768 struct gsm_mncc *mncc;
771 mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
772 gsm_trace_header(p_g_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
773 mncc->fields |= MNCC_F_CAUSE;
774 mncc->cause.coding = 3;
775 mncc->cause.location = param->disconnectinfo.location;
776 mncc->cause.value = param->disconnectinfo.cause;
777 add_trace("cause", "coding", "%d", mncc->cause.coding);
778 add_trace("cause", "location", "%d", mncc->cause.location);
779 add_trace("cause", "value", "%d", mncc->cause.value);
781 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
783 new_state(PORT_STATE_RELEASE);
784 trigger_work(&p_g_delete);
789 * endpoint sends messages to the port
791 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
795 if (Port::message_epoint(epoint_id, message_id, param))
799 case MESSAGE_NOTIFY: /* display and notifications */
801 message_notify(epoint_id, message_id, param);
804 // case MESSAGE_FACILITY: /* facility message */
805 // message_facility(epoint_id, message_id, param);
808 case MESSAGE_PROCEEDING: /* message not handles */
812 case MESSAGE_PROGRESS:
814 message_progress(epoint_id, message_id, param);
817 case MESSAGE_ALERTING: /* call of endpoint is ringing */
819 if (p_state!=PORT_STATE_IN_PROCEEDING)
821 message_alerting(epoint_id, message_id, param);
822 if (p_g_notify_pending) {
823 /* send pending notify message during connect */
824 message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
825 message_free(p_g_notify_pending);
826 p_g_notify_pending = NULL;
830 case MESSAGE_CONNECT: /* call of endpoint is connected */
832 if (p_state!=PORT_STATE_IN_PROCEEDING
833 && p_state!=PORT_STATE_IN_ALERTING)
835 message_connect(epoint_id, message_id, param);
836 if (p_g_notify_pending) {
837 /* send pending notify message during connect */
838 message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
839 message_free(p_g_notify_pending);
840 p_g_notify_pending = NULL;
844 case MESSAGE_DISCONNECT: /* call has been disconnected */
846 if (p_state!=PORT_STATE_IN_PROCEEDING
847 && p_state!=PORT_STATE_IN_ALERTING
848 && p_state!=PORT_STATE_OUT_SETUP
849 && p_state!=PORT_STATE_OUT_OVERLAP
850 && p_state!=PORT_STATE_OUT_PROCEEDING
851 && p_state!=PORT_STATE_OUT_ALERTING
852 && p_state!=PORT_STATE_CONNECT
853 && p_state!=PORT_STATE_CONNECT_WAITING)
855 message_disconnect(epoint_id, message_id, param);
858 case MESSAGE_RELEASE: /* release isdn port */
860 if (p_state==PORT_STATE_RELEASE)
862 message_release(epoint_id, message_id, param);
870 /* deletes only if l3id is release, otherwhise it will be triggered then */
871 static int delete_event(struct lcr_work *work, void *instance, int index)
873 class Pgsm *gsmport = (class Pgsm *)instance;
897 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
899 struct mncc_q_entry *qe;
901 qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
907 memcpy(qe->data, mncc, len);
909 /* in case of empty list ... */
910 if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
911 /* the list head and tail both point to the new qe */
912 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
914 /* append to tail of list */
915 lcr_gsm->mncc_q_tail->next = qe;
916 lcr_gsm->mncc_q_tail = qe;
919 lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
924 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
926 struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
930 /* dequeue the successfully sent message */
931 lcr_gsm->mncc_q_hd = qe->next;
934 if (qe == lcr_gsm->mncc_q_tail)
935 lcr_gsm->mncc_q_tail = NULL;
940 /* routine called by LCR code if it wants to send a message to OpenBSC */
941 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
945 /* FIXME: the caller should provide this */
948 len = sizeof(struct gsm_data_frame) + 33;
951 len = sizeof(struct gsm_mncc);
955 return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
958 /* close MNCC socket */
959 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
962 class Pgsm *pgsm = NULL;
963 struct lcr_msg *message;
965 PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
970 /* free all the calls that were running through the MNCC interface */
973 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_GSM) {
974 pgsm = (class Pgsm *)port;
975 if (pgsm->p_g_lcr_gsm == lcr_gsm) {
976 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
977 message->param.disconnectinfo.cause = 27; // temp. unavail.
978 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
979 message_put(message);
980 pgsm->new_state(PORT_STATE_RELEASE);
981 trigger_work(&pgsm->p_g_delete);
987 /* flush the queue */
988 while (mncc_q_dequeue(lcr_gsm))
991 /* start the re-connect timer */
992 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
997 /* write to OpenBSC via MNCC socket */
998 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1000 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1001 struct mncc_q_entry *qe, *qe2;
1005 qe = lcr_gsm->mncc_q_hd;
1007 lfd->when &= ~LCR_FD_WRITE;
1010 rc = write(lfd->fd, qe->data, qe->len);
1012 return mncc_fd_close(lcr_gsm, lfd);
1015 if (rc < (int)qe->len)
1017 /* dequeue the successfully sent message */
1018 qe2 = mncc_q_dequeue(lcr_gsm);
1025 /* read from OpenBSC via MNCC socket */
1026 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1028 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1030 static char buf[sizeof(struct gsm_mncc)+1024];
1031 struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1033 memset(buf, 0, sizeof(buf));
1034 rc = recv(lfd->fd, buf, sizeof(buf), 0);
1036 return mncc_fd_close(lcr_gsm, lfd);
1040 /* Hand the MNCC message into LCR */
1041 switch (lcr_gsm->type) {
1043 case LCR_GSM_TYPE_NETWORK:
1044 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1047 case LCR_GSM_TYPE_MS:
1048 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1055 /* file descriptor callback if we can read or write form MNCC socket */
1056 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1060 if (what & LCR_FD_READ)
1061 rc = mncc_fd_read(lfd, inst, idx);
1065 if (what & LCR_FD_WRITE)
1066 rc = mncc_fd_write(lfd, inst, idx);
1071 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1073 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1076 lcr_gsm->mncc_lfd.fd = -1;
1078 fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1080 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1084 rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1085 sizeof(lcr_gsm->sun));
1087 PERROR("Could not connect to MNCC socket %s, "
1088 "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1089 SOCKET_RETRY_TIMER);
1091 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1093 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1094 lcr_gsm->mncc_lfd.fd = fd;
1095 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);