forked from onatbas/bluetooth.koplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetoothmanagerdbus.lua
285 lines (248 loc) · 7.81 KB
/
bluetoothmanagerdbus.lua
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
local logger = require("logger")
local _ = require("gettext")
---@class BluetoothManager
---@field protected adapter_path string
---@field protected adapter_interface string
---@field protected dbus_dest string
local BluetoothManager = {
adapter_path = "/org/bluez/hci0", -- BT adapter path TODO: automate
adapter_interface = "org.bluez.Adapter1", -- interface for dbus TODO: automate with Introspect?
dbus_dest = "com.kobo.mtk.bluedroid", -- dest for dbus-send TODO: automate
}
---@protected
---@param command string
---@return string
function BluetoothManager:_run_dbus_command(command)
local handle = io.popen(command .. " 2>&1")
local result = handle:read("*a")
handle:close()
logger.dbg("bluetooth result", result)
return result
end
---@protected
---@param dest string
---@param device_path string
---@param interface string
---@param method string
---@param args string[]
---@return string
function BluetoothManager:_create_dbus_command(dest, device_path, interface, method, args)
local command = "dbus-send --system --dest="
.. dest
.. " --type=method_call --print-reply "
.. device_path
.. " "
.. interface
.. "."
.. method
for _, arg in ipairs(args or {}) do
command = command .. " " .. arg
end
logger.dbg("bluetooth command:", command)
return command
end
---@protected
---@param result string
---@return BluetoothItem[]
function BluetoothManager:_parseManagedObjects(result)
local objects = {}
local current_object = nil
local current_property = nil
for line in result:gmatch("[^\n]+") do
local object_path = line:match('^%s+object path "(.*)"')
if object_path then
current_object = { path = object_path }
current_property = nil
table.insert(objects, current_object)
end
local interface_name = line:match('^%s+string "(.*)"')
if interface_name and not current_object.interface then
current_object["interface"] = interface_name
current_property = nil
elseif interface_name then
current_property = interface_name
end
local string_type, value = line:match("^%s+variant%s+([%w%d]*)%s+(.*)$")
local function all_trim(s)
return s:match("^%s*(.*)"):match("(.-)%s*$")
end
if current_property and value then
value = all_trim(value)
if string_type == "boolean" then
value = value == "true"
elseif string_type:match("int%d%d") then
value = tonumber(value)
else
value = value:match('"(.*)"')
end
current_object[current_property:lower()] = value
current_property = nil
end
end
return objects
end
-- GetManagedObjects
---@protected
---@return any
function BluetoothManager:_getManagedObjects()
local command =
self:_create_dbus_command(self.dbus_dest, "/", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")
return self:_run_dbus_command(command)
end
---@return BluetoothItem[]
function BluetoothManager:listDevices()
local result = self:_getManagedObjects()
return self:_parseManagedObjects(result)
end
-- Discover devices
function BluetoothManager:startDiscovery()
local command =
self:_create_dbus_command(self.dbus_dest, self.adapter_path, self.adapter_interface, "StartDiscovery")
return self:_run_dbus_command(command)
end
function BluetoothManager:stopDiscovery()
local command =
self:_create_dbus_command(self.dbus_dest, self.adapter_path, self.adapter_interface, "StopDiscovery")
return self:_run_dbus_command(command)
end
---@return boolean
function BluetoothManager:isDiscoveryOn()
return self:_getProperty(self.adapter_path, self.adapter_interface, "Discovering")
end
-- Set device as trusted
---@param device BluetoothItem
---@param value boolean
---@return boolean
function BluetoothManager:setTrusted(device, value)
local device_path = device.path
return self:_setProperty(device_path, "org.bluez.Device1", "Trusted", value)
end
-- Pair with a device
---@param device BluetoothItem
---@return string
function BluetoothManager:pairDevice(device)
local device_path = device.path
local command = self:_create_dbus_command(self.dbus_dest, device_path, "org.bluez.Device1", "Pair")
return self:_run_dbus_command(command)
end
-- Connect to a device
---@param device BluetoothItem
---@return string
function BluetoothManager:connectDevice(device)
local device_path = device.path
local command = self:_create_dbus_command(self.dbus_dest, device_path, "org.bluez.Device1", "Connect")
return self:_run_dbus_command(command)
end
-- Disconnect from a device
---@param device BluetoothItem
---@return string
function BluetoothManager:disconnectDevice(device)
local device_path = device.path
local command = self:_create_dbus_command(self.dbus_dest, device_path, "org.bluez.Device1", "Disconnect")
return self:_run_dbus_command(command)
end
-- Forget a device
---@param device BluetoothItem
---@return string
function BluetoothManager:removeDevice(device)
local device_path = device.path
local args = {
"objpath:" .. device_path,
}
local command =
self:_create_dbus_command(self.dbus_dest, self.adapter_path, self.adapter_interface, "RemoveDevice", args)
return self:_run_dbus_command(command)
end
-- Power on or off the adapter
---@protected
---@param value boolean
---@return boolean
function BluetoothManager:_setPowered(value)
return self:_setProperty(self.adapter_path, self.adapter_interface, "Powered", value)
end
---@return boolean
function BluetoothManager:setBluetoothOn()
return self:_setPowered(true)
end
---@return boolean
function BluetoothManager:setBluetoothOff()
return self:_setPowered(false)
end
---@return boolean
function BluetoothManager:isBluetoothOn()
return self:_getProperty(self.adapter_path, self.adapter_interface, "Powered")
end
---@param device BluetoothItem
---@return boolean
function BluetoothManager:isDeviceTrusted(device)
return self:_getProperty(device.path, device.interface, "Trusted")
end
---@param device BluetoothItem
---@return boolean
function BluetoothManager:isDevicePaired(device)
return self:_getProperty(device.path, device.interface, "Paired")
end
---@param device BluetoothItem
---@return boolean
function BluetoothManager:isDeviceConnected(device)
return self:_getProperty(device.path, device.interface, "Connected")
end
-- Get device or adapter property (e.g., Powered, Connected, Paired, etc.)
---@protected
---@param device_path string
---@param interface string
---@param property string
---@return boolean | number | string
function BluetoothManager:_getProperty(device_path, interface, property)
local args = {
"string:'" .. interface .. "'",
"string:'" .. property .. "'",
}
local command =
self:_create_dbus_command(self.dbus_dest, device_path, "org.freedesktop.DBus.Properties", "Get", args)
local output = self:_run_dbus_command(command)
local result = output:match("variant%s+(.+)")
return self:_parseResult(result)
end
---@protected
---@param result string
---@return boolean | number | string
function BluetoothManager:_parseResult(result)
local string_type, value = result:match("([%w%d]*)%s+(.*)%s*")
local function all_trim(s)
return s:match("^%s*(.*)"):match("(.-)%s*$")
end
if value then
value = all_trim(value)
if string_type == "boolean" then
value = value == "true"
elseif string_type:match("int%d%d") then
value = tonumber(value)
else
value = value:match('"(.*)"')
end
else
value = "none"
end
return value
end
-- Set device or adapter property
---@protected
---@param device_path string
---@param interface string
---@param property string
---@param value any
---@return boolean | number | string
function BluetoothManager:_setProperty(device_path, interface, property, value)
local args = {
"string:'" .. interface .. "'",
"string:'" .. property .. "'",
"variant:boolean:" .. tostring(value),
}
local command =
self:_create_dbus_command(self.dbus_dest, device_path, "org.freedesktop.DBus.Properties", "Set", args)
local output = self:_run_dbus_command(command)
local result = self:_getProperty(device_path, interface, property)
return result
end
return BluetoothManager