amiro-os / components / bluetooth / bluetooth-connector.cpp @ 181f2892
History | View | Annotate | Download (2.79 KB)
1 |
#include <ch.hpp> |
---|---|
2 |
#include <hal.h> |
3 |
|
4 |
#include <string.h> |
5 |
#include <chprintf.h> |
6 |
|
7 |
#include <amiro/bluetooth/bluetooth-connector.hpp> |
8 |
#include <amiro/bluetooth/bluetooth-serial.hpp> |
9 |
#include <amiro/bluetooth/bluetooth-wiimote.hpp> |
10 |
|
11 |
using namespace chibios_rt; |
12 |
using namespace amiro; |
13 |
|
14 |
/*
|
15 |
* Class constructor
|
16 |
*/
|
17 |
BluetoothConnector::BluetoothConnector(BLUETOOTH *bluetooth, void *bluetoothDev, const char *typeConn) { |
18 |
iwrap = &(bluetooth->iwrap); |
19 |
objDev = bluetoothDev; |
20 |
typeDev = typeConn; |
21 |
} |
22 |
|
23 |
//----------------------------------------------------------------
|
24 |
|
25 |
|
26 |
void BluetoothConnector::bluetoothConnectorListen(const char *addr) { |
27 |
BluetoothProfile profile; |
28 |
profile.addr = (strstr(addr, "ALL")) ? "free" : addr; |
29 |
profile.connector = this;
|
30 |
profile.connect = &BluetoothConnector::bluetoothConnectorActivate; |
31 |
profile.disconnect = &BluetoothConnector::bluetoothConnectorDeactivate; |
32 |
profile.linkid = 0xFF;
|
33 |
|
34 |
/* Listen all address can have only one profile at the last profile,
|
35 |
otherwise a fixed address will take it at first */
|
36 |
if (strstr(addr, "ALL") && (iwrap->profiles[7].addr == NULL)) { |
37 |
iwrap->profiles[7] = profile;
|
38 |
return;
|
39 |
} |
40 |
|
41 |
for(int i = 0; i < 8; i++) { |
42 |
if(iwrap->profiles[i].addr == NULL) { |
43 |
iwrap->profiles[i] = profile; |
44 |
break;
|
45 |
} |
46 |
} |
47 |
} |
48 |
|
49 |
void BluetoothConnector::bluetoothConnectorConnect(const char *addr) { |
50 |
int size = strlen(addr) + 17; |
51 |
char cmd[size];
|
52 |
strcpy(cmd, "CALL ");
|
53 |
strcat(cmd, addr); |
54 |
|
55 |
for (int i = 0; i < 8; i++) { |
56 |
if (strstr(iwrap->profiles[i].addr, addr) && (iwrap->profiles[i].linkid == 0xFF)) { |
57 |
switch (typeDev[0]) { |
58 |
case 'S': // 'S' for "SERIAL" |
59 |
strcat(cmd, " 1101 RFCOMM");
|
60 |
break;
|
61 |
case 'W': // 'W' for "WIIMOTE" |
62 |
strcat(cmd, " 0013 L2CAP ");
|
63 |
break;
|
64 |
} |
65 |
iwrap->bluetoothIwrapSendCommand(cmd); |
66 |
BaseThread::sleep(MS2ST(1000)); // waiting connection establish |
67 |
break;
|
68 |
} |
69 |
} |
70 |
} |
71 |
|
72 |
void BluetoothConnector::bluetoothConnectorDisconnect(const char *addr) { |
73 |
char cmd[8]; |
74 |
strcpy(cmd, "CLOSE ");
|
75 |
|
76 |
for (int i = 0; i < 8; i++) { |
77 |
if (strstr(iwrap->profiles[i].addr, addr) && (iwrap->profiles[i].linkid != 0xFF)) { |
78 |
cmd[6] = iwrap->profiles[i].linkid + 0x30; |
79 |
cmd[7]= '\0'; |
80 |
iwrap->bluetoothIwrapSendCommand(cmd); |
81 |
BaseThread::sleep(MS2ST(2000)); // waiting disconnection |
82 |
break;
|
83 |
} |
84 |
} |
85 |
} |
86 |
|
87 |
void BluetoothConnector::bluetoothConnectorActivate(uint8_t linkid) {
|
88 |
if (strstr(typeDev, "SERIAL")) |
89 |
((BluetoothSerial *) objDev)->bluetoothSerialStart(linkid); |
90 |
else
|
91 |
((BluetoothWiimote *) objDev)->bluetoothWiimoteStart(linkid); |
92 |
} |
93 |
|
94 |
void BluetoothConnector::bluetoothConnectorDeactivate() {
|
95 |
if (strstr(typeDev, "SERIAL")) |
96 |
((BluetoothSerial *) objDev)->bluetoothSerialStop(); |
97 |
else
|
98 |
((BluetoothWiimote *) objDev)->bluetoothWiimoteStop(); |
99 |
} |