-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAttysScan.mm
105 lines (91 loc) · 3.09 KB
/
AttysScan.mm
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
#include "AttysScan.h"
#import <Foundation/Foundation.h>
#import <IOBluetooth/objc/IOBluetoothDevice.h>
#import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
#import <IOBluetooth/objc/IOBluetoothRFCOMMChannel.h>
#import <IOBluetoothUI/objc/IOBluetoothDeviceSelectorController.h>
#include <thread>
// convenience class which can be used globally
AttysScan attysScan;
/**
* Scans for Attys Devices
* Fills up the variables
* Returns zero on success
**/
int AttysScan::scan(int maxAttysDevs) {
attysComm = new AttysComm*[maxAttysDevs];
assert(attysComm != nullptr);
for (int devNo = 0; devNo < maxAttysDevs; devNo++) {
attysComm[devNo] = nullptr;
}
nAttysDevices = 0;
_RPT0(0,"Attempting to connect\n");
NSArray *deviceArray = [IOBluetoothDevice pairedDevices];
if ( ( deviceArray == nil ) || ( [deviceArray count] == 0 ) ) {
if (statusCallback) {
statusCallback->message(SCAN_NODEV, "No Attys paired");
}
return 0;
}
_RPT1(0,"We have %lu paired device(s).\n",(unsigned long)[deviceArray count]);
// let's probe how many we have
nAttysDevices = 0;
for (int i = 0; (i < [deviceArray count]) && (i < maxAttysDevs); i++) {
IOBluetoothDevice *device = [deviceArray objectAtIndex:i];
_RPT1(0,"device name = %s ",[device.name UTF8String]);
char name[256];
strcpy(name,[device.name UTF8String]);
if (strstr(name, "GN-ATTYS") != 0) {
_RPT0(0, "! Found one. ");
// allocate a socket
attysComm[nAttysDevices] = new AttysComm((__bridge void*)device);
if (attysComm[nAttysDevices] == NULL) {
break;
}
try {
if (statusCallback) {
char tmp[256];
sprintf(tmp,"connecting to %s",name);
statusCallback->message(SCAN_CONNECTING, tmp);
}
attysComm[nAttysDevices]->connect();
attysComm[nAttysDevices]->setAttysName(name);
nAttysDevices++;
_RPT0(0, "\n");
if (statusCallback) {
char tmp[256];
sprintf(tmp,"connected to %s",name);
statusCallback->message(SCAN_CONNECTED, tmp);
}
}
catch (const char *msg) {
if (statusCallback) {
statusCallback->message(SCAN_CONNECTERR, msg);
}
attysComm[nAttysDevices]->closeSocket();
delete attysComm[nAttysDevices];
attysComm[nAttysDevices] = NULL;
_RPT0(0, "Connection error.\n");
}
}
else {
_RPT0(0, "\n");
}
}
sortAttys();
// get them both to sync
for (int devNo = 0; devNo < nAttysDevices; devNo++) {
attysComm[devNo]->resetRingbuffer();
}
return 0;
}
AttysScan::~AttysScan() {
if (!attysComm) return;
for (int devNo = 0; devNo < nAttysDevices; devNo++) {
if (attysComm[devNo]) {
delete attysComm[devNo];
attysComm[devNo] = NULL;
}
}
delete[] attysComm;
}