forked from noble/bleno
-
Notifications
You must be signed in to change notification settings - Fork 51
/
test-matter.js
162 lines (131 loc) · 4.23 KB
/
test-matter.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
const bleno = require('./index');
let handshakeDone = false;
let C2Callback = null;
console.log('bleno-matter');
class C3DynamicReadOnlyCharacteristic extends bleno.Characteristic {
constructor() {
super({
uuid: '64630238-8772-45F2-B87D-748A83218F04',
properties: ['read']
});
}
onReadRequest(offset, callback) {
let result = this.RESULT_SUCCESS;
let data = Buffer.from('dynamic value');
console.log('C3DynamicReadOnlyCharacteristic read request: ' + data.toString('hex') + ' ' + offset);
if (offset > data.length) {
result = this.RESULT_INVALID_OFFSET;
data = null;
} else {
data = data.slice(offset);
}
callback(result, data);
}
}
class C1WriteOnlyCharacteristic extends bleno.Characteristic {
constructor() {
super({
uuid: '18EE2EF5-263D-4559-959F-4F9C429F9D11',
properties: ['write']
});
}
onWriteRequest(data, offset, withoutResponse, callback) {
console.log('C1WriteOnlyCharacteristic write request: ' + data.toString('hex') + ' ' + offset + ' ' + withoutResponse);
if (data[0] === 0x65 && data[1] === 0x6c) {
handshakeDone = true;
callback();
return;
}
callback(this.RESULT_SUCCESS);
}
}
class C2IndicateOnlyCharacteristic extends bleno.Characteristic {
constructor() {
super({
uuid: '18EE2EF5-263D-4559-959F-4F9C429F9D12',
properties: ['indicate']
});
}
onSubscribe(maxValueSize, updateValueCallback) {
console.log('C2IndicateOnlyCharacteristic subscribe ' + maxValueSize);
if (handshakeDone) {
C2Callback = updateValueCallback;
console.log('C2IndicateOnlyCharacteristic handshake response');
updateValueCallback(Buffer.from("656c04000106", "hex"));
}
}
onUnsubscribe() {
console.log('C2IndicateOnlyCharacteristic unsubscribe');
if (this.changeInterval) {
clearInterval(this.changeInterval);
this.changeInterval = null;
}
}
onIndicate() {
console.log('C2IndicateOnlyCharacteristic on indicate');
}
}
class MatterService extends bleno.PrimaryService {
constructor() {
super({
uuid: 'fff6', // Matter
characteristics: [
new C3DynamicReadOnlyCharacteristic(),
new C1WriteOnlyCharacteristic(),
new C2IndicateOnlyCharacteristic()
]
});
}
}
bleno.on('stateChange', function(state) {
console.log('on -> stateChange: ' + state + ', address = ' + bleno.address);
if (state === 'poweredOn') {
//bleno.startAdvertising('test', ['fffffffffffffffffffffffffffffff0']);
// Data as defined 5.4.2.5.6 Matter Spec for a hardcoded product and vendor Id
// Theoretically use this QR Code https://project-chip.github.io/connectedhomeip/qrcode.html?data=MT:86PS0KQS02-10648G00
const advertiseBuffer = Buffer.alloc(15);
advertiseBuffer.writeUInt8(0x02, 0);
advertiseBuffer.writeUInt8(0x01, 1);
advertiseBuffer.writeUInt8(0x06, 2);
advertiseBuffer.writeUInt8(0x0B, 3);
advertiseBuffer.writeUInt8(0x16, 4);
advertiseBuffer.writeUInt16LE(0xFFF6, 5);
advertiseBuffer.writeUInt8(0x00, 7);
advertiseBuffer.writeUInt16LE(1234, 8);
advertiseBuffer.writeUInt16LE(65522, 10);
advertiseBuffer.writeUInt16LE(32770, 12);
advertiseBuffer.writeUInt8(0x00, 14);
bleno.startAdvertisingWithEIRData(advertiseBuffer);
} else {
bleno.stopAdvertising();
}
});
// Linux only events /////////////////
bleno.on('accept', function(clientAddress) {
console.log('on -> accept, client: ' + clientAddress);
bleno.updateRssi();
});
bleno.on('disconnect', function(clientAddress) {
console.log('on -> disconnect, client: ' + clientAddress);
});
bleno.on('rssiUpdate', function(rssi) {
console.log('on -> rssiUpdate: ' + rssi);
});
//////////////////////////////////////
bleno.on('mtuChange', function(mtu) {
console.log('on -> mtuChange: ' + mtu);
});
bleno.on('advertisingStart', function(error) {
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
if (!error) {
bleno.setServices([
new MatterService()
]);
}
});
bleno.on('advertisingStop', function() {
console.log('on -> advertisingStop');
});
bleno.on('servicesSet', function(error) {
console.log('on -> servicesSet: ' + (error ? 'error ' + error : 'success'));
});