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)
205 ret = mncc_send((struct gsm_network *)instance, msg_type, data);
208 ret = mncc_send((struct osmocom_ms *)instance, msg_type, data);
215 static int delete_event(struct lcr_work *work, void *instance, int index);
220 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)
222 p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
223 memset(&p_m_g_delete, 0, sizeof(p_m_g_delete));
224 add_work(&p_m_g_delete, delete_event, this, 0);
225 p_m_g_instance = NULL;
228 p_m_g_gsm_b_sock = -1;
229 p_m_g_gsm_b_index = -1;
230 p_m_g_gsm_b_active = 0;
231 p_m_g_notify_pending = NULL;
232 p_m_g_decoder = gsm_audio_create();
233 p_m_g_encoder = gsm_audio_create();
234 if (!p_m_g_encoder || !p_m_g_decoder) {
235 PERROR("Failed to create GSM audio codec instance\n");
236 trigger_work(&p_m_g_delete);
239 p_m_g_tch_connected = 0;
241 PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
249 PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
251 del_work(&p_m_g_delete);
253 /* remove queued message */
254 if (p_m_g_notify_pending)
255 message_free(p_m_g_notify_pending);
257 /* close audio transfer socket */
258 if (p_m_g_gsm_b_sock > -1)
263 gsm_audio_destroy(p_m_g_encoder);
265 gsm_audio_destroy(p_m_g_decoder);
269 /* close bsc side bchannel */
270 void Pgsm::bchannel_close(void)
272 if (p_m_g_gsm_b_sock > -1) {
273 unregister_fd(&p_m_g_gsm_b_fd);
274 close(p_m_g_gsm_b_sock);
276 p_m_g_gsm_b_sock = -1;
277 p_m_g_gsm_b_index = -1;
278 p_m_g_gsm_b_active = 0;
281 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index);
283 /* open external side bchannel */
284 int Pgsm::bchannel_open(int index)
287 struct sockaddr_mISDN addr;
288 struct mISDNhead act;
290 if (p_m_g_gsm_b_sock > -1) {
291 PERROR("Socket already created for index %d\n", index);
296 ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
298 PERROR("Failed to open bchannel-socket for index %d\n", index);
302 memset(&p_m_g_gsm_b_fd, 0, sizeof(p_m_g_gsm_b_fd));
303 p_m_g_gsm_b_fd.fd = p_m_g_gsm_b_sock;
304 register_fd(&p_m_g_gsm_b_fd, LCR_FD_READ, b_handler, this, 0);
307 /* bind socket to bchannel */
308 addr.family = AF_ISDN;
309 addr.dev = mISDNloop.port;
310 addr.channel = index+1+(index>15);
311 ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
313 PERROR("Failed to bind bchannel-socket for index %d\n", index);
317 /* activate bchannel */
318 PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
319 act.prim = PH_ACTIVATE_REQ;
321 ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
323 PERROR("Failed to activate index %d\n", index);
328 p_m_g_gsm_b_index = index;
333 /* receive from bchannel */
334 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
336 unsigned char frame[33];
338 /* encoder init failed */
342 /* (currently) not connected, so don't flood tch! */
343 if (!p_m_g_tch_connected)
346 /* write to rx buffer */
348 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
349 if (p_m_g_rxpos == 160) {
353 gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
359 /* transmit to bchannel */
360 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
362 unsigned char buf[MISDN_HEADER_LEN+len];
363 struct mISDNhead *hh = (struct mISDNhead *)buf;
366 if (!p_m_g_gsm_b_active)
369 /* make and send frame */
370 hh->prim = PH_DATA_REQ;
372 memcpy(buf+MISDN_HEADER_LEN, data, len);
373 ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
375 PERROR("Failed to send to socket index %d\n", p_m_g_gsm_b_index);
378 void Pgsm::frame_send(void *_frame)
380 unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
381 struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
383 frame->msg_type = GSM_TCHF_FRAME;
384 frame->callref = p_m_g_callref;
385 memcpy(frame->data, _frame, 33);
387 mncc_send((struct gsm_network *)p_m_g_instance, frame->msg_type, frame);
390 mncc_send((struct osmocom_ms *)p_m_g_instance, frame->msg_type, frame);
395 void Pgsm::frame_receive(void *arg)
397 struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
398 signed short samples[160];
399 unsigned char data[160];
405 if ((frame->data[0]>>4) != 0xd)
406 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
409 gsm_audio_decode(p_m_g_decoder, frame->data, samples);
410 for (i = 0; i < 160; i++) {
411 data[i] = audio_s16_to_law[samples[i] & 0xffff];
415 bchannel_send(PH_DATA_REQ, 0, data, 160);
422 void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
426 /* select message and primitive text */
427 SCPY(msgtext, mncc_name(msg_type));
431 switch(port->p_type) {
432 case PORT_TYPE_GSM_BS_OUT:
433 SCAT(msgtext, " LCR->BSC");
435 case PORT_TYPE_GSM_BS_IN:
436 SCAT(msgtext, " LCR<-BSC");
438 case PORT_TYPE_GSM_MS_OUT:
439 SCAT(msgtext, " LCR->MS");
441 case PORT_TYPE_GSM_MS_IN:
442 SCAT(msgtext, " LCR<-MS");
446 SCAT(msgtext, " ----");
448 /* init trace with given values */
449 start_trace(mISDNport?mISDNport->portnum:-1,
450 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
451 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
452 port?port->p_dialinginfo.id:NULL,
455 port?port->p_serial:0,
459 /* select free bchannel from loopback interface */
460 int Pgsm::hunt_bchannel(void)
462 return loop_hunt_bchannel(this, p_m_mISDNport);
465 /* PROCEEDING INDICATION */
466 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
468 struct gsm_mncc *mode;
470 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
471 if (mncc->fields & MNCC_F_CAUSE) {
472 add_trace("cause", "coding", "%d", mncc->cause.coding);
473 add_trace("cause", "location", "%", mncc->cause.location);
474 add_trace("cause", "value", "%", mncc->cause.value);
478 /* modify lchan to GSM codec V1 */
479 gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
480 mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
481 mode->lchan_mode = 0x01; /* GSM V1 */
482 add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
484 send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
488 /* CALL PROCEEDING INDICATION */
489 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
491 struct lcr_msg *message;
492 struct gsm_mncc *frame;
494 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
497 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
498 message_put(message);
500 new_state(PORT_STATE_OUT_PROCEEDING);
502 if (p_m_mISDNport->earlyb && !p_m_g_tch_connected) { /* only if ... */
503 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
505 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
506 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
507 p_m_g_tch_connected = 1;
511 /* ALERTING INDICATION */
512 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
514 struct lcr_msg *message;
515 struct gsm_mncc *frame;
517 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
520 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
521 message_put(message);
523 new_state(PORT_STATE_OUT_ALERTING);
525 if (p_m_mISDNport->earlyb && !p_m_g_tch_connected) { /* only if ... */
526 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
528 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
529 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
530 p_m_g_tch_connected = 1;
534 /* CONNECT INDICATION */
535 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
537 struct gsm_mncc *resp, *frame;
538 struct lcr_msg *message;
540 SCPY(p_connectinfo.id, mncc->connected.number);
541 SCPY(p_connectinfo.imsi, mncc->imsi);
542 p_connectinfo.present = INFO_PRESENT_ALLOWED;
543 p_connectinfo.screen = INFO_SCREEN_NETWORK;
544 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
545 p_connectinfo.isdn_port = p_m_portnum;
546 SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
548 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
549 if (p_connectinfo.id[0])
550 add_trace("connect", "number", "%s", p_connectinfo.id);
551 else if (mncc->imsi[0])
552 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
554 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
558 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
559 resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
561 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
563 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
564 memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
565 message_put(message);
567 new_state(PORT_STATE_CONNECT);
569 if (!p_m_g_tch_connected) { /* only if ... */
570 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
572 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
573 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
574 p_m_g_tch_connected = 1;
578 /* CONNECT ACK INDICATION */
579 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
581 struct gsm_mncc *frame;
583 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
586 new_state(PORT_STATE_CONNECT);
588 if (!p_m_g_tch_connected) { /* only if ... */
589 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
591 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
592 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
593 p_m_g_tch_connected = 1;
597 /* DISCONNECT INDICATION */
598 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
600 struct lcr_msg *message;
601 int cause = 16, location = 0;
602 struct gsm_mncc *resp;
604 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
605 if (mncc->fields & MNCC_F_CAUSE) {
606 location = mncc->cause.location;
607 cause = mncc->cause.value;
608 add_trace("cause", "coding", "%d", mncc->cause.coding);
609 add_trace("cause", "location", "%d", location);
610 add_trace("cause", "value", "%d", cause);
615 resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
616 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
618 resp->fields |= MNCC_F_CAUSE;
619 resp->cause.coding = 3;
620 resp->cause.location = 1;
621 resp->cause.value = cause;
622 add_trace("cause", "coding", "%d", resp->cause.coding);
623 add_trace("cause", "location", "%d", resp->cause.location);
624 add_trace("cause", "value", "%d", resp->cause.value);
627 send_and_free_mncc(p_m_g_instance, resp->msg_type, resp);
629 /* sending release to endpoint */
630 while(p_epointlist) {
631 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
632 message->param.disconnectinfo.cause = cause;
633 message->param.disconnectinfo.location = location;
634 message_put(message);
636 free_epointlist(p_epointlist);
638 new_state(PORT_STATE_RELEASE);
639 trigger_work(&p_m_g_delete);
642 /* CC_RELEASE INDICATION */
643 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
645 int location = 0, cause = 16;
646 struct lcr_msg *message;
648 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
649 if (mncc->fields & MNCC_F_CAUSE) {
650 location = mncc->cause.location;
651 cause = mncc->cause.value;
652 add_trace("cause", "coding", "%d", mncc->cause.coding);
653 add_trace("cause", "location", "%d", mncc->cause.location);
654 add_trace("cause", "value", "%d", mncc->cause.value);
658 /* sending release to endpoint */
659 while(p_epointlist) {
660 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
661 message->param.disconnectinfo.cause = cause;
662 message->param.disconnectinfo.location = location;
663 message_put(message);
665 free_epointlist(p_epointlist);
667 new_state(PORT_STATE_RELEASE);
668 trigger_work(&p_m_g_delete);
671 /* NOTIFY INDICATION */
672 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
674 struct lcr_msg *message;
676 gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
677 add_trace("notify", NULL, "%d", mncc->notify);
680 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
681 message->param.notifyinfo.notify = mncc->notify;
682 message_put(message);
686 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
688 struct gsm_mncc *mncc;
691 // printf("if = %d\n", param->notifyinfo.notify);
692 if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
693 notify = param->notifyinfo.notify & 0x7f;
694 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
695 /* queue notification */
696 if (p_m_g_notify_pending)
697 message_free(p_m_g_notify_pending);
698 p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
699 memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
701 /* sending notification */
702 gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
703 add_trace("notify", NULL, "%d", notify);
705 mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
706 mncc->notify = notify;
707 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
712 /* MESSAGE_ALERTING */
713 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
715 struct gsm_mncc *mncc;
718 gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
719 mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
720 if (p_m_mISDNport->tones) {
721 mncc->fields |= MNCC_F_PROGRESS;
722 mncc->progress.coding = 3; /* GSM */
723 mncc->progress.location = 1;
724 mncc->progress.descr = 8;
725 add_trace("progress", "coding", "%d", mncc->progress.coding);
726 add_trace("progress", "location", "%d", mncc->progress.location);
727 add_trace("progress", "descr", "%d", mncc->progress.descr);
730 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
732 new_state(PORT_STATE_IN_ALERTING);
734 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
735 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
737 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
738 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
739 p_m_g_tch_connected = 1;
743 /* MESSAGE_CONNECT */
744 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
746 struct gsm_mncc *mncc;
748 /* copy connected information */
749 memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
750 /* screen outgoing caller id */
751 do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
754 mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
755 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
756 /* caller information */
757 mncc->fields |= MNCC_F_CONNECTED;
758 mncc->connected.plan = 1;
759 switch (p_callerinfo.ntype) {
760 case INFO_NTYPE_UNKNOWN:
761 mncc->connected.type = 0x0;
763 case INFO_NTYPE_INTERNATIONAL:
764 mncc->connected.type = 0x1;
766 case INFO_NTYPE_NATIONAL:
767 mncc->connected.type = 0x2;
769 case INFO_NTYPE_SUBSCRIBER:
770 mncc->connected.type = 0x4;
772 default: /* INFO_NTYPE_NOTPRESENT */
773 mncc->fields &= ~MNCC_F_CONNECTED;
776 switch (p_callerinfo.screen) {
777 case INFO_SCREEN_USER:
778 mncc->connected.screen = 0;
780 default: /* INFO_SCREEN_NETWORK */
781 mncc->connected.screen = 3;
784 switch (p_callerinfo.present) {
785 case INFO_PRESENT_ALLOWED:
786 mncc->connected.present = 0;
788 case INFO_PRESENT_RESTRICTED:
789 mncc->connected.present = 1;
791 default: /* INFO_PRESENT_NOTAVAIL */
792 mncc->connected.present = 2;
795 if (mncc->fields & MNCC_F_CONNECTED) {
796 SCPY(mncc->connected.number, p_connectinfo.id);
797 add_trace("connected", "type", "%d", mncc->connected.type);
798 add_trace("connected", "plan", "%d", mncc->connected.plan);
799 add_trace("connected", "present", "%d", mncc->connected.present);
800 add_trace("connected", "screen", "%d", mncc->connected.screen);
801 add_trace("connected", "number", "%s", mncc->connected.number);
804 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
806 new_state(PORT_STATE_CONNECT_WAITING);
809 /* MESSAGE_DISCONNECT */
810 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
812 struct gsm_mncc *mncc;
814 /* send disconnect */
815 mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
816 gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
817 if (p_m_mISDNport->tones) {
818 mncc->fields |= MNCC_F_PROGRESS;
819 mncc->progress.coding = 3; /* GSM */
820 mncc->progress.location = 1;
821 mncc->progress.descr = 8;
822 add_trace("progress", "coding", "%d", mncc->progress.coding);
823 add_trace("progress", "location", "%d", mncc->progress.location);
824 add_trace("progress", "descr", "%d", mncc->progress.descr);
826 mncc->fields |= MNCC_F_CAUSE;
827 mncc->cause.coding = 3;
828 mncc->cause.location = param->disconnectinfo.location;
829 mncc->cause.value = param->disconnectinfo.cause;
830 add_trace("cause", "coding", "%d", mncc->cause.coding);
831 add_trace("cause", "location", "%d", mncc->cause.location);
832 add_trace("cause", "value", "%d", mncc->cause.value);
834 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
836 new_state(PORT_STATE_OUT_DISCONNECT);
838 if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
839 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
841 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
842 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
843 p_m_g_tch_connected = 1;
848 /* MESSAGE_RELEASE */
849 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
851 struct gsm_mncc *mncc;
854 mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
855 gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
856 mncc->fields |= MNCC_F_CAUSE;
857 mncc->cause.coding = 3;
858 mncc->cause.location = param->disconnectinfo.location;
859 mncc->cause.value = param->disconnectinfo.cause;
860 add_trace("cause", "coding", "%d", mncc->cause.coding);
861 add_trace("cause", "location", "%d", mncc->cause.location);
862 add_trace("cause", "value", "%d", mncc->cause.value);
864 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
866 new_state(PORT_STATE_RELEASE);
867 trigger_work(&p_m_g_delete);
872 * endpoint sends messages to the port
874 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
876 if (PmISDN::message_epoint(epoint_id, message_id, param))
880 case MESSAGE_NOTIFY: /* display and notifications */
881 message_notify(epoint_id, message_id, param);
884 // case MESSAGE_FACILITY: /* facility message */
885 // message_facility(epoint_id, message_id, param);
888 case MESSAGE_PROCEEDING: /* message not handles */
891 case MESSAGE_ALERTING: /* call of endpoint is ringing */
892 if (p_state!=PORT_STATE_IN_PROCEEDING)
894 message_alerting(epoint_id, message_id, param);
895 if (p_m_g_notify_pending) {
896 /* send pending notify message during connect */
897 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
898 message_free(p_m_g_notify_pending);
899 p_m_g_notify_pending = NULL;
903 case MESSAGE_CONNECT: /* call of endpoint is connected */
904 if (p_state!=PORT_STATE_IN_PROCEEDING
905 && p_state!=PORT_STATE_IN_ALERTING)
907 message_connect(epoint_id, message_id, param);
908 if (p_m_g_notify_pending) {
909 /* send pending notify message during connect */
910 message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
911 message_free(p_m_g_notify_pending);
912 p_m_g_notify_pending = NULL;
916 case MESSAGE_DISCONNECT: /* call has been disconnected */
917 if (p_state!=PORT_STATE_IN_PROCEEDING
918 && p_state!=PORT_STATE_IN_ALERTING
919 && p_state!=PORT_STATE_OUT_SETUP
920 && p_state!=PORT_STATE_OUT_OVERLAP
921 && p_state!=PORT_STATE_OUT_PROCEEDING
922 && p_state!=PORT_STATE_OUT_ALERTING
923 && p_state!=PORT_STATE_CONNECT
924 && p_state!=PORT_STATE_CONNECT_WAITING)
926 message_disconnect(epoint_id, message_id, param);
929 case MESSAGE_RELEASE: /* release isdn port */
930 if (p_state==PORT_STATE_RELEASE)
932 message_release(epoint_id, message_id, param);
940 /* deletes only if l3id is release, otherwhise it will be triggered then */
941 static int delete_event(struct lcr_work *work, void *instance, int index)
943 class Pgsm *gsmport = (class Pgsm *)instance;
951 * handler of bchannel events
953 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index)
955 class Pgsm *gsmport = (class Pgsm *)instance;
957 unsigned char buffer[2048+MISDN_HEADER_LEN];
958 struct mISDNhead *hh = (struct mISDNhead *)buffer;
960 /* handle message from bchannel */
961 if (gsmport->p_m_g_gsm_b_sock > -1) {
962 ret = recv(gsmport->p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
963 if (ret >= (int)MISDN_HEADER_LEN) {
965 /* we don't care about confirms, we use rx data to sync tx */
968 /* we receive audio data, we respond to it AND we send tones */
970 gsmport->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
972 case PH_ACTIVATE_IND:
973 gsmport->p_m_g_gsm_b_active = 1;
975 case PH_DEACTIVATE_IND:
976 gsmport->p_m_g_gsm_b_active = 0;
980 if (ret < 0 && errno != EWOULDBLOCK)
981 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
990 /* free gsm instance */
1004 /* create gsm instance */
1005 gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1010 int handle_gsm(void)