Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

SnStatusFrame.h

Committer:
uci1
Date:
2012-06-30
Revision:
0:664899e0b988
Child:
1:e392595b4b76

File content as of revision 0:664899e0b988:

#ifndef SN_SnStatusFrame
#define SN_SnStatusFrame

#include <stdint.h>

#include "SnBitUtils.h"
#include "SnCommWin.h"
#include "SnConfigFrame.h"
#include "SnEventFrame.h"

struct SnStatusFrame {
    //
    // A simple struct (everything public) to function like namespace.
    // The contents of the status frame are sent from here, in order to
    // help make sure the status frame is the same for each comm method.
    //
    // No member variables are used in order to preserve memory on the mbed.
    // (i.e. no actual status frame middle-man object exists)
    //
    
    static const uint8_t    kIOVers;    // MUST BE INCREASED if bytes written/read change!!
    
    static const uint32_t   kMaxSizeOf =
               1u + sizeof(uint64_t)
            + (sizeof(uint32_t)*3u) + (sizeof(uint16_t))
            + (sizeof(uint8_t)*3u) + SnConfigFrame::kConfLblLen
            + SnEventFrame::kMaxSizeOf;
    
    template<class T>
    static
    SnCommWin::ECommWinResult WriteTo(T x,
                                      const SnConfigFrame::EDatPackBit type,
                                      const SnConfigFrame& conf,
                                      const SnEventFrame& evt,
                                      char* const evtBuf) {
        // expect 'x' to be a MODSERIAL& or a char const*
        
        uint8_t loseLSB=0, loseMSB=0;
        uint16_t wvBase=0;
        conf.GetPackParsFor(type, loseLSB, loseMSB, wvBase);
        
        evt.WriteTo(evtBuf, loseLSB, loseMSB, wvBase);
        const uint32_t llen = strlen(conf.GetLabel());
        
        // if anything about these writes changes, update kIOVers and SizeOf
        SnBitUtils::WriteTo(x, SnStatusFrame::kIOVers);
        SnBitUtils::WriteTo(x, conf.GetMacAddress());
        SnBitUtils::WriteTo(x, llen);
        SnBitUtils::WriteTo(x, conf.GetLabel(), llen);
        SnBitUtils::WriteTo(x, conf.GetRun());
        SnBitUtils::WriteTo(x, time(0));
        SnBitUtils::WriteTo(x, loseLSB);
        SnBitUtils::WriteTo(x, loseMSB);
        SnBitUtils::WriteTo(x, wvBase);
        SnBitUtils::WriteTo(x, evtBuf, evt.SizeOf(loseLSB, loseMSB));
        
        return SnCommWin::kOkMsgSent;
    }
        
    static
    uint32_t SizeOf(const uint32_t confLblLen,
                    const uint8_t loseLSB, const uint8_t loseMSB) {
        // number of bytes read/written during i/o
        const uint32_t msz = kMaxSizeOf - SnConfigFrame::kConfLblLen 
                           + confLblLen;
        if ((loseLSB==0) && (loseMSB==0)) {
            return msz;
        } else {
            return msz - SnEventFrame::kMaxSizeOf 
                       + SnEventFrame::SizeOf(loseLSB, loseMSB);
        }
    }
    
    static
    uint32_t SizeOf(const SnConfigFrame& conf) {
        return SizeOf(conf.GetLabelStrLen(),
                      conf.GetWvLoseLSB(), conf.GetWvLoseMSB());
    }
    
};

#endif // SN_SnStatusFrame