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