forked from baalexander/rosnodejs
/
fields.js
308 lines (280 loc) · 7.82 KB
/
fields.js
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
303
304
305
306
307
// A set of functions for checking, reading, and writing ROS message fields,
// including built-in fields, array fields, and message fields.
var fields = exports
// An array of all the ROS Message primitive types.
fields.primitiveTypes = [
'char'
, 'byte'
, 'bool'
, 'int8'
, 'uint8'
, 'int16'
, 'uint16'
, 'int32'
, 'uint32'
, 'int64'
, 'uint64'
, 'float32'
, 'float64'
, 'string'
, 'time'
, 'duration'
]
// Checks if the field is a built-in type.
//
// Examples: bool, string, and duration.
fields.isPrimitiveType = function(fieldType) {
return (fields.primitiveTypes.indexOf(fieldType) >= 0)
}
// Checks if the field is an array.
//
// Examples: int8[] and geometry_msgs.Twist[6]
fields.isArray = function(fieldType) {
// Looks for [] at the end. Can contain a number inside like float64[32].
var regex = /.*\[*\]$/
return (fieldType.match(regex) !== null)
}
// Checks if the field is a message type.
//
// Examples: std_msgs/String and geometry_msgs/Twist
fields.isMessageType = function(fieldType) {
// Is considered a message type if not a primitive and not an array
return !this.isPrimitiveType(fieldType) && !this.isArray(fieldType)
}
// Returns the field type for the array.
//
// Example: std_msgs/String[] returns std_msgs/String.
fields.getFieldTypeOfArray = function(arrayType) {
return arrayType.split('[')[0]
}
// Converts the string value of a field to its JavaScript data type.
fields.parseField = function(fieldType, fieldValue) {
var parsedValue = fieldValue
if (fieldType === 'bool') {
if (fieldValue === '1') {
parsedValue = true
}
else {
parsedValue = false
}
}
else if (fieldType === 'int8') {
parsedValue = parseInt(fieldValue)
}
else if (fieldType === 'uint8') {
parsedValue = parseInt(fieldValue)
parsedValue = Math.abs(parsedValue)
}
else if (fieldType === 'int16') {
parsedValue = parseInt(fieldValue)
}
else if (fieldType === 'uint16') {
parsedValue = parseInt(fieldValue)
parsedValue = Math.abs(parsedValue)
}
else if (fieldType === 'int32') {
parsedValue = parseInt(fieldValue)
}
else if (fieldType === 'uint32') {
parsedValue = parseInt(fieldValue)
parsedValue = Math.abs(parsedValue)
}
else if (fieldType === 'int64') {
throwUnsupportedInt64Exception()
}
else if (fieldType === 'uint64') {
throwUnsupportedInt64Exception()
}
else if (fieldType === 'float32') {
parsedValue = parseFloat(fieldValue)
}
else if (fieldType === 'float64') {
parsedValue = parseFloat(fieldValue)
}
return parsedValue
}
// Reads a primitive field from the buffer.
fields.readFieldFromBuffer = function(fieldType, buffer, bufferOffset) {
var fieldValue = null
if (fieldType === 'bool') {
fieldValue = buffer.readUInt8(bufferOffset)
}
else if (fieldType === 'int8') {
fieldValue = buffer.readInt8(bufferOffset)
}
else if (fieldType === 'uint8') {
fieldValue = buffer.readUInt8(bufferOffset)
}
else if (fieldType === 'int16') {
fieldValue = buffer.readInt16LE(bufferOffset)
}
else if (fieldType === 'uint16') {
fieldValue = buffer.readUInt16LE(bufferOffset)
}
else if (fieldType === 'int32') {
fieldValue = buffer.readInt32LE(bufferOffset)
}
else if (fieldType === 'uint32') {
fieldValue = buffer.readUInt32LE(bufferOffset)
}
else if (fieldType === 'int64') {
throwUnsupportedInt64Exception()
}
else if (fieldType === 'uint64') {
throwUnsupportedInt64Exception()
}
else if (fieldType === 'float32') {
fieldValue = buffer.readFloatLE(bufferOffset)
}
else if (fieldType === 'float64') {
fieldValue = buffer.readDoubleLE(bufferOffset)
}
else if (fieldType === 'string') {
var fieldLength = buffer.readUInt32LE(bufferOffset)
, fieldStart = bufferOffset + 4
, fieldEnd = fieldStart + fieldLength
fieldValue = buffer.toString('utf8', fieldStart, fieldEnd)
}
return fieldValue
}
// Writes a primitive field to the buffer.
fields.writeFieldToBuffer = function(fieldType, fieldValue, buffer, bufferOffset) {
if (fieldType === 'bool') {
buffer.writeUInt8(fieldValue, bufferOffset)
}
else if (fieldType === 'int8') {
buffer.writeInt8(fieldValue, bufferOffset)
}
else if (fieldType === 'uint8') {
buffer.writeUInt8(fieldValue, bufferOffset)
}
else if (fieldType === 'int16') {
buffer.writeInt16LE(fieldValue, bufferOffset)
}
else if (fieldType === 'uint16') {
buffer.writeUInt16LE(fieldValue, bufferOffset)
}
else if (fieldType === 'int32') {
buffer.writeInt32LE(fieldValue, bufferOffset)
}
else if (fieldType === 'uint32') {
buffer.writeUInt32LE(fieldValue, bufferOffset)
}
else if (fieldType === 'int64') {
throwUnsupportedInt64Exception()
}
else if (fieldType === 'uint64') {
throwUnsupportedInt64Exception()
}
else if (fieldType === 'float32') {
buffer.writeFloatLE(fieldValue, bufferOffset)
}
else if (fieldType === 'float64') {
buffer.writeDoubleLE(fieldValue, bufferOffset)
}
else if (fieldType === 'string') {
buffer.writeUInt32LE(fieldValue.length, bufferOffset)
bufferOffset += 4
buffer.write(fieldValue, bufferOffset, 'ascii')
}
}
// Returns the size of a field in bytes. Includes primitive, array, and message
// type fields.
fields.getFieldSize = function(fieldType, fieldValue) {
var fieldSize = 0
if (this.isPrimitiveType(fieldType)) {
return getPrimitiveFieldSize(fieldType, fieldValue)
}
else if (this.isArray(fieldType)) {
return getArraySize(fieldType, fieldValue)
}
else if (this.isMessageType(fieldType)) {
return getMessageSize(fieldValue)
}
return fieldSize
}
// Returns the size of a built-in field in bytes.
function getPrimitiveFieldSize(fieldType, fieldValue) {
var fieldSize = 0
if (fieldType === 'char') {
fieldSize = 1
}
else if (fieldType === 'byte') {
fieldSize = 1
}
else if (fieldType === 'bool') {
fieldSize = 1
}
else if (fieldType === 'int8') {
fieldSize = 1
}
else if (fieldType === 'uint8') {
fieldSize = 1
}
else if (fieldType === 'int16') {
fieldSize = 2
}
else if (fieldType === 'uint16') {
fieldSize = 2
}
else if (fieldType === 'int32') {
fieldSize = 4
}
else if (fieldType === 'uint32') {
fieldSize = 4
}
else if (fieldType === 'int64') {
fieldSize = 8
}
else if (fieldType === 'uint64') {
fieldSize = 8
}
else if (fieldType === 'float32') {
fieldSize = 4
}
else if (fieldType === 'float64') {
fieldSize = 8
}
else if (fieldType === 'string') {
if (fieldValue !== undefined) {
fieldSize = fieldValue.length + 4
}
}
else if (fieldType === 'time') {
fieldSize = 8
}
else if (fieldType === 'duration') {
fieldSize = 8
}
return fieldSize
}
// Returns the size of an array field in bytes.
function getArraySize(arrayType, array) {
var arraySize = 4
, arrayFieldType = fields.getFieldTypeOfArray(arrayType)
for (var i = 0; i < array.length; i++) {
var value = array[i]
arraySize += fields.getFieldSize(arrayFieldType, value)
}
return arraySize
}
// Returns the size of a message type field in bytes. This includes any inner
// messages in the calculation too.
function getMessageSize(message) {
var messageSize = 0
, fieldOrder = message.get('fields')
, fieldTypes = message.get('fieldTypes')
for (var fieldIndex = 0; fieldIndex < fieldOrder.length; fieldIndex++) {
var fieldName = fieldOrder[fieldIndex]
, fieldType = fieldTypes[fieldName]
, fieldValue = message.get(fieldName)
, fieldSize = fields.getFieldSize(fieldType, fieldValue)
messageSize += fieldSize
}
return messageSize
}
// 64-bit integers are not supported at this time.
function throwUnsupportedInt64Exception() {
var error = new Error('int64 and uint64 are currently unsupported field types. See https://github.com/baalexander/rosnodejs/issues/2')
throw error
}