-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdp.h
302 lines (243 loc) · 6.97 KB
/
sdp.h
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#ifndef SDP_H
#define SDP_H
#include "fiber.h"
#include "common.h"
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
namespace sdp
{
template <typename T>
void write(frame &pkt, u8 type, const T &arg)
{
pkt.write_u8(type);
pkt.write(arg);
}
void write_long(frame &pkt, u8 unspec, const void *src, u32 size);
struct data
{
virtual usize size() const = 0;
virtual void write(frame &pkt) const = 0;
};
struct nil : public data
{
virtual usize size() const override { return 1; }
virtual void write(frame &pkt) const override { pkt.write_u8(0); }
};
struct uint8 : public data
{
u8 value;
uint8(u8 value) : value(value) {}
virtual usize size() const override { return 2; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_UINT8, value); }
};
struct uint16 : public data
{
u16 value;
uint16(u16 value) : value(value) {}
virtual usize size() const override { return 3; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_UINT16, htons(value)); }
};
struct uint32 : public data
{
u32 value;
uint32(u32 value) : value(value) {}
virtual usize size() const override { return 5; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_UINT32, htonl(value)); }
};
struct uint64 : public data
{
u64 value;
uint64(u64 value) : value(value) {}
virtual usize size() const override { return 9; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_UINT64, hton64(value)); }
};
struct uint128 : public data
{
u128 value;
uint128(u128 value) : value(value) {}
virtual usize size() const override { return 17; }
virtual void write(frame &pkt) const override
{
u128 be;
hton128(&value, &be);
sdp::write(pkt, SDP_UINT128, be);
}
};
struct int8 : public data
{
i8 value;
int8(i8 value) : value(value) {}
virtual usize size() const override { return 2; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_INT8, value); }
};
struct int16 : public data
{
i16 value;
int16(i16 value) : value(value) {}
virtual usize size() const override { return 3; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_INT16, htons(value)); }
};
struct int32 : public data
{
i32 value;
int32(i32 value) : value(value) {}
virtual usize size() const override { return 5; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_INT32, htonl(value)); }
};
struct int64 : public data
{
i64 value;
int64(i64 value) : value(value) {}
virtual usize size() const override { return 9; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_INT64, hton64(value)); }
};
struct uuid16 : public data
{
u16 value;
uuid16(u16 value) : value(value) {}
virtual usize size() const override { return 3; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_UUID16, htons(value)); }
};
struct uuid32 : public data
{
u32 value;
uuid32(u32 value) : value(value) {}
virtual usize size() const override { return 5; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_UUID32, htonl(value)); }
};
struct uuid128 : public data
{
u128 value;
uuid128(u128 value) : value(value) {}
virtual usize size() const override { return 17; }
virtual void write(frame &pkt) const override
{
u128 be;
hton128(&value, &be);
sdp::write(pkt, SDP_UUID128, be);
}
};
struct longdata : public data
{
protected:
usize size_long(u32 size) const
{
if (size < (1 << 8))
return size + 2;
else if (size < (1 << 16))
return size + 3;
else
return size + 5;
}
void write_long(frame &pkt, u8 unspec, const void *src, u32 size) const
{
if (size < (1 << 8))
sdp::write(pkt, unspec | 0x05, (u8)size);
else if (size < (1 << 16))
sdp::write(pkt, unspec | 0x06, htons((u16)size));
else
sdp::write(pkt, unspec | 0x07, htonl((u32)size));
pkt.write(src, size);
}
};
struct text : public longdata
{
block value;
text(const char *value) : value((const u8 *)value, strlen(value)) {}
text(const block &value) : value(value) {}
virtual usize size() const override { return size_long(value.size); }
virtual void write(frame &pkt) const override
{
write_long(pkt, SDP_TEXT_STR_UNSPEC, value.data, value.size);
}
};
struct url : public text
{
virtual void write(frame &pkt) const override
{
write_long(pkt, SDP_TEXT_STR_UNSPEC, value.data, value.size);
}
};
struct boolean : public data
{
bool value;
boolean(bool value) : value(value) {}
virtual usize size() const override { return 2; }
virtual void write(frame &pkt) const override { sdp::write(pkt, SDP_BOOL, value); }
};
struct sequence : public longdata
{
sequence() : data(256) {}
sequence(const sequence ©) = delete;
template <typename... TArgs>
sequence(int, const TArgs &... args)
{
std::vector<const sdp::data *> vec{&args...};
// printf("ctor %zu\n", vec.size());
for (auto d : vec)
add(*d);
}
template <typename... TArgs>
sequence(const TArgs &... args)
{
std::vector<const sdp::data *> vec{&args...};
// printf("ctor %zu\n", vec.size());
for (auto d : vec)
add(*d);
}
virtual usize size() const override { return size_long(index); }
virtual void write(frame &pkt) const override
{
// printf("write sequence %02x %zu %zu\n", pkt.data, index, size_long(index));
write_long(pkt, SDP_SEQ_UNSPEC, &data[0], index);
}
void raw(const block pkt)
{
data.resize(index + pkt.size);
memcpy(&data[index], pkt.data, pkt.size);
index += pkt.size;
}
void add(const data &value)
{
data.resize(index + value.size());
frame dst(&data[index], value.size());
// printf("add %02x %zu\n", &data[index], value.size());
value.write(dst);
index += value.size();
}
protected:
std::vector<u8> data;
usize index = 0;
};
struct alternative : public sequence
{
virtual void write(frame &pkt) const override
{
write_long(pkt, SDP_ALT_UNSPEC, &data[0], index);
}
};
struct service
{
public:
void get_attrs(frame &out)
{
sequence sq;
for (const auto &pair : attributes)
{
sq.add(uint16(pair.first));
sq.raw(block(pair.second.data(), pair.second.size()));
}
sq.write(out);
}
void set_attr(u16 key, const data &value)
{
auto i = attributes.try_emplace(key, value.size());
frame out(&i.first->second[0], i.first->second.size());
// printf("attr %02x %zu\n", &i.first->second[0], value.size());
value.write(out);
}
private:
std::unordered_map<u16, std::vector<u8>> attributes;
};
}; // namespace sdp
#endif