-
Notifications
You must be signed in to change notification settings - Fork 41
/
adapterState.js
186 lines (165 loc) · 5.86 KB
/
adapterState.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
/* Copyright (c) 2010 - 2017, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Use in source and binary forms, redistribution in binary form only, with
* or without modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 2. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 3. This software, with or without modification, must only be used with a Nordic
* Semiconductor ASA integrated circuit.
*
* 4. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
'use strict';
/**
* Class that provides state management for an `Adapter`.
*/
class AdapterState {
/**
* Create an object to store `Adapter's` state.
*
* @constructor
* @param {string} instanceId The unique Id of this adapter.
* @param {string} port The port this adapter uses. For example it can be 'COM1', '/dev/ttyUSB0' or similar.
* @param {Number} serialNumber The serial number of the hardware device being controlled by this adapter.
*/
constructor(instanceId, port, serialNumber) {
this._instanceId = `${instanceId}.${port}`;
this._port = port;
this._serialNumber = serialNumber;
this._address = null;
this._addressType = null;
/**
* The baud rate that the adapter's serial port is configured with.
* @type {number}
*/
this.baudRate = null;
/**
* The parity that the adapter's serial port is configured with.
* Can be either 'none' or 'even'.
* @type {string}
*/
this.parity = null;
/**
* Whether flow control is configured for this adapter's serial port.
* Can be either 'none' or 'hw'.
* @type {string}
*/
this.flowControl = null;
/**
* Whether the adapter is in the process of being opened.
* @type {boolean}
*/
this.opening = false;
/**
* Whether the adapter is available and successfully opened.
* @type {boolean}
*/
this.available = false;
/**
* Whether the BLE stack has been enabled.
* @type {boolean}
*/
this.bleEnabled = false;
/**
* Whether the adapter is currently scanning for devices.
* @type {boolean}
*/
this.scanning = false;
/**
* Whether the adapter is currently advertising.
* @type {boolean}
*/
this.advertising = false;
/**
* Whether the adapter is currently connecting to a device.
* @type {boolean}
*/
this.connecting = false;
/**
* The device name that is related to this adapter.
* @type {string}
*/
this.name = null;
/**
* The SoftDevice firmware version used by this adapter.
* @type {string}
*/
this.firmwareVersion = null;
}
/**
* Get the instanceId of this adapter.
* @returns {string} Unique Id of this adapter.
*/
get instanceId() {
return this._instanceId;
}
/**
* Get the port this adapter uses.
* @returns {string} The port this adapter uses. For example it can be 'COM1', '/dev/ttyUSB0' or similar.
*/
get port() {
return this._port;
}
/**
* Get the serial number of the hardware device this adapter controls.
* @returns {Number} The serial number of the hardware device being controlled by this adapter.
*/
get serialNumber() {
return this._serialNumber;
}
get powered() {
// TODO: ?
}
/**
* Get the Bluetooth address.
* @returns {null|string} The Bluetooth address of the Bluetooth device this adapter controls.
*/
get address() {
return this._address;
}
/**
* Set the Bluetooth address.
* @param {string} address The new Bluetooth address of the Bluetooth device this adapter controls.
*/
set address(address) {
if (typeof address === 'string') {
this._address = address;
this._addressType = 'BLE_GAP_ADDR_TYPE_RANDOM_STATIC';
} else {
this._address = address.address;
this._addressType = address.type;
}
}
/**
* Get the BLE address type. 'BLE_GAP_ADDR_TYPE_RANDOM_STATIC' or `BLE_GAP_ADDR_TYPE_PUBLIC`.
* @returns {null|string} BLE address type of the device adapter controls.
*/
get addressType() {
return this._addressType;
}
}
module.exports = AdapterState;