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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
// Copyright (c) 2017 Akos Kiss.
//
// Licensed under the BSD 3-Clause License
// <LICENSE.md or https://opensource.org/licenses/BSD-3-Clause>.
// This file may not be copied, modified, or distributed except
// according to those terms.
use std::os::raw::{c_char, c_int, c_uint};
use objc::runtime::{Class, Object, BOOL};
#[allow(non_upper_case_globals)]
pub const nil: *mut Object = 0 as *mut Object;
pub mod ns {
use super::*;
// NSObject
pub fn object_copy(nsobject: *mut Object) -> *mut Object {
unsafe {
let copy: *mut Object = msg_send![nsobject, copy];
copy
}
}
// NSNumber
pub fn number_withbool(value: BOOL) -> *mut Object {
unsafe {
let nsnumber: *mut Object =
msg_send![Class::get("NSNumber").unwrap(), numberWithBool: value];
nsnumber
}
}
pub fn number_withunsignedlonglong(value: u64) -> *mut Object {
unsafe {
let nsnumber: *mut Object = msg_send![
Class::get("NSNumber").unwrap(),
numberWithUnsignedLongLong: value
];
nsnumber
}
}
pub fn number_unsignedlonglongvalue(nsnumber: *mut Object) -> u64 {
unsafe {
let value: u64 = msg_send![nsnumber, unsignedLongLongValue];
value
}
}
// NSString
pub fn string(cstring: *const c_char) -> *mut Object /* NSString* */ {
unsafe {
let nsstring: *mut Object = msg_send![
Class::get("NSString").unwrap(),
stringWithUTF8String: cstring
];
nsstring
}
}
pub fn string_utf8string(nsstring: *mut Object) -> *const c_char {
unsafe {
let utf8string: *const c_char = msg_send![nsstring, UTF8String];
utf8string
}
}
// NSArray
pub fn array_count(nsarray: *mut Object) -> c_uint {
unsafe {
let count: c_uint = msg_send![nsarray, count];
count
}
}
pub fn array_objectatindex(nsarray: *mut Object, index: c_uint) -> *mut Object {
unsafe {
let object: *mut Object = msg_send![nsarray, objectAtIndex: index];
object
}
}
// NSDictionary
pub fn dictionary_allkeys(nsdict: *mut Object) -> *mut Object /* NSArray* */ {
unsafe {
let keys: *mut Object = msg_send![nsdict, allKeys];
keys
}
}
pub fn dictionary_objectforkey(nsdict: *mut Object, key: *mut Object) -> *mut Object {
unsafe {
let object: *mut Object = msg_send![nsdict, objectForKey: key];
object
}
}
// NSMutableDictionary : NSDictionary
pub fn mutabledictionary() -> *mut Object {
unsafe {
let nsmutdict: *mut Object =
msg_send![Class::get("NSMutableDictionary").unwrap(), dictionaryWithCapacity:0];
nsmutdict
}
}
pub fn mutabledictionary_removeobjectforkey(nsmutdict: *mut Object, key: *mut Object) {
unsafe {
let () = msg_send![nsmutdict, removeObjectForKey: key];
}
}
pub fn mutabledictionary_setobject_forkey(
nsmutdict: *mut Object,
object: *mut Object,
key: *mut Object,
) {
unsafe {
let () = msg_send![nsmutdict, setObject:object forKey:key];
}
}
// NSData
pub fn data(bytes: *const u8, length: c_uint) -> *mut Object /* NSData* */ {
unsafe {
let data: *mut Object =
msg_send![Class::get("NSData").unwrap(), dataWithBytes:bytes length:length];
data
}
}
pub fn data_length(nsdata: *mut Object) -> c_uint {
unsafe {
let length: c_uint = msg_send![nsdata, length];
length
}
}
pub fn data_bytes(nsdata: *mut Object) -> *const u8 {
unsafe {
let bytes: *const u8 = msg_send![nsdata, bytes];
bytes
}
}
// NSUUID
pub fn uuid_uuidstring(nsuuid: *mut Object) -> *mut Object /* NSString* */ {
unsafe {
let uuidstring: *mut Object = msg_send![nsuuid, UUIDString];
uuidstring
}
}
}
pub mod io {
use super::*;
#[link(name = "IOBluetooth", kind = "framework")]
extern "C" {
pub fn IOBluetoothPreferenceGetControllerPowerState() -> c_int;
pub fn IOBluetoothPreferenceSetControllerPowerState(state: c_int);
pub fn IOBluetoothPreferenceGetDiscoverableState() -> c_int;
pub fn IOBluetoothPreferenceSetDiscoverableState(state: c_int);
}
// IOBluetoothHostController
pub fn bluetoothhostcontroller_defaultcontroller() -> *mut Object /* IOBluetoothHostController* */
{
unsafe {
let defaultcontroller: *mut Object = msg_send![
Class::get("IOBluetoothHostController").unwrap(),
defaultController
];
defaultcontroller
}
}
pub fn bluetoothhostcontroller_nameasstring(iobthc: *mut Object) -> *mut Object /* NSString* */
{
unsafe {
let name: *mut Object = msg_send![iobthc, nameAsString];
name
}
}
pub fn bluetoothhostcontroller_addressasstring(iobthc: *mut Object) -> *mut Object /* NSString* */
{
unsafe {
let address: *mut Object = msg_send![iobthc, addressAsString];
address
}
}
pub fn bluetoothhostcontroller_classofdevice(iobthc: *mut Object) -> u32 {
unsafe {
let classofdevice: u32 = msg_send![iobthc, classOfDevice];
classofdevice
}
}
// IOBluetoothPreference...
pub fn bluetoothpreferencegetcontrollerpowerstate() -> c_int {
unsafe { IOBluetoothPreferenceGetControllerPowerState() }
}
pub fn bluetoothpreferencesetcontrollerpowerstate(state: c_int) {
unsafe {
IOBluetoothPreferenceSetControllerPowerState(state);
}
}
pub fn bluetoothpreferencegetdiscoverablestate() -> c_int {
unsafe { IOBluetoothPreferenceGetDiscoverableState() }
}
pub fn bluetoothpreferencesetdiscoverablestate(state: c_int) {
unsafe {
IOBluetoothPreferenceSetDiscoverableState(state);
}
}
}
pub mod cb {
use super::*;
mod link {
use super::*;
#[link(name = "CoreBluetooth", kind = "framework")]
extern "C" {
pub static CBAdvertisementDataServiceUUIDsKey: *mut Object;
pub static CBCentralManagerScanOptionAllowDuplicatesKey: *mut Object;
}
}
// CBCentralManager
pub fn centralmanager(delegate: *mut Object, /*CBCentralManagerDelegate* */) -> *mut Object /*CBCentralManager* */
{
unsafe {
let cbcentralmanager: *mut Object =
msg_send![Class::get("CBCentralManager").unwrap(), alloc];
let () = msg_send![cbcentralmanager, initWithDelegate:delegate queue:nil];
cbcentralmanager
}
}
pub fn centralmanager_scanforperipherals_options(
cbcentralmanager: *mut Object,
options: *mut Object, /* NSDictionary<NSString*,id> */
) {
unsafe {
let () =
msg_send![cbcentralmanager, scanForPeripheralsWithServices:nil options:options];
}
}
pub fn centralmanager_stopscan(cbcentralmanager: *mut Object) {
unsafe {
let () = msg_send![cbcentralmanager, stopScan];
}
}
pub fn centralmanager_connectperipheral(
cbcentralmanager: *mut Object,
peripheral: *mut Object, /* CBPeripheral* */
) {
unsafe {
let () = msg_send![cbcentralmanager, connectPeripheral:peripheral options:nil];
}
}
pub fn centralmanager_cancelperipheralconnection(
cbcentralmanager: *mut Object,
peripheral: *mut Object, /* CBPeripheral* */
) {
unsafe {
let () = msg_send![cbcentralmanager, cancelPeripheralConnection: peripheral];
}
}
// CBPeer
pub fn peer_identifier(cbpeer: *mut Object) -> *mut Object /* NSUUID* */ {
unsafe {
let identifier: *mut Object = msg_send![cbpeer, identifier];
identifier
}
}
// CBPeripheral : CBPeer
pub fn peripheral_name(cbperipheral: *mut Object) -> *mut Object /* NSString* */ {
unsafe {
let name: *mut Object = msg_send![cbperipheral, name];
name
}
}
pub fn peripheral_state(cbperipheral: *mut Object) -> c_int {
unsafe {
let state: c_int = msg_send![cbperipheral, state];
state
}
}
pub fn peripheral_setdelegate(
cbperipheral: *mut Object,
delegate: *mut Object, /* CBPeripheralDelegate* */
) {
unsafe {
let () = msg_send![cbperipheral, setDelegate: delegate];
}
}
pub fn peripheral_discoverservices(cbperipheral: *mut Object) {
unsafe {
let () = msg_send![cbperipheral, discoverServices: nil];
}
}
pub fn peripheral_discoverincludedservicesforservice(
cbperipheral: *mut Object,
service: *mut Object, /* CBService* */
) {
unsafe {
let () = msg_send![cbperipheral, discoverIncludedServices:nil forService:service];
}
}
pub fn peripheral_services(cbperipheral: *mut Object) -> *mut Object /* NSArray<CBService*>* */
{
unsafe {
let services: *mut Object = msg_send![cbperipheral, services];
services
}
}
pub fn peripheral_discovercharacteristicsforservice(
cbperipheral: *mut Object,
service: *mut Object, /* CBService* */
) {
unsafe {
let () = msg_send![cbperipheral, discoverCharacteristics:nil forService:service];
}
}
pub fn peripheral_readvalueforcharacteristic(
cbperipheral: *mut Object,
characteristic: *mut Object, /* CBCharacteristic* */
) {
unsafe {
let () = msg_send![cbperipheral, readValueForCharacteristic: characteristic];
}
}
pub fn peripheral_writevalue_forcharacteristic(
cbperipheral: *mut Object,
value: *mut Object, /* NSData* */
characteristic: *mut Object, /* CBCharacteristic* */
) {
unsafe {
let () =
msg_send![cbperipheral, writeValue:value forCharacteristic:characteristic type:0];
// CBCharacteristicWriteWithResponse from CBPeripheral.h
}
}
pub fn peripheral_setnotifyvalue_forcharacteristic(
cbperipheral: *mut Object,
value: BOOL,
characteristic: *mut Object, /* CBCharacteristic* */
) {
unsafe {
let () = msg_send![cbperipheral, setNotifyValue:value forCharacteristic:characteristic];
}
}
pub fn peripheral_discoverdescriptorsforcharacteristic(
cbperipheral: *mut Object,
characteristic: *mut Object, /* CBCharacteristic* */
) {
unsafe {
let () = msg_send![
cbperipheral,
discoverDescriptorsForCharacteristic: characteristic
];
}
}
// CBPeripheralState = NSInteger from CBPeripheral.h
pub const PERIPHERALSTATE_CONNECTED: c_int = 2; // CBPeripheralStateConnected
// CBAttribute
pub fn attribute_uuid(cbattribute: *mut Object) -> *mut Object /* CBUUID* */ {
unsafe {
let uuid: *mut Object = msg_send![cbattribute, UUID];
uuid
}
}
// CBService : CBAttribute
// pub fn service_isprimary(cbservice: *mut Object) -> BOOL {
// unsafe {
// let isprimary: BOOL = msg_send![cbservice, isPrimary];
// isprimary
// }
// }
pub fn service_includedservices(cbservice: *mut Object) -> *mut Object /* NSArray<CBService*>* */
{
unsafe {
let includedservices: *mut Object = msg_send![cbservice, includedServices];
includedservices
}
}
pub fn service_characteristics(cbservice: *mut Object) -> *mut Object /* NSArray<CBCharacteristic*>* */
{
unsafe {
let characteristics: *mut Object = msg_send![cbservice, characteristics];
characteristics
}
}
// CBCharacteristic : CBAttribute
pub fn characteristic_isnotifying(cbcharacteristic: *mut Object) -> BOOL {
unsafe {
let isnotifying: BOOL = msg_send![cbcharacteristic, isNotifying];
isnotifying
}
}
pub fn characteristic_value(cbcharacteristic: *mut Object) -> *mut Object /* NSData* */ {
unsafe {
let value: *mut Object = msg_send![cbcharacteristic, value];
value
}
}
pub fn characteristic_properties(cbcharacteristic: *mut Object) -> c_uint {
unsafe {
let properties: c_uint = msg_send![cbcharacteristic, properties];
properties
}
}
// CBCharacteristicProperties = NSUInteger from CBCharacteristic.h
pub const CHARACTERISTICPROPERTY_BROADCAST: c_uint = 0x01; // CBCharacteristicPropertyBroadcast
pub const CHARACTERISTICPROPERTY_READ: c_uint = 0x02; // CBCharacteristicPropertyRead
pub const CHARACTERISTICPROPERTY_WRITEWITHOUTRESPONSE: c_uint = 0x04; // CBCharacteristicPropertyWriteWithoutResponse
pub const CHARACTERISTICPROPERTY_WRITE: c_uint = 0x08; // CBCharacteristicPropertyWrite
pub const CHARACTERISTICPROPERTY_NOTIFY: c_uint = 0x10; // CBCharacteristicPropertyNotify
pub const CHARACTERISTICPROPERTY_INDICATE: c_uint = 0x20; // CBCharacteristicPropertyIndicate
pub const CHARACTERISTICPROPERTY_AUTHENTICATEDSIGNEDWRITES: c_uint = 0x40; // CBCharacteristicPropertyAuthenticatedSignedWrites
// CBUUID
pub fn uuid_uuidstring(cbuuid: *mut Object) -> *mut Object /* NSString* */ {
unsafe {
let uuidstring: *mut Object = msg_send![cbuuid, UUIDString];
uuidstring
}
}
// CBCentralManagerScanOption...Key
// CBAdvertisementData...Key
pub use self::link::{
CBAdvertisementDataServiceUUIDsKey as ADVERTISEMENTDATASERVICEUUIDSKEY,
CBCentralManagerScanOptionAllowDuplicatesKey as CENTRALMANAGERSCANOPTIONALLOWDUPLICATESKEY,
};
}
|