6d98bd0421438cd8f586ee0051b29a3538fad261
[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 <assert.h>
24
25 #define SOCKET_RETRY_TIMER      5
26
27 //struct lcr_gsm *gsm = NULL;
28
29 int new_callref = 1;
30
31 /* names of MNCC-SAP */
32 static const struct _value_string {
33         int msg_type;
34         const char *name;
35 } mncc_names[] = {
36         { 0,                    "New call ref" },
37         { 1,                    "Codec negotiation" },
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
86         { MNCC_LCHAN_MODIFY,    "MNCC_LCHAN_MODIFY" },
87
88         { MNCC_BRIDGE,          "MNCC_BRIDGE" },
89         { MNCC_FRAME_RECV,      "MNCC_FRAME_RECV" },
90         { MNCC_FRAME_DROP,      "MNCC_FRAME_DROP" },
91         { MNCC_RTP_CREATE,      "MNCC_RTP_CREATE" },
92         { MNCC_RTP_CONNECT,     "MNCC_RTP_CONNECT" },
93         { MNCC_RTP_FREE,        "MNCC_RTP_FREE" },
94
95         { 0,                    NULL }
96 };
97
98 const char *mncc_name(int value)
99 {
100         int i = 0;
101         static char ukn[32];
102
103         while (mncc_names[i].name) {
104                 if (mncc_names[i].msg_type == value)
105                         return mncc_names[i].name;
106                 i++;
107         }
108         SPRINT(ukn, "unknown(0x%x)", value);
109         return ukn;
110 }
111
112 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data);
113
114 /*
115  * create and send mncc message
116  */
117 struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
118 {
119         struct gsm_mncc *mncc;
120
121         mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
122         mncc->msg_type = msg_type;
123         mncc->callref = callref;
124         return (mncc);
125 }
126 int send_and_free_mncc(struct lcr_gsm *lcr_gsm, unsigned int msg_type, void *data)
127 {
128         int ret = 0;
129
130         if (lcr_gsm) {
131                 ret = mncc_send(lcr_gsm, msg_type, data);
132         }
133         free(data);
134
135         return ret;
136 }
137
138 void Pgsm::send_mncc_rtp_connect(void)
139 {
140         struct gsm_mncc_rtp *nrtp;
141
142         nrtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
143         nrtp->ip = p_g_rtp_ip_remote;
144         nrtp->port = p_g_rtp_port_remote;
145         switch (p_g_media_type) {
146         case MEDIA_TYPE_GSM:
147                 nrtp->payload_msg_type = GSM_TCHF_FRAME;
148                 break;
149         case MEDIA_TYPE_GSM_EFR:
150                 nrtp->payload_msg_type = GSM_TCHF_FRAME_EFR;
151                 break;
152         case MEDIA_TYPE_AMR:
153                 nrtp->payload_msg_type = GSM_TCH_FRAME_AMR;
154                 break;
155         case MEDIA_TYPE_GSM_HR:
156                 nrtp->payload_msg_type = GSM_TCHH_FRAME;
157                 break;
158         }
159         nrtp->payload_type = p_g_payload_type;
160         PDEBUG(DEBUG_GSM, "sending MNCC RTP connect with payload_msg_type=%x, payload_type=%d\n", nrtp->payload_msg_type, nrtp->payload_type);
161         send_and_free_mncc(p_g_lcr_gsm, nrtp->msg_type, nrtp);
162 }
163
164 static int delete_event(struct lcr_work *work, void *instance, int index);
165
166 /*
167  * constructor
168  */
169 Pgsm::Pgsm(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
170 {
171 #ifdef WITH_GSMHR
172         signed short homing[160];
173         int i;
174 #endif
175
176         p_g_tones = 0;
177         if (interface->is_tones == IS_YES)
178                 p_g_tones = 1;
179         p_g_earlyb = 0;
180         if (interface->is_earlyb == IS_YES)
181                 p_g_earlyb = 1;
182         p_g_rtp_bridge = 0;
183         if (interface->rtp_bridge)
184                 p_g_rtp_bridge = 1;
185         p_g_rtp_payloads = 0;
186         memset(&p_g_samples, 0, sizeof(p_g_samples));
187         p_callerinfo.itype = (interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
188         memset(&p_g_delete, 0, sizeof(p_g_delete));
189         add_work(&p_g_delete, delete_event, this, 0);
190         p_g_lcr_gsm = NULL;
191         p_g_callref = 0;
192         p_g_mode = 0;
193         p_g_gsm_b_sock = -1;
194         p_g_gsm_b_index = -1;
195         p_g_gsm_b_active = 0;
196         p_g_notify_pending = NULL;
197         p_g_setup_pending = NULL;
198         p_g_connect_pending = NULL;
199         p_g_fr_decoder = NULL;
200         p_g_fr_encoder = NULL;
201         p_g_hr_decoder = NULL;
202         p_g_hr_encoder = NULL;
203         p_g_amr_decoder = NULL;
204         p_g_amr_encoder = NULL;
205         p_g_amr_cmr = 0;
206         p_g_amr_cmr_valid = 0;
207 #ifdef WITH_GSMFR
208         p_g_fr_decoder = gsm_fr_create();
209         p_g_fr_encoder = gsm_fr_create();
210         if (!p_g_fr_encoder || !p_g_fr_decoder) {
211                 PERROR("Failed to create GSM FR codec instance\n");
212                 trigger_work(&p_g_delete);
213         }
214 #endif
215 #ifdef WITH_GSMHR
216         p_g_hr_decoder = gsm_hr_create();
217         p_g_hr_encoder = gsm_hr_create();
218         if (!p_g_hr_encoder || !p_g_hr_decoder) {
219                 PERROR("Failed to create GSM HR codec instance\n");
220                 trigger_work(&p_g_delete);
221         }
222         /* Homing */
223         for (i = 0; i < 160; i++)
224                 homing[i] = 0x0008;
225         gsm_hr_encode(p_g_hr_encoder, homing, NULL);
226 #endif
227 #ifdef WITH_GSMAMR
228         p_g_amr_decoder = gsm_amr_create();
229         p_g_amr_encoder = gsm_amr_create();
230         if (!p_g_amr_encoder || !p_g_amr_decoder) {
231                 PERROR("Failed to create GSM AMR codec instance\n");
232                 trigger_work(&p_g_delete);
233         }
234 #endif
235         p_g_rxpos = 0;
236         p_g_tch_connected = 0;
237         p_g_media_type = 0;
238
239         PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
240 }
241
242 /*
243  * destructor
244  */
245 Pgsm::~Pgsm()
246 {
247         PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
248
249         del_work(&p_g_delete);
250
251         /* remove queued message */
252         if (p_g_notify_pending)
253                 message_free(p_g_notify_pending);
254         if (p_g_setup_pending)
255                 message_free(p_g_setup_pending);
256         if (p_g_connect_pending)
257                 message_free(p_g_connect_pending);
258
259 //#ifdef WITH_GSMFR
260         /* close codec */
261         if (p_g_fr_encoder)
262                 gsm_fr_destroy(p_g_fr_encoder);
263         if (p_g_fr_decoder)
264                 gsm_fr_destroy(p_g_fr_decoder);
265 //#endif
266 #ifdef WITH_GSMHR
267         if (p_g_hr_encoder)
268                 gsm_hr_destroy(p_g_hr_encoder);
269         if (p_g_hr_decoder)
270                 gsm_hr_destroy(p_g_hr_decoder);
271 #endif
272 #ifdef WITH_GSMAMR
273         /* close codec */
274         if (p_g_amr_encoder)
275                 gsm_amr_destroy(p_g_amr_encoder);
276         if (p_g_amr_decoder)
277                 gsm_amr_destroy(p_g_amr_decoder);
278 #endif
279 }
280
281
282 /* receive encoded frame from gsm */
283 void Pgsm::frame_receive(void *arg)
284 {
285         struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
286         unsigned char data[160];
287         int i, cmr;
288
289         switch (frame->msg_type) {
290         case GSM_TCHF_FRAME:
291                 if (p_g_media_type != MEDIA_TYPE_GSM) {
292                         PERROR("FR frame, but current media type mismatches.\n");
293                         return;
294                 }
295                 if (!p_g_fr_decoder) {
296                         PERROR("FR frame, but decoder not created.\n");
297                         return;
298                 }
299                 if ((frame->data[0]>>4) != 0xd) {
300                         PDEBUG(DEBUG_GSM, "received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
301                         goto bfi;
302                 }
303 #ifdef WITH_GSMFR
304                 /* decode */
305                 gsm_fr_decode(p_g_fr_decoder, frame->data, p_g_samples);
306                 for (i = 0; i < 160; i++) {
307                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
308                 }
309 #endif
310                 break;
311         case GSM_TCHH_FRAME:
312                 if (p_g_media_type != MEDIA_TYPE_GSM_HR) {
313                         PERROR("HR frame, but current media type mismatches.\n");
314                         return;
315                 }
316                 if (!p_g_hr_decoder) {
317                         PERROR("HR frame, but decoder not created.\n");
318                         return;
319                 }
320                 if ((frame->data[0]>>4) != 0x0)
321                         goto bfi;
322 #ifdef WITH_GSMHR
323                 /* decode */
324                 if (gsm_hr_decode(p_g_hr_decoder, frame->data, p_g_samples))
325                         goto bfi;
326                 for (i = 0; i < 160; i++) {
327                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
328                 }
329 #endif
330                 break;
331         case GSM_TCHF_FRAME_EFR:
332                 if (p_g_media_type != MEDIA_TYPE_GSM_EFR) {
333                         PERROR("EFR frame, but current media type mismatches.\n");
334                         return;
335                 }
336                 if (!p_g_amr_decoder) {
337                         PERROR("EFR frame, but decoder not created.\n");
338                         return;
339                 }
340                 if ((frame->data[0]>>4) != 0xc)
341                         goto bfi;
342 #ifdef WITH_GSMAMR
343                 /* decode */
344                 gsm_efr_decode(p_g_amr_decoder, frame->data, p_g_samples);
345                 for (i = 0; i < 160; i++) {
346                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
347                 }
348 #endif
349                 break;
350         case GSM_TCH_FRAME_AMR:
351                 if (p_g_media_type != MEDIA_TYPE_AMR) {
352                         PERROR("AMR frame, but current media type mismatches.\n");
353                         return;
354                 }
355                 if (!p_g_amr_decoder) {
356                         PERROR("AMR frame, but decoder not created.\n");
357                         return;
358                 }
359                 cmr = (frame->data[1] >> 4);
360                 if (cmr <= 7) {
361                         p_g_amr_cmr = cmr;
362                         p_g_amr_cmr_valid = 1;
363                 }
364                 if (!(frame->data[2] & 0x04))
365                         goto bfi;
366 #ifdef WITH_GSMAMR
367                 /* decode (skip length byte in front) */
368                 gsm_amr_decode(p_g_amr_decoder, frame->data + 1, p_g_samples);
369                 for (i = 0; i < 160; i++) {
370                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
371                 }
372 #endif
373                 break;
374         case GSM_BAD_FRAME:
375         default:
376 bfi:
377                 if (p_echotest) {
378                         /* beep on bad frame */
379                         for (i = 0; i < 160; i++) {
380                                 if ((i & 3) > 2)
381                                         p_g_samples[i] = 15000;
382                                 else
383                                         p_g_samples[i] = -15000;
384                                 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
385                         }
386                 } else {
387                         /* repeat on bad frame */
388                         for (i = 0; i < 160; i++) {
389                                 p_g_samples[i] = (p_g_samples[i] * 14) >> 4;
390                                 data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
391                         }
392                 }
393         }
394
395         /* record data */
396         if (p_record)
397                 record(data, 160, 0); // from down
398         if (p_tap)
399                 tap(data, 160, 0); // from down
400
401         /* local echo */
402         if (p_echotest)
403                 bridge_rx(data, 160);
404
405         /* send to remote*/
406         bridge_tx(data, 160);
407 }
408
409 /* send traffic to gsm */
410 int Pgsm::bridge_rx(unsigned char *data, int len)
411 {
412         int ret;
413
414         if ((ret = Port::bridge_rx(data, len)))
415                 return ret;
416
417         if (p_tone_name[0])
418                 return -EINVAL;
419
420         return audio_send(data, len);
421 }
422
423 int Pgsm::audio_send(unsigned char *data, int len)
424 {
425         unsigned char frame[33];
426         int ret;
427
428         /* record data */
429         if (p_record)
430                 record(data, len, 1); // from up
431         if (p_tap)
432                 tap(data, len, 1); // from up
433
434         /* (currently) not connected, so don't flood tch! */
435         if (!p_g_tch_connected)
436                 return -EINVAL;
437
438         /* write to rx buffer */
439         while(len--) {
440                 p_g_rxdata[p_g_rxpos++] = audio_law_to_s32[*data++];
441                 if (p_g_rxpos != 160)
442                         continue;
443                 p_g_rxpos = 0;
444
445                 switch (p_g_media_type) {
446                 case MEDIA_TYPE_GSM:
447                         if (!p_g_fr_encoder) {
448                                 PERROR("FR frame, but encoder not created.\n");
449                                 break;
450                         }
451 #ifdef WITH_GSMFR
452                         /* encode data */
453                         gsm_fr_encode(p_g_fr_encoder, p_g_rxdata, frame);
454                         frame_send(frame, 33, GSM_TCHF_FRAME);
455 #endif
456                         break;
457                 case MEDIA_TYPE_GSM_HR:
458                         if (!p_g_hr_encoder) {
459                                 PERROR("HR frame, but encoder not created.\n");
460                                 break;
461                         }
462 #ifdef WITH_GSMHR
463                         /* encode data */
464                         gsm_hr_encode(p_g_hr_encoder, p_g_rxdata, frame);
465                         frame_send(frame, 15, GSM_TCHH_FRAME);
466 #endif
467                         break;
468                 case MEDIA_TYPE_GSM_EFR:
469                         if (!p_g_amr_encoder) {
470                                 PERROR("EFR frame, but encoder not created.\n");
471                                 break;
472                         }
473 #ifdef WITH_GSMAMR
474                         /* encode data */
475                         gsm_efr_encode(p_g_amr_encoder, p_g_rxdata, frame);
476                         frame_send(frame, 31, GSM_TCHF_FRAME_EFR);
477 #endif
478                         break;
479                 case MEDIA_TYPE_AMR:
480                         if (!p_g_amr_encoder) {
481                                 PERROR("AMR frame, but encoder not created.\n");
482                                 break;
483                         }
484                         if (!p_g_amr_cmr_valid) {
485                                 PDEBUG(DEBUG_GSM, "no valid CMR yet.\n");
486                                 break;
487                         }
488 #ifdef WITH_GSMAMR
489                         /* encode data (prefix a length byte) */
490                         ret = gsm_amr_encode(p_g_amr_encoder, p_g_rxdata, frame + 1, p_g_amr_cmr);
491                         frame[0] = ret;
492                         frame_send(frame, ret + 1, GSM_TCH_FRAME_AMR);
493 #endif
494                         break;
495                 }
496         }
497
498         return 0;
499 }
500
501 void Pgsm::frame_send(void *_frame, int len, int msg_type)
502 {
503         unsigned char buffer[sizeof(struct gsm_data_frame) + len];
504         struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
505         
506         frame->msg_type = msg_type;
507         frame->callref = p_g_callref;
508         memcpy(frame->data, _frame, len);
509
510         if (p_g_lcr_gsm) {
511                 mncc_send(p_g_lcr_gsm, frame->msg_type, frame);
512         }
513 }
514
515 /*
516  * create trace
517  */
518 void gsm_trace_header(const char *interface_name, class Pgsm *port, unsigned int msg_type, int direction)
519 {
520         char msgtext[64];
521         struct interface *interface = interface_first;
522
523         interface = getinterfacebyname(interface_name);
524         if (!interface)
525                 return;
526
527         /* select message and primitive text */
528         SCPY(msgtext, mncc_name(msg_type));
529
530         /* add direction */
531         if (port) {
532                 switch(port->p_type) {
533                 case PORT_TYPE_GSM_BS_OUT:
534                 case PORT_TYPE_GSM_BS_IN:
535                         SCAT(msgtext, " LCR<->BSC");
536                         break;
537                 case PORT_TYPE_GSM_MS_OUT:
538                 case PORT_TYPE_GSM_MS_IN:
539                         SCAT(msgtext, " LCR<->MS");
540                         break;
541                 }
542         } else
543                 SCAT(msgtext, " ----");
544
545         /* init trace with given values */
546         start_trace(-1,
547                     interface,
548                     port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
549                     port?port->p_dialinginfo.id:NULL,
550                     direction,
551                     CATEGORY_CH,
552                     port?port->p_serial:0,
553                     msgtext);
554 }
555
556 /* modify lchan to given payload type */
557 void Pgsm::modify_lchan(int media_type)
558 {
559         struct gsm_mncc *mode;
560
561         /* already modified to that media type */
562         if (p_g_media_type == media_type)
563                 return;
564
565         p_g_media_type = media_type;
566         gsm_trace_header(p_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
567         mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
568         switch (media_type) {
569         case MEDIA_TYPE_GSM_EFR:
570                 add_trace("speech", "version", "EFR given");
571                 mode->lchan_mode = 0x21; /* GSM V2 */
572                 mode->lchan_type = 0x02; /* TCH/F */
573                 break;
574         case MEDIA_TYPE_AMR:
575                 add_trace("speech", "version", "AMR given");
576                 mode->lchan_mode = 0x41; /* GSM V3 */
577                 /* as we don't know the available channels, this type will be set by openbsc automatically */
578                 mode->lchan_type = 0x02; /* TCH/F */
579                 break;
580         case MEDIA_TYPE_GSM_HR:
581                 add_trace("speech", "version", "Half Rate given");
582                 mode->lchan_mode = 0x01; /* GSM V1 */
583                 mode->lchan_type = 0x03; /* TCH/H */
584         default:
585                 add_trace("speech", "version", "Full Rate given");
586                 mode->lchan_mode = 0x01; /* GSM V1 */
587                 mode->lchan_type = 0x02; /* TCH/F */
588         }
589         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
590         end_trace();
591         send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
592 }
593
594 /* CALL PROCEEDING INDICATION (from network) */
595 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
596 {
597         struct lcr_msg *message;
598         struct gsm_mncc *frame;
599
600         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
601         end_trace();
602
603         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
604         message_put(message);
605
606         new_state(PORT_STATE_OUT_PROCEEDING);
607
608         if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
609                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
610                 end_trace();
611                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
612                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
613                 p_g_tch_connected = 1;
614         }
615
616         /* modify to GSM FR (this is GSM user side only, so there is FR supported only) */
617         modify_lchan(MEDIA_TYPE_GSM);
618 }
619
620 /* ALERTING INDICATION */
621 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
622 {
623         struct lcr_msg *message;
624         struct gsm_mncc *frame;
625
626         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
627         end_trace();
628
629         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
630         message_put(message);
631
632         new_state(PORT_STATE_OUT_ALERTING);
633
634         if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
635                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
636                 end_trace();
637                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
638                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
639                 p_g_tch_connected = 1;
640         }
641
642         /* modify to GSM FR, if not already */
643         if (!p_g_media_type) {
644                 modify_lchan(MEDIA_TYPE_GSM);
645         }
646 }
647
648 /* CONNECT INDICATION */
649 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
650 {
651         struct gsm_mncc *resp, *frame;
652         struct lcr_msg *message;
653
654         SCPY(p_connectinfo.id, mncc->connected.number);
655         SCPY(p_connectinfo.imsi, mncc->imsi);
656         p_connectinfo.present = INFO_PRESENT_ALLOWED;
657         p_connectinfo.screen = INFO_SCREEN_NETWORK;
658         p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
659         SCPY(p_connectinfo.interface, p_interface_name);
660
661         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
662         if (p_connectinfo.id[0])
663                 add_trace("connect", "number", "%s", p_connectinfo.id);
664         else if (mncc->imsi[0])
665                 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
666         if (mncc->imsi[0])
667                 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
668         end_trace();
669
670         /* send resp */
671         gsm_trace_header(p_interface_name, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
672         resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_g_callref);
673         end_trace();
674         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
675
676         new_state(PORT_STATE_CONNECT);
677
678         if (!p_g_tch_connected) { /* only if ... */
679                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
680                 end_trace();
681                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
682                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
683                 p_g_tch_connected = 1;
684         }
685
686         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
687         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
688
689         /* if we have a bridge, but not yet modified, the phone accepts out requested payload.
690          * we force the first in list */
691         if (p_g_rtp_bridge) {
692                 if (!p_g_media_type) {
693                         /* modify to first given type */
694                         modify_lchan(p_g_rtp_media_types[0]);
695                         /* also set payload type */
696                         p_g_payload_type = p_g_rtp_payload_types[0];
697                 }
698                 message->param.connectinfo.rtpinfo.media_types[0] = p_g_media_type;
699                 message->param.connectinfo.rtpinfo.payload_types[0] = p_g_payload_type;
700                 message->param.connectinfo.rtpinfo.payloads = 1;
701         } else {
702                 /* modify to GSM FR, if not already
703                 * for network side, this should have been already happened */
704                 if (!p_g_media_type)
705                         modify_lchan(MEDIA_TYPE_GSM);
706         }
707
708
709         if (p_g_rtp_bridge) {
710                 struct gsm_mncc_rtp *rtp;
711
712                 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding connect msg\n");
713                 p_g_connect_pending = message;
714                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
715                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
716         } else
717                 message_put(message);
718 }
719
720 /* CONNECT ACK INDICATION */
721 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
722 {
723         struct gsm_mncc *frame;
724
725         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
726         end_trace();
727
728         new_state(PORT_STATE_CONNECT);
729
730         if (!p_g_tch_connected) { /* only if ... */
731                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
732                 end_trace();
733                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
734                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
735                 p_g_tch_connected = 1;
736         }
737
738         /* modify to GSM FR, if not already */
739         if (!p_g_media_type) {
740                 modify_lchan(MEDIA_TYPE_GSM);
741         }
742 }
743
744 /* DISCONNECT INDICATION */
745 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
746 {
747         struct lcr_msg *message;
748         int cause = 16, location = 0;
749         struct gsm_mncc *resp;
750
751         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
752         if (mncc->fields & MNCC_F_CAUSE) {
753                 location = mncc->cause.location;
754                 cause = mncc->cause.value;
755                 add_trace("cause", "coding", "%d", mncc->cause.coding);
756                 add_trace("cause", "location", "%d", location);
757                 add_trace("cause", "value", "%d", cause);
758         }
759         end_trace();
760
761         /* send release */
762         resp = create_mncc(MNCC_REL_REQ, p_g_callref);
763         gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
764 #if 0
765         resp->fields |= MNCC_F_CAUSE;
766         resp->cause.coding = 3;
767         resp->cause.location = 1;
768         resp->cause.value = cause;
769         add_trace("cause", "coding", "%d", resp->cause.coding);
770         add_trace("cause", "location", "%d", resp->cause.location);
771         add_trace("cause", "value", "%d", resp->cause.value);
772 #endif
773         end_trace();
774         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
775
776         /* sending release to endpoint */
777         while(p_epointlist) {
778                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
779                 message->param.disconnectinfo.cause = cause;
780                 message->param.disconnectinfo.location = location;
781                 message_put(message);
782                 /* remove epoint */
783                 free_epointlist(p_epointlist);
784         }
785         new_state(PORT_STATE_RELEASE);
786         trigger_work(&p_g_delete);
787 }
788
789 /* CC_RELEASE INDICATION */
790 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
791 {
792         int location = 0, cause = 16;
793         struct lcr_msg *message;
794
795         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
796         if (mncc->fields & MNCC_F_CAUSE) {
797                 location = mncc->cause.location;
798                 cause = mncc->cause.value;
799                 add_trace("cause", "coding", "%d", mncc->cause.coding);
800                 add_trace("cause", "location", "%d", mncc->cause.location);
801                 add_trace("cause", "value", "%d", mncc->cause.value);
802         }
803         end_trace();
804
805         /* sending release to endpoint */
806         while(p_epointlist) {
807                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
808                 message->param.disconnectinfo.cause = cause;
809                 message->param.disconnectinfo.location = location;
810                 message_put(message);
811                 /* remove epoint */
812                 free_epointlist(p_epointlist);
813         }
814         new_state(PORT_STATE_RELEASE);
815         trigger_work(&p_g_delete);
816 }
817
818 /* NOTIFY INDICATION */
819 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
820 {
821         struct lcr_msg *message;
822
823         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
824         add_trace("notify", NULL, "%d", mncc->notify);
825         end_trace();
826
827         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
828         message->param.notifyinfo.notify = mncc->notify;
829         message_put(message);
830 }
831
832 /* MESSAGE_NOTIFY */
833 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
834 {
835         struct gsm_mncc *mncc;
836         int notify;
837
838 //      printf("if = %d\n", param->notifyinfo.notify);
839         if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
840                 notify = param->notifyinfo.notify & 0x7f;
841                 /* skip all notifiy ids that are not 0, 1, 2 */
842                 if (notify > 2)
843                         return;
844                 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
845                         /* queue notification */
846                         if (p_g_notify_pending)
847                                 message_free(p_g_notify_pending);
848                         p_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
849                         memcpy(&p_g_notify_pending->param, param, sizeof(union parameter));
850                 } else {
851                         /* sending notification */
852                         gsm_trace_header(p_interface_name, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
853                         add_trace("notify", NULL, "%d", notify);
854                         end_trace();
855                         mncc = create_mncc(MNCC_NOTIFY_REQ, p_g_callref);
856                         mncc->notify = notify;
857                         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
858                 }
859         }
860 }
861
862 /* RTP create indication */
863 void Pgsm::rtp_create_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
864 {
865         struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
866
867         /* send queued setup, as we received remote RTP info */
868         if (p_g_setup_pending) {
869                 struct lcr_msg *message;
870
871                 message = p_g_setup_pending;
872                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding setup\n", rtp->ip, rtp->port);
873                 message->param.setup.rtpinfo.ip = rtp->ip;
874                 message->param.setup.rtpinfo.port = rtp->port;
875                 message_put(message);
876                 p_g_setup_pending = NULL;
877         }
878         if (p_g_connect_pending) {
879                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) connecting RTP... \n", rtp->ip, rtp->port);
880                 send_mncc_rtp_connect();
881         }
882 }
883
884 /* RTP connect indication */
885 void Pgsm::rtp_connect_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
886 {
887         struct lcr_msg *message;
888         struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
889
890         if (p_g_connect_pending) {
891                 message = p_g_connect_pending;
892                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding connect\n", rtp->ip, rtp->port);
893                 message->param.connectinfo.rtpinfo.ip = rtp->ip;
894                 message->param.connectinfo.rtpinfo.port = rtp->port;
895                 message_put(message);
896                 p_g_connect_pending = NULL;
897         }
898 }
899
900 /* MESSAGE_PROGRESS */
901 void Pgsm::message_progress(unsigned int epoint_id, int message_id, union parameter *param)
902 {
903         if (param->progressinfo.progress == 8) {
904                 PDEBUG(DEBUG_GSM, "Remote provides tones for us\n");
905                 p_g_tones = 1;
906         }
907
908         if (param->progressinfo.rtpinfo.port) {
909                 PDEBUG(DEBUG_GSM, "PROGRESS with RTP peer info, sent to BSC (%08x,%d) with media %d, pt %d\n", param->progressinfo.rtpinfo.ip, param->progressinfo.rtpinfo.port, param->progressinfo.rtpinfo.media_types[0], param->progressinfo.rtpinfo.payload_types[0]);
910
911                 /* modify channel to givne type, also sets media type */
912                 modify_lchan(param->progressinfo.rtpinfo.media_types[0]);
913
914                 /* connect RTP */
915                 p_g_rtp_ip_remote = param->progressinfo.rtpinfo.ip;
916                 p_g_rtp_port_remote = param->progressinfo.rtpinfo.port;
917                 /* p_g_media_type is already set by modify_lchan() */
918                 p_g_payload_type = param->progressinfo.rtpinfo.payload_types[0];
919                 send_mncc_rtp_connect();
920         }
921 }
922
923 /* MESSAGE_ALERTING */
924 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
925 {
926         struct gsm_mncc *mncc;
927
928         /* send alert */
929         gsm_trace_header(p_interface_name, this, MNCC_ALERT_REQ, DIRECTION_OUT);
930         mncc = create_mncc(MNCC_ALERT_REQ, p_g_callref);
931         if (p_g_tones) {
932                 mncc->fields |= MNCC_F_PROGRESS;
933                 mncc->progress.coding = 3; /* GSM */
934                 mncc->progress.location = 1;
935                 mncc->progress.descr = 8;
936                 add_trace("progress", "coding", "%d", mncc->progress.coding);
937                 add_trace("progress", "location", "%d", mncc->progress.location);
938                 add_trace("progress", "descr", "%d", mncc->progress.descr);
939         }
940         end_trace();
941         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
942
943         new_state(PORT_STATE_IN_ALERTING);
944
945         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
946                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
947                 end_trace();
948                 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
949                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
950                 p_g_tch_connected = 1;
951         }
952
953         /* modify to GSM FR, if not already */
954         if (!p_g_media_type) {
955                 modify_lchan(MEDIA_TYPE_GSM);
956         }
957 }
958
959 /* MESSAGE_CONNECT */
960 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
961 {
962         struct gsm_mncc *mncc;
963
964         /* copy connected information */
965         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
966         /* screen outgoing caller id */
967         do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_interface_name);
968
969         /* send connect */
970         mncc = create_mncc(MNCC_SETUP_RSP, p_g_callref);
971         gsm_trace_header(p_interface_name, this, MNCC_SETUP_RSP, DIRECTION_OUT);
972         /* caller information */
973         mncc->fields |= MNCC_F_CONNECTED;
974         mncc->connected.plan = 1;
975         switch (p_callerinfo.ntype) {
976                 case INFO_NTYPE_UNKNOWN:
977                 mncc->connected.type = 0x0;
978                 break;
979                 case INFO_NTYPE_INTERNATIONAL:
980                 mncc->connected.type = 0x1;
981                 break;
982                 case INFO_NTYPE_NATIONAL:
983                 mncc->connected.type = 0x2;
984                 break;
985                 case INFO_NTYPE_SUBSCRIBER:
986                 mncc->connected.type = 0x4;
987                 break;
988                 default: /* INFO_NTYPE_NOTPRESENT */
989                 mncc->fields &= ~MNCC_F_CONNECTED;
990                 break;
991         }
992         switch (p_callerinfo.screen) {
993                 case INFO_SCREEN_USER:
994                 mncc->connected.screen = 0;
995                 break;
996                 default: /* INFO_SCREEN_NETWORK */
997                 mncc->connected.screen = 3;
998                 break;
999         }
1000         switch (p_callerinfo.present) {
1001                 case INFO_PRESENT_ALLOWED:
1002                 mncc->connected.present = 0;
1003                 break;
1004                 case INFO_PRESENT_RESTRICTED:
1005                 mncc->connected.present = 1;
1006                 break;
1007                 default: /* INFO_PRESENT_NOTAVAIL */
1008                 mncc->connected.present = 2;
1009                 break;
1010         }
1011         if (mncc->fields & MNCC_F_CONNECTED) {
1012                 SCPY(mncc->connected.number, p_connectinfo.id);
1013                 add_trace("connected", "type", "%d", mncc->connected.type);
1014                 add_trace("connected", "plan", "%d", mncc->connected.plan);
1015                 add_trace("connected", "present", "%d", mncc->connected.present);
1016                 add_trace("connected", "screen", "%d", mncc->connected.screen);
1017                 add_trace("connected", "number", "%s", mncc->connected.number);
1018         }
1019         end_trace();
1020         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
1021
1022         new_state(PORT_STATE_CONNECT_WAITING);
1023
1024         if (param->connectinfo.rtpinfo.port) {
1025                 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->connectinfo.rtpinfo.ip, param->connectinfo.rtpinfo.port);
1026
1027                 /* modify channel to givne type, also sets media type */
1028                 modify_lchan(param->connectinfo.rtpinfo.media_types[0]);
1029
1030                 /* connect RTP */
1031                 p_g_rtp_ip_remote = param->connectinfo.rtpinfo.ip;
1032                 p_g_rtp_port_remote = param->connectinfo.rtpinfo.port;
1033                 /* p_g_media_type is already set by modify_lchan() */
1034                 p_g_payload_type = param->connectinfo.rtpinfo.payload_types[0];
1035                 send_mncc_rtp_connect();
1036         }
1037 }
1038
1039 /* MESSAGE_DISCONNECT */
1040 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1041 {
1042         struct gsm_mncc *mncc;
1043
1044         /* send disconnect */
1045         mncc = create_mncc(MNCC_DISC_REQ, p_g_callref);
1046         gsm_trace_header(p_interface_name, this, MNCC_DISC_REQ, DIRECTION_OUT);
1047         if (p_g_tones) {
1048                 mncc->fields |= MNCC_F_PROGRESS;
1049                 mncc->progress.coding = 3; /* GSM */
1050                 mncc->progress.location = 1;
1051                 mncc->progress.descr = 8;
1052                 add_trace("progress", "coding", "%d", mncc->progress.coding);
1053                 add_trace("progress", "location", "%d", mncc->progress.location);
1054                 add_trace("progress", "descr", "%d", mncc->progress.descr);
1055         }
1056         mncc->fields |= MNCC_F_CAUSE;
1057         mncc->cause.coding = 3;
1058         mncc->cause.location = param->disconnectinfo.location;
1059         mncc->cause.value = param->disconnectinfo.cause;
1060         add_trace("cause", "coding", "%d", mncc->cause.coding);
1061         add_trace("cause", "location", "%d", mncc->cause.location);
1062         add_trace("cause", "value", "%d", mncc->cause.value);
1063 #ifdef WITH_GSM_MS
1064         /* special case for BS mode */
1065         if (param->disconnectinfo.transfer.result && (p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
1066                 ((class Pgsm_bs *)this)->enc_ie_facility_ect(mncc, &param->disconnectinfo.transfer);
1067                 gsm_trace_facility((unsigned char *)mncc->facility.info, mncc->facility.len);
1068         }
1069 #endif
1070         end_trace();
1071         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
1072
1073         new_state(PORT_STATE_OUT_DISCONNECT);
1074
1075         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
1076                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1077                 end_trace();
1078                 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
1079                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
1080                 p_g_tch_connected = 1;
1081         }
1082 }
1083
1084
1085 /* MESSAGE_RELEASE */
1086 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1087 {
1088         struct gsm_mncc *mncc;
1089
1090         /* send release */
1091         mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
1092         gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
1093         mncc->fields |= MNCC_F_CAUSE;
1094         mncc->cause.coding = 3;
1095         mncc->cause.location = param->disconnectinfo.location;
1096         mncc->cause.value = param->disconnectinfo.cause;
1097         add_trace("cause", "coding", "%d", mncc->cause.coding);
1098         add_trace("cause", "location", "%d", mncc->cause.location);
1099         add_trace("cause", "value", "%d", mncc->cause.value);
1100         end_trace();
1101         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
1102
1103         new_state(PORT_STATE_RELEASE);
1104         trigger_work(&p_g_delete);
1105         return;
1106 }
1107
1108 /*
1109  * endpoint sends messages to the port
1110  */
1111 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1112 {
1113         int ret = 0;
1114
1115         if (Port::message_epoint(epoint_id, message_id, param))
1116                 return 1;
1117
1118         switch(message_id) {
1119                 case MESSAGE_NOTIFY: /* display and notifications */
1120                 ret = 1;
1121                 message_notify(epoint_id, message_id, param);
1122                 break;
1123
1124 //              case MESSAGE_FACILITY: /* facility message */
1125 //              message_facility(epoint_id, message_id, param);
1126 //              break;
1127
1128                 case MESSAGE_PROCEEDING: /* message not handles */
1129                 ret = 1;
1130                 break;
1131
1132                 case MESSAGE_PROGRESS:
1133                 ret = 1;
1134                 message_progress(epoint_id, message_id, param);
1135                 break;
1136
1137                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1138                 ret = 1;
1139                 if (p_state!=PORT_STATE_IN_PROCEEDING)
1140                         break;
1141                 message_alerting(epoint_id, message_id, param);
1142                 if (p_g_notify_pending) {
1143                         /* send pending notify message during connect */
1144                         message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
1145                         message_free(p_g_notify_pending);
1146                         p_g_notify_pending = NULL;
1147                 }
1148                 break;
1149
1150                 case MESSAGE_CONNECT: /* call of endpoint is connected */
1151                 ret = 1;
1152                 if (p_state!=PORT_STATE_IN_PROCEEDING
1153                  && p_state!=PORT_STATE_IN_ALERTING)
1154                         break;
1155                 message_connect(epoint_id, message_id, param);
1156                 if (p_g_notify_pending) {
1157                         /* send pending notify message during connect */
1158                         message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
1159                         message_free(p_g_notify_pending);
1160                         p_g_notify_pending = NULL;
1161                 }
1162                 break;
1163
1164                 case MESSAGE_DISCONNECT: /* call has been disconnected */
1165                 ret = 1;
1166                 if (p_state!=PORT_STATE_IN_PROCEEDING
1167                  && p_state!=PORT_STATE_IN_ALERTING
1168                  && p_state!=PORT_STATE_OUT_SETUP
1169                  && p_state!=PORT_STATE_OUT_OVERLAP
1170                  && p_state!=PORT_STATE_OUT_PROCEEDING
1171                  && p_state!=PORT_STATE_OUT_ALERTING
1172                  && p_state!=PORT_STATE_CONNECT
1173                  && p_state!=PORT_STATE_CONNECT_WAITING)
1174                         break;
1175                 message_disconnect(epoint_id, message_id, param);
1176                 break;
1177
1178                 case MESSAGE_RELEASE: /* release isdn port */
1179                 ret = 1;
1180                 if (p_state==PORT_STATE_RELEASE)
1181                         break;
1182                 message_release(epoint_id, message_id, param);
1183                 break;
1184
1185         }
1186
1187         return ret;
1188 }
1189
1190 /* deletes only if l3id is release, otherwhise it will be triggered then */
1191 static int delete_event(struct lcr_work *work, void *instance, int index)
1192 {
1193         class Pgsm *gsmport = (class Pgsm *)instance;
1194
1195         delete gsmport;
1196
1197         return 0;
1198 }
1199
1200 int gsm_exit(int rc)
1201 {
1202         return(rc);
1203 }
1204
1205 int gsm_init(void)
1206 {
1207         /* seed the PRNG */
1208         srand(time(NULL));
1209
1210         return 0;
1211 }
1212
1213 /*
1214  * MNCC interface
1215  */
1216
1217 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
1218 {
1219         struct mncc_q_entry *qe;
1220
1221         qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
1222         if (!qe)
1223                 return -ENOMEM;
1224
1225         qe->next = NULL;
1226         qe->len = len;
1227         memcpy(qe->data, mncc, len);
1228
1229         /* in case of empty list ... */
1230         if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
1231                 /* the list head and tail both point to the new qe */
1232                 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
1233         } else {
1234                 /* append to tail of list */
1235                 lcr_gsm->mncc_q_tail->next = qe;
1236                 lcr_gsm->mncc_q_tail = qe;
1237         }
1238
1239         lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
1240
1241         return 0;
1242 }
1243
1244 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
1245 {
1246         struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
1247         if (!qe)
1248                 return NULL;
1249
1250         /* dequeue the successfully sent message */
1251         lcr_gsm->mncc_q_hd = qe->next;
1252         if (!qe)
1253                 return NULL;
1254         if (qe == lcr_gsm->mncc_q_tail)
1255                 lcr_gsm->mncc_q_tail = NULL;
1256
1257         return qe;
1258 }
1259
1260 /* routine called by LCR code if it wants to send a message to OpenBSC */
1261 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
1262 {
1263         int len = 0;
1264
1265         /* FIXME: the caller should provide this */
1266         switch (msg_type) {
1267         case GSM_TCHF_FRAME:
1268                 len = sizeof(struct gsm_data_frame) + 33;
1269                 break;
1270         default:
1271                 len = sizeof(struct gsm_mncc);
1272                 break;
1273         }
1274                 
1275         return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
1276 }
1277
1278 /* close MNCC socket */
1279 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
1280 {
1281         class Port *port;
1282         class Pgsm *pgsm = NULL;
1283         struct lcr_msg *message;
1284
1285         PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
1286         close(lfd->fd);
1287         unregister_fd(lfd);
1288         lfd->fd = -1;
1289
1290         /* free all the calls that were running through the MNCC interface */
1291         port = port_first;
1292         while(port) {
1293                 if ((port->p_type & PORT_CLASS_MASK) == PORT_CLASS_GSM) {
1294                         pgsm = (class Pgsm *)port;
1295                         if (pgsm->p_g_lcr_gsm == lcr_gsm) {
1296                                 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1297                                 message->param.disconnectinfo.cause = 27; // temp. unavail.
1298                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1299                                 message_put(message);
1300                                 pgsm->new_state(PORT_STATE_RELEASE);
1301                                 trigger_work(&pgsm->p_g_delete);
1302                         }
1303                 }
1304                 port = port->next;
1305         }
1306
1307         /* flush the queue */
1308         while (mncc_q_dequeue(lcr_gsm))
1309                 ;
1310
1311         /* start the re-connect timer */
1312         schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1313
1314         return 0;
1315 }
1316
1317 /* write to OpenBSC via MNCC socket */
1318 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1319 {
1320         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1321         struct mncc_q_entry *qe, *qe2;
1322         int rc;
1323
1324         while (1) {
1325                 qe = lcr_gsm->mncc_q_hd;
1326                 if (!qe) {
1327                         lfd->when &= ~LCR_FD_WRITE;
1328                         break;
1329                 }
1330                 rc = write(lfd->fd, qe->data, qe->len);
1331                 if (rc == 0)
1332                         return mncc_fd_close(lcr_gsm, lfd);
1333                 if (rc < 0)
1334                         return rc;
1335                 if (rc < (int)qe->len)
1336                         return -1;
1337                 /* dequeue the successfully sent message */
1338                 qe2 = mncc_q_dequeue(lcr_gsm);
1339                 assert(qe == qe2);
1340                 free(qe);
1341         }
1342         return 0;
1343 }
1344
1345 /* read from OpenBSC via MNCC socket */
1346 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1347 {
1348         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1349         int rc;
1350         static char buf[sizeof(struct gsm_mncc)+1024];
1351         struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1352         struct gsm_mncc_hello *hello = (struct gsm_mncc_hello *) buf;
1353
1354         memset(buf, 0, sizeof(buf));
1355         rc = recv(lfd->fd, buf, sizeof(buf), 0);
1356         if (rc == 0)
1357                 return mncc_fd_close(lcr_gsm, lfd);
1358         if (rc < 0)
1359                 return rc;
1360
1361         /* TODO: size check? */
1362         switch (mncc_prim->msg_type) {
1363         case MNCC_SOCKET_HELLO:
1364                 if (hello->version != MNCC_SOCK_VERSION) {
1365                         PERROR("MNCC version different. BSC version is %u\n", hello->version);
1366                         mncc_fd_close(lcr_gsm, lfd);
1367                         return 0;
1368                 }
1369                 if (hello->mncc_size != sizeof(struct gsm_mncc)) {
1370                         PERROR("MNCC gsm_mncc size differs: %u %u\n",
1371                                 hello->mncc_size, sizeof(struct gsm_mncc));
1372                         mncc_fd_close(lcr_gsm, lfd);
1373                         return 0;
1374                 }
1375                 if (hello->data_frame_size != sizeof(struct gsm_data_frame)) {
1376                         PERROR("MNCC gsm_mncc size differs: %u %u\n",
1377                                 hello->data_frame_size, sizeof(struct gsm_data_frame));
1378                         mncc_fd_close(lcr_gsm, lfd);
1379                         return 0;
1380                 }
1381
1382 #define CHECK_OFFSET(hello, field, lcr_gsm, lfd)        \
1383                 if (hello->field ##_offset != __builtin_offsetof(struct gsm_mncc, field)) {     \
1384                         PERROR("MNCC gsm_mncc offset of %s is %u %u\n",                         \
1385                                 #field, hello->field ##_offset,                                 \
1386                                 __builtin_offsetof(struct gsm_mncc, field));                    \
1387                         mncc_fd_close(lcr_gsm, lfd);                                            \
1388                         return 0;                                                               \
1389                 }
1390
1391                 CHECK_OFFSET(hello, called, lcr_gsm, lfd);
1392                 CHECK_OFFSET(hello, signal, lcr_gsm, lfd);
1393                 CHECK_OFFSET(hello, emergency, lcr_gsm, lfd);
1394                 CHECK_OFFSET(hello, lchan_type, lcr_gsm, lfd);
1395 #undef CHECK_OFFSET
1396
1397                 break;
1398         }
1399
1400         /* Hand the MNCC message into LCR */
1401         switch (lcr_gsm->type) {
1402 #ifdef WITH_GSM_BS
1403         case LCR_GSM_TYPE_NETWORK:
1404                 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1405 #endif
1406 #ifdef WITH_GSM_MS
1407         case LCR_GSM_TYPE_MS:
1408                 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1409 #endif
1410         default:
1411                 return 0;
1412         }
1413 }
1414
1415 /* file descriptor callback if we can read or write form MNCC socket */
1416 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1417 {
1418         int rc = 0;
1419
1420         if (what & LCR_FD_READ)
1421                 rc = mncc_fd_read(lfd, inst, idx);
1422         if (rc < 0)
1423                 return rc;
1424
1425         if (what & LCR_FD_WRITE)
1426                 rc = mncc_fd_write(lfd, inst, idx);
1427
1428         return rc;
1429 }
1430
1431 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1432 {
1433         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1434         int fd, rc;
1435
1436         lcr_gsm->mncc_lfd.fd = -1;
1437
1438         fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1439         if (fd < 0) {
1440                 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1441                 return fd;
1442         }
1443
1444         rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1445                      sizeof(lcr_gsm->sun));
1446         if (rc < 0) {
1447                 PERROR("Could not connect to MNCC socket %s, "
1448                         "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1449                         SOCKET_RETRY_TIMER);
1450                 close(fd);
1451                 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1452         } else {
1453                 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1454                 lcr_gsm->mncc_lfd.fd = fd;
1455                 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);
1456         }
1457
1458         return 0;
1459 }
1460