summaryrefslogtreecommitdiff
path: root/src/voltronic/pseudo_device.cc
blob: 58cd95c1746f2c74e614d22409d6b7571c875e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: BSD-3-Clause

#include <stdexcept>
#include <sstream>
#include <cstring>

#include "device.h"
#include "crc.h"
#include "hexdump/hexdump.h"
#include "../logging.h"

namespace voltronic {

// PI
//static const char* response = "^D00518";

// GS
static const char* response = "^D1060000,000,2300,500,0115,0018,002,500,000,000,000,000,078,019,000,000,0000,0000,0000,0000,0,0,0,1,2,2,0,0";

// PIRI
//static const char* response = "^D0882300,217,2300,500,217,5000,5000,480,500,570,420,576,540,2,30,060,0,1,1,6,0,0,0,1,2,00";

// DI
//static const char* response = "^D0682300,500,0,408,540,564,460,540,060,30,0,0,1,0,0,0,1,0,0,1,1,0,1,1";

// set response
//static const char* response = "^1";

// TODO: maybe move size and crc stuff to readLoop()?
size_t PseudoDevice::read(u8* buf, size_t bufSize) {
    size_t pseudoResponseSize = strlen(response);

    size_t responseSize = pseudoResponseSize;
    if (flags_ & FLAG_READ_CRC)
        responseSize += 2;

    if (responseSize + 1 > bufSize) {
        std::ostringstream error;
        error << "buffer is not large enough (" << (responseSize + 1) << " > " << bufSize << ")";
        throw std::overflow_error(error.str());
    }

    memcpy(buf, response, responseSize);

    if (flags_ & FLAG_READ_CRC) {
        CRC crc = crc_calculate(buf, pseudoResponseSize);
        crc_write(crc, &buf[pseudoResponseSize]);
    }

    buf[responseSize] = '\r';

    return responseSize + 1;
}

size_t PseudoDevice::write(const u8* data, size_t dataSize) {
    if (verbose_) {
        myerr << "dataSize=" << dataSize;
        std::cerr << hexdump((void*)data, dataSize);
    }
    return dataSize;
}

}