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