Make chan_lcr compile with latest Asterisk.
[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                 uint8_t dynamic_type = 96;
300
301                 add_trace("bearer", "capa", "given by MS");
302                 for (i = 0; mncc->bearer_cap.speech_ver[i] >= 0; i++) {
303                         /* select payload type we support */
304                         switch (mncc->bearer_cap.speech_ver[i]) {
305                         case 0:
306                                 add_trace("speech", "version", "Full Rate given");
307                                 media_type = MEDIA_TYPE_GSM;
308                                 payload_type = PAYLOAD_TYPE_GSM;
309                                 break;
310                         case 2:
311                                 add_trace("speech", "version", "EFR given");
312                                 media_type = MEDIA_TYPE_GSM_EFR;
313                                 payload_type = dynamic_type++;
314                                 break;
315                         case 4:
316                                 add_trace("speech", "version", "AMR given");
317                                 media_type = MEDIA_TYPE_AMR;
318                                 payload_type = dynamic_type++;
319                                 break;
320                         case 1:
321                                 add_trace("speech", "version", "Half Rate given");
322                                 media_type = MEDIA_TYPE_GSM_HR;
323                                 payload_type = dynamic_type++;
324                                 break;
325                         default:
326                                 add_trace("speech", "version", "%d given", mncc->bearer_cap.speech_ver[i]);
327                                 media_type = 0;
328                                 payload_type = 0;
329                         }
330                         /* wen don't support it, so we check the next */
331                         if (!media_type) {
332                                 add_trace("speech", "ignored", "Not supported by LCR");
333                                 continue;
334                         }
335                         if (!p_g_rtp_bridge) {
336                                 if (media_type != MEDIA_TYPE_GSM) {
337                                         add_trace("speech", "ignored", "Not suitable for LCR");
338                                         continue;
339                                 }
340                         }
341                         if (*payloads <= max_payloads) {
342                                 media_types[*payloads] = media_type;
343                                 payload_types[*payloads] = payload_type;
344                                 (*payloads)++;
345                         }
346                 }
347         } else {
348                 add_trace("bearer", "capa", "not given by MS");
349                 add_trace("speech", "version", "Full Rate given");
350                 media_types[0] = MEDIA_TYPE_GSM;
351                 payload_types[0] = PAYLOAD_TYPE_GSM;
352                 *payloads = 1;
353         }
354         if (!(*payloads))
355                 add_trace("error", "", "All given payload types unsupported");
356         end_trace();
357 }
358
359 /*
360  * handles all indications
361  */
362 /* SETUP INDICATION */
363 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
364 {
365         class Endpoint *epoint;
366         struct lcr_msg *message;
367         struct gsm_mncc *proceeding, *frame;
368         struct interface *interface;
369         int media_types[8];
370         unsigned char payload_types[8];
371         int payloads = 0;
372
373         interface = getinterfacebyname(p_g_interface_name);
374         if (!interface) {
375                 PERROR("Cannot find interface %s.\n", p_g_interface_name);
376                 return;
377         }
378
379         /* process given callref */
380         gsm_trace_header(p_g_interface_name, this, 0, DIRECTION_IN);
381         add_trace("callref", "new", "0x%x", callref);
382         if (p_g_callref) {
383                 /* release in case the ID is already in use */
384                 add_trace("error", NULL, "callref already in use");
385                 end_trace();
386                 mncc = create_mncc(MNCC_REJ_REQ, callref);
387                 gsm_trace_header(p_g_interface_name, this, MNCC_REJ_REQ, DIRECTION_OUT);
388                 mncc->fields |= MNCC_F_CAUSE;
389                 mncc->cause.coding = 3;
390                 mncc->cause.location = 1;
391                 mncc->cause.value = 47;
392                 add_trace("cause", "coding", "%d", mncc->cause.coding);
393                 add_trace("cause", "location", "%d", mncc->cause.location);
394                 add_trace("cause", "value", "%d", mncc->cause.value);
395                 add_trace("reason", NULL, "callref already in use");
396                 end_trace();
397                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
398                 new_state(PORT_STATE_RELEASE);
399                 trigger_work(&p_g_delete);
400                 return;
401         }
402         p_g_callref = callref;
403         end_trace();
404
405         /* caller info */
406         if (mncc->clir.inv)
407                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
408         else
409                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
410         if (mncc->calling.number[0])
411                 SCPY(p_callerinfo.id, mncc->calling.number);
412         else
413                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
414         SCPY(p_callerinfo.imsi, mncc->imsi);
415         p_callerinfo.screen = INFO_SCREEN_NETWORK;
416         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
417         SCPY(p_callerinfo.interface, p_g_interface_name);
418
419         /* dialing information */
420         SCAT(p_dialinginfo.id, mncc->called.number);
421         switch (mncc->called.type) {
422                 case 0x1:
423                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
424                 break;
425                 case 0x2:
426                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
427                 break;
428                 case 0x4:
429                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
430                 break;
431                 default:
432                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
433                 break;
434         }
435         if (mncc->emergency) {
436                 SCPY(p_dialinginfo.id, "emergency");
437         }
438         p_dialinginfo.sending_complete = 1;
439
440         /* bearer capability */
441         p_capainfo.bearer_capa = INFO_BC_SPEECH;
442         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
443         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
444         p_capainfo.source_mode = B_MODE_TRANSPARENT;
445         p_g_mode = p_capainfo.source_mode;
446
447         /* get list of offered payload types
448          * if list ist empty, the FR V1 is selected */
449         select_payload_type(mncc, payload_types, media_types, &payloads, sizeof(payload_types));
450         /* if no given payload type is supported, we assume  */
451         if (!payloads) {
452                 media_types[0] = MEDIA_TYPE_GSM;
453                 payload_types[0] = PAYLOAD_TYPE_GSM;
454                 payloads = 1;
455         }
456 #if 0
457         /* if no given payload type is supported, we reject the call */
458         if (!payloads) {
459                 mncc = create_mncc(MNCC_REJ_REQ, callref);
460                 gsm_trace_header(p_g_interface_name, this, MNCC_REJ_REQ, DIRECTION_OUT);
461                 mncc->fields |= MNCC_F_CAUSE;
462                 mncc->cause.coding = 3;
463                 mncc->cause.location = 1;
464                 mncc->cause.value = 65;
465                 add_trace("cause", "coding", "%d", mncc->cause.coding);
466                 add_trace("cause", "location", "%d", mncc->cause.location);
467                 add_trace("cause", "value", "%d", mncc->cause.value);
468                 add_trace("reason", NULL, "Given speech codec(s) not supported");
469                 end_trace();
470                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
471                 new_state(PORT_STATE_RELEASE);
472                 trigger_work(&p_g_delete);
473                 return;
474         }
475 #endif
476
477         /* useruser */
478
479         /* what infos did we got ... */
480         gsm_trace_header(p_g_interface_name, this, msg_type, DIRECTION_IN);
481         if (p_callerinfo.id[0])
482                 add_trace("calling", "number", "%s", p_callerinfo.id);
483         else
484                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
485         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
486         add_trace("dialing", "number", "%s", p_dialinginfo.id);
487         end_trace();
488
489         /* create endpoint */
490         if (p_epointlist)
491                 FATAL("Incoming call but already got an endpoint.\n");
492         if (!(epoint = new Endpoint(p_serial, 0)))
493                 FATAL("No memory for Endpoint instance\n");
494         epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
495         epointlist_new(epoint->ep_serial);
496
497         /* modify lchan in case of no rtp bridge */
498         if (!p_g_rtp_bridge)
499                 modify_lchan(media_types[0]);
500
501         /* send call proceeding */
502         gsm_trace_header(p_g_interface_name, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
503         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_g_callref);
504         if (p_g_tones) {
505                 proceeding->fields |= MNCC_F_PROGRESS;
506                 proceeding->progress.coding = 3; /* GSM */
507                 proceeding->progress.location = 1;
508                 proceeding->progress.descr = 8;
509                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
510                 add_trace("progress", "location", "%d", proceeding->progress.location);
511                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
512         }
513         end_trace();
514         send_and_free_mncc(p_g_lcr_gsm, proceeding->msg_type, proceeding);
515
516         new_state(PORT_STATE_IN_PROCEEDING);
517
518         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
519                 gsm_trace_header(p_g_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
520                 end_trace();
521                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
522                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
523                 p_g_tch_connected = 1;
524         }
525
526         /* send setup message to endpoit */
527         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
528         message->param.setup.port_type = p_type;
529 //      message->param.setup.dtmf = 0;
530         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
531         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
532         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
533         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
534         message->param.setup.useruser.len = strlen(mncc->useruser.info);
535         message->param.setup.useruser.protocol = mncc->useruser.proto;
536         if (p_g_rtp_bridge) {
537                 struct gsm_mncc_rtp *rtp;
538                 int i;
539
540                 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding setup\n");
541                 p_g_setup_pending = message;
542                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
543                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
544
545                 for (i = 0; i < (int)sizeof(message->param.setup.rtpinfo.payload_types) && i < payloads; i++) {
546                         message->param.setup.rtpinfo.media_types[i] = media_types[i];
547                         message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
548                         message->param.setup.rtpinfo.payloads++;
549                 }
550
551         } else
552                 message_put(message);
553
554 }
555
556 /*
557  * BSC sends message to port
558  */
559 int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
560 {
561         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
562         unsigned int callref = mncc->callref;
563         class Port *port;
564         class Pgsm_bs *pgsm_bs = NULL;
565         char name[64];
566 //      struct mISDNport *mISDNport;
567
568         /* Special messages */
569         switch(msg_type) {
570         }
571
572         /* find callref */
573         callref = mncc->callref;
574         port = port_first;
575         while(port) {
576                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
577                         pgsm_bs = (class Pgsm_bs *)port;
578                         if (pgsm_bs->p_g_callref == callref) {
579                                 break;
580                         }
581                 }
582                 port = port->next;
583         }
584
585         if (msg_type == GSM_TCHF_FRAME
586          || msg_type == GSM_TCHF_BAD_FRAME) {
587                 if (port) {
588                         /* inject DTMF, if enabled */
589                         if (pgsm_bs->p_g_dtmf) {
590                                 unsigned char data[160];
591                                 int i;
592
593                                 for (i = 0; i < 160; i++) {
594                                         data[i] = pgsm_bs->p_g_dtmf[pgsm_bs->p_g_dtmf_index++];
595                                         if (pgsm_bs->p_g_dtmf_index == 8000)
596                                                 pgsm_bs->p_g_dtmf_index = 0;
597                                 }
598                                 /* send */
599                                 pgsm_bs->bridge_tx(data, 160);
600                         } else
601                                 pgsm_bs->frame_receive(arg);
602                         /* if we do not bridge we need to inject audio, if available */
603                         if (!pgsm_bs->p_bridge || pgsm_bs->p_tone_name[0]) {
604                                 unsigned char data[160];
605                                 int i;
606
607                                 i = pgsm_bs->read_audio(data, 160);
608                                 if (i)
609                                         pgsm_bs->audio_send(data, i);
610                         }
611                 }
612                 return 0;
613         }
614
615         if (!port) {
616                 struct interface *interface;
617
618                 if (msg_type != MNCC_SETUP_IND)
619                         return(0);
620
621                 interface = getinterfacebyname(lcr_gsm->interface_name);
622                 if (!interface) {
623                         struct gsm_mncc *rej;
624
625                         rej = create_mncc(MNCC_REJ_REQ, callref);
626                         rej->fields |= MNCC_F_CAUSE;
627                         rej->cause.coding = 3;
628                         rej->cause.location = 1;
629                         rej->cause.value = 27;
630                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
631                         add_trace("cause", "coding", "%d", rej->cause.coding);
632                         add_trace("cause", "location", "%d", rej->cause.location);
633                         add_trace("cause", "value", "%d", rej->cause.value);
634                         add_trace("reason", NULL, "interface %s not found", lcr_gsm->interface_name);
635                         end_trace();
636                         send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
637                         return 0;
638                 }
639                 /* creating port object, transparent until setup with hdlc */
640                 SPRINT(name, "%s-%d-in", interface->name, 0);
641                 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, name, NULL, interface)))
642                         FATAL("Cannot create Port instance.\n");
643         }
644
645         switch(msg_type) {
646                 case MNCC_SETUP_IND:
647                 pgsm_bs->setup_ind(msg_type, callref, mncc);
648                 break;
649
650                 case MNCC_RTP_CREATE:
651                 pgsm_bs->rtp_create_ind(msg_type, callref, mncc);
652                 break;
653
654                 case MNCC_RTP_CONNECT:
655                 pgsm_bs->rtp_connect_ind(msg_type, callref, mncc);
656                 break;
657
658                 case MNCC_START_DTMF_IND:
659                 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
660                 break;
661
662                 case MNCC_STOP_DTMF_IND:
663                 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
664                 break;
665
666                 case MNCC_CALL_CONF_IND:
667                 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
668                 break;
669
670                 case MNCC_ALERT_IND:
671                 pgsm_bs->alert_ind(msg_type, callref, mncc);
672                 break;
673
674                 case MNCC_SETUP_CNF:
675                 pgsm_bs->setup_cnf(msg_type, callref, mncc);
676                 break;
677
678                 case MNCC_SETUP_COMPL_IND:
679                 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
680                 break;
681
682                 case MNCC_DISC_IND:
683                 pgsm_bs->disc_ind(msg_type, callref, mncc);
684                 break;
685
686                 case MNCC_REL_IND:
687                 case MNCC_REL_CNF:
688                 case MNCC_REJ_IND:
689                 pgsm_bs->rel_ind(msg_type, callref, mncc);
690                 break;
691
692                 case MNCC_NOTIFY_IND:
693                 pgsm_bs->notify_ind(msg_type, callref, mncc);
694                 break;
695
696                 case MNCC_HOLD_IND:
697                 pgsm_bs->hold_ind(msg_type, callref, mncc);
698                 break;
699
700                 case MNCC_RETRIEVE_IND:
701                 pgsm_bs->retr_ind(msg_type, callref, mncc);
702                 break;
703
704                 default:
705                 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);
706         }
707         return(0);
708 }
709
710 /* MESSAGE_SETUP */
711 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
712 {
713         struct lcr_msg *message;
714         struct epoint_list *epointlist;
715         struct gsm_mncc *mncc;
716
717         /* copy setup infos to port */
718         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
719         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
720         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
721         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
722
723         /* no GSM MNCC connection */
724         if (p_g_lcr_gsm->mncc_lfd.fd < 0) {
725                 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
726                 add_trace("failure", NULL, "No MNCC connection.");
727                 end_trace();
728                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
729                 message->param.disconnectinfo.cause = 41; // temp. failure.
730                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
731                 message_put(message);
732                 new_state(PORT_STATE_RELEASE);
733                 trigger_work(&p_g_delete);
734                 return;
735         }
736
737         /* no number */
738         if (!p_dialinginfo.id[0]) {
739                 gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
740                 add_trace("failure", NULL, "No dialed subscriber given.");
741                 end_trace();
742                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
743                 message->param.disconnectinfo.cause = 28;
744                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
745                 message_put(message);
746                 new_state(PORT_STATE_RELEASE);
747                 trigger_work(&p_g_delete);
748                 return;
749         }
750
751         /* unsupported codec for RTP bridge */
752         if (param->setup.rtpinfo.port) {
753                 int i;
754
755                 p_g_rtp_payloads = 0;
756                 gsm_trace_header(p_g_interface_name, this, 1 /* codec negotioation */, DIRECTION_NONE);
757                 for (i = 0; i < param->setup.rtpinfo.payloads; i++) {
758                         switch (param->setup.rtpinfo.media_types[i]) {
759                         case MEDIA_TYPE_GSM:
760                         case MEDIA_TYPE_GSM_EFR:
761                         case MEDIA_TYPE_AMR:
762                         case MEDIA_TYPE_GSM_HR:
763                                 add_trace("rtp", "payload", "%s:%d supported", media_type2name(param->setup.rtpinfo.media_types[i]), param->setup.rtpinfo.payload_types[i]);
764                                 if (p_g_rtp_payloads < (int)sizeof(p_g_rtp_payload_types)) {
765                                         p_g_rtp_media_types[p_g_rtp_payloads] = param->setup.rtpinfo.media_types[i];
766                                         p_g_rtp_payload_types[p_g_rtp_payloads] = param->setup.rtpinfo.payload_types[i];
767                                         p_g_rtp_payloads++;
768                                 }
769                                 break;
770                         default:
771                                 add_trace("rtp", "payload", "%s:%d unsupported", media_type2name(param->setup.rtpinfo.media_types[i]), param->setup.rtpinfo.payload_types[i]);
772                         }
773                 }
774                 end_trace();
775                 if (!p_g_rtp_payloads) {
776                         gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
777                         add_trace("failure", NULL, "No payload given that is supported by GSM");
778                         end_trace();
779                         message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
780                         message->param.disconnectinfo.cause = 65;
781                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
782                         message_put(message);
783                         new_state(PORT_STATE_RELEASE);
784                         trigger_work(&p_g_delete);
785                         return;
786                 }
787         }
788
789 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
790         /* screen outgoing caller id */
791         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_g_interface_name);
792
793         /* attach only if not already */
794         epointlist = p_epointlist;
795         while(epointlist) {
796                 if (epointlist->epoint_id == epoint_id)
797                         break;
798                 epointlist = epointlist->next;
799         }
800         if (!epointlist)
801                 epointlist_new(epoint_id);
802
803         /* creating l3id */
804         gsm_trace_header(p_g_interface_name, this, 0, DIRECTION_OUT);
805         p_g_callref = new_callref++;
806         add_trace("callref", "new", "0x%x", p_g_callref);
807         end_trace();
808
809         gsm_trace_header(p_g_interface_name, this, MNCC_SETUP_REQ, DIRECTION_OUT);
810         mncc = create_mncc(MNCC_SETUP_REQ, p_g_callref);
811         /* caller information */
812         mncc->fields |= MNCC_F_CALLING;
813         mncc->calling.plan = 1;
814         switch (p_callerinfo.ntype) {
815                 case INFO_NTYPE_UNKNOWN:
816                 mncc->calling.type = 0x0;
817                 break;
818                 case INFO_NTYPE_INTERNATIONAL:
819                 mncc->calling.type = 0x1;
820                 break;
821                 case INFO_NTYPE_NATIONAL:
822                 mncc->calling.type = 0x2;
823                 break;
824                 case INFO_NTYPE_SUBSCRIBER:
825                 mncc->calling.type = 0x4;
826                 break;
827                 default: /* INFO_NTYPE_NOTPRESENT */
828                 mncc->fields &= ~MNCC_F_CALLING;
829                 break;
830         }
831         switch (p_callerinfo.screen) {
832                 case INFO_SCREEN_USER:
833                 mncc->calling.screen = 0;
834                 break;
835                 default: /* INFO_SCREEN_NETWORK */
836                 mncc->calling.screen = 3;
837                 break;
838         }
839         switch (p_callerinfo.present) {
840                 case INFO_PRESENT_ALLOWED:
841                 mncc->calling.present = 0;
842                 break;
843                 case INFO_PRESENT_RESTRICTED:
844                 mncc->calling.present = 1;
845                 break;
846                 default: /* INFO_PRESENT_NOTAVAIL */
847                 mncc->calling.present = 2;
848                 break;
849         }
850         if (mncc->fields & MNCC_F_CALLING) {
851                 SCPY(mncc->calling.number, p_callerinfo.id);
852                 add_trace("calling", "type", "%d", mncc->calling.type);
853                 add_trace("calling", "plan", "%d", mncc->calling.plan);
854                 add_trace("calling", "present", "%d", mncc->calling.present);
855                 add_trace("calling", "screen", "%d", mncc->calling.screen);
856                 add_trace("calling", "number", "%s", mncc->calling.number);
857         }
858         /* dialing information */
859         mncc->fields |= MNCC_F_CALLED;
860         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
861                 SCPY(mncc->imsi, p_dialinginfo.id+5);
862                 add_trace("dialing", "imsi", "%s", mncc->imsi);
863         } else {
864                 SCPY(mncc->called.number, p_dialinginfo.id);
865                 add_trace("dialing", "number", "%s", mncc->called.number);
866         }
867         
868         /* sending user-user */
869
870         /* redirecting number */
871         mncc->fields |= MNCC_F_REDIRECTING;
872         mncc->redirecting.plan = 1;
873         switch (p_redirinfo.ntype) {
874                 case INFO_NTYPE_UNKNOWN:
875                 mncc->redirecting.type = 0x0;
876                 break;
877                 case INFO_NTYPE_INTERNATIONAL:
878                 mncc->redirecting.type = 0x1;
879                 break;
880                 case INFO_NTYPE_NATIONAL:
881                 mncc->redirecting.type = 0x2;
882                 break;
883                 case INFO_NTYPE_SUBSCRIBER:
884                 mncc->redirecting.type = 0x4;
885                 break;
886                 default: /* INFO_NTYPE_NOTPRESENT */
887                 mncc->fields &= ~MNCC_F_REDIRECTING;
888                 break;
889         }
890         switch (p_redirinfo.screen) {
891                 case INFO_SCREEN_USER:
892                 mncc->redirecting.screen = 0;
893                 break;
894                 default: /* INFO_SCREE_NETWORK */
895                 mncc->redirecting.screen = 3;
896                 break;
897         }
898         switch (p_redirinfo.present) {
899                 case INFO_PRESENT_ALLOWED:
900                 mncc->redirecting.present = 0;
901                 break;
902                 case INFO_PRESENT_RESTRICTED:
903                 mncc->redirecting.present = 1;
904                 break;
905                 default: /* INFO_PRESENT_NOTAVAIL */
906                 mncc->redirecting.present = 2;
907                 break;
908         }
909         /* sending redirecting number only in ntmode */
910         if (mncc->fields & MNCC_F_REDIRECTING) {
911                 SCPY(mncc->redirecting.number, p_redirinfo.id);
912                 add_trace("redir", "type", "%d", mncc->redirecting.type);
913                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
914                 add_trace("redir", "present", "%d", mncc->redirecting.present);
915                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
916                 add_trace("redir", "number", "%s", mncc->redirecting.number);
917         }
918
919         end_trace();
920         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
921
922         new_state(PORT_STATE_OUT_SETUP);
923
924         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
925         message_put(message);
926
927         /* RTP bridge */
928         if (param->setup.rtpinfo.port) {
929                 p_g_rtp_bridge = 1;
930                 p_g_rtp_ip_remote = param->setup.rtpinfo.ip;
931                 p_g_rtp_port_remote = param->setup.rtpinfo.port;
932         } else
933                 p_g_rtp_bridge = 0;
934 }
935
936 /*
937  * endpoint sends messages to the port
938  */
939 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
940 {
941         if (Pgsm::message_epoint(epoint_id, message_id, param))
942                 return(1);
943
944         switch(message_id) {
945                 case MESSAGE_SETUP: /* dial-out command received from epoint */
946                 if (p_state!=PORT_STATE_IDLE)
947                         break;
948                 message_setup(epoint_id, message_id, param);
949                 break;
950
951                 default:
952                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
953         }
954
955         return(1);
956 }
957
958 int gsm_bs_exit(int rc)
959 {
960         /* free gsm instance */
961         if (gsm_bs) {
962                 if (gsm_bs->mncc_lfd.fd > -1) {
963                         close(gsm_bs->mncc_lfd.fd);
964                         unregister_fd(&gsm_bs->mncc_lfd);
965                 }
966
967                 del_timer(&gsm_bs->socket_retry);
968                 free(gsm_bs);
969                 gsm_bs = NULL;
970         }
971
972
973         return(rc);
974 }
975
976 int gsm_bs_init(struct interface *interface)
977 {
978         /* create gsm instance */
979         gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
980
981         SCPY(gsm_bs->interface_name, interface->name);
982         gsm_bs->type = LCR_GSM_TYPE_NETWORK;
983         gsm_bs->sun.sun_family = AF_UNIX;
984         SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
985
986         memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
987         add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
988
989         /* do the initial connect */
990         mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);
991
992         generate_dtmf();
993
994         return 0;
995 }