Adding bridge between protocol handlers (ports)
[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         mode->lchan_type = 0x02;
357         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
358         end_trace();
359         send_and_free_mncc(p_m_g_lcr_gsm, mode->msg_type, mode);
360
361         /* send call proceeding */
362         gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
363         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
364         if (p_m_mISDNport->tones) {
365                 proceeding->fields |= MNCC_F_PROGRESS;
366                 proceeding->progress.coding = 3; /* GSM */
367                 proceeding->progress.location = 1;
368                 proceeding->progress.descr = 8;
369                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
370                 add_trace("progress", "location", "%d", proceeding->progress.location);
371                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
372         }
373         end_trace();
374         send_and_free_mncc(p_m_g_lcr_gsm, proceeding->msg_type, proceeding);
375
376         new_state(PORT_STATE_IN_PROCEEDING);
377
378         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
379                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
380                 end_trace();
381                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
382                 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
383                 p_m_g_tch_connected = 1;
384         }
385
386         /* send setup message to endpoit */
387         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
388         message->param.setup.isdn_port = p_m_portnum;
389         message->param.setup.port_type = p_type;
390 //      message->param.setup.dtmf = 0;
391         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
392         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
393         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
394         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
395         message->param.setup.useruser.len = strlen(mncc->useruser.info);
396         message->param.setup.useruser.protocol = mncc->useruser.proto;
397         message_put(message);
398 }
399
400 /*
401  * BSC sends message to port
402  */
403 int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
404 {
405         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
406         unsigned int callref = mncc->callref;
407         class Port *port;
408         class Pgsm_bs *pgsm_bs = NULL;
409         char name[64];
410         struct mISDNport *mISDNport;
411
412         /* Special messages */
413         switch(msg_type) {
414         }
415
416         /* find callref */
417         callref = mncc->callref;
418         port = port_first;
419         while(port) {
420                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_BS) {
421                         pgsm_bs = (class Pgsm_bs *)port;
422                         if (pgsm_bs->p_m_g_callref == callref) {
423                                 break;
424                         }
425                 }
426                 port = port->next;
427         }
428
429         if (msg_type == GSM_TCHF_FRAME) {
430                 if (port) {
431                         /* inject DTMF, if enabled */
432                         if (pgsm_bs->p_m_g_dtmf) {
433                                 unsigned char data[160];
434                                 int i;
435
436                                 for (i = 0; i < 160; i++) {
437                                         data[i] = pgsm_bs->p_m_g_dtmf[pgsm_bs->p_m_g_dtmf_index++];
438                                         if (pgsm_bs->p_m_g_dtmf_index == 8000)
439                                                 pgsm_bs->p_m_g_dtmf_index = 0;
440                                 }
441                                 /* send */
442                                 pgsm_bs->bchannel_send(PH_DATA_REQ, 0, data, 160);
443                         } else
444                                 pgsm_bs->frame_receive(arg);
445                 }
446                 return 0;
447         }
448
449         if (!port) {
450                 if (msg_type != MNCC_SETUP_IND)
451                         return(0);
452                 /* find gsm port */
453                 mISDNport = mISDNport_first;
454                 while(mISDNport) {
455                         if (mISDNport->gsm_bs)
456                                 break;
457                         mISDNport = mISDNport->next;
458                 }
459                 if (!mISDNport) {
460                         struct gsm_mncc *rej;
461
462                         rej = create_mncc(MNCC_REJ_REQ, callref);
463                         rej->fields |= MNCC_F_CAUSE;
464                         rej->cause.coding = 3;
465                         rej->cause.location = 1;
466                         rej->cause.value = 27;
467                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
468                         add_trace("cause", "coding", "%d", rej->cause.coding);
469                         add_trace("cause", "location", "%d", rej->cause.location);
470                         add_trace("cause", "value", "%d", rej->cause.value);
471                         end_trace();
472                         send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
473                         return 0;
474                 }
475                 /* creating port object, transparent until setup with hdlc */
476                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
477                 if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
478
479                         FATAL("Cannot create Port instance.\n");
480         }
481
482         switch(msg_type) {
483                 case MNCC_SETUP_IND:
484                 pgsm_bs->setup_ind(msg_type, callref, mncc);
485                 break;
486
487                 case MNCC_START_DTMF_IND:
488                 pgsm_bs->start_dtmf_ind(msg_type, callref, mncc);
489                 break;
490
491                 case MNCC_STOP_DTMF_IND:
492                 pgsm_bs->stop_dtmf_ind(msg_type, callref, mncc);
493                 break;
494
495                 case MNCC_CALL_CONF_IND:
496                 pgsm_bs->call_conf_ind(msg_type, callref, mncc);
497                 break;
498
499                 case MNCC_ALERT_IND:
500                 pgsm_bs->alert_ind(msg_type, callref, mncc);
501                 break;
502
503                 case MNCC_SETUP_CNF:
504                 pgsm_bs->setup_cnf(msg_type, callref, mncc);
505                 break;
506
507                 case MNCC_SETUP_COMPL_IND:
508                 pgsm_bs->setup_compl_ind(msg_type, callref, mncc);
509                 break;
510
511                 case MNCC_DISC_IND:
512                 pgsm_bs->disc_ind(msg_type, callref, mncc);
513                 break;
514
515                 case MNCC_REL_IND:
516                 case MNCC_REL_CNF:
517                 case MNCC_REJ_IND:
518                 pgsm_bs->rel_ind(msg_type, callref, mncc);
519                 break;
520
521                 case MNCC_NOTIFY_IND:
522                 pgsm_bs->notify_ind(msg_type, callref, mncc);
523                 break;
524
525                 case MNCC_HOLD_IND:
526                 pgsm_bs->hold_ind(msg_type, callref, mncc);
527                 break;
528
529                 case MNCC_RETRIEVE_IND:
530                 pgsm_bs->retr_ind(msg_type, callref, mncc);
531                 break;
532
533                 default:
534                 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);
535         }
536         return(0);
537 }
538
539 /* MESSAGE_SETUP */
540 void Pgsm_bs::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
541 {
542         struct lcr_msg *message;
543         int ret;
544         struct epoint_list *epointlist;
545         struct gsm_mncc *mncc;
546         int channel;
547
548         /* copy setup infos to port */
549         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
550         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
551         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
552         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
553
554         /* no GSM MNCC connection */
555         if (p_m_g_lcr_gsm->mncc_lfd.fd < 0) {
556                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
557                 add_trace("failure", NULL, "No MNCC connection.");
558                 end_trace();
559                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
560                 message->param.disconnectinfo.cause = 41; // temp. failure.
561                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
562                 message_put(message);
563                 new_state(PORT_STATE_RELEASE);
564                 trigger_work(&p_m_g_delete);
565                 return;
566         }
567
568         /* no number */
569         if (!p_dialinginfo.id[0]) {
570                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
571                 add_trace("failure", NULL, "No dialed subscriber given.");
572                 end_trace();
573                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
574                 message->param.disconnectinfo.cause = 28;
575                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
576                 message_put(message);
577                 new_state(PORT_STATE_RELEASE);
578                 trigger_work(&p_m_g_delete);
579                 return;
580         }
581         
582         /* release if port is blocked */
583         if (p_m_mISDNport->ifport->block) {
584                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
585                 add_trace("failure", NULL, "Port blocked.");
586                 end_trace();
587                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
588                 message->param.disconnectinfo.cause = 27; // temp. unavail.
589                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
590                 message_put(message);
591                 new_state(PORT_STATE_RELEASE);
592                 trigger_work(&p_m_g_delete);
593                 return;
594         }
595
596         /* hunt channel */
597         ret = channel = hunt_bchannel();
598         if (ret < 0)
599                 goto no_channel;
600         /* open channel */
601         ret = seize_bchannel(channel, 1);
602         if (ret < 0) {
603                 no_channel:
604                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
605                 add_trace("failure", NULL, "No internal audio channel available.");
606                 end_trace();
607                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
608                 message->param.disconnectinfo.cause = 34;
609                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
610                 message_put(message);
611                 new_state(PORT_STATE_RELEASE);
612                 trigger_work(&p_m_g_delete);
613                 return;
614         }
615         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
616         if (bchannel_open(p_m_b_index))
617                 goto no_channel;
618
619 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
620         /* screen outgoing caller id */
621         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
622
623         /* attach only if not already */
624         epointlist = p_epointlist;
625         while(epointlist) {
626                 if (epointlist->epoint_id == epoint_id)
627                         break;
628                 epointlist = epointlist->next;
629         }
630         if (!epointlist)
631                 epointlist_new(epoint_id);
632
633         /* creating l3id */
634         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
635         p_m_g_callref = new_callref++;
636         add_trace("callref", "new", "0x%x", p_m_g_callref);
637         end_trace();
638
639         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
640         mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
641         /* caller information */
642         mncc->fields |= MNCC_F_CALLING;
643         mncc->calling.plan = 1;
644         switch (p_callerinfo.ntype) {
645                 case INFO_NTYPE_UNKNOWN:
646                 mncc->calling.type = 0x0;
647                 break;
648                 case INFO_NTYPE_INTERNATIONAL:
649                 mncc->calling.type = 0x1;
650                 break;
651                 case INFO_NTYPE_NATIONAL:
652                 mncc->calling.type = 0x2;
653                 break;
654                 case INFO_NTYPE_SUBSCRIBER:
655                 mncc->calling.type = 0x4;
656                 break;
657                 default: /* INFO_NTYPE_NOTPRESENT */
658                 mncc->fields &= ~MNCC_F_CALLING;
659                 break;
660         }
661         switch (p_callerinfo.screen) {
662                 case INFO_SCREEN_USER:
663                 mncc->calling.screen = 0;
664                 break;
665                 default: /* INFO_SCREEN_NETWORK */
666                 mncc->calling.screen = 3;
667                 break;
668         }
669         switch (p_callerinfo.present) {
670                 case INFO_PRESENT_ALLOWED:
671                 mncc->calling.present = 0;
672                 break;
673                 case INFO_PRESENT_RESTRICTED:
674                 mncc->calling.present = 1;
675                 break;
676                 default: /* INFO_PRESENT_NOTAVAIL */
677                 mncc->calling.present = 2;
678                 break;
679         }
680         if (mncc->fields & MNCC_F_CALLING) {
681                 SCPY(mncc->calling.number, p_callerinfo.id);
682                 add_trace("calling", "type", "%d", mncc->calling.type);
683                 add_trace("calling", "plan", "%d", mncc->calling.plan);
684                 add_trace("calling", "present", "%d", mncc->calling.present);
685                 add_trace("calling", "screen", "%d", mncc->calling.screen);
686                 add_trace("calling", "number", "%s", mncc->calling.number);
687         }
688         /* dialing information */
689         mncc->fields |= MNCC_F_CALLED;
690         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
691                 SCPY(mncc->imsi, p_dialinginfo.id+5);
692                 add_trace("dialing", "imsi", "%s", mncc->imsi);
693         } else {
694                 SCPY(mncc->called.number, p_dialinginfo.id);
695                 add_trace("dialing", "number", "%s", mncc->called.number);
696         }
697         
698         /* sending user-user */
699
700         /* redirecting number */
701         mncc->fields |= MNCC_F_REDIRECTING;
702         mncc->redirecting.plan = 1;
703         switch (p_redirinfo.ntype) {
704                 case INFO_NTYPE_UNKNOWN:
705                 mncc->redirecting.type = 0x0;
706                 break;
707                 case INFO_NTYPE_INTERNATIONAL:
708                 mncc->redirecting.type = 0x1;
709                 break;
710                 case INFO_NTYPE_NATIONAL:
711                 mncc->redirecting.type = 0x2;
712                 break;
713                 case INFO_NTYPE_SUBSCRIBER:
714                 mncc->redirecting.type = 0x4;
715                 break;
716                 default: /* INFO_NTYPE_NOTPRESENT */
717                 mncc->fields &= ~MNCC_F_REDIRECTING;
718                 break;
719         }
720         switch (p_redirinfo.screen) {
721                 case INFO_SCREEN_USER:
722                 mncc->redirecting.screen = 0;
723                 break;
724                 default: /* INFO_SCREE_NETWORK */
725                 mncc->redirecting.screen = 3;
726                 break;
727         }
728         switch (p_redirinfo.present) {
729                 case INFO_PRESENT_ALLOWED:
730                 mncc->redirecting.present = 0;
731                 break;
732                 case INFO_PRESENT_RESTRICTED:
733                 mncc->redirecting.present = 1;
734                 break;
735                 default: /* INFO_PRESENT_NOTAVAIL */
736                 mncc->redirecting.present = 2;
737                 break;
738         }
739         /* sending redirecting number only in ntmode */
740         if (mncc->fields & MNCC_F_REDIRECTING) {
741                 SCPY(mncc->redirecting.number, p_redirinfo.id);
742                 add_trace("redir", "type", "%d", mncc->redirecting.type);
743                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
744                 add_trace("redir", "present", "%d", mncc->redirecting.present);
745                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
746                 add_trace("redir", "number", "%s", mncc->redirecting.number);
747         }
748         /* bearer capability */
749         //todo
750
751         end_trace();
752         send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
753
754         new_state(PORT_STATE_OUT_SETUP);
755
756         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
757         message_put(message);
758
759         new_state(PORT_STATE_OUT_PROCEEDING);
760 }
761
762 /*
763  * endpoint sends messages to the port
764  */
765 int Pgsm_bs::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
766 {
767         if (Pgsm::message_epoint(epoint_id, message_id, param))
768                 return(1);
769
770         switch(message_id) {
771                 case MESSAGE_SETUP: /* dial-out command received from epoint */
772                 if (p_state!=PORT_STATE_IDLE)
773                         break;
774                 message_setup(epoint_id, message_id, param);
775                 break;
776
777                 default:
778                 PDEBUG(DEBUG_GSM, "Pgsm_bs(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
779         }
780
781         return(1);
782 }
783
784 int gsm_bs_exit(int rc)
785 {
786         /* free gsm instance */
787         if (gsm_bs) {
788                 if (gsm_bs->mncc_lfd.fd > -1) {
789                         close(gsm_bs->mncc_lfd.fd);
790                         unregister_fd(&gsm_bs->mncc_lfd);
791                 }
792
793                 del_timer(&gsm_bs->socket_retry);
794                 free(gsm_bs);
795                 gsm_bs = NULL;
796         }
797
798
799         return(rc);
800 }
801
802 int gsm_bs_init(void)
803 {
804         /* create gsm instance */
805         gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
806
807         gsm_bs->type = LCR_GSM_TYPE_NETWORK;
808         gsm_bs->sun.sun_family = AF_UNIX;
809         SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
810
811         memset(&gsm_bs->socket_retry, 0, sizeof(gsm_bs->socket_retry));
812         add_timer(&gsm_bs->socket_retry, mncc_socket_retry_cb, gsm_bs, 0);
813
814         /* do the initial connect */
815         mncc_socket_retry_cb(&gsm_bs->socket_retry, gsm_bs, 0);
816
817         generate_dtmf();
818
819         return 0;
820 }