Removed LF from debug code.
[lcr.git] / gsm_ms.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN gsm (MS mode)                                                       **
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 <osmocore/select.h>
21 #include <osmocore/talloc.h>
22
23 #include <osmocom/osmocom_data.h>
24 #include <osmocom/logging.h>
25 #include <osmocom/l1l2_interface.h>
26 #include <osmocom/l23_app.h>
27 }
28
29 struct llist_head ms_list;
30 struct log_target *stderr_target;
31 void *l23_ctx = NULL;
32 int (*l23_app_work) (struct osmocom_ms *ms) = NULL;
33 int (*l23_app_exit) (struct osmocom_ms *ms) = NULL;
34
35 /*
36  * constructor
37  */
38 Pgsm_ms::Pgsm_ms(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : Pgsm(type, mISDNport, portname, settings, channel, exclusive, mode)
39 {
40         struct osmocom_ms *ms = NULL;
41         char *ms_name = mISDNport->ifport->gsm_ms_name;
42
43         p_m_g_instance = NULL;
44
45         llist_for_each_entry(ms, &ms_list, entity) {
46                 if (!strcmp(ms->name, ms_name)) {
47                         p_m_g_instance = ms;
48                         break;
49                 }
50         }
51
52         if (!p_m_g_instance)
53                 FATAL("MS name %s does not exists. Please fix!");
54
55         PDEBUG(DEBUG_GSM, "Created new GSMMSPort(%s %s).\n", portname, ms_name);
56 }
57
58 /*
59  * destructor
60  */
61 Pgsm_ms::~Pgsm_ms()
62 {
63         PDEBUG(DEBUG_GSM, "Destroyed GSM MS process(%s).\n", p_name);
64 }
65
66 /*
67  * handles all indications
68  */
69 /* SETUP INDICATION */
70 void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
71 {
72         int ret;
73         class Endpoint *epoint;
74         struct lcr_msg *message;
75         int channel;
76         struct gsm_mncc *mode, *proceeding, *frame;
77
78         /* process given callref */
79         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
80         add_trace("callref", "new", "0x%x", callref);
81         if (p_m_g_callref) {
82                 /* release in case the ID is already in use */
83                 add_trace("error", NULL, "callref already in use");
84                 end_trace();
85                 mncc = create_mncc(MNCC_REJ_REQ, callref);
86                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
87                 mncc->fields |= MNCC_F_CAUSE;
88                 mncc->cause.coding = 3;
89                 mncc->cause.location = 1;
90                 mncc->cause.value = 47;
91                 add_trace("cause", "coding", "%d", mncc->cause.coding);
92                 add_trace("cause", "location", "%d", mncc->cause.location);
93                 add_trace("cause", "value", "%d", mncc->cause.value);
94                 add_trace("reason", NULL, "callref already in use");
95                 end_trace();
96                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
97                 new_state(PORT_STATE_RELEASE);
98                 trigger_work(&p_m_g_delete);
99                 return;
100         }
101         p_m_g_callref = callref;
102         end_trace();
103
104         /* if blocked, release call with MT_RELEASE_COMPLETE */
105         if (p_m_mISDNport->ifport->block) {
106                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
107                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
108                 mncc->fields |= MNCC_F_CAUSE;
109                 mncc->cause.coding = 3;
110                 mncc->cause.location = 1;
111                 mncc->cause.value = 27;
112                 add_trace("cause", "coding", "%d", mncc->cause.coding);
113                 add_trace("cause", "location", "%d", mncc->cause.location);
114                 add_trace("cause", "value", "%d", mncc->cause.value);
115                 add_trace("reason", NULL, "port is blocked");
116                 end_trace();
117                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
118                 new_state(PORT_STATE_RELEASE);
119                 trigger_work(&p_m_g_delete);
120                 return;
121         }
122
123         /* caller information */
124         p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
125         if (mncc->fields & MNCC_F_CALLING) {
126                 switch (mncc->calling.present) {
127                         case 1:
128                         p_callerinfo.present = INFO_PRESENT_RESTRICTED;
129                         break;
130                         case 2:
131                         p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
132                         break;
133                         default:
134                         p_callerinfo.present = INFO_PRESENT_ALLOWED;
135                         break;
136                 }
137                 switch (mncc->calling.screen) {
138                         case 0:
139                         p_callerinfo.screen = INFO_SCREEN_USER;
140                         break;
141                         case 1:
142                         p_callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
143                         break;
144                         case 2:
145                         p_callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
146                         break;
147                         default:
148                         p_callerinfo.screen = INFO_SCREEN_NETWORK;
149                         break;
150                 }
151                 switch (mncc->calling.type) {
152                         case 0x0:
153                         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
154                         break;
155                         case 0x1:
156                         p_callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
157                         break;
158                         case 0x2:
159                         p_callerinfo.ntype = INFO_NTYPE_NATIONAL;
160                         break;
161                         case 0x4:
162                         p_callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
163                         break;
164                         default:
165                         p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
166                         break;
167                 }
168                 SCPY(p_callerinfo.id, mncc->calling.number);
169                 add_trace("calling", "type", "%d", mncc->calling.type);
170                 add_trace("calling", "plan", "%d", mncc->calling.plan);
171                 add_trace("calling", "present", "%d", mncc->calling.present);
172                 add_trace("calling", "screen", "%d", mncc->calling.screen);
173                 add_trace("calling", "number", "%s", mncc->calling.number);
174         }
175         p_callerinfo.isdn_port = p_m_portnum;
176         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
177         /* dialing information */
178         if (mncc->fields & MNCC_F_CALLED) {
179                 SCAT(p_dialinginfo.id, mncc->called.number);
180                 switch (mncc->called.type) {
181                         case 0x1:
182                         p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
183                         break;
184                         case 0x2:
185                         p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
186                         break;
187                         case 0x4:
188                         p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
189                         break;
190                         default:
191                         p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
192                         break;
193                 }
194                 add_trace("dialing", "type", "%d", mncc->called.type);
195                 add_trace("dialing", "plan", "%d", mncc->called.plan);
196                 add_trace("dialing", "number", "%s", mncc->called.number);
197         }
198         /* redir info */
199         p_redirinfo.ntype = INFO_NTYPE_NOTPRESENT;
200         if (mncc->fields & MNCC_F_REDIRECTING) {
201                 switch (mncc->redirecting.present) {
202                         case 1:
203                         p_redirinfo.present = INFO_PRESENT_RESTRICTED;
204                         break;
205                         case 2:
206                         p_redirinfo.present = INFO_PRESENT_NOTAVAIL;
207                         break;
208                         default:
209                         p_redirinfo.present = INFO_PRESENT_ALLOWED;
210                         break;
211                 }
212                 switch (mncc->redirecting.screen) {
213                         case 0:
214                         p_redirinfo.screen = INFO_SCREEN_USER;
215                         break;
216                         case 1:
217                         p_redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
218                         break;
219                         case 2:
220                         p_redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
221                         break;
222                         default:
223                         p_redirinfo.screen = INFO_SCREEN_NETWORK;
224                         break;
225                 }
226                 switch (mncc->redirecting.type) {
227                         case 0x0:
228                         p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
229                         break;
230                         case 0x1:
231                         p_redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
232                         break;
233                         case 0x2:
234                         p_redirinfo.ntype = INFO_NTYPE_NATIONAL;
235                         break;
236                         case 0x4:
237                         p_redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
238                         break;
239                         default:
240                         p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
241                         break;
242                 }
243                 SCPY(p_redirinfo.id, mncc->redirecting.number);
244                 add_trace("redir", "type", "%d", mncc->redirecting.type);
245                 add_trace("redir", "plan", "%d", mncc->redirecting.plan);
246                 add_trace("redir", "present", "%d", mncc->redirecting.present);
247                 add_trace("redir", "screen", "%d", mncc->redirecting.screen);
248                 add_trace("redir", "number", "%s", mncc->redirecting.number);
249                 p_redirinfo.isdn_port = p_m_portnum;
250         }
251         /* bearer capability */
252         if (mncc->fields & MNCC_F_BEARER_CAP) {
253                 switch (mncc->bearer_cap.transfer) {
254                         case 1:
255                         p_capainfo.bearer_capa = INFO_BC_DATAUNRESTRICTED;
256                         break;
257                         case 2:
258                         case 3:
259                         p_capainfo.bearer_capa = INFO_BC_AUDIO;
260                         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
261                         break;
262                         default:
263                         p_capainfo.bearer_capa = INFO_BC_SPEECH;
264                         p_capainfo.bearer_info1 = (options.law=='a')?3:2;
265                         break;
266                 }
267                 switch (mncc->bearer_cap.mode) {
268                         case 1:
269                         p_capainfo.bearer_mode = INFO_BMODE_PACKET;
270                         break;
271                         default:
272                         p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
273                         break;
274                 }
275         } else {
276                 p_capainfo.bearer_capa = INFO_BC_SPEECH;
277                 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
278                 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
279         }
280         /* if packet mode works some day, see dss1.cpp for conditions */
281         p_capainfo.source_mode = B_MODE_TRANSPARENT;
282
283         /* hunt channel */
284         ret = channel = hunt_bchannel();
285         if (ret < 0)
286                 goto no_channel;
287
288         /* open channel */
289         ret = seize_bchannel(channel, 1);
290         if (ret < 0) {
291                 no_channel:
292                 mncc = create_mncc(MNCC_REJ_REQ, p_m_g_callref);
293                 gsm_trace_header(p_m_mISDNport, this, MNCC_REJ_REQ, DIRECTION_OUT);
294                 mncc->fields |= MNCC_F_CAUSE;
295                 mncc->cause.coding = 3;
296                 mncc->cause.location = 1;
297                 mncc->cause.value = 34;
298                 add_trace("cause", "coding", "%d", mncc->cause.coding);
299                 add_trace("cause", "location", "%d", mncc->cause.location);
300                 add_trace("cause", "value", "%d", mncc->cause.value);
301                 add_trace("reason", NULL, "no channel");
302                 end_trace();
303                 send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
304                 new_state(PORT_STATE_RELEASE);
305                 trigger_work(&p_m_g_delete);
306                 return;
307         }
308         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
309         if (bchannel_open(p_m_b_index))
310                 goto no_channel;
311
312         /* what infos did we got ... */
313         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
314         if (p_callerinfo.id[0])
315                 add_trace("calling", "number", "%s", p_callerinfo.id);
316         else
317                 SPRINT(p_callerinfo.id, "imsi-%s", p_callerinfo.imsi);
318         add_trace("calling", "imsi", "%s", p_callerinfo.imsi);
319         add_trace("dialing", "number", "%s", p_dialinginfo.id);
320         end_trace();
321
322         /* create endpoint */
323         if (p_epointlist)
324                 FATAL("Incoming call but already got an endpoint.\n");
325         if (!(epoint = new Endpoint(p_serial, 0)))
326                 FATAL("No memory for Endpoint instance\n");
327         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
328                 FATAL("No memory for Endpoint Application instance\n");
329         epointlist_new(epoint->ep_serial);
330
331         /* modify lchan to GSM codec V1 */
332         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
333         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
334         mode->lchan_mode = 0x01; /* GSM V1 */
335         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
336         end_trace();
337         send_and_free_mncc(p_m_g_instance, mode->msg_type, mode);
338
339         /* send call proceeding */
340         gsm_trace_header(p_m_mISDNport, this, MNCC_CALL_PROC_REQ, DIRECTION_OUT);
341         proceeding = create_mncc(MNCC_CALL_PROC_REQ, p_m_g_callref);
342         if (p_m_mISDNport->tones) {
343                 proceeding->fields |= MNCC_F_PROGRESS;
344                 proceeding->progress.coding = 3; /* GSM */
345                 proceeding->progress.location = 1;
346                 proceeding->progress.descr = 8;
347                 add_trace("progress", "coding", "%d", proceeding->progress.coding);
348                 add_trace("progress", "location", "%d", proceeding->progress.location);
349                 add_trace("progress", "descr", "%d", proceeding->progress.descr);
350         }
351         end_trace();
352         send_and_free_mncc(p_m_g_instance, proceeding->msg_type, proceeding);
353
354         new_state(PORT_STATE_IN_PROCEEDING);
355
356         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
357                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
358                 end_trace();
359                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
360                 send_and_free_mncc(p_m_g_instance, frame->msg_type, frame);
361                 p_m_g_tch_connected = 1;
362         }
363
364         /* send setup message to endpoit */
365         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
366         message->param.setup.isdn_port = p_m_portnum;
367         message->param.setup.port_type = p_type;
368 //      message->param.setup.dtmf = 0;
369         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
370         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
371         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
372         SCPY((char *)message->param.setup.useruser.data, (char *)mncc->useruser.info);
373         message->param.setup.useruser.len = strlen(mncc->useruser.info);
374         message->param.setup.useruser.protocol = mncc->useruser.proto;
375         message_put(message);
376 }
377
378 /*
379  * MS sends message to port
380  */
381 static int message_ms(struct osmocom_ms *ms, int msg_type, void *arg)
382 {
383         struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
384         unsigned int callref = mncc->callref;
385         class Port *port;
386         class Pgsm_ms *pgsm_ms = NULL;
387         char name[64];
388         struct mISDNport *mISDNport;
389
390         /* Special messages */
391         if (msg_type) {
392         }
393
394         /* find callref */
395         callref = mncc->callref;
396         port = port_first;
397         while(port) {
398                 if ((port->p_type & PORT_CLASS_GSM_MASK) == PORT_CLASS_GSM_MS) {
399                         pgsm_ms = (class Pgsm_ms *)port;
400                         if (pgsm_ms->p_m_g_callref == callref) {
401                                 break;
402                         }
403                 }
404                 port = port->next;
405         }
406
407         if (msg_type == GSM_TCHF_FRAME) {
408                 if (port)
409                         pgsm_ms->frame_receive((struct gsm_trau_frame *)arg);
410                 return 0;
411         }
412
413         if (!port) {
414                 if (msg_type != MNCC_SETUP_IND)
415                         return(0);
416                 /* find gsm ms port */
417                 mISDNport = mISDNport_first;
418                 while(mISDNport) {
419                         if (mISDNport->gsm_ms && !strcmp(mISDNport->ifport->interface->name, ms->name))
420                                 break;
421                         mISDNport = mISDNport->next;
422                 }
423                 if (!mISDNport) {
424                         struct gsm_mncc *rej;
425
426                         rej = create_mncc(MNCC_REJ_REQ, callref);
427                         rej->fields |= MNCC_F_CAUSE;
428                         rej->cause.coding = 3;
429                         rej->cause.location = 1;
430                         rej->cause.value = 27;
431                         gsm_trace_header(NULL, NULL, MNCC_REJ_REQ, DIRECTION_OUT);
432                         add_trace("cause", "coding", "%d", rej->cause.coding);
433                         add_trace("cause", "location", "%d", rej->cause.location);
434                         add_trace("cause", "value", "%d", rej->cause.value);
435                         end_trace();
436                         send_and_free_mncc(ms, rej->msg_type, rej);
437                         return 0;
438                 }
439                 /* creating port object, transparent until setup with hdlc */
440                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
441                 if (!(pgsm_ms = new Pgsm_ms(PORT_TYPE_GSM_MS_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
442
443                         FATAL("Cannot create Port instance.\n");
444         }
445
446         switch(msg_type) {
447                 case MNCC_SETUP_IND:
448                 pgsm_ms->setup_ind(msg_type, callref, mncc);
449                 break;
450
451                 case MNCC_ALERT_IND:
452                 pgsm_ms->alert_ind(msg_type, callref, mncc);
453                 break;
454
455                 case MNCC_SETUP_CNF:
456                 pgsm_ms->setup_cnf(msg_type, callref, mncc);
457                 break;
458
459                 case MNCC_SETUP_COMPL_IND:
460                 pgsm_ms->setup_compl_ind(msg_type, callref, mncc);
461                 break;
462
463                 case MNCC_DISC_IND:
464                 pgsm_ms->disc_ind(msg_type, callref, mncc);
465                 break;
466
467                 case MNCC_REL_IND:
468                 case MNCC_REL_CNF:
469                 case MNCC_REJ_IND:
470                 pgsm_ms->rel_ind(msg_type, callref, mncc);
471                 break;
472
473                 case MNCC_NOTIFY_IND:
474                 pgsm_ms->notify_ind(msg_type, callref, mncc);
475                 break;
476
477                 default:
478                 ;
479         }
480         return(0);
481 }
482
483 /* MESSAGE_SETUP */
484 void Pgsm_ms::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
485 {
486         struct lcr_msg *message;
487         int ret;
488         struct epoint_list *epointlist;
489         struct gsm_mncc *mncc;
490         int channel;
491
492         /* copy setup infos to port */
493         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
494         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
495         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
496         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
497
498         /* no number */
499         if (!p_dialinginfo.id[0]) {
500                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
501                 add_trace("failure", NULL, "No dialed subscriber given.");
502                 end_trace();
503                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
504                 message->param.disconnectinfo.cause = 28;
505                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
506                 message_put(message);
507                 new_state(PORT_STATE_RELEASE);
508                 trigger_work(&p_m_g_delete);
509                 return;
510         }
511         
512         /* release if port is blocked */
513         if (p_m_mISDNport->ifport->block) {
514                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
515                 add_trace("failure", NULL, "Port blocked.");
516                 end_trace();
517                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
518                 message->param.disconnectinfo.cause = 27; // temp. unavail.
519                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
520                 message_put(message);
521                 new_state(PORT_STATE_RELEASE);
522                 trigger_work(&p_m_g_delete);
523                 return;
524         }
525
526         /* hunt channel */
527         ret = channel = hunt_bchannel();
528         if (ret < 0)
529                 goto no_channel;
530         /* open channel */
531         ret = seize_bchannel(channel, 1);
532         if (ret < 0) {
533                 no_channel:
534                 gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
535                 add_trace("failure", NULL, "No internal audio channel available.");
536                 end_trace();
537                 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
538                 message->param.disconnectinfo.cause = 34;
539                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
540                 message_put(message);
541                 new_state(PORT_STATE_RELEASE);
542                 trigger_work(&p_m_g_delete);
543                 return;
544         }
545         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
546         if (bchannel_open(p_m_b_index))
547                 goto no_channel;
548
549         /* attach only if not already */
550         epointlist = p_epointlist;
551         while(epointlist) {
552                 if (epointlist->epoint_id == epoint_id)
553                         break;
554                 epointlist = epointlist->next;
555         }
556         if (!epointlist)
557                 epointlist_new(epoint_id);
558
559         /* creating l3id */
560         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
561         p_m_g_callref = new_callref++;
562         add_trace("callref", "new", "0x%x", p_m_g_callref);
563         end_trace();
564
565         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_REQ, DIRECTION_OUT);
566         mncc = create_mncc(MNCC_SETUP_REQ, p_m_g_callref);
567         if (!strncasecmp(p_dialinginfo.id, "emerg", 5)) {
568                 mncc->emergency = 1;
569         } else {
570                 /* caller info (only clir) */
571                 switch (p_callerinfo.present) {
572                         case INFO_PRESENT_ALLOWED:
573                         mncc->clir.inv = 1;
574                         break;
575                         default:
576                         mncc->clir.sup = 1;
577                 }
578                 /* dialing information (mandatory) */
579                 mncc->fields |= MNCC_F_CALLED;
580                 if (!strncmp(p_dialinginfo.id, "imsi-", 5)) {
581                         SCPY(mncc->imsi, p_dialinginfo.id+5);
582                         add_trace("dialing", "imsi", "%s", mncc->imsi);
583                 } else {
584                         SCPY(mncc->called.number, p_dialinginfo.id);
585                         add_trace("dialing", "number", "%s", mncc->called.number);
586                 }
587                 
588                 /* bearer capability (mandatory) */
589                 mncc->fields |= MNCC_F_BEARER_CAP;
590                 mncc->bearer_cap.coding = 0;
591                 mncc->bearer_cap.radio = 1;
592                 mncc->bearer_cap.speech_ctm = 0;
593                 mncc->bearer_cap.speech_ver[0] = 0;
594                 mncc->bearer_cap.speech_ver[1] = -1; /* end of list */
595                 switch (p_capainfo.bearer_capa) {
596                         case INFO_BC_DATAUNRESTRICTED:
597                         case INFO_BC_DATARESTRICTED:
598                         mncc->bearer_cap.transfer = 1;
599                         break;
600                         case INFO_BC_SPEECH:
601                         mncc->bearer_cap.transfer = 0;
602                         break;
603                         default:
604                         mncc->bearer_cap.transfer = 2;
605                         break;
606                 }
607                 switch (p_capainfo.bearer_mode) {
608                         case INFO_BMODE_PACKET:
609                         mncc->bearer_cap.mode = 1;
610                         break;
611                         default:
612                         mncc->bearer_cap.mode = 0;
613                         break;
614                 }
615         }
616
617         end_trace();
618         send_and_free_mncc(p_m_g_instance, mncc->msg_type, mncc);
619
620         new_state(PORT_STATE_OUT_SETUP);
621
622         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
623         message_put(message);
624
625         new_state(PORT_STATE_OUT_PROCEEDING);
626 }
627
628 /*
629  * endpoint sends messages to the port
630  */
631 int Pgsm_ms::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
632 {
633         if (Pgsm::message_epoint(epoint_id, message_id, param))
634                 return(1);
635
636         switch(message_id) {
637                 case MESSAGE_SETUP: /* dial-out command received from epoint */
638                 if (p_state!=PORT_STATE_IDLE)
639                         break;
640                 message_setup(epoint_id, message_id, param);
641                 break;
642
643                 default:
644                 PDEBUG(DEBUG_GSM, "Pgsm_ms(%s) gsm port with (caller id %s) received unhandled nessage: %d\n", p_name, p_callerinfo.id, message_id);
645         }
646
647         return(1);
648 }
649
650 int gsm_ms_exit(int rc)
651 {
652
653         return(rc);
654 }
655
656 int gsm_ms_init(void)
657 {
658         INIT_LLIST_HEAD(&ms_list);
659         log_init(&log_info);
660         stderr_target = log_target_create_stderr();
661         log_add_target(stderr_target);
662         log_set_all_filter(stderr_target, 1);
663
664         l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
665
666         return 0;
667 }
668
669 /* add a new GSM mobile instance */
670 int gsm_ms_new(const char *name, const char *socket_path)
671 {
672         struct osmocom_ms *ms = NULL;
673         int rc;
674
675         PDEBUG(DEBUG_GSM, "GSM: creating new instance '%s' on '%s'\n", name, socket_path);
676
677         ms = talloc_zero(l23_ctx, struct osmocom_ms);
678         if (!ms) {
679                 FATAL("Failed to allocate MS\n");
680         }
681         /* must add here, because other init processes may search in the list */
682         llist_add_tail(&ms->entity, &ms_list);
683
684
685         SPRINT(ms->name, name);
686
687         rc = layer2_open(ms, socket_path);
688         if (rc < 0) {
689                 FATAL("Failed during layer2_open()\n");
690         }
691
692         lapdm_init(&ms->l2_entity.lapdm_dcch, ms);
693         lapdm_init(&ms->l2_entity.lapdm_acch, ms);
694
695         rc = l23_app_init(ms);
696         if (rc < 0) {
697                 FATAL("Failed to init layer23\n");
698         }
699         ms->cclayer.mncc_recv = message_ms;
700
701         return 0;
702 }
703
704 int gsm_ms_delete(const char *name)
705 {
706         struct osmocom_ms *ms = NULL;
707         int found = 0;
708
709         PDEBUG(DEBUG_GSM, "GSM: destroying instance '%s'\n", name);
710
711         llist_for_each_entry(ms, &ms_list, entity) {
712                 if (!strcmp(ms->name, name)) {
713                         found = 1;
714                         break;
715                 }
716         }
717
718         if (!found) {
719                 FATAL("Failed delete layer23, instance '%s' not found\n", name);
720         }
721
722         l23_app_exit(ms);
723         llist_del(&ms->entity);
724
725         return 0;
726 }
727
728 /*
729  * handles bsc select function within LCR's main loop
730  */
731 int handle_gsm_ms(void)
732 {
733         struct osmocom_ms *ms = NULL;
734         int work = 0;
735
736         llist_for_each_entry(ms, &ms_list, entity) {
737                 if (l23_app_work(ms))
738                         work = 1;
739 //              debug_reset_context();
740                 if (bsc_select_main(1)) /* polling */
741                         work = 1;
742         }
743
744         return work;
745 }
746