1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
18 #include "gsm_audio.h"
21 #include <mISDN/mISDNcompat.h>
23 struct lcr_gsm *gsm = NULL;
27 /* names of MNCC-SAP */
28 static const struct _value_string {
32 #if defined(MNCC_SETUP_REQ)
33 { MNCC_SETUP_REQ, "MNCC_SETUP_REQ" },
35 #if defined(MNCC_SETUP_IND)
36 { MNCC_SETUP_IND, "MNCC_SETUP_IND" },
38 #if defined(MNCC_SETUP_RSP)
39 { MNCC_SETUP_RSP, "MNCC_SETUP_RSP" },
41 #if defined(MNCC_SETUP_CNF)
42 { MNCC_SETUP_CNF, "MNCC_SETUP_CNF" },
44 #if defined(MNCC_SETUP_COMPL_REQ)
45 { MNCC_SETUP_COMPL_REQ, "MNCC_SETUP_COMPL_REQ" },
47 #if defined(MNCC_SETUP_COMPL_IND)
48 { MNCC_SETUP_COMPL_IND, "MNCC_SETUP_COMPL_IND" },
50 #if defined(MNCC_CALL_CONF_IND)
51 { MNCC_CALL_CONF_IND, "MNCC_CALL_CONF_IND" },
53 #if defined(MNCC_CALL_PROC_REQ)
54 { MNCC_CALL_PROC_REQ, "MNCC_CALL_PROC_REQ" },
56 #if defined(MNCC_PROGRESS_REQ)
57 { MNCC_PROGRESS_REQ, "MNCC_PROGRESS_REQ" },
59 #if defined(MNCC_ALERT_REQ)
60 { MNCC_ALERT_REQ, "MNCC_ALERT_REQ" },
62 #if defined(MNCC_ALERT_IND)
63 { MNCC_ALERT_IND, "MNCC_ALERT_IND" },
65 #if defined(MNCC_NOTIFY_REQ)
66 { MNCC_NOTIFY_REQ, "MNCC_NOTIFY_REQ" },
68 #if defined(MNCC_NOTIFY_IND)
69 { MNCC_NOTIFY_IND, "MNCC_NOTIFY_IND" },
71 #if defined(MNCC_DISC_REQ)
72 { MNCC_DISC_REQ, "MNCC_DISC_REQ" },
74 #if defined(MNCC_DISC_IND)
75 { MNCC_DISC_IND, "MNCC_DISC_IND" },
77 #if defined(MNCC_REL_REQ)
78 { MNCC_REL_REQ, "MNCC_REL_REQ" },
80 #if defined(MNCC_REL_IND)
81 { MNCC_REL_IND, "MNCC_REL_IND" },
83 #if defined(MNCC_REL_CNF)
84 { MNCC_REL_CNF, "MNCC_REL_CNF" },
86 #if defined(MNCC_FACILITY_REQ)
87 { MNCC_FACILITY_REQ, "MNCC_FACILITY_REQ" },
89 #if defined(MNCC_FACILITY_IND)
90 { MNCC_FACILITY_IND, "MNCC_FACILITY_IND" },
92 #if defined(MNCC_START_DTMF_IND)
93 { MNCC_START_DTMF_IND, "MNCC_START_DTMF_IND" },
95 #if defined(MNCC_START_DTMF_RSP)
96 { MNCC_START_DTMF_RSP, "MNCC_START_DTMF_RSP" },
98 #if defined(MNCC_START_DTMF_REJ)
99 { MNCC_START_DTMF_REJ, "MNCC_START_DTMF_REJ" },
101 #if defined(MNCC_STOP_DTMF_IND)
102 { MNCC_STOP_DTMF_IND, "MNCC_STOP_DTMF_IND" },
104 #if defined(MNCC_STOP_DTMF_RSP)
105 { MNCC_STOP_DTMF_RSP, "MNCC_STOP_DTMF_RSP" },
107 #if defined(MNCC_MODIFY_REQ)
108 { MNCC_MODIFY_REQ, "MNCC_MODIFY_REQ" },
110 #if defined(MNCC_MODIFY_IND)
111 { MNCC_MODIFY_IND, "MNCC_MODIFY_IND" },
113 #if defined(MNCC_MODIFY_RSP)
114 { MNCC_MODIFY_RSP, "MNCC_MODIFY_RSP" },
116 #if defined(MNCC_MODIFY_CNF)
117 { MNCC_MODIFY_CNF, "MNCC_MODIFY_CNF" },
119 #if defined(MNCC_MODIFY_REJ)
120 { MNCC_MODIFY_REJ, "MNCC_MODIFY_REJ" },
122 #if defined(MNCC_HOLD_IND)
123 { MNCC_HOLD_IND, "MNCC_HOLD_IND" },
125 #if defined(MNCC_HOLD_CNF)
126 { MNCC_HOLD_CNF, "MNCC_HOLD_CNF" },
128 #if defined(MNCC_HOLD_REJ)
129 { MNCC_HOLD_REJ, "MNCC_HOLD_REJ" },
131 #if defined(MNCC_RETRIEVE_IND)
132 { MNCC_RETRIEVE_IND, "MNCC_RETRIEVE_IND" },
134 #if defined(MNCC_RETRIEVE_CNF)
135 { MNCC_RETRIEVE_CNF, "MNCC_RETRIEVE_CNF" },
137 #if defined(MNCC_RETRIEVE_REJ)
138 { MNCC_RETRIEVE_REJ, "MNCC_RETRIEVE_REJ" },
140 #if defined(MNCC_USERINFO_REQ)
141 { MNCC_USERINFO_REQ, "MNCC_USERINFO_REQ" },
143 #if defined(MNCC_USERINFO_IND)
144 { MNCC_USERINFO_IND, "MNCC_USERINFO_IND" },
146 #if defined(MNCC_REJ_REQ)
147 { MNCC_REJ_REQ, "MNCC_REJ_REQ" },
149 #if defined(MNCC_REJ_IND)
150 { MNCC_REJ_IND, "MNCC_REJ_IND" },
152 #if defined(MNCC_PROGRESS_IND)
153 { MNCC_PROGRESS_IND, "MNCC_PROGRESS_IND" },
155 #if defined(MNCC_CALL_PROC_IND)
156 { MNCC_CALL_PROC_IND, "MNCC_CALL_PROC_IND" },
158 #if defined(MNCC_CALL_CONF_REQ)
159 { MNCC_CALL_CONF_REQ, "MNCC_CALL_CONF_REQ" },
161 #if defined(MNCC_START_DTMF_REQ)
162 { MNCC_START_DTMF_REQ, "MNCC_START_DTMF_REQ" },
164 #if defined(MNCC_STOP_DTMF_REQ)
165 { MNCC_STOP_DTMF_REQ, "MNCC_STOP_DTMF_REQ" },
167 #if defined(MNCC_HOLD_REQ)
168 { MNCC_HOLD_REQ, "MNCC_HOLD_REQ " },
170 #if defined(MNCC_RETRIEVE_REQ)
171 { MNCC_RETRIEVE_REQ, "MNCC_RETRIEVE_REQ" },
176 const char *mncc_name(int value)
180 while (mncc_names[i].name) {
181 if (mncc_names[i].msg_type == value)
182 return mncc_names[i].name;
189 * create and send mncc message
191 struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
193 struct gsm_mncc *mncc;
195 mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
196 mncc->msg_type = msg_type;
197 mncc->callref = callref;
200 int send_and_free_mncc(void *instance, unsigned int msg_type, void *data)
206 ret = mncc_send((struct gsm_network *)instance, msg_type, data);
209 ret = mncc_send((struct osmocom_ms *)instance, msg_type, data);
217 static int delete_event(struct lcr_work *work, void *instance, int index);
222 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)
224 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
225 memset(&p_m_g_delete, 0, sizeof(p_m_g_delete));
226 add_work(&p_m_g_delete, delete_event, this, 0);
227 p_m_g_instance = NULL;
230 p_m_g_gsm_b_sock = -1;
231 p_m_g_gsm_b_index = -1;
232 p_m_g_gsm_b_active = 0;
233 p_m_g_notify_pending = NULL;
234 p_m_g_decoder = gsm_audio_create();
235 p_m_g_encoder = gsm_audio_create();
236 if (!p_m_g_encoder || !p_m_g_decoder) {
237 PERROR("Failed to create GSM audio codec instance\n");
238 trigger_work(&p_m_g_delete);
241 p_m_g_tch_connected = 0;
243 PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
251 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
253 del_work(&p_m_g_delete);
255 /* remove queued message */
256 if (p_m_g_notify_pending)
257 message_free(p_m_g_notify_pending);
259 /* close audio transfer socket */
260 if (p_m_g_gsm_b_sock > -1)
265 gsm_audio_destroy(p_m_g_encoder);
267 gsm_audio_destroy(p_m_g_decoder);
271 /* close bsc side bchannel */
272 void Pgsm::bchannel_close(void)
274 if (p_m_g_gsm_b_sock > -1) {
275 unregister_fd(&p_m_g_gsm_b_fd);
276 close(p_m_g_gsm_b_sock);
278 p_m_g_gsm_b_sock = -1;
279 p_m_g_gsm_b_index = -1;
280 p_m_g_gsm_b_active = 0;
283 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index);
285 /* open external side bchannel */
286 int Pgsm::bchannel_open(int index)
289 struct sockaddr_mISDN addr;
290 struct mISDNhead act;
292 if (p_m_g_gsm_b_sock > -1) {
293 PERROR("Socket already created for index %d\n", index);
298 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
300 PERROR("Failed to open bchannel-socket for index %d\n", index);
304 memset(&p_m_g_gsm_b_fd, 0, sizeof(p_m_g_gsm_b_fd));
305 p_m_g_gsm_b_fd.fd = p_m_g_gsm_b_sock;
306 register_fd(&p_m_g_gsm_b_fd, LCR_FD_READ, b_handler, this, 0);
309 /* bind socket to bchannel */
310 addr.family = AF_ISDN;
311 addr.dev = mISDNloop.port;
312 addr.channel = index+1+(index>15);
313 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
315 PERROR("Failed to bind bchannel-socket for index %d\n", index);
319 /* activate bchannel */
320 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
321 act.prim = PH_ACTIVATE_REQ;
323 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
325 PERROR("Failed to activate index %d\n", index);
330 p_m_g_gsm_b_index = index;
335 /* receive from bchannel */
336 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
338 unsigned char frame[33];
340 /* encoder init failed */
344 /* (currently) not connected, so don't flood tch! */
345 if (!p_m_g_tch_connected)
348 /* write to rx buffer */
350 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
351 if (p_m_g_rxpos == 160) {
355 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
361 /* transmit to bchannel */
362 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
364 unsigned char buf[MISDN_HEADER_LEN+len];
365 struct mISDNhead *hh = (struct mISDNhead *)buf;
368 if (!p_m_g_gsm_b_active)
371 /* make and send frame */
372 hh->prim = PH_DATA_REQ;
374 memcpy(buf+MISDN_HEADER_LEN, data, len);
375 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
377 PERROR("Failed to send to socket index %d\n", p_m_g_gsm_b_index);
380 void Pgsm::frame_send(void *_frame)
382 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
383 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
385 frame->msg_type = GSM_TCHF_FRAME;
386 frame->callref = p_m_g_callref;
387 memcpy(frame->data, _frame, 33);
389 mncc_send((struct gsm_network *)p_m_g_instance, frame->msg_type, frame);
392 mncc_send((struct osmocom_ms *)p_m_g_instance, frame->msg_type, frame);
397 void Pgsm::frame_receive(void *arg)
399 struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
400 signed short samples[160];
401 unsigned char data[160];
407 if ((frame->data[0]>>4) != 0xd)
408 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
411 gsm_audio_decode(p_m_g_decoder, frame->data, samples);
412 for (i = 0; i < 160; i++) {
413 data[i] = audio_s16_to_law[samples[i] & 0xffff];
417 bchannel_send(PH_DATA_REQ, 0, data, 160);
424 void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
428 /* select message and primitive text */
429 SCPY(msgtext, mncc_name(msg_type));
433 switch(port->p_type) {
434 case PORT_TYPE_GSM_BS_OUT:
435 case PORT_TYPE_GSM_BS_IN:
436 SCAT(msgtext, " LCR<->BSC");
438 case PORT_TYPE_GSM_MS_OUT:
439 case PORT_TYPE_GSM_MS_IN:
440 SCAT(msgtext, " LCR<->MS");
444 SCAT(msgtext, " ----");
446 /* init trace with given values */
447 start_trace(mISDNport?mISDNport->portnum:-1,
448 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
449 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
450 port?port->p_dialinginfo.id:NULL,
453 port?port->p_serial:0,
457 /* select free bchannel from loopback interface */
458 int Pgsm::hunt_bchannel(void)
460 return loop_hunt_bchannel(this, p_m_mISDNport);
463 /* PROCEEDING INDICATION */
464 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
466 struct gsm_mncc *mode;
468 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
469 if (mncc->fields & MNCC_F_CAUSE) {
470 add_trace("cause", "coding", "%d", mncc->cause.coding);
471 add_trace("cause", "location", "%", mncc->cause.location);
472 add_trace("cause", "value", "%", mncc->cause.value);
476 /* modify lchan to GSM codec V1 */
477 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
478 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
479 mode->lchan_mode = 0x01; /* GSM V1 */
480 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
482 send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
486 /* CALL PROCEEDING INDICATION */
487 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
489 struct lcr_msg *message;
490 struct gsm_mncc *frame;
492 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
495 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
496 message_put(message);
498 new_state(PORT_STATE_OUT_PROCEEDING);
500 if (p_m_mISDNport->earlyb && !p_m_g_tch_connected) { /* only if ... */
501 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
503 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
504 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
505 p_m_g_tch_connected = 1;
509 /* ALERTING INDICATION */
510 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
512 struct lcr_msg *message;
513 struct gsm_mncc *frame;
515 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
518 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
519 message_put(message);
521 new_state(PORT_STATE_OUT_ALERTING);
523 if (p_m_mISDNport->earlyb && !p_m_g_tch_connected) { /* only if ... */
524 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
526 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
527 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
528 p_m_g_tch_connected = 1;
532 /* CONNECT INDICATION */
533 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
535 struct gsm_mncc *resp, *frame;
536 struct lcr_msg *message;
538 SCPY(p_connectinfo.id, mncc->connected.number);
539 SCPY(p_connectinfo.imsi, mncc->imsi);
540 p_connectinfo.present = INFO_PRESENT_ALLOWED;
541 p_connectinfo.screen = INFO_SCREEN_NETWORK;
542 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
543 p_connectinfo.isdn_port = p_m_portnum;
544 SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
546 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
547 if (p_connectinfo.id[0])
548 add_trace("connect", "number", "%s", p_connectinfo.id);
549 else if (mncc->imsi[0])
550 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
552 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
556 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
557 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
559 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
561 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
562 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
563 message_put(message);
565 new_state(PORT_STATE_CONNECT);
567 if (!p_m_g_tch_connected) { /* only if ... */
568 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
570 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
571 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
572 p_m_g_tch_connected = 1;
576 /* CONNECT ACK INDICATION */
577 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
579 struct gsm_mncc *frame;
581 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
584 new_state(PORT_STATE_CONNECT);
586 if (!p_m_g_tch_connected) { /* only if ... */
587 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
589 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
590 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
591 p_m_g_tch_connected = 1;
595 /* DISCONNECT INDICATION */
596 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
598 struct lcr_msg *message;
599 int cause = 16, location = 0;
600 struct gsm_mncc *resp;
602 gsm_trace_header(p_m_mISDNport, 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", location);
608 add_trace("cause", "value", "%d", cause);
613 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
614 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
616 resp->fields |= MNCC_F_CAUSE;
617 resp->cause.coding = 3;
618 resp->cause.location = 1;
619 resp->cause.value = cause;
620 add_trace("cause", "coding", "%d", resp->cause.coding);
621 add_trace("cause", "location", "%d", resp->cause.location);
622 add_trace("cause", "value", "%d", resp->cause.value);
625 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
627 /* sending release to endpoint */
628 while(p_epointlist) {
629 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
630 message->param.disconnectinfo.cause = cause;
631 message->param.disconnectinfo.location = location;
632 message_put(message);
634 free_epointlist(p_epointlist);
636 new_state(PORT_STATE_RELEASE);
637 trigger_work(&p_m_g_delete);
640 /* CC_RELEASE INDICATION */
641 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
643 int location = 0, cause = 16;
644 struct lcr_msg *message;
646 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
647 if (mncc->fields & MNCC_F_CAUSE) {
648 location = mncc->cause.location;
649 cause = mncc->cause.value;
650 add_trace("cause", "coding", "%d", mncc->cause.coding);
651 add_trace("cause", "location", "%d", mncc->cause.location);
652 add_trace("cause", "value", "%d", mncc->cause.value);
656 /* sending release to endpoint */
657 while(p_epointlist) {
658 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
659 message->param.disconnectinfo.cause = cause;
660 message->param.disconnectinfo.location = location;
661 message_put(message);
663 free_epointlist(p_epointlist);
665 new_state(PORT_STATE_RELEASE);
666 trigger_work(&p_m_g_delete);
669 /* NOTIFY INDICATION */
670 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
672 struct lcr_msg *message;
674 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
675 add_trace("notify", NULL, "%d", mncc->notify);
678 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
679 message->param.notifyinfo.notify = mncc->notify;
680 message_put(message);
684 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
686 struct gsm_mncc *mncc;
689 // printf("if = %d\n", param->notifyinfo.notify);
690 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
691 notify = param->notifyinfo.notify & 0x7f;
692 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
693 /* queue notification */
694 if (p_m_g_notify_pending)
695 message_free(p_m_g_notify_pending);
696 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
697 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
699 /* sending notification */
700 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
701 add_trace("notify", NULL, "%d", notify);
703 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
704 mncc->notify = notify;
705 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
710 /* MESSAGE_ALERTING */
711 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
713 struct gsm_mncc *mncc;
716 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
717 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
718 if (p_m_mISDNport->tones) {
719 mncc->fields |= MNCC_F_PROGRESS;
720 mncc->progress.coding = 3; /* GSM */
721 mncc->progress.location = 1;
722 mncc->progress.descr = 8;
723 add_trace("progress", "coding", "%d", mncc->progress.coding);
724 add_trace("progress", "location", "%d", mncc->progress.location);
725 add_trace("progress", "descr", "%d", mncc->progress.descr);
728 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
730 new_state(PORT_STATE_IN_ALERTING);
732 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
733 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
735 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
736 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
737 p_m_g_tch_connected = 1;
741 /* MESSAGE_CONNECT */
742 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
744 struct gsm_mncc *mncc;
746 /* copy connected information */
747 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
748 /* screen outgoing caller id */
749 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
752 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
753 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
754 /* caller information */
755 mncc->fields |= MNCC_F_CONNECTED;
756 mncc->connected.plan = 1;
757 switch (p_callerinfo.ntype) {
758 case INFO_NTYPE_UNKNOWN:
759 mncc->connected.type = 0x0;
761 case INFO_NTYPE_INTERNATIONAL:
762 mncc->connected.type = 0x1;
764 case INFO_NTYPE_NATIONAL:
765 mncc->connected.type = 0x2;
767 case INFO_NTYPE_SUBSCRIBER:
768 mncc->connected.type = 0x4;
770 default: /* INFO_NTYPE_NOTPRESENT */
771 mncc->fields &= ~MNCC_F_CONNECTED;
774 switch (p_callerinfo.screen) {
775 case INFO_SCREEN_USER:
776 mncc->connected.screen = 0;
778 default: /* INFO_SCREEN_NETWORK */
779 mncc->connected.screen = 3;
782 switch (p_callerinfo.present) {
783 case INFO_PRESENT_ALLOWED:
784 mncc->connected.present = 0;
786 case INFO_PRESENT_RESTRICTED:
787 mncc->connected.present = 1;
789 default: /* INFO_PRESENT_NOTAVAIL */
790 mncc->connected.present = 2;
793 if (mncc->fields & MNCC_F_CONNECTED) {
794 SCPY(mncc->connected.number, p_connectinfo.id);
795 add_trace("connected", "type", "%d", mncc->connected.type);
796 add_trace("connected", "plan", "%d", mncc->connected.plan);
797 add_trace("connected", "present", "%d", mncc->connected.present);
798 add_trace("connected", "screen", "%d", mncc->connected.screen);
799 add_trace("connected", "number", "%s", mncc->connected.number);
802 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
804 new_state(PORT_STATE_CONNECT_WAITING);
807 /* MESSAGE_DISCONNECT */
808 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
810 struct gsm_mncc *mncc;
812 /* send disconnect */
813 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
814 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
815 if (p_m_mISDNport->tones) {
816 mncc->fields |= MNCC_F_PROGRESS;
817 mncc->progress.coding = 3; /* GSM */
818 mncc->progress.location = 1;
819 mncc->progress.descr = 8;
820 add_trace("progress", "coding", "%d", mncc->progress.coding);
821 add_trace("progress", "location", "%d", mncc->progress.location);
822 add_trace("progress", "descr", "%d", mncc->progress.descr);
824 mncc->fields |= MNCC_F_CAUSE;
825 mncc->cause.coding = 3;
826 mncc->cause.location = param->disconnectinfo.location;
827 mncc->cause.value = param->disconnectinfo.cause;
828 add_trace("cause", "coding", "%d", mncc->cause.coding);
829 add_trace("cause", "location", "%d", mncc->cause.location);
830 add_trace("cause", "value", "%d", mncc->cause.value);
832 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
834 new_state(PORT_STATE_OUT_DISCONNECT);
836 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
837 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
839 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
840 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
841 p_m_g_tch_connected = 1;
846 /* MESSAGE_RELEASE */
847 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
849 struct gsm_mncc *mncc;
852 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
853 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
854 mncc->fields |= MNCC_F_CAUSE;
855 mncc->cause.coding = 3;
856 mncc->cause.location = param->disconnectinfo.location;
857 mncc->cause.value = param->disconnectinfo.cause;
858 add_trace("cause", "coding", "%d", mncc->cause.coding);
859 add_trace("cause", "location", "%d", mncc->cause.location);
860 add_trace("cause", "value", "%d", mncc->cause.value);
862 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
864 new_state(PORT_STATE_RELEASE);
865 trigger_work(&p_m_g_delete);
870 * endpoint sends messages to the port
872 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
874 if (PmISDN::message_epoint(epoint_id, message_id, param))
878 case MESSAGE_NOTIFY: /* display and notifications */
879 message_notify(epoint_id, message_id, param);
882 // case MESSAGE_FACILITY: /* facility message */
883 // message_facility(epoint_id, message_id, param);
886 case MESSAGE_PROCEEDING: /* message not handles */
889 case MESSAGE_ALERTING: /* call of endpoint is ringing */
890 if (p_state!=PORT_STATE_IN_PROCEEDING)
892 message_alerting(epoint_id, message_id, param);
893 if (p_m_g_notify_pending) {
894 /* send pending notify message during connect */
895 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
896 message_free(p_m_g_notify_pending);
897 p_m_g_notify_pending = NULL;
901 case MESSAGE_CONNECT: /* call of endpoint is connected */
902 if (p_state!=PORT_STATE_IN_PROCEEDING
903 && p_state!=PORT_STATE_IN_ALERTING)
905 message_connect(epoint_id, message_id, param);
906 if (p_m_g_notify_pending) {
907 /* send pending notify message during connect */
908 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
909 message_free(p_m_g_notify_pending);
910 p_m_g_notify_pending = NULL;
914 case MESSAGE_DISCONNECT: /* call has been disconnected */
915 if (p_state!=PORT_STATE_IN_PROCEEDING
916 && p_state!=PORT_STATE_IN_ALERTING
917 && p_state!=PORT_STATE_OUT_SETUP
918 && p_state!=PORT_STATE_OUT_OVERLAP
919 && p_state!=PORT_STATE_OUT_PROCEEDING
920 && p_state!=PORT_STATE_OUT_ALERTING
921 && p_state!=PORT_STATE_CONNECT
922 && p_state!=PORT_STATE_CONNECT_WAITING)
924 message_disconnect(epoint_id, message_id, param);
927 case MESSAGE_RELEASE: /* release isdn port */
928 if (p_state==PORT_STATE_RELEASE)
930 message_release(epoint_id, message_id, param);
938 /* deletes only if l3id is release, otherwhise it will be triggered then */
939 static int delete_event(struct lcr_work *work, void *instance, int index)
941 class Pgsm *gsmport = (class Pgsm *)instance;
949 * handler of bchannel events
951 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index)
953 class Pgsm *gsmport = (class Pgsm *)instance;
955 unsigned char buffer[2048+MISDN_HEADER_LEN];
956 struct mISDNhead *hh = (struct mISDNhead *)buffer;
958 /* handle message from bchannel */
959 if (gsmport->p_m_g_gsm_b_sock > -1) {
960 ret = recv(gsmport->p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
961 if (ret >= (int)MISDN_HEADER_LEN) {
963 /* we don't care about confirms, we use rx data to sync tx */
966 /* we receive audio data, we respond to it AND we send tones */
968 gsmport->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
970 case PH_ACTIVATE_IND:
971 gsmport->p_m_g_gsm_b_active = 1;
973 case PH_DEACTIVATE_IND:
974 gsmport->p_m_g_gsm_b_active = 0;
978 if (ret < 0 && errno != EWOULDBLOCK)
979 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
988 /* free gsm instance */
1002 /* create gsm instance */
1003 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1008 int handle_gsm(void)