Replaced polling loop for LCR and chan_lcr with select based event loop.
[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
14 #ifndef _GNU_SOURCE
15 #define _GNU_SOURCE
16 #endif
17 extern "C" {
18 #include <getopt.h>
19
20 #include <openbsc/db.h>
21 #include <openbsc/select.h>
22 #include <openbsc/debug.h>
23 #include <openbsc/e1_input.h>
24 #include <openbsc/talloc.h>
25 #include <openbsc/mncc.h>
26 #include <openbsc/trau_frame.h>
27 struct gsm_network *bsc_gsmnet = 0;
28 extern int ipacc_rtp_direct;
29 extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *),
30                                  const char *cfg_file);
31 extern int bsc_shutdown_net(struct gsm_network *net);
32 void talloc_ctx_init(void);
33 void on_dso_load_token(void);
34 void on_dso_load_rrlp(void);
35 void on_dso_load_ho_dec(void);
36 int bts_model_unknown_init(void);
37 int bts_model_bs11_init(void);
38 int bts_model_nanobts_init(void);
39 static struct debug_target *stderr_target;
40
41 /* timer to store statistics */
42 #define DB_SYNC_INTERVAL        60, 0
43 static struct timer_list db_sync_timer;
44
45 #include "gsm_audio.h"
46
47 #undef AF_ISDN
48 #undef PF_ISDN
49 extern  int     AF_ISDN;
50 #define PF_ISDN AF_ISDN
51 }
52
53
54 struct lcr_gsm *gsm = NULL;
55
56 static unsigned int new_callref = 1;
57
58 /* timer handling */
59 static int _db_store_counter(struct counter *counter, void *data)
60 {
61         return db_store_counter(counter);
62 }
63
64 static void db_sync_timer_cb(void *data)
65 {
66         /* store counters to database and re-schedule */
67         counters_for_each(_db_store_counter, NULL);
68         bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL);
69 }
70
71 /*
72  * create and send mncc message
73  */
74 static struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
75 {
76         struct gsm_mncc *mncc;
77
78         mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
79         mncc->msg_type = msg_type;
80         mncc->callref = callref;
81         return (mncc);
82 }
83 static int send_and_free_mncc(struct gsm_network *net, unsigned int msg_type, void *data)
84 {
85         int ret;
86
87         ret = mncc_send(net, msg_type, data);
88         free(data);
89
90         return ret;
91 }
92
93 static int delete_event(struct lcr_work *work, void *instance, int index);
94
95 /*
96  * constructor
97  */
98 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)
99 {
100         p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
101         memset(&p_m_g_delete, 0, sizeof(p_m_g_delete));
102         add_work(&p_m_g_delete, delete_event, this, 0);
103         p_m_g_callref = 0;
104         p_m_g_mode = 0;
105         p_m_g_gsm_b_sock = -1;
106         p_m_g_gsm_b_index = -1;
107         p_m_g_gsm_b_active = 0;
108         p_m_g_notify_pending = NULL;
109         p_m_g_decoder = gsm_audio_create();
110         p_m_g_encoder = gsm_audio_create();
111         if (!p_m_g_encoder || !p_m_g_decoder) {
112                 PERROR("Failed to create GSM audio codec instance\n");
113                 trigger_work(&p_m_g_delete);
114         }
115         p_m_g_rxpos = 0;
116         p_m_g_tch_connected = 0;
117
118         PDEBUG(DEBUG_GSM, "Created new mISDNPort(%s).\n", portname);
119 }
120
121 /*
122  * destructor
123  */
124 Pgsm::~Pgsm()
125 {
126         PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
127
128         del_work(&p_m_g_delete);
129
130         /* remove queued message */
131         if (p_m_g_notify_pending)
132                 message_free(p_m_g_notify_pending);
133
134         /* close audio transfer socket */
135         if (p_m_g_gsm_b_sock > -1)
136                 bchannel_close();
137
138         /* close codec */
139         if (p_m_g_encoder)
140                 gsm_audio_destroy(p_m_g_encoder);
141         if (p_m_g_decoder)
142                 gsm_audio_destroy(p_m_g_decoder);
143 }
144
145
146 /* close bsc side bchannel */
147 void Pgsm::bchannel_close(void)
148 {
149         if (p_m_g_gsm_b_sock > -1) {
150                 unregister_fd(&p_m_g_gsm_b_fd);
151                 close(p_m_g_gsm_b_sock);
152         }
153         p_m_g_gsm_b_sock = -1;
154         p_m_g_gsm_b_index = -1;
155         p_m_g_gsm_b_active = 0;
156 }
157
158 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index);
159
160 /* open bsc side bchannel */
161 int Pgsm::bchannel_open(int index)
162 {
163         int ret;
164         struct sockaddr_mISDN addr;
165         struct mISDNhead act;
166
167         if (p_m_g_gsm_b_sock > -1) {
168                 PERROR("Socket already created for index %d\n", index);
169                 return(-EIO);
170         }
171
172         /* open socket */
173         ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
174         if (ret < 0) {
175                 PERROR("Failed to open bchannel-socket for index %d\n", index);
176                 bchannel_close();
177                 return(ret);
178         }
179         memset(&p_m_g_gsm_b_fd, 0, sizeof(p_m_g_gsm_b_fd.fd));
180         p_m_g_gsm_b_fd.fd = p_m_g_gsm_b_sock;
181         register_fd(&p_m_g_gsm_b_fd, LCR_FD_READ, b_handler, this, 0);
182
183
184         /* bind socket to bchannel */
185         addr.family = AF_ISDN;
186         addr.dev = gsm->gsm_port;
187         addr.channel = index+1+(index>15);
188         ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
189         if (ret < 0) {
190                 PERROR("Failed to bind bchannel-socket for index %d\n", index);
191                 bchannel_close();
192                 return(ret);
193         }
194         /* activate bchannel */
195         PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
196         act.prim = PH_ACTIVATE_REQ; 
197         act.id = 0;
198         ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
199         if (ret < 0) {
200                 PERROR("Failed to activate index %d\n", index);
201                 bchannel_close();
202                 return(ret);
203         }
204
205         p_m_g_gsm_b_index = index;
206
207         return(0);
208 }
209
210 /* receive from bchannel */
211 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
212 {
213         unsigned char frame[33];
214
215         /* encoder init failed */
216         if (!p_m_g_encoder)
217                 return;
218
219         /* (currently) not connected, so don't flood tch! */
220         if (!p_m_g_tch_connected)
221                 return;
222
223         /* write to rx buffer */
224         while(len--) {
225                 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
226                 if (p_m_g_rxpos == 160) {
227                         p_m_g_rxpos = 0;
228
229                         /* encode data */
230                         gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
231                         frame_send(frame);
232                 }
233         }
234 }
235
236 /* transmit to bchannel */
237 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
238 {
239         unsigned char buf[MISDN_HEADER_LEN+len];
240         struct mISDNhead *hh = (struct mISDNhead *)buf;
241         int ret;
242
243         if (!p_m_g_gsm_b_active)
244                 return;
245
246         /* make and send frame */
247         hh->prim = PH_DATA_REQ;
248         hh->id = 0;
249         memcpy(buf+MISDN_HEADER_LEN, data, len);
250         ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
251         if (ret <= 0)
252                 PERROR("Failed to send to socket index %d\n", index);
253 }
254
255 void Pgsm::frame_send(void *_frame)
256 {
257         unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
258         struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
259         
260         frame->msg_type = GSM_TCHF_FRAME;
261         frame->callref = p_m_g_callref;
262         memcpy(frame->data, _frame, 33);
263         mncc_send((struct gsm_network *)gsm->network, frame->msg_type, frame);
264 }
265
266
267 void Pgsm::frame_receive(void *_frame)
268 {
269         struct gsm_data_frame *frame = (struct gsm_data_frame *)_frame;
270         signed short samples[160];
271         unsigned char data[160];
272         int i;
273
274         if (!p_m_g_decoder)
275                 return;
276
277         if ((frame->data[0]>>4) != 0xd)
278                 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
279         
280         /* decode */
281         gsm_audio_decode(p_m_g_decoder, frame->data, samples);
282         for (i = 0; i < 160; i++) {
283                 data[i] = audio_s16_to_law[samples[i] & 0xffff];
284         }
285
286         /* send */
287         bchannel_send(PH_DATA_REQ, 0, data, 160);
288 }
289
290
291 /*
292  * create trace
293  **/
294 static void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
295 {
296         char msgtext[64];
297
298         /* select message and primitive text */
299         SCPY(msgtext, get_mncc_name(msg_type));
300
301         /* add direction */
302         if (direction == DIRECTION_OUT)
303                 SCAT(msgtext, " MSC->BSC");
304         else
305                 SCAT(msgtext, " MSC<-BSC");
306
307         /* init trace with given values */
308         start_trace(mISDNport?mISDNport->portnum:-1,
309                     mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
310                     port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
311                     port?port->p_dialinginfo.id:NULL,
312                     direction,
313                     CATEGORY_CH,
314                     port?port->p_serial:0,
315                     msgtext);
316 }
317
318
319
320 /* select bchannel */
321 int Pgsm::hunt_bchannel(void)
322 {
323         int channel;
324         int i;
325
326         chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
327         add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
328         if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
329                 add_trace("conclusion", NULL, "all channels are reserved");
330                 end_trace();
331                 return(-34); // no channel
332         }
333         /* find channel */
334         i = 0;
335         channel = 0;
336         while(i < p_m_mISDNport->b_num) {
337                 if (p_m_mISDNport->b_port[i] == NULL) {
338                         channel = i+1+(i>=15);
339                         break;
340                 }
341                 i++;
342         }
343         if (!channel) {
344                 add_trace("conclusion", NULL, "no channel available");
345                 end_trace();
346                 return(-6); // channel unacceptable
347         }
348         add_trace("conclusion", NULL, "channel available");
349         add_trace("connect", "channel", "%d", channel);
350         end_trace();
351         return(channel);
352 }
353
354
355 /*
356  * handles all indications
357  */
358 /* SETUP INDICATION */
359 void Pgsm::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
360 {
361         int ret;
362         class Endpoint *epoint;
363         struct lcr_msg *message;
364         int channel;
365         struct gsm_mncc *mode, *proceeding, *frame;
366
367         /* emergency shutdown */
368         printf("%d %d\n", mncc->emergency, !gsm->conf.noemergshut);
369         if (mncc->emergency && !gsm->conf.noemergshut) {
370                 start_trace(p_m_mISDNport->portnum,
371                         p_m_mISDNport->ifport->interface,
372                         NULL,
373                         NULL,
374                         DIRECTION_NONE,
375                         CATEGORY_CH,
376                         0,
377                         "EMERGENCY SHUTDOWN (due to received emergency call)");
378                 end_trace();
379                 quit = 1;
380         }
381         /* process given callref */
382         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
383         add_trace("callref", "new", "0x%x", callref);
384         if (p_m_g_callref) {
385                 /* release in case the ID is already in use */
386                 add_trace("error", NULL, "callref already in use");
387                 end_trace();
388                 mncc = create_mncc(MNCC_REJ_REQ, callref);
389                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
390                 mncc->fields |= MNCC_F_CAUSE;
391                 mncc->cause.coding = 3;
392                 mncc->cause.location = 1;
393                 mncc->cause.value = 47;
394                 add_trace("cause", "coding", "%d", mncc->cause.coding);
395                 add_trace("cause", "location", "%d", mncc->cause.location);
396                 add_trace("cause", "value", "%d", mncc->cause.value);
397                 add_trace("reason", NULL, "callref already in use");
398                 end_trace();
399                 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
400                 new_state(PORT_STATE_RELEASE);
401                 trigger_work(&p_m_g_delete);
402                 return;
403         }
404         p_m_g_callref = callref;
405         end_trace();
406
407         /* if blocked, release call with MT_RELEASE_COMPLETE */
408         if (p_m_mISDNport->ifport->block) {
409                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
410                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
411                 mncc->fields |= MNCC_F_CAUSE;
412                 mncc->cause.coding = 3;
413                 mncc->cause.location = 1;
414                 mncc->cause.value = 27;
415                 add_trace("cause", "coding", "%d", mncc->cause.coding);
416                 add_trace("cause", "location", "%d", mncc->cause.location);
417                 add_trace("cause", "value", "%d", mncc->cause.value);
418                 add_trace("reason", NULL, "port is blocked");
419                 end_trace();
420                 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
421                 new_state(PORT_STATE_RELEASE);
422                 trigger_work(&p_m_g_delete);
423                 return;
424         }
425
426         /* caller info */
427         if (mncc->clir.inv)
428                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
429         else
430                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
431         if (mncc->calling.number[0])
432                 SCPY(p_callerinfo.id, mncc->calling.number);
433         else
434                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
435         SCPY(p_callerinfo.imsi, mncc->imsi);
436         p_callerinfo.screen = INFO_SCREEN_NETWORK;
437         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
438         p_callerinfo.isdn_port = p_m_portnum;
439         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
440
441         /* dialing information */
442         SCAT(p_dialinginfo.id, mncc->called.number);
443         switch (mncc->called.type) {
444                 case 0x1:
445                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
446                 break;
447                 case 0x2:
448                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
449                 break;
450                 case 0x4:
451                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
452                 break;
453                 default:
454                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
455                 break;
456         }
457         if (mncc->emergency) {
458                 SCPY(p_dialinginfo.id, "emergency");
459         }
460         p_dialinginfo.sending_complete = 1;
461
462         /* bearer capability */
463         // todo
464         p_capainfo.bearer_capa = INFO_BC_SPEECH;
465         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
466         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
467         p_capainfo.source_mode = B_MODE_TRANSPARENT;
468         p_m_g_mode = p_capainfo.source_mode;
469
470         /* useruser */
471
472         /* hunt channel */
473         ret = channel = hunt_bchannel();
474         if (ret < 0)
475                 goto no_channel;
476
477         /* open channel */
478         ret = seize_bchannel(channel, 1);
479         if (ret < 0) {
480                 no_channel:
481                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
482                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
483                 mncc->fields |= MNCC_F_CAUSE;
484                 mncc->cause.coding = 3;
485                 mncc->cause.location = 1;
486                 mncc->cause.value = 34;
487                 add_trace("cause", "coding", "%d", mncc->cause.coding);
488                 add_trace("cause", "location", "%d", mncc->cause.location);
489                 add_trace("cause", "value", "%d", mncc->cause.value);
490                 add_trace("reason", NULL, "no channel");
491                 end_trace();
492                 send_and_free_mncc((struct gsm_network *)gsm->network, mncc->msg_type, mncc);
493                 new_state(PORT_STATE_RELEASE);
494                 trigger_work(&p_m_g_delete);
495                 return;
496         }
497         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
498         if (bchannel_open(p_m_b_index))
499                 goto no_channel;
500
501         /* what infos did we got ... */
502         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
503         if (p_callerinfo.id[0])
504                 add_trace("calling", "number", "%s", p_callerinfo.id);
505         else
506                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
507         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
508         add_trace("dialing", "number", "%s", p_dialinginfo.id);
509         end_trace();
510
511         /* create endpoint */
512         if (p_epointlist)
513                 FATAL("Incoming call but already got an endpoint.\n");
514         if (!(epoint = new Endpoint(p_serial, 0)))
515                 FATAL("No memory for Endpoint instance\n");
516         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
517                 FATAL("No memory for Endpoint Application instance\n");
518         epointlist_new(epoint->ep_serial);
519
520         /* modify lchan to GSM codec V1 */
521         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
522         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
523         mode->lchan_mode = 0x01; /* GSM V1 */
524         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
525         end_trace();
526         send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
527
528         /* send call proceeding */
529         gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
530         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
531         if (p_m_mISDNport->tones) {
532                 proceeding->fields |= MNCC_F_PROGRESS;
533                 proceeding->progress.coding = 3; /* GSM */
534                 proceeding->progress.location = 1;
535                 proceeding->progress.descr = 8;
536                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
537                 add_trace("progress", "location", "%d", proceeding->progress.location);
538                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
539         }
540         end_trace();
541         send_and_free_mncc((struct gsm_network *)gsm->network, proceeding->msg_type, proceeding);
542
543         new_state(PORT_STATE_IN_PROCEEDING);
544
545         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
546                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
547                 end_trace();
548                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
549                 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
550                 p_m_g_tch_connected = 1;
551         }
552
553         /* send setup message to endpoit */
554         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
555         message->param.setup.isdn_port = p_m_portnum;
556         message->param.setup.port_type = p_type;
557 //      message->param.setup.dtmf = 0;
558         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
559         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
560         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
561         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
562         message->param.setup.useruser.len = strlen(mncc->useruser.info);
563         message->param.setup.useruser.protocol = mncc->useruser.proto;
564         message_put(message);
565 }
566
567 /* DTMF INDICATION */
568 void Pgsm::start_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
569 {
570         struct lcr_msg *message;
571         struct gsm_mncc *resp;
572
573         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
574         add_trace("keypad", NULL, "%c", mncc->keypad);
575         end_trace();
576         SPRINT(p_dialinginfo.id, "%c", mncc->keypad);
577         p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
578
579         /* send resp */
580         gsm_trace_header(p_m_mISDNport, this, MNCC_START_DTMF_RSP, DIRECTION_OUT);
581         add_trace("keypad", NULL, "%c", mncc->keypad);
582         end_trace();
583         resp = create_mncc(MNCC_START_DTMF_RSP, p_m_g_callref);
584         resp->keypad = mncc->keypad;
585         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
586
587         /* send dialing information */
588         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
589         memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
590         message_put(message);
591 }
592 void Pgsm::stop_dtmf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
593 {
594         struct gsm_mncc *resp;
595
596         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
597         add_trace("keypad", NULL, "%c", mncc->keypad);
598         end_trace();
599
600         /* send resp */
601         gsm_trace_header(p_m_mISDNport, this, MNCC_STOP_DTMF_RSP, DIRECTION_OUT);
602         add_trace("keypad", NULL, "%c", mncc->keypad);
603         end_trace();
604         resp = create_mncc(MNCC_STOP_DTMF_RSP, p_m_g_callref);
605         resp->keypad = mncc->keypad;
606         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
607 }
608
609 /* PROCEEDING INDICATION */
610 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
611 {
612         struct gsm_mncc *mode;
613
614         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
615         if (mncc->fields & MNCC_F_CAUSE) {
616                 add_trace("cause", "coding", "%d", mncc->cause.coding);
617                 add_trace("cause", "location", "%", mncc->cause.location);
618                 add_trace("cause", "value", "%", mncc->cause.value);
619         }
620         end_trace();
621
622         /* modify lchan to GSM codec V1 */
623         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
624         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
625         mode->lchan_mode = 0x01; /* GSM V1 */
626         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
627         end_trace();
628         send_and_free_mncc((struct gsm_network *)gsm->network, mode->msg_type, mode);
629
630 }
631
632 /* ALERTING INDICATION */
633 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
634 {
635         struct lcr_msg *message;
636
637         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
638         end_trace();
639
640         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
641         message_put(message);
642
643         new_state(PORT_STATE_OUT_ALERTING);
644
645 }
646
647 /* CONNECT INDICATION */
648 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
649 {
650         struct gsm_mncc *resp, *frame;
651         struct lcr_msg *message;
652
653         SCPY(p_connectinfo.id, mncc->connected.number);
654         SCPY(p_connectinfo.imsi, mncc->imsi);
655         p_connectinfo.present = INFO_PRESENT_ALLOWED;
656         p_connectinfo.screen = INFO_SCREEN_NETWORK;
657         p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
658         p_connectinfo.isdn_port = p_m_portnum;
659         SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
660
661         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
662         if (p_connectinfo.id[0])
663                 add_trace("connect", "number", "%s", p_connectinfo.id);
664         else if (mncc->imsi[0])
665                 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
666         if (mncc->imsi[0])
667                 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
668         end_trace();
669
670         /* send resp */
671         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
672         resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
673         end_trace();
674         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
675
676         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
677         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
678         message_put(message);
679
680         new_state(PORT_STATE_CONNECT);
681
682         if (!p_m_g_tch_connected) { /* only if ... */
683                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
684                 end_trace();
685                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
686                 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
687                 p_m_g_tch_connected = 1;
688         }
689 }
690
691 /* CONNECT ACK INDICATION */
692 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
693 {
694         struct gsm_mncc *frame;
695
696         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
697         end_trace();
698
699         new_state(PORT_STATE_CONNECT);
700
701         if (!p_m_g_tch_connected) { /* only if ... */
702                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
703                 end_trace();
704                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
705                 send_and_free_mncc((struct gsm_network *)gsm->network, frame->msg_type, frame);
706                 p_m_g_tch_connected = 1;
707         }
708 }
709
710 /* DISCONNECT INDICATION */
711 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
712 {
713         struct lcr_msg *message;
714         int cause = 16, location = 0;
715         struct gsm_mncc *resp;
716
717         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
718         if (mncc->fields & MNCC_F_CAUSE) {
719                 location = mncc->cause.location;
720                 cause = mncc->cause.value;
721                 add_trace("cause", "coding", "%d", mncc->cause.coding);
722                 add_trace("cause", "location", "%d", location);
723                 add_trace("cause", "value", "%d", cause);
724         }
725         end_trace();
726
727         /* send release */
728         resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
729         gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
730 #if 0
731         resp->fields |= MNCC_F_CAUSE;
732         resp->cause.coding = 3;
733         resp->cause.location = 1;
734         resp->cause.value = cause;
735         add_trace("cause", "coding", "%d", resp->cause.coding);
736         add_trace("cause", "location", "%d", resp->cause.location);
737         add_trace("cause", "value", "%d", resp->cause.value);
738 #endif
739         end_trace();
740         send_and_free_mncc((struct gsm_network *)gsm->network, resp->msg_type, resp);
741
742         /* sending release to endpoint */
743         while(p_epointlist) {
744                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
745                 message->param.disconnectinfo.cause = cause;
746                 message->param.disconnectinfo.location = location;
747                 message_put(message);
748                 /* remove epoint */
749                 free_epointlist(p_epointlist);
750         }
751         new_state(PORT_STATE_RELEASE);
752         trigger_work(&p_m_g_delete);
753 }
754
755 /* CC_RELEASE INDICATION */
756 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
757 {
758         int location = 0, cause = 16;
759         struct lcr_msg *message;
760
761         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
762         if (mncc->fields & MNCC_F_CAUSE) {
763                 location = mncc->cause.location;
764                 cause = mncc->cause.value;
765                 add_trace("cause", "coding", "%d", mncc->cause.coding);
766                 add_trace("cause", "location", "%d", mncc->cause.location);
767                 add_trace("cause", "value", "%d", mncc->cause.value);
768         }
769         end_trace();
770
771         /* sending release to endpoint */
772         while(p_epointlist) {
773                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
774                 message->param.disconnectinfo.cause = cause;
775                 message->param.disconnectinfo.location = location;
776                 message_put(message);
777                 /* remove epoint */
778                 free_epointlist(p_epointlist);
779         }
780         new_state(PORT_STATE_RELEASE);
781         trigger_work(&p_m_g_delete);
782 }
783
784 /* NOTIFY INDICATION */
785 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
786 {
787         struct lcr_msg *message;
788
789         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
790         add_trace("notify", NULL, "%d", mncc->notify);
791         end_trace();
792
793         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
794         message->param.notifyinfo.notify = mncc->notify;
795         message_put(message);
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_bsc(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_TCHF_FRAME) {
892                 if (port)
893                         pgsm->frame_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->fields |= MNCC_F_CAUSE;
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                 trigger_work(&p_m_g_delete);
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                 trigger_work(&p_m_g_delete);
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                 trigger_work(&p_m_g_delete);
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->fields |= MNCC_F_CALLING;
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->fields &= ~MNCC_F_CALLING;
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->fields & MNCC_F_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->fields |= MNCC_F_CALLED;
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->fields |= MNCC_F_REDIRECTING;
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->fields &= ~MNCC_F_REDIRECTING;
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->fields & MNCC_F_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->fields |= MNCC_F_PROGRESS;
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->fields |= MNCC_F_CONNECTED;
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->fields &= ~MNCC_F_CONNECTED;
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->fields & MNCC_F_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->fields |= MNCC_F_PROGRESS;
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->fields |= MNCC_F_CAUSE;
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->fields |= MNCC_F_CAUSE;
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         trigger_work(&p_m_g_delete);
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 /* deletes only if l3id is release, otherwhise it will be triggered then */
1462 static int delete_event(struct lcr_work *work, void *instance, int index)
1463 {
1464         class Pgsm *gsmport = (class Pgsm *)instance;
1465
1466         delete gsmport;
1467
1468         return 0;
1469 }
1470
1471 /*
1472  * handler of bchannel events
1473  */
1474 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1475 {
1476         class Pgsm *gsmport = (class Pgsm *)instance;
1477         int ret;
1478         unsigned char buffer[2048+MISDN_HEADER_LEN];
1479         struct mISDNhead *hh = (struct mISDNhead *)buffer;
1480
1481         /* handle message from bchannel */
1482         if (gsmport->p_m_g_gsm_b_sock > -1) {
1483                 ret = recv(gsmport->p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
1484                 if (ret >= (int)MISDN_HEADER_LEN) {
1485                         switch(hh->prim) {
1486                                 /* we don't care about confirms, we use rx data to sync tx */
1487                                 case PH_DATA_CNF:
1488                                 break;
1489                                 /* we receive audio data, we respond to it AND we send tones */
1490                                 case PH_DATA_IND:
1491                                 gsmport->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1492                                 break;
1493                                 case PH_ACTIVATE_IND:
1494                                 gsmport->p_m_g_gsm_b_active = 1;
1495                                 break;
1496                                 case PH_DEACTIVATE_IND:
1497                                 gsmport->p_m_g_gsm_b_active = 0;
1498                                 break;
1499                         }
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 0;
1507 }
1508
1509 static void gsm_sock_close(void)
1510 {
1511         if (gsm->gsm_sock > -1)
1512                 close(gsm->gsm_sock);
1513         gsm->gsm_sock = -1;
1514 }
1515
1516 static int gsm_sock_open(char *portname)
1517 {
1518         int ret;
1519         int cnt;
1520         unsigned long on = 1;
1521         struct sockaddr_mISDN addr;
1522         struct mISDN_devinfo devinfo;
1523         int pri, bri;
1524
1525         /* check port counts */
1526         ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1527         if (ret < 0) {
1528                 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1529                 return(ret);
1530         }
1531
1532         if (cnt <= 0) {
1533                 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1534                 return -EIO;
1535         }
1536         gsm->gsm_port = mISDN_getportbyname(mISDNsocket, cnt, portname);
1537         if (gsm->gsm_port < 0) {
1538                 PERROR_RUNTIME("Port name '%s' not found, did you load loopback interface for GSM?.\n", portname);
1539                 return gsm->gsm_port;
1540         }
1541         /* get protocol */
1542         bri = pri = 0;
1543         devinfo.id = gsm->gsm_port;
1544         ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1545         if (ret < 0) {
1546                 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", gsm->gsm_port, ret);
1547                 return ret;
1548         }
1549         if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1550                 bri = 1;
1551         }
1552         if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1553                 pri = 1;
1554         }
1555         if (!pri && !pri) {
1556                 PERROR_RUNTIME("GSM port %d does not support TE PRI or TE BRI.\n", gsm->gsm_port);
1557         }
1558         /* open socket */
1559         if ((gsm->gsm_sock = socket(PF_ISDN, SOCK_DGRAM, (pri)?ISDN_P_TE_E1:ISDN_P_TE_S0)) < 0) {
1560                 PERROR_RUNTIME("GSM port %d failed to open socket.\n", gsm->gsm_port);
1561                 gsm_sock_close();
1562                 return gsm->gsm_sock;
1563         }
1564         /* set nonblocking io */
1565         if ((ret = ioctl(gsm->gsm_sock, FIONBIO, &on)) < 0) {
1566                 PERROR_RUNTIME("GSM port %d failed to set socket into nonblocking io.\n", gsm->gsm_port);
1567                 gsm_sock_close();
1568                 return ret;
1569         }
1570         /* bind socket to dchannel */
1571         memset(&addr, 0, sizeof(addr));
1572         addr.family = AF_ISDN;
1573         addr.dev = gsm->gsm_port;
1574         addr.channel = 0;
1575         if ((ret = bind(gsm->gsm_sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
1576                 PERROR_RUNTIME("GSM port %d failed to bind socket. (name = %s errno=%d)\n", gsm->gsm_port, portname, errno);
1577                 gsm_sock_close();
1578                 return (ret);
1579         }
1580
1581         return 0;
1582 }
1583
1584
1585 int gsm_exit(int rc)
1586 {
1587         /* free gsm instance */
1588         if (gsm) {
1589                 if (gsm->gsm_sock > -1)
1590                         gsm_sock_close();
1591                 /* shutdown network */
1592                 if (gsm->network)
1593                         bsc_shutdown_net((struct gsm_network *)gsm->network);
1594                 /* free network */
1595 //              if (gsm->network) {
1596 //                      free((struct gsm_network *)gsm->network); /* TBD */
1597 //              }
1598                 free(gsm);
1599                 gsm = NULL;
1600         }
1601
1602         return(rc);
1603 }
1604
1605 int gsm_init(void)
1606 {
1607         char hlr[128], cfg[128], filename[128];
1608         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1609         int pcapfd, rc;
1610
1611         debug_init();
1612         tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
1613         talloc_ctx_init();
1614         on_dso_load_token();
1615         on_dso_load_rrlp();
1616         on_dso_load_ho_dec();
1617         stderr_target = debug_target_create_stderr();
1618         debug_add_target(stderr_target);
1619
1620         bts_model_unknown_init();
1621         bts_model_bs11_init();
1622         bts_model_nanobts_init();
1623
1624         /* enable filters */
1625         debug_set_all_filter(stderr_target, 1);
1626
1627         /* seed the PRNG */
1628         srand(time(NULL));
1629
1630         /* create gsm instance */
1631         gsm = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
1632         gsm->gsm_sock = -1;
1633
1634         /* parse options */
1635         if (!gsm_conf(&gsm->conf)) {
1636                 PERROR("%s", gsm_conf_error);
1637                 return gsm_exit(-EINVAL);
1638         }
1639
1640         /* set debug */
1641         if (gsm->conf.debug[0])
1642                 debug_parse_category_mask(stderr_target, gsm->conf.debug);
1643
1644         /* open pcap file */
1645         if (gsm->conf.pcapfile[0]) {
1646                 if (gsm->conf.pcapfile[0] == '/')
1647                         SCPY(filename, gsm->conf.pcapfile);
1648                 else
1649                         SPRINT(filename, "%s/%s", CONFIG_DATA, gsm->conf.pcapfile);
1650                 pcapfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode);
1651                 if (pcapfd < 0) {
1652                         PERROR("Failed to open file for pcap\n");
1653                         return gsm_exit(-1);
1654                 }
1655                 e1_set_pcap_fd(pcapfd);
1656         }
1657
1658         /* use RTP proxy for audio streaming */
1659         ipacc_rtp_direct = 0;
1660
1661         /* init database */
1662         if (gsm->conf.hlr[0] == '/')
1663                 SCPY(hlr, gsm->conf.hlr);
1664         else
1665                 SPRINT(hlr, "%s/%s", CONFIG_DATA, gsm->conf.hlr);
1666         if (db_init(hlr)) {
1667                 PERROR("GSM DB: Failed to init database '%s'. Please check the option settings.\n", hlr);
1668                 return gsm_exit(-1);
1669         }
1670         printf("DB: Database initialized.\n");
1671         if (db_prepare()) {
1672                 PERROR("GSM DB: Failed to prepare database.\n");
1673                 return gsm_exit(-1);
1674         }
1675         printf("DB: Database prepared.\n");
1676
1677         /* setup the timer */
1678         db_sync_timer.cb = db_sync_timer_cb;
1679         db_sync_timer.data = NULL;
1680         bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL);
1681
1682         /* bootstrap network */
1683         if (gsm->conf.openbsc_cfg[0] == '/')
1684                 SCPY(cfg, gsm->conf.openbsc_cfg);
1685         else
1686                 SPRINT(cfg, "%s/%s", CONFIG_DATA, gsm->conf.openbsc_cfg);
1687         rc = bsc_bootstrap_network(&message_bsc, cfg);
1688         if (rc < 0) {
1689                 PERROR("Failed to bootstrap GSM network.\n");
1690                 return gsm_exit(-1);
1691         }
1692         gsm->network = bsc_gsmnet;
1693
1694         /* open gsm loop interface */
1695         if (gsm_sock_open(gsm->conf.interface_bsc)) {
1696                 return gsm_exit(-1);
1697         }
1698
1699         return 0;
1700 }
1701
1702 /*
1703  * handles bsc select function within LCR's main loop
1704  */
1705 int handle_gsm(void)
1706 {
1707         int ret1, ret2;
1708
1709         ret1 = bsc_upqueue((struct gsm_network *)gsm->network);
1710         debug_reset_context();
1711         ret2 = bsc_select_main(1); /* polling */
1712         if (ret1 || ret2)
1713                 return 1;
1714         return 0;
1715 }
1716