Adding handling of bad GSM audio frames
[lcr.git] / gsm.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **            MNCC-Interface: Harald Welte                                   **
8 **                                                                           **
9 ** mISDN gsm                                                                 **
10 **                                                                           **
11 \*****************************************************************************/ 
12
13 #include "main.h"
14 #include "mncc.h"
15
16 #ifndef _GNU_SOURCE
17 #define _GNU_SOURCE
18 #endif
19 extern "C" {
20 #include "gsm_audio.h"
21 }
22
23 #include <mISDN/mISDNcompat.h>
24 #include <assert.h>
25
26 #define SOCKET_RETRY_TIMER      5
27
28 //struct lcr_gsm *gsm = NULL;
29
30 int new_callref = 1;
31
32 /* names of MNCC-SAP */
33 static const struct _value_string {
34         int msg_type;
35         const char *name;
36 } mncc_names[] = {
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" },
85         { 0,                    NULL }
86 };
87
88 const char *mncc_name(int value)
89 {
90         int i = 0;
91
92         while (mncc_names[i].name) {
93                 if (mncc_names[i].msg_type == value)
94                         return mncc_names[i].name;
95                 i++;
96         }
97         return "unknown";
98 }
99
100 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data);
101
102 /*
103  * create and send mncc message
104  */
105 struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
106 {
107         struct gsm_mncc *mncc;
108
109         mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
110         mncc->msg_type = msg_type;
111         mncc->callref = callref;
112         return (mncc);
113 }
114 int send_and_free_mncc(struct lcr_gsm *lcr_gsm, unsigned int msg_type, void *data)
115 {
116         int ret = 0;
117
118         if (lcr_gsm) {
119                 ret = mncc_send(lcr_gsm, msg_type, data);
120         }
121         free(data);
122
123         return ret;
124 }
125
126 static int delete_event(struct lcr_work *work, void *instance, int index);
127
128 /*
129  * constructor
130  */
131 Pgsm::Pgsm(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings)
132 {
133         p_g_tones = 0;
134         if (interface->is_tones == IS_YES)
135                 p_g_tones = 1;
136         p_g_earlyb = 0;
137         if (interface->is_earlyb == IS_YES)
138                 p_g_earlyb = 1;
139         p_g_rtp_bridge = 0;
140         if (interface->rtp_bridge)
141                 p_g_rtp_bridge = 1;
142         memset(&p_g_samples, 0, sizeof(p_g_samples));
143         SCPY(p_g_interface_name, interface->name); 
144         p_callerinfo.itype = (interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
145         memset(&p_g_delete, 0, sizeof(p_g_delete));
146         add_work(&p_g_delete, delete_event, this, 0);
147         p_g_lcr_gsm = NULL;
148         p_g_callref = 0;
149         p_g_mode = 0;
150         p_g_gsm_b_sock = -1;
151         p_g_gsm_b_index = -1;
152         p_g_gsm_b_active = 0;
153         p_g_notify_pending = NULL;
154         p_g_setup_pending = NULL;
155         p_g_connect_pending = NULL;
156         p_g_decoder = gsm_audio_create();
157         p_g_encoder = gsm_audio_create();
158         if (!p_g_encoder || !p_g_decoder) {
159                 PERROR("Failed to create GSM audio codec instance\n");
160                 trigger_work(&p_g_delete);
161         }
162         p_g_rxpos = 0;
163         p_g_tch_connected = 0;
164
165         PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
166 }
167
168 /*
169  * destructor
170  */
171 Pgsm::~Pgsm()
172 {
173         PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
174
175         del_work(&p_g_delete);
176
177         /* remove queued message */
178         if (p_g_notify_pending)
179                 message_free(p_g_notify_pending);
180         if (p_g_setup_pending)
181                 message_free(p_g_setup_pending);
182         if (p_g_connect_pending)
183                 message_free(p_g_connect_pending);
184
185         /* close codec */
186         if (p_g_encoder)
187                 gsm_audio_destroy(p_g_encoder);
188         if (p_g_decoder)
189                 gsm_audio_destroy(p_g_decoder);
190 }
191
192
193 /* receive encoded frame from gsm */
194 void Pgsm::frame_receive(void *arg)
195 {
196         struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
197         unsigned char data[160];
198         int i;
199
200         if (!p_g_decoder)
201                 return;
202
203         if (frame->msg_type != GSM_TCHF_BAD_FRAME) {
204                 if ((frame->data[0]>>4) != 0xd)
205                         PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
206         
207                 /* decode */
208                 gsm_audio_decode(p_g_decoder, frame->data, p_g_samples);
209                 for (i = 0; i < 160; i++) {
210                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
211                 }
212         } else if (p_echotest) {
213                 /* beep on bad frame */
214                 for (i = 0; i < 160; i++) {
215                         if ((i & 3) > 2)
216                                 p_g_samples[i] = 15000;
217                         else
218                                 p_g_samples[i] = -15000;
219                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
220                 }
221         } else {
222                 /* repeat on bad frame */
223                 for (i = 0; i < 160; i++) {
224                         p_g_samples[i] = (p_g_samples[i] * 14) >> 4;
225                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
226                 }
227         }
228
229         /* local echo */
230         if (p_echotest)
231                 bridge_rx(data, 160);
232
233         /* send to remote*/
234         bridge_tx(data, 160);
235 }
236
237 /* send traffic to gsm */
238 int Pgsm::bridge_rx(unsigned char *data, int len)
239 {
240         unsigned char frame[33];
241
242         /* encoder init failed */
243         if (!p_g_encoder)
244                 return -EINVAL;
245
246         /* (currently) not connected, so don't flood tch! */
247         if (!p_g_tch_connected)
248                 return -EINVAL;
249
250         /* write to rx buffer */
251         while(len--) {
252                 p_g_rxdata[p_g_rxpos++] = audio_law_to_s32[*data++];
253                 if (p_g_rxpos == 160) {
254                         p_g_rxpos = 0;
255
256                         /* encode data */
257                         gsm_audio_encode(p_g_encoder, p_g_rxdata, frame);
258                         frame_send(frame);
259                 }
260         }
261
262         return 0;
263 }
264
265 void Pgsm::frame_send(void *_frame)
266 {
267         unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
268         struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
269         
270         frame->msg_type = GSM_TCHF_FRAME;
271         frame->callref = p_g_callref;
272         memcpy(frame->data, _frame, 33);
273
274         if (p_g_lcr_gsm) {
275                 mncc_send(p_g_lcr_gsm, frame->msg_type, frame);
276         }
277 }
278
279 /*
280  * create trace
281  */
282 void gsm_trace_header(const char *interface_name, class Pgsm *port, unsigned int msg_type, int direction)
283 {
284         char msgtext[64];
285         struct interface *interface = interface_first;
286
287         interface = getinterfacebyname(interface_name);
288         if (!interface)
289                 return;
290
291         /* select message and primitive text */
292         SCPY(msgtext, mncc_name(msg_type));
293
294         /* add direction */
295         if (port) {
296                 switch(port->p_type) {
297                 case PORT_TYPE_GSM_BS_OUT:
298                 case PORT_TYPE_GSM_BS_IN:
299                         SCAT(msgtext, " LCR<->BSC");
300                         break;
301                 case PORT_TYPE_GSM_MS_OUT:
302                 case PORT_TYPE_GSM_MS_IN:
303                         SCAT(msgtext, " LCR<->MS");
304                         break;
305                 }
306         } else
307                 SCAT(msgtext, " ----");
308
309         /* init trace with given values */
310         start_trace(0,
311                     interface,
312                     port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
313                     port?port->p_dialinginfo.id:NULL,
314                     direction,
315                     CATEGORY_CH,
316                     port?port->p_serial:0,
317                     msgtext);
318 }
319
320 /* PROCEEDING INDICATION */
321 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
322 {
323         struct gsm_mncc *mode;
324
325         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
326         if (mncc->fields & MNCC_F_CAUSE) {
327                 add_trace("cause", "coding", "%d", mncc->cause.coding);
328                 add_trace("cause", "location", "%", mncc->cause.location);
329                 add_trace("cause", "value", "%", mncc->cause.value);
330         }
331         end_trace();
332
333         /* modify lchan to GSM codec V1 */
334         gsm_trace_header(p_g_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
335         mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
336         mode->lchan_mode = 0x01; /* GSM V1 */
337         mode->lchan_type = 0x02;
338         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
339         end_trace();
340         send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
341
342 }
343
344 /* CALL PROCEEDING INDICATION */
345 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
346 {
347         struct lcr_msg *message;
348         struct gsm_mncc *frame;
349
350         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
351         end_trace();
352
353         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
354         message_put(message);
355
356         new_state(PORT_STATE_OUT_PROCEEDING);
357
358         if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
359                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
360                 end_trace();
361                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
362                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
363                 p_g_tch_connected = 1;
364         }
365 }
366
367 /* ALERTING INDICATION */
368 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
369 {
370         struct lcr_msg *message;
371         struct gsm_mncc *frame;
372
373         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
374         end_trace();
375
376         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
377         message_put(message);
378
379         new_state(PORT_STATE_OUT_ALERTING);
380
381         if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
382                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
383                 end_trace();
384                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
385                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
386                 p_g_tch_connected = 1;
387         }
388 }
389
390 /* CONNECT INDICATION */
391 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
392 {
393         struct gsm_mncc *resp, *frame;
394         struct lcr_msg *message;
395
396         SCPY(p_connectinfo.id, mncc->connected.number);
397         SCPY(p_connectinfo.imsi, mncc->imsi);
398         p_connectinfo.present = INFO_PRESENT_ALLOWED;
399         p_connectinfo.screen = INFO_SCREEN_NETWORK;
400         p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
401         SCPY(p_connectinfo.interface, p_g_interface_name);
402
403         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
404         if (p_connectinfo.id[0])
405                 add_trace("connect", "number", "%s", p_connectinfo.id);
406         else if (mncc->imsi[0])
407                 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
408         if (mncc->imsi[0])
409                 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
410         end_trace();
411
412         /* send resp */
413         gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
414         resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_g_callref);
415         end_trace();
416         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
417
418         new_state(PORT_STATE_CONNECT);
419
420         if (!p_g_tch_connected) { /* only if ... */
421                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
422                 end_trace();
423                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
424                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
425                 p_g_tch_connected = 1;
426         }
427
428         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
429         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
430         message->param.connectinfo.rtpinfo.payload_type = 3; /* FIXME: receive payload type from peer */
431
432         if (p_g_rtp_bridge) {
433                 struct gsm_mncc_rtp *rtp;
434
435                 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding connect msg\n");
436                 p_g_connect_pending = message;
437                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
438                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
439         } else
440                 message_put(message);
441 }
442
443 /* CONNECT ACK INDICATION */
444 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
445 {
446         struct gsm_mncc *frame;
447
448         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
449         end_trace();
450
451         new_state(PORT_STATE_CONNECT);
452
453         if (!p_g_tch_connected) { /* only if ... */
454                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
455                 end_trace();
456                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
457                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
458                 p_g_tch_connected = 1;
459         }
460 }
461
462 /* DISCONNECT INDICATION */
463 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
464 {
465         struct lcr_msg *message;
466         int cause = 16, location = 0;
467         struct gsm_mncc *resp;
468
469         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
470         if (mncc->fields & MNCC_F_CAUSE) {
471                 location = mncc->cause.location;
472                 cause = mncc->cause.value;
473                 add_trace("cause", "coding", "%d", mncc->cause.coding);
474                 add_trace("cause", "location", "%d", location);
475                 add_trace("cause", "value", "%d", cause);
476         }
477         end_trace();
478
479         /* send release */
480         resp = create_mncc(MNCC_REL_REQ, p_g_callref);
481         gsm_trace_header(p_g_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
482 #if 0
483         resp->fields |= MNCC_F_CAUSE;
484         resp->cause.coding = 3;
485         resp->cause.location = 1;
486         resp->cause.value = cause;
487         add_trace("cause", "coding", "%d", resp->cause.coding);
488         add_trace("cause", "location", "%d", resp->cause.location);
489         add_trace("cause", "value", "%d", resp->cause.value);
490 #endif
491         end_trace();
492         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
493
494         /* sending release to endpoint */
495         while(p_epointlist) {
496                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
497                 message->param.disconnectinfo.cause = cause;
498                 message->param.disconnectinfo.location = location;
499                 message_put(message);
500                 /* remove epoint */
501                 free_epointlist(p_epointlist);
502         }
503         new_state(PORT_STATE_RELEASE);
504         trigger_work(&p_g_delete);
505 }
506
507 /* CC_RELEASE INDICATION */
508 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
509 {
510         int location = 0, cause = 16;
511         struct lcr_msg *message;
512
513         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
514         if (mncc->fields & MNCC_F_CAUSE) {
515                 location = mncc->cause.location;
516                 cause = mncc->cause.value;
517                 add_trace("cause", "coding", "%d", mncc->cause.coding);
518                 add_trace("cause", "location", "%d", mncc->cause.location);
519                 add_trace("cause", "value", "%d", mncc->cause.value);
520         }
521         end_trace();
522
523         /* sending release to endpoint */
524         while(p_epointlist) {
525                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
526                 message->param.disconnectinfo.cause = cause;
527                 message->param.disconnectinfo.location = location;
528                 message_put(message);
529                 /* remove epoint */
530                 free_epointlist(p_epointlist);
531         }
532         new_state(PORT_STATE_RELEASE);
533         trigger_work(&p_g_delete);
534 }
535
536 /* NOTIFY INDICATION */
537 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
538 {
539         struct lcr_msg *message;
540
541         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
542         add_trace("notify", NULL, "%d", mncc->notify);
543         end_trace();
544
545         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
546         message->param.notifyinfo.notify = mncc->notify;
547         message_put(message);
548 }
549
550 /* MESSAGE_NOTIFY */
551 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
552 {
553         struct gsm_mncc *mncc;
554         int notify;
555
556 //      printf("if = %d\n", param->notifyinfo.notify);
557         if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
558                 notify = param->notifyinfo.notify & 0x7f;
559                 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
560                         /* queue notification */
561                         if (p_g_notify_pending)
562                                 message_free(p_g_notify_pending);
563                         p_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
564                         memcpy(&p_g_notify_pending->param, param, sizeof(union parameter));
565                 } else {
566                         /* sending notification */
567                         gsm_trace_header(p_g_interface_name, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
568                         add_trace("notify", NULL, "%d", notify);
569                         end_trace();
570                         mncc = create_mncc(MNCC_NOTIFY_REQ, p_g_callref);
571                         mncc->notify = notify;
572                         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
573                 }
574         }
575 }
576
577 /* RTP create indication */
578 void Pgsm::rtp_create_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
579 {
580         struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
581
582         /* send queued setup, as we received remote RTP info */
583         if (p_g_setup_pending) {
584                 struct lcr_msg *message;
585
586                 message = p_g_setup_pending;
587                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding setup\n", rtp->ip, rtp->port);
588                 message->param.setup.rtpinfo.ip = rtp->ip;
589                 message->param.setup.rtpinfo.port = rtp->port;
590                 message_put(message);
591                 p_g_setup_pending = NULL;
592         }
593         if (p_g_connect_pending) {
594                 struct gsm_mncc_rtp *nrtp;
595
596                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) connecting RTP... \n", rtp->ip, rtp->port);
597                 nrtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
598                 nrtp->ip = p_g_rtp_ip_remote;
599                 nrtp->port = p_g_rtp_port_remote;
600                 send_and_free_mncc(p_g_lcr_gsm, nrtp->msg_type, nrtp);
601         }
602 }
603
604 /* RTP connect indication */
605 void Pgsm::rtp_connect_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
606 {
607         struct lcr_msg *message;
608         struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
609
610         if (p_g_connect_pending) {
611                 message = p_g_connect_pending;
612                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding connect\n", rtp->ip, rtp->port);
613                 message->param.connectinfo.rtpinfo.ip = rtp->ip;
614                 message->param.connectinfo.rtpinfo.port = rtp->port;
615                 message_put(message);
616                 p_g_connect_pending = NULL;
617         }
618 }
619
620 /* MESSAGE_PROGRESS */
621 void Pgsm::message_progress(unsigned int epoint_id, int message_id, union parameter *param)
622 {
623         if (param->progressinfo.progress == 8) {
624                 PDEBUG(DEBUG_GSM, "Remote provides tones for us\n");
625                 p_g_tones = 1;
626         }
627
628         if (param->progressinfo.rtpinfo.port) {
629                 struct gsm_mncc_rtp *rtp;
630
631                 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->progressinfo.rtpinfo.ip, param->progressinfo.rtpinfo.port);
632                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
633                 rtp->ip = param->progressinfo.rtpinfo.ip;
634                 rtp->port = param->progressinfo.rtpinfo.port;
635                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
636         }
637 }
638
639 /* MESSAGE_ALERTING */
640 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
641 {
642         struct gsm_mncc *mncc;
643
644         /* send alert */
645         gsm_trace_header(p_g_interface_name, this, MNCC_ALERT_REQ, DIRECTION_OUT);
646         mncc = create_mncc(MNCC_ALERT_REQ, p_g_callref);
647         if (p_g_tones) {
648                 mncc->fields |= MNCC_F_PROGRESS;
649                 mncc->progress.coding = 3; /* GSM */
650                 mncc->progress.location = 1;
651                 mncc->progress.descr = 8;
652                 add_trace("progress", "coding", "%d", mncc->progress.coding);
653                 add_trace("progress", "location", "%d", mncc->progress.location);
654                 add_trace("progress", "descr", "%d", mncc->progress.descr);
655         }
656         end_trace();
657         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
658
659         new_state(PORT_STATE_IN_ALERTING);
660
661         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
662                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
663                 end_trace();
664                 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
665                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
666                 p_g_tch_connected = 1;
667         }
668 }
669
670 /* MESSAGE_CONNECT */
671 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
672 {
673         struct gsm_mncc *mncc;
674
675         /* copy connected information */
676         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
677         /* screen outgoing caller id */
678         do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_g_interface_name);
679
680         /* send connect */
681         mncc = create_mncc(MNCC_SETUP_RSP, p_g_callref);
682         gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_RSP, DIRECTION_OUT);
683         /* caller information */
684         mncc->fields |= MNCC_F_CONNECTED;
685         mncc->connected.plan = 1;
686         switch (p_callerinfo.ntype) {
687                 case INFO_NTYPE_UNKNOWN:
688                 mncc->connected.type = 0x0;
689                 break;
690                 case INFO_NTYPE_INTERNATIONAL:
691                 mncc->connected.type = 0x1;
692                 break;
693                 case INFO_NTYPE_NATIONAL:
694                 mncc->connected.type = 0x2;
695                 break;
696                 case INFO_NTYPE_SUBSCRIBER:
697                 mncc->connected.type = 0x4;
698                 break;
699                 default: /* INFO_NTYPE_NOTPRESENT */
700                 mncc->fields &= ~MNCC_F_CONNECTED;
701                 break;
702         }
703         switch (p_callerinfo.screen) {
704                 case INFO_SCREEN_USER:
705                 mncc->connected.screen = 0;
706                 break;
707                 default: /* INFO_SCREEN_NETWORK */
708                 mncc->connected.screen = 3;
709                 break;
710         }
711         switch (p_callerinfo.present) {
712                 case INFO_PRESENT_ALLOWED:
713                 mncc->connected.present = 0;
714                 break;
715                 case INFO_PRESENT_RESTRICTED:
716                 mncc->connected.present = 1;
717                 break;
718                 default: /* INFO_PRESENT_NOTAVAIL */
719                 mncc->connected.present = 2;
720                 break;
721         }
722         if (mncc->fields & MNCC_F_CONNECTED) {
723                 SCPY(mncc->connected.number, p_connectinfo.id);
724                 add_trace("connected", "type", "%d", mncc->connected.type);
725                 add_trace("connected", "plan", "%d", mncc->connected.plan);
726                 add_trace("connected", "present", "%d", mncc->connected.present);
727                 add_trace("connected", "screen", "%d", mncc->connected.screen);
728                 add_trace("connected", "number", "%s", mncc->connected.number);
729         }
730         end_trace();
731         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
732
733         new_state(PORT_STATE_CONNECT_WAITING);
734
735         if (param->connectinfo.rtpinfo.port) {
736                 struct gsm_mncc_rtp *rtp;
737
738                 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->connectinfo.rtpinfo.ip, param->connectinfo.rtpinfo.port);
739                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
740                 rtp->ip = param->connectinfo.rtpinfo.ip;
741                 rtp->port = param->connectinfo.rtpinfo.port;
742                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
743         }
744
745 }
746
747 /* MESSAGE_DISCONNECT */
748 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
749 {
750         struct gsm_mncc *mncc;
751
752         /* send disconnect */
753         mncc = create_mncc(MNCC_DISC_REQ, p_g_callref);
754         gsm_trace_header(p_g_interface_name, this, MNCC_DISC_REQ, DIRECTION_OUT);
755         if (p_g_tones) {
756                 mncc->fields |= MNCC_F_PROGRESS;
757                 mncc->progress.coding = 3; /* GSM */
758                 mncc->progress.location = 1;
759                 mncc->progress.descr = 8;
760                 add_trace("progress", "coding", "%d", mncc->progress.coding);
761                 add_trace("progress", "location", "%d", mncc->progress.location);
762                 add_trace("progress", "descr", "%d", mncc->progress.descr);
763         }
764         mncc->fields |= MNCC_F_CAUSE;
765         mncc->cause.coding = 3;
766         mncc->cause.location = param->disconnectinfo.location;
767         mncc->cause.value = param->disconnectinfo.cause;
768         add_trace("cause", "coding", "%d", mncc->cause.coding);
769         add_trace("cause", "location", "%d", mncc->cause.location);
770         add_trace("cause", "value", "%d", mncc->cause.value);
771         end_trace();
772         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
773
774         new_state(PORT_STATE_OUT_DISCONNECT);
775
776         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
777                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
778                 end_trace();
779                 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
780                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
781                 p_g_tch_connected = 1;
782         }
783 }
784
785
786 /* MESSAGE_RELEASE */
787 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
788 {
789         struct gsm_mncc *mncc;
790
791         /* send release */
792         mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
793         gsm_trace_header(p_g_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
794         mncc->fields |= MNCC_F_CAUSE;
795         mncc->cause.coding = 3;
796         mncc->cause.location = param->disconnectinfo.location;
797         mncc->cause.value = param->disconnectinfo.cause;
798         add_trace("cause", "coding", "%d", mncc->cause.coding);
799         add_trace("cause", "location", "%d", mncc->cause.location);
800         add_trace("cause", "value", "%d", mncc->cause.value);
801         end_trace();
802         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
803
804         new_state(PORT_STATE_RELEASE);
805         trigger_work(&p_g_delete);
806         return;
807 }
808
809 /*
810  * endpoint sends messages to the port
811  */
812 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
813 {
814         int ret = 0;
815
816         if (Port::message_epoint(epoint_id, message_id, param))
817                 return 1;
818
819         switch(message_id) {
820                 case MESSAGE_NOTIFY: /* display and notifications */
821                 ret = 1;
822                 message_notify(epoint_id, message_id, param);
823                 break;
824
825 //              case MESSAGE_FACILITY: /* facility message */
826 //              message_facility(epoint_id, message_id, param);
827 //              break;
828
829                 case MESSAGE_PROCEEDING: /* message not handles */
830                 ret = 1;
831                 break;
832
833                 case MESSAGE_PROGRESS:
834                 ret = 1;
835                 message_progress(epoint_id, message_id, param);
836                 break;
837
838                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
839                 ret = 1;
840                 if (p_state!=PORT_STATE_IN_PROCEEDING)
841                         break;
842                 message_alerting(epoint_id, message_id, param);
843                 if (p_g_notify_pending) {
844                         /* send pending notify message during connect */
845                         message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
846                         message_free(p_g_notify_pending);
847                         p_g_notify_pending = NULL;
848                 }
849                 break;
850
851                 case MESSAGE_CONNECT: /* call of endpoint is connected */
852                 ret = 1;
853                 if (p_state!=PORT_STATE_IN_PROCEEDING
854                  && p_state!=PORT_STATE_IN_ALERTING)
855                         break;
856                 message_connect(epoint_id, message_id, param);
857                 if (p_g_notify_pending) {
858                         /* send pending notify message during connect */
859                         message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
860                         message_free(p_g_notify_pending);
861                         p_g_notify_pending = NULL;
862                 }
863                 break;
864
865                 case MESSAGE_DISCONNECT: /* call has been disconnected */
866                 ret = 1;
867                 if (p_state!=PORT_STATE_IN_PROCEEDING
868                  && p_state!=PORT_STATE_IN_ALERTING
869                  && p_state!=PORT_STATE_OUT_SETUP
870                  && p_state!=PORT_STATE_OUT_OVERLAP
871                  && p_state!=PORT_STATE_OUT_PROCEEDING
872                  && p_state!=PORT_STATE_OUT_ALERTING
873                  && p_state!=PORT_STATE_CONNECT
874                  && p_state!=PORT_STATE_CONNECT_WAITING)
875                         break;
876                 message_disconnect(epoint_id, message_id, param);
877                 break;
878
879                 case MESSAGE_RELEASE: /* release isdn port */
880                 ret = 1;
881                 if (p_state==PORT_STATE_RELEASE)
882                         break;
883                 message_release(epoint_id, message_id, param);
884                 break;
885
886         }
887
888         return ret;
889 }
890
891 /* deletes only if l3id is release, otherwhise it will be triggered then */
892 static int delete_event(struct lcr_work *work, void *instance, int index)
893 {
894         class Pgsm *gsmport = (class Pgsm *)instance;
895
896         delete gsmport;
897
898         return 0;
899 }
900
901 int gsm_exit(int rc)
902 {
903         return(rc);
904 }
905
906 int gsm_init(void)
907 {
908         /* seed the PRNG */
909         srand(time(NULL));
910
911         return 0;
912 }
913
914 /*
915  * MNCC interface
916  */
917
918 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
919 {
920         struct mncc_q_entry *qe;
921
922         qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
923         if (!qe)
924                 return -ENOMEM;
925
926         qe->next = NULL;
927         qe->len = len;
928         memcpy(qe->data, mncc, len);
929
930         /* in case of empty list ... */
931         if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
932                 /* the list head and tail both point to the new qe */
933                 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
934         } else {
935                 /* append to tail of list */
936                 lcr_gsm->mncc_q_tail->next = qe;
937                 lcr_gsm->mncc_q_tail = qe;
938         }
939
940         lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
941
942         return 0;
943 }
944
945 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
946 {
947         struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
948         if (!qe)
949                 return NULL;
950
951         /* dequeue the successfully sent message */
952         lcr_gsm->mncc_q_hd = qe->next;
953         if (!qe)
954                 return NULL;
955         if (qe == lcr_gsm->mncc_q_tail)
956                 lcr_gsm->mncc_q_tail = NULL;
957
958         return qe;
959 }
960
961 /* routine called by LCR code if it wants to send a message to OpenBSC */
962 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
963 {
964         int len = 0;
965
966         /* FIXME: the caller should provide this */
967         switch (msg_type) {
968         case GSM_TCHF_FRAME:
969                 len = sizeof(struct gsm_data_frame) + 33;
970                 break;
971         default:
972                 len = sizeof(struct gsm_mncc);
973                 break;
974         }
975                 
976         return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
977 }
978
979 /* close MNCC socket */
980 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
981 {
982         class Port *port;
983         class Pgsm *pgsm = NULL;
984         struct lcr_msg *message;
985
986         PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
987         close(lfd->fd);
988         unregister_fd(lfd);
989         lfd->fd = -1;
990
991         /* free all the calls that were running through the MNCC interface */
992         port = port_first;
993         while(port) {
994                 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_GSM) {
995                         pgsm = (class Pgsm *)port;
996                         if (pgsm->p_g_lcr_gsm == lcr_gsm) {
997                                 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
998                                 message->param.disconnectinfo.cause = 27; // temp. unavail.
999                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1000                                 message_put(message);
1001                                 pgsm->new_state(PORT_STATE_RELEASE);
1002                                 trigger_work(&pgsm->p_g_delete);
1003                         }
1004                 }
1005                 port = port->next;
1006         }
1007
1008         /* flush the queue */
1009         while (mncc_q_dequeue(lcr_gsm))
1010                 ;
1011
1012         /* start the re-connect timer */
1013         schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1014
1015         return 0;
1016 }
1017
1018 /* write to OpenBSC via MNCC socket */
1019 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1020 {
1021         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1022         struct mncc_q_entry *qe, *qe2;
1023         int rc;
1024
1025         while (1) {
1026                 qe = lcr_gsm->mncc_q_hd;
1027                 if (!qe) {
1028                         lfd->when &= ~LCR_FD_WRITE;
1029                         break;
1030                 }
1031                 rc = write(lfd->fd, qe->data, qe->len);
1032                 if (rc == 0)
1033                         return mncc_fd_close(lcr_gsm, lfd);
1034                 if (rc < 0)
1035                         return rc;
1036                 if (rc < (int)qe->len)
1037                         return -1;
1038                 /* dequeue the successfully sent message */
1039                 qe2 = mncc_q_dequeue(lcr_gsm);
1040                 assert(qe == qe2);
1041                 free(qe);
1042         }
1043         return 0;
1044 }
1045
1046 /* read from OpenBSC via MNCC socket */
1047 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1048 {
1049         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1050         int rc;
1051         static char buf[sizeof(struct gsm_mncc)+1024];
1052         struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1053
1054         memset(buf, 0, sizeof(buf));
1055         rc = recv(lfd->fd, buf, sizeof(buf), 0);
1056         if (rc == 0)
1057                 return mncc_fd_close(lcr_gsm, lfd);
1058         if (rc < 0)
1059                 return rc;
1060
1061         /* Hand the MNCC message into LCR */
1062         switch (lcr_gsm->type) {
1063 #ifdef WITH_GSM_BS
1064         case LCR_GSM_TYPE_NETWORK:
1065                 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1066 #endif
1067 #ifdef WITH_GSM_MS
1068         case LCR_GSM_TYPE_MS:
1069                 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1070 #endif
1071         default:
1072                 return 0;
1073         }
1074 }
1075
1076 /* file descriptor callback if we can read or write form MNCC socket */
1077 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1078 {
1079         int rc = 0;
1080
1081         if (what & LCR_FD_READ)
1082                 rc = mncc_fd_read(lfd, inst, idx);
1083         if (rc < 0)
1084                 return rc;
1085
1086         if (what & LCR_FD_WRITE)
1087                 rc = mncc_fd_write(lfd, inst, idx);
1088
1089         return rc;
1090 }
1091
1092 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1093 {
1094         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1095         int fd, rc;
1096
1097         lcr_gsm->mncc_lfd.fd = -1;
1098
1099         fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1100         if (fd < 0) {
1101                 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1102                 return fd;
1103         }
1104
1105         rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1106                      sizeof(lcr_gsm->sun));
1107         if (rc < 0) {
1108                 PERROR("Could not connect to MNCC socket %s, "
1109                         "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1110                         SOCKET_RETRY_TIMER);
1111                 close(fd);
1112                 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1113         } else {
1114                 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1115                 lcr_gsm->mncc_lfd.fd = fd;
1116                 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);
1117         }
1118
1119         return 0;
1120 }
1121