+ }
+ return 0;
+}
+
+/*
+ * release all calls (due to broken socket)
+ */
+static void release_all_calls(void)
+{
+ struct chan_call *call;
+
+again:
+ call = call_first;
+ while(call) {
+ /* no ast, so we may directly free call */
+ if (!call->ast) {
+ CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
+ free_call(call);
+ goto again;
+ }
+ /* already in release process */
+ if (call->state == CHAN_LCR_STATE_RELEASE) {
+ call = call->next;
+ continue;
+ }
+ /* release or queue release */
+ call->ref = 0;
+ call->state = CHAN_LCR_STATE_RELEASE;
+ if (!call->pbx_started) {
+ CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
+ ast_hangup(call->ast); // call will be destroyed here
+ goto again;
+ }
+ CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
+ if (!wake_global) {
+ wake_global = 1;
+ char byte = 0;
+ write(wake_pipe[1], &byte, 1);
+ }
+ strcpy(call->queue_string, "H");
+ call = call->next;
+ }
+
+ /* release all bchannels */
+ while(bchannel_first)
+ free_bchannel(bchannel_first);
+}
+
+void close_socket(void);
+
+/* asterisk handler
+ * warning! not thread safe
+ * returns -1 for socket error, 0 for no work, 1 for work
+ */
+static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
+{
+ int len;
+ struct admin_list *admin;
+ struct admin_message msg;
+
+ lock_debug("handle+");
+ if ((what & LCR_FD_READ)) {
+ /* read from socket */
+ len = read(lcr_sock, &msg, sizeof(msg));
+ if (len == 0) {
+ CERROR(NULL, NULL, "Socket closed.\n");
+ error:
+ CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
+ close_socket();
+ release_all_calls();
+ schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
+ lock_debug("handle-");
+ return 0;
+ }
+ if (len > 0) {
+ if (len != sizeof(msg)) {
+ CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
+ goto error;
+ }
+ if (msg.message != ADMIN_MESSAGE) {
+ CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
+ goto error;
+ }
+ receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
+ } else {
+ CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
+ goto error;
+ }
+ }
+
+ if ((what & LCR_FD_WRITE)) {
+ /* write to socket */
+ if (!admin_first) {
+ socket_fd.when &= ~LCR_FD_WRITE;
+ lock_debug("handle-");
+ return 0;
+ }
+ admin = admin_first;
+ len = write(lcr_sock, &admin->msg, sizeof(msg));
+ if (len == 0) {
+ CERROR(NULL, NULL, "Socket closed.\n");
+ goto error;
+ }
+ if (len > 0) {
+ if (len != sizeof(msg)) {
+ CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
+ goto error;
+ }
+ /* free head */
+ admin_first = admin->next;
+ free(admin);
+ global_change = 1;
+ } else {
+ CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
+ goto error;
+ }
+ }
+
+ lock_debug("handle-");
+ return 0;
+}
+
+/*
+ * open and close socket and thread
+ */
+int open_socket(void)
+{
+ int conn;
+ struct sockaddr_un sock_address;
+ union parameter param;
+
+ /* open socket */
+ if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
+ CERROR(NULL, NULL, "Failed to create socket.\n");
+ return lcr_sock;
+ }
+
+ /* set socket address and name */
+ memset(&sock_address, 0, sizeof(sock_address));
+ sock_address.sun_family = PF_UNIX;
+ sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
+
+ /* connect socket */
+ if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
+ close(lcr_sock);
+ lcr_sock = -1;
+ CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
+ return conn;
+ }
+
+ /* register socket fd */
+ memset(&socket_fd, 0, sizeof(socket_fd));
+ socket_fd.fd = lcr_sock;
+ register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
+
+ /* enque hello message */
+ memset(¶m, 0, sizeof(param));
+ strcpy(param.hello.application, "asterisk");
+ send_message(MESSAGE_HELLO, 0, ¶m);
+
+ return lcr_sock;
+}
+
+void close_socket(void)
+{
+ struct admin_list *admin, *temp;
+
+ unregister_fd(&socket_fd);
+
+ /* flush pending messages */
+ admin = admin_first;
+ while(admin) {
+ temp = admin;
+ admin = admin->next;
+ free(temp);
+ }
+ admin_first = NULL;
+
+ /* close socket */
+ if (lcr_sock >= 0)
+ close(lcr_sock);
+ lcr_sock = -1;
+ global_change = 1;
+}
+
+
+/* sending queue to asterisk */
+static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
+{
+ char byte;
+
+ lock_debug("wake+");
+ read(wake_pipe[0], &byte, 1);
+
+ wake_global = 0;
+ lock_debug("wake-");
+
+ return 0;
+}
+
+static void handle_queue()
+{
+ struct chan_call *call;
+ struct ast_channel *ast;
+ struct ast_frame fr;
+ char *p;
+
+ call = call_first;
+ while(call) {
+ p = call->queue_string;
+ ast = call->ast;
+ if (*p && ast) {
+ lock_debug("A1+");
+ ast_channel_lock(ast);
+ lock_debug("A1-");
+ while(*p) {
+ switch (*p) {
+ case 'T':
+ CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
+ ast_queue_control(ast, AST_CONTROL_PROGRESS);
+ break;
+ case 'P':
+ CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
+ ast_queue_control(ast, AST_CONTROL_PROCEEDING);
+ break;
+ case 'R':
+ CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
+ ast_queue_control(ast, AST_CONTROL_RINGING);
+ ast_setstate(ast, AST_STATE_RINGING);
+ break;
+ case 'N':
+ CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
+ ast_queue_control(ast, AST_CONTROL_ANSWER);
+ break;
+ case 'H':
+ CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
+ ast_queue_hangup(ast);
+ break;
+ case '1': case '2': case '3': case 'A':
+ case '4': case '5': case '6': case 'B':
+ case '7': case '8': case '9': case 'C':
+ case '*': case '0': case '#': case 'D':
+ CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
+ /* send digit to asterisk */
+ memset(&fr, 0, sizeof(fr));
+
+ #ifdef LCR_FOR_ASTERISK
+ fr.frametype = AST_FRAME_DTMF_BEGIN;
+ #endif
+
+ #ifdef LCR_FOR_CALLWEAVER
+ fr.frametype = AST_FRAME_DTMF;
+ #endif
+
+ fr.subclass = *p;
+ fr.delivery = ast_tv(0, 0);
+ ast_queue_frame(ast, &fr);
+
+ #ifdef LCR_FOR_ASTERISK
+ fr.frametype = AST_FRAME_DTMF_END;
+ ast_queue_frame(ast, &fr);
+ #endif
+
+ break;
+ default:
+ CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
+ }
+ p++;
+ }
+ call->queue_string[0] = '\0';
+ ast_channel_unlock(ast);
+ lock_debug("a1");
+ }
+ call = call->next;
+ }
+}
+
+static int handle_retry(struct lcr_timer *timer, void *instance, int index)
+{
+ CDEBUG(NULL, NULL, "Retry to open socket.\n");
+ if (open_socket() < 0)
+ schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
+
+ return 0;
+}
+
+void lock_chan(void)
+{
+ lock_debug("C+");
+ ast_mutex_lock(&chan_lock);
+ lock_debug("C-");
+}
+
+void unlock_chan(void)
+{
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("c");
+}
+
+/* chan_lcr thread */
+static void *chan_thread(void *arg)
+{
+ if (pipe(wake_pipe) < 0) {
+ CERROR(NULL, NULL, "Failed to open pipe.\n");
+ return NULL;
+ }
+ memset(&wake_fd, 0, sizeof(wake_fd));
+ wake_fd.fd = wake_pipe[0];
+ register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
+
+ memset(&socket_retry, 0, sizeof(socket_retry));
+ add_timer(&socket_retry, handle_retry, NULL, 0);
+
+ bchannel_pid = getpid();
+
+ /* open socket the first time */
+ handle_retry(NULL, NULL, 0);
+
+ lock_debug("A2+");
+ ast_mutex_lock(&chan_lock);
+ lock_debug("A2-");
+
+ while(!quit) {
+ handle_queue();
+ select_main(0, &global_change, lock_chan, unlock_chan);
+ }
+
+ close_socket();
+
+ del_timer(&socket_retry);
+
+ unregister_fd(&wake_fd);
+ close(wake_pipe[0]);
+ close(wake_pipe[1]);
+
+ CERROR(NULL, NULL, "Thread exit.\n");
+
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a2");
+
+ return NULL;
+}
+
+/*
+ * new asterisk instance
+ */
+static
+struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
+{
+ char exten[256], *dial, *interface, *opt;
+ struct ast_channel *ast;
+ struct chan_call *call;
+
+ lock_debug("A3+");
+ ast_mutex_lock(&chan_lock);
+ lock_debug("A3-");
+ CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
+
+ /* if socket is closed */
+ if (lcr_sock < 0) {
+ CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a3");
+ return NULL;
+ }
+
+ /* create call instance */
+ call = alloc_call();
+ if (!call) {
+ /* failed to create instance */
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a3");
+ return NULL;
+ }
+
+ /* create asterisk channel instrance */
+
+ #ifdef LCR_FOR_ASTERISK
+ ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
+ #endif
+
+ #ifdef LCR_FOR_CALLWEAVER
+ ast = ast_channel_alloc(1);
+ #endif
+
+ if (!ast) {
+ CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
+ free_call(call);
+ /* failed to create instance */
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a3");
+ return NULL;
+ }
+ ast->tech = &lcr_tech;
+ ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
+ /* configure channel */
+ ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
+ ast->readformat = ast->rawreadformat = ast->nativeformats;
+ ast->writeformat = ast->rawwriteformat = ast->nativeformats;
+ ast->priority = 1;
+ ast->hangupcause = 0;
+
+ /* link together */
+ call->ast = ast;
+ ast->tech_pvt = call;
+ ast->fds[0] = call->pipe[0];
+ call->pbx_started = 0;
+ /* set state */
+ call->state = CHAN_LCR_STATE_OUT_PREPARE;
+
+ /*
+ * Extract interface, dialstring, options from data.
+ * Formats can be:
+ * <dialstring>
+ * <interface>/<dialstring>
+ * <interface>/<dialstring>/options
+ */
+ strncpy(exten, (char *)data, sizeof(exten)-1);
+ exten[sizeof(exten)-1] = '\0';
+ if ((dial = strchr(exten, '/'))) {
+ *dial++ = '\0';
+ interface = exten;
+ if ((opt = strchr(dial, '/')))
+ *opt++ = '\0';
+ else
+ opt = "";
+ } else {
+ dial = exten;
+ interface = "";
+ opt = "";
+ }
+ strncpy(call->interface, interface, sizeof(call->interface)-1);
+ strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
+ apply_opt(call, (char *)opt);
+
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a3");
+ return ast;
+}
+
+/*
+ * call from asterisk
+ */
+static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
+{
+ union parameter newparam;
+ struct chan_call *call;
+
+ lock_debug("A4+");
+ ast_mutex_lock(&chan_lock);
+ lock_debug("A4-");
+ call = ast->tech_pvt;
+
+ #ifdef LCR_FOR_CALLWEAVER
+ ast->type = "LCR";
+ snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
+ #endif
+
+ if (!call) {
+ CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a4");
+ return -1;
+ }
+
+ CDEBUG(NULL, ast, "Received call from Asterisk.\n");
+
+ /* pbx process is started */
+ call->pbx_started = 1;
+ /* send MESSAGE_NEWREF */
+ memset(&newparam, 0, sizeof(union parameter));
+ newparam.direction = 0; /* request from app */
+ send_message(MESSAGE_NEWREF, 0, &newparam);
+
+ /* set hdlc if capability requires hdlc */
+ if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
+ || ast->transfercapability == INFO_BC_DATARESTRICTED
+ || ast->transfercapability == INFO_BC_VIDEO)
+ call->hdlc = 1;
+ /* if hdlc is forced by option, we change transcap to data */
+ if (call->hdlc
+ && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
+ && ast->transfercapability != INFO_BC_DATARESTRICTED
+ && ast->transfercapability != INFO_BC_VIDEO)
+ ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
+
+ call->cid_num[0] = 0;
+ call->cid_name[0] = 0;
+ call->cid_rdnis[0] = 0;
+
+ if (ast->cid.cid_num) if (ast->cid.cid_num[0])
+ strncpy(call->cid_num, ast->cid.cid_num,
+ sizeof(call->cid_num)-1);
+
+ if (ast->cid.cid_name) if (ast->cid.cid_name[0])
+ strncpy(call->cid_name, ast->cid.cid_name,
+ sizeof(call->cid_name)-1);
+ if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
+ strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
+ sizeof(call->cid_rdnis)-1);
+
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a4");
+ return 0;
+}
+
+static void send_digit_to_chan(struct ast_channel * ast, char digit )
+{
+ static const char* dtmf_tones[] = {
+ "!941+1336/100,!0/100", /* 0 */
+ "!697+1209/100,!0/100", /* 1 */
+ "!697+1336/100,!0/100", /* 2 */
+ "!697+1477/100,!0/100", /* 3 */
+ "!770+1209/100,!0/100", /* 4 */
+ "!770+1336/100,!0/100", /* 5 */
+ "!770+1477/100,!0/100", /* 6 */
+ "!852+1209/100,!0/100", /* 7 */
+ "!852+1336/100,!0/100", /* 8 */
+ "!852+1477/100,!0/100", /* 9 */
+ "!697+1633/100,!0/100", /* A */
+ "!770+1633/100,!0/100", /* B */
+ "!852+1633/100,!0/100", /* C */
+ "!941+1633/100,!0/100", /* D */
+ "!941+1209/100,!0/100", /* * */
+ "!941+1477/100,!0/100" }; /* # */
+
+ if (digit >= '0' && digit <='9')
+ ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
+ else if (digit >= 'A' && digit <= 'D')
+ ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
+ else if (digit == '*')
+ ast_playtones_start(ast,0,dtmf_tones[14], 0);
+ else if (digit == '#')
+ ast_playtones_start(ast,0,dtmf_tones[15], 0);
+ else {
+ /* not handled */
+ ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
+ "'%c' for '%s'\n", digit, ast->name);
+ }
+}
+
+#ifdef LCR_FOR_ASTERISK
+static int lcr_digit_begin(struct ast_channel *ast, char digit)
+#endif
+#ifdef LCR_FOR_CALLWEAVER
+static int lcr_digit(struct ast_channel *ast, char digit)
+#endif
+{
+ struct chan_call *call;
+ union parameter newparam;
+ char buf[]="x";
+
+#ifdef LCR_FOR_CALLWEAVER
+ int inband_dtmf = 0;
+#endif
+
+ /* only pass IA5 number space */
+ if (digit > 126 || digit < 32)
+ return 0;
+
+ lock_debug("A5+");
+ ast_mutex_lock(&chan_lock);
+ lock_debug("A5-");
+ call = ast->tech_pvt;
+ if (!call) {
+ CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a5");
+ return -1;
+ }
+
+ CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
+
+ /* send information or queue them */
+ if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
+ CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
+ memset(&newparam, 0, sizeof(union parameter));
+ if (call->keypad) {
+ newparam.information.keypad[0] = digit;
+ newparam.information.keypad[1] = '\0';
+ } else {
+ newparam.information.id[0] = digit;
+ newparam.information.id[1] = '\0';
+ }
+ send_message(MESSAGE_INFORMATION, call->ref, &newparam);
+ } else
+ if (!call->ref
+ && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
+ CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
+ *buf = digit;
+ strncat(call->dialque, buf, strlen(call->dialque)-1);
+ }
+
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a5");
+
+#ifdef LCR_FOR_ASTERISK
+ return 0;
+}
+
+static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
+{
+ int inband_dtmf = 0;
+ struct chan_call *call;
+#endif
+
+ lock_debug("A6+");
+ ast_mutex_lock(&chan_lock);
+ lock_debug("A6-");
+
+ call = ast->tech_pvt;
+
+ if (!call) {
+ CERROR(NULL, ast,
+ "Received digit from Asterisk, "
+ "but no call instance exists.\n");
+ ast_mutex_unlock(&chan_lock);
+ lock_debug("a6");
+ return -1;
+ }
+
+ CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);