S K UCI / Mbed 2 deprecated AutonomousDAQ

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
2:e67f7c158087
Parent:
1:e392595b4b76
Child:
3:24c5f0f50bf1
--- a/SnCommUsb.cpp	Fri Jul 20 19:04:02 2012 +0000
+++ b/SnCommUsb.cpp	Tue Jul 24 02:07:23 2012 +0000
@@ -5,6 +5,8 @@
 #include "SnStatusFrame.h"
 #include "SnConfigFrame.h"
 #include "SnEventFrame.h"
+#include "SnHeaderFrame.h"
+#include "SnSDUtils.h"
 
 MODSERIAL* SnCommUsb::fgCpu     =  0;
 
@@ -35,13 +37,12 @@
                                                 const bool sendStatus,
                                                 const SnConfigFrame& conf,
                                                 const SnEventFrame& evt,
-                                                char* const evtBuf,
-                                                char* const statBuf) {
+                                                char* const genBuf) {
     // usb port always connected (or never)
     SnCommWin::ECommWinResult ret = SnCommWin::kConnected;
     
     if (sendStatus) {
-        ret = SendStatus(conf, evt, evtBuf, statBuf);
+        ret = SendStatus(conf, evt, genBuf);
     }
     
     return ret;
@@ -55,58 +56,77 @@
     SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
     
     bool hasData = fgCpu->readable()==1;
-    
-    DigitalOut led1(LED1);
-    DigitalOut led3(LED3);
-    DigitalOut led4(LED4);
-    
+        
     while ( (hasData==false) && (time(0) < timeOut) ) {        
         wait_ms(10);
         
         hasData = fgCpu->readable()==1;
-        
-        led1=1; wait(0.5);
-        led1=0; wait(0.5);
     }
     
     if( hasData ) {
-        led3=1; wait(1);
+        uint8_t  mcode;
+        uint32_t mlen;
+        SnHeaderFrame::ReadFrom(*fgCpu, mcode, mlen);
+        // TODO: do something with the header info?
         conf.ReadFrom(*fgCpu);
         res = SnCommWin::kOkWithMsg;
     } else {
-        led4=1; wait(1);
         res = SnCommWin::kOkNoMsg;
     }
     
-    led1=0;
-    led3=0;
-    led4=0;
-    
     return res;
 }
 
 SnCommWin::ECommWinResult SnCommUsb::SendStatus(const SnConfigFrame& conf,
                                                 const SnEventFrame& evt,
-                                                char* const evtBuf,
-                                                char* const statBuf) {
-    // statBuf not used. status written directly to serial port
-    return SnStatusFrame::WriteTo(*fgCpu, SnConfigFrame::kUSB, conf, evt, evtBuf);
+                                                char* const genBuf) {
+    const bool ok = SnHeaderFrame::WriteTo(*fgCpu,
+        SnHeaderFrame::kStatusCode, SnStatusFrame::SizeOf(conf));
+    if (ok) {
+        return SnStatusFrame::WriteTo(*fgCpu, SnConfigFrame::kUSB, conf, evt, genBuf);
+    } else {
+        return SnCommWin::kFailNoneSent;
+    }
 }
 
 SnCommWin::ECommWinResult SnCommUsb::SendData(FILE* inf) {
-    int c;
-    do {
-        c = getc(inf);
-        fgCpu->putc(c);
-    } while (c!=EOF);
-    return SnCommWin::kOkMsgSent;
+    const int foff = ftell(inf);
+    fseek(inf, 0, SEEK_END);
+    const int fsize = ftell(inf) - foff;
+    fseek(inf, foff, SEEK_SET);
+    if (fsize>0) {
+        SnHeaderFrame::WriteTo(*fgCpu,
+            SnHeaderFrame::kFileCode, fsize);
+        SendBytes(inf, fsize);
+        return SnCommWin::kOkMsgSent;
+    } else {
+        return SnCommWin::kFailNoneSent;
+    }
+}
+
+bool SnCommUsb::SendBytes(FILE* f, const uint32_t nbytes) {
+    register uint32_t i=0;
+    for (i=0; (i<nbytes) 
+           && (feof(f)==0) 
+           && (ferror(f)==0); i++) {
+        fgCpu->putc( getc(f) );
+    }
+    return (i==nbytes);
+}
+
+bool SnCommUsb::SendBytes(const char* const buf,
+                          const uint32_t nbytes) {
+    const char* b = buf;
+    for (uint32_t j=0; j<nbytes; j++, b++) {
+        fgCpu->putc( *b );
+    }
+    return true;
 }
 
 SnCommWin::ECommWinResult SnCommUsb::SendConfAndEvents(FILE* inf,
                                                     const SnConfigFrame& curConf,
                                                     SnEventFrame& evt,
-                                                    char* const evtBuf,
-                                                    char* const confBuf,
+                                                    char* const genBuf,
                                                     const uint32_t nevts,
                                                     const uint32_t firstEvt) {
     // firstEvt==0 ==> start at beginning
@@ -114,13 +134,16 @@
     //    for the fcn to send the full file)
     
     // TODO: check memory for temporary config/event frames?
+    uint64_t macadr;
+    uint32_t run;
+    uint16_t seq;
+    SnSDUtils::ReadFileHeader(inf, macadr, run, seq);
     SnConfigFrame conf;
     conf.ReadFrom(inf);
-    conf.WriteTo(*fgCpu);
 
     uint8_t sLoseLSB=0, sLoseMSB=0;
     uint16_t sWvBase=0;
-    conf.GetPackParsFor(SnConfigFrame::kUSB, sLoseLSB, sLoseMSB, sWvBase);
+    curConf.GetPackParsFor(SnConfigFrame::kUSB, sLoseLSB, sLoseMSB, sWvBase);
     // do we have to unpack & repack events?
     const bool repack =    (sLoseLSB != conf.GetWvLoseLSB())
                         && (sLoseMSB != conf.GetWvLoseMSB())
@@ -130,37 +153,39 @@
     const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(),
                                                 conf.GetWvLoseMSB());
     // move up to first event
-    fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET);
-    if (repack) {
-        // repack ==> send event by event
-        // size of event sent over afar
-        const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
-        char* b;
-        register uint32_t i;
-        for (i=0; (i<nevts) && (ferror(inf)==0) && (feof(inf)==0); i++) {
-            evt.ReadFrom(inf, evtBuf,
+    const uint32_t csize = conf.SizeOf();
+    fseek(inf, csize + (firstEvt*esize), SEEK_SET);
+
+    // send the header and conf
+    char* b = genBuf;
+    SnHeaderFrame::WriteTo(b,
+        SnHeaderFrame::kConfAndEvtsCode, csize);
+    conf.WriteTo(b);
+    bool ok = SendBytes(genBuf, csize);
+
+    // size of event sent over afar
+    const uint32_t ssize = (repack) ?
+        SnEventFrame::SizeOf(sLoseLSB, sLoseMSB) :
+        esize;
+    
+    for (uint32_t i=0; i<nevts; i++) {
+        if (repack) {
+            evt.ReadFrom(inf, genBuf,
                 conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
                 conf.GetWvBaseline());
-            evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase);
-            b = evtBuf;
-            for (uint32_t j=0; j<ssize; j++, b++) {
-                fgCpu->putc( *b );
-            }
-        }
-        if (i!=nevts) {
-            return SnCommWin::kFailPartSent;
-        }
-    } else {
-        // no repacking ==> just send the bytes from the file
-        const uint32_t nbytes = nevts*esize;
-        register uint32_t i;
-        for (i=0; i<nbytes && (ferror(inf)==0) && (feof(inf)==0); i++) {
-            fgCpu->putc( fgetc(inf) );
-        }
-        if (i!=nbytes) {
-            return SnCommWin::kFailPartSent;
+            evt.WriteTo(genBuf, sLoseLSB, sLoseMSB, sWvBase);
+            
+            ok &= SendBytes(SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode,
+                                              ssize),
+                            SnHeaderFrame::SizeOf());
+            ok &= SendBytes(genBuf, 
+                    evt.SizeOf(conf.GetWvLoseLSB(), conf.GetWvLoseMSB()));
+        } else {
+            ok &= SendBytes(SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode,
+                                              ssize),
+                            SnHeaderFrame::SizeOf());
+            ok &= SendBytes(inf, ssize);
         }
     }
-    
-    return SnCommWin::kOkMsgSent;
+    return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
 }