summaryrefslogtreecommitdiff
path: root/src/CommonAPI/DBus/DBusInputStream.cpp
blob: 0c838a02caf564a47561e97e37512044c2dccbca (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <iomanip>

#include <CommonAPI/DBus/DBusInputStream.hpp>

namespace CommonAPI {
namespace DBus {

DBusInputStream::DBusInputStream(const CommonAPI::DBus::DBusMessage &_message)
    : begin_(_message.getBodyData()),
      current_(0),
      size_((size_t)(_message.getBodyLength())),
      exception_(nullptr),
      message_(_message) {
}

DBusInputStream::~DBusInputStream() {}

const CommonAPI::DBus::DBusError& DBusInputStream::getError() const {
    return (*exception_);
}

bool DBusInputStream::isErrorSet() const {
    return (exception_ != nullptr);
}

void DBusInputStream::clearError() {
    exception_ = nullptr;
}

void DBusInputStream::align(const size_t _boundary) {
    const unsigned int mask = static_cast<unsigned int>(_boundary) - 1;
    current_ = (current_ + mask) & (~mask);
}

char *DBusInputStream::_readRaw(const size_t _size) {
    if ((current_ + _size) > size_) {
        setError();
        return NULL;
    }

    char *data = (char *) (begin_ + current_);
    current_ += _size;
    return data;
}

void DBusInputStream::setError() {
    exception_ = new CommonAPI::DBus::DBusError();
}

void DBusInputStream::pushPosition() {
    positions_.push(current_);
}

size_t DBusInputStream::popPosition() {
    size_t itsPosition = positions_.top();
    positions_.pop();
    return itsPosition;
}

void DBusInputStream::pushSize(size_t _size) {
    sizes_.push(static_cast<unsigned int>(_size));
}

size_t DBusInputStream::popSize() {
    size_t itsSize = sizes_.top();
    sizes_.pop();
    return itsSize;
}

InputStream<DBusInputStream> &DBusInputStream::readValue(bool &_value, const EmptyDeployment *_depl) {
    uint32_t tmp;
    readValue(tmp, _depl);
    if (tmp > 1)
        setError();
    _value = (tmp != 0);
    return (*this);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(int8_t &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(int16_t &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(int32_t &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(int64_t &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(uint8_t &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(uint16_t &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(uint32_t &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(uint64_t &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(float &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(double &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    return _readValue(_value);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(std::string &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    uint32_t length(0);
    _readValue(length);

    // length field does not include terminating 0-byte, therefore length of data to read is +1
    char *data = _readRaw(length + 1);
    if (!hasError()) {
        // The string contained in a DBus-message is required to be 0-terminated, therefore the following line works
        _value = data;
    }

    return (*this);
}

InputStream<DBusInputStream> &DBusInputStream::readValue(Version &_value, const EmptyDeployment *_depl) {
    (void)_depl;
    align(8);
    _readValue(_value.Major);
    _readValue(_value.Minor);
    return *this;
}

} // namespace DBus
} // namespace CommonAPI