Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

SnCommAfarTCP.cpp

Committer:
uci1
Date:
2012-08-02
Revision:
4:a91682e19d6b
Parent:
3:24c5f0f50bf1
Child:
5:9cea89700c66

File content as of revision 4:a91682e19d6b:

/*
#include "SnCommAfarTCP.h"

SnCommAfarTCP::SnCommAfarTCP(const bool useb64,
                             char* const b64buf, const uint32_t bblen) :
    fUseB64(useb64), fB64buf(b64buf), fbblen(bblen), fRmtServ(remote),
    fEth(new EthernetInterface), fSock(new TCPSocketConnection) {
    
    fEth->init("128.195.204.148",  // my IP
               "255.255.255.0",    // mask
               "128.195.204.1");   // gateway
    
    fRserv = "128.195.204.151";
    fRport = 6655;
    
}

SnCommAfarTCP::~SnCommAfarTCP() {
    delete fEth;
    delete fSock;
}

int SnCommAfarTCP::Receive(char* const buf, const uin32_t mlen,
                           const uint32_t bsize,
                           const uint32_t timeout_clock) {
    
}

int SnCommAfarTCP::SendAll(const char* const data, const uint32_t length,
                           const uint32_t timeout_clock) {
    int res=0;
    uint32_t b=0;
    while ( (length>b) && (time(0)<timeout_clock) ) {
        res = fSock->send_all(data+b, length-b);
        switch (res) {
            case -1:
                // TODO: how to check the error?
                continue;
            case 0:
                return res;
            default:
                b += res;
        };
    }
    return res; // timeout
}


bool SnCommAfarTCP::Connect(const uint32_t timeout) {
    bool isConn = false;

    while ( (isConn==false) && ( time(0) < timeout) ) {
        wait_ms(250);
        isConn = (fEth->connect()==0);
    }
    
    while ( (isConn==false) && ( time(0) < timeout) ) {
        wait_ms(250);
        isConn = (fSock->connect(fRserv.c_str(), fRport)==0);s
    }
    
    return isConn;
}

SnCommWin::ECommWinResult SnCommAfarTCP::OpenWindow(const uint32_t timeout,
                                                    const bool sendStatus,
                                                    const SnConfigFrame& conf,
                                                    const SnEventFrame& evt,
                                                    char* const genBuf) {    
    const bool canCon = Connect(timeout);
    
    SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected
                                           : SnCommWin::kCanNotConnect;
    
    if (canCon && sendStatus) {
        ret = SendStatus(conf, evt, genBuf);
    }
    
    return ret;
}

SnCommWin::ECommWinResult SnCommAfarTCP::WaitHandshake(const uint32_t timeout,
                                                       char* const buf,
                                                       const uint32_t bsize) {
    printf("WaitHandshake, to=%u\r\n",timeout);
    
    uint32_t mlen=0; // this message length
    const bool rd = Receive(buf, mlen, bsize, timeout, fB64buf, fbblen);
    if (rd) {
        uint32_t msgLen=0;
        uint8_t  msgCode=0;
        const char* b = buf;
        SnHeaderFrame::ReadFrom(b, msgCode, msgLen);
        if (msgCode==SnHeaderFrame::kHandshakeCode) {
            return SnCommWin::kOkWithMsg;
        } else {
            // TODO: somehow handle unexpected message?
            return SnCommWin::kUnexpectedRec;
        }
    }
    return SnCommWin::kOkNoMsg;
}
*/