Statistics
| Branch: | Tag: | Revision:

amiro-os / components / bluetooth / bluetooth-serial.cpp @ 10687985

History | View | Annotate | Download (2.49 KB)

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