/* Copyright (C) 2017-2018 Fredrik Öhrström This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include"util.h" #include"serial.h" #include #include #include #include #include #include #include #include #include #include #include static int openSerialTTY(const char *tty, int baud_rate); struct SerialDeviceTTY; struct SerialCommunicationManagerImp : public SerialCommunicationManager { SerialCommunicationManagerImp(time_t exit_after_seconds); ~SerialCommunicationManagerImp() { } unique_ptr createSerialDeviceTTY(string dev, int baud_rate); void listenTo(SerialDevice *sd, function cb); void stop(); void waitForStop(); bool isRunning(); void opened(SerialDeviceTTY *sd); void closed(SerialDeviceTTY *sd); private: void *eventLoop(); static void *startLoop(void *); bool running_; pthread_t thread_; int max_fd_; vector devices_; time_t start_time_; time_t exit_after_seconds_; }; struct SerialDeviceImp : public SerialDevice { private: function on_data_; friend struct SerialCommunicationManagerImp; }; struct SerialDeviceTTY : public SerialDeviceImp { SerialDeviceTTY(string device, int baud_rate, SerialCommunicationManagerImp *manager); ~SerialDeviceTTY(); bool open(bool fail_if_not_ok); void close(); bool send(vector &data); int receive(vector *data); int fd() { return fd_; } SerialCommunicationManager *manager() { return manager_; } private: string device_; int baud_rate_ {}; int fd_ {}; pthread_mutex_t write_lock_ = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t read_lock_ = PTHREAD_MUTEX_INITIALIZER; SerialCommunicationManagerImp *manager_; }; SerialDeviceTTY::SerialDeviceTTY(string device, int baud_rate, SerialCommunicationManagerImp *manager) { device_ = device; baud_rate_ = baud_rate; manager_ = manager; } SerialDeviceTTY::~SerialDeviceTTY() { close(); } bool SerialDeviceTTY::open(bool fail_if_not_ok) { bool ok = checkCharacterDeviceExists(device_.c_str(), fail_if_not_ok); if (!ok) return false; fd_ = openSerialTTY(device_.c_str(), baud_rate_); if (fd_ == -1) { if (fail_if_not_ok) { error("Could not open %s with %d baud N81\n", device_.c_str(), baud_rate_); } else { return false; } } manager_->opened(this); verbose("(serial) opened %s\n", device_.c_str()); return true; } void SerialDeviceTTY::close() { ::flock(fd_, LOCK_UN); ::close(fd_); fd_ = -1; manager_->closed(this); verbose("(serial) closed %s\n", device_.c_str()); } bool SerialDeviceTTY::send(vector &data) { if (data.size() == 0) return true; pthread_mutex_lock(&write_lock_); bool rc = true; int n = data.size(); int written = 0; while (true) { int nw = write(fd_, &data[written], n-written); if (nw > 0) written += nw; if (nw < 0) { if (errno==EINTR) continue; rc = false; goto end; } if (written == n) break; } if (isDebugEnabled()) { string msg = bin2hex(data); debug("(serial %s) sent \"%s\"\n", device_.c_str(), msg.c_str()); } end: pthread_mutex_unlock(&write_lock_); return rc; } int SerialDeviceTTY::receive(vector *data) { pthread_mutex_lock(&read_lock_); data->clear(); int available = 0; int num_read = 0; ioctl(fd_, FIONREAD, &available); if (!available) goto end; data->resize(available); while (true) { int nr = read(fd_, &((*data)[num_read]), available-num_read); if (nr > 0) num_read += nr; if (nr < 0) { if (errno==EINTR) continue; goto end; } if (num_read == available) break; } if (isDebugEnabled()) { string msg = bin2hex(*data); debug("(serial %s) received \"%s\"\n", device_.c_str(), msg.c_str()); } end: pthread_mutex_unlock(&read_lock_); return num_read; } SerialCommunicationManagerImp::SerialCommunicationManagerImp(time_t exit_after_seconds) { running_ = true; max_fd_ = 0; pthread_create(&thread_, NULL, startLoop, this); //running_ = (rc == 0); start_time_ = time(NULL); exit_after_seconds_ = exit_after_seconds; } void *SerialCommunicationManagerImp::startLoop(void *a) { auto t = (SerialCommunicationManagerImp*)a; return t->eventLoop(); } unique_ptr SerialCommunicationManagerImp::createSerialDeviceTTY(string device, int baud_rate) { return unique_ptr(new SerialDeviceTTY(device, baud_rate, this)); } void SerialCommunicationManagerImp::listenTo(SerialDevice *sd, function cb) { SerialDeviceImp *si = dynamic_cast(sd); if (!si) { error("Internal error: Invalid serial device passed to listenTo.\n"); } si->on_data_ = cb; } void SerialCommunicationManagerImp::stop() { running_ = false; } void SerialCommunicationManagerImp::waitForStop() { while (running_) { usleep(1000*1000);} pthread_kill(thread_, SIGUSR1); pthread_join(thread_, NULL); for (SerialDevice *d : devices_) { d->close(); } } bool SerialCommunicationManagerImp::isRunning() { return running_; } void SerialCommunicationManagerImp::opened(SerialDeviceTTY *sd) { max_fd_ = max(sd->fd(), max_fd_); devices_.push_back(sd); pthread_kill(thread_, SIGUSR1); } void SerialCommunicationManagerImp::closed(SerialDeviceTTY *sd) { auto p = find(devices_.begin(), devices_.end(), sd); if (p != devices_.end()) { devices_.erase(p); } max_fd_ = 0; for (SerialDevice *d : devices_) { if (d->fd() > max_fd_) { max_fd_ = d->fd(); } } } void *SerialCommunicationManagerImp::eventLoop() { fd_set readfds; while (running_) { FD_ZERO(&readfds); for (SerialDevice *d : devices_) { FD_SET(d->fd(), &readfds); } struct timeval timeout { 3600, 0 }; if (exit_after_seconds_ > 0) { time_t curr = time(NULL); time_t diff = curr-start_time_; if (diff > exit_after_seconds_) { verbose("(serial) exit after %ld seconds\n", diff); stop(); break; } timeout.tv_sec = exit_after_seconds_ - diff; } int activity = select(max_fd_+1 , &readfds , NULL , NULL, &timeout); if (!running_) break; if (activity < 0 && errno!=EINTR) { warning("(serial) internal error after select! errno=%s\n", strerror(errno)); } if (activity > 0) { for (SerialDevice *d : devices_) { if (FD_ISSET(d->fd(), &readfds)) { SerialDeviceImp *si = dynamic_cast(d); if (si->on_data_) si->on_data_(); } } } } verbose("(serial) event loop stopped!\n"); return NULL; } unique_ptr createSerialCommunicationManager(time_t exit_after_seconds) { return unique_ptr(new SerialCommunicationManagerImp(exit_after_seconds)); } static int openSerialTTY(const char *tty, int baud_rate) { int rc = 0; speed_t speed = 0; struct termios tios; int fd = open(tty, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { usleep(1000*1000); fd = open(tty, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) goto err; } rc = flock(fd, LOCK_EX | LOCK_NB); if (rc == -1) { // It is already locked by another wmbusmeter process. warning("Device %s is already in use and locked.\n", tty); goto err; } switch (baud_rate) { case 9600: speed = B9600; break; case 19200: speed = B19200; break; case 38400: speed = B38400; break; case 57600: speed = B57600; break; case 115200: speed = B115200;break; default: goto err; } memset(&tios, 0, sizeof(tios)); rc = cfsetispeed(&tios, speed); if (rc < 0) goto err; rc = cfsetospeed(&tios, speed); if (rc < 0) goto err; tios.c_cflag |= (CREAD | CLOCAL); tios.c_cflag &= ~CSIZE; tios.c_cflag |= CS8; tios.c_cflag &=~ CSTOPB; tios.c_cflag &=~ PARENB; tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); tios.c_iflag &= ~INPCK; tios.c_iflag &= ~(IXON | IXOFF | IXANY); tios.c_oflag &=~ OPOST; tios.c_cc[VMIN] = 0; tios.c_cc[VTIME] = 0; rc = tcsetattr(fd, TCSANOW, &tios); if (rc < 0) goto err; return fd; err: if (fd != -1) close(fd); return -1; } SerialCommunicationManager::~SerialCommunicationManager() { }