/
GattServer.kt
328 lines (286 loc) · 14.2 KB
/
GattServer.kt
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Copyright (C) 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
package com.ul.ims.gmdl.bleofflinetransfer.peripheral
import android.bluetooth.*
import android.bluetooth.BluetoothGattDescriptor.PERMISSION_WRITE
import android.content.Context
import android.util.Log
import com.ul.ims.gmdl.bleofflinetransfer.*
import com.ul.ims.gmdl.bleofflinetransfer.common.BleEventListener
import com.ul.ims.gmdl.bleofflinetransfer.common.DataNotifier
import com.ul.ims.gmdl.bleofflinetransfer.common.GattCommon
import com.ul.ims.gmdl.bleofflinetransfer.config.ServiceCharacteristics
import com.ul.ims.gmdl.bleofflinetransfer.exceptions.BluetoothException
import com.ul.ims.gmdl.bleofflinetransfer.exceptions.GattException
import com.ul.ims.gmdl.bleofflinetransfer.exceptions.TransportLayerException
import com.ul.ims.gmdl.bleofflinetransfer.utils.ByteUtils
import com.ul.ims.gmdl.bleofflinetransfer.utils.ByteUtils.decodeData
import com.ul.ims.gmdl.bleofflinetransfer.utils.ByteUtils.deviceString
import com.ul.ims.gmdl.offlinetransfer.executorLayer.IExecutorEventListener
import com.ul.ims.gmdl.offlinetransfer.transportLayer.EventType
import java.io.ByteArrayOutputStream
import java.util.*
class GattServer (
private val context: Context,
private val bluetoothManager: BluetoothManager,
private val bleServiceCharacteristics: ServiceCharacteristics,
private val publicKeyHash: ByteArray,
override var bleEventListener: BleEventListener?
) : GattCommon, BluetoothGattServerCallback() {
override var stateCharacteristic: BluetoothGattCharacteristic? = null
override var serverToClientCharacteristic: BluetoothGattCharacteristic? = null
override var clientToServerCharacteristic: BluetoothGattCharacteristic? = null
override var identCharacteristic: BluetoothGattCharacteristic? = null
override var maxSupportedMtu: Int = MAX_MTU
override var dataStream: ByteArrayOutputStream = ByteArrayOutputStream()
override var executorEventListener: IExecutorEventListener? = null
override var bluetoothGattService: BluetoothGattService? = null
private var readyForNextFile: Boolean = true
private var bluetoothGattServer: BluetoothGattServer? = null
private var currentDevice: BluetoothDevice? = null
private var requestId: Int = 0
private var offset: Int = 0
private var dataNotifier: DataNotifier? = null
init {
setup()
}
companion object {
val LOG_TAG = "GattServer"
}
/**
* Setup the Characteristics to be advertised
* **/
private fun setup() {
try {
bluetoothGattServer = bluetoothManager.openGattServer(context, this)
bluetoothGattServer?.let {
bluetoothGattService = BluetoothGattService(bleServiceCharacteristics.serviceUuid,
BluetoothGattService.SERVICE_TYPE_PRIMARY)
// Server2Client
serverToClientCharacteristic = bleServiceCharacteristics.server2ClientCharacteristic
val descriptorServerToClient = BluetoothGattDescriptor(
UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG), PERMISSION_WRITE)
serverToClientCharacteristic?.addDescriptor(descriptorServerToClient)
bluetoothGattService?.addCharacteristic(serverToClientCharacteristic)
// Client2Server
clientToServerCharacteristic = bleServiceCharacteristics.client2ServerCharacteristic
bluetoothGattService?.addCharacteristic(clientToServerCharacteristic)
// State
stateCharacteristic = bleServiceCharacteristics.stateCharacteristic
val descriptorState = BluetoothGattDescriptor(
UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG), PERMISSION_WRITE)
stateCharacteristic?.addDescriptor(descriptorState)
bluetoothGattService?.addCharacteristic(stateCharacteristic)
// Ident
identCharacteristic = bleServiceCharacteristics.identCharacteristic
bluetoothGattService?.addCharacteristic(identCharacteristic)
identCharacteristic?.value = publicKeyHash
Log.d(LOG_TAG, "Setup with Service: ${bluetoothGattService?.uuid}, " +
"serverToClientCharacteristic: ${serverToClientCharacteristic?.uuid} " +
"with Value: ${serverToClientCharacteristic?.value}, " +
"clientToServerCharacteristic: ${clientToServerCharacteristic?.uuid} " +
"with Value: ${clientToServerCharacteristic?.value}, " +
"stateCharacteristic: ${stateCharacteristic?.uuid} with Value: " +
"${stateCharacteristic?.value}, " +
"identCharacteristic: ${identCharacteristic?.uuid} with Value: " +
ByteUtils.bytesToHex(identCharacteristic?.value)
)
}
} catch (ex: IllegalArgumentException) {
Log.e(LOG_TAG, ex.message, ex)
}
}
//region GattServiceInterface
fun startServer() {
getBluetoothGattServer().addService(bluetoothGattService)
getPeripheralEventListener().onBLEEvent("Service UUID = ${bluetoothGattService?.uuid}",
EventType.GATT_SERVICE_STARTED)
}
override fun onServiceAdded(status: Int, service: BluetoothGattService?) {
if (bluetoothGattService == service) {
if (BluetoothGatt.GATT_SUCCESS == status) {
Log.d(javaClass.simpleName, "Service started.")
} else {
Log.d(javaClass.simpleName, "Service failed to start?: $status")
getPeripheralEventListener().onBLEEvent("Service not successfully added", EventType.ERROR)
}
}
}
fun stopServer() {
if (getBluetoothGattServer().getService(bluetoothGattService?.uuid) != null) {
getBluetoothGattServer().removeService(bluetoothGattService)
}
}
//region ITransportLayer
fun setDelegate(executorEventListener: IExecutorEventListener) {
this.executorEventListener = executorEventListener
}
fun close() {
try {
currentDevice.takeIf { it != null }.apply {
getBluetoothGattServer().cancelConnection(currentDevice)
}
getBluetoothGattServer().clearServices()
getBluetoothGattServer().close()
} catch (ex: BluetoothException) {
Log.e(LOG_TAG, ex.localizedMessage, ex)
} catch (ex: NullPointerException) {
Log.e(LOG_TAG, ex.localizedMessage, ex)
}
currentDevice = null
}
fun write(data: ByteArray, chunkSize: Int) {
serverToClientCharacteristic?.let {
dataNotifier = DataNotifier(data, chunkSize, getBluetoothGattServer(), it, getPeripheralEventListener(), currentDevice)
val notifier = dataNotifier
if (notifier != null)
notifier.notifyInParts()
else
getPeripheralEventListener().onBLEEvent("dataNotifier is null", EventType.ERROR)
}
}
//endregion
fun writeToState(status: Byte) {
Log.i(javaClass.simpleName, "Gatt server writeToState")
stateCharacteristic?.value = byteArrayOf(status)
val stateStatusWritten = getBluetoothGattServer().notifyCharacteristicChanged(currentDevice,stateCharacteristic,false)
Log.i(javaClass.simpleName, "writeCharacteristic(${ByteUtils.bytesToHex(stateCharacteristic?.value)}): status = $status, Successfully written = $stateStatusWritten")
if (!stateStatusWritten) {
throw GattException("Could not write characteristic ${stateCharacteristic?.uuid} (value ${ByteUtils.bytesToHex(stateCharacteristic?.value)}.")
}
}
//region BluetoothGattServerCallback
@Synchronized
override fun onConnectionStateChange(device: BluetoothDevice, status: Int, newState: Int) {
Log.d(javaClass.simpleName, "onConnectionStateChange: ${deviceString(device)}, " +
status + ", " + newState)
//todo: check
status.takeIf { it == BluetoothGatt.GATT_SUCCESS }?.apply {
when(newState) {
BluetoothGatt.STATE_CONNECTED -> {
if (currentDevice == null) {
currentDevice = device
getPeripheralEventListener().onBLEEvent("To device ${deviceString(device)}", EventType.GATT_CONNECTED)
}
else {
Log.i(javaClass.simpleName, "Already connected ${deviceString(device)}")
}
}
BluetoothGatt.STATE_DISCONNECTED -> {
getPeripheralEventListener().onBLEEvent("Disconnected from remote device",
EventType.GATT_DISCONNECTED)
}
}
}
}
@Synchronized
override fun onMtuChanged(device: BluetoothDevice?, mtu: Int) {
this.maxSupportedMtu = mtu
}
@Synchronized
override fun onCharacteristicReadRequest(device: BluetoothDevice, requestId: Int, offset: Int,
characteristic: BluetoothGattCharacteristic
) {
Log.i(javaClass.simpleName, "onCharacteristicReadRequest: ${characteristic.uuid}, " +
"from ${deviceString(device)}, RequestId: $requestId, offset: $offset")
when(characteristic.uuid) {
identCharacteristic?.uuid -> {
getBluetoothGattServer().sendResponse(device, requestId,
BluetoothGatt.GATT_SUCCESS, 0, publicKeyHash)
}
}
}
override fun onCharacteristicWriteRequest(device: BluetoothDevice, requestId: Int,
characteristic: BluetoothGattCharacteristic,
preparedWrite: Boolean, responseNeeded: Boolean,
offset: Int, value: ByteArray?) {
if (device.address != currentDevice?.address) {
Log.i(javaClass.simpleName, "Unexpected device; returning error")
getPeripheralEventListener().onBLEEvent("Unexpected device; returning error", EventType.ERROR)
getBluetoothGattServer().sendResponse(device, requestId, BluetoothGatt.GATT_WRITE_NOT_PERMITTED, offset, null)
return
}
this.requestId = requestId
Log.i(javaClass.simpleName, "onCharacteristicWriteRequest: ${characteristic.uuid}, " +
"from ${deviceString(device)}, RequestId: $requestId, Offset: $offset, " +
"PreparedWrite: $preparedWrite and value: ${ByteUtils.bytesToHex(value)}")
if (characteristic.uuid == bleServiceCharacteristics.stateUuid) {
decodeState(value, requestId)
return
}
if (characteristic.uuid != bleServiceCharacteristics.client2ServerUuid) {
Log.i(javaClass.simpleName, "Unexpected characteristic.")
return
}
Log.i(javaClass.simpleName, "using offset ${this.offset}")
this.offset = offset
if (value == null) {
throw TransportLayerException("Received clientToServerCharacteristic value is null")
}
Log.i(javaClass.simpleName, "Current value of clientToServerCharacteristic: ${ByteUtils.bytesToHex(value)}")
if (isDataPending(value)) {
getPeripheralEventListener().onBLEEvent("Further data awaited", EventType.TRANSFER_IN_PROGRESS)
dataStream.write(decodeData(value))
} else {
dataStream.write(decodeData(value))
val receivedData = dataStream.toByteArray()
Log.i("GattServer", "Reached last chunk of data after splitting into mtu-sized chunks: ${ByteUtils.bytesToHex(receivedData)}")
Log.i("GattServer", "Delegated (to ITransportEventListener) data: ${ByteUtils.bytesToHex(receivedData)}")
getDelegate().onReceive(receivedData)
dataStream.reset()
}
}
@Synchronized
override fun onNotificationSent(device: BluetoothDevice?, status: Int) {
Log.i(javaClass.simpleName, "onNotificationSent: device = $device, send status = $status")
val notifier = dataNotifier
if (notifier != null)
notifier.notificationSentCalledBack()
else
getPeripheralEventListener().onBLEEvent("dataNotifier is null", EventType.ERROR)
}
private fun isDataPending(value: ByteArray): Boolean {
return value[0] == DATA_PENDING
}
private fun decodeState(value: ByteArray?, requestId: Int) {
if (value == null){
throw GattException("Value of stateCharacteristic is null")
}
if (value.size != 1) {
throw GattException("Invalid status")
}
if (value[0] == READY_FOR_TRANSMISSION) {
getDelegate().onEvent(EventType.STATE_READY_FOR_TRANSMISSION.description, requestId)
getPeripheralEventListener().onBLEEvent("Status updated", EventType.STATE_READY_FOR_TRANSMISSION)
}
if (value[0] == TERMINATE_TRANSMISSION) {
getPeripheralEventListener().onBLEEvent("Status updated", EventType.GATT_DISCONNECTED)
}
}
private fun getBluetoothGattServer(): BluetoothGattServer {
return bluetoothGattServer ?: throw BluetoothException("bluetoothGattServer is null")
}
private fun getDelegate(): IExecutorEventListener {
return executorEventListener ?: throw TransportLayerException("Delegate not set")
}
private fun getPeripheralEventListener() : BleEventListener {
return bleEventListener ?: throw BluetoothException("Ble Event listener is null")
}
fun setReadyForNextFile(boolean: Boolean) {
this.readyForNextFile = boolean
}
//endregion
}