86e7f4498de9cc5f02079a33c7c52c2b0b06de73
[lcr.git] / gsm.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN gsm                                                                 **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13 extern "C" {
14 #include "openbsc/gsm_data.h"
15 #include "openbsc/mncc.h"
16 #include "openbsc/trau_frame.h"
17 #include "openbsc/select.h"
18 #include "openbsc/debug.h"
19 #include "openbsc/e1_input.h"
20 #include "bootstrap.h"
21 #include "gsm_audio.h"
22
23 #undef AF_ISDN
24 #undef PF_ISDN
25 extern  int     AF_ISDN;
26 #define PF_ISDN AF_ISDN
27 }
28
29 struct lcr_gsm *gsm = NULL;
30
31 static unsigned int new_callref = 1;
32
33
34 /*
35  * create and send mncc message
36  */
37 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
38 {
39         struct gsm_mncc *mncc;
40
41         mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
42         mncc->msg_type = msg_type;
43         mncc->callref = callref;
44         return (mncc);
45 }
46 static int send_and_free_mncc(struct gsm_network *net, unsigned int msg_type, void *data)
47 {
48         int ret;
49
50         ret = mncc_send(net, msg_type, data);
51         free(data);
52
53         return ret;
54 }
55
56
57 /*
58  * constructor
59  */
60 Pgsm::Pgsm(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : PmISDN(type, mISDNport, portname, settings, channel, exclusive, mode)
61 {
62         p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
63         p_m_g_callref = 0;
64         p_m_g_mode = 0;
65         p_m_g_gsm_b_sock = -1;
66         p_m_g_gsm_b_index = -1;
67         p_m_g_gsm_b_active = 0;
68         p_m_g_notify_pending = NULL;
69         p_m_g_decoder = gsm_audio_create();
70         p_m_g_encoder = gsm_audio_create();
71         if (!p_m_g_encoder || !p_m_g_decoder) {
72                 PERROR("Failed to create GSM audio codec instance\n");
73                 p_m_delete = 1;
74         }
75         p_m_g_rxpos = 0;
76         p_m_g_tch_connected = 0;
77
78         PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
79 }
80
81 /*
82  * destructor
83  */
84 Pgsm::~Pgsm()
85 {
86         PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
87
88         /* remove queued message */
89         if (p_m_g_notify_pending)
90                 message_free(p_m_g_notify_pending);
91
92         /* close audio transfer socket */
93         if (p_m_g_gsm_b_sock > -1)
94                 bchannel_close();
95
96         /* close codec */
97         if (p_m_g_encoder)
98                 gsm_audio_destroy(p_m_g_encoder);
99         if (p_m_g_decoder)
100                 gsm_audio_destroy(p_m_g_decoder);
101 }
102
103
104 /* close bsc side bchannel */
105 void Pgsm::bchannel_close(void)
106 {
107         if (p_m_g_gsm_b_sock > -1)
108                 close(p_m_g_gsm_b_sock);
109         p_m_g_gsm_b_sock = -1;
110         p_m_g_gsm_b_index = -1;
111         p_m_g_gsm_b_active = 0;
112 }
113
114 /* open bsc side bchannel */
115 int Pgsm::bchannel_open(int index)
116 {
117         int ret;
118         unsigned int on = 1;
119         struct sockaddr_mISDN addr;
120         struct mISDNhead act;
121
122         if (p_m_g_gsm_b_sock > -1) {
123                 PERROR("Socket already created for index %d\n", index);
124                 return(-EIO);
125         }
126
127         /* open socket */
128         ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
129         if (ret < 0) {
130                 PERROR("Failed to open bchannel-socket for index %d\n", index);
131                 bchannel_close();
132                 return(ret);
133         }
134         
135         /* set nonblocking io */
136         ret = ioctl(p_m_g_gsm_b_sock, FIONBIO, &on);
137         if (ret < 0) {
138                 PERROR("Failed to set bchannel-socket index %d into nonblocking IO\n", index);
139                 bchannel_close();
140                 return(ret);
141         }
142
143         /* bind socket to bchannel */
144         addr.family = AF_ISDN;
145         addr.dev = gsm->gsm_port;
146         addr.channel = index+1+(index>15);
147         ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
148         if (ret < 0) {
149                 PERROR("Failed to bind bchannel-socket for index %d\n", index);
150                 bchannel_close();
151                 return(ret);
152         }
153         /* activate bchannel */
154         PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
155         act.prim = PH_ACTIVATE_REQ; 
156         act.id = 0;
157         ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
158         if (ret < 0) {
159                 PERROR("Failed to activate index %d\n", index);
160                 bchannel_close();
161                 return(ret);
162         }
163
164         p_m_g_gsm_b_index = index;
165
166         return(0);
167 }
168
169 /* receive from bchannel */
170 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
171 {
172         int i, j, k;
173         unsigned char frame[33];
174         struct decoded_trau_frame tf;
175
176         /* encoder init failed */
177         if (!p_m_g_encoder)
178                 return;
179
180         /* (currently) not connected, so don't flood tch! */
181         if (!p_m_g_tch_connected)
182                 return;
183
184         /* write to rx buffer */
185         while(len--) {
186                 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
187                 if (p_m_g_rxpos == 160) {
188                         p_m_g_rxpos = 0;
189
190                         /* encode data */
191                         gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
192
193                         /* set c-bits and t-bits */
194                         tf.c_bits[0] = 1;
195                         tf.c_bits[1] = 1;
196                         tf.c_bits[2] = 1;
197                         tf.c_bits[3] = 0;
198                         tf.c_bits[4] = 0;
199                         memset(&tf.c_bits[5], 0, 6);
200                         memset(&tf.c_bits[11], 1, 10);
201                         memset(&tf.t_bits[0], 1, 4);
202
203                         /* reassemble d-bits */
204                         i = 0;
205                         j = 0;
206                         k = 0;
207                         while(i < 260) {
208                                 tf.d_bits[i] = (frame[j] >> k) & 1;
209                                 if (++k == 8) {
210                                         k = 0;
211                                         j++;
212                                 }
213                                 i++;
214                         }
215
216                         trau_send(&tf);
217                 }
218         }
219 }
220
221 /* transmit to bchannel */
222 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
223 {
224         unsigned char buf[MISDN_HEADER_LEN+len];
225         struct mISDNhead *hh = (struct mISDNhead *)buf;
226         int ret;
227
228         if (!p_m_g_gsm_b_active)
229                 return;
230
231         /* make and send frame */
232         hh->prim = PH_DATA_REQ;
233         hh->id = 0;
234         memcpy(buf+MISDN_HEADER_LEN, data, len);
235         ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
236         if (ret <= 0)
237                 PERROR("Failed to send to socket index %d\n", index);
238 }
239
240 void Pgsm::trau_send(void *_tf)
241 {
242         struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_tf;
243         unsigned char data[sizeof(struct gsm_trau_frame) + sizeof(struct decoded_trau_frame)];
244         struct gsm_trau_frame *frame = (struct gsm_trau_frame *)data;
245         
246         frame->msg_type = GSM_TRAU_FRAME;
247         frame->callref = p_m_g_callref;
248         memcpy(frame->data, tf, sizeof(struct decoded_trau_frame));
249         mncc_send((struct gsm_network *)gsm->network, frame->msg_type, frame);
250 }
251
252
253 void Pgsm::trau_receive(void *_frame)
254 {
255         struct gsm_trau_frame *frm = (struct gsm_trau_frame *)_frame;
256         struct decoded_trau_frame *tf = (struct decoded_trau_frame *)frm->data;
257 //struct decoded_trau_frame *tf = (struct decoded_trau_frame *)_frame;
258         unsigned char frame[33];
259         signed short samples[160];
260         unsigned char data[160];
261         int i, j, k;
262
263         if (!p_m_g_decoder)
264                 return;
265
266 //      printf("got trau %d %d %d %d %d\n", tf->c_bits[0], tf->c_bits[1], tf->c_bits[2], tf->c_bits[3], tf->c_bits[4]);
267         if (tf->c_bits[0]!=0 || tf->c_bits[1]!=0 || tf->c_bits[2]!=0 || tf->c_bits[3]!=1 || tf->c_bits[4]!=0)
268                 PERROR("illegal trau (C1-C5) %d %d %d %d %d\n", tf->c_bits[0], tf->c_bits[1], tf->c_bits[2], tf->c_bits[3], tf->c_bits[4]);
269
270         /* set GSM_MAGIC */
271         memset(&frame, 0, sizeof(frame));
272 //      frame[0] = 0xd << 4;
273
274         /* reassemble bits */
275         i = 0;
276         j = 0;
277         k = 0;
278         while(i < 260) {
279                 if (tf->d_bits[i] > 1)
280                         PERROR("fix!\n");
281                 frame[j] |= (tf->d_bits[i] << k);
282                 if (++k == 8) {
283                         k = 0;
284                         j++;
285                 }
286                 i++;
287         }
288         
289         /* decode */
290         gsm_audio_decode(p_m_g_decoder, frame, samples);
291         for (i = 0; i < 160; i++) {
292                 data[i] = audio_s16_to_law[samples[i] & 0xffff];
293         }
294
295         /* send */
296         bchannel_send(PH_DATA_REQ, 0, data, 160);
297 }
298
299
300 /*
301  * create trace
302  **/
303 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
304 {
305         char msgtext[64];
306
307         /* select message and primitive text */
308         SCPY(msgtext, get_mncc_name(msg_type));
309
310         /* add direction */
311         if (direction == DIRECTION_OUT)
312                 SCAT(msgtext, " MSC->BSC");
313         else
314                 SCAT(msgtext, " MSC<-BSC");
315
316         /* init trace with given values */
317         start_trace(mISDNport?mISDNport->portnum:-1,
318                     mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
319                     port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
320                     port?port->p_dialinginfo.id:NULL,
321                     direction,
322                     CATEGORY_CH,
323                     port?port->p_serial:0,
324                     msgtext);
325 }
326
327
328
329 /* select bchannel */
330 int Pgsm::hunt_bchannel(void)
331 {
332         int channel;
333         int i;
334
335         chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
336         add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
337         if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
338                 add_trace("conclusion", NULL, "all channels are reserved");
339                 end_trace();
340                 return(-34); // no channel
341         }
342         /* find channel */
343         i = 0;
344         channel = 0;
345         while(i < p_m_mISDNport->b_num) {
346                 if (p_m_mISDNport->b_port[i] == NULL) {
347                         channel = i+1+(i>=15);
348                         break;
349                 }
350                 i++;
351         }
352         if (!channel) {
353                 add_trace("conclusion", NULL, "no channel available");
354                 end_trace();
355                 return(-6); // channel unacceptable
356         }
357         add_trace("conclusion", NULL, "channel available");
358         add_trace("connect", "channel", "%d", channel);
359         end_trace();
360         return(channel);
361 }
362
363
364 /*
365  * handles all indications
366  */
367 /* SETUP INDICATION */
368 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
369 {
370         int ret;
371         class Endpoint *epoint;
372         struct lcr_msg *message;
373         int channel;
374         struct gsm_mncc *mode, *proceeding, *frame;
375
376         /* emergency shutdown */
377         printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
378         if (mncc->emergency && !gsm->conf.noemergshut) {
379                 start_trace(p_m_mISDNport->portnum,
380                         p_m_mISDNport->ifport->interface,
381                         NULL,
382                         NULL,
383                         DIRECTION_NONE,
384                         CATEGORY_CH,
385                         0,
386                         "EMERGENCY SHUTDOWN (due to received emergency call)");
387                 end_trace();
388                 quit = 1;
389         }
390         /* process given callref */
391         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
392         add_trace("callref", "new", "0x%x", callref);
393         if (p_m_g_callref) {
394                 /* release in case the ID is already in use */
395                 add_trace("error", NULL, "callref already in use");
396                 end_trace();
397                 mncc = create_mncc(MNCC_REJ_REQ, callref);
398                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
399                 mncc->cause = 1;
400                 mncc->cause_coding = 3;
401                 mncc->cause_location = 1;
402                 mncc->cause_value = 47;
403                 add_trace("cause", "coding", "%d", mncc->cause_coding);
404                 add_trace("cause", "location", "%d", mncc->cause_location);
405                 add_trace("cause", "value", "%d", mncc->cause_value);
406                 add_trace("reason", NULL, "callref already in use");
407                 end_trace();
408                 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
409                 new_state(PORT_STATE_RELEASE);
410                 p_m_delete = 1;
411                 return;
412         }
413         p_m_g_callref = callref;
414         end_trace();
415
416         /* if blocked, release call with MT_RELEASE_COMPLETE */
417         if (p_m_mISDNport->ifport->block) {
418                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
419                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
420                 mncc->cause = 1;
421                 mncc->cause_coding = 3;
422                 mncc->cause_location = 1;
423                 mncc->cause_value = 27;
424                 add_trace("cause", "coding", "%d", mncc->cause_coding);
425                 add_trace("cause", "location", "%d", mncc->cause_location);
426                 add_trace("cause", "value", "%d", mncc->cause_value);
427                 add_trace("reason", NULL, "port is blocked");
428                 end_trace();
429                 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
430                 new_state(PORT_STATE_RELEASE);
431                 p_m_delete = 1;
432                 return;
433         }
434
435         /* caller info */
436         if (mncc->clir_inv)
437                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
438         else
439                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
440         if (mncc->calling_number[0])
441                 SCPY(p_callerinfo.id, mncc->calling_number);
442         else
443                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
444         SCPY(p_callerinfo.imsi, mncc->imsi);
445         p_callerinfo.screen = INFO_SCREEN_NETWORK;
446         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
447         p_callerinfo.isdn_port = p_m_portnum;
448         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
449
450         /* dialing information */
451         SCAT(p_dialinginfo.id, mncc->called_number);
452         switch (mncc->called_type) {
453                 case 0x1:
454                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
455                 break;
456                 case 0x2:
457                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
458                 break;
459                 case 0x4:
460                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
461                 break;
462                 default:
463                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
464                 break;
465         }
466         if (mncc->emergency) {
467                 SCPY(p_dialinginfo.id, "emergency");
468         }
469         p_dialinginfo.sending_complete = 1;
470
471         /* bearer capability */
472         // todo
473         p_capainfo.bearer_capa = INFO_BC_SPEECH;
474         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
475         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
476         p_capainfo.source_mode = B_MODE_TRANSPARENT;
477         p_m_g_mode = p_capainfo.source_mode;
478
479         /* useruser */
480
481         /* hunt channel */
482         ret = channel = hunt_bchannel();
483         if (ret < 0)
484                 goto no_channel;
485
486         /* open channel */
487         ret = seize_bchannel(channel, 1);
488         if (ret < 0) {
489                 no_channel:
490                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
491                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
492                 mncc->cause = 1;
493                 mncc->cause_coding = 3;
494                 mncc->cause_location = 1;
495                 mncc->cause_value = 34;
496                 add_trace("cause", "coding", "%d", mncc->cause_coding);
497                 add_trace("cause", "location", "%d", mncc->cause_location);
498                 add_trace("cause", "value", "%d", mncc->cause_value);
499                 add_trace("reason", NULL, "no channel");
500                 end_trace();
501                 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
502                 new_state(PORT_STATE_RELEASE);
503                 p_m_delete = 1;
504                 return;
505         }
506         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
507         if (bchannel_open(p_m_b_index))
508                 goto no_channel;
509
510         /* what infos did we got ... */
511         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
512         if (p_callerinfo.id[0])
513                 add_trace("calling", "number", "%s", p_callerinfo.id);
514         else
515                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
516         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
517         add_trace("dialing", "number", "%s", p_dialinginfo.id);
518         end_trace();
519
520         /* create endpoint */
521         if (p_epointlist)
522                 FATAL("Incoming call but already got an endpoint.\n");
523         if (!(epoint = new Endpoint(p_serial, 0)))
524                 FATAL("No memory for Endpoint instance\n");
525         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
526                 FATAL("No memory for Endpoint Application instance\n");
527         epointlist_new(epoint->ep_serial);
528
529         /* modify lchan to GSM codec V1 */
530         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
531         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
532         mode->lchan_mode = 0x01; /* GSM V1 */
533         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
534         end_trace();
535         send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
536
537         /* send call proceeding */
538         gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
539         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
540         if (p_m_mISDNport->tones) {
541                 proceeding->progress = 1;
542                 proceeding->progress_coding = 3; /* GSM */
543                 proceeding->progress_location = 1;
544                 proceeding->progress_descr = 8;
545                 add_trace("progress", "coding", "%d", proceeding->progress_coding);
546                 add_trace("progress", "location", "%d", proceeding->progress_location);
547                 add_trace("progress", "descr", "%d", proceeding->progress_descr);
548         }
549         end_trace();
550         send_and_free_mncc((struct gsm_network *)gsm->network, proceeding->msg_type, proceeding);
551
552         new_state(PORT_STATE_IN_PROCEEDING);
553
554         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
555                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
556                 end_trace();
557                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
558                 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
559                 p_m_g_tch_connected = 1;
560         }
561
562         /* send setup message to endpoit */
563         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
564         message->param.setup.isdn_port = p_m_portnum;
565         message->param.setup.port_type = p_type;
566 //      message->param.setup.dtmf = 0;
567         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
568         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
569         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
570         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser_info);
571         message->param.setup.useruser.len = strlen(mncc->useruser_info);
572         message->param.setup.useruser.protocol = mncc->useruser_proto;
573         message_put(message);
574 }
575
576 /* DTMF INDICATION */
577 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
578 {
579         struct lcr_msg *message;
580         struct gsm_mncc *resp;
581
582         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
583         add_trace("keypad", NULL, "%c", mncc->keypad);
584         end_trace();
585         SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
586         p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
587
588         /* send resp */
589         gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
590         add_trace("keypad", NULL, "%c", mncc->keypad);
591         end_trace();
592         resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
593         resp->keypad = mncc->keypad;
594         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
595
596         /* send dialing information */
597         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
598         memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
599         message_put(message);
600 }
601 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
602 {
603         struct gsm_mncc *resp;
604
605         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
606         add_trace("keypad", NULL, "%c", mncc->keypad);
607         end_trace();
608
609         /* send resp */
610         gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
611         add_trace("keypad", NULL, "%c", mncc->keypad);
612         end_trace();
613         resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
614         resp->keypad = mncc->keypad;
615         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
616 }
617
618 /* PROCEEDING INDICATION */
619 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
620 {
621         struct gsm_mncc *mode;
622
623         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
624         if (mncc->cause) {
625                 add_trace("cause", "coding", "%d", mncc->cause_coding);
626                 add_trace("cause", "location", "%", mncc->cause_location);
627                 add_trace("cause", "value", "%", mncc->cause_value);
628         }
629         end_trace();
630
631         /* modify lchan to GSM codec V1 */
632         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
633         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
634         mode->lchan_mode = 0x01; /* GSM V1 */
635         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
636         end_trace();
637         send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
638
639 }
640
641 /* ALERTING INDICATION */
642 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
643 {
644         struct lcr_msg *message;
645
646         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
647         end_trace();
648
649         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
650         message_put(message);
651
652         new_state(PORT_STATE_OUT_ALERTING);
653
654 }
655
656 /* CONNECT INDICATION */
657 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
658 {
659         struct gsm_mncc *resp, *frame;
660         struct lcr_msg *message;
661
662         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
663         end_trace();
664
665         SCPY(p_connectinfo.id, mncc->calling_number);
666         SCPY(p_connectinfo.imsi, mncc->imsi);
667         p_connectinfo.present = INFO_PRESENT_ALLOWED;
668         p_connectinfo.screen = INFO_SCREEN_NETWORK;
669         p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
670         p_connectinfo.isdn_port = p_m_portnum;
671         SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
672         /* send resp */
673         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
674         if (p_connectinfo.id[0])
675                 add_trace("connect", "number", "%s", p_connectinfo.id);
676         else
677                 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
678         add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
679         resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
680         end_trace();
681         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
682
683         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
684         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
685         message_put(message);
686
687         new_state(PORT_STATE_CONNECT);
688
689         if (!p_m_g_tch_connected) { /* only if ... */
690                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
691                 end_trace();
692                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
693                 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
694                 p_m_g_tch_connected = 1;
695         }
696 }
697
698 /* CONNECT ACK INDICATION */
699 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
700 {
701         struct gsm_mncc *frame;
702
703         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
704         end_trace();
705
706         new_state(PORT_STATE_CONNECT);
707
708         if (!p_m_g_tch_connected) { /* only if ... */
709                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
710                 end_trace();
711                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
712                 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
713                 p_m_g_tch_connected = 1;
714         }
715 }
716
717 /* DISCONNECT INDICATION */
718 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
719 {
720         struct lcr_msg *message;
721         int cause = 16, location = 0;
722         struct gsm_mncc *resp;
723
724         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
725         if (mncc->cause) {
726                 location = mncc->cause_location;
727                 cause = mncc->cause_value;
728                 add_trace("cause", "coding", "%d", mncc->cause_coding);
729                 add_trace("cause", "location", "%d", location);
730                 add_trace("cause", "value", "%d", cause);
731         }
732         end_trace();
733
734         /* send release */
735         resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
736         gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
737 #if 0
738         resp->cause = 1;
739         resp->cause_coding = 3;
740         resp->cause_location = 1;
741         resp->cause_value = cause;
742         add_trace("cause", "coding", "%d", resp->cause_coding);
743         add_trace("cause", "location", "%d", resp->cause_location);
744         add_trace("cause", "value", "%d", resp->cause_value);
745 #endif
746         end_trace();
747         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
748
749         /* sending release to endpoint */
750         while(p_epointlist) {
751                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
752                 message->param.disconnectinfo.cause = cause;
753                 message->param.disconnectinfo.location = location;
754                 message_put(message);
755                 /* remove epoint */
756                 free_epointlist(p_epointlist);
757         }
758         new_state(PORT_STATE_RELEASE);
759         p_m_delete = 1;
760 }
761
762 /* CC_RELEASE INDICATION */
763 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
764 {
765         int location = 0, cause = 16;
766         struct lcr_msg *message;
767
768         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
769         if (mncc->cause) {
770                 location = mncc->cause_location;
771                 cause = mncc->cause_value;
772                 add_trace("cause", "coding", "%d", mncc->cause_coding);
773                 add_trace("cause", "location", "%d", mncc->cause_location);
774                 add_trace("cause", "value", "%d", mncc->cause_value);
775         }
776         end_trace();
777
778         /* sending release to endpoint */
779         while(p_epointlist) {
780                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
781                 message->param.disconnectinfo.cause = cause;
782                 message->param.disconnectinfo.location = location;
783                 message_put(message);
784                 /* remove epoint */
785                 free_epointlist(p_epointlist);
786         }
787         new_state(PORT_STATE_RELEASE);
788         p_m_delete = 1;
789 }
790
791 /* NOTIFY INDICATION */
792 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
793 {
794         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
795         end_trace();
796 }
797
798
799 /* HOLD INDICATION */
800 void Pgsm::hold_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
801 {
802         struct lcr_msg *message;
803         struct gsm_mncc *resp, *frame;
804
805         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
806         end_trace();
807
808         /* notify the hold of call */
809         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
810         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
811         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
812         message_put(message);
813
814         /* acknowledge hold */
815         gsm_trace_header(p_m_mISDNport, this, MNCC_HOLD_CNF, DIRECTION_OUT);
816         end_trace();
817         resp = create_mncc(MNCC_HOLD_CNF, p_m_g_callref);
818         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
819
820         /* disable audio */
821         if (p_m_g_tch_connected) { /* it should be true */
822                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_DROP, DIRECTION_OUT);
823                 end_trace();
824                 frame = create_mncc(MNCC_FRAME_DROP, p_m_g_callref);
825                 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
826                 p_m_g_tch_connected = 0;
827         }
828 }
829
830
831 /* RETRIEVE INDICATION */
832 void Pgsm::retr_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
833 {
834         struct lcr_msg *message;
835         struct gsm_mncc *resp, *frame;
836
837         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
838         end_trace();
839
840         /* notify the retrieve of call */
841         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
842         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
843         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
844         message_put(message);
845
846         /* acknowledge retr */
847         gsm_trace_header(p_m_mISDNport, this, MNCC_RETRIEVE_CNF, DIRECTION_OUT);
848         end_trace();
849         resp = create_mncc(MNCC_RETRIEVE_CNF, p_m_g_callref);
850         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
851
852         /* enable audio */
853         if (!p_m_g_tch_connected) { /* it should be true */
854                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
855                 end_trace();
856                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
857                 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
858                 p_m_g_tch_connected = 1;
859         }
860 }
861
862 /*
863  * BSC sends message to port
864  */
865 static int message_bcs(struct gsm_network *net, int msg_type, void *arg)
866 {
867         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
868         unsigned int callref = mncc->callref;
869         class Port *port;
870         class Pgsm *pgsm = NULL;
871         char name[64];
872         struct mISDNport *mISDNport;
873
874         /* Special messages */
875         switch(msg_type) {
876         }
877
878         /* find callref */
879         callref = mncc->callref;
880         port = port_first;
881         while(port) {
882                 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_GSM) {
883                         pgsm = (class Pgsm *)port;
884                         if (pgsm->p_m_g_callref == callref) {
885                                 break;
886                         }
887                 }
888                 port = port->next;
889         }
890
891         if (msg_type == GSM_TRAU_FRAME) {
892                 if (port)
893                         pgsm->trau_receive((struct gsm_trau_frame *)arg);
894                 return 0;
895         }
896
897         if (!port) {
898                 if (msg_type != MNCC_SETUP_IND)
899                         return(0);
900                 /* find gsm port */
901                 mISDNport = mISDNport_first;
902                 while(mISDNport) {
903                         if (mISDNport->gsm)
904                                 break;
905                         mISDNport = mISDNport->next;
906                 }
907                 if (!mISDNport) {
908                         struct gsm_mncc *rej;
909
910                         rej = create_mncc(MNCC_REJ_REQ, callref);
911                         rej->cause = 1;
912                         rej->cause_coding = 3;
913                         rej->cause_location = 1;
914                         rej->cause_value = 27;
915                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
916                         add_trace("cause", "coding", "%d", rej->cause_coding);
917                         add_trace("cause", "location", "%d", rej->cause_location);
918                         add_trace("cause", "value", "%d", rej->cause_value);
919                         end_trace();
920                         send_and_free_mncc((struct gsm_network *)gsm->network, rej->msg_type, rej);
921                         return 0;
922                 }
923                 /* creating port object, transparent until setup with hdlc */
924                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
925                 if (!(pgsm = new Pgsm(PORT_TYPE_GSM_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
926
927                         FATAL("Cannot create Port instance.\n");
928         }
929
930         switch(msg_type) {
931                 case MNCC_SETUP_IND:
932                 pgsm->setup_ind(msg_type, callref, mncc);
933                 break;
934
935                 case MNCC_START_DTMF_IND:
936                 pgsm->start_dtmf_ind(msg_type, callref, mncc);
937                 break;
938
939                 case MNCC_STOP_DTMF_IND:
940                 pgsm->stop_dtmf_ind(msg_type, callref, mncc);
941                 break;
942
943                 case MNCC_CALL_CONF_IND:
944                 pgsm->call_conf_ind(msg_type, callref, mncc);
945                 break;
946
947                 case MNCC_ALERT_IND:
948                 pgsm->alert_ind(msg_type, callref, mncc);
949                 break;
950
951                 case MNCC_SETUP_CNF:
952                 pgsm->setup_cnf(msg_type, callref, mncc);
953                 break;
954
955                 case MNCC_SETUP_COMPL_IND:
956                 pgsm->setup_compl_ind(msg_type, callref, mncc);
957                 break;
958
959                 case MNCC_DISC_IND:
960                 pgsm->disc_ind(msg_type, callref, mncc);
961                 break;
962
963                 case MNCC_REL_IND:
964                 case MNCC_REL_CNF:
965                 case MNCC_REJ_IND:
966                 pgsm->rel_ind(msg_type, callref, mncc);
967                 break;
968
969                 case MNCC_NOTIFY_IND:
970                 pgsm->notify_ind(msg_type, callref, mncc);
971                 break;
972
973                 case MNCC_HOLD_IND:
974                 pgsm->hold_ind(msg_type, callref, mncc);
975                 break;
976
977                 case MNCC_RETRIEVE_IND:
978                 pgsm->retr_ind(msg_type, callref, mncc);
979                 break;
980
981                 default:
982                 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: 0x%x\n", pgsm->p_name, pgsm->p_callerinfo.id, msg_type);
983         }
984         return(0);
985 }
986
987 /* MESSAGE_SETUP */
988 void Pgsm::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
989 {
990         struct lcr_msg *message;
991         int ret;
992         struct epoint_list *epointlist;
993         struct gsm_mncc *mncc;
994         int channel;
995
996         /* copy setup infos to port */
997         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
998         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
999         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
1000         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
1001
1002         /* no number */
1003         if (!p_dialinginfo.id[0]) {
1004                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1005                 add_trace("failure", NULL, "No dialed subscriber given.");
1006                 end_trace();
1007                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1008                 message->param.disconnectinfo.cause = 28;
1009                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1010                 message_put(message);
1011                 new_state(PORT_STATE_RELEASE);
1012                 p_m_delete = 1;
1013                 return;
1014         }
1015         
1016         /* release if port is blocked */
1017         if (p_m_mISDNport->ifport->block) {
1018                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1019                 add_trace("failure", NULL, "Port blocked.");
1020                 end_trace();
1021                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1022                 message->param.disconnectinfo.cause = 27; // temp. unavail.
1023                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1024                 message_put(message);
1025                 new_state(PORT_STATE_RELEASE);
1026                 p_m_delete = 1;
1027                 return;
1028         }
1029
1030         /* hunt channel */
1031         ret = channel = hunt_bchannel();
1032         if (ret < 0)
1033                 goto no_channel;
1034         /* open channel */
1035         ret = seize_bchannel(channel, 1);
1036         if (ret < 0) {
1037                 no_channel:
1038                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1039                 add_trace("failure", NULL, "No internal audio channel available.");
1040                 end_trace();
1041                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1042                 message->param.disconnectinfo.cause = 34;
1043                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1044                 message_put(message);
1045                 new_state(PORT_STATE_RELEASE);
1046                 p_m_delete = 1;
1047                 return;
1048         }
1049         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1050         if (bchannel_open(p_m_b_index))
1051                 goto no_channel;
1052
1053 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1054         /* screen outgoing caller id */
1055         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1056
1057         /* attach only if not already */
1058         epointlist = p_epointlist;
1059         while(epointlist) {
1060                 if (epointlist->epoint_id == epoint_id)
1061                         break;
1062                 epointlist = epointlist->next;
1063         }
1064         if (!epointlist)
1065                 epointlist_new(epoint_id);
1066
1067         /* creating l3id */
1068         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1069         p_m_g_callref = new_callref++;
1070         add_trace("callref", "new", "0x%x", p_m_g_callref);
1071         end_trace();
1072
1073         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
1074         mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
1075         /* caller information */
1076         mncc->calling = 1;
1077         mncc->calling_plan = 1;
1078         switch (p_callerinfo.ntype) {
1079                 case INFO_NTYPE_UNKNOWN:
1080                 mncc->calling_type = 0x0;
1081                 break;
1082                 case INFO_NTYPE_INTERNATIONAL:
1083                 mncc->calling_type = 0x1;
1084                 break;
1085                 case INFO_NTYPE_NATIONAL:
1086                 mncc->calling_type = 0x2;
1087                 break;
1088                 case INFO_NTYPE_SUBSCRIBER:
1089                 mncc->calling_type = 0x4;
1090                 break;
1091                 default: /* INFO_NTYPE_NOTPRESENT */
1092                 mncc->calling = 0;
1093                 break;
1094         }
1095         switch (p_callerinfo.screen) {
1096                 case INFO_SCREEN_USER:
1097                 mncc->calling_screen = 0;
1098                 break;
1099                 default: /* INFO_SCREEN_NETWORK */
1100                 mncc->calling_screen = 3;
1101                 break;
1102         }
1103         switch (p_callerinfo.present) {
1104                 case INFO_PRESENT_ALLOWED:
1105                 mncc->calling_present = 0;
1106                 break;
1107                 case INFO_PRESENT_RESTRICTED:
1108                 mncc->calling_present = 1;
1109                 break;
1110                 default: /* INFO_PRESENT_NOTAVAIL */
1111                 mncc->calling_present = 2;
1112                 break;
1113         }
1114         if (mncc->calling) {
1115                 SCPY(mncc->calling_number, p_callerinfo.id);
1116                 add_trace("calling", "type", "%d", mncc->calling_type);
1117                 add_trace("calling", "plan", "%d", mncc->calling_plan);
1118                 add_trace("calling", "present", "%d", mncc->calling_present);
1119                 add_trace("calling", "screen", "%d", mncc->calling_screen);
1120                 add_trace("calling", "number", "%s", mncc->calling_number);
1121         }
1122         /* dialing information */
1123         mncc->called = 1;
1124         if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
1125                 SCPY(mncc->imsi, p_dialinginfo.id+5);
1126                 add_trace("dialing", "imsi", "%s", mncc->imsi);
1127         } else {
1128                 SCPY(mncc->called_number, p_dialinginfo.id);
1129                 add_trace("dialing", "number", "%s", mncc->called_number);
1130         }
1131         
1132         /* sending user-user */
1133
1134         /* redirecting number */
1135         mncc->redirecting = 1;
1136         mncc->redirecting_plan = 1;
1137         switch (p_redirinfo.ntype) {
1138                 case INFO_NTYPE_UNKNOWN:
1139                 mncc->redirecting_type = 0x0;
1140                 break;
1141                 case INFO_NTYPE_INTERNATIONAL:
1142                 mncc->redirecting_type = 0x1;
1143                 break;
1144                 case INFO_NTYPE_NATIONAL:
1145                 mncc->redirecting_type = 0x2;
1146                 break;
1147                 case INFO_NTYPE_SUBSCRIBER:
1148                 mncc->redirecting_type = 0x4;
1149                 break;
1150                 default: /* INFO_NTYPE_NOTPRESENT */
1151                 mncc->redirecting = 0;
1152                 break;
1153         }
1154         switch (p_redirinfo.screen) {
1155                 case INFO_SCREEN_USER:
1156                 mncc->redirecting_screen = 0;
1157                 break;
1158                 default: /* INFO_SCREE_NETWORK */
1159                 mncc->redirecting_screen = 3;
1160                 break;
1161         }
1162         switch (p_redirinfo.present) {
1163                 case INFO_PRESENT_ALLOWED:
1164                 mncc->redirecting_present = 0;
1165                 break;
1166                 case INFO_PRESENT_RESTRICTED:
1167                 mncc->redirecting_present = 1;
1168                 break;
1169                 default: /* INFO_PRESENT_NOTAVAIL */
1170                 mncc->redirecting_present = 2;
1171                 break;
1172         }
1173         /* sending redirecting number only in ntmode */
1174         if (mncc->redirecting) {
1175                 SCPY(mncc->redirecting_number, p_redirinfo.id);
1176                 add_trace("redir", "type", "%d", mncc->redirecting_type);
1177                 add_trace("redir", "plan", "%d", mncc->redirecting_plan);
1178                 add_trace("redir", "present", "%d", mncc->redirecting_present);
1179                 add_trace("redir", "screen", "%d", mncc->redirecting_screen);
1180                 add_trace("redir", "number", "%s", mncc->redirecting_number);
1181         }
1182         /* bearer capability */
1183         //todo
1184
1185         end_trace();
1186         send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1187
1188         new_state(PORT_STATE_OUT_SETUP);
1189
1190         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1191         message_put(message);
1192
1193         new_state(PORT_STATE_OUT_PROCEEDING);
1194 }
1195
1196 /* MESSAGE_NOTIFY */
1197 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1198 {
1199         struct gsm_mncc *mncc;
1200         int notify;
1201
1202         printf("if = %d\n", param->notifyinfo.notify);
1203         if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
1204                 notify = param->notifyinfo.notify & 0x7f;
1205                 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
1206                         /* queue notification */
1207                         if (p_m_g_notify_pending)
1208                                 message_free(p_m_g_notify_pending);
1209                         p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
1210                         memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
1211                 } else {
1212                         /* sending notification */
1213                         gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
1214                         add_trace("notify", NULL, "%d", notify);
1215                         end_trace();
1216                         mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
1217                         mncc->notify = notify;
1218                         send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1219                 }
1220         }
1221 }
1222
1223 /* MESSAGE_ALERTING */
1224 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
1225 {
1226         struct gsm_mncc *mncc;
1227
1228         /* send alert */
1229         gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
1230         mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
1231         if (p_m_mISDNport->tones) {
1232                 mncc->progress = 1;
1233                 mncc->progress_coding = 3; /* GSM */
1234                 mncc->progress_location = 1;
1235                 mncc->progress_descr = 8;
1236                 add_trace("progress", "coding", "%d", mncc->progress_coding);
1237                 add_trace("progress", "location", "%d", mncc->progress_location);
1238                 add_trace("progress", "descr", "%d", mncc->progress_descr);
1239         }
1240         end_trace();
1241         send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1242
1243         new_state(PORT_STATE_IN_ALERTING);
1244
1245         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1246                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1247                 end_trace();
1248                 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1249                 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1250                 p_m_g_tch_connected = 1;
1251         }
1252 }
1253
1254 /* MESSAGE_CONNECT */
1255 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1256 {
1257         struct gsm_mncc *mncc;
1258
1259         /* copy connected information */
1260         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
1261         /* screen outgoing caller id */
1262         do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
1263
1264         /* send connect */
1265         mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
1266         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
1267         /* caller information */
1268         mncc->connected = 1;
1269         mncc->connected_plan = 1;
1270         switch (p_callerinfo.ntype) {
1271                 case INFO_NTYPE_UNKNOWN:
1272                 mncc->connected_type = 0x0;
1273                 break;
1274                 case INFO_NTYPE_INTERNATIONAL:
1275                 mncc->connected_type = 0x1;
1276                 break;
1277                 case INFO_NTYPE_NATIONAL:
1278                 mncc->connected_type = 0x2;
1279                 break;
1280                 case INFO_NTYPE_SUBSCRIBER:
1281                 mncc->connected_type = 0x4;
1282                 break;
1283                 default: /* INFO_NTYPE_NOTPRESENT */
1284                 mncc->connected = 0;
1285                 break;
1286         }
1287         switch (p_callerinfo.screen) {
1288                 case INFO_SCREEN_USER:
1289                 mncc->connected_screen = 0;
1290                 break;
1291                 default: /* INFO_SCREEN_NETWORK */
1292                 mncc->connected_screen = 3;
1293                 break;
1294         }
1295         switch (p_callerinfo.present) {
1296                 case INFO_PRESENT_ALLOWED:
1297                 mncc->connected_present = 0;
1298                 break;
1299                 case INFO_PRESENT_RESTRICTED:
1300                 mncc->connected_present = 1;
1301                 break;
1302                 default: /* INFO_PRESENT_NOTAVAIL */
1303                 mncc->connected_present = 2;
1304                 break;
1305         }
1306         if (mncc->connected) {
1307                 SCPY(mncc->connected_number, p_connectinfo.id);
1308                 add_trace("connected", "type", "%d", mncc->connected_type);
1309                 add_trace("connected", "plan", "%d", mncc->connected_plan);
1310                 add_trace("connected", "present", "%d", mncc->connected_present);
1311                 add_trace("connected", "screen", "%d", mncc->connected_screen);
1312                 add_trace("connected", "number", "%s", mncc->connected_number);
1313         }
1314         end_trace();
1315         send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1316
1317         new_state(PORT_STATE_CONNECT_WAITING);
1318 }
1319
1320 /* MESSAGE_DISCONNECT */
1321 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
1322 {
1323         struct gsm_mncc *mncc;
1324
1325         /* send disconnect */
1326         mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
1327         gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
1328         if (p_m_mISDNport->tones) {
1329                 mncc->progress = 1;
1330                 mncc->progress_coding = 3; /* GSM */
1331                 mncc->progress_location = 1;
1332                 mncc->progress_descr = 8;
1333                 add_trace("progress", "coding", "%d", mncc->progress_coding);
1334                 add_trace("progress", "location", "%d", mncc->progress_location);
1335                 add_trace("progress", "descr", "%d", mncc->progress_descr);
1336         }
1337         mncc->cause = 1;
1338         mncc->cause_coding = 3;
1339         mncc->cause_location = param->disconnectinfo.location;
1340         mncc->cause_value = param->disconnectinfo.cause;
1341         add_trace("cause", "coding", "%d", mncc->cause_coding);
1342         add_trace("cause", "location", "%d", mncc->cause_location);
1343         add_trace("cause", "value", "%d", mncc->cause_value);
1344         end_trace();
1345         send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1346
1347         new_state(PORT_STATE_OUT_DISCONNECT);
1348
1349         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
1350                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
1351                 end_trace();
1352                 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
1353                 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1354                 p_m_g_tch_connected = 1;
1355         }
1356 }
1357
1358
1359 /* MESSAGE_RELEASE */
1360 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1361 {
1362         struct gsm_mncc *mncc;
1363
1364         /* send release */
1365         mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
1366         gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
1367         mncc->cause = 1;
1368         mncc->cause_coding = 3;
1369         mncc->cause_location = param->disconnectinfo.location;
1370         mncc->cause_value = param->disconnectinfo.cause;
1371         add_trace("cause", "coding", "%d", mncc->cause_coding);
1372         add_trace("cause", "location", "%d", mncc->cause_location);
1373         add_trace("cause", "value", "%d", mncc->cause_value);
1374         end_trace();
1375         send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
1376
1377         new_state(PORT_STATE_RELEASE);
1378         p_m_delete = 1;
1379         return;
1380 }
1381
1382
1383 /*
1384  * endpoint sends messages to the port
1385  */
1386 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1387 {
1388         if (PmISDN::message_epoint(epoint_id, message_id, param))
1389                 return(1);
1390
1391         switch(message_id) {
1392                 case MESSAGE_SETUP: /* dial-out command received from epoint */
1393                 if (p_state!=PORT_STATE_IDLE)
1394                         break;
1395                 message_setup(epoint_id, message_id, param);
1396                 break;
1397
1398                 case MESSAGE_NOTIFY: /* display and notifications */
1399                 message_notify(epoint_id, message_id, param);
1400                 break;
1401
1402 //              case MESSAGE_FACILITY: /* facility message */
1403 //              message_facility(epoint_id, message_id, param);
1404 //              break;
1405
1406                 case MESSAGE_PROCEEDING: /* message not handles */
1407                 break;
1408
1409                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
1410                 if (p_state!=PORT_STATE_IN_PROCEEDING)
1411                         break;
1412                 message_alerting(epoint_id, message_id, param);
1413                 if (p_m_g_notify_pending) {
1414                         /* send pending notify message during connect */
1415                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1416                         message_free(p_m_g_notify_pending);
1417                         p_m_g_notify_pending = NULL;
1418                 }
1419                 break;
1420
1421                 case MESSAGE_CONNECT: /* call of endpoint is connected */
1422                 if (p_state!=PORT_STATE_IN_PROCEEDING
1423                  && p_state!=PORT_STATE_IN_ALERTING)
1424                         break;
1425                 message_connect(epoint_id, message_id, param);
1426                 if (p_m_g_notify_pending) {
1427                         /* send pending notify message during connect */
1428                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
1429                         message_free(p_m_g_notify_pending);
1430                         p_m_g_notify_pending = NULL;
1431                 }
1432                 break;
1433
1434                 case MESSAGE_DISCONNECT: /* call has been disconnected */
1435                 if (p_state!=PORT_STATE_IN_PROCEEDING
1436                  && p_state!=PORT_STATE_IN_ALERTING
1437                  && p_state!=PORT_STATE_OUT_SETUP
1438                  && p_state!=PORT_STATE_OUT_OVERLAP
1439                  && p_state!=PORT_STATE_OUT_PROCEEDING
1440                  && p_state!=PORT_STATE_OUT_ALERTING
1441                  && p_state!=PORT_STATE_CONNECT
1442                  && p_state!=PORT_STATE_CONNECT_WAITING)
1443                         break;
1444                 message_disconnect(epoint_id, message_id, param);
1445                 break;
1446
1447                 case MESSAGE_RELEASE: /* release isdn port */
1448                 if (p_state==PORT_STATE_RELEASE)
1449                         break;
1450                 message_release(epoint_id, message_id, param);
1451                 break;
1452
1453                 default:
1454                 PDEBUG(DEBUG_GSM, "Pgsm(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
1455         }
1456
1457         return(1);
1458 }
1459
1460
1461 /*
1462  * handler
1463  */
1464 int Pgsm::handler(void)
1465 {
1466         int ret;
1467         int work = 0;
1468         unsigned char buffer[2048+MISDN_HEADER_LEN];
1469         struct mISDNhead *hh = (struct mISDNhead *)buffer;
1470
1471         if ((ret = PmISDN::handler()))
1472                 return(ret);
1473
1474         /* handle destruction */
1475         if (p_m_delete) {
1476                 delete this;
1477                 return(-1);
1478         }
1479
1480         /* handle message from bchannel */
1481         if (p_m_g_gsm_b_sock > -1) {
1482                 ret = recv(p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1483                 if (ret >= (int)MISDN_HEADER_LEN) {
1484                         switch(hh->prim) {
1485                                 /* we don't care about confirms, we use rx data to sync tx */
1486                                 case PH_DATA_CNF:
1487                                 break;
1488                                 /* we receive audio data, we respond to it AND we send tones */
1489                                 case PH_DATA_IND:
1490                                 bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1491                                 break;
1492                                 case PH_ACTIVATE_IND:
1493                                 p_m_g_gsm_b_active = 1;
1494                                 break;
1495                                 case PH_DEACTIVATE_IND:
1496                                 p_m_g_gsm_b_active = 0;
1497                                 break;
1498                         }
1499                         work = 1;
1500                 } else {
1501                         if (ret < 0 && errno != EWOULDBLOCK)
1502                                 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
1503                 }
1504         }
1505
1506         return(work);
1507 }
1508
1509
1510 /*
1511  * handles bsc select function within LCR's main loop
1512  */
1513 int handle_gsm(void)
1514 {
1515         int ret1, ret2;
1516
1517         ret1 = bsc_upqueue((struct gsm_network *)gsm->network);
1518         ret2 = bsc_select_main(1); /* polling */
1519         if (ret1 || ret2)
1520                 return 1;
1521         return 0;
1522 }
1523
1524 static void gsm_sock_close(void)
1525 {
1526         if (gsm->gsm_sock > -1)
1527                 close(gsm->gsm_sock);
1528         gsm->gsm_sock = -1;
1529 }
1530
1531 static int gsm_sock_open(char *portname)
1532 {
1533         int ret;
1534         int cnt;
1535         unsigned long on = 1;
1536         struct sockaddr_mISDN addr;
1537         struct mISDN_devinfo devinfo;
1538         int pri, bri;
1539
1540         /* check port counts */
1541         ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1542         if (ret < 0) {
1543                 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1544                 return(ret);
1545         }
1546
1547         if (cnt <= 0) {
1548                 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1549                 return -EIO;
1550         }
1551         gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1552         if (gsm->gsm_port < 0) {
1553                 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1554                 return gsm->gsm_port;
1555         }
1556         /* get protocol */
1557         bri = pri = 0;
1558         devinfo.id = gsm->gsm_port;
1559         ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1560         if (ret < 0) {
1561                 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1562                 return ret;
1563         }
1564         if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1565                 bri = 1;
1566         }
1567         if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1568                 pri = 1;
1569         }
1570         if (!pri && !pri) {
1571                 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1572         }
1573         /* open socket */
1574         if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_TE_S0)) < 0) {
1575                 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1576                 gsm_sock_close();
1577                 return gsm->gsm_sock;
1578         }
1579         /* set nonblocking io */
1580         if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1581                 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1582                 gsm_sock_close();
1583                 return ret;
1584         }
1585         /* bind socket to dchannel */
1586         memset(&addr, 0, sizeof(addr));
1587         addr.family = AF_ISDN;
1588         addr.dev = gsm->gsm_port;
1589         addr.channel = 0;
1590         if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1591                 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1592                 gsm_sock_close();
1593                 return (ret);
1594         }
1595
1596         return 0;
1597 }
1598
1599
1600 int gsm_exit(int rc)
1601 {
1602         /* free gsm instance */
1603         if (gsm) {
1604                 if (gsm->gsm_sock > -1)
1605                         gsm_sock_close();
1606                 /* shutdown network */
1607                 if (gsm->network)
1608                         shutdown_net((struct gsm_network *)gsm->network);
1609                 /* free network */
1610                 if (gsm->network) {
1611                         free((struct gsm_network *)gsm->network); /* TBD */
1612                 }
1613                 free(gsm);
1614                 gsm = NULL;
1615         }
1616
1617         return(rc);
1618 }
1619
1620 int gsm_init(void)
1621 {
1622         char hlr[128], filename[128];
1623         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1624         int pcapfd;
1625
1626         /* create gsm instance */
1627         gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1628         gsm->gsm_sock = -1;
1629
1630         /* parse options */
1631         if (!gsm_conf(&gsm->conf)) {
1632                 PERROR("%s", gsm_conf_error);
1633                 return gsm_exit(-EINVAL);
1634         }
1635
1636         /* set debug */
1637         debug_parse_category_mask(gsm->conf.debug);
1638
1639         /* init database */
1640         if (gsm->conf.hlr[0] == '/')
1641                 SCPY(hlr, gsm->conf.hlr);
1642         else
1643                 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1644
1645         // TODO multiple base stations
1646         if (gsm->conf.numbts < 1 || gsm->conf.numbts > 1) {
1647                 PERROR("Expecting exactly one BTS. You defined %d.\n", gsm->conf.numbts);
1648                 return gsm_exit(-1);
1649         }
1650
1651         /* bootstrap network */
1652         gsm->network = bootstrap_network(&message_bcs, gsm->conf.bts[0].type, gsm->conf.mcc, gsm->conf.mnc, gsm->conf.lac, gsm->conf.bts[0].frequency[0], gsm->conf.bts[0].card, !gsm->conf.keep_l2, gsm->conf.short_name, gsm->conf.long_name, hlr, gsm->conf.allow_all);
1653         if (!gsm->network) {
1654                 PERROR("Failed to bootstrap GSM network.\n");
1655                 return gsm_exit(-1);
1656         }
1657
1658         /* open gsm loop interface */
1659         if (gsm_sock_open(gsm->conf.interface_bsc)) {
1660                 return gsm_exit(-1);
1661         }
1662
1663         /* open pcap file */
1664         if (gsm->conf.pcapfile[0]) {
1665                 if (gsm->conf.pcapfile[0] == '/')
1666                         SCPY(filename, gsm->conf.pcapfile);
1667                 else
1668                         SPRINT(filename, "%s/%s", CONFIG_DATA, gsm->conf.pcapfile);
1669                 pcapfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode);
1670                 if (pcapfd < 0) {
1671                         PERROR("Failed to open file for pcap\n");
1672                         return gsm_exit(-1);
1673                 }
1674                 e1_set_pcap_fd(pcapfd);
1675         }
1676
1677         return 0;
1678 }
1679