incyclist-devices 2.2.9 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/lib/antv2/base/interface.js +2 -2
  2. package/lib/antv2/factories/index.d.ts +1 -0
  3. package/lib/antv2/factories/index.js +17 -0
  4. package/lib/antv2/fe/adapter.js +5 -4
  5. package/lib/antv2/index.d.ts +1 -0
  6. package/lib/antv2/index.js +12 -8
  7. package/lib/base/adpater.js +1 -1
  8. package/lib/ble/adapter-factory.d.ts +20 -24
  9. package/lib/ble/adapter-factory.js +13 -36
  10. package/lib/ble/base/adapter.d.ts +16 -5
  11. package/lib/ble/base/adapter.js +169 -52
  12. package/lib/ble/base/comms.d.ts +2 -74
  13. package/lib/ble/base/comms.js +3 -596
  14. package/lib/ble/base/interface.d.ts +96 -0
  15. package/lib/ble/base/interface.js +544 -0
  16. package/lib/ble/base/peripheral.d.ts +34 -0
  17. package/lib/ble/base/peripheral.js +286 -0
  18. package/lib/ble/base/sensor.d.ts +31 -0
  19. package/lib/ble/base/sensor.js +130 -0
  20. package/lib/ble/base/types.d.ts +7 -0
  21. package/lib/ble/base/types.js +7 -0
  22. package/lib/ble/bindings/mock.js +6 -0
  23. package/lib/ble/bindings/types.d.ts +3 -4
  24. package/lib/ble/ble-interface.d.ts +7 -4
  25. package/lib/ble/ble-interface.js +16 -2
  26. package/lib/ble/ble-peripheral.d.ts +1 -0
  27. package/lib/ble/ble-peripheral.js +7 -11
  28. package/lib/ble/consts.d.ts +0 -6
  29. package/lib/ble/consts.js +1 -7
  30. package/lib/ble/cp/adapter.d.ts +3 -3
  31. package/lib/ble/cp/adapter.js +12 -13
  32. package/lib/ble/cp/comm.d.ts +1 -1
  33. package/lib/ble/cp/comm.js +2 -2
  34. package/lib/ble/cp/index.d.ts +1 -1
  35. package/lib/ble/cp/index.js +2 -2
  36. package/lib/ble/cp/sensor.d.ts +27 -0
  37. package/lib/ble/cp/sensor.js +107 -0
  38. package/lib/ble/elite/comms.d.ts +1 -1
  39. package/lib/ble/elite/comms.js +2 -2
  40. package/lib/ble/factories/adapter-factory.d.ts +32 -0
  41. package/lib/ble/factories/adapter-factory.js +104 -0
  42. package/lib/ble/factories/index.d.ts +2 -0
  43. package/lib/ble/factories/index.js +18 -0
  44. package/lib/ble/factories/interface-factory.d.ts +7 -0
  45. package/lib/ble/factories/interface-factory.js +18 -0
  46. package/lib/ble/fm/adapter.d.ts +9 -4
  47. package/lib/ble/fm/adapter.js +104 -108
  48. package/lib/ble/fm/comms.d.ts +1 -1
  49. package/lib/ble/fm/comms.js +3 -3
  50. package/lib/ble/fm/consts.d.ts +93 -0
  51. package/lib/ble/fm/consts.js +55 -1
  52. package/lib/ble/fm/index.d.ts +1 -1
  53. package/lib/ble/fm/index.js +2 -2
  54. package/lib/ble/fm/sensor.d.ts +44 -0
  55. package/lib/ble/fm/sensor.js +384 -0
  56. package/lib/ble/hr/adapter.d.ts +3 -3
  57. package/lib/ble/hr/adapter.js +5 -8
  58. package/lib/ble/hr/comm.d.ts +1 -1
  59. package/lib/ble/hr/comm.js +2 -2
  60. package/lib/ble/hr/index.d.ts +1 -1
  61. package/lib/ble/hr/index.js +2 -2
  62. package/lib/ble/hr/sensor.d.ts +17 -0
  63. package/lib/ble/hr/sensor.js +52 -0
  64. package/lib/ble/index.d.ts +4 -3
  65. package/lib/ble/index.js +29 -13
  66. package/lib/ble/tacx/adapter.d.ts +4 -2
  67. package/lib/ble/tacx/adapter.js +26 -66
  68. package/lib/ble/tacx/comms.d.ts +1 -1
  69. package/lib/ble/tacx/comms.js +2 -2
  70. package/lib/ble/tacx/consts.d.ts +23 -0
  71. package/lib/ble/tacx/consts.js +27 -0
  72. package/lib/ble/tacx/index.d.ts +1 -1
  73. package/lib/ble/tacx/index.js +2 -2
  74. package/lib/ble/tacx/sensor.d.ts +63 -0
  75. package/lib/ble/tacx/sensor.js +596 -0
  76. package/lib/ble/types.d.ts +81 -52
  77. package/lib/ble/utils.d.ts +7 -2
  78. package/lib/ble/utils.js +84 -9
  79. package/lib/ble/wahoo/adapter.d.ts +2 -2
  80. package/lib/ble/wahoo/adapter.js +4 -72
  81. package/lib/ble/wahoo/comms.d.ts +1 -1
  82. package/lib/ble/wahoo/comms.js +2 -2
  83. package/lib/ble/wahoo/consts.d.ts +16 -0
  84. package/lib/ble/wahoo/consts.js +7 -0
  85. package/lib/ble/wahoo/index.d.ts +1 -1
  86. package/lib/ble/wahoo/index.js +2 -2
  87. package/lib/ble/wahoo/sensor.d.ts +48 -0
  88. package/lib/ble/wahoo/sensor.js +356 -0
  89. package/lib/direct-connect/base/comms.d.ts +3 -0
  90. package/lib/direct-connect/base/comms.js +7 -0
  91. package/lib/direct-connect/base/interface.d.ts +75 -0
  92. package/lib/direct-connect/base/interface.js +306 -0
  93. package/lib/direct-connect/base/peripheral.d.ts +50 -0
  94. package/lib/direct-connect/base/peripheral.js +413 -0
  95. package/lib/direct-connect/base/sensor.d.ts +3 -0
  96. package/lib/direct-connect/base/sensor.js +7 -0
  97. package/lib/direct-connect/bindings/index.d.ts +1 -0
  98. package/lib/direct-connect/bindings/index.js +17 -0
  99. package/lib/direct-connect/bindings/types.d.ts +36 -0
  100. package/lib/direct-connect/bindings/types.js +2 -0
  101. package/lib/direct-connect/consts.d.ts +17 -0
  102. package/lib/direct-connect/consts.js +20 -0
  103. package/lib/direct-connect/index.d.ts +3 -0
  104. package/lib/direct-connect/index.js +22 -0
  105. package/lib/direct-connect/messages/CharacteristicNotification.d.ts +12 -0
  106. package/lib/direct-connect/messages/CharacteristicNotification.js +23 -0
  107. package/lib/direct-connect/messages/DiscoverCharacteristics.d.ts +22 -0
  108. package/lib/direct-connect/messages/DiscoverCharacteristics.js +43 -0
  109. package/lib/direct-connect/messages/DiscoverServices.d.ts +15 -0
  110. package/lib/direct-connect/messages/DiscoverServices.js +33 -0
  111. package/lib/direct-connect/messages/EnableCharacteristicNotifications.d.ts +18 -0
  112. package/lib/direct-connect/messages/EnableCharacteristicNotifications.js +35 -0
  113. package/lib/direct-connect/messages/ReadCharacteristic.d.ts +18 -0
  114. package/lib/direct-connect/messages/ReadCharacteristic.js +34 -0
  115. package/lib/direct-connect/messages/WriteCharacteristic.d.ts +18 -0
  116. package/lib/direct-connect/messages/WriteCharacteristic.js +36 -0
  117. package/lib/direct-connect/messages/error.d.ts +5 -0
  118. package/lib/direct-connect/messages/error.js +18 -0
  119. package/lib/direct-connect/messages/factory.d.ts +6 -0
  120. package/lib/direct-connect/messages/factory.js +44 -0
  121. package/lib/direct-connect/messages/index.d.ts +9 -0
  122. package/lib/direct-connect/messages/index.js +25 -0
  123. package/lib/direct-connect/messages/message.d.ts +20 -0
  124. package/lib/direct-connect/messages/message.js +90 -0
  125. package/lib/direct-connect/types.d.ts +24 -0
  126. package/lib/direct-connect/types.js +9 -0
  127. package/lib/direct-connect/utils.d.ts +5 -0
  128. package/lib/direct-connect/utils.js +73 -0
  129. package/lib/factories/adapters.js +16 -4
  130. package/lib/factories/interfaces.d.ts +2 -1
  131. package/lib/factories/interfaces.js +10 -7
  132. package/lib/index.d.ts +5 -4
  133. package/lib/index.js +6 -2
  134. package/lib/serial/bindings/tcp.d.ts +5 -4
  135. package/lib/serial/bindings/tcp.js +16 -23
  136. package/lib/types/device.d.ts +2 -1
  137. package/lib/types/device.js +1 -0
  138. package/lib/utils/operation.d.ts +17 -0
  139. package/lib/utils/operation.js +20 -0
  140. package/lib/utils/task.d.ts +44 -0
  141. package/lib/utils/task.js +127 -0
  142. package/package.json +3 -1
@@ -0,0 +1,413 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.DirectConnectPeripheral = void 0;
16
+ const task_1 = require("../../utils/task");
17
+ const interface_1 = __importDefault(require("./interface"));
18
+ const messages_1 = require("../messages");
19
+ const events_1 = __importDefault(require("events"));
20
+ const consts_1 = require("../consts");
21
+ const utils_1 = require("../../ble/utils");
22
+ class DirectConnectPeripheral {
23
+ static create(announcement) {
24
+ const { address, port } = announcement;
25
+ const key = `${address}:${port}`;
26
+ if (!this.instances[key])
27
+ this.instances[key] = new DirectConnectPeripheral(announcement);
28
+ return this.instances[key];
29
+ }
30
+ constructor(announcement) {
31
+ this.announcement = announcement;
32
+ this.msgSeqNo = 0;
33
+ this.onDataHandler = this.onData.bind(this);
34
+ this.onPortErrorHandler = this.onPortError.bind(this);
35
+ this.onPortCloseHandler = this.onPortClose.bind(this);
36
+ this.partialBuffer = null;
37
+ this.remainingBuffer = null;
38
+ this.eventEmitter = new events_1.default();
39
+ this.subscribed = [];
40
+ }
41
+ get services() {
42
+ const services = this.announcement.serviceUUIDs;
43
+ return services.map(s => ({ uuid: s }));
44
+ }
45
+ connect() {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ if (this.isConnected())
48
+ return true;
49
+ if (this.isConnecting())
50
+ yield this.connectTask.getPromise();
51
+ this.connectTask = new task_1.InteruptableTask(this.startConnection(), {
52
+ timeout: 1000,
53
+ });
54
+ return yield this.connectTask.run();
55
+ });
56
+ }
57
+ disconnect() {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ try {
60
+ yield this.connectTask.stop();
61
+ yield this.stopConnection();
62
+ delete this.socket;
63
+ }
64
+ catch (err) {
65
+ return false;
66
+ }
67
+ return true;
68
+ });
69
+ }
70
+ isConnected() {
71
+ return this.socket !== undefined;
72
+ }
73
+ isConnecting() {
74
+ var _a;
75
+ return (_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.isRunning();
76
+ }
77
+ onDisconnect(callback) {
78
+ this.onDisconnectHandler = callback;
79
+ }
80
+ discoverServices() {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const seqNo = this.getNextSeqNo();
83
+ const message = new messages_1.DiscoverServiceMessage();
84
+ const request = message.createRequest(seqNo, {});
85
+ this.logEvent({ message: 'DiscoverServices request', path: this.getPath(), raw: request.toString('hex') });
86
+ const response = yield this.send(seqNo, request);
87
+ const res = message.parseResponse(response);
88
+ const uuids = res.body.serviceDefinitions.map(s => (0, utils_1.beautifyUUID)(s.serviceUUID));
89
+ this.logEvent({ message: 'DiscoverServices response', path: this.getPath(), uuids, raw: request.toString('hex') });
90
+ return res.body.serviceDefinitions.map(s => s.serviceUUID);
91
+ });
92
+ }
93
+ discoverCharacteristics(serviceUUID) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ const seqNo = this.getNextSeqNo();
96
+ const message = new messages_1.DiscoverCharacteristicsMessage();
97
+ const request = message.createRequest(seqNo, { serviceUUID: (0, utils_1.parseUUID)(serviceUUID) });
98
+ this.logEvent({ message: 'DiscoverCharacteritics request', path: this.getPath(), service: (0, utils_1.beautifyUUID)(serviceUUID), raw: request.toString('hex') });
99
+ const response = yield this.send(seqNo, request);
100
+ const res = message.parseResponse(response);
101
+ const service = (0, utils_1.beautifyUUID)(res.body.serviceUUID);
102
+ const characteristics = res.body.characteristicDefinitions.map(cd => `${(0, utils_1.beautifyUUID)(cd.characteristicUUID)}:${cd.properties.join('/')}`);
103
+ this.logEvent({ message: 'DiscoverCharacteritics response', path: this.getPath(), service, characteristics, raw: request.toString('hex') });
104
+ return res.body.characteristicDefinitions.map(c => ({ uuid: c.characteristicUUID, properties: c.properties }));
105
+ });
106
+ }
107
+ subscribe(characteristicUUID, callback) {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ const seqNo = this.getNextSeqNo();
110
+ const message = new messages_1.EnableCharacteristicNotificationsMessage();
111
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), enable: true });
112
+ this.logEvent({ message: 'EnableCharacteristicNotifications request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), enabled: true, raw: request.toString('hex') });
113
+ const response = yield this.send(seqNo, request);
114
+ const res = message.parseResponse(response);
115
+ this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: request.toString('hex') });
116
+ const confirmed = res.body.characteristicUUID;
117
+ if ((0, utils_1.parseUUID)(confirmed) === (0, utils_1.parseUUID)(characteristicUUID)) {
118
+ this.subscribed.push(characteristicUUID);
119
+ this.eventEmitter.on((0, utils_1.parseUUID)(characteristicUUID), (data) => {
120
+ callback(characteristicUUID, data);
121
+ });
122
+ return true;
123
+ }
124
+ return false;
125
+ });
126
+ }
127
+ unsubscribe(characteristicUUID) {
128
+ return __awaiter(this, void 0, void 0, function* () {
129
+ try {
130
+ const seqNo = this.getNextSeqNo();
131
+ const message = new messages_1.EnableCharacteristicNotificationsMessage();
132
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), enable: false });
133
+ this.logEvent({ message: 'EnableCharacteristicNotifications request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), enabled: false, raw: request.toString('hex') });
134
+ const response = yield this.send(seqNo, request);
135
+ const res = message.parseResponse(response);
136
+ this.logEvent({ message: 'EnableCharacteristicNotifications response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID), raw: request.toString('hex') });
137
+ const confirmed = res.body.characteristicUUID;
138
+ if ((0, utils_1.parseUUID)(confirmed) === (0, utils_1.parseUUID)(characteristicUUID)) {
139
+ this.subscribed.splice(this.subscribed.indexOf(characteristicUUID), 1);
140
+ this.eventEmitter.removeAllListeners((0, utils_1.parseUUID)(characteristicUUID));
141
+ return true;
142
+ }
143
+ return false;
144
+ }
145
+ catch (err) {
146
+ this.logEvent({ messsage: 'EnableCharacteristicNotifications failed', reason: err.message });
147
+ return false;
148
+ }
149
+ });
150
+ }
151
+ subscribeAll(callback) {
152
+ return __awaiter(this, void 0, void 0, function* () {
153
+ try {
154
+ let services = [];
155
+ try {
156
+ services = yield this.discoverServices();
157
+ }
158
+ catch (err) {
159
+ this.logEvent({ message: 'could not discover services', reason: err.message });
160
+ }
161
+ const res = [];
162
+ for (let i = 0; i < services.length; i++) {
163
+ const service = services[i];
164
+ let characteristics = [];
165
+ try {
166
+ characteristics = yield this.discoverCharacteristics(service);
167
+ }
168
+ catch (err) {
169
+ this.logEvent({ message: 'could not discover characteristics', service, reason: err.message });
170
+ return false;
171
+ }
172
+ if (!(characteristics === null || characteristics === void 0 ? void 0 : characteristics.length))
173
+ continue;
174
+ for (let j = 0; j < characteristics.length; j++) {
175
+ const characteristic = characteristics[j];
176
+ if (characteristic.properties.includes('notify'))
177
+ yield this.subscribe(characteristic.uuid, callback);
178
+ }
179
+ }
180
+ return true;
181
+ }
182
+ catch (err) {
183
+ this.logEvent({ message: 'could not subscribe', reason: err.message });
184
+ return false;
185
+ }
186
+ });
187
+ }
188
+ subscribeSelected(characteristics, callback) {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ const retry = [];
191
+ for (let i = 0; i < characteristics.length; i++) {
192
+ const uuid = characteristics[i];
193
+ const success = yield this.subscribe(uuid, callback);
194
+ if (!success)
195
+ retry.push(uuid);
196
+ }
197
+ for (let i = 0; i < retry.length; i++) {
198
+ const c = retry[i];
199
+ yield this.subscribe(c.uuid, callback);
200
+ }
201
+ return true;
202
+ });
203
+ }
204
+ unsubscribeAll() {
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ const promises = [];
207
+ this.subscribed.forEach(characteristicUUID => {
208
+ promises.push(this.unsubscribe(characteristicUUID));
209
+ });
210
+ yield Promise.allSettled(promises);
211
+ return true;
212
+ });
213
+ }
214
+ read(characteristicUUID) {
215
+ return __awaiter(this, void 0, void 0, function* () {
216
+ const seqNo = this.getNextSeqNo();
217
+ const message = new messages_1.ReadCharacteristicMessage();
218
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID) });
219
+ this.logEvent({ message: 'ReadCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), raw: request.toString('hex') });
220
+ const response = yield this.send(seqNo, request);
221
+ const res = message.parseResponse(response);
222
+ this.logEvent({ message: 'ReadCharacteristic response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID),
223
+ data: Buffer.from(res.body.characteristicData).toString('hex'),
224
+ raw: request.toString('hex') });
225
+ return Buffer.from(res.body.characteristicData);
226
+ });
227
+ }
228
+ write(characteristicUUID, data, options) {
229
+ return __awaiter(this, void 0, void 0, function* () {
230
+ return new Promise(resolve => {
231
+ if (!(options === null || options === void 0 ? void 0 : options.withoutResponse)) {
232
+ const uuid = (0, utils_1.parseUUID)(characteristicUUID);
233
+ this.eventEmitter.once(uuid, (data) => {
234
+ resolve(data);
235
+ });
236
+ }
237
+ const seqNo = this.getNextSeqNo();
238
+ const message = new messages_1.WriteCharacteristicMessage();
239
+ const request = message.createRequest(seqNo, { characteristicUUID: (0, utils_1.parseUUID)(characteristicUUID), characteristicData: data });
240
+ this.logEvent({ message: 'WriteCharacteristic request', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID),
241
+ data: data.toString('hex'),
242
+ raw: request.toString('hex') });
243
+ this.send(seqNo, request).then((response) => {
244
+ const res = message.parseResponse(response);
245
+ this.logEvent({ message: 'WriteCharacteristic response', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(res.body.characteristicUUID),
246
+ raw: request.toString('hex') });
247
+ if (options === null || options === void 0 ? void 0 : options.withoutResponse) {
248
+ resolve(Buffer.from([]));
249
+ }
250
+ })
251
+ .catch(err => {
252
+ this.logEvent({ message: 'WriteCharacteristic error', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(characteristicUUID), error: err.message });
253
+ });
254
+ });
255
+ });
256
+ }
257
+ startConnection() {
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ const { address, port } = this.announcement;
260
+ try {
261
+ const net = this.getBinding().net;
262
+ this.socket = net.createSocket();
263
+ return new Promise((resolve, reject) => {
264
+ this.socket.once('timeout', () => {
265
+ this.stopConnection().then(() => resolve(false));
266
+ });
267
+ this.socket.once('error', (err) => {
268
+ this.stopConnection().then(() => resolve(false));
269
+ });
270
+ this.socket.once('connect', () => {
271
+ this.socket.on('data', this.onDataHandler);
272
+ this.socket.on('error', this.onPortErrorHandler);
273
+ this.socket.on('close', this.onPortCloseHandler);
274
+ this.socket.on('end', this.onPortCloseHandler);
275
+ resolve(true);
276
+ });
277
+ this.socket.connect(port, address);
278
+ });
279
+ }
280
+ catch (err) {
281
+ return false;
282
+ }
283
+ });
284
+ }
285
+ onPortError(err) {
286
+ this.logEvent({ message: 'port error', path: this.getPath(), reason: err.message });
287
+ }
288
+ onPortClose() {
289
+ return __awaiter(this, void 0, void 0, function* () {
290
+ this.socket.removeAllListeners();
291
+ this.logEvent({ message: 'port closed', path: this.getPath() });
292
+ try {
293
+ yield this.disconnect();
294
+ }
295
+ catch (_a) { }
296
+ if (this.onDisconnectHandler)
297
+ this.onDisconnectHandler();
298
+ });
299
+ }
300
+ getPath() {
301
+ const { address, port } = this.announcement;
302
+ const path = `${address}:${port}`;
303
+ return path;
304
+ }
305
+ stopConnection() {
306
+ return __awaiter(this, void 0, void 0, function* () {
307
+ this.eventEmitter.removeAllListeners();
308
+ if (!this.isConnected())
309
+ return true;
310
+ yield this.unsubscribeAll();
311
+ this.socket.removeAllListeners();
312
+ return new Promise(done => {
313
+ const onClosed = () => {
314
+ this.socket.removeAllListeners();
315
+ delete this.socket;
316
+ done(true);
317
+ };
318
+ const onCloseError = (err) => {
319
+ this.logEvent({ message: 'port error', path: this.getPath(), reason: err.message });
320
+ this.socket.removeAllListeners();
321
+ delete this.socket;
322
+ done(false);
323
+ };
324
+ this.socket.on('end', onClosed);
325
+ this.socket.on('error', onCloseError);
326
+ this.socket.on('close', onClosed);
327
+ this.socket.destroy();
328
+ });
329
+ });
330
+ }
331
+ getNextSeqNo() {
332
+ this.msgSeqNo = (this.msgSeqNo + 1) % 256;
333
+ return this.msgSeqNo;
334
+ }
335
+ send(seqNo, data) {
336
+ return __awaiter(this, void 0, void 0, function* () {
337
+ this.socket.write(data);
338
+ return new Promise(done => {
339
+ this.eventEmitter.once(`response-${seqNo}`, (response) => {
340
+ done(response);
341
+ });
342
+ });
343
+ });
344
+ }
345
+ getNextMessage(data) {
346
+ let incoming = data;
347
+ if (this.partialBuffer) {
348
+ incoming = Buffer.concat([
349
+ Buffer.from(this.partialBuffer),
350
+ Buffer.from(data)
351
+ ]);
352
+ delete this.partialBuffer;
353
+ }
354
+ if (incoming.length < 6) {
355
+ this.partialBuffer = Buffer.from(incoming);
356
+ return null;
357
+ }
358
+ const header = (0, messages_1.parseHeader)(incoming);
359
+ if (incoming.length < header.length + 6) {
360
+ this.partialBuffer = Buffer.from(incoming);
361
+ return null;
362
+ }
363
+ if (incoming.length > header.length + 6) {
364
+ this.remainingBuffer = Buffer.from(incoming.subarray(header.length + 6));
365
+ incoming = incoming.subarray(0, header.length + 6);
366
+ }
367
+ return incoming;
368
+ }
369
+ processRemaining() {
370
+ if (this.remainingBuffer) {
371
+ const next = Buffer.from(this.remainingBuffer);
372
+ delete this.remainingBuffer;
373
+ this.onData(next);
374
+ }
375
+ }
376
+ onData(data) {
377
+ return __awaiter(this, void 0, void 0, function* () {
378
+ const incoming = this.getNextMessage(data);
379
+ if (incoming === null)
380
+ return;
381
+ try {
382
+ const header = (0, messages_1.parseHeader)(incoming);
383
+ if (header.msgId === consts_1.DC_MESSAGE_CHARACTERISTIC_NOTIFICATION) {
384
+ const message = new messages_1.CharacteristicNotificationMessage();
385
+ const notification = message.parseResponse(incoming);
386
+ this.msgSeqNo = notification.header.seqNum;
387
+ const uuid = (0, utils_1.parseUUID)(notification.body.characteristicUUID);
388
+ this.logEvent({ message: 'Characteristic notification', path: this.getPath(), characteristic: (0, utils_1.beautifyUUID)(notification.body.characteristicUUID),
389
+ data: Buffer.from(notification.body.characteristicData).toString('hex') });
390
+ this.eventEmitter.emit(uuid, notification.body.characteristicData);
391
+ }
392
+ else {
393
+ this.eventEmitter.emit(`response-${header.seqNum}`, incoming);
394
+ }
395
+ }
396
+ catch (err) {
397
+ this.logEvent({ message: 'error', fn: 'onData', error: err.message, stack: err.stack, path: this.getPath(), raw: incoming.toString('hex') });
398
+ }
399
+ this.processRemaining();
400
+ });
401
+ }
402
+ getInterface() {
403
+ return interface_1.default.getInstance();
404
+ }
405
+ logEvent(event) {
406
+ this.getInterface().logEvent(event);
407
+ }
408
+ getBinding() {
409
+ return this.getInterface().getBinding();
410
+ }
411
+ }
412
+ exports.DirectConnectPeripheral = DirectConnectPeripheral;
413
+ DirectConnectPeripheral.instances = {};
@@ -0,0 +1,3 @@
1
+ import { BleComms } from "../../ble/base/comms";
2
+ export declare class DirectConnectComms extends BleComms {
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DirectConnectComms = void 0;
4
+ const comms_1 = require("../../ble/base/comms");
5
+ class DirectConnectComms extends comms_1.BleComms {
6
+ }
7
+ exports.DirectConnectComms = DirectConnectComms;
@@ -0,0 +1 @@
1
+ export * from './types';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,36 @@
1
+ import EventEmitter from "events";
2
+ import { PeripheralAnnouncement } from "../../ble/types";
3
+ export interface Socket extends EventEmitter {
4
+ connect(port: number, host: string): Socket;
5
+ destroy(): void;
6
+ write(data: Buffer): boolean;
7
+ }
8
+ export interface NetBinding {
9
+ createSocket(): Socket;
10
+ }
11
+ export interface DirectConnectBinding {
12
+ mdns: MulticastDnsBinding;
13
+ net: NetBinding;
14
+ }
15
+ type KeyValue = {
16
+ [key: string]: any;
17
+ };
18
+ export interface BrowserConfig {
19
+ type: string;
20
+ name?: string;
21
+ protocol?: 'tcp' | 'udp';
22
+ subtypes?: string[];
23
+ txt?: KeyValue;
24
+ }
25
+ export interface MulticastDnsAnnouncement extends PeripheralAnnouncement {
26
+ address: string;
27
+ protocol?: 'tcp' | 'udp';
28
+ port: number;
29
+ serialNo?: string;
30
+ }
31
+ export interface MulticastDnsBinding {
32
+ connect(): void;
33
+ disconnect(): void;
34
+ find(opts: BrowserConfig | null, onup?: (service: MulticastDnsAnnouncement) => void): void;
35
+ }
36
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ export declare const DC_RC_REQUEST_COMPLETED_SUCCESSFULLY = 0;
2
+ export declare const DC_RC_UNKNOWN_MESSAGE_TYPE = 1;
3
+ export declare const DC_RC_UNEXPECTED_ERROR = 2;
4
+ export declare const DC_RC_SERVICE_NOT_FOUND = 3;
5
+ export declare const DC_RC_CHARACTERISTIC_NOT_FOUND = 4;
6
+ export declare const DC_RC_CHARACTERISTIC_OPERATION_NOT_SUPPORTED = 5;
7
+ export declare const DC_RC_CHARACTERISTIC_WRITE_FAILED_INVALID_SIZE = 6;
8
+ export declare const DC_RC_UNKNOWN_PROTOCOL_VERSION = 7;
9
+ export declare const DC_MESSAGE_DISCOVER_SERVICES = 1;
10
+ export declare const DC_MESSAGE_DISCOVER_CHARACTERISTICS = 2;
11
+ export declare const DC_MESSAGE_READ_CHARACTERISTIC = 3;
12
+ export declare const DC_MESSAGE_WRITE_CHARACTERISTIC = 4;
13
+ export declare const DC_MESSAGE_ENABLE_CHARACTERISTIC_NOTIFICATIONS = 5;
14
+ export declare const DC_MESSAGE_CHARACTERISTIC_NOTIFICATION = 6;
15
+ export declare const DC_ERROR_INVALID_MESSAGE_TYPE = 1;
16
+ export declare const DC_ERROR_INVALID_MESSAGE_LENGTH = 2;
17
+ export declare const DC_ERROR_UNKNOWN_MESSAGE_TYPE = 3;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DC_ERROR_UNKNOWN_MESSAGE_TYPE = exports.DC_ERROR_INVALID_MESSAGE_LENGTH = exports.DC_ERROR_INVALID_MESSAGE_TYPE = exports.DC_MESSAGE_CHARACTERISTIC_NOTIFICATION = exports.DC_MESSAGE_ENABLE_CHARACTERISTIC_NOTIFICATIONS = exports.DC_MESSAGE_WRITE_CHARACTERISTIC = exports.DC_MESSAGE_READ_CHARACTERISTIC = exports.DC_MESSAGE_DISCOVER_CHARACTERISTICS = exports.DC_MESSAGE_DISCOVER_SERVICES = exports.DC_RC_UNKNOWN_PROTOCOL_VERSION = exports.DC_RC_CHARACTERISTIC_WRITE_FAILED_INVALID_SIZE = exports.DC_RC_CHARACTERISTIC_OPERATION_NOT_SUPPORTED = exports.DC_RC_CHARACTERISTIC_NOT_FOUND = exports.DC_RC_SERVICE_NOT_FOUND = exports.DC_RC_UNEXPECTED_ERROR = exports.DC_RC_UNKNOWN_MESSAGE_TYPE = exports.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY = void 0;
4
+ exports.DC_RC_REQUEST_COMPLETED_SUCCESSFULLY = 0;
5
+ exports.DC_RC_UNKNOWN_MESSAGE_TYPE = 1;
6
+ exports.DC_RC_UNEXPECTED_ERROR = 2;
7
+ exports.DC_RC_SERVICE_NOT_FOUND = 3;
8
+ exports.DC_RC_CHARACTERISTIC_NOT_FOUND = 4;
9
+ exports.DC_RC_CHARACTERISTIC_OPERATION_NOT_SUPPORTED = 5;
10
+ exports.DC_RC_CHARACTERISTIC_WRITE_FAILED_INVALID_SIZE = 6;
11
+ exports.DC_RC_UNKNOWN_PROTOCOL_VERSION = 7;
12
+ exports.DC_MESSAGE_DISCOVER_SERVICES = 0x01;
13
+ exports.DC_MESSAGE_DISCOVER_CHARACTERISTICS = 0x02;
14
+ exports.DC_MESSAGE_READ_CHARACTERISTIC = 0x03;
15
+ exports.DC_MESSAGE_WRITE_CHARACTERISTIC = 0x04;
16
+ exports.DC_MESSAGE_ENABLE_CHARACTERISTIC_NOTIFICATIONS = 0x05;
17
+ exports.DC_MESSAGE_CHARACTERISTIC_NOTIFICATION = 0x06;
18
+ exports.DC_ERROR_INVALID_MESSAGE_TYPE = 1;
19
+ exports.DC_ERROR_INVALID_MESSAGE_LENGTH = 2;
20
+ exports.DC_ERROR_UNKNOWN_MESSAGE_TYPE = 3;
@@ -0,0 +1,3 @@
1
+ export * from './types';
2
+ export * from './consts';
3
+ export * from './messages';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ const interface_factory_1 = require("../ble/factories/interface-factory");
18
+ const interface_1 = require("./base/interface");
19
+ __exportStar(require("./types"), exports);
20
+ __exportStar(require("./consts"), exports);
21
+ __exportStar(require("./messages"), exports);
22
+ interface_factory_1.BleMultiTransportInterfaceFactory.register('wifi', interface_1.DirectConnectInterfaceFactory);
@@ -0,0 +1,12 @@
1
+ import { EmptyBody, TDCBody, TDCResponse } from "../types";
2
+ import { Message } from "./message";
3
+ export type TDCCharacteristicNotification = TDCResponse<TDCCharacteristicNotificationBody>;
4
+ export interface TDCCharacteristicNotificationBody extends TDCBody {
5
+ characteristicUUID: string;
6
+ characteristicData: Uint8Array;
7
+ }
8
+ export declare class CharacteristicNotificationMessage extends Message<EmptyBody, TDCCharacteristicNotificationBody> {
9
+ constructor(seqNum?: number);
10
+ buildResponseBody(body: TDCCharacteristicNotificationBody): Buffer;
11
+ parseResponseBody(body: Buffer): TDCCharacteristicNotificationBody;
12
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CharacteristicNotificationMessage = void 0;
4
+ const consts_1 = require("../consts");
5
+ const message_1 = require("./message");
6
+ class CharacteristicNotificationMessage extends message_1.Message {
7
+ constructor(seqNum) {
8
+ super(consts_1.DC_MESSAGE_CHARACTERISTIC_NOTIFICATION);
9
+ }
10
+ buildResponseBody(body) {
11
+ return Buffer.concat([Buffer.from(body.characteristicUUID, "hex"),
12
+ Buffer.from(body.characteristicData)]);
13
+ }
14
+ parseResponseBody(body) {
15
+ const characteristicUUID = Buffer.from(body.subarray(0, 16)).toString("hex");
16
+ const characteristicData = body.subarray(16, body.length);
17
+ return {
18
+ characteristicUUID,
19
+ characteristicData,
20
+ };
21
+ }
22
+ }
23
+ exports.CharacteristicNotificationMessage = CharacteristicNotificationMessage;
@@ -0,0 +1,22 @@
1
+ import { BleProperty } from "../../ble/types";
2
+ import { TDCBody, TDCRequest, TDCResponse } from "../types";
3
+ import { Message } from "./message";
4
+ export type TDCDiscoverCharacteristicsRequest = TDCRequest<TDCDiscoverCharacteristicsRequestBody>;
5
+ export type TDCDiscoverCharacteristicsResponse = TDCResponse<TDCDiscoverCharacteristicsResponseBody>;
6
+ export interface TDCDiscoverCharacteristicsRequestBody extends TDCBody {
7
+ serviceUUID: string;
8
+ }
9
+ export interface TDCDiscoverCharacteristicsResponseBody extends TDCBody {
10
+ serviceUUID: string;
11
+ characteristicDefinitions: Array<{
12
+ characteristicUUID: string;
13
+ properties: BleProperty[];
14
+ }>;
15
+ }
16
+ export declare class DiscoverCharacteristicsMessage extends Message<TDCDiscoverCharacteristicsRequestBody, TDCDiscoverCharacteristicsResponseBody> {
17
+ constructor();
18
+ parseRequestBody(body: Buffer): TDCDiscoverCharacteristicsRequestBody;
19
+ buildRequestBody(body: TDCDiscoverCharacteristicsRequestBody): Buffer;
20
+ buildResponseBody(body: TDCDiscoverCharacteristicsResponseBody): Buffer;
21
+ parseResponseBody(body: Buffer): TDCDiscoverCharacteristicsResponseBody;
22
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DiscoverCharacteristicsMessage = void 0;
4
+ const utils_1 = require("../../ble/utils");
5
+ const consts_1 = require("../consts");
6
+ const message_1 = require("./message");
7
+ class DiscoverCharacteristicsMessage extends message_1.Message {
8
+ constructor() {
9
+ super(consts_1.DC_MESSAGE_DISCOVER_CHARACTERISTICS);
10
+ }
11
+ parseRequestBody(body) {
12
+ const serviceUUID = body.subarray(0, 16).toString("hex");
13
+ return {
14
+ serviceUUID,
15
+ };
16
+ }
17
+ buildRequestBody(body) {
18
+ return Buffer.from(body.serviceUUID, "hex");
19
+ }
20
+ buildResponseBody(body) {
21
+ const serviceUUIDBuffer = Buffer.from(body.serviceUUID, "hex");
22
+ const characteristicBuffers = body.characteristicDefinitions.map((charDef) => Buffer.concat([
23
+ Buffer.from(charDef.characteristicUUID, "hex"),
24
+ Buffer.from([(0, utils_1.propertyVal)(charDef.properties)]),
25
+ ]));
26
+ return Buffer.concat([serviceUUIDBuffer, ...characteristicBuffers]);
27
+ }
28
+ parseResponseBody(body) {
29
+ const length = body.length;
30
+ const serviceUUID = body.subarray(0, 16).toString("hex");
31
+ const characteristicDefinitions = [];
32
+ for (let i = 16; i < length; i += 17) {
33
+ const characteristicUUID = Buffer.from(body.subarray(i, i + 16)).toString("hex");
34
+ const properties = (0, utils_1.propertyFromVal)(body.readUInt8(i + 16));
35
+ characteristicDefinitions.push({ characteristicUUID, properties });
36
+ }
37
+ return {
38
+ serviceUUID,
39
+ characteristicDefinitions,
40
+ };
41
+ }
42
+ }
43
+ exports.DiscoverCharacteristicsMessage = DiscoverCharacteristicsMessage;