-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathserviceFactory.js
145 lines (131 loc) · 7.27 KB
/
serviceFactory.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
/* 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';
const Service = require('./service');
const Characteristic = require('./characteristic');
const Descriptor = require('./descriptor');
/**
* Class that provides factory methods for creating `Service`, `Characteristic` and `Descriptor` instances.
*/
class ServiceFactory {
/**
* Factory method to create a service in the Bluetooth `Device's` 'local.server' GATT attribute table.
*
* @param {string} uuid A 128-bit or 16-bit unique identifier for this service.
* @param {string} serviceType The server service type. 'primary' (default) or `secondary`.
* @returns {Service} A newly created `Service` instance.
*/
createService(uuid, serviceType) {
serviceType = typeof serviceType !== 'undefined' ? serviceType : 'primary';
return new Service('local.server', uuid, serviceType);
}
/**
* Factory method to create a characteristic in the Bluetooth `Device's` GATT attribute table.
*
* @param {Service} service The `Service` instance this characteristic is to be added to.
* @param {string} uuid A 128-bit or 16-bit unique identifier for this characteristic.
* @param {array} value The initial value of this characteristic.
* @param {Object} properties This GATT characteristic's metadata.
* Available characteristic properties:
* <ul>
* <li>{boolean} broadcast: Broadcasting of the value permitted.
* <li>{boolean} read: Reading the value permitted.
* <li>{boolean} write: Writing the value with Write Request permitted.
* <li>{boolean} writeWoResp: Writing the value with Write Command permitted.
* <li>{boolean} reliableWrite: Writing the value with Queued Write operations permitted.
* <li>{boolean} notify: Notications of the value permitted.
* <li>{boolean} indicate: Indications of the value permitted.
* <li>{boolean} authSignedWr: Writing the value with Signed Write Command permitted.
* <li>{boolean} wrAux: Writing the Characteristic User Description descriptor permitted.
* </ul>
* @param {Object} options This GATT characteristic's attribute's metadata.
* Available characteristic options:
* <ul>
* <li>{string} readPerm: Read permissions.
* <li>{string} writePerm: Write permissions.
* <li>{boolean} variableLength: Variable length attribute.
* <li>{number} maxLength: Maximum attribute value length in bytes, see ref BLE_GATTS_ATTR_LENS_MAX for maximum values.
* <li>{boolean} readAuth: Read authorization and value will be requested from the application on every read operation.
* <li>{boolean} writeAuth: Write authorization will be requested from the application on every Write Request operation (but not Write Command).
* <li>Note: vloc = this._bleDriver.BLE_GATTS_VLOC_STACK; // Attribute Value is located in stack memory, no user memory is required.
* </ul>
* @returns {Characteristic} A newly created `Characteristic` instance.
*/
createCharacteristic(service, uuid, value, properties, options) {
// TODO: we are mutating the service parameter here. May want to re-work this method.
if (!service) {
throw new Error('Service to add characteristics to must be provided.');
}
if (service._factory_characteristics === undefined) {
service._factory_characteristics = [];
}
const characteristic = new Characteristic(service.instanceId, uuid, value, properties, options);
service._factory_characteristics.push(characteristic);
return characteristic;
}
/**
* Factory method to create a descriptor in the Bluetooth `Device's` GATT attribute table.
*
* @param {Characteristic} characteristic The `Characteristic` instance this descriptor is to be added to.
* @param {string} uuid A 128-bit or 16-bit unique identifier for this descriptor.
* @param {array} value The initial value of this descriptor.
* @param {Object} options This GATT descriptor's attribute's metadata.
* Available descriptor options:
* <ul>
* <li>{string} readPerm: Read permissions.
* <li>{string} writePerm: Write permissions.
* <li>{boolean} variableLength: Variable length attribute.
* <li>{number} maxLength: Maximum attribute value length in bytes, see ref BLE_GATTS_ATTR_LENS_MAX for maximum values.
* <li>{boolean} readAuth: Read authorization and value will be requested from the application on every read operation.
* <li>{boolean} writeAuth: Write authorization will be requested from the application on every Write Request operation (but not Write Command).
* <li>Note: vloc = this._bleDriver.BLE_GATTS_VLOC_STACK; // Attribute Value is located in stack memory, no user memory is required.
* </ul>
* @returns {Descriptor} A newly created `Descriptor` instance.
*/
createDescriptor(characteristic, uuid, value, options) {
// TODO: we are mutating the characteristic parameter here. May want to re-work this method.
if (!characteristic) {
throw new Error('Characteristic to add descriptor to must be provided.');
}
if (characteristic._factory_descriptors === undefined) {
characteristic._factory_descriptors = [];
}
const descriptor = new Descriptor(characteristic.instanceId, uuid, value, options);
characteristic._factory_descriptors.push(descriptor);
return descriptor;
}
}
module.exports = ServiceFactory;