changeset 345:b595ff13547b

fluid-mnf/target.c: ported, passed compilation
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 13 Mar 2020 06:11:33 +0000
parents 2bd27d940023
children 2aa78da4033b
files fluid-mnf/target.c
diffstat 1 files changed, 24 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/fluid-mnf/target.c	Fri Mar 13 05:53:20 2020 +0000
+++ b/fluid-mnf/target.c	Fri Mar 13 06:11:33 2020 +0000
@@ -8,6 +8,9 @@
  *
  * $Id: target.c 1.20 Mon, 21 Oct 2002 18:39:13 +0200 mmj $
  *
+ * This serial interface handling architecture has been majorly redesigned
+ * by Mychaela N. Falconia for the present fluid-mnf Linux port.
+ *
  ******************************************************************************/
 
 #include "serial.h"
@@ -15,10 +18,9 @@
 #include "trace.h"
 
 #include <stdio.h>
-
-#if defined(MSC) || defined(BCC)
-  #include "windows.h"
-#endif
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
 int target_trace(unsigned char ch);
 
@@ -27,12 +29,9 @@
  * Globals
  ******************************************************************************/
 
-HANDLE hEvent;
-
 #define RECVBUF_SIZE  (65536)
 
 static unsigned char recvbuf[RECVBUF_SIZE];
-static int recvbuf_size_target;
 static int recvbuf_put;
 static int recvbuf_get;
 
@@ -93,22 +92,15 @@
 
 int target_driver_init(int uart, int baudrate, char *flowcontrol)
 {
-    int error;
+    int rc;
+
+    rc = serial_init(uart, baudrate, flowcontrol);
+    if (rc < 0)
+        return rc;
 
     target_recv_reset();
 
-    hEvent = CreateEvent(NULL,  // lpEventAttributes
-                         FALSE, // bManualReset (OFF)
-                         FALSE, // bInitialstate (OFF)
-                         NULL); // lpName
-
-    if (hEvent == NULL) {
-        error = E_DRIVER_INIT + E_OS;
-    }
-    else {
-        error = serial_init(uart, baudrate, flowcontrol);
-    }
-    return error;
+    return 0;
 }
 
 int target_driver_baudrate(int baudrate)
@@ -138,25 +130,6 @@
  * Target Wait and Receive
  ******************************************************************************/
 
-void target_recv_signal(void)
-{
-    // Notify waiting thread when we have received the requested number of
-    // bytes. Note that we use auto-resetting events, so you have to be VERY
-    // careful to wait for the exact number of bytes you want. Otherwise you
-    // might trigger the event twice in quick succession and this will
-    // result in the next call of target_wait()'s to return immediately
-    // without waiting!
-
-    if (recvbuf_size() >= recvbuf_size_target && recvbuf_size_target > 0) {
-        // When we have triggered the event, disable further triggering
-        // until next call of target_wait()
-        recvbuf_size_target = 0;
-        tr(TrDriverGet, "!");
-        if (SetEvent(hEvent) == 0)
-            fprintf(stderr, "FATAL: recv_push_tm(): SetEvent() failed with Win32 error %d\n", GetLastError());
-    }
-}
-
 void target_recv_push(char *buf, int size)
 {
     if (size == 0) {
@@ -169,8 +142,6 @@
         recvbuf_put &= (RECVBUF_SIZE - 1);
     }
     tr(TrDriverGet, " G%d", recvbuf_size());
-
-    target_recv_signal();
 }
 
 // Wait maximum <timeout> milli-seconds for <size> bytes to arrive. If
@@ -179,31 +150,31 @@
 // total waiting time.
 int target_wait(int size, int timeout)
 {
-    int result = 0;
+    char rxbuf[512];
+    int cc;
 
     tr(TrTargetWait, "target_wait(%d, %d)\n", size, timeout);
 
     if (size > 0)
         timeout += serial_transfer_time(size);
 
-    recvbuf_size_target = size;
     target_trace_timer = timeout;
 
     if (size == 0) {
-        Sleep(timeout);
+        usleep(timeout * 1000);
+        return 0;
     }
-    else {
-        target_recv_signal();
 
-        switch (WaitForSingleObject(hEvent, timeout)) {
-        case WAIT_OBJECT_0: result = recvbuf_size(); break;
-        case WAIT_TIMEOUT:  result = E_RECV_TIMEOUT; break;
-        default:            result = E_OS + E_DRIVER_WAIT; break;
-        }
+    while (recvbuf_size() < size) {
+        cc = serial_recv(rxbuf, sizeof rxbuf, timeout);
+        if (cc < 0)
+            return cc;
+        if (cc == 0)
+            return E_RECV_TIMEOUT;
+        target_recv_push(rxbuf, cc);
     }
-    recvbuf_size_target = 0;
 
-    return result;
+    return recvbuf_size();
 }
 
 int target_recv(void *outbuf, int size)