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 { MNCC_SETUP_REQ, "MNCC_SETUP_REQ" },
38 { MNCC_SETUP_IND, "MNCC_SETUP_IND" },
39 { MNCC_SETUP_RSP, "MNCC_SETUP_RSP" },
40 { MNCC_SETUP_CNF, "MNCC_SETUP_CNF" },
41 { MNCC_SETUP_COMPL_REQ, "MNCC_SETUP_COMPL_REQ" },
42 { MNCC_SETUP_COMPL_IND, "MNCC_SETUP_COMPL_IND" },
43 { MNCC_CALL_CONF_IND, "MNCC_CALL_CONF_IND" },
44 { MNCC_CALL_PROC_REQ, "MNCC_CALL_PROC_REQ" },
45 { MNCC_PROGRESS_REQ, "MNCC_PROGRESS_REQ" },
46 { MNCC_ALERT_REQ, "MNCC_ALERT_REQ" },
47 { MNCC_ALERT_IND, "MNCC_ALERT_IND" },
48 { MNCC_NOTIFY_REQ, "MNCC_NOTIFY_REQ" },
49 { MNCC_NOTIFY_IND, "MNCC_NOTIFY_IND" },
50 { MNCC_DISC_REQ, "MNCC_DISC_REQ" },
51 { MNCC_DISC_IND, "MNCC_DISC_IND" },
52 { MNCC_REL_REQ, "MNCC_REL_REQ" },
53 { MNCC_REL_IND, "MNCC_REL_IND" },
54 { MNCC_REL_CNF, "MNCC_REL_CNF" },
55 { MNCC_FACILITY_REQ, "MNCC_FACILITY_REQ" },
56 { MNCC_FACILITY_IND, "MNCC_FACILITY_IND" },
57 { MNCC_START_DTMF_IND, "MNCC_START_DTMF_IND" },
58 { MNCC_START_DTMF_RSP, "MNCC_START_DTMF_RSP" },
59 { MNCC_START_DTMF_REJ, "MNCC_START_DTMF_REJ" },
60 { MNCC_STOP_DTMF_IND, "MNCC_STOP_DTMF_IND" },
61 { MNCC_STOP_DTMF_RSP, "MNCC_STOP_DTMF_RSP" },
62 { MNCC_MODIFY_REQ, "MNCC_MODIFY_REQ" },
63 { MNCC_MODIFY_IND, "MNCC_MODIFY_IND" },
64 { MNCC_MODIFY_RSP, "MNCC_MODIFY_RSP" },
65 { MNCC_MODIFY_CNF, "MNCC_MODIFY_CNF" },
66 { MNCC_MODIFY_REJ, "MNCC_MODIFY_REJ" },
67 { MNCC_HOLD_IND, "MNCC_HOLD_IND" },
68 { MNCC_HOLD_CNF, "MNCC_HOLD_CNF" },
69 { MNCC_HOLD_REJ, "MNCC_HOLD_REJ" },
70 { MNCC_RETRIEVE_IND, "MNCC_RETRIEVE_IND" },
71 { MNCC_RETRIEVE_CNF, "MNCC_RETRIEVE_CNF" },
72 { MNCC_RETRIEVE_REJ, "MNCC_RETRIEVE_REJ" },
73 { MNCC_USERINFO_REQ, "MNCC_USERINFO_REQ" },
74 { MNCC_USERINFO_IND, "MNCC_USERINFO_IND" },
75 { MNCC_REJ_REQ, "MNCC_REJ_REQ" },
76 { MNCC_REJ_IND, "MNCC_REJ_IND" },
77 { MNCC_PROGRESS_IND, "MNCC_PROGRESS_IND" },
78 { MNCC_CALL_PROC_IND, "MNCC_CALL_PROC_IND" },
79 { MNCC_CALL_CONF_REQ, "MNCC_CALL_CONF_REQ" },
80 { MNCC_START_DTMF_REQ, "MNCC_START_DTMF_REQ" },
81 { MNCC_STOP_DTMF_REQ, "MNCC_STOP_DTMF_REQ" },
82 { MNCC_HOLD_REQ, "MNCC_HOLD_REQ " },
83 { MNCC_RETRIEVE_REQ, "MNCC_RETRIEVE_REQ" },
87 const char *mncc_name(int value)
91 while (mncc_names[i].name) {
92 if (mncc_names[i].msg_type == value)
93 return mncc_names[i].name;
99 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data);
102 * create and send mncc message
104 struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
106 struct gsm_mncc *mncc;
108 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
109 mncc->msg_type = msg_type;
110 mncc->callref = callref;
113 int send_and_free_mncc(struct lcr_gsm *lcr_gsm, unsigned int msg_type, void *data)
118 ret = mncc_send(lcr_gsm, msg_type, data);
125 static int delete_event(struct lcr_work *work, void *instance, int index);
130 Pgsm::Pgsm(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : PmISDN(type, mISDNport, portname, settings, channel, exclusive, mode)
132 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
133 memset(&p_m_g_delete, 0, sizeof(p_m_g_delete));
134 add_work(&p_m_g_delete, delete_event, this, 0);
135 p_m_g_lcr_gsm = NULL;
138 p_m_g_gsm_b_sock = -1;
139 p_m_g_gsm_b_index = -1;
140 p_m_g_gsm_b_active = 0;
141 p_m_g_notify_pending = NULL;
142 p_m_g_decoder = gsm_audio_create();
143 p_m_g_encoder = gsm_audio_create();
144 if (!p_m_g_encoder || !p_m_g_decoder) {
145 PERROR("Failed to create GSM audio codec instance\n");
146 trigger_work(&p_m_g_delete);
149 p_m_g_tch_connected = 0;
151 PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
159 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
161 del_work(&p_m_g_delete);
163 /* remove queued message */
164 if (p_m_g_notify_pending)
165 message_free(p_m_g_notify_pending);
167 /* close audio transfer socket */
168 if (p_m_g_gsm_b_sock > -1)
173 gsm_audio_destroy(p_m_g_encoder);
175 gsm_audio_destroy(p_m_g_decoder);
179 /* close bsc side bchannel */
180 void Pgsm::bchannel_close(void)
182 if (p_m_g_gsm_b_sock > -1) {
183 unregister_fd(&p_m_g_gsm_b_fd);
184 close(p_m_g_gsm_b_sock);
186 p_m_g_gsm_b_sock = -1;
187 p_m_g_gsm_b_index = -1;
188 p_m_g_gsm_b_active = 0;
191 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index);
193 /* open external side bchannel */
194 int Pgsm::bchannel_open(int index)
197 struct sockaddr_mISDN addr;
198 struct mISDNhead act;
200 if (p_m_g_gsm_b_sock > -1) {
201 PERROR("Socket already created for index %d\n", index);
206 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
208 PERROR("Failed to open bchannel-socket for index %d\n", index);
212 memset(&p_m_g_gsm_b_fd, 0, sizeof(p_m_g_gsm_b_fd));
213 p_m_g_gsm_b_fd.fd = p_m_g_gsm_b_sock;
214 register_fd(&p_m_g_gsm_b_fd, LCR_FD_READ, b_handler, this, 0);
217 /* bind socket to bchannel */
218 addr.family = AF_ISDN;
219 addr.dev = mISDNloop.port;
220 addr.channel = index+1+(index>15);
221 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
223 PERROR("Failed to bind bchannel-socket for index %d\n", index);
227 /* activate bchannel */
228 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
229 act.prim = PH_ACTIVATE_REQ;
231 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
233 PERROR("Failed to activate index %d\n", index);
238 p_m_g_gsm_b_index = index;
243 /* receive from bchannel */
244 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
246 unsigned char frame[33];
248 /* encoder init failed */
252 /* (currently) not connected, so don't flood tch! */
253 if (!p_m_g_tch_connected)
256 /* write to rx buffer */
258 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
259 if (p_m_g_rxpos == 160) {
263 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
269 /* transmit to bchannel */
270 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
272 unsigned char buf[MISDN_HEADER_LEN+len];
273 struct mISDNhead *hh = (struct mISDNhead *)buf;
276 if (!p_m_g_gsm_b_active)
279 /* make and send frame */
280 hh->prim = PH_DATA_REQ;
282 memcpy(buf+MISDN_HEADER_LEN, data, len);
283 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
285 PERROR("Failed to send to socket index %d\n", p_m_g_gsm_b_index);
288 void Pgsm::frame_send(void *_frame)
290 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
291 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
293 frame->msg_type = GSM_TCHF_FRAME;
294 frame->callref = p_m_g_callref;
295 memcpy(frame->data, _frame, 33);
298 mncc_send(p_m_g_lcr_gsm, frame->msg_type, frame);
303 void Pgsm::frame_receive(void *arg)
305 struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
306 signed short samples[160];
307 unsigned char data[160];
313 if ((frame->data[0]>>4) != 0xd)
314 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
317 gsm_audio_decode(p_m_g_decoder, frame->data, samples);
318 for (i = 0; i < 160; i++) {
319 data[i] = audio_s16_to_law[samples[i] & 0xffff];
323 bchannel_send(PH_DATA_REQ, 0, data, 160);
330 void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
334 /* select message and primitive text */
335 SCPY(msgtext, mncc_name(msg_type));
339 switch(port->p_type) {
340 case PORT_TYPE_GSM_BS_OUT:
341 case PORT_TYPE_GSM_BS_IN:
342 SCAT(msgtext, " LCR<->BSC");
344 case PORT_TYPE_GSM_MS_OUT:
345 case PORT_TYPE_GSM_MS_IN:
346 SCAT(msgtext, " LCR<->MS");
350 SCAT(msgtext, " ----");
352 /* init trace with given values */
353 start_trace(mISDNport?mISDNport->portnum:-1,
354 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
355 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
356 port?port->p_dialinginfo.id:NULL,
359 port?port->p_serial:0,
363 /* select free bchannel from loopback interface */
364 int Pgsm::hunt_bchannel(void)
366 return loop_hunt_bchannel(this, p_m_mISDNport);
369 /* PROCEEDING INDICATION */
370 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
372 struct gsm_mncc *mode;
374 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
375 if (mncc->fields & MNCC_F_CAUSE) {
376 add_trace("cause", "coding", "%d", mncc->cause.coding);
377 add_trace("cause", "location", "%", mncc->cause.location);
378 add_trace("cause", "value", "%", mncc->cause.value);
382 /* modify lchan to GSM codec V1 */
383 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
384 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
385 mode->lchan_mode = 0x01; /* GSM V1 */
386 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
388 send_and_free_mncc(p_m_g_lcr_gsm, mode->msg_type, mode);
392 /* CALL PROCEEDING INDICATION */
393 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
395 struct lcr_msg *message;
396 struct gsm_mncc *frame;
398 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
401 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
402 message_put(message);
404 new_state(PORT_STATE_OUT_PROCEEDING);
406 if (p_m_mISDNport->earlyb && !p_m_g_tch_connected) { /* only if ... */
407 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
409 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
410 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
411 p_m_g_tch_connected = 1;
415 /* ALERTING INDICATION */
416 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
418 struct lcr_msg *message;
419 struct gsm_mncc *frame;
421 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
424 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
425 message_put(message);
427 new_state(PORT_STATE_OUT_ALERTING);
429 if (p_m_mISDNport->earlyb && !p_m_g_tch_connected) { /* only if ... */
430 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
432 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
433 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
434 p_m_g_tch_connected = 1;
438 /* CONNECT INDICATION */
439 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
441 struct gsm_mncc *resp, *frame;
442 struct lcr_msg *message;
444 SCPY(p_connectinfo.id, mncc->connected.number);
445 SCPY(p_connectinfo.imsi, mncc->imsi);
446 p_connectinfo.present = INFO_PRESENT_ALLOWED;
447 p_connectinfo.screen = INFO_SCREEN_NETWORK;
448 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
449 p_connectinfo.isdn_port = p_m_portnum;
450 SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
452 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
453 if (p_connectinfo.id[0])
454 add_trace("connect", "number", "%s", p_connectinfo.id);
455 else if (mncc->imsi[0])
456 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
458 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
462 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
463 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
465 send_and_free_mncc(p_m_g_lcr_gsm, resp->msg_type, resp);
467 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
468 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
469 message_put(message);
471 new_state(PORT_STATE_CONNECT);
473 if (!p_m_g_tch_connected) { /* only if ... */
474 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
476 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
477 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
478 p_m_g_tch_connected = 1;
482 /* CONNECT ACK INDICATION */
483 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
485 struct gsm_mncc *frame;
487 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
490 new_state(PORT_STATE_CONNECT);
492 if (!p_m_g_tch_connected) { /* only if ... */
493 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
495 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
496 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
497 p_m_g_tch_connected = 1;
501 /* DISCONNECT INDICATION */
502 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
504 struct lcr_msg *message;
505 int cause = 16, location = 0;
506 struct gsm_mncc *resp;
508 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
509 if (mncc->fields & MNCC_F_CAUSE) {
510 location = mncc->cause.location;
511 cause = mncc->cause.value;
512 add_trace("cause", "coding", "%d", mncc->cause.coding);
513 add_trace("cause", "location", "%d", location);
514 add_trace("cause", "value", "%d", cause);
519 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
520 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
522 resp->fields |= MNCC_F_CAUSE;
523 resp->cause.coding = 3;
524 resp->cause.location = 1;
525 resp->cause.value = cause;
526 add_trace("cause", "coding", "%d", resp->cause.coding);
527 add_trace("cause", "location", "%d", resp->cause.location);
528 add_trace("cause", "value", "%d", resp->cause.value);
531 send_and_free_mncc(p_m_g_lcr_gsm, resp->msg_type, resp);
533 /* sending release to endpoint */
534 while(p_epointlist) {
535 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
536 message->param.disconnectinfo.cause = cause;
537 message->param.disconnectinfo.location = location;
538 message_put(message);
540 free_epointlist(p_epointlist);
542 new_state(PORT_STATE_RELEASE);
543 trigger_work(&p_m_g_delete);
546 /* CC_RELEASE INDICATION */
547 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
549 int location = 0, cause = 16;
550 struct lcr_msg *message;
552 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
553 if (mncc->fields & MNCC_F_CAUSE) {
554 location = mncc->cause.location;
555 cause = mncc->cause.value;
556 add_trace("cause", "coding", "%d", mncc->cause.coding);
557 add_trace("cause", "location", "%d", mncc->cause.location);
558 add_trace("cause", "value", "%d", mncc->cause.value);
562 /* sending release to endpoint */
563 while(p_epointlist) {
564 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
565 message->param.disconnectinfo.cause = cause;
566 message->param.disconnectinfo.location = location;
567 message_put(message);
569 free_epointlist(p_epointlist);
571 new_state(PORT_STATE_RELEASE);
572 trigger_work(&p_m_g_delete);
575 /* NOTIFY INDICATION */
576 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
578 struct lcr_msg *message;
580 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
581 add_trace("notify", NULL, "%d", mncc->notify);
584 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
585 message->param.notifyinfo.notify = mncc->notify;
586 message_put(message);
590 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
592 struct gsm_mncc *mncc;
595 // printf("if = %d\n", param->notifyinfo.notify);
596 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
597 notify = param->notifyinfo.notify & 0x7f;
598 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
599 /* queue notification */
600 if (p_m_g_notify_pending)
601 message_free(p_m_g_notify_pending);
602 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
603 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
605 /* sending notification */
606 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
607 add_trace("notify", NULL, "%d", notify);
609 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
610 mncc->notify = notify;
611 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
616 /* MESSAGE_ALERTING */
617 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
619 struct gsm_mncc *mncc;
622 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
623 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
624 if (p_m_mISDNport->tones) {
625 mncc->fields |= MNCC_F_PROGRESS;
626 mncc->progress.coding = 3; /* GSM */
627 mncc->progress.location = 1;
628 mncc->progress.descr = 8;
629 add_trace("progress", "coding", "%d", mncc->progress.coding);
630 add_trace("progress", "location", "%d", mncc->progress.location);
631 add_trace("progress", "descr", "%d", mncc->progress.descr);
634 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
636 new_state(PORT_STATE_IN_ALERTING);
638 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
639 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
641 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
642 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
643 p_m_g_tch_connected = 1;
647 /* MESSAGE_CONNECT */
648 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
650 struct gsm_mncc *mncc;
652 /* copy connected information */
653 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
654 /* screen outgoing caller id */
655 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
658 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
659 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
660 /* caller information */
661 mncc->fields |= MNCC_F_CONNECTED;
662 mncc->connected.plan = 1;
663 switch (p_callerinfo.ntype) {
664 case INFO_NTYPE_UNKNOWN:
665 mncc->connected.type = 0x0;
667 case INFO_NTYPE_INTERNATIONAL:
668 mncc->connected.type = 0x1;
670 case INFO_NTYPE_NATIONAL:
671 mncc->connected.type = 0x2;
673 case INFO_NTYPE_SUBSCRIBER:
674 mncc->connected.type = 0x4;
676 default: /* INFO_NTYPE_NOTPRESENT */
677 mncc->fields &= ~MNCC_F_CONNECTED;
680 switch (p_callerinfo.screen) {
681 case INFO_SCREEN_USER:
682 mncc->connected.screen = 0;
684 default: /* INFO_SCREEN_NETWORK */
685 mncc->connected.screen = 3;
688 switch (p_callerinfo.present) {
689 case INFO_PRESENT_ALLOWED:
690 mncc->connected.present = 0;
692 case INFO_PRESENT_RESTRICTED:
693 mncc->connected.present = 1;
695 default: /* INFO_PRESENT_NOTAVAIL */
696 mncc->connected.present = 2;
699 if (mncc->fields & MNCC_F_CONNECTED) {
700 SCPY(mncc->connected.number, p_connectinfo.id);
701 add_trace("connected", "type", "%d", mncc->connected.type);
702 add_trace("connected", "plan", "%d", mncc->connected.plan);
703 add_trace("connected", "present", "%d", mncc->connected.present);
704 add_trace("connected", "screen", "%d", mncc->connected.screen);
705 add_trace("connected", "number", "%s", mncc->connected.number);
708 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
710 new_state(PORT_STATE_CONNECT_WAITING);
713 /* MESSAGE_DISCONNECT */
714 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
716 struct gsm_mncc *mncc;
718 /* send disconnect */
719 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
720 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
721 if (p_m_mISDNport->tones) {
722 mncc->fields |= MNCC_F_PROGRESS;
723 mncc->progress.coding = 3; /* GSM */
724 mncc->progress.location = 1;
725 mncc->progress.descr = 8;
726 add_trace("progress", "coding", "%d", mncc->progress.coding);
727 add_trace("progress", "location", "%d", mncc->progress.location);
728 add_trace("progress", "descr", "%d", mncc->progress.descr);
730 mncc->fields |= MNCC_F_CAUSE;
731 mncc->cause.coding = 3;
732 mncc->cause.location = param->disconnectinfo.location;
733 mncc->cause.value = param->disconnectinfo.cause;
734 add_trace("cause", "coding", "%d", mncc->cause.coding);
735 add_trace("cause", "location", "%d", mncc->cause.location);
736 add_trace("cause", "value", "%d", mncc->cause.value);
738 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
740 new_state(PORT_STATE_OUT_DISCONNECT);
742 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
743 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
745 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
746 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
747 p_m_g_tch_connected = 1;
752 /* MESSAGE_RELEASE */
753 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
755 struct gsm_mncc *mncc;
758 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
759 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
760 mncc->fields |= MNCC_F_CAUSE;
761 mncc->cause.coding = 3;
762 mncc->cause.location = param->disconnectinfo.location;
763 mncc->cause.value = param->disconnectinfo.cause;
764 add_trace("cause", "coding", "%d", mncc->cause.coding);
765 add_trace("cause", "location", "%d", mncc->cause.location);
766 add_trace("cause", "value", "%d", mncc->cause.value);
768 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
770 new_state(PORT_STATE_RELEASE);
771 trigger_work(&p_m_g_delete);
776 * endpoint sends messages to the port
778 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
780 if (PmISDN::message_epoint(epoint_id, message_id, param))
784 case MESSAGE_NOTIFY: /* display and notifications */
785 message_notify(epoint_id, message_id, param);
788 // case MESSAGE_FACILITY: /* facility message */
789 // message_facility(epoint_id, message_id, param);
792 case MESSAGE_PROCEEDING: /* message not handles */
795 case MESSAGE_ALERTING: /* call of endpoint is ringing */
796 if (p_state!=PORT_STATE_IN_PROCEEDING)
798 message_alerting(epoint_id, message_id, param);
799 if (p_m_g_notify_pending) {
800 /* send pending notify message during connect */
801 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
802 message_free(p_m_g_notify_pending);
803 p_m_g_notify_pending = NULL;
807 case MESSAGE_CONNECT: /* call of endpoint is connected */
808 if (p_state!=PORT_STATE_IN_PROCEEDING
809 && p_state!=PORT_STATE_IN_ALERTING)
811 message_connect(epoint_id, message_id, param);
812 if (p_m_g_notify_pending) {
813 /* send pending notify message during connect */
814 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
815 message_free(p_m_g_notify_pending);
816 p_m_g_notify_pending = NULL;
820 case MESSAGE_DISCONNECT: /* call has been disconnected */
821 if (p_state!=PORT_STATE_IN_PROCEEDING
822 && p_state!=PORT_STATE_IN_ALERTING
823 && p_state!=PORT_STATE_OUT_SETUP
824 && p_state!=PORT_STATE_OUT_OVERLAP
825 && p_state!=PORT_STATE_OUT_PROCEEDING
826 && p_state!=PORT_STATE_OUT_ALERTING
827 && p_state!=PORT_STATE_CONNECT
828 && p_state!=PORT_STATE_CONNECT_WAITING)
830 message_disconnect(epoint_id, message_id, param);
833 case MESSAGE_RELEASE: /* release isdn port */
834 if (p_state==PORT_STATE_RELEASE)
836 message_release(epoint_id, message_id, param);
844 /* deletes only if l3id is release, otherwhise it will be triggered then */
845 static int delete_event(struct lcr_work *work, void *instance, int index)
847 class Pgsm *gsmport = (class Pgsm *)instance;
855 * handler of bchannel events
857 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index)
859 class Pgsm *gsmport = (class Pgsm *)instance;
861 unsigned char buffer[2048+MISDN_HEADER_LEN];
862 struct mISDNhead *hh = (struct mISDNhead *)buffer;
864 /* handle message from bchannel */
865 if (gsmport->p_m_g_gsm_b_sock > -1) {
866 ret = recv(gsmport->p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
867 if (ret >= (int)MISDN_HEADER_LEN) {
869 /* we don't care about confirms, we use rx data to sync tx */
872 /* we receive audio data, we respond to it AND we send tones */
874 gsmport->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
876 case PH_ACTIVATE_IND:
877 gsmport->p_m_g_gsm_b_active = 1;
879 case PH_DEACTIVATE_IND:
880 gsmport->p_m_g_gsm_b_active = 0;
884 if (ret < 0 && errno != EWOULDBLOCK)
885 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
909 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
911 struct mncc_q_entry *qe;
913 qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
919 memcpy(qe->data, mncc, len);
921 /* in case of empty list ... */
922 if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
923 /* the list head and tail both point to the new qe */
924 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
926 /* append to tail of list */
927 lcr_gsm->mncc_q_tail->next = qe;
928 lcr_gsm->mncc_q_tail = qe;
931 lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
936 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
938 struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
942 /* dequeue the successfully sent message */
943 lcr_gsm->mncc_q_hd = qe->next;
946 if (qe == lcr_gsm->mncc_q_tail)
947 lcr_gsm->mncc_q_tail = NULL;
952 /* routine called by LCR code if it wants to send a message to OpenBSC */
953 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
957 /* FIXME: the caller should provide this */
960 len = sizeof(struct gsm_data_frame) + 33;
963 len = sizeof(struct gsm_mncc);
967 return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
970 /* close MNCC socket */
971 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
974 class Pgsm *pgsm = NULL;
975 struct lcr_msg *message;
977 PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
982 /* free all the calls that were running through the MNCC interface */
985 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_GSM) {
986 pgsm = (class Pgsm *)port;
987 if (pgsm->p_m_g_lcr_gsm == lcr_gsm) {
988 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
989 message->param.disconnectinfo.cause = 27; // temp. unavail.
990 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
991 message_put(message);
992 pgsm->new_state(PORT_STATE_RELEASE);
993 trigger_work(&pgsm->p_m_g_delete);
999 /* flush the queue */
1000 while (mncc_q_dequeue(lcr_gsm))
1003 /* start the re-connect timer */
1004 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1009 /* write to OpenBSC via MNCC socket */
1010 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1012 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1013 struct mncc_q_entry *qe, *qe2;
1017 qe = lcr_gsm->mncc_q_hd;
1019 lfd->when &= ~LCR_FD_WRITE;
1022 rc = write(lfd->fd, qe->data, qe->len);
1024 return mncc_fd_close(lcr_gsm, lfd);
1027 if (rc < (int)qe->len)
1029 /* dequeue the successfully sent message */
1030 qe2 = mncc_q_dequeue(lcr_gsm);
1037 /* read from OpenBSC via MNCC socket */
1038 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1040 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1042 static char buf[sizeof(struct gsm_mncc)+1024];
1043 struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1045 memset(buf, 0, sizeof(buf));
1046 rc = recv(lfd->fd, buf, sizeof(buf), 0);
1048 return mncc_fd_close(lcr_gsm, lfd);
1052 /* Hand the MNCC message into LCR */
1053 switch (lcr_gsm->type) {
1055 case LCR_GSM_TYPE_NETWORK:
1056 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1059 case LCR_GSM_TYPE_MS:
1060 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1067 /* file descriptor callback if we can read or write form MNCC socket */
1068 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1072 if (what & LCR_FD_READ)
1073 rc = mncc_fd_read(lfd, inst, idx);
1077 if (what & LCR_FD_WRITE)
1078 rc = mncc_fd_write(lfd, inst, idx);
1083 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1085 struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1088 lcr_gsm->mncc_lfd.fd = -1;
1090 fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1092 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1096 rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1097 sizeof(lcr_gsm->sun));
1099 PERROR("Could not connect to MNCC socket %s, "
1100 "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1101 SOCKET_RETRY_TIMER);
1103 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1105 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1106 lcr_gsm->mncc_lfd.fd = fd;
1107 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);