-
Notifications
You must be signed in to change notification settings - Fork 4
/
btmessage.cpp
229 lines (191 loc) · 5.5 KB
/
btmessage.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright 2014 Ruediger Gad
*
* This file is part of SkippingStones.
*
* SkippingStones is largely based on libpebble by Liam McLoughlin
* https://github.com/Hexxeh/libpebble
*
* SkippingStones is published under the same license as libpebble (as of 10-02-2014):
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "btmessage.h"
#include <QDebug>
/*
* Constructors and operators
*/
BtMessage::BtMessage(QObject *parent) :
QObject(parent),
_currentIndex(0)
{
}
BtMessage::BtMessage(const QByteArray &data, QObject *parent) :
QObject(parent)
{
_data = data;
_currentIndex = 0;
}
BtMessage::BtMessage(const BtMessage &msg, QObject *parent) :
QObject(parent)
{
_data = msg._data;
_currentIndex = msg._currentIndex;
}
BtMessage& BtMessage::operator = (const BtMessage &msg) {
_data = msg._data;
_currentIndex = msg._currentIndex;
return *this;
}
/*
* Append actions
*/
void BtMessage::appendMessage(BtMessage *msg) {
_data.append(msg->_data);
}
void BtMessage::appendInt8(int i) {
_data.append((char) (i & 255));
}
void BtMessage::appendInt16(int i) {
_data.append((char) ((i >> 8) & 255));
_data.append((char) (i & 255));
}
void BtMessage::appendInt16le(int i) {
_data.append((char) (i & 255));
_data.append((char) ((i >> 8) & 255));
}
void BtMessage::appendInt32(int i) {
_data.append((char) ((i >> 24) & 255));
_data.append((char) ((i >> 16) & 255));
_data.append((char) ((i >> 8) & 255));
_data.append((char) (i & 255));
}
void BtMessage::appendInt32le(int i) {
_data.append((char) (i & 255));
_data.append((char) ((i >> 8) & 255));
_data.append((char) ((i >> 16) & 255));
_data.append((char) ((i >> 24) & 255));
}
void BtMessage::appendString(QString str) {
_data.append(str.toUtf8());
}
void BtMessage::appendHex(QString data) {
_data.append(QByteArray::fromHex(data.toLatin1()));
}
/*
* Prepend actions
*/
void BtMessage::prependInt8(int i) {
_data.prepend((char) (i & 255));
}
void BtMessage::prependInt16(int i) {
_data.prepend((char) (i & 255));
_data.prepend((char) ((i >> 8) & 255));
}
void BtMessage::prependInt32(int i) {
_data.prepend((char) (i & 255));
_data.prepend((char) ((i >> 8) & 255));
_data.prepend((char) ((i >> 16) & 255));
_data.prepend((char) ((i >> 24) & 255));
}
void BtMessage::prependString(QString str) {
for (int i = str.length() - 1; i >= 0; i--) {
_data.prepend(str.at(i).toLatin1());
}
}
/*
* Getters by index
*/
int BtMessage::readInt8(int index) {
return _data[index];
}
int BtMessage::readInt16(int index) {
return (_data[index] << 8) | _data[index + 1];
}
int BtMessage::readInt16le(int index) {
return (_data[index + 1] << 8) | _data[index];
}
int BtMessage::readInt32(int index) {
return (_data[index] << 24) | (_data[index + 1] << 16) | (_data[index + 2] << 8) | _data[index + 3];
}
int BtMessage::readInt32le(int index) {
return (_data[index + 3] << 24) | (_data[index + 2] << 16) | (_data[index + 1] << 8) | _data[index];
}
QString BtMessage::readHexString(int index, int size) {
return QString(_data.mid(index, size).toHex());
}
QString BtMessage::readString(int index, int size) {
QByteArray tmp = _data.mid(index, size);
for (int i = 0; i < tmp.length(); i++) {
if (tmp.at(i) == 0) {
tmp.remove(i, 1);
i--;
}
}
return QString(tmp);
}
/*
* Relative getters
*/
QString BtMessage::readString(int size) {
QString ret = readString(_currentIndex, size);
_currentIndex += size;
return ret;
}
int BtMessage::nextInt8() {
int ret = readInt8(_currentIndex);
_currentIndex += 1;
return ret;
}
int BtMessage::nextInt16() {
int ret = readInt16(_currentIndex);
_currentIndex += 2;
return ret;
}
int BtMessage::nextInt32() {
int ret = readInt32(_currentIndex);
_currentIndex += 4;
return ret;
}
/*
* Other methods
*/
QByteArray BtMessage::data() {
return _data;
}
int BtMessage::endpoint() {
return readInt16(2);
}
int BtMessage::payloadLength() {
return readInt16(0);
}
void BtMessage::resetIndex() {
_currentIndex = 0;
}
int BtMessage::size() {
return _data.size();
}
int BtMessage::stringLength(QString str) {
return QByteArray(str.toUtf8()).length();
}
QString BtMessage::toHexString() {
// qDebug() << "Data size:" << _data.size()
// << "; Hex Size:" << _data.toHex().size()
// << "; Hex String Size:" << QString(_data.toHex()).size();
return QString(_data.toHex());
}