amiro-os / components / bluetooth / bluetooth-serial.cpp @ 33c91ff2
History | View | Annotate | Download (2.549 KB)
1 |
#include <ch.hpp> |
---|---|
2 |
#include <hal.h> |
3 |
|
4 |
#include <amiro/bluetooth/bluetooth-serial.hpp> |
5 |
|
6 |
#include <global.hpp> |
7 |
|
8 |
using namespace chibios_rt; |
9 |
using namespace amiro; |
10 |
|
11 |
extern Global global;
|
12 |
|
13 |
/*
|
14 |
* Class constructor
|
15 |
*/
|
16 |
BluetoothSerial::BluetoothSerial(BLUETOOTH *bluetooth, uint8_t rxtx) : |
17 |
BaseStaticThread<128>(), serialConn(bluetooth, this, "SERIAL"), |
18 |
mailbox(mailboxBuffer, BLUETOOTH_SERIAL_MAILBOX_SIZE) { |
19 |
iwrap = &(bluetooth->iwrap); |
20 |
rx_tx = rxtx; |
21 |
linkId = 0xFF;
|
22 |
stopflag = 0;
|
23 |
} |
24 |
|
25 |
//----------------------------------------------------------------
|
26 |
|
27 |
msg_t BluetoothSerial::main(void) {
|
28 |
setName("BluetoothSerial");
|
29 |
|
30 |
while (!this->shouldTerminate()) { |
31 |
if (serialReceive())
|
32 |
this->requestTerminate();
|
33 |
} |
34 |
return RDY_OK;
|
35 |
} |
36 |
|
37 |
/*
|
38 |
* Member functions
|
39 |
*/
|
40 |
msg_t BluetoothSerial::serialReceive() { |
41 |
BluetoothDescriptor* recv_descriptor = NULL;
|
42 |
uint8_t *buffer; |
43 |
size_t length; |
44 |
msg_t msg; |
45 |
|
46 |
msg = mailbox.fetch((msg_t*) &recv_descriptor, TIME_INFINITE); |
47 |
if ((msg == RDY_RESET) || stopflag)
|
48 |
return RDY_RESET;
|
49 |
|
50 |
buffer = recv_descriptor->bluetoothDescriptorGetPayload(); |
51 |
length = recv_descriptor->bluetoothDescriptorGetPayloadLength(); |
52 |
|
53 |
for (size_t i = 0; i < length; i++) |
54 |
chSequentialStreamPut((BaseSequentialStream*) &global.sercanmux1, buffer[i]); |
55 |
|
56 |
msg = iwrap->transport.bluetoothTransportGetStorageMailbox()->post((msg_t) recv_descriptor, TIME_INFINITE); |
57 |
if ((msg == RDY_RESET) || stopflag)
|
58 |
return RDY_RESET;
|
59 |
|
60 |
return RDY_OK;
|
61 |
} |
62 |
|
63 |
msg_t BluetoothSerial::serialTransmit(const uint8_t* serialdata, size_t length) {
|
64 |
msg_t msg; |
65 |
|
66 |
if ((rx_tx & 0x01) && (linkId != 0xFF) && (!stopflag)) |
67 |
msg = iwrap->iwrapTransmit(linkId, serialdata, length); |
68 |
else
|
69 |
msg = RDY_RESET; |
70 |
|
71 |
return msg;
|
72 |
} |
73 |
|
74 |
void BluetoothSerial::bluetoothSerialStart(uint8_t linkid) {
|
75 |
linkId = linkid; |
76 |
iwrap->transport.bluetoothTransportSetReceiveMailbox(linkId, &this->mailbox);
|
77 |
|
78 |
if ((rx_tx & 0x02)) { // && (!stopflag)) { |
79 |
this->start(NORMALPRIO);
|
80 |
} |
81 |
stopflag = 0;
|
82 |
} |
83 |
|
84 |
void BluetoothSerial::bluetoothSerialStop() {
|
85 |
linkId = 0xFF;
|
86 |
stopflag = 1;
|
87 |
mailbox.post(RDY_RESET, TIME_INFINITE); // TIME_IMMEDIATE TIME_INFINITE
|
88 |
} |
89 |
|
90 |
bool BluetoothSerial::bluetoothSerialIsConnected() {
|
91 |
if (linkId == 0xFF) |
92 |
return false; |
93 |
|
94 |
return true; |
95 |
} |
96 |
|
97 |
void BluetoothSerial::bluetoothSerialListen(const char *addr) { |
98 |
serialConn.bluetoothConnectorListen(addr); |
99 |
} |
100 |
|
101 |
void BluetoothSerial::bluetoothSerialConnect(const char *addr){ |
102 |
serialConn.bluetoothConnectorConnect(addr); |
103 |
} |
104 |
|
105 |
void BluetoothSerial::bluetoothSerialDisconnect(const char *addr) { |
106 |
serialConn.bluetoothConnectorDisconnect(addr); |
107 |
} |