forked from atc1441/ATC_MiThermometer
-
Notifications
You must be signed in to change notification settings - Fork 216
/
ble.h
254 lines (224 loc) · 10.1 KB
/
ble.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "app.h"
#include "stack/ble/ble.h"
#define BTHOME_UUID16 0xFCD2 // 16-bit UUID Service 0xFCD2 BTHOME
extern uint8_t mac_public[6], mac_random_static[6];
extern uint8_t ble_name[MAX_DEV_NAME_LEN + 2];
enum {
OTA_NONE = 0,
OTA_WORK,
OTA_WAIT,
OTA_EXTENDED
} OTA_STAGES;
extern uint8_t ota_is_working; // OTA_STAGES
enum {
CONNECTED_FLG_ENABLE = 0,
CONNECTED_FLG_PAR_UPDATE = 1,
CONNECTED_FLG_BONDING = 2,
CONNECTED_FLG_RESET_OF_DISCONNECT = 7
} CONNECTED_FLG_BITS;
extern uint8_t ble_connected; // BIT(CONNECTED_FLG_BITS): bit 0 - connected, bit 1 - conn_param_update, bit 2 - paring success, bit 7 - reset device on disconnect
//extern uint32_t adv_send_count;
#if (DEV_SERVICES & SERVICE_LE_LR)
#define ADV_BUFFER_SIZE (62-3)
extern u8 ext_adv_init; // flag ext_adv init
#else
#define ADV_BUFFER_SIZE (31-3)
#endif
typedef struct _adv_buf_t {
uint32_t send_count; // count & id advertise, = beacon_nonce.cnt32
uint8_t meas_count;
uint8_t call_count; // count 1..update_count
uint8_t update_count; // refresh adv_buf.data in next set_adv_data()
#if (DEV_SERVICES & SERVICE_LE_LR) // support extension advertise
uint8_t ext_adv_init; // flag ext_adv init
#endif
uint8_t data_size; // Advertise data size
uint8_t flag[3]; // Advertise type flags
uint8_t data[ADV_BUFFER_SIZE];
}adv_buf_t;
extern adv_buf_t adv_buf;
extern u16 batteryValueInCCC;
extern u16 tempValueInCCC;
extern u16 temp2ValueInCCC;
extern u16 humiValueInCCC;
extern u16 anaValueInCCC;
extern u16 RxTxValueInCCC;
#define SEND_BUFFER_SIZE (ATT_MTU_SIZE-3) // = 20
extern uint8_t send_buf[SEND_BUFFER_SIZE];
extern u8 my_RxTx_Data[sizeof(cfg) + 2];
#if (DEVICE_TYPE == DEVICE_LYWSD03MMC) || (DEVICE_TYPE == DEVICE_MJWSD05MMC) || (DEVICE_TYPE == DEVICE_MHO_C401) || (DEVICE_TYPE == DEVICE_MJWSD05MMC_EN)
extern u8 my_HardStr[4];
extern u8 my_SerialStr[20];
#endif
#define DEF_CON_INERVAL 16 // in 1.25 ms -> 16*1.25 = 20 ms
#define DEF_CON_LAT_INERVAL 1000 // in 1 ms -> 1 sec
#define DEF_CONNECT_LATENCY (((int)(DEF_CON_LAT_INERVAL*100)/(int)(DEF_CON_INERVAL * 125))-1) // = 49, (49+1)*1.25*16 = 1000 ms)
#define CONNECTABLE_ADV_INERVAL 1600 //x0.625 ms = 1 sec
// advDelay - a pseudo-random value in the range from 0 to 10 ms is added to a fixed advInterval so that advertising events change over time.
#define ADV_DELAY 10 //x0.625 ms = 6.25 ms
typedef struct
{
/** Minimum value for the connection event (interval. 0x0006 - 0x0C80 * 1.25 ms) */
u16 intervalMin;
/** Maximum value for the connection event (interval. 0x0006 - 0x0C80 * 1.25 ms) */
u16 intervalMax;
/** Number of LL latency connection events (0x0000 - 0x03e8) */
u16 latency;
/** Connection Timeout (0x000A - 0x0C80 * 10 ms) */
u16 timeout;
} gap_periConnectParams_t;
extern gap_periConnectParams_t my_periConnParameters;
///////////////////////////////////// ATT HANDLER define ///////////////////////////////////////
typedef enum
{
ATT_H_START = 0,
//// Gap ////
/**********************************************************************************************/
GenericAccess_PS_H, //UUID: 2800, VALUE: uuid 1800
GenericAccess_DeviceName_CD_H, //UUID: 2803, VALUE: Prop: Read | Notify
GenericAccess_DeviceName_DP_H, //UUID: 2A00, VALUE: device name
GenericAccess_Appearance_CD_H, //UUID: 2803, VALUE: Prop: Read
GenericAccess_Appearance_DP_H, //UUID: 2A01, VALUE: appearance
CONN_PARAM_CD_H, //UUID: 2803, VALUE: Prop: Read
CONN_PARAM_DP_H, //UUID: 2A04, VALUE: connParameter
//// Gatt ////
/**********************************************************************************************/
GenericAttribute_PS_H, //UUID: 2800, VALUE: uuid 1801
GenericAttribute_ServiceChanged_CD_H, //UUID: 2803, VALUE: Prop: Indicate
GenericAttribute_ServiceChanged_DP_H, //UUID: 2A05, VALUE: service change
GenericAttribute_ServiceChanged_CCB_H, //UUID: 2902, VALUE: serviceChangeCCC
#if USE_DEVICE_INFO_CHR_UUID
//// device information ////
/**********************************************************************************************/
DeviceInformation_PS_H, //UUID: 2800, VALUE: uuid 180A
DeviceInformation_ModName_CD_H, //UUID: 2803, VALUE: Prop: Read
DeviceInformation_ModName_DP_H, //UUID: 2A24, VALUE: Model Number String
DeviceInformation_SerialN_CD_H, //UUID: 2803, VALUE: Prop: Read
DeviceInformation_SerialN_DP_H, //UUID: 2A25, VALUE: Serial Number String
DeviceInformation_FirmRev_CD_H, //UUID: 2803, VALUE: Prop: Read
DeviceInformation_FirmRev_DP_H, //UUID: 2A26, VALUE: Firmware Revision String
DeviceInformation_HardRev_CD_H, //UUID: 2803, VALUE: Prop: Read
DeviceInformation_HardRev_DP_H, //UUID: 2A27, VALUE: Hardware Revision String
DeviceInformation_SoftRev_CD_H, //UUID: 2803, VALUE: Prop: Read
DeviceInformation_SoftRev_DP_H, //UUID: 2A28, VALUE: Software Revision String
DeviceInformation_ManName_CD_H, //UUID: 2803, VALUE: Prop: Read
DeviceInformation_ManName_DP_H, //UUID: 2A29, VALUE: Manufacturer Name String
#endif
//// Battery service ////
/**********************************************************************************************/
BATT_PS_H, //UUID: 2800, VALUE: uuid 180f
BATT_LEVEL_INPUT_CD_H, //UUID: 2803, VALUE: Prop: Read | Notify
BATT_LEVEL_INPUT_DP_H, //UUID: 2A19 VALUE: batVal
BATT_LEVEL_INPUT_CCB_H, //UUID: 2902, VALUE: batValCCC
#if (DEV_SERVICES & (SERVICE_THS | SERVICE_PLM))
//// Temp/Humi service ////
/**********************************************************************************************/
TEMP_PS_H, //UUID: 2800, VALUE: uuid 181A
TEMP_LEVEL_INPUT_CD_H, //UUID: 2803, VALUE: Prop: Read | Notify
TEMP_LEVEL_INPUT_DP_H, //UUID: 2A1F VALUE: last_temp
TEMP_LEVEL_INPUT_CCB_H, //UUID: 2902, VALUE: tempValCCC
TEMP2_LEVEL_INPUT_CD_H, //UUID: 2803, VALUE: Prop: Read | Notify
TEMP2_LEVEL_INPUT_DP_H, //UUID: 2A6E VALUE: measured_data.temp
TEMP2_LEVEL_INPUT_CCB_H, //UUID: 2902, VALUE: temp2ValCCC
HUMI_LEVEL_INPUT_CD_H, //UUID: 2803, VALUE: Prop: Read | Notify
HUMI_LEVEL_INPUT_DP_H, //UUID: 2A6F VALUE: measured_data.humi
HUMI_LEVEL_INPUT_CCB_H, //UUID: 2902, VALUE: humiValCCC
#elif (DEV_SERVICES & SERVICE_18B20)
//// Temp service ////
/**********************************************************************************************/
TEMP_PS_H, //UUID: 2800, VALUE: uuid 181A
TEMP_LEVEL_INPUT_CD_H, //UUID: 2803, VALUE: Prop: Read | Notify
TEMP_LEVEL_INPUT_DP_H, //UUID: 2A1F VALUE: last_temp
TEMP_LEVEL_INPUT_CCB_H, //UUID: 2902, VALUE: tempValCCC
TEMP2_LEVEL_INPUT_CD_H, //UUID: 2803, VALUE: Prop: Read | Notify
TEMP2_LEVEL_INPUT_DP_H, //UUID: 2A6E VALUE: measured_data.temp
TEMP2_LEVEL_INPUT_CCB_H, //UUID: 2902, VALUE: temp2ValCCC
#endif
#if (DEV_SERVICES & SERVICE_IUS)
/**********************************************************************************************/
ANA_PS_H, //UUID: 2800, VALUE: uuid 181A
ANA_VALUE_INPUT_CD_H, //UUID: 2803, VALUE: Prop: Read | Notify
ANA_VALUE_INPUT_DP_H, //UUID: 2A1F VALUE: analog value (uint16)
ANA_VALUE_INPUT_CCB_H, //UUID: 2902, VALUE: anapValCCC
#endif
//// Telink OTA ////
/**********************************************************************************************/
OTA_PS_H, //UUID: 2800, VALUE: telink ota service uuid
OTA_CMD_OUT_CD_H, //UUID: 2803, VALUE: Prop: read | write_without_rsp
OTA_CMD_OUT_DP_H, //UUID: telink ota uuid, VALUE: otaData
OTA_CMD_OUT_DESC_H, //UUID: 2901, VALUE: otaName
//// Custom RxTx ////
/**********************************************************************************************/
RxTx_PS_H, //UUID: 2800, VALUE: 1F10 RxTx service uuid
RxTx_CMD_OUT_CD_H, //UUID: 2803, VALUE: Prop: read | write_without_rsp
RxTx_CMD_OUT_DP_H, //UUID: 1F1F, VALUE: RxTxData
RxTx_CMD_OUT_DESC_H, //UUID: 2902, VALUE: RxTxValueInCCC
// Mi Advertising char
/**********************************************************************************************/
Mi_PS_H, //UUID: 2800, VALUE: 0xFE95 service uuid
Mi_CMD_OUT_DESC_H, //UUID: 2901, VALUE: my_MiName
ATT_END_H
} ATT_HANDLE;
void app_enter_ota_mode(void);
void set_adv_data(void);
void my_att_init();
void init_ble();
void ble_set_name(void);
void ble_send_measures(void);
void ble_send_ext(void);
void ble_send_lcd(void);
void ble_send_cmf(void);
#if (DEV_SERVICES & SERVICE_RDS) || (DEV_SERVICES & SERVICE_TH_TRG)
void ble_send_trg(void);
void ble_send_trg_flg(void);
#endif
#if (DEV_SERVICES & SERVICE_KEY) || (DEV_SERVICES & SERVICE_RDS)
void set_adv_con_time(int restore);
#endif
#if (DEV_SERVICES & SERVICE_HISTORY)
void send_memo_blk(void);
#endif
int otaWritePre(void * p);
#ifdef CHG_CONN_PARAM
int chgConnParameters(void * p);
#endif
int RxTxWrite(void * p);
void ev_adv_timeout(u8 e, u8 *p, int n);
void set_pvvx_adv_data(void);
void set_atc_adv_data(void);
void set_mi_adv_data(void);
#if (DEV_SERVICES & SERVICE_LE_LR)
void load_adv_data(void);
#endif
#if (DEV_SERVICES & (SERVICE_THS | SERVICE_18B20 | SERVICE_PLM))
inline void ble_send_temp01(void) {
bls_att_pushNotifyData(TEMP_LEVEL_INPUT_DP_H, (u8 *) &measured_data.temp_x01, 2);
}
inline void ble_send_temp001(void) {
#if (DEV_SERVICES & (SERVICE_THS | SERVICE_PLM))
bls_att_pushNotifyData(TEMP2_LEVEL_INPUT_DP_H, (u8 *) &measured_data.temp, 2);
#else
bls_att_pushNotifyData(TEMP2_LEVEL_INPUT_DP_H, (u8 *) &measured_data.xtemp[0], 2);
#endif
}
#if (DEV_SERVICES & (SERVICE_THS | SERVICE_PLM))
inline void ble_send_humi(void) {
bls_att_pushNotifyData(HUMI_LEVEL_INPUT_DP_H, (u8 *) &measured_data.humi, 2);
}
#endif
#endif
#if (DEV_SERVICES & SERVICE_IUS)
inline void ble_send_ana(void) {
bls_att_pushNotifyData(ANA_VALUE_INPUT_DP_H, (u8 *) &measured_data.voltage, 2);
}
#endif
inline void ble_send_battery(void) {
bls_att_pushNotifyData(BATT_LEVEL_INPUT_DP_H, (u8 *) &measured_data.battery_level, 1);
}
inline void ble_send_cfg(void) {
memcpy(&my_RxTx_Data[2], &cfg, sizeof(cfg));
bls_att_pushNotifyData(RxTx_CMD_OUT_DP_H, my_RxTx_Data, sizeof(cfg) + 3);
}