Remove unused variable ret
[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 /*
18  * DTMF stuff
19  */
20 unsigned char dtmf_samples[16][8000];
21 static int dtmf_x[4] = { 1209, 1336, 1477, 1633 };
22 static int dtmf_y[4] = { 697, 770, 852, 941 };
23
24 void generate_dtmf(void)
25 {
26         double fx, fy, sample;
27         int i, x, y;
28         unsigned char *law;
29
30         for (y = 0; y < 4; y++) {
31                 fy = 2 * 3.1415927 * ((double)dtmf_y[y]) / 8000.0;
32                 for (x = 0; x < 4; x++) {
33                         fx = 2 * 3.1415927 * ((double)dtmf_x[x]) / 8000.0;
34                         law = dtmf_samples[y << 2 | x];
35                         for (i = 0; i < 8000; i++) {
36                                 sample = sin(fy * ((double)i)) * 0.251 * 32767.0; /* -6 dB */
37                                 sample += sin(fx * ((double)i)) * 0.158 * 32767.0; /* -8 dB */
38                                 *law++ = audio_s16_to_law[(int)sample & 0xffff];
39                         }
40                 }
41         }
42 }
43
44
45 /*
46  * constructor
47  */
48 Pgsm_bs::Pgsm_bs(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : Pgsm(type, mISDNport, portname, settings, channel, exclusive, mode)
49 {
50         p_m_g_lcr_gsm = gsm_bs;
51         p_m_g_dtmf = NULL;
52         p_m_g_dtmf_index = 0;
53
54         PDEBUG(DEBUG_GSM, "Created new GSMBSPort(%s).\n", portname);
55 }
56
57 /*
58  * destructor
59  */
60 Pgsm_bs::~Pgsm_bs()
61 {
62         PDEBUG(DEBUG_GSM, "Destroyed GSM BS process(%s).\n", p_name);
63 }
64
65 /* DTMF INDICATION */
66 void Pgsm_bs::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
67 {
68 //      struct lcr_msg *message;
69         struct gsm_mncc *resp;
70
71         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
72         add_trace("keypad", NULL, "%c", mncc->keypad);
73         end_trace();
74         SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
75         p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
76
77         /* send resp */
78         gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
79         add_trace("keypad", NULL, "%c", mncc->keypad);
80         end_trace();
81         resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
82         resp->fields |= MNCC_F_KEYPAD;
83         resp->keypad = mncc->keypad;
84         send_and_free_mncc(p_m_g_lcr_gsm, resp->msg_type, resp);
85
86 #if 0
87         /* send dialing information */
88         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
89         memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
90         message_put(message);
91 #endif
92
93         /* generate DTMF tones */
94         switch (mncc->keypad) {
95                 case '1': p_m_g_dtmf = dtmf_samples[0]; break;
96                 case '2': p_m_g_dtmf = dtmf_samples[1]; break;
97                 case '3': p_m_g_dtmf = dtmf_samples[2]; break;
98                 case 'a':
99                 case 'A': p_m_g_dtmf = dtmf_samples[3]; break;
100                 case '4': p_m_g_dtmf = dtmf_samples[4]; break;
101                 case '5': p_m_g_dtmf = dtmf_samples[5]; break;
102                 case '6': p_m_g_dtmf = dtmf_samples[6]; break;
103                 case 'b':
104                 case 'B': p_m_g_dtmf = dtmf_samples[7]; break;
105                 case '7': p_m_g_dtmf = dtmf_samples[8]; break;
106                 case '8': p_m_g_dtmf = dtmf_samples[9]; break;
107                 case '9': p_m_g_dtmf = dtmf_samples[10]; break;
108                 case 'c':
109                 case 'C': p_m_g_dtmf = dtmf_samples[11]; break;
110                 case '*': p_m_g_dtmf = dtmf_samples[12]; break;
111                 case '0': p_m_g_dtmf = dtmf_samples[13]; break;
112                 case '#': p_m_g_dtmf = dtmf_samples[14]; break;
113                 case 'd':
114                 case 'D': p_m_g_dtmf = dtmf_samples[15]; break;
115         }
116         p_m_g_dtmf_index = 0;
117 }
118 void Pgsm_bs::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
119 {
120         struct gsm_mncc *resp;
121
122         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
123         add_trace("keypad", NULL, "%c", mncc->keypad);
124         end_trace();
125
126         /* send resp */
127         gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
128         add_trace("keypad", NULL, "%c", mncc->keypad);
129         end_trace();
130         resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
131         resp->keypad = mncc->keypad;
132         send_and_free_mncc(p_m_g_lcr_gsm, resp->msg_type, resp);
133         
134         /* stop DTMF */
135         p_m_g_dtmf = NULL;
136 }
137
138 /* HOLD INDICATION */
139 void Pgsm_bs::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
140 {
141         struct lcr_msg *message;
142         struct gsm_mncc *resp, *frame;
143
144         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
145         end_trace();
146
147         /* notify the hold of call */
148         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
149         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
150         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
151         message_put(message);
152
153         /* acknowledge hold */
154         gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
155         end_trace();
156         resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
157         send_and_free_mncc(p_m_g_lcr_gsm, resp->msg_type, resp);
158
159         /* disable audio */
160         if (p_m_g_tch_connected) { /* it should be true */
161                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
162                 end_trace();
163                 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
164                 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
165                 p_m_g_tch_connected = 0;
166         }
167 }
168
169
170 /* RETRIEVE INDICATION */
171 void Pgsm_bs::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
172 {
173         struct lcr_msg *message;
174         struct gsm_mncc *resp, *frame;
175
176         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
177         end_trace();
178
179         /* notify the retrieve of call */
180         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
181         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
182         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
183         message_put(message);
184
185         /* acknowledge retr */
186         gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
187         end_trace();
188         resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
189         send_and_free_mncc(p_m_g_lcr_gsm, resp->msg_type, resp);
190
191         /* enable audio */
192         if (!p_m_g_tch_connected) { /* it should be true */
193                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
194                 end_trace();
195                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
196                 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
197                 p_m_g_tch_connected = 1;
198         }
199 }
200
201 /*
202  * handles all indications
203  */
204 /* SETUP INDICATION */
205 void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
206 {
207         int ret;
208         class Endpoint *epoint;
209         struct lcr_msg *message;
210         int channel;
211         struct gsm_mncc *mode, *proceeding, *frame;
212
213         /* process given callref */
214         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
215         add_trace("callref", "new", "0x%x", callref);
216         if (p_m_g_callref) {
217                 /* release in case the ID is already in use */
218                 add_trace("error", NULL, "callref already in use");
219                 end_trace();
220                 mncc = create_mncc(MNCC_REJ_REQ, callref);
221                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
222                 mncc->fields |= MNCC_F_CAUSE;
223                 mncc->cause.coding = 3;
224                 mncc->cause.location = 1;
225                 mncc->cause.value = 47;
226                 add_trace("cause", "coding", "%d", mncc->cause.coding);
227                 add_trace("cause", "location", "%d", mncc->cause.location);
228                 add_trace("cause", "value", "%d", mncc->cause.value);
229                 add_trace("reason", NULL, "callref already in use");
230                 end_trace();
231                 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
232                 new_state(PORT_STATE_RELEASE);
233                 trigger_work(&p_m_g_delete);
234                 return;
235         }
236         p_m_g_callref = callref;
237         end_trace();
238
239         /* if blocked, release call with MT_RELEASE_COMPLETE */
240         if (p_m_mISDNport->ifport->block) {
241                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
242                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
243                 mncc->fields |= MNCC_F_CAUSE;
244                 mncc->cause.coding = 3;
245                 mncc->cause.location = 1;
246                 mncc->cause.value = 27;
247                 add_trace("cause", "coding", "%d", mncc->cause.coding);
248                 add_trace("cause", "location", "%d", mncc->cause.location);
249                 add_trace("cause", "value", "%d", mncc->cause.value);
250                 add_trace("reason", NULL, "port is blocked");
251                 end_trace();
252                 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
253                 new_state(PORT_STATE_RELEASE);
254                 trigger_work(&p_m_g_delete);
255                 return;
256         }
257
258         /* caller info */
259         if (mncc->clir.inv)
260                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
261         else
262                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
263         if (mncc->calling.number[0])
264                 SCPY(p_callerinfo.id, mncc->calling.number);
265         else
266                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
267         SCPY(p_callerinfo.imsi, mncc->imsi);
268         p_callerinfo.screen = INFO_SCREEN_NETWORK;
269         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
270         p_callerinfo.isdn_port = p_m_portnum;
271         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
272
273         /* dialing information */
274         SCAT(p_dialinginfo.id, mncc->called.number);
275         switch (mncc->called.type) {
276                 case 0x1:
277                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
278                 break;
279                 case 0x2:
280                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
281                 break;
282                 case 0x4:
283                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
284                 break;
285                 default:
286                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
287                 break;
288         }
289         if (mncc->emergency) {
290                 SCPY(p_dialinginfo.id, "emergency");
291         }
292         p_dialinginfo.sending_complete = 1;
293
294         /* bearer capability */
295         // todo
296         p_capainfo.bearer_capa = INFO_BC_SPEECH;
297         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
298         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
299         p_capainfo.source_mode = B_MODE_TRANSPARENT;
300         p_m_g_mode = p_capainfo.source_mode;
301
302         /* useruser */
303
304         /* hunt channel */
305         ret = channel = hunt_bchannel();
306         if (ret < 0)
307                 goto no_channel;
308
309         /* open channel */
310         ret = seize_bchannel(channel, 1);
311         if (ret < 0) {
312                 no_channel:
313                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
314                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
315                 mncc->fields |= MNCC_F_CAUSE;
316                 mncc->cause.coding = 3;
317                 mncc->cause.location = 1;
318                 mncc->cause.value = 34;
319                 add_trace("cause", "coding", "%d", mncc->cause.coding);
320                 add_trace("cause", "location", "%d", mncc->cause.location);
321                 add_trace("cause", "value", "%d", mncc->cause.value);
322                 add_trace("reason", NULL, "no channel");
323                 end_trace();
324                 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
325                 new_state(PORT_STATE_RELEASE);
326                 trigger_work(&p_m_g_delete);
327                 return;
328         }
329         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
330         if (bchannel_open(p_m_b_index))
331                 goto no_channel;
332
333         /* what infos did we got ... */
334         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
335         if (p_callerinfo.id[0])
336                 add_trace("calling", "number", "%s", p_callerinfo.id);
337         else
338                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
339         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
340         add_trace("dialing", "number", "%s", p_dialinginfo.id);
341         end_trace();
342
343         /* create endpoint */
344         if (p_epointlist)
345                 FATAL("Incoming call but already got an endpoint.\n");
346         if (!(epoint = new Endpoint(p_serial, 0)))
347                 FATAL("No memory for Endpoint instance\n");
348         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
349                 FATAL("No memory for Endpoint Application instance\n");
350         epointlist_new(epoint->ep_serial);
351
352         /* modify lchan to GSM codec V1 */
353         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
354         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
355         mode->lchan_mode = 0x01; /* GSM V1 */
356         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
357         end_trace();
358         send_and_free_mncc(p_m_g_lcr_gsm, mode->msg_type, mode);
359
360         /* send call proceeding */
361         gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
362         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
363         if (p_m_mISDNport->tones) {
364                 proceeding->fields |= MNCC_F_PROGRESS;
365                 proceeding->progress.coding = 3; /* GSM */
366                 proceeding->progress.location = 1;
367                 proceeding->progress.descr = 8;
368                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
369                 add_trace("progress", "location", "%d", proceeding->progress.location);
370                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
371         }
372         end_trace();
373         send_and_free_mncc(p_m_g_lcr_gsm, proceeding->msg_type, proceeding);
374
375         new_state(PORT_STATE_IN_PROCEEDING);
376
377         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
378                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
379                 end_trace();
380                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
381                 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
382                 p_m_g_tch_connected = 1;
383         }
384
385         /* send setup message to endpoit */
386         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
387         message->param.setup.isdn_port = p_m_portnum;
388         message->param.setup.port_type = p_type;
389 //      message->param.setup.dtmf = 0;
390         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
391         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
392         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
393         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
394         message->param.setup.useruser.len = strlen(mncc->useruser.info);
395         message->param.setup.useruser.protocol = mncc->useruser.proto;
396         message_put(message);
397 }
398
399 /*
400  * BSC sends message to port
401  */
402 int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
403 {
404         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
405         unsigned int callref = mncc->callref;
406         class Port *port;
407         class Pgsm_bs *pgsm_bs = NULL;
408         char name[64];
409         struct mISDNport *mISDNport;
410
411         /* Special messages */
412         switch(msg_type) {
413         }
414
415         /* find callref */
416         callref = mncc->callref;
417         port = port_first;
418         while(port) {
419                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
420                         pgsm_bs = (class Pgsm_bs *)port;
421                         if (pgsm_bs->p_m_g_callref == callref) {
422                                 break;
423                         }
424                 }
425                 port = port->next;
426         }
427
428         if (msg_type == GSM_TCHF_FRAME) {
429                 if (port) {
430                         /* inject DTMF, if enabled */
431                         if (pgsm_bs->p_m_g_dtmf) {
432                                 unsigned char data[160];
433                                 int i;
434
435                                 for (i = 0; i < 160; i++) {
436                                         data[i] = pgsm_bs->p_m_g_dtmf[pgsm_bs->p_m_g_dtmf_index++];
437                                         if (pgsm_bs->p_m_g_dtmf_index == 8000)
438                                                 pgsm_bs->p_m_g_dtmf_index = 0;
439                                 }
440                                 /* send */
441                                 pgsm_bs->bchannel_send(PH_DATA_REQ, 0, data, 160);
442                         } else
443                                 pgsm_bs->frame_receive(arg);
444                 }
445                 return 0;
446         }
447
448         if (!port) {
449                 if (msg_type != MNCC_SETUP_IND)
450                         return(0);
451                 /* find gsm port */
452                 mISDNport = mISDNport_first;
453                 while(mISDNport) {
454                         if (mISDNport->gsm_bs)
455                                 break;
456                         mISDNport = mISDNport->next;
457                 }
458                 if (!mISDNport) {
459                         struct gsm_mncc *rej;
460
461                         rej = create_mncc(MNCC_REJ_REQ, callref);
462                         rej->fields |= MNCC_F_CAUSE;
463                         rej->cause.coding = 3;
464                         rej->cause.location = 1;
465                         rej->cause.value = 27;
466                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
467                         add_trace("cause", "coding", "%d", rej->cause.coding);
468                         add_trace("cause", "location", "%d", rej->cause.location);
469                         add_trace("cause", "value", "%d", rej->cause.value);
470                         end_trace();
471                         send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
472                         return 0;
473                 }
474                 /* creating port object, transparent until setup with hdlc */
475                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
476                 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
477
478                         FATAL("Cannot create Port instance.\n");
479         }
480
481         switch(msg_type) {
482                 case MNCC_SETUP_IND:
483                 pgsm_bs->setup_ind(msg_type, callref, mncc);
484                 break;
485
486                 case MNCC_START_DTMF_IND:
487                 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
488                 break;
489
490                 case MNCC_STOP_DTMF_IND:
491                 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
492                 break;
493
494                 case MNCC_CALL_CONF_IND:
495                 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
496                 break;
497
498                 case MNCC_ALERT_IND:
499                 pgsm_bs->alert_ind(msg_type, callref, mncc);
500                 break;
501
502                 case MNCC_SETUP_CNF:
503                 pgsm_bs->setup_cnf(msg_type, callref, mncc);
504                 break;
505
506                 case MNCC_SETUP_COMPL_IND:
507                 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
508                 break;
509
510                 case MNCC_DISC_IND:
511                 pgsm_bs->disc_ind(msg_type, callref, mncc);
512                 break;
513
514                 case MNCC_REL_IND:
515                 case MNCC_REL_CNF:
516                 case MNCC_REJ_IND:
517                 pgsm_bs->rel_ind(msg_type, callref, mncc);
518                 break;
519
520                 case MNCC_NOTIFY_IND:
521                 pgsm_bs->notify_ind(msg_type, callref, mncc);
522                 break;
523
524                 case MNCC_HOLD_IND:
525                 pgsm_bs->hold_ind(msg_type, callref, mncc);
526                 break;
527
528                 case MNCC_RETRIEVE_IND:
529                 pgsm_bs->retr_ind(msg_type, callref, mncc);
530                 break;
531
532                 default:
533                 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);
534         }
535         return(0);
536 }
537
538 /* MESSAGE_SETUP */
539 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
540 {
541         struct lcr_msg *message;
542         int ret;
543         struct epoint_list *epointlist;
544         struct gsm_mncc *mncc;
545         int channel;
546
547         /* copy setup infos to port */
548         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
549         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
550         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
551         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
552
553         /* no GSM MNCC connection */
554         if (p_m_g_lcr_gsm->mncc_lfd.fd < 0) {
555                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
556                 add_trace("failure", NULL, "No MNCC connection.");
557                 end_trace();
558                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
559                 message->param.disconnectinfo.cause = 41; // temp. failure.
560                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
561                 message_put(message);
562                 new_state(PORT_STATE_RELEASE);
563                 trigger_work(&p_m_g_delete);
564                 return;
565         }
566
567         /* no number */
568         if (!p_dialinginfo.id[0]) {
569                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
570                 add_trace("failure", NULL, "No dialed subscriber given.");
571                 end_trace();
572                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
573                 message->param.disconnectinfo.cause = 28;
574                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
575                 message_put(message);
576                 new_state(PORT_STATE_RELEASE);
577                 trigger_work(&p_m_g_delete);
578                 return;
579         }
580         
581         /* release if port is blocked */
582         if (p_m_mISDNport->ifport->block) {
583                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
584                 add_trace("failure", NULL, "Port blocked.");
585                 end_trace();
586                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
587                 message->param.disconnectinfo.cause = 27; // temp. unavail.
588                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
589                 message_put(message);
590                 new_state(PORT_STATE_RELEASE);
591                 trigger_work(&p_m_g_delete);
592                 return;
593         }
594
595         /* hunt channel */
596         ret = channel = hunt_bchannel();
597         if (ret < 0)
598                 goto no_channel;
599         /* open channel */
600         ret = seize_bchannel(channel, 1);
601         if (ret < 0) {
602                 no_channel:
603                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
604                 add_trace("failure", NULL, "No internal audio channel available.");
605                 end_trace();
606                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
607                 message->param.disconnectinfo.cause = 34;
608                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
609                 message_put(message);
610                 new_state(PORT_STATE_RELEASE);
611                 trigger_work(&p_m_g_delete);
612                 return;
613         }
614         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
615         if (bchannel_open(p_m_b_index))
616                 goto no_channel;
617
618 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
619         /* screen outgoing caller id */
620         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
621
622         /* attach only if not already */
623         epointlist = p_epointlist;
624         while(epointlist) {
625                 if (epointlist->epoint_id == epoint_id)
626                         break;
627                 epointlist = epointlist->next;
628         }
629         if (!epointlist)
630                 epointlist_new(epoint_id);
631
632         /* creating l3id */
633         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
634         p_m_g_callref = new_callref++;
635         add_trace("callref", "new", "0x%x", p_m_g_callref);
636         end_trace();
637
638         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
639         mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
640         /* caller information */
641         mncc->fields |= MNCC_F_CALLING;
642         mncc->calling.plan = 1;
643         switch (p_callerinfo.ntype) {
644                 case INFO_NTYPE_UNKNOWN:
645                 mncc->calling.type = 0x0;
646                 break;
647                 case INFO_NTYPE_INTERNATIONAL:
648                 mncc->calling.type = 0x1;
649                 break;
650                 case INFO_NTYPE_NATIONAL:
651                 mncc->calling.type = 0x2;
652                 break;
653                 case INFO_NTYPE_SUBSCRIBER:
654                 mncc->calling.type = 0x4;
655                 break;
656                 default: /* INFO_NTYPE_NOTPRESENT */
657                 mncc->fields &= ~MNCC_F_CALLING;
658                 break;
659         }
660         switch (p_callerinfo.screen) {
661                 case INFO_SCREEN_USER:
662                 mncc->calling.screen = 0;
663                 break;
664                 default: /* INFO_SCREEN_NETWORK */
665                 mncc->calling.screen = 3;
666                 break;
667         }
668         switch (p_callerinfo.present) {
669                 case INFO_PRESENT_ALLOWED:
670                 mncc->calling.present = 0;
671                 break;
672                 case INFO_PRESENT_RESTRICTED:
673                 mncc->calling.present = 1;
674                 break;
675                 default: /* INFO_PRESENT_NOTAVAIL */
676                 mncc->calling.present = 2;
677                 break;
678         }
679         if (mncc->fields & MNCC_F_CALLING) {
680                 SCPY(mncc->calling.number, p_callerinfo.id);
681                 add_trace("calling", "type", "%d", mncc->calling.type);
682                 add_trace("calling", "plan", "%d", mncc->calling.plan);
683                 add_trace("calling", "present", "%d", mncc->calling.present);
684                 add_trace("calling", "screen", "%d", mncc->calling.screen);
685                 add_trace("calling", "number", "%s", mncc->calling.number);
686         }
687         /* dialing information */
688         mncc->fields |= MNCC_F_CALLED;
689         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
690                 SCPY(mncc->imsi, p_dialinginfo.id+5);
691                 add_trace("dialing", "imsi", "%s", mncc->imsi);
692         } else {
693                 SCPY(mncc->called.number, p_dialinginfo.id);
694                 add_trace("dialing", "number", "%s", mncc->called.number);
695         }
696         
697         /* sending user-user */
698
699         /* redirecting number */
700         mncc->fields |= MNCC_F_REDIRECTING;
701         mncc->redirecting.plan = 1;
702         switch (p_redirinfo.ntype) {
703                 case INFO_NTYPE_UNKNOWN:
704                 mncc->redirecting.type = 0x0;
705                 break;
706                 case INFO_NTYPE_INTERNATIONAL:
707                 mncc->redirecting.type = 0x1;
708                 break;
709                 case INFO_NTYPE_NATIONAL:
710                 mncc->redirecting.type = 0x2;
711                 break;
712                 case INFO_NTYPE_SUBSCRIBER:
713                 mncc->redirecting.type = 0x4;
714                 break;
715                 default: /* INFO_NTYPE_NOTPRESENT */
716                 mncc->fields &= ~MNCC_F_REDIRECTING;
717                 break;
718         }
719         switch (p_redirinfo.screen) {
720                 case INFO_SCREEN_USER:
721                 mncc->redirecting.screen = 0;
722                 break;
723                 default: /* INFO_SCREE_NETWORK */
724                 mncc->redirecting.screen = 3;
725                 break;
726         }
727         switch (p_redirinfo.present) {
728                 case INFO_PRESENT_ALLOWED:
729                 mncc->redirecting.present = 0;
730                 break;
731                 case INFO_PRESENT_RESTRICTED:
732                 mncc->redirecting.present = 1;
733                 break;
734                 default: /* INFO_PRESENT_NOTAVAIL */
735                 mncc->redirecting.present = 2;
736                 break;
737         }
738         /* sending redirecting number only in ntmode */
739         if (mncc->fields & MNCC_F_REDIRECTING) {
740                 SCPY(mncc->redirecting.number, p_redirinfo.id);
741                 add_trace("redir", "type", "%d", mncc->redirecting.type);
742                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
743                 add_trace("redir", "present", "%d", mncc->redirecting.present);
744                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
745                 add_trace("redir", "number", "%s", mncc->redirecting.number);
746         }
747         /* bearer capability */
748         //todo
749
750         end_trace();
751         send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
752
753         new_state(PORT_STATE_OUT_SETUP);
754
755         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
756         message_put(message);
757
758         new_state(PORT_STATE_OUT_PROCEEDING);
759 }
760
761 /*
762  * endpoint sends messages to the port
763  */
764 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
765 {
766         if (Pgsm::message_epoint(epoint_id, message_id, param))
767                 return(1);
768
769         switch(message_id) {
770                 case MESSAGE_SETUP: /* dial-out command received from epoint */
771                 if (p_state!=PORT_STATE_IDLE)
772                         break;
773                 message_setup(epoint_id, message_id, param);
774                 break;
775
776                 default:
777                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
778         }
779
780         return(1);
781 }
782
783 int gsm_bs_exit(int rc)
784 {
785         /* free gsm instance */
786         if (gsm_bs) {
787                 if (gsm_bs->mncc_lfd.fd > -1) {
788                         close(gsm_bs->mncc_lfd.fd);
789                         unregister_fd(&gsm_bs->mncc_lfd);
790                 }
791
792                 del_timer(&gsm_bs->socket_retry);
793                 free(gsm_bs);
794                 gsm_bs = NULL;
795         }
796
797
798         return(rc);
799 }
800
801 int gsm_bs_init(void)
802 {
803         /* create gsm instance */
804         gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
805
806         gsm_bs->type = LCR_GSM_TYPE_NETWORK;
807         gsm_bs->sun.sun_family = AF_UNIX;
808         SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
809
810         memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
811         add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
812
813         /* do the initial connect */
814         mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);
815
816         generate_dtmf();
817
818         return 0;
819 }