From c6e3721763f29c96af2a25fcd7259b193c6dc24f Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Tue, 19 Mar 2019 12:17:47 +0100
Subject: [PATCH] Cleanup code, fix underflow error

---
 plugins/serial2002/serial2002.c     | 21 +++++++--------------
 plugins/serial2002/serial2002_lib.c | 11 +++--------
 2 files changed, 10 insertions(+), 22 deletions(-)

diff --git a/plugins/serial2002/serial2002.c b/plugins/serial2002/serial2002.c
index 79de31d..ae0af1b 100644
--- a/plugins/serial2002/serial2002.c
+++ b/plugins/serial2002/serial2002.c
@@ -133,12 +133,13 @@ static struct moberg_status analog_out_write(
   struct moberg_channel_context *channel = &analog_out->channel_context;
   struct moberg_device_context *device = channel->device;
   struct analog_map map = device->analog_out.map[channel->index];
-  long as_long = value - map.min / map.delta;
+  long as_long = (value - map.min) / map.delta;
   if (as_long < 0) {
-    value = 0;
+    as_long = 0;
   } else if (as_long > map.maxdata) {
     as_long = map.maxdata;
   }
+
   struct serial2002_data data = { is_channel, map.index, as_long };
   return serial2002_write(device->port.fd,  data);
 }
@@ -204,7 +205,6 @@ return_result:
   return result;
 err_einval:
   return MOBERG_ERRNO(EINVAL);
-  fprintf(stderr, "%s\n", __FUNCTION__);
   return MOBERG_OK;
 }
 
@@ -218,6 +218,7 @@ static struct moberg_device_context *new_context(struct moberg *moberg,
     result->moberg = moberg;
     result->dlclose = dlclose;
     result->dlhandle = dlhandle;
+    result->port.timeout = 100000;
   }
   return result;
 }
@@ -258,9 +259,10 @@ static void remap_analog(
         remap->map[remap->count].delta =
           (remap->map[remap->count].max - remap->map[remap->count].min) /
           remap->map[remap->count].maxdata;
-      }
+      } else {
+        remap->map[remap->count].delta = 1.0;
 
-      fprintf(stderr, "%d -> %d\n", remap->count, i);
+      }
       remap->count++;
     }
   }
@@ -276,7 +278,6 @@ static void remap_digital(
   for (int i = 0 ; i < count ; i++) {
     if (channel[i].kind == kind) {
       remap->map[remap->count].index = i;
-      fprintf(stderr, "%d -> %d\n", remap->count, i);
       remap->count++;
     }
   }
@@ -287,7 +288,6 @@ static struct moberg_status device_open(struct moberg_device_context *device)
   struct moberg_status result;
   int fd = -1;
 
-  fprintf(stderr, "%s\n", __FUNCTION__);
   if (device->port.count == 0) {
     fd = open(device->port.name, O_RDWR);
     if (fd < 0) { goto err_errno; }
@@ -323,10 +323,8 @@ static struct moberg_status device_open(struct moberg_device_context *device)
     device->port.fd = fd;
   }
   device->port.count++;
-  fprintf(stderr, "OPENED %d\n", device->port.count);
   return MOBERG_OK;
 err_errno:
-  fprintf(stderr, "ERRNO %d\n", errno);
   result = MOBERG_ERRNO(errno);
 err_result:
   if (fd >= 0) {
@@ -337,16 +335,13 @@ err_result:
 
 static struct moberg_status device_close(struct moberg_device_context *device)
 {
-  fprintf(stderr, "%s\n", __FUNCTION__);
   if (device->port.count < 0) { errno = ENODEV; goto err_errno; }
   device->port.count--;
   if (device->port.count == 0) {
-    fprintf(stderr, "CLOSE\n");
     if (close(device->port.fd) < 0) { goto err_errno; }
   }
   return MOBERG_OK;
 err_errno:
-  fprintf(stderr, "CLOSE %d\n", errno);
   return MOBERG_ERRNO(errno);
 
 }
@@ -371,14 +366,12 @@ static int channel_down(struct moberg_channel *channel)
 
 static struct moberg_status channel_open(struct moberg_channel *channel)
 {
-  fprintf(stderr, "%s\n", __FUNCTION__);
   struct moberg_status result = device_open(channel->context->device);
   return result;
 }
 
 static struct moberg_status channel_close(struct moberg_channel *channel)
 {
-  fprintf(stderr, "%s\n", __FUNCTION__);
   struct moberg_status result = device_close(channel->context->device);
   return result;
 }
diff --git a/plugins/serial2002/serial2002_lib.c b/plugins/serial2002/serial2002_lib.c
index 5fb5354..d8fad61 100644
--- a/plugins/serial2002/serial2002_lib.c
+++ b/plugins/serial2002/serial2002_lib.c
@@ -33,7 +33,6 @@ static struct moberg_status tty_write(int fd, unsigned char *buf, int count)
   while (n < count) {
     int written = write(fd, &buf[n], count - n);
     if (written == 0) {
-      fprintf(stderr, "Failed to write\n");
       return MOBERG_ERRNO(ENODATA);
     } else if (written < 0) {
       return MOBERG_ERRNO(errno);
@@ -50,7 +49,7 @@ static struct moberg_status tty_read(int fd, long timeout, unsigned char *value)
   while (1) {
     pollfd.fd = fd;
     pollfd.events = POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR;
-    int err = poll(&pollfd, 1, timeout);
+    int err = poll(&pollfd, 1, timeout / 1000);
     if (err >= 1) {
       break;
     } else if (err == 0) {
@@ -100,14 +99,11 @@ struct moberg_status serial2002_read(int f, long timeout,
     }
     
     length++;
-    if (data < 0) {
-      fprintf(stderr, "serial2002 error\n");
-      break;
-    } else if (length < 6 && data & 0x80) {
+    if (length < 6 && data & 0x80) {
       value->value = (value->value << 7) | (data & 0x7f);
     } else if (length == 6 && data & 0x80) {
-      fprintf(stderr, "Too long\n");
       value->kind = is_invalid;
+      return MOBERG_ERRNO(EFBIG);
       break;
     } else {
       if (length == 1) {
@@ -180,7 +176,6 @@ static double interpret_value(int value)
       result *= 1e-6;
       break;
   }
-  fprintf(stderr, "%f\n", result);
   return result;
 }
 
-- 
GitLab