Fixed missing channel IE in some cases.
[lcr.git] / gsm.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **            MNCC-Interface: Harald Welte                                   **
8 **                                                                           **
9 ** mISDN gsm                                                                 **
10 **                                                                           **
11 \*****************************************************************************/ 
12
13 #include "main.h"
14 #include "mncc.h"
15
16 #ifndef _GNU_SOURCE
17 #define _GNU_SOURCE
18 #endif
19 extern "C" {
20 #include "gsm_audio.h"
21 }
22
23 #include <mISDN/mISDNcompat.h>
24 #include <assert.h>
25
26 #define SOCKET_RETRY_TIMER      5
27
28 //struct lcr_gsm *gsm = NULL;
29
30 int new_callref = 1;
31
32 /* names of MNCC-SAP */
33 static const struct _value_string {
34         int msg_type;
35         const char *name;
36 } mncc_names[] = {
37         { MNCC_SETUP_REQ,       "MNCC_SETUP_REQ" },
38         { MNCC_SETUP_IND,       "MNCC_SETUP_IND" },
39         { MNCC_SETUP_RSP,       "MNCC_SETUP_RSP" },
40         { MNCC_SETUP_CNF,       "MNCC_SETUP_CNF" },
41         { MNCC_SETUP_COMPL_REQ, "MNCC_SETUP_COMPL_REQ" },
42         { MNCC_SETUP_COMPL_IND, "MNCC_SETUP_COMPL_IND" },
43         { MNCC_CALL_CONF_IND,   "MNCC_CALL_CONF_IND" },
44         { MNCC_CALL_PROC_REQ,   "MNCC_CALL_PROC_REQ" },
45         { MNCC_PROGRESS_REQ,    "MNCC_PROGRESS_REQ" },
46         { MNCC_ALERT_REQ,       "MNCC_ALERT_REQ" },
47         { MNCC_ALERT_IND,       "MNCC_ALERT_IND" },
48         { MNCC_NOTIFY_REQ,      "MNCC_NOTIFY_REQ" },
49         { MNCC_NOTIFY_IND,      "MNCC_NOTIFY_IND" },
50         { MNCC_DISC_REQ,        "MNCC_DISC_REQ" },
51         { MNCC_DISC_IND,        "MNCC_DISC_IND" },
52         { MNCC_REL_REQ,         "MNCC_REL_REQ" },
53         { MNCC_REL_IND,         "MNCC_REL_IND" },
54         { MNCC_REL_CNF,         "MNCC_REL_CNF" },
55         { MNCC_FACILITY_REQ,    "MNCC_FACILITY_REQ" },
56         { MNCC_FACILITY_IND,    "MNCC_FACILITY_IND" },
57         { MNCC_START_DTMF_IND,  "MNCC_START_DTMF_IND" },
58         { MNCC_START_DTMF_RSP,  "MNCC_START_DTMF_RSP" },
59         { MNCC_START_DTMF_REJ,  "MNCC_START_DTMF_REJ" },
60         { MNCC_STOP_DTMF_IND,   "MNCC_STOP_DTMF_IND" },
61         { MNCC_STOP_DTMF_RSP,   "MNCC_STOP_DTMF_RSP" },
62         { MNCC_MODIFY_REQ,      "MNCC_MODIFY_REQ" },
63         { MNCC_MODIFY_IND,      "MNCC_MODIFY_IND" },
64         { MNCC_MODIFY_RSP,      "MNCC_MODIFY_RSP" },
65         { MNCC_MODIFY_CNF,      "MNCC_MODIFY_CNF" },
66         { MNCC_MODIFY_REJ,      "MNCC_MODIFY_REJ" },
67         { MNCC_HOLD_IND,        "MNCC_HOLD_IND" },
68         { MNCC_HOLD_CNF,        "MNCC_HOLD_CNF" },
69         { MNCC_HOLD_REJ,        "MNCC_HOLD_REJ" },
70         { MNCC_RETRIEVE_IND,    "MNCC_RETRIEVE_IND" },
71         { MNCC_RETRIEVE_CNF,    "MNCC_RETRIEVE_CNF" },
72         { MNCC_RETRIEVE_REJ,    "MNCC_RETRIEVE_REJ" },
73         { MNCC_USERINFO_REQ,    "MNCC_USERINFO_REQ" },
74         { MNCC_USERINFO_IND,    "MNCC_USERINFO_IND" },
75         { MNCC_REJ_REQ,         "MNCC_REJ_REQ" },
76         { MNCC_REJ_IND,         "MNCC_REJ_IND" },
77         { MNCC_PROGRESS_IND,    "MNCC_PROGRESS_IND" },
78         { MNCC_CALL_PROC_IND,   "MNCC_CALL_PROC_IND" },
79         { MNCC_CALL_CONF_REQ,   "MNCC_CALL_CONF_REQ" },
80         { MNCC_START_DTMF_REQ,  "MNCC_START_DTMF_REQ" },
81         { MNCC_STOP_DTMF_REQ,   "MNCC_STOP_DTMF_REQ" },
82         { MNCC_HOLD_REQ,        "MNCC_HOLD_REQ " },
83         { MNCC_RETRIEVE_REQ,    "MNCC_RETRIEVE_REQ" },
84         { 0,                    NULL }
85 };
86
87 const char *mncc_name(int value)
88 {
89         int i = 0;
90
91         while (mncc_names[i].name) {
92                 if (mncc_names[i].msg_type == value)
93                         return mncc_names[i].name;
94                 i++;
95         }
96         return "unknown";
97 }
98
99 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data);
100
101 /*
102  * create and send mncc message
103  */
104 struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
105 {
106         struct gsm_mncc *mncc;
107
108         mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
109         mncc->msg_type = msg_type;
110         mncc->callref = callref;
111         return (mncc);
112 }
113 int send_and_free_mncc(struct lcr_gsm *lcr_gsm, unsigned int msg_type, void *data)
114 {
115         int ret = 0;
116
117         if (lcr_gsm) {
118                 ret = mncc_send(lcr_gsm, msg_type, data);
119         }
120         free(data);
121
122         return ret;
123 }
124
125 static int delete_event(struct lcr_work *work, void *instance, int index);
126
127 /*
128  * constructor
129  */
130 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)
131 {
132         p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
133         memset(&p_m_g_delete, 0, sizeof(p_m_g_delete));
134         add_work(&p_m_g_delete, delete_event, this, 0);
135         p_m_g_lcr_gsm = NULL;
136         p_m_g_callref = 0;
137         p_m_g_mode = 0;
138         p_m_g_gsm_b_sock = -1;
139         p_m_g_gsm_b_index = -1;
140         p_m_g_gsm_b_active = 0;
141         p_m_g_notify_pending = NULL;
142         p_m_g_decoder = gsm_audio_create();
143         p_m_g_encoder = gsm_audio_create();
144         if (!p_m_g_encoder || !p_m_g_decoder) {
145                 PERROR("Failed to create GSM audio codec instance\n");
146                 trigger_work(&p_m_g_delete);
147         }
148         p_m_g_rxpos = 0;
149         p_m_g_tch_connected = 0;
150
151         PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
152 }
153
154 /*
155  * destructor
156  */
157 Pgsm::~Pgsm()
158 {
159         PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
160
161         del_work(&p_m_g_delete);
162
163         /* remove queued message */
164         if (p_m_g_notify_pending)
165                 message_free(p_m_g_notify_pending);
166
167         /* close audio transfer socket */
168         if (p_m_g_gsm_b_sock > -1)
169                 bchannel_close();
170
171         /* close codec */
172         if (p_m_g_encoder)
173                 gsm_audio_destroy(p_m_g_encoder);
174         if (p_m_g_decoder)
175                 gsm_audio_destroy(p_m_g_decoder);
176 }
177
178
179 /* close bsc side bchannel */
180 void Pgsm::bchannel_close(void)
181 {
182         if (p_m_g_gsm_b_sock > -1) {
183                 unregister_fd(&p_m_g_gsm_b_fd);
184                 close(p_m_g_gsm_b_sock);
185         }
186         p_m_g_gsm_b_sock = -1;
187         p_m_g_gsm_b_index = -1;
188         p_m_g_gsm_b_active = 0;
189 }
190
191 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index);
192
193 /* open external side bchannel */
194 int Pgsm::bchannel_open(int index)
195 {
196         int ret;
197         struct sockaddr_mISDN addr;
198         struct mISDNhead act;
199
200         if (p_m_g_gsm_b_sock > -1) {
201                 PERROR("Socket already created for index %d\n", index);
202                 return(-EIO);
203         }
204
205         /* open socket */
206         ret = p_m_g_gsm_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
207         if (ret < 0) {
208                 PERROR("Failed to open bchannel-socket for index %d\n", index);
209                 bchannel_close();
210                 return(ret);
211         }
212         memset(&p_m_g_gsm_b_fd, 0, sizeof(p_m_g_gsm_b_fd));
213         p_m_g_gsm_b_fd.fd = p_m_g_gsm_b_sock;
214         register_fd(&p_m_g_gsm_b_fd, LCR_FD_READ, b_handler, this, 0);
215
216
217         /* bind socket to bchannel */
218         addr.family = AF_ISDN;
219         addr.dev = mISDNloop.port;
220         addr.channel = index+1+(index>15);
221         ret = bind(p_m_g_gsm_b_sock, (struct sockaddr *)&addr, sizeof(addr));
222         if (ret < 0) {
223                 PERROR("Failed to bind bchannel-socket for index %d\n", index);
224                 bchannel_close();
225                 return(ret);
226         }
227         /* activate bchannel */
228         PDEBUG(DEBUG_GSM, "Activating GSM side channel index %i.\n", index);
229         act.prim = PH_ACTIVATE_REQ; 
230         act.id = 0;
231         ret = sendto(p_m_g_gsm_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
232         if (ret < 0) {
233                 PERROR("Failed to activate index %d\n", index);
234                 bchannel_close();
235                 return(ret);
236         }
237
238         p_m_g_gsm_b_index = index;
239
240         return(0);
241 }
242
243 /* receive from bchannel */
244 void Pgsm::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
245 {
246         unsigned char frame[33];
247
248         /* encoder init failed */
249         if (!p_m_g_encoder)
250                 return;
251
252         /* (currently) not connected, so don't flood tch! */
253         if (!p_m_g_tch_connected)
254                 return;
255
256         /* write to rx buffer */
257         while(len--) {
258                 p_m_g_rxdata[p_m_g_rxpos++] = audio_law_to_s32[*data++];
259                 if (p_m_g_rxpos == 160) {
260                         p_m_g_rxpos = 0;
261
262                         /* encode data */
263                         gsm_audio_encode(p_m_g_encoder, p_m_g_rxdata, frame);
264                         frame_send(frame);
265                 }
266         }
267 }
268
269 /* transmit to bchannel */
270 void Pgsm::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
271 {
272         unsigned char buf[MISDN_HEADER_LEN+len];
273         struct mISDNhead *hh = (struct mISDNhead *)buf;
274         int ret;
275
276         if (!p_m_g_gsm_b_active)
277                 return;
278
279         /* make and send frame */
280         hh->prim = PH_DATA_REQ;
281         hh->id = 0;
282         memcpy(buf+MISDN_HEADER_LEN, data, len);
283         ret = sendto(p_m_g_gsm_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
284         if (ret <= 0)
285                 PERROR("Failed to send to socket index %d\n", p_m_g_gsm_b_index);
286 }
287
288 void Pgsm::frame_send(void *_frame)
289 {
290         unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
291         struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
292         
293         frame->msg_type = GSM_TCHF_FRAME;
294         frame->callref = p_m_g_callref;
295         memcpy(frame->data, _frame, 33);
296
297         if (p_m_g_lcr_gsm) {
298                 mncc_send(p_m_g_lcr_gsm, frame->msg_type, frame);
299         }
300 }
301
302
303 void Pgsm::frame_receive(void *arg)
304 {
305         struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
306         signed short samples[160];
307         unsigned char data[160];
308         int i;
309
310         if (!p_m_g_decoder)
311                 return;
312
313         if ((frame->data[0]>>4) != 0xd)
314                 PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
315         
316         /* decode */
317         gsm_audio_decode(p_m_g_decoder, frame->data, samples);
318         for (i = 0; i < 160; i++) {
319                 data[i] = audio_s16_to_law[samples[i] & 0xffff];
320         }
321
322         /* send */
323         bchannel_send(PH_DATA_REQ, 0, data, 160);
324 }
325
326
327 /*
328  * create trace
329  */
330 void gsm_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg_type, int direction)
331 {
332         char msgtext[64];
333
334         /* select message and primitive text */
335         SCPY(msgtext, mncc_name(msg_type));
336
337         /* add direction */
338         if (port) {
339                 switch(port->p_type) {
340                 case PORT_TYPE_GSM_BS_OUT:
341                 case PORT_TYPE_GSM_BS_IN:
342                         SCAT(msgtext, " LCR<->BSC");
343                         break;
344                 case PORT_TYPE_GSM_MS_OUT:
345                 case PORT_TYPE_GSM_MS_IN:
346                         SCAT(msgtext, " LCR<->MS");
347                         break;
348                 }
349         } else
350                 SCAT(msgtext, " ----");
351
352         /* init trace with given values */
353         start_trace(mISDNport?mISDNport->portnum:-1,
354                     mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
355                     port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
356                     port?port->p_dialinginfo.id:NULL,
357                     direction,
358                     CATEGORY_CH,
359                     port?port->p_serial:0,
360                     msgtext);
361 }
362
363 /* select free bchannel from loopback interface */
364 int Pgsm::hunt_bchannel(void)
365 {
366         return loop_hunt_bchannel(this, p_m_mISDNport);
367 }
368
369 /* PROCEEDING INDICATION */
370 void Pgsm::call_conf_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
371 {
372         struct gsm_mncc *mode;
373
374         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
375         if (mncc->fields & MNCC_F_CAUSE) {
376                 add_trace("cause", "coding", "%d", mncc->cause.coding);
377                 add_trace("cause", "location", "%", mncc->cause.location);
378                 add_trace("cause", "value", "%", mncc->cause.value);
379         }
380         end_trace();
381
382         /* modify lchan to GSM codec V1 */
383         gsm_trace_header(p_m_mISDNport, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
384         mode = create_mncc(MNCC_LCHAN_MODIFY, p_m_g_callref);
385         mode->lchan_mode = 0x01; /* GSM V1 */
386         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
387         end_trace();
388         send_and_free_mncc(p_m_g_lcr_gsm, mode->msg_type, mode);
389
390 }
391
392 /* CALL PROCEEDING INDICATION */
393 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
394 {
395         struct lcr_msg *message;
396         struct gsm_mncc *frame;
397
398         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
399         end_trace();
400
401         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
402         message_put(message);
403
404         new_state(PORT_STATE_OUT_PROCEEDING);
405
406         if (p_m_mISDNport->earlyb && !p_m_g_tch_connected) { /* only if ... */
407                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
408                 end_trace();
409                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
410                 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
411                 p_m_g_tch_connected = 1;
412         }
413 }
414
415 /* ALERTING INDICATION */
416 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
417 {
418         struct lcr_msg *message;
419         struct gsm_mncc *frame;
420
421         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
422         end_trace();
423
424         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
425         message_put(message);
426
427         new_state(PORT_STATE_OUT_ALERTING);
428
429         if (p_m_mISDNport->earlyb && !p_m_g_tch_connected) { /* only if ... */
430                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
431                 end_trace();
432                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
433                 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
434                 p_m_g_tch_connected = 1;
435         }
436 }
437
438 /* CONNECT INDICATION */
439 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
440 {
441         struct gsm_mncc *resp, *frame;
442         struct lcr_msg *message;
443
444         SCPY(p_connectinfo.id, mncc->connected.number);
445         SCPY(p_connectinfo.imsi, mncc->imsi);
446         p_connectinfo.present = INFO_PRESENT_ALLOWED;
447         p_connectinfo.screen = INFO_SCREEN_NETWORK;
448         p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
449         p_connectinfo.isdn_port = p_m_portnum;
450         SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
451
452         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
453         if (p_connectinfo.id[0])
454                 add_trace("connect", "number", "%s", p_connectinfo.id);
455         else if (mncc->imsi[0])
456                 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
457         if (mncc->imsi[0])
458                 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
459         end_trace();
460
461         /* send resp */
462         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
463         resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_m_g_callref);
464         end_trace();
465         send_and_free_mncc(p_m_g_lcr_gsm, resp->msg_type, resp);
466
467         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
468         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
469         message_put(message);
470
471         new_state(PORT_STATE_CONNECT);
472
473         if (!p_m_g_tch_connected) { /* only if ... */
474                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
475                 end_trace();
476                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
477                 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
478                 p_m_g_tch_connected = 1;
479         }
480 }
481
482 /* CONNECT ACK INDICATION */
483 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
484 {
485         struct gsm_mncc *frame;
486
487         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
488         end_trace();
489
490         new_state(PORT_STATE_CONNECT);
491
492         if (!p_m_g_tch_connected) { /* only if ... */
493                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
494                 end_trace();
495                 frame = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
496                 send_and_free_mncc(p_m_g_lcr_gsm, frame->msg_type, frame);
497                 p_m_g_tch_connected = 1;
498         }
499 }
500
501 /* DISCONNECT INDICATION */
502 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
503 {
504         struct lcr_msg *message;
505         int cause = 16, location = 0;
506         struct gsm_mncc *resp;
507
508         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
509         if (mncc->fields & MNCC_F_CAUSE) {
510                 location = mncc->cause.location;
511                 cause = mncc->cause.value;
512                 add_trace("cause", "coding", "%d", mncc->cause.coding);
513                 add_trace("cause", "location", "%d", location);
514                 add_trace("cause", "value", "%d", cause);
515         }
516         end_trace();
517
518         /* send release */
519         resp = create_mncc(MNCC_REL_REQ, p_m_g_callref);
520         gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
521 #if 0
522         resp->fields |= MNCC_F_CAUSE;
523         resp->cause.coding = 3;
524         resp->cause.location = 1;
525         resp->cause.value = cause;
526         add_trace("cause", "coding", "%d", resp->cause.coding);
527         add_trace("cause", "location", "%d", resp->cause.location);
528         add_trace("cause", "value", "%d", resp->cause.value);
529 #endif
530         end_trace();
531         send_and_free_mncc(p_m_g_lcr_gsm, resp->msg_type, resp);
532
533         /* sending release to endpoint */
534         while(p_epointlist) {
535                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
536                 message->param.disconnectinfo.cause = cause;
537                 message->param.disconnectinfo.location = location;
538                 message_put(message);
539                 /* remove epoint */
540                 free_epointlist(p_epointlist);
541         }
542         new_state(PORT_STATE_RELEASE);
543         trigger_work(&p_m_g_delete);
544 }
545
546 /* CC_RELEASE INDICATION */
547 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
548 {
549         int location = 0, cause = 16;
550         struct lcr_msg *message;
551
552         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
553         if (mncc->fields & MNCC_F_CAUSE) {
554                 location = mncc->cause.location;
555                 cause = mncc->cause.value;
556                 add_trace("cause", "coding", "%d", mncc->cause.coding);
557                 add_trace("cause", "location", "%d", mncc->cause.location);
558                 add_trace("cause", "value", "%d", mncc->cause.value);
559         }
560         end_trace();
561
562         /* sending release to endpoint */
563         while(p_epointlist) {
564                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
565                 message->param.disconnectinfo.cause = cause;
566                 message->param.disconnectinfo.location = location;
567                 message_put(message);
568                 /* remove epoint */
569                 free_epointlist(p_epointlist);
570         }
571         new_state(PORT_STATE_RELEASE);
572         trigger_work(&p_m_g_delete);
573 }
574
575 /* NOTIFY INDICATION */
576 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
577 {
578         struct lcr_msg *message;
579
580         gsm_trace_header(p_m_mISDNport, this, msg_type, DIRECTION_IN);
581         add_trace("notify", NULL, "%d", mncc->notify);
582         end_trace();
583
584         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
585         message->param.notifyinfo.notify = mncc->notify;
586         message_put(message);
587 }
588
589 /* MESSAGE_NOTIFY */
590 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
591 {
592         struct gsm_mncc *mncc;
593         int notify;
594
595 //      printf("if = %d\n", param->notifyinfo.notify);
596         if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
597                 notify = param->notifyinfo.notify & 0x7f;
598                 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
599                         /* queue notification */
600                         if (p_m_g_notify_pending)
601                                 message_free(p_m_g_notify_pending);
602                         p_m_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
603                         memcpy(&p_m_g_notify_pending->param, param, sizeof(union parameter));
604                 } else {
605                         /* sending notification */
606                         gsm_trace_header(p_m_mISDNport, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
607                         add_trace("notify", NULL, "%d", notify);
608                         end_trace();
609                         mncc = create_mncc(MNCC_NOTIFY_REQ, p_m_g_callref);
610                         mncc->notify = notify;
611                         send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
612                 }
613         }
614 }
615
616 /* MESSAGE_ALERTING */
617 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
618 {
619         struct gsm_mncc *mncc;
620
621         /* send alert */
622         gsm_trace_header(p_m_mISDNport, this, MNCC_ALERT_REQ, DIRECTION_OUT);
623         mncc = create_mncc(MNCC_ALERT_REQ, p_m_g_callref);
624         if (p_m_mISDNport->tones) {
625                 mncc->fields |= MNCC_F_PROGRESS;
626                 mncc->progress.coding = 3; /* GSM */
627                 mncc->progress.location = 1;
628                 mncc->progress.descr = 8;
629                 add_trace("progress", "coding", "%d", mncc->progress.coding);
630                 add_trace("progress", "location", "%d", mncc->progress.location);
631                 add_trace("progress", "descr", "%d", mncc->progress.descr);
632         }
633         end_trace();
634         send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
635
636         new_state(PORT_STATE_IN_ALERTING);
637
638         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
639                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
640                 end_trace();
641                 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
642                 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
643                 p_m_g_tch_connected = 1;
644         }
645 }
646
647 /* MESSAGE_CONNECT */
648 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
649 {
650         struct gsm_mncc *mncc;
651
652         /* copy connected information */
653         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
654         /* screen outgoing caller id */
655         do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
656
657         /* send connect */
658         mncc = create_mncc(MNCC_SETUP_RSP, p_m_g_callref);
659         gsm_trace_header(p_m_mISDNport, this, MNCC_SETUP_RSP, DIRECTION_OUT);
660         /* caller information */
661         mncc->fields |= MNCC_F_CONNECTED;
662         mncc->connected.plan = 1;
663         switch (p_callerinfo.ntype) {
664                 case INFO_NTYPE_UNKNOWN:
665                 mncc->connected.type = 0x0;
666                 break;
667                 case INFO_NTYPE_INTERNATIONAL:
668                 mncc->connected.type = 0x1;
669                 break;
670                 case INFO_NTYPE_NATIONAL:
671                 mncc->connected.type = 0x2;
672                 break;
673                 case INFO_NTYPE_SUBSCRIBER:
674                 mncc->connected.type = 0x4;
675                 break;
676                 default: /* INFO_NTYPE_NOTPRESENT */
677                 mncc->fields &= ~MNCC_F_CONNECTED;
678                 break;
679         }
680         switch (p_callerinfo.screen) {
681                 case INFO_SCREEN_USER:
682                 mncc->connected.screen = 0;
683                 break;
684                 default: /* INFO_SCREEN_NETWORK */
685                 mncc->connected.screen = 3;
686                 break;
687         }
688         switch (p_callerinfo.present) {
689                 case INFO_PRESENT_ALLOWED:
690                 mncc->connected.present = 0;
691                 break;
692                 case INFO_PRESENT_RESTRICTED:
693                 mncc->connected.present = 1;
694                 break;
695                 default: /* INFO_PRESENT_NOTAVAIL */
696                 mncc->connected.present = 2;
697                 break;
698         }
699         if (mncc->fields & MNCC_F_CONNECTED) {
700                 SCPY(mncc->connected.number, p_connectinfo.id);
701                 add_trace("connected", "type", "%d", mncc->connected.type);
702                 add_trace("connected", "plan", "%d", mncc->connected.plan);
703                 add_trace("connected", "present", "%d", mncc->connected.present);
704                 add_trace("connected", "screen", "%d", mncc->connected.screen);
705                 add_trace("connected", "number", "%s", mncc->connected.number);
706         }
707         end_trace();
708         send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
709
710         new_state(PORT_STATE_CONNECT_WAITING);
711 }
712
713 /* MESSAGE_DISCONNECT */
714 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
715 {
716         struct gsm_mncc *mncc;
717
718         /* send disconnect */
719         mncc = create_mncc(MNCC_DISC_REQ, p_m_g_callref);
720         gsm_trace_header(p_m_mISDNport, this, MNCC_DISC_REQ, DIRECTION_OUT);
721         if (p_m_mISDNport->tones) {
722                 mncc->fields |= MNCC_F_PROGRESS;
723                 mncc->progress.coding = 3; /* GSM */
724                 mncc->progress.location = 1;
725                 mncc->progress.descr = 8;
726                 add_trace("progress", "coding", "%d", mncc->progress.coding);
727                 add_trace("progress", "location", "%d", mncc->progress.location);
728                 add_trace("progress", "descr", "%d", mncc->progress.descr);
729         }
730         mncc->fields |= MNCC_F_CAUSE;
731         mncc->cause.coding = 3;
732         mncc->cause.location = param->disconnectinfo.location;
733         mncc->cause.value = param->disconnectinfo.cause;
734         add_trace("cause", "coding", "%d", mncc->cause.coding);
735         add_trace("cause", "location", "%d", mncc->cause.location);
736         add_trace("cause", "value", "%d", mncc->cause.value);
737         end_trace();
738         send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
739
740         new_state(PORT_STATE_OUT_DISCONNECT);
741
742         if (p_m_mISDNport->tones && !p_m_g_tch_connected) { /* only if ... */
743                 gsm_trace_header(p_m_mISDNport, this, MNCC_FRAME_RECV, DIRECTION_OUT);
744                 end_trace();
745                 mncc = create_mncc(MNCC_FRAME_RECV, p_m_g_callref);
746                 send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
747                 p_m_g_tch_connected = 1;
748         }
749 }
750
751
752 /* MESSAGE_RELEASE */
753 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
754 {
755         struct gsm_mncc *mncc;
756
757         /* send release */
758         mncc = create_mncc(MNCC_REL_REQ, p_m_g_callref);
759         gsm_trace_header(p_m_mISDNport, this, MNCC_REL_REQ, DIRECTION_OUT);
760         mncc->fields |= MNCC_F_CAUSE;
761         mncc->cause.coding = 3;
762         mncc->cause.location = param->disconnectinfo.location;
763         mncc->cause.value = param->disconnectinfo.cause;
764         add_trace("cause", "coding", "%d", mncc->cause.coding);
765         add_trace("cause", "location", "%d", mncc->cause.location);
766         add_trace("cause", "value", "%d", mncc->cause.value);
767         end_trace();
768         send_and_free_mncc(p_m_g_lcr_gsm, mncc->msg_type, mncc);
769
770         new_state(PORT_STATE_RELEASE);
771         trigger_work(&p_m_g_delete);
772         return;
773 }
774
775 /*
776  * endpoint sends messages to the port
777  */
778 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
779 {
780         if (PmISDN::message_epoint(epoint_id, message_id, param))
781                 return(1);
782
783         switch(message_id) {
784                 case MESSAGE_NOTIFY: /* display and notifications */
785                 message_notify(epoint_id, message_id, param);
786                 break;
787
788 //              case MESSAGE_FACILITY: /* facility message */
789 //              message_facility(epoint_id, message_id, param);
790 //              break;
791
792                 case MESSAGE_PROCEEDING: /* message not handles */
793                 break;
794
795                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
796                 if (p_state!=PORT_STATE_IN_PROCEEDING)
797                         break;
798                 message_alerting(epoint_id, message_id, param);
799                 if (p_m_g_notify_pending) {
800                         /* send pending notify message during connect */
801                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
802                         message_free(p_m_g_notify_pending);
803                         p_m_g_notify_pending = NULL;
804                 }
805                 break;
806
807                 case MESSAGE_CONNECT: /* call of endpoint is connected */
808                 if (p_state!=PORT_STATE_IN_PROCEEDING
809                  && p_state!=PORT_STATE_IN_ALERTING)
810                         break;
811                 message_connect(epoint_id, message_id, param);
812                 if (p_m_g_notify_pending) {
813                         /* send pending notify message during connect */
814                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_g_notify_pending->type, &p_m_g_notify_pending->param);
815                         message_free(p_m_g_notify_pending);
816                         p_m_g_notify_pending = NULL;
817                 }
818                 break;
819
820                 case MESSAGE_DISCONNECT: /* call has been disconnected */
821                 if (p_state!=PORT_STATE_IN_PROCEEDING
822                  && p_state!=PORT_STATE_IN_ALERTING
823                  && p_state!=PORT_STATE_OUT_SETUP
824                  && p_state!=PORT_STATE_OUT_OVERLAP
825                  && p_state!=PORT_STATE_OUT_PROCEEDING
826                  && p_state!=PORT_STATE_OUT_ALERTING
827                  && p_state!=PORT_STATE_CONNECT
828                  && p_state!=PORT_STATE_CONNECT_WAITING)
829                         break;
830                 message_disconnect(epoint_id, message_id, param);
831                 break;
832
833                 case MESSAGE_RELEASE: /* release isdn port */
834                 if (p_state==PORT_STATE_RELEASE)
835                         break;
836                 message_release(epoint_id, message_id, param);
837                 break;
838
839         }
840
841         return(0);
842 }
843
844 /* deletes only if l3id is release, otherwhise it will be triggered then */
845 static int delete_event(struct lcr_work *work, void *instance, int index)
846 {
847         class Pgsm *gsmport = (class Pgsm *)instance;
848
849         delete gsmport;
850
851         return 0;
852 }
853
854 /*
855  * handler of bchannel events
856  */
857 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index)
858 {
859         class Pgsm *gsmport = (class Pgsm *)instance;
860         int ret;
861         unsigned char buffer[2048+MISDN_HEADER_LEN];
862         struct mISDNhead *hh = (struct mISDNhead *)buffer;
863
864         /* handle message from bchannel */
865         if (gsmport->p_m_g_gsm_b_sock > -1) {
866                 ret = recv(gsmport->p_m_g_gsm_b_sock, buffer, sizeof(buffer), 0);
867                 if (ret >= (int)MISDN_HEADER_LEN) {
868                         switch(hh->prim) {
869                                 /* we don't care about confirms, we use rx data to sync tx */
870                                 case PH_DATA_CNF:
871                                 break;
872                                 /* we receive audio data, we respond to it AND we send tones */
873                                 case PH_DATA_IND:
874                                 gsmport->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
875                                 break;
876                                 case PH_ACTIVATE_IND:
877                                 gsmport->p_m_g_gsm_b_active = 1;
878                                 break;
879                                 case PH_DEACTIVATE_IND:
880                                 gsmport->p_m_g_gsm_b_active = 0;
881                                 break;
882                         }
883                 } else {
884                         if (ret < 0 && errno != EWOULDBLOCK)
885                                 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
886                 }
887         }
888
889         return 0;
890 }
891
892 int gsm_exit(int rc)
893 {
894         return(rc);
895 }
896
897 int gsm_init(void)
898 {
899         /* seed the PRNG */
900         srand(time(NULL));
901
902         return 0;
903 }
904
905 /*
906  * MNCC interface
907  */
908
909 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
910 {
911         struct mncc_q_entry *qe;
912
913         qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
914         if (!qe)
915                 return -ENOMEM;
916
917         qe->next = NULL;
918         qe->len = len;
919         memcpy(qe->data, mncc, len);
920
921         /* in case of empty list ... */
922         if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
923                 /* the list head and tail both point to the new qe */
924                 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
925         } else {
926                 /* append to tail of list */
927                 lcr_gsm->mncc_q_tail->next = qe;
928                 lcr_gsm->mncc_q_tail = qe;
929         }
930
931         lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
932
933         return 0;
934 }
935
936 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
937 {
938         struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
939         if (!qe)
940                 return NULL;
941
942         /* dequeue the successfully sent message */
943         lcr_gsm->mncc_q_hd = qe->next;
944         if (!qe)
945                 return NULL;
946         if (qe == lcr_gsm->mncc_q_tail)
947                 lcr_gsm->mncc_q_tail = NULL;
948
949         return qe;
950 }
951
952 /* routine called by LCR code if it wants to send a message to OpenBSC */
953 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
954 {
955         int len = 0;
956
957         /* FIXME: the caller should provide this */
958         switch (msg_type) {
959         case GSM_TCHF_FRAME:
960                 len = sizeof(struct gsm_data_frame) + 33;
961                 break;
962         default:
963                 len = sizeof(struct gsm_mncc);
964                 break;
965         }
966                 
967         return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
968 }
969
970 /* close MNCC socket */
971 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
972 {
973         class Port *port;
974         class Pgsm *pgsm = NULL;
975         struct lcr_msg *message;
976
977         PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
978         close(lfd->fd);
979         unregister_fd(lfd);
980         lfd->fd = -1;
981
982         /* free all the calls that were running through the MNCC interface */
983         port = port_first;
984         while(port) {
985                 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_GSM) {
986                         pgsm = (class Pgsm *)port;
987                         if (pgsm->p_m_g_lcr_gsm == lcr_gsm) {
988                                 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
989                                 message->param.disconnectinfo.cause = 27; // temp. unavail.
990                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
991                                 message_put(message);
992                                 pgsm->new_state(PORT_STATE_RELEASE);
993                                 trigger_work(&pgsm->p_m_g_delete);
994                         }
995                 }
996                 port = port->next;
997         }
998
999         /* flush the queue */
1000         while (mncc_q_dequeue(lcr_gsm))
1001                 ;
1002
1003         /* start the re-connect timer */
1004         schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1005
1006         return 0;
1007 }
1008
1009 /* write to OpenBSC via MNCC socket */
1010 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1011 {
1012         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1013         struct mncc_q_entry *qe, *qe2;
1014         int rc;
1015
1016         while (1) {
1017                 qe = lcr_gsm->mncc_q_hd;
1018                 if (!qe) {
1019                         lfd->when &= ~LCR_FD_WRITE;
1020                         break;
1021                 }
1022                 rc = write(lfd->fd, qe->data, qe->len);
1023                 if (rc == 0)
1024                         return mncc_fd_close(lcr_gsm, lfd);
1025                 if (rc < 0)
1026                         return rc;
1027                 if (rc < (int)qe->len)
1028                         return -1;
1029                 /* dequeue the successfully sent message */
1030                 qe2 = mncc_q_dequeue(lcr_gsm);
1031                 assert(qe == qe2);
1032                 free(qe);
1033         }
1034         return 0;
1035 }
1036
1037 /* read from OpenBSC via MNCC socket */
1038 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1039 {
1040         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1041         int rc;
1042         static char buf[sizeof(struct gsm_mncc)+1024];
1043         struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1044
1045         memset(buf, 0, sizeof(buf));
1046         rc = recv(lfd->fd, buf, sizeof(buf), 0);
1047         if (rc == 0)
1048                 return mncc_fd_close(lcr_gsm, lfd);
1049         if (rc < 0)
1050                 return rc;
1051
1052         /* Hand the MNCC message into LCR */
1053         switch (lcr_gsm->type) {
1054 #ifdef WITH_GSM_BS
1055         case LCR_GSM_TYPE_NETWORK:
1056                 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1057 #endif
1058 #ifdef WITH_GSM_MS
1059         case LCR_GSM_TYPE_MS:
1060                 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1061 #endif
1062         default:
1063                 return 0;
1064         }
1065 }
1066
1067 /* file descriptor callback if we can read or write form MNCC socket */
1068 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1069 {
1070         int rc = 0;
1071
1072         if (what & LCR_FD_READ)
1073                 rc = mncc_fd_read(lfd, inst, idx);
1074         if (rc < 0)
1075                 return rc;
1076
1077         if (what & LCR_FD_WRITE)
1078                 rc = mncc_fd_write(lfd, inst, idx);
1079
1080         return rc;
1081 }
1082
1083 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1084 {
1085         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1086         int fd, rc;
1087
1088         lcr_gsm->mncc_lfd.fd = -1;
1089
1090         fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1091         if (fd < 0) {
1092                 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1093                 return fd;
1094         }
1095
1096         rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1097                      sizeof(lcr_gsm->sun));
1098         if (rc < 0) {
1099                 PERROR("Could not connect to MNCC socket %s, "
1100                         "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1101                         SOCKET_RETRY_TIMER);
1102                 close(fd);
1103                 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1104         } else {
1105                 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1106                 lcr_gsm->mncc_lfd.fd = fd;
1107                 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);
1108         }
1109
1110         return 0;
1111 }
1112