Repository navigation
Expand file tree
/
Copy pathsimCBOR.lua
More file actions
199 lines (162 loc) · 4.47 KB
/
Copy pathsimCBOR.lua
File metadata and controls
199 lines (162 loc) · 4.47 KB
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
local simCBOR = {}
simCBOR.Tags = {
Array = {
Nd = 40,
Nd_RowMajor = 40,
Nd_ColMajor = 1040,
Homog = 41,
U8 = 64,
U16BE = 65,
U32BE = 66,
U64BE = 67,
U8C = 68,
U16LE = 69,
U32LE = 70,
U64LE = 71,
S8 = 72,
S16BE = 73,
S32BE = 74,
S64BE = 75,
S16LE = 77,
S32LE = 78,
S64LE = 79,
F16BE = 80,
F32BE = 81,
F64BE = 82,
F128BE = 83,
F16LE = 84,
F32LE = 85,
F64LE = 86,
F128LE = 87,
},
Sim = {
Handle = 4294999999,
HandleArray = 4294999998,
Quaternion = 4294980000,
Color = 4294970000,
Pose = 4294980500,
},
}
local sim1 = require 'sim-1'
local sim = require 'sim-2'
local cbor = require 'org.conman.cbor'
local type_tags = {}
type_tags.BIN = function(value, iskey)
if not iskey then
value = Buffer(value)
end
return value
end
local function registerTag(tagNumber)
return function(fn)
type_tags["TAG_" .. tagNumber] = fn
end
end
-- NOTE: before defining custom type tags, check on
-- https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml
-- if a type tag for that type has been already reserved.
-- RFC8746 multi-dimensional array tag
registerTag(simCBOR.Tags.Array.Nd)(function(values)
local simEigen = require 'simEigen'
local rows, cols = table.unpack(values[1])
local data = values[2]
return simEigen.Matrix(rows, cols, data)
end)
-- RFC8746 typed-arrays: uint8 Typed Array
registerTag(simCBOR.Tags.Array.U8)(function(values)
return sim1.unpackUInt8Table(values)
end)
-- RFC8746 typed-arrays: uint16, little endian, Typed Array
registerTag(simCBOR.Tags.Array.U16LE)(function(values)
return sim1.unpackUInt16Table(values)
end)
-- RFC8746 typed-arrays: uint32, little endian, Typed Array
registerTag(simCBOR.Tags.Array.U32LE)(function(values)
return sim1.unpackUInt32Table(values)
end)
-- RFC8746 typed-arrays: sint32, little endian, Typed Array
registerTag(simCBOR.Tags.Array.S32LE)(function(values)
return sim1.unpackInt32Table(values)
end)
-- RFC8746 typed-arrays: sint64, little endian, Typed Array
registerTag(simCBOR.Tags.Array.S64LE)(function(values)
return sim1.unpackInt64Table(values)
end)
-- RFC8746 typed-arrays: IEEE 754 binary32, little endian, Typed Array
registerTag(simCBOR.Tags.Array.F32LE)(function(values)
return sim1.unpackFloatTable(values)
end)
-- RFC8746 typed-arrays: IEEE 754 binary64, little endian, Typed Array
registerTag(simCBOR.Tags.Array.F64LE)(function(values)
return sim1.unpackDoubleTable(values)
end)
registerTag(simCBOR.Tags.Sim.Color)(function(value)
local rgba = sim1.unpackUInt8Table(value)
for i = 1, 4 do rgba[i] = rgba[i] / 255. end
return Color(rgba)
end)
registerTag(simCBOR.Tags.Sim.Quaternion)(function(value)
local simEigen = require 'simEigen'
return simEigen.Quaternion(value)
end)
registerTag(simCBOR.Tags.Sim.Pose)(function(value)
local simEigen = require 'simEigen'
return simEigen.Pose(value)
end)
registerTag(simCBOR.Tags.Sim.Handle)(function(value)
if value ~= -1 then
return sim.Object(value)
end
end)
registerTag(simCBOR.Tags.Sim.HandleArray)(function(value)
return map(function(h)
if h == -1 then return nil end
return sim.Object(h)
end, value)
end)
function simCBOR.decode(data, opts)
opts = opts or {}
local _type_tags = type_tags
if opts.typeTags then
_type_tags = table.update({}, _type_tags, opts.typeTags)
end
if isbuffer(data) then
data = tostring(data)
end
return cbor.decode(data, 1, _type_tags)
end
function simCBOR.encode(value, opts)
opts = opts or {}
local old_encode_map
if opts.encodeMap then
old_encode_map = cbor.__ENCODE_MAP
cbor.__ENCODE_MAP = table.update({}, cbor.__ENCODE_MAP, opts.encodeMap)
end
local ret = cbor.encode(value)
if old_encode_map then
cbor.__ENCODE_MAP = old_encode_map
end
return ret
end
for _, name in ipairs {
'isfloat',
'isinteger',
'isnumber',
'pdecode',
'pencode',
'_VERSION',
'SIMPLE',
'TAG',
'TYPE',
'__ENCODE_MAP',
} do
simCBOR[name] = cbor[name]
end
simCBOR.null = setmetatable(
{},
{
__tocbor = function() return '\xF6' end,
__tostring = function() return 'nil' end,
}
)
return simCBOR