forked from beegee-tokyo/RAK4631-LoRa-BLE-Config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble.cpp
153 lines (122 loc) · 4.26 KB
/
ble.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
@file ble.cpp
@author Bernd Giesecke (bernd.giesecke@rakwireless.com)
@brief BLE initialization & device configuration
@version 0.1
@date 2021-01-10
@copyright Copyright (c) 2021
*/
#include "main.h"
/** OTA DFU service */
BLEDfu ble_dfu;
/** BLE UART service */
BLEUart ble_uart;
/** Device information service */
BLEDis ble_dis;
extern BLEService lorawan_service;
/** Flag if BLE UART is connected */
bool ble_uart_is_connected = false;
// Connect callback
void connect_callback(uint16_t conn_handle);
// Disconnect callback
void disconnect_callback(uint16_t conn_handle, uint8_t reason);
// Uart RX callback
void bleuart_rx_callback(uint16_t conn_handle);
/**
@brief Initialize BLE and start advertising
*/
void init_ble(void)
{
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.configPrphConn(91, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
// Start BLE
Bluefruit.begin(1, 0);
// Set max power. Accepted values are: (min) -40, -20, -16, -12, -8, -4, 0, 2, 3, 4, 5, 6, 7, 8 (max)
Bluefruit.setTxPower(8);
// Create device name
char helper_string[256] = {0};
uint32_t addr_high = ((*((uint32_t *)(0x100000a8))) & 0x0000ffff) | 0x0000c000;
uint32_t addr_low = *((uint32_t *)(0x100000a4));
#ifdef _VARIANT_ISP4520_
/** DIO1 GPIO pin for ISP4520 */
sprintf(helper_string, "ISP-%02X%02X%02X%02X%02X%02X",
(uint8_t)(addr_high), (uint8_t)(addr_high >> 8), (uint8_t)(addr_low),
(uint8_t)(addr_low >> 8), (uint8_t)(addr_low >> 16), (uint8_t)(addr_low >> 24));
#else
/** DIO1 GPIO pin for RAK4631 */
sprintf(helper_string, "RAK-%02X%02X%02X%02X%02X%02X",
(uint8_t)(addr_high), (uint8_t)(addr_high >> 8), (uint8_t)(addr_low),
(uint8_t)(addr_low >> 8), (uint8_t)(addr_low >> 16), (uint8_t)(addr_low >> 24));
#endif
Bluefruit.setName(helper_string);
// Set connection/disconnect callbacks
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
// Configure and Start Device Information Service
ble_dis.setManufacturer("RAKwireless");
ble_dis.setModel("RAK4631");
sprintf(helper_string, "%f", SW_VERSION);
ble_dis.setSoftwareRev(helper_string);
ble_dis.setHardwareRev("52840");
ble_dis.begin();
// Start the DFU service
ble_dfu.begin();
// Start the UART service
ble_uart.begin();
ble_uart.setRxCallback(bleuart_rx_callback);
// Initialize the LoRaWAN setting service
init_settings_characteristic();
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); //
Bluefruit.Advertising.addService(lorawan_service);
Bluefruit.Advertising.addName();
Bluefruit.Advertising.addTxPower();
/* Start Advertising
- Enable auto advertising if disconnected
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
- Timeout for fast mode is 30 seconds
- Start(timeout) with timeout = 0 will advertise forever (until connected)
For recommended advertising interval
https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(15); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising
}
/**
@brief Callback when client connects
@param conn_handle: Connection handle id
*/
void connect_callback(uint16_t conn_handle)
{
(void)conn_handle;
ble_uart_is_connected = true;
}
/**
@brief Callback invoked when a connection is dropped
@param conn_handle: connection handle id
@param reason: disconnect reason
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void)conn_handle;
(void)reason;
ble_uart_is_connected = false;
}
/**
Callback if data has been sent from the connected client
@param conn_handle
The connection handle
*/
void bleuart_rx_callback(uint16_t conn_handle)
{
(void)conn_handle;
String uart_rx_buff = "";
uart_rx_buff = ble_uart.readStringUntil('\n');
uart_rx_buff.toUpperCase();
MYLOG("BLE", "BLE Received %s", uart_rx_buff.c_str());
}