#include "datatypes.h"
+#include <termios.h>
+#include <unistd.h>
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct sockaddr_un local;
local.sun_family = AF_UNIX;
- strcpy(local.sun_path, path); // TODO: strlen ???
+ if(sizeof(local.sun_path) <= strlen(path)) {
+ log_printf(ERROR, "socket path is to long (max %d)", sizeof(local.sun_path)-1);
+ return -1;
+ }
+ strcpy(local.sun_path, path);
unlink(local.sun_path);
int len = SUN_LEN(&local);
int ret = bind(fd, (struct sockaddr*)&local, len);
return ret;
}
-int handle_command(const char* cmd, int fd, cmd_t** cmd_q, client_t* client_lst)
+int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst)
{
+ log_printf(DEBUG, "processing command from %d", fd);
+
if(!cmd_q || !cmd)
return -1;
}
else {
log_printf(WARNING, "unknown command '%s'", cmd);
- return 1;
+ return 0;
}
char* param = strchr(cmd, ' ');
if(param)
return 0;
}
-int process_cmd(int fd, cmd_t **cmd_q, client_t* client_lst)
+int process_door(const char* str, int door_fd, cmd_t **cmd_q, client_t* client_lst)
{
- log_printf(DEBUG, "processing command from %d", fd);
+ log_printf(NOTICE, "door-firmware: %s", str);
+
+ int cmd_fd = -1;
+ if(cmd_q && (*cmd_q)) {
+ cmd_fd = (*cmd_q)->fd;
+ send_response(cmd_fd, str);
+ }
+
+ if(!strncmp(str, "Status:", 7)) {
+ client_t* client;
+ for(client = client_lst; client; client = client->next)
+ if(client->status_listener && client->fd != cmd_fd)
+ send_response(client->fd, str);
+ }
+
+ cmd_pop(cmd_q);
- static char buffer[100];
- int ret = 0;
- do { // TODO: replace this whith a actually working readline
- memset(buffer, 0, 100);
- ret = recv(fd, buffer, sizeof(buffer), 0);
- if(!ret)
- return 1;
- char* saveptr;
- char* tok = strtok_r(buffer, "\n\r", &saveptr);
- do {
- ret = handle_command(tok, fd, cmd_q, client_lst);
- if(ret < 0)
- return ret;
- } while(tok = strtok_r(NULL, "\n\r", &saveptr));
- } while (ret == -1 && errno == EINTR);
-
return 0;
}
-int process_door(int door_fd, cmd_t **cmd_q, client_t* client_lst)
+int nonblock_readline(read_buffer_t buffer, int fd, cmd_t** cmd_q, client_t* client_lst, int (*cb)(const char*, int, cmd_t**, client_t*))
{
- log_printf(DEBUG, "processing data from door (fd=%d)", door_fd);
-
- static char buffer[100];
int ret = 0;
- do { // TODO: replace this whith a actually working readline
- memset(buffer, 0, 100);
- ret = read(door_fd, buffer, sizeof(buffer));
- if(!ret)
+ for(;;) {
+ ret = read(fd, &buffer.buf[buffer.offset], 1);
+ if(!ret)
return 2;
- char* saveptr;
- char* tok = strtok_r(buffer, "\n\r", &saveptr);
- do {
- if(!cmd_q || !(*cmd_q))
- break;
-
- send_response((*cmd_q)->fd, tok);
+ else if(ret == -1 && errno == EAGAIN)
+ return 0;
+ else if(ret < 0)
+ break;
- if(!strncmp(tok, "Status:", 7)) {
- client_t* client;
- for(client = client_lst; client; client = client->next)
- if(client->status_listener)
- send_response(client->fd, tok);
- }
+ if(buffer.buf[buffer.offset] == '\n') {
+ buffer.buf[buffer.offset] = 0;
+ ret = (cb)(buffer.buf, fd, cmd_q, client_lst);
+ break;
+ }
- cmd_pop(cmd_q);
- } while(tok = strtok_r(NULL, "\n\r", &saveptr));
- } while (ret == -1 && errno == EINTR);
+ buffer.offset++;
+ if(buffer.offset >= sizeof(buffer.buf)) {
+ log_printf(DEBUG, "string too long (fd=%d)", fd);
+ return 0;
+ }
+ }
- return 0;
+ return ret;
}
-
int main_loop(int door_fd, int cmd_listen_fd)
{
- log_printf(INFO, "entering main loop");
+ log_printf(NOTICE, "entering main loop");
fd_set readfds, tmpfds;
FD_ZERO(&readfds);
cmd_t* cmd_q = NULL;
client_t* client_lst = NULL;
+ read_buffer_t door_buffer;
+ door_buffer.offset = 0;
+
int sig_fd = signal_init();
if(sig_fd < 0)
return -1;
if(!ret || ret == -1)
continue;
- if(signal_handle()) {
- return_value = 1;
- break;
+ if(FD_ISSET(sig_fd, &tmpfds)) {
+ if(signal_handle()) {
+ return_value = 1;
+ break;
+ }
}
-
+
if(FD_ISSET(door_fd, &tmpfds)) {
- return_value = process_door(door_fd, &cmd_q, client_lst);
+ return_value = nonblock_readline(door_buffer, door_fd, &cmd_q, client_lst, process_door);
if(return_value)
break;
}
log_printf(DEBUG, "new command connection (fd=%d)", new_fd);
FD_SET(new_fd, &readfds);
max_fd = (max_fd < new_fd) ? new_fd : max_fd;
+ fcntl(new_fd, F_SETFL, O_NONBLOCK);
client_add(&client_lst, new_fd);
}
client_t* lst = client_lst;
while(lst) {
if(FD_ISSET(lst->fd, &tmpfds)) {
- return_value = process_cmd(lst->fd, &cmd_q, client_lst);
- if(return_value == 1) {
+ return_value = nonblock_readline(lst->buffer, lst->fd, &cmd_q, client_lst, process_cmd);
+ if(return_value == 2) {
log_printf(DEBUG, "removing closed command connection (fd=%d)", lst->fd);
client_t* deletee = lst;
lst = lst->next;
return return_value;
}
+int setup_tty(int fd)
+{
+ struct termios tmio;
+
+ int ret = tcgetattr(fd, &tmio);
+ if(ret) {
+ log_printf(ERROR, "Error on tcgetattr(): %s", strerror(errno));
+ return ret;
+ }
+
+ ret = cfsetospeed(&tmio, B9600);
+ if(ret) {
+ log_printf(ERROR, "Error on cfsetospeed(): %s", strerror(errno));
+ return ret;
+ }
+
+ ret = cfsetispeed(&tmio, B9600);
+ if(ret) {
+ log_printf(ERROR, "Error on cfsetispeed(): %s", strerror(errno));
+ return ret;
+ }
+
+ tmio.c_lflag &= ~ECHO;
+
+ ret = tcsetattr(fd, TCSANOW, &tmio);
+ if(ret) {
+ log_printf(ERROR, "Error on tcsetattr(): %s", strerror(errno));
+ return ret;
+ }
+
+ ret = tcflush(fd, TCIFLUSH);
+ if(ret) {
+ log_printf(ERROR, "Error on tcflush(): %s", strerror(errno));
+ return ret;
+ }
+
+ fd_set fds;
+ struct timeval tv;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 50000;
+ for(;;) {
+ ret = select(fd+1, &fds, NULL, NULL, &tv);
+ if(ret > 0) {
+ char buffer[100];
+ ret = read(fd, buffer, sizeof(buffer));
+ }
+ else
+ break;
+ }
+
+ return 0;
+}
+
int main(int argc, char* argv[])
{
log_init();
fclose(pid_file);
}
- int door_fd = open(opt.door_dev_, O_RDWR);
- if(door_fd < 0) {
- log_printf(ERROR, "unable to open %s: %s", opt.door_dev_, strerror(errno));
- options_clear(&opt);
- log_close();
- exit(-1);
- }
-
int cmd_listen_fd = init_command_socket(opt.command_sock_);
if(cmd_listen_fd < 0) {
- close(door_fd);
options_clear(&opt);
log_close();
exit(-1);
}
+
+ int door_fd = 0;
+ for(;;) {
+ door_fd = open(opt.door_dev_, O_RDWR | O_NOCTTY | O_NONBLOCK);
+ if(door_fd < 0)
+ ret = 2;
+ else {
+ ret = setup_tty(door_fd);
+ if(ret)
+ ret = 2;
+ else
+ ret = main_loop(door_fd, cmd_listen_fd);
+ }
- ret = main_loop(door_fd, cmd_listen_fd);
+ if(ret == 2) {
+ log_printf(ERROR, "%s error, trying to reopen in 5 seconds..", opt.door_dev_);
+ if(door_fd > 0)
+ close(door_fd);
+ sleep(5);
+ }
+ else
+ break;
+ }
close(cmd_listen_fd);
- close(door_fd);
+ if(door_fd > 0)
+ close(door_fd);
if(!ret)
log_printf(NOTICE, "normal shutdown");
else if(ret < 0)
log_printf(NOTICE, "shutdown after error");
- else if(ret == 2)
- log_printf(ERROR, "shutdown after %s read error", opt.door_dev_);
else
log_printf(NOTICE, "shutdown after signal");