-
Notifications
You must be signed in to change notification settings - Fork 6
/
SerialStenoMachine.kt
359 lines (309 loc) · 8.73 KB
/
SerialStenoMachine.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// This file is part of Dotterel which is released under GPL-2.0-or-later.
// See file <LICENSE.txt> or go to <http://www.gnu.org/licenses/> for full license details.
package nimble.dotterel.machines
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbDeviceConnection
import android.hardware.usb.UsbManager
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import com.eclipsesource.json.Json
import com.eclipsesource.json.JsonObject
import java.io.Closeable
import nimble.dotterel.Dotterel
import nimble.dotterel.StenoMachine
import nimble.dotterel.translation.KeyLayout
import nimble.dotterel.translation.KeyMap
import nimble.dotterel.translation.Stroke
import nimble.dotterel.util.mapValues
import java.io.IOException
import nimble.dotterel.StenoMachineTracker
val UsbDevice.id: String
get() = when
{
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ->
"${this.manufacturerName} ${this.productName} ${this.version}" +
" (${this.vendorId.toString(16)}:${this.productId.toString(16)})" +
" : ${this.serialNumber}"
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ->
"${this.manufacturerName} ${this.productName}" +
" (${this.vendorId.toString(16)}:${this.productId.toString(16)})" +
" : ${this.serialNumber}"
else ->
"ID ${this.vendorId.toString(16)}:0x${this.productId.toString(16)}"
}
val SERIAL_LIBRARIES = mapOf<String, (UsbDevice, UsbDeviceConnection) -> SerialSocket>(
Pair(
"felHr",
{ device: UsbDevice, connection: UsbDeviceConnection ->
FelHrSerialSocket(device, connection)
}),
Pair(
"usb-serial-for-android",
{ device: UsbDevice, connection: UsbDeviceConnection ->
UsbForAndroidSerialSocket(device, connection)
})
)
val PROTOCOLS = mapOf<String, (SerialSocket) -> StenoSerialProtocol>(
Pair("TxBolt", { socket: SerialSocket -> TxBoltSerialProtocol(socket) }),
Pair("GeminiPr", { socket: SerialSocket -> GeminiPrSerialProtocol(socket) })
)
val BAUDRATE_DEFAULT = 9200
enum class DataBits
{
_5,
_6,
_7,
_8;
companion object
{
val DEFAULT = _8
fun valueOf2(s: String) = DataBits.valueOf("_$s")
}
}
enum class StopBits
{
_1,
_1_5,
_2;
companion object
{
val DEFAULT = _1
fun valueOf2(s: String) = StopBits.valueOf("_${s.replace(".", "_")}")
}
}
enum class Parity
{
NONE,
ODD,
EVEN,
MARK,
SPACE;
companion object
{
val DEFAULT = NONE
fun valueOf2(s: String) = Parity.valueOf(s.toUpperCase())
}
}
enum class FlowControl
{
OFF,
RTS_CTS,
DSR_DTR,
XON_XOFF;
companion object
{
val DEFAULT = OFF
fun valueOf2(s: String) = FlowControl.valueOf(s.replace("/", "_").toUpperCase())
}
}
private val DEFAULT_SERIAL_CONFIG = Json.parse("""{
"library": "usb-serial-for-android",
"protocol": "TxBolt"
}""").asObject()
private const val ACTION_USB_PERMISSION = "nimble.dotterel.USB_PERMISSION"
private const val ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED"
class SerialStenoMachine(
private val app: Dotterel,
val id: String
) :
StenoMachine,
StenoMachine.Listener,
Dotterel.IntentListener
{
private val usbManager: UsbManager = this.app.getSystemService(Context.USB_SERVICE) as UsbManager
private var device: UsbDevice? = null
private var socket: SerialSocket? = null
private var protocol: StenoSerialProtocol? = null
class Factory :
StenoMachine.Factory,
Dotterel.IntentListener
{
override var tracker: StenoMachineTracker? = null
set(v)
{
field = v
val tracker = v ?: return
tracker.intentForwarder
.add(UsbManager.ACTION_USB_DEVICE_ATTACHED, this)
tracker.intentForwarder
.add(UsbManager.ACTION_USB_DEVICE_DETACHED, this)
(tracker.androidContext.getSystemService(Context.USB_SERVICE) as UsbManager)
.deviceList
.values
.map({ it.id })
.forEach({ tracker.addMachine(Pair("Serial", it)) })
}
override fun makeStenoMachine(app: Dotterel, id: String) = SerialStenoMachine(app, id)
override fun onIntent(context: Context, intent: Intent)
{
when(intent.action)
{
UsbManager.ACTION_USB_DEVICE_ATTACHED ->
this.tracker?.addMachine(Pair(
"Serial",
intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE).id))
UsbManager.ACTION_USB_DEVICE_DETACHED ->
this.tracker?.removeMachine(Pair(
"Serial",
intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE).id))
}
}
}
init
{
this.app.intentForwarder.add(ACTION_USB_DETACHED, this)
}
override fun setConfig(
keyLayout: KeyLayout,
config: JsonObject,
systemConfig: JsonObject)
{
try
{
@Suppress("NAME_SHADOWING")
val config = config.get(this.id)?.asObject() ?: JsonObject()
val device = this.usbManager.deviceList?.values?.find({ it.id == this.id })!!
this.device = device
if(!this.usbManager.hasPermission(device))
{
this.requestUserPermission()
return
}
else
Log.i("Dotterel", "USB device access permission granted for ${this.id}")
this.socket = SERIAL_LIBRARIES[config.get("library")?.asString() ?: DEFAULT_SERIAL_CONFIG.get("library").asString()]
?.invoke(
this.device!!,
this.usbManager.openDevice(device)!!)
?.also({ socket ->
config.get("baudRate")?.also({ v -> socket.baudRate = v.asInt() })
config.get("dataBits")?.also({ v -> socket.dataBits = DataBits.valueOf2(v.asString()) })
config.get("stopBits")?.also({ v -> socket.stopBits = StopBits.valueOf2(v.asString()) })
config.get("parity")?.also({ v -> socket.parity = Parity.valueOf2(v.asString()) })
config.get("flowControl")?.also({ v -> socket.flowControl = FlowControl.valueOf2(v.asString()) })
})
val protocolName = config.get("protocol")?.asString() ?: DEFAULT_SERIAL_CONFIG.get("protocol").asString()
val mapping = systemConfig[protocolName]!!.asObject()
.get("layout").asObject()
.mapValues({ it.value.asArray().map({ key -> key.asString() }) })
this.protocol = PROTOCOLS[protocolName]
?.invoke(this.socket!!)
?.also({ protocol ->
protocol.keyLayout = keyLayout
protocol.keyMap = KeyMap(
keyLayout,
mapping,
{
val b = protocol.keys.indexOf(it)
if(b == -1)
null
else
b
})
protocol.strokeListener = this.strokeListener
})
this.protocol?.strokeListener = this
this.socket?.protocol = this.protocol
}
catch(e: java.lang.NullPointerException)
{
throw IllegalArgumentException(e)
}
catch(e: java.lang.UnsupportedOperationException)
{
throw IllegalArgumentException(e)
}
catch(e: IOException)
{
Log.e("Dotterel", "Error opening serial socket ${this.id}: $e")
}
}
override var strokeListener: StenoMachine.Listener? = null
set(v)
{
field = v
this.protocol?.strokeListener = v
}
override fun close()
{
this.socket?.close()
}
private fun requestUserPermission()
{
Log.i("Dotterel", "Requesting USB device access permission for ${this.id}")
this.app.intentForwarder.add(ACTION_USB_PERMISSION, this)
val pendingIntent = PendingIntent.getBroadcast(
this.app,
0,
Intent(ACTION_USB_PERMISSION),
0)
this.usbManager.requestPermission(this.device, pendingIntent)
}
override fun onIntent(context: Context, intent: Intent)
{
when(intent.action)
{
ACTION_USB_PERMISSION ->
{
if(intent.extras?.getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED) == true)
this.app.configureMachine(Pair("Serial", this.id))
else
{
val m = "USB device access permission not granted for ${this.id}"
Log.e("Dotterel", m)
Toast.makeText(this.app, m, Toast.LENGTH_LONG).show()
}
}
}
}
override fun applyStroke(s: Stroke)
{
this.strokeListener?.applyStroke(s)
this.app.poke()
this.app.window.window?.makeActive()
}
override fun changeStroke(s: Stroke)
{
this.strokeListener?.changeStroke(s)
}
}
abstract class StenoSerialProtocol(override val socket: SerialSocket) : SerialProtocol
{
abstract val keys: List<String>
var strokeListener: StenoMachine.Listener? = null
var keyLayout: KeyLayout = KeyLayout("")
var keyMap: KeyMap<Int> = KeyMap(KeyLayout(""), mapOf(), { 0 })
private fun keysToStroke(keys: Long): Stroke =
(0 until this.keys.size).fold(
Stroke(this.keyLayout, 0L),
{ acc, it ->
if(keys and (1L shl it) != 0L)
acc + this.keyMap.parse(it)
else
acc
})
fun applyStroke(keys: Long) =
this.strokeListener?.applyStroke(this.keysToStroke(keys))
}
private const val MAX_KEYS = 64
interface SerialSocket : Closeable
{
var baudRate: Int
var dataBits: DataBits
var stopBits: StopBits
var parity: Parity
var flowControl: FlowControl
var protocol: SerialProtocol?
fun send(data: ByteArray)
override fun close()
}
interface SerialProtocol
{
val socket: SerialSocket
fun receive(data: ByteArray)
}