Add support for TCH/H and half rate codec
[lcr.git] / gsm_bs.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN gsm (BS mode)                                                       **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13 #include "mncc.h"
14
15 struct lcr_gsm *gsm_bs = NULL;
16
17 #define PAYLOAD_TYPE_GSM 3
18
19 /*
20  * DTMF stuff
21  */
22 unsigned char dtmf_samples[16][8000];
23 static int dtmf_x[4] = { 1209, 1336, 1477, 1633 };
24 static int dtmf_y[4] = { 697, 770, 852, 941 };
25
26 void generate_dtmf(void)
27 {
28         double fx, fy, sample;
29         int i, x, y;
30         unsigned char *law;
31
32         for (y = 0; y < 4; y++) {
33                 fy = 2 * 3.1415927 * ((double)dtmf_y[y]) / 8000.0;
34                 for (x = 0; x < 4; x++) {
35                         fx = 2 * 3.1415927 * ((double)dtmf_x[x]) / 8000.0;
36                         law = dtmf_samples[y << 2 | x];
37                         for (i = 0; i < 8000; i++) {
38                                 sample = sin(fy * ((double)i)) * 0.251 * 32767.0; /* -6 dB */
39                                 sample += sin(fx * ((double)i)) * 0.158 * 32767.0; /* -8 dB */
40                                 *law++ = audio_s16_to_law[(int)sample & 0xffff];
41                         }
42                 }
43         }
44 }
45
46
47 /*
48  * constructor
49  */
50 Pgsm_bs::Pgsm_bs(int type, char *portname, struct port_settings *settings, struct interface *interface) : Pgsm(type, portname, settings, interface)
51 {
52         p_g_lcr_gsm = gsm_bs;
53         p_g_dtmf = NULL;
54         p_g_dtmf_index = 0;
55
56         PDEBUG(DEBUG_GSM, "Created new GSMBSPort(%s).\n", portname);
57 }
58
59 /*
60  * destructor
61  */
62 Pgsm_bs::~Pgsm_bs()
63 {
64         PDEBUG(DEBUG_GSM, "Destroyed GSM BS process(%s).\n", p_name);
65 }
66
67 static const char *media_type2name(unsigned char media_type) {
68         switch (media_type) {
69         case MEDIA_TYPE_ULAW:
70                 return "PCMU";
71         case MEDIA_TYPE_ALAW:
72                 return "PCMA";
73         case MEDIA_TYPE_GSM:
74                 return "GSM";
75         case MEDIA_TYPE_GSM_HR:
76                 return "GSM-HR";
77         case MEDIA_TYPE_GSM_EFR:
78                 return "GSM-EFR";
79         case MEDIA_TYPE_AMR:
80                 return "AMR";
81         }
82
83         return "UKN";
84 }
85
86 /* PROCEEDING INDICATION (from MS) */
87 void Pgsm_bs::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
88 {
89         int media_types[8];
90         unsigned char payload_types[8];
91         int payloads = 0;
92
93         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
94         if (mncc->fields & MNCC_F_CAUSE) {
95                 add_trace("cause", "coding", "%d", mncc->cause.coding);
96                 add_trace("cause", "location", "%", mncc->cause.location);
97                 add_trace("cause", "value", "%", mncc->cause.value);
98         }
99         end_trace();
100
101         new_state(PORT_STATE_OUT_PROCEEDING);
102
103         /* get list of offered payload types
104          * if list ist empty, the FR V1 is selected */
105         select_payload_type(mncc, payload_types, media_types, &payloads, sizeof(payload_types));
106         /* if no given payload type is supported, we select from channel type */
107         if (!payloads) {
108                 switch (mncc->lchan_type) {
109                 case GSM_LCHAN_TCH_F:
110                         media_types[0] = MEDIA_TYPE_GSM;
111                         payload_types[0] = PAYLOAD_TYPE_GSM;
112                         payloads = 1;
113                         break;
114                 case GSM_LCHAN_TCH_H:
115                         media_types[0] = MEDIA_TYPE_GSM_HR;
116                         payload_types[0] = 96; /* dynamic */
117                         payloads = 1;
118                         break;
119                 default:
120                         mncc = create_mncc(MNCC_REL_REQ, callref);
121                         gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
122                         mncc->fields |= MNCC_F_CAUSE;
123                         mncc->cause.coding = 3;
124                         mncc->cause.location = 1;
125                         mncc->cause.value = 65;
126                         add_trace("cause", "coding", "%d", mncc->cause.coding);
127                         add_trace("cause", "location", "%d", mncc->cause.location);
128                         add_trace("cause", "value", "%d", mncc->cause.value);
129                         add_trace("reason", NULL, "Given lchan not supported");
130                         end_trace();
131                         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
132                         new_state(PORT_STATE_RELEASE);
133                         trigger_work(&p_g_delete);
134                         return;
135                 }
136         }
137
138         /* select first payload type that matches the rtp list */
139         if (p_g_rtp_bridge) {
140                 int i, j;
141
142                 for (i = 0; i < p_g_rtp_payloads; i++) {
143                         for (j = 0; j < payloads; j++) {
144                                 if (p_g_rtp_media_types[i] == media_types[j])
145                                         break;
146                         }
147                         if (j < payloads)
148                                 break;
149                 }
150                 if (i == p_g_rtp_payloads) {
151                         struct lcr_msg *message;
152
153                         /* payload offered by remote RTP is not supported */
154                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
155                         message->param.disconnectinfo.cause = 65;
156                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
157                         message_put(message);
158                         /* send release */
159                         mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
160                         gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
161                         mncc->fields |= MNCC_F_CAUSE;
162                         mncc->cause.coding = 3;
163                         mncc->cause.location = LOCATION_PRIVATE_LOCAL;
164                         mncc->cause.value = 65;
165                         add_trace("cause", "coding", "%d", mncc->cause.coding);
166                         add_trace("cause", "location", "%d", mncc->cause.location);
167                         add_trace("cause", "value", "%d", mncc->cause.value);
168                         add_trace("reason", NULL, "None of the payload types are supported by MS");
169                         end_trace();
170                         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
171                         new_state(PORT_STATE_RELEASE);
172                         trigger_work(&p_g_delete);
173
174                         return;
175                 }
176                 modify_lchan(p_g_rtp_media_types[i]);
177                 /* use the payload type from received rtp list, not from locally generated payload types */
178                 p_g_payload_type = p_g_rtp_payload_types[i];
179         } else {
180                 /* modify to first given payload */
181                 modify_lchan(media_types[0]);
182                 p_g_payload_type = payload_types[0];
183         }
184 }
185
186 /* DTMF INDICATION */
187 void Pgsm_bs::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
188 {
189         struct gsm_mncc *resp;
190
191         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
192         add_trace("keypad", NULL, "%c", mncc->keypad);
193         end_trace();
194         SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
195         p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
196
197         /* send resp */
198         gsm_trace_header(p_interface_name, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
199         add_trace("keypad", NULL, "%c", mncc->keypad);
200         end_trace();
201         resp = create_mncc(MNCC_START_DTMF_RSP, p_g_callref);
202         resp->fields |= MNCC_F_KEYPAD;
203         resp->keypad = mncc->keypad;
204         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
205
206         if (p_g_rtp_bridge) {
207                 /* if two members are bridged */
208                 if (p_bridge && p_bridge->first && p_bridge->first->next && !p_bridge->first->next->next) {
209                         class Port *remote = NULL;
210
211                         /* select other member */
212                         if (p_bridge->first->port == this)
213                                 remote = p_bridge->first->next->port;
214                         if (p_bridge->first->next->port == this)
215                                 remote = p_bridge->first->port;
216
217                         if (remote) {
218                                 struct lcr_msg *message;
219
220                                 /* send dtmf information, because we bridge RTP directly */
221                                 message = message_create(0, remote->p_serial, EPOINT_TO_PORT, MESSAGE_DTMF);
222                                 message->param.dtmf = mncc->keypad;
223                                 message_put(message);
224                         }
225                 }
226         } else {
227                 /* generate DTMF tones, since we do audio forwarding inside LCR */
228                 switch (mncc->keypad) {
229                         case '1': p_g_dtmf = dtmf_samples[0]; break;
230                         case '2': p_g_dtmf = dtmf_samples[1]; break;
231                         case '3': p_g_dtmf = dtmf_samples[2]; break;
232                         case 'a':
233                         case 'A': p_g_dtmf = dtmf_samples[3]; break;
234                         case '4': p_g_dtmf = dtmf_samples[4]; break;
235                         case '5': p_g_dtmf = dtmf_samples[5]; break;
236                         case '6': p_g_dtmf = dtmf_samples[6]; break;
237                         case 'b':
238                         case 'B': p_g_dtmf = dtmf_samples[7]; break;
239                         case '7': p_g_dtmf = dtmf_samples[8]; break;
240                         case '8': p_g_dtmf = dtmf_samples[9]; break;
241                         case '9': p_g_dtmf = dtmf_samples[10]; break;
242                         case 'c':
243                         case 'C': p_g_dtmf = dtmf_samples[11]; break;
244                         case '*': p_g_dtmf = dtmf_samples[12]; break;
245                         case '0': p_g_dtmf = dtmf_samples[13]; break;
246                         case '#': p_g_dtmf = dtmf_samples[14]; break;
247                         case 'd':
248                         case 'D': p_g_dtmf = dtmf_samples[15]; break;
249                 }
250                 p_g_dtmf_index = 0;
251         }
252 }
253 void Pgsm_bs::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
254 {
255         struct gsm_mncc *resp;
256
257         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
258         add_trace("keypad", NULL, "%c", mncc->keypad);
259         end_trace();
260
261         /* send resp */
262         gsm_trace_header(p_interface_name, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
263         add_trace("keypad", NULL, "%c", mncc->keypad);
264         end_trace();
265         resp = create_mncc(MNCC_STOP_DTMF_RSP, p_g_callref);
266         resp->keypad = mncc->keypad;
267         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
268         
269         /* stop DTMF */
270         p_g_dtmf = NULL;
271 }
272
273 /* HOLD INDICATION */
274 void Pgsm_bs::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
275 {
276         struct lcr_msg *message;
277         struct gsm_mncc *resp, *frame;
278
279         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
280         end_trace();
281
282         /* notify the hold of call */
283         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
284         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
285         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
286         message_put(message);
287
288         /* acknowledge hold */
289         gsm_trace_header(p_interface_name, this, MNCC_HOLD_CNF, DIRECTION_OUT);
290         end_trace();
291         resp = create_mncc(MNCC_HOLD_CNF, p_g_callref);
292         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
293
294         /* disable audio */
295         if (p_g_tch_connected) { /* it should be true */
296                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_DROP, DIRECTION_OUT);
297                 end_trace();
298                 frame = create_mncc(MNCC_FRAME_DROP, p_g_callref);
299                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
300                 p_g_tch_connected = 0;
301         }
302 }
303
304
305 /* RETRIEVE INDICATION */
306 void Pgsm_bs::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
307 {
308         struct lcr_msg *message;
309         struct gsm_mncc *resp, *frame;
310
311         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
312         end_trace();
313
314         /* notify the retrieve of call */
315         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
316         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
317         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
318         message_put(message);
319
320         /* acknowledge retr */
321         gsm_trace_header(p_interface_name, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
322         end_trace();
323         resp = create_mncc(MNCC_RETRIEVE_CNF, p_g_callref);
324         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
325
326         /* enable audio */
327         if (!p_g_tch_connected) { /* it should be true */
328                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
329                 end_trace();
330                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
331                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
332                 p_g_tch_connected = 1;
333         }
334 }
335
336 /*
337  * select payload type by given list or GSM V1 FR 
338  * return the payload type or 0 if not given 
339  */
340
341 void Pgsm_bs::select_payload_type(struct gsm_mncc *mncc, unsigned char *payload_types, int *media_types, int *payloads, int max_payloads)
342 {
343         int media_type;
344         unsigned char payload_type;
345         int half;
346         void *encoder, *decoder;
347
348         *payloads = 0;
349
350         gsm_trace_header(p_interface_name, this, 1 /* codec negotioation */, DIRECTION_NONE);
351         if ((mncc->fields & MNCC_F_BEARER_CAP)) {
352                 /* select preferred payload type from list */
353                 int i;
354                 unsigned char dynamic_type = 96;
355
356                 add_trace("bearer", "capa", "given by MS");
357                 for (i = 0; mncc->bearer_cap.speech_ver[i] >= 0; i++) {
358                         half = 0;
359                         /* select payload type we support */
360                         switch (mncc->bearer_cap.speech_ver[i]) {
361                         case 0:
362                                 add_trace("speech", "version", "Full Rate given");
363                                 media_type = MEDIA_TYPE_GSM;
364                                 payload_type = PAYLOAD_TYPE_GSM;
365                                 encoder = p_g_fr_encoder;
366                                 decoder = p_g_fr_decoder;
367                                 break;
368                         case 2:
369                                 add_trace("speech", "version", "EFR given");
370                                 media_type = MEDIA_TYPE_GSM_EFR;
371                                 payload_type = dynamic_type++;
372                                 encoder = p_g_amr_encoder;
373                                 decoder = p_g_amr_decoder;
374                                 break;
375                         case 4:
376                                 add_trace("speech", "version", "AMR given");
377                                 media_type = MEDIA_TYPE_AMR;
378                                 payload_type = dynamic_type++;
379                                 encoder = p_g_amr_encoder;
380                                 decoder = p_g_amr_decoder;
381                                 break;
382                         case 1:
383                                 add_trace("speech", "version", "Half Rate given");
384                                 media_type = MEDIA_TYPE_GSM_HR;
385                                 payload_type = dynamic_type++;
386                                 encoder = p_g_hr_encoder;
387                                 decoder = p_g_hr_decoder;
388                                 half = 1;
389                                 break;
390                         case 5:
391                                 add_trace("speech", "version", "AMR Half Rate given");
392                                 media_type = MEDIA_TYPE_AMR;
393                                 payload_type = dynamic_type++;
394                                 encoder = p_g_amr_encoder;
395                                 decoder = p_g_amr_decoder;
396                                 half = 1;
397                                 break;
398                         default:
399                                 add_trace("speech", "version", "%d given", mncc->bearer_cap.speech_ver[i]);
400                                 media_type = 0;
401                                 payload_type = 0;
402                         }
403                         /* wen don't support it, so we check the next */
404                         if (!media_type) {
405                                 add_trace("speech", "ignored", "Not supported by LCR");
406                                 continue;
407                         }
408                         if (!half && mncc->lchan_type != GSM_LCHAN_TCH_F) {
409                                 add_trace("speech", "ignored", "Not TCH/F");
410                                 continue;
411                         }
412                         if (half && mncc->lchan_type != GSM_LCHAN_TCH_H) {
413                                 add_trace("speech", "ignored", "Not TCH/H");
414                                 continue;
415                         }
416                         if (!p_g_rtp_bridge) {
417                                 if (!encoder || !decoder) {
418                                         add_trace("speech", "ignored", "Codec not supported");
419                                         continue;
420                                 }
421                         }
422                         if (*payloads <= max_payloads) {
423                                 media_types[*payloads] = media_type;
424                                 payload_types[*payloads] = payload_type;
425                                 (*payloads)++;
426                         }
427                 }
428         } else {
429                 add_trace("bearer", "capa", "not given by MS");
430                 add_trace("speech", "version", "Full Rate given");
431                 media_types[0] = MEDIA_TYPE_GSM;
432                 payload_types[0] = PAYLOAD_TYPE_GSM;
433                 *payloads = 1;
434         }
435         if (!(*payloads))
436                 add_trace("error", "", "All given payload types unsupported");
437         end_trace();
438 }
439
440 /*
441  * handles all indications
442  */
443 /* SETUP INDICATION */
444 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
445 {
446         class Endpoint *epoint;
447         struct lcr_msg *message;
448         struct gsm_mncc *proceeding, *frame;
449         struct interface *interface;
450         int media_types[8];
451         unsigned char payload_types[8];
452         int payloads = 0;
453
454         interface = getinterfacebyname(p_interface_name);
455         if (!interface) {
456                 PERROR("Cannot find interface %s.\n", p_interface_name);
457                 return;
458         }
459
460         /* process given callref */
461         gsm_trace_header(p_interface_name, this, 0, DIRECTION_IN);
462         add_trace("callref", "new", "0x%x", callref);
463         if (p_g_callref) {
464                 /* release in case the ID is already in use */
465                 add_trace("error", NULL, "callref already in use");
466                 end_trace();
467                 mncc = create_mncc(MNCC_REJ_REQ, callref);
468                 gsm_trace_header(p_interface_name, this, MNCC_REJ_REQ, DIRECTION_OUT);
469                 mncc->fields |= MNCC_F_CAUSE;
470                 mncc->cause.coding = 3;
471                 mncc->cause.location = 1;
472                 mncc->cause.value = 47;
473                 add_trace("cause", "coding", "%d", mncc->cause.coding);
474                 add_trace("cause", "location", "%d", mncc->cause.location);
475                 add_trace("cause", "value", "%d", mncc->cause.value);
476                 add_trace("reason", NULL, "callref already in use");
477                 end_trace();
478                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
479                 new_state(PORT_STATE_RELEASE);
480                 trigger_work(&p_g_delete);
481                 return;
482         }
483         p_g_callref = callref;
484         end_trace();
485
486         /* caller info */
487         if (mncc->clir.inv)
488                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
489         else
490                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
491         if (mncc->calling.number[0])
492                 SCPY(p_callerinfo.id, mncc->calling.number);
493         else
494                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
495         SCPY(p_callerinfo.imsi, mncc->imsi);
496         p_callerinfo.screen = INFO_SCREEN_NETWORK;
497         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
498         SCPY(p_callerinfo.interface, p_interface_name);
499
500         /* dialing information */
501         SCAT(p_dialinginfo.id, mncc->called.number);
502         switch (mncc->called.type) {
503                 case 0x1:
504                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
505                 break;
506                 case 0x2:
507                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
508                 break;
509                 case 0x4:
510                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
511                 break;
512                 default:
513                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
514                 break;
515         }
516         if (mncc->emergency) {
517                 SCPY(p_dialinginfo.id, "emergency");
518         }
519         p_dialinginfo.sending_complete = 1;
520
521         /* bearer capability */
522         p_capainfo.bearer_capa = INFO_BC_SPEECH;
523         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
524         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
525         p_capainfo.source_mode = B_MODE_TRANSPARENT;
526         p_g_mode = p_capainfo.source_mode;
527
528         /* get list of offered payload types
529          * if list ist empty, the FR V1 is selected */
530         select_payload_type(mncc, payload_types, media_types, &payloads, sizeof(payload_types));
531         /* if no given payload type is supported, we select from channel type */
532         if (!payloads) {
533                 switch (mncc->lchan_type) {
534                 case GSM_LCHAN_TCH_F:
535                         media_types[0] = MEDIA_TYPE_GSM;
536                         payload_types[0] = PAYLOAD_TYPE_GSM;
537                         payloads = 1;
538                         break;
539                 case GSM_LCHAN_TCH_H:
540                         media_types[0] = MEDIA_TYPE_GSM_HR;
541                         payload_types[0] = 96; /* dynamic */
542                         payloads = 1;
543                         break;
544                 default:
545                         mncc = create_mncc(MNCC_REJ_REQ, callref);
546                         gsm_trace_header(p_interface_name, this, MNCC_REJ_REQ, DIRECTION_OUT);
547                         mncc->fields |= MNCC_F_CAUSE;
548                         mncc->cause.coding = 3;
549                         mncc->cause.location = 1;
550                         mncc->cause.value = 65;
551                         add_trace("cause", "coding", "%d", mncc->cause.coding);
552                         add_trace("cause", "location", "%d", mncc->cause.location);
553                         add_trace("cause", "value", "%d", mncc->cause.value);
554                         add_trace("reason", NULL, "Given lchan not supported");
555                         end_trace();
556                         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
557                         new_state(PORT_STATE_RELEASE);
558                         trigger_work(&p_g_delete);
559                         return;
560                 }
561         }
562 #if 0
563         /* if no given payload type is supported, we reject the call */
564         if (!payloads) {
565         }
566 #endif
567
568         /* useruser */
569
570         /* what infos did we got ... */
571         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
572         if (p_callerinfo.id[0])
573                 add_trace("calling", "number", "%s", p_callerinfo.id);
574         else
575                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
576         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
577         add_trace("dialing", "number", "%s", p_dialinginfo.id);
578         end_trace();
579
580         /* create endpoint */
581         if (p_epointlist)
582                 FATAL("Incoming call but already got an endpoint.\n");
583         if (!(epoint = new Endpoint(p_serial, 0)))
584                 FATAL("No memory for Endpoint instance\n");
585         epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
586         epointlist_new(epoint->ep_serial);
587
588         /* modify lchan in case of no rtp bridge */
589         if (!p_g_rtp_bridge)
590                 modify_lchan(media_types[0]);
591
592         /* send call proceeding */
593         gsm_trace_header(p_interface_name, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
594         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_g_callref);
595         if (p_g_tones) {
596                 proceeding->fields |= MNCC_F_PROGRESS;
597                 proceeding->progress.coding = 3; /* GSM */
598                 proceeding->progress.location = 1;
599                 proceeding->progress.descr = 8;
600                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
601                 add_trace("progress", "location", "%d", proceeding->progress.location);
602                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
603         }
604         end_trace();
605         send_and_free_mncc(p_g_lcr_gsm, proceeding->msg_type, proceeding);
606
607         new_state(PORT_STATE_IN_PROCEEDING);
608
609         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
610                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
611                 end_trace();
612                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
613                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
614                 p_g_tch_connected = 1;
615         }
616
617         /* send setup message to endpoit */
618         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
619         message->param.setup.port_type = p_type;
620 //      message->param.setup.dtmf = 0;
621         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
622         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
623         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
624         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
625         message->param.setup.useruser.len = strlen(mncc->useruser.info);
626         message->param.setup.useruser.protocol = mncc->useruser.proto;
627         if (p_g_rtp_bridge) {
628                 struct gsm_mncc_rtp *rtp;
629                 int i;
630
631                 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding setup\n");
632                 p_g_setup_pending = message;
633                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
634                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
635
636                 for (i = 0; i < (int)sizeof(message->param.setup.rtpinfo.payload_types) && i < payloads; i++) {
637                         message->param.setup.rtpinfo.media_types[i] = media_types[i];
638                         message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
639                         message->param.setup.rtpinfo.payloads++;
640                 }
641
642         } else
643                 message_put(message);
644
645 }
646
647 /*
648  * BSC sends message to port
649  */
650 int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
651 {
652         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
653         unsigned int callref = mncc->callref;
654         class Port *port;
655         class Pgsm_bs *pgsm_bs = NULL;
656         char name[64];
657 //      struct mISDNport *mISDNport;
658
659         /* Special messages */
660         switch(msg_type) {
661         }
662
663         /* find callref */
664         callref = mncc->callref;
665         port = port_first;
666         while(port) {
667                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
668                         pgsm_bs = (class Pgsm_bs *)port;
669                         if (pgsm_bs->p_g_callref == callref) {
670                                 break;
671                         }
672                 }
673                 port = port->next;
674         }
675
676         if (msg_type == GSM_TCHF_FRAME
677          || msg_type == GSM_TCHF_FRAME_EFR
678          || msg_type == GSM_TCHH_FRAME
679          || msg_type == GSM_TCH_FRAME_AMR
680          || msg_type == GSM_BAD_FRAME) {
681                 if (port) {
682                         /* inject DTMF, if enabled */
683                         if (pgsm_bs->p_g_dtmf) {
684                                 unsigned char data[160];
685                                 int i;
686
687                                 for (i = 0; i < 160; i++) {
688                                         data[i] = pgsm_bs->p_g_dtmf[pgsm_bs->p_g_dtmf_index++];
689                                         if (pgsm_bs->p_g_dtmf_index == 8000)
690                                                 pgsm_bs->p_g_dtmf_index = 0;
691                                 }
692                                 /* send */
693                                 pgsm_bs->bridge_tx(data, 160);
694                         } else
695                                 pgsm_bs->frame_receive(arg);
696                         /* if we do not bridge we need to inject audio, if available */
697                         if (!pgsm_bs->p_bridge || pgsm_bs->p_tone_name[0]) {
698                                 unsigned char data[160];
699                                 int i;
700
701                                 i = pgsm_bs->read_audio(data, 160);
702                                 if (i)
703                                         pgsm_bs->audio_send(data, i);
704                         }
705                 }
706                 return 0;
707         }
708
709         if (!port) {
710                 struct interface *interface;
711
712                 if (msg_type != MNCC_SETUP_IND)
713                         return(0);
714
715                 interface = getinterfacebyname(lcr_gsm->interface_name);
716                 if (!interface) {
717                         struct gsm_mncc *rej;
718
719                         rej = create_mncc(MNCC_REJ_REQ, callref);
720                         rej->fields |= MNCC_F_CAUSE;
721                         rej->cause.coding = 3;
722                         rej->cause.location = 1;
723                         rej->cause.value = 27;
724                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
725                         add_trace("cause", "coding", "%d", rej->cause.coding);
726                         add_trace("cause", "location", "%d", rej->cause.location);
727                         add_trace("cause", "value", "%d", rej->cause.value);
728                         add_trace("reason", NULL, "interface %s not found", lcr_gsm->interface_name);
729                         end_trace();
730                         send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
731                         return 0;
732                 }
733                 /* creating port object, transparent until setup with hdlc */
734                 SPRINT(name, "%s-%d-in", interface->name, 0);
735                 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, name, NULL, interface)))
736                         FATAL("Cannot create Port instance.\n");
737         }
738
739         switch(msg_type) {
740                 case MNCC_SETUP_IND:
741                 pgsm_bs->setup_ind(msg_type, callref, mncc);
742                 break;
743
744                 case MNCC_RTP_CREATE:
745                 pgsm_bs->rtp_create_ind(msg_type, callref, mncc);
746                 break;
747
748                 case MNCC_RTP_CONNECT:
749                 pgsm_bs->rtp_connect_ind(msg_type, callref, mncc);
750                 break;
751
752                 case MNCC_START_DTMF_IND:
753                 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
754                 break;
755
756                 case MNCC_STOP_DTMF_IND:
757                 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
758                 break;
759
760                 case MNCC_CALL_CONF_IND:
761                 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
762                 break;
763
764                 case MNCC_ALERT_IND:
765                 pgsm_bs->alert_ind(msg_type, callref, mncc);
766                 break;
767
768                 case MNCC_SETUP_CNF:
769                 pgsm_bs->setup_cnf(msg_type, callref, mncc);
770                 break;
771
772                 case MNCC_SETUP_COMPL_IND:
773                 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
774                 break;
775
776                 case MNCC_DISC_IND:
777                 pgsm_bs->disc_ind(msg_type, callref, mncc);
778                 break;
779
780                 case MNCC_REL_IND:
781                 case MNCC_REL_CNF:
782                 case MNCC_REJ_IND:
783                 pgsm_bs->rel_ind(msg_type, callref, mncc);
784                 break;
785
786                 case MNCC_NOTIFY_IND:
787                 pgsm_bs->notify_ind(msg_type, callref, mncc);
788                 break;
789
790                 case MNCC_HOLD_IND:
791                 pgsm_bs->hold_ind(msg_type, callref, mncc);
792                 break;
793
794                 case MNCC_RETRIEVE_IND:
795                 pgsm_bs->retr_ind(msg_type, callref, mncc);
796                 break;
797
798                 default:
799                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: 0x%x\n", pgsm_bs->p_name, pgsm_bs->p_callerinfo.id, msg_type);
800         }
801         return(0);
802 }
803
804 /* MESSAGE_SETUP */
805 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
806 {
807         struct lcr_msg *message;
808         struct epoint_list *epointlist;
809         struct gsm_mncc *mncc;
810
811         /* copy setup infos to port */
812         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
813         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
814         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
815         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
816
817         /* no GSM MNCC connection */
818         if (p_g_lcr_gsm->mncc_lfd.fd < 0) {
819                 gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
820                 add_trace("failure", NULL, "No MNCC connection.");
821                 end_trace();
822                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
823                 message->param.disconnectinfo.cause = 41; // temp. failure.
824                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
825                 message_put(message);
826                 new_state(PORT_STATE_RELEASE);
827                 trigger_work(&p_g_delete);
828                 return;
829         }
830
831         /* no number */
832         if (!p_dialinginfo.id[0]) {
833                 gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
834                 add_trace("failure", NULL, "No dialed subscriber given.");
835                 end_trace();
836                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
837                 message->param.disconnectinfo.cause = 28;
838                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
839                 message_put(message);
840                 new_state(PORT_STATE_RELEASE);
841                 trigger_work(&p_g_delete);
842                 return;
843         }
844
845         /* unsupported codec for RTP bridge */
846         if (param->setup.rtpinfo.port) {
847                 int i;
848
849                 p_g_rtp_payloads = 0;
850                 gsm_trace_header(p_interface_name, this, 1 /* codec negotioation */, DIRECTION_NONE);
851                 for (i = 0; i < param->setup.rtpinfo.payloads; i++) {
852                         switch (param->setup.rtpinfo.media_types[i]) {
853                         case MEDIA_TYPE_GSM:
854                         case MEDIA_TYPE_GSM_EFR:
855                         case MEDIA_TYPE_AMR:
856                         case MEDIA_TYPE_GSM_HR:
857                                 add_trace("rtp", "payload", "%s:%d supported", media_type2name(param->setup.rtpinfo.media_types[i]), param->setup.rtpinfo.payload_types[i]);
858                                 if (p_g_rtp_payloads < (int)sizeof(p_g_rtp_payload_types)) {
859                                         p_g_rtp_media_types[p_g_rtp_payloads] = param->setup.rtpinfo.media_types[i];
860                                         p_g_rtp_payload_types[p_g_rtp_payloads] = param->setup.rtpinfo.payload_types[i];
861                                         p_g_rtp_payloads++;
862                                 }
863                                 break;
864                         default:
865                                 add_trace("rtp", "payload", "%s:%d unsupported", media_type2name(param->setup.rtpinfo.media_types[i]), param->setup.rtpinfo.payload_types[i]);
866                         }
867                 }
868                 end_trace();
869                 if (!p_g_rtp_payloads) {
870                         gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
871                         add_trace("failure", NULL, "No payload given that is supported by GSM");
872                         end_trace();
873                         message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
874                         message->param.disconnectinfo.cause = 65;
875                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
876                         message_put(message);
877                         new_state(PORT_STATE_RELEASE);
878                         trigger_work(&p_g_delete);
879                         return;
880                 }
881         }
882
883 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
884         /* screen outgoing caller id */
885         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_interface_name);
886
887         /* attach only if not already */
888         epointlist = p_epointlist;
889         while(epointlist) {
890                 if (epointlist->epoint_id == epoint_id)
891                         break;
892                 epointlist = epointlist->next;
893         }
894         if (!epointlist)
895                 epointlist_new(epoint_id);
896
897         /* creating l3id */
898         gsm_trace_header(p_interface_name, this, 0, DIRECTION_OUT);
899         p_g_callref = new_callref++;
900         add_trace("callref", "new", "0x%x", p_g_callref);
901         end_trace();
902
903         gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
904         mncc = create_mncc(MNCC_SETUP_REQ, p_g_callref);
905         /* caller information */
906         mncc->fields |= MNCC_F_CALLING;
907         mncc->calling.plan = 1;
908         switch (p_callerinfo.ntype) {
909                 case INFO_NTYPE_UNKNOWN:
910                 mncc->calling.type = 0x0;
911                 break;
912                 case INFO_NTYPE_INTERNATIONAL:
913                 mncc->calling.type = 0x1;
914                 break;
915                 case INFO_NTYPE_NATIONAL:
916                 mncc->calling.type = 0x2;
917                 break;
918                 case INFO_NTYPE_SUBSCRIBER:
919                 mncc->calling.type = 0x4;
920                 break;
921                 default: /* INFO_NTYPE_NOTPRESENT */
922                 mncc->fields &= ~MNCC_F_CALLING;
923                 break;
924         }
925         switch (p_callerinfo.screen) {
926                 case INFO_SCREEN_USER:
927                 mncc->calling.screen = 0;
928                 break;
929                 default: /* INFO_SCREEN_NETWORK */
930                 mncc->calling.screen = 3;
931                 break;
932         }
933         switch (p_callerinfo.present) {
934                 case INFO_PRESENT_ALLOWED:
935                 mncc->calling.present = 0;
936                 break;
937                 case INFO_PRESENT_RESTRICTED:
938                 mncc->calling.present = 1;
939                 break;
940                 default: /* INFO_PRESENT_NOTAVAIL */
941                 mncc->calling.present = 2;
942                 break;
943         }
944         if (mncc->fields & MNCC_F_CALLING) {
945                 SCPY(mncc->calling.number, p_callerinfo.id);
946                 add_trace("calling", "type", "%d", mncc->calling.type);
947                 add_trace("calling", "plan", "%d", mncc->calling.plan);
948                 add_trace("calling", "present", "%d", mncc->calling.present);
949                 add_trace("calling", "screen", "%d", mncc->calling.screen);
950                 add_trace("calling", "number", "%s", mncc->calling.number);
951         }
952         /* dialing information */
953         mncc->fields |= MNCC_F_CALLED;
954         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
955                 SCPY(mncc->imsi, p_dialinginfo.id+5);
956                 add_trace("dialing", "imsi", "%s", mncc->imsi);
957         } else {
958                 SCPY(mncc->called.number, p_dialinginfo.id);
959                 add_trace("dialing", "number", "%s", mncc->called.number);
960         }
961         
962         /* sending user-user */
963
964         /* redirecting number */
965         mncc->fields |= MNCC_F_REDIRECTING;
966         mncc->redirecting.plan = 1;
967         switch (p_redirinfo.ntype) {
968                 case INFO_NTYPE_UNKNOWN:
969                 mncc->redirecting.type = 0x0;
970                 break;
971                 case INFO_NTYPE_INTERNATIONAL:
972                 mncc->redirecting.type = 0x1;
973                 break;
974                 case INFO_NTYPE_NATIONAL:
975                 mncc->redirecting.type = 0x2;
976                 break;
977                 case INFO_NTYPE_SUBSCRIBER:
978                 mncc->redirecting.type = 0x4;
979                 break;
980                 default: /* INFO_NTYPE_NOTPRESENT */
981                 mncc->fields &= ~MNCC_F_REDIRECTING;
982                 break;
983         }
984         switch (p_redirinfo.screen) {
985                 case INFO_SCREEN_USER:
986                 mncc->redirecting.screen = 0;
987                 break;
988                 default: /* INFO_SCREE_NETWORK */
989                 mncc->redirecting.screen = 3;
990                 break;
991         }
992         switch (p_redirinfo.present) {
993                 case INFO_PRESENT_ALLOWED:
994                 mncc->redirecting.present = 0;
995                 break;
996                 case INFO_PRESENT_RESTRICTED:
997                 mncc->redirecting.present = 1;
998                 break;
999                 default: /* INFO_PRESENT_NOTAVAIL */
1000                 mncc->redirecting.present = 2;
1001                 break;
1002         }
1003         /* sending redirecting number only in ntmode */
1004         if (mncc->fields & MNCC_F_REDIRECTING) {
1005                 SCPY(mncc->redirecting.number, p_redirinfo.id);
1006                 add_trace("redir", "type", "%d", mncc->redirecting.type);
1007                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
1008                 add_trace("redir", "present", "%d", mncc->redirecting.present);
1009                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
1010                 add_trace("redir", "number", "%s", mncc->redirecting.number);
1011         }
1012
1013         end_trace();
1014         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
1015
1016         new_state(PORT_STATE_OUT_SETUP);
1017
1018         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1019         message_put(message);
1020
1021         /* RTP bridge */
1022         if (param->setup.rtpinfo.port) {
1023                 p_g_rtp_bridge = 1;
1024                 p_g_rtp_ip_remote = param->setup.rtpinfo.ip;
1025                 p_g_rtp_port_remote = param->setup.rtpinfo.port;
1026         } else
1027                 p_g_rtp_bridge = 0;
1028 }
1029
1030 /*
1031  * endpoint sends messages to the port
1032  */
1033 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1034 {
1035         if (Pgsm::message_epoint(epoint_id, message_id, param))
1036                 return(1);
1037
1038         switch(message_id) {
1039                 case MESSAGE_SETUP: /* dial-out command received from epoint */
1040                 if (p_state!=PORT_STATE_IDLE)
1041                         break;
1042                 message_setup(epoint_id, message_id, param);
1043                 break;
1044
1045                 default:
1046                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1047         }
1048
1049         return(1);
1050 }
1051
1052 int gsm_bs_exit(int rc)
1053 {
1054         /* free gsm instance */
1055         if (gsm_bs) {
1056                 if (gsm_bs->mncc_lfd.fd > -1) {
1057                         close(gsm_bs->mncc_lfd.fd);
1058                         unregister_fd(&gsm_bs->mncc_lfd);
1059                 }
1060
1061                 del_timer(&gsm_bs->socket_retry);
1062                 free(gsm_bs);
1063                 gsm_bs = NULL;
1064         }
1065
1066
1067         return(rc);
1068 }
1069
1070 int gsm_bs_init(struct interface *interface)
1071 {
1072         /* create gsm instance */
1073         gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1074
1075         SCPY(gsm_bs->interface_name, interface->name);
1076         gsm_bs->type = LCR_GSM_TYPE_NETWORK;
1077         gsm_bs->sun.sun_family = AF_UNIX;
1078         SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
1079
1080         memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
1081         add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
1082
1083         /* do the initial connect */
1084         mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);
1085
1086         generate_dtmf();
1087
1088         return 0;
1089 }