Cosmetics
[opentx.git] / radio / src / bluetooth.h
blob2f4f2e1c6849f9c37acd611523f6cbe39317a215
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 enum BluetoothStates {
22 #if defined(PCBX9E)
23 BLUETOOTH_INIT,
24 BLUETOOTH_WAIT_TTM,
25 BLUETOOTH_WAIT_BAUDRATE_CHANGE,
26 #endif
27 BLUETOOTH_STATE_OFF,
28 BLUETOOTH_STATE_FACTORY_BAUDRATE_INIT,
29 BLUETOOTH_STATE_BAUDRATE_SENT,
30 BLUETOOTH_STATE_BAUDRATE_INIT,
31 BLUETOOTH_STATE_NAME_SENT,
32 BLUETOOTH_STATE_POWER_SENT,
33 BLUETOOTH_STATE_ROLE_SENT,
34 BLUETOOTH_STATE_IDLE,
35 BLUETOOTH_STATE_DISCOVER_REQUESTED,
36 BLUETOOTH_STATE_DISCOVER_SENT,
37 BLUETOOTH_STATE_DISCOVER_START,
38 BLUETOOTH_STATE_DISCOVER_END,
39 BLUETOOTH_STATE_BIND_REQUESTED,
40 BLUETOOTH_STATE_CONNECT_SENT,
41 BLUETOOTH_STATE_CONNECTED,
42 BLUETOOTH_STATE_DISCONNECTED,
43 BLUETOOTH_STATE_CLEAR_REQUESTED,
44 BLUETOOTH_STATE_FLASH_FIRMWARE
47 #define LEN_BLUETOOTH_ADDR 16
48 #define MAX_BLUETOOTH_DISTANT_ADDR 6
49 #define BLUETOOTH_PACKET_SIZE 14
50 #define BLUETOOTH_LINE_LENGTH 32
52 class Bluetooth
54 public:
55 void writeString(const char * str);
56 char * readline(bool error_reset = true);
57 void write(const uint8_t * data, uint8_t length);
59 void forwardTelemetry(const uint8_t * packet);
60 void wakeup();
61 const char * flashFirmware(const char * filename);
63 volatile uint8_t state;
64 char localAddr[LEN_BLUETOOTH_ADDR+1];
65 char distantAddr[LEN_BLUETOOTH_ADDR+1];
67 protected:
68 void pushByte(uint8_t byte);
69 uint8_t read(uint8_t * data, uint8_t size, uint32_t timeout=1000/*ms*/);
70 void appendTrainerByte(uint8_t data);
71 void processTrainerFrame(const uint8_t * buffer);
72 void processTrainerByte(uint8_t data);
73 void sendTrainer();
74 void receiveTrainer();
76 uint8_t bootloaderChecksum(uint8_t command, const uint8_t * data, uint8_t size);
77 void bootloaderSendCommand(uint8_t command, const void *data = nullptr, uint8_t size = 0);
78 void bootloaderSendCommandResponse(uint8_t response);
79 const char * bootloaderWaitCommandResponse(uint32_t timeout=1000/*ms*/);
80 const char * bootloaderWaitResponseData(uint8_t *data, uint8_t size);
81 const char * bootloaderSetAutoBaud();
82 const char * bootloaderReadStatus(uint8_t &status);
83 const char * bootloaderCheckStatus();
84 const char * bootloaderSendData(const uint8_t * data, uint8_t size);
85 const char * bootloaderEraseFlash(uint32_t start, uint32_t size);
86 const char * bootloaderStartWriteFlash(uint32_t start, uint32_t size);
87 const char * bootloaderWriteFlash(const uint8_t * data, uint32_t size);
88 const char * doFlashFirmware(const char * filename);
90 uint8_t buffer[BLUETOOTH_LINE_LENGTH+1];
91 uint8_t bufferIndex = 0;
92 tmr10ms_t wakeupTime = 0;
93 uint8_t crc;
96 extern Bluetooth bluetooth;