-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPayloadBuilder.cpp
175 lines (141 loc) · 3.73 KB
/
PayloadBuilder.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
/* _ _ _ _____ _ _ _____ _ _ ___ ___ _ __
* /_\ | | |_ _| |_ (_)_ _ __ _ __|_ _|_ _| | |__ / __| \| |/ /
* / _ \| | | | | | ' \| | ' \/ _` (_-< | |/ _` | | / / \__ \ |) | ' <
* /_/ \_\_|_| |_| |_||_|_|_||_\__, /__/ |_|\__,_|_|_\_\ |___/___/|_|\_\
* |___/
*
* Copyright 2017 AllThingsTalk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "PayloadBuilder.h"
// initialize the payload buffer with the given maximum size
PayloadBuilder::PayloadBuilder(ATT_NBIOT &nbiot, uint8_t size) : maxsize(size)
{
buffer = (uint8_t*) malloc(size);
cursor = 0;
_nbiot = &nbiot;
}
PayloadBuilder::PayloadBuilder(ATT_NBIOT &nbiot) : maxsize(60)
{
buffer = (uint8_t*) malloc(60);
cursor = 0;
_nbiot = &nbiot;
}
PayloadBuilder::~PayloadBuilder(void)
{
free(buffer);
}
// reset the payload, to call before building a frame payload
void PayloadBuilder::reset(void)
{
cursor = 0;
}
// return the current size of the payload
uint8_t PayloadBuilder::getSize(void)
{
return cursor;
}
// return the payload buffer
uint8_t* PayloadBuilder::getBuffer(void)
{
return buffer;
}
uint8_t PayloadBuilder::copy(uint8_t* dst)
{
memcpy(dst, buffer, cursor);
return cursor;
}
bool PayloadBuilder::send(void* packet, unsigned char size)
{
return _nbiot->sendPayload(packet, size);
}
bool PayloadBuilder::send()
{
memcpy(sendBuffer, buffer, cursor);
return _nbiot->sendPayload(&sendBuffer, cursor);
}
uint8_t PayloadBuilder::addBoolean(uint8_t value)
{
if ((cursor + ATTALK_BOOLEAN_SIZE) > maxsize) {
return 0;
}
buffer[cursor++] = value;
return cursor;
}
uint8_t PayloadBuilder::addInteger(int value)
{
if ((cursor + ATTALK_INTEGER_SIZE) > maxsize) {
return 0;
}
byte* fb = (byte*) &value;
buffer[cursor++] = fb[0];
buffer[cursor++] = fb[1];
return cursor;
}
uint8_t PayloadBuilder::addNumber(float value)
{
if ((cursor + ATTALK_NUMBER_SIZE) > maxsize) {
return 0;
}
byte* fb = (byte*) &value;
buffer[cursor++] = fb[0];
buffer[cursor++] = fb[1];
buffer[cursor++] = fb[2];
buffer[cursor++] = fb[3];
return cursor;
}
uint8_t PayloadBuilder::addGPS(float latitude, float longitude, float altitude)
{
if ((cursor + ATTALK_GPS_SIZE) > maxsize) {
return 0;
}
byte* fb = (byte*) &latitude;
buffer[cursor++] = fb[0];
buffer[cursor++] = fb[1];
buffer[cursor++] = fb[2];
buffer[cursor++] = fb[3];
fb = (byte*) &longitude;
buffer[cursor++] = fb[0];
buffer[cursor++] = fb[1];
buffer[cursor++] = fb[2];
buffer[cursor++] = fb[3];
fb = (byte*) &altitude;
buffer[cursor++] = fb[0];
buffer[cursor++] = fb[1];
buffer[cursor++] = fb[2];
buffer[cursor++] = fb[3];
return cursor;
}
uint8_t PayloadBuilder::addAccelerometer(float x, float y, float z)
{
if ((cursor + ATTALK_ACCEL_SIZE) > maxsize) {
return 0;
}
byte* fb = (byte*) &x;
buffer[cursor++] = fb[0];
buffer[cursor++] = fb[1];
buffer[cursor++] = fb[2];
buffer[cursor++] = fb[3];
fb = (byte*) &y;
buffer[cursor++] = fb[0];
buffer[cursor++] = fb[1];
buffer[cursor++] = fb[2];
buffer[cursor++] = fb[3];
fb = (byte*) &z;
buffer[cursor++] = fb[0];
buffer[cursor++] = fb[1];
buffer[cursor++] = fb[2];
buffer[cursor++] = fb[3];
return cursor;
}