This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
buildControlBuffer.js
81 lines (56 loc) · 2.03 KB
/
buildControlBuffer.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
/*
Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.
SPDX-License-Identifier: LicenseRef-.amazon.com.-AmznSL-1.0
Licensed under the Amazon Software License (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file 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.
*/
'use strict';
const fs = require('fs');
var accessories_pb = require('./amaprotos/accessories_pb');
var device_pb = require('./amaprotos/device_pb.js');
var common_pb = require('./amaprotos/common_pb.js');
// ce is an instance of ControlEnvelope
function buildControlBuffer(ce) {
var buf = ce.serializeBinary();
var finalArray = new Uint8Array(3 + buf.length);
// Hardcoding the first two bytes. More efficient.
finalArray[0] = 0x10;
finalArray[1] = 0x00;
finalArray[2] = buf.length;
finalArray.set(buf, 3);
// Uncomment the lines for debugging
// var s = arrayToHexString(finalArray);
// fs.writeFileSync('/tmp/my.txt', s);
return finalArray;
}
function buildUnsupportedResponse(cmd) {
var res = new accessories_pb.Response();
res.setErrorCode(common_pb.ErrorCode.UNSUPPORTED);
var ce = new accessories_pb.ControlEnvelope();
ce.setCommand(cmd);
ce.setResponse(res);
return buildControlBuffer(ce);
}
// For debugging purposes
// Builds two-digit hex values separated by space
function arrayToHexString(a) {
var ret = "";
var len = a.length;
for(let i=0;i<len;i++) {
var x = a[i];
var value = ('0' + x.toString(16)).slice(-2);
if (i > 0) {
ret = ret + ' ';
}
ret = ret + value;
}
return ret;
}
module.exports.buildControlBuffer = buildControlBuffer;
module.exports.buildUnsupportedResponse = buildUnsupportedResponse;