97be9425ac41210dad46d68c444b91c33824d2c0
[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         struct interface *interface;
811
812         interface = getinterfacebyname(p_interface_name);
813         if (!interface) {
814                 PERROR("Cannot find interface %s.\n", p_interface_name);
815                 return;
816         }
817
818         /* copy setup infos to port */
819         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
820         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
821         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
822         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
823
824         /* no GSM MNCC connection */
825         if (p_g_lcr_gsm->mncc_lfd.fd < 0) {
826                 gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
827                 add_trace("failure", NULL, "No MNCC connection.");
828                 end_trace();
829                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
830                 message->param.disconnectinfo.cause = 41; // temp. failure.
831                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
832                 message_put(message);
833                 new_state(PORT_STATE_RELEASE);
834                 trigger_work(&p_g_delete);
835                 return;
836         }
837
838         /* no number */
839         if (!p_dialinginfo.id[0]) {
840                 gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
841                 add_trace("failure", NULL, "No dialed subscriber given.");
842                 end_trace();
843                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
844                 message->param.disconnectinfo.cause = 28;
845                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
846                 message_put(message);
847                 new_state(PORT_STATE_RELEASE);
848                 trigger_work(&p_g_delete);
849                 return;
850         }
851
852         /* unsupported codec for RTP bridge */
853         if (param->setup.rtpinfo.port) {
854                 int i;
855
856                 p_g_rtp_payloads = 0;
857                 gsm_trace_header(p_interface_name, this, 1 /* codec negotioation */, DIRECTION_NONE);
858                 for (i = 0; i < param->setup.rtpinfo.payloads; i++) {
859                         switch (param->setup.rtpinfo.media_types[i]) {
860                         case MEDIA_TYPE_GSM:
861                         case MEDIA_TYPE_GSM_EFR:
862                         case MEDIA_TYPE_AMR:
863                         case MEDIA_TYPE_GSM_HR:
864                                 add_trace("rtp", "payload", "%s:%d supported", media_type2name(param->setup.rtpinfo.media_types[i]), param->setup.rtpinfo.payload_types[i]);
865                                 if (p_g_rtp_payloads < (int)sizeof(p_g_rtp_payload_types)) {
866                                         p_g_rtp_media_types[p_g_rtp_payloads] = param->setup.rtpinfo.media_types[i];
867                                         p_g_rtp_payload_types[p_g_rtp_payloads] = param->setup.rtpinfo.payload_types[i];
868                                         p_g_rtp_payloads++;
869                                 }
870                                 break;
871                         default:
872                                 add_trace("rtp", "payload", "%s:%d unsupported", media_type2name(param->setup.rtpinfo.media_types[i]), param->setup.rtpinfo.payload_types[i]);
873                         }
874                 }
875                 end_trace();
876                 if (!p_g_rtp_payloads) {
877                         gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
878                         add_trace("failure", NULL, "No payload given that is supported by GSM");
879                         end_trace();
880                         message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
881                         message->param.disconnectinfo.cause = 65;
882                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
883                         message_put(message);
884                         new_state(PORT_STATE_RELEASE);
885                         trigger_work(&p_g_delete);
886                         return;
887                 }
888         }
889
890 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
891         /* screen outgoing caller id */
892         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_interface_name);
893
894         /* attach only if not already */
895         epointlist = p_epointlist;
896         while(epointlist) {
897                 if (epointlist->epoint_id == epoint_id)
898                         break;
899                 epointlist = epointlist->next;
900         }
901         if (!epointlist)
902                 epointlist_new(epoint_id);
903
904         /* creating l3id */
905         gsm_trace_header(p_interface_name, this, 0, DIRECTION_OUT);
906         p_g_callref = new_callref++;
907         add_trace("callref", "new", "0x%x", p_g_callref);
908         end_trace();
909
910         gsm_trace_header(p_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
911         mncc = create_mncc(MNCC_SETUP_REQ, p_g_callref);
912         /* caller information */
913         mncc->fields |= MNCC_F_CALLING;
914         mncc->calling.plan = 1;
915         switch (p_callerinfo.ntype) {
916                 case INFO_NTYPE_UNKNOWN:
917                 mncc->calling.type = 0x0;
918                 break;
919                 case INFO_NTYPE_INTERNATIONAL:
920                 mncc->calling.type = 0x1;
921                 break;
922                 case INFO_NTYPE_NATIONAL:
923                 mncc->calling.type = 0x2;
924                 break;
925                 case INFO_NTYPE_SUBSCRIBER:
926                 mncc->calling.type = 0x4;
927                 break;
928                 default: /* INFO_NTYPE_NOTPRESENT */
929                 mncc->fields &= ~MNCC_F_CALLING;
930                 break;
931         }
932         switch (p_callerinfo.screen) {
933                 case INFO_SCREEN_USER:
934                 mncc->calling.screen = 0;
935                 break;
936                 default: /* INFO_SCREEN_NETWORK */
937                 mncc->calling.screen = 3;
938                 break;
939         }
940         switch (p_callerinfo.present) {
941                 case INFO_PRESENT_ALLOWED:
942                 mncc->calling.present = 0;
943                 break;
944                 case INFO_PRESENT_RESTRICTED:
945                 mncc->calling.present = 1;
946                 break;
947                 default: /* INFO_PRESENT_NOTAVAIL */
948                 mncc->calling.present = 2;
949                 break;
950         }
951         if (mncc->fields & MNCC_F_CALLING) {
952                 SCPY(mncc->calling.number, p_callerinfo.id);
953                 add_trace("calling", "type", "%d", mncc->calling.type);
954                 add_trace("calling", "plan", "%d", mncc->calling.plan);
955                 add_trace("calling", "present", "%d", mncc->calling.present);
956                 add_trace("calling", "screen", "%d", mncc->calling.screen);
957                 add_trace("calling", "number", "%s", mncc->calling.number);
958         }
959         /* dialing information */
960         mncc->fields |= MNCC_F_CALLED;
961         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
962                 SCPY(mncc->imsi, p_dialinginfo.id+5);
963                 add_trace("dialing", "imsi", "%s", mncc->imsi);
964         } else {
965                 SCPY(mncc->called.number, p_dialinginfo.id);
966                 add_trace("dialing", "number", "%s", mncc->called.number);
967         }
968         
969         /* sending user-user */
970
971         /* redirecting number */
972         mncc->fields |= MNCC_F_REDIRECTING;
973         mncc->redirecting.plan = 1;
974         switch (p_redirinfo.ntype) {
975                 case INFO_NTYPE_UNKNOWN:
976                 mncc->redirecting.type = 0x0;
977                 break;
978                 case INFO_NTYPE_INTERNATIONAL:
979                 mncc->redirecting.type = 0x1;
980                 break;
981                 case INFO_NTYPE_NATIONAL:
982                 mncc->redirecting.type = 0x2;
983                 break;
984                 case INFO_NTYPE_SUBSCRIBER:
985                 mncc->redirecting.type = 0x4;
986                 break;
987                 default: /* INFO_NTYPE_NOTPRESENT */
988                 mncc->fields &= ~MNCC_F_REDIRECTING;
989                 break;
990         }
991         switch (p_redirinfo.screen) {
992                 case INFO_SCREEN_USER:
993                 mncc->redirecting.screen = 0;
994                 break;
995                 default: /* INFO_SCREE_NETWORK */
996                 mncc->redirecting.screen = 3;
997                 break;
998         }
999         switch (p_redirinfo.present) {
1000                 case INFO_PRESENT_ALLOWED:
1001                 mncc->redirecting.present = 0;
1002                 break;
1003                 case INFO_PRESENT_RESTRICTED:
1004                 mncc->redirecting.present = 1;
1005                 break;
1006                 default: /* INFO_PRESENT_NOTAVAIL */
1007                 mncc->redirecting.present = 2;
1008                 break;
1009         }
1010         /* sending redirecting number only in ntmode */
1011         if (mncc->fields & MNCC_F_REDIRECTING) {
1012                 SCPY(mncc->redirecting.number, p_redirinfo.id);
1013                 add_trace("redir", "type", "%d", mncc->redirecting.type);
1014                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
1015                 add_trace("redir", "present", "%d", mncc->redirecting.present);
1016                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
1017                 add_trace("redir", "number", "%s", mncc->redirecting.number);
1018         }
1019
1020         if (interface->gsm_bs_hr) {
1021                 add_trace("lchan", "type", "TCH/H or TCH/F");
1022                 mncc->lchan_type = GSM_LCHAN_TCH_H;
1023         } else {
1024                 add_trace("lchan", "type", "TCH/F");
1025                 mncc->lchan_type = GSM_LCHAN_TCH_F;
1026         }
1027
1028         end_trace();
1029         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
1030
1031         new_state(PORT_STATE_OUT_SETUP);
1032
1033         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1034         message_put(message);
1035
1036         /* RTP bridge */
1037         if (param->setup.rtpinfo.port) {
1038                 p_g_rtp_bridge = 1;
1039                 p_g_rtp_ip_remote = param->setup.rtpinfo.ip;
1040                 p_g_rtp_port_remote = param->setup.rtpinfo.port;
1041         } else
1042                 p_g_rtp_bridge = 0;
1043 }
1044
1045 /*
1046  * endpoint sends messages to the port
1047  */
1048 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1049 {
1050         if (Pgsm::message_epoint(epoint_id, message_id, param))
1051                 return(1);
1052
1053         switch(message_id) {
1054                 case MESSAGE_SETUP: /* dial-out command received from epoint */
1055                 if (p_state!=PORT_STATE_IDLE)
1056                         break;
1057                 message_setup(epoint_id, message_id, param);
1058                 break;
1059
1060                 default:
1061                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1062         }
1063
1064         return(1);
1065 }
1066
1067 int gsm_bs_exit(int rc)
1068 {
1069         /* free gsm instance */
1070         if (gsm_bs) {
1071                 if (gsm_bs->mncc_lfd.fd > -1) {
1072                         close(gsm_bs->mncc_lfd.fd);
1073                         unregister_fd(&gsm_bs->mncc_lfd);
1074                 }
1075
1076                 del_timer(&gsm_bs->socket_retry);
1077                 free(gsm_bs);
1078                 gsm_bs = NULL;
1079         }
1080
1081
1082         return(rc);
1083 }
1084
1085 int gsm_bs_init(struct interface *interface)
1086 {
1087         /* create gsm instance */
1088         gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1089
1090         SCPY(gsm_bs->interface_name, interface->name);
1091         gsm_bs->type = LCR_GSM_TYPE_NETWORK;
1092         gsm_bs->sun.sun_family = AF_UNIX;
1093         SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
1094
1095         memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
1096         add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
1097
1098         /* do the initial connect */
1099         mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);
1100
1101         generate_dtmf();
1102
1103         return 0;
1104 }