Skip to content

Commit

Permalink
support for "HA BLE" beacon encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed Mar 7, 2022
1 parent 649242a commit aeff2e5
Show file tree
Hide file tree
Showing 27 changed files with 1,015 additions and 528 deletions.
Binary file removed ATC_v37a.bin
Binary file not shown.
Binary file added ATC_v37b.bin
Binary file not shown.
Binary file removed CGDK2_v37a.bin
Binary file not shown.
Binary file added CGDK2_v37b.bin
Binary file not shown.
Binary file removed CGG1_v37a.bin
Binary file not shown.
Binary file added CGG1_v37b.bin
Binary file not shown.
Binary file removed MHO_C401_v37a.bin
Binary file not shown.
Binary file added MHO_C401_v37b.bin
Binary file not shown.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The custom firmware can be flashed _via a modern browser_ and _over-the-air (OTA

* Extended battery life (over a year)
* Improved measurement accuracy and extended format in 0.01 units
* Full support in HA ['Passive BLE Monitor integration'](https://github.com/custom-components/ble_monitor) and 'HA BLE' format (DIY sensors)
* Full support in HA ['Passive BLE Monitor integration'](https://github.com/custom-components/ble_monitor) and 'HA BLE' format
* Supports [encrypted connections using **PIN-code**](https://github.com/pvvx/ATC_MiThermometer/issues/174#issuecomment-1003987084) and encrypted **bindkey** beacon
* Support BT5.0+ PHY, CSA1/CSA2
* **3 LCD Display Screens** (Looping): Temperature & Humidity & Comfort, Temperature & Battery Level, Clock
Expand Down Expand Up @@ -110,10 +110,10 @@ You can directly update/flash the firmware without downloading the binaries belo

**Custom Firmware Versions:**

* [LYWSD03MMC Custom Firmware Version 3.7](https://github.com/pvvx/ATC_MiThermometer/raw/master/ATC_v37a.bin) (HW: B1.4, B1.6, B1.7, B1.9, B2.0)
* [MHO-C401 Custom Firmware Version 3.7](https://github.com/pvvx/ATC_MiThermometer/raw/master/MHO_C401_v37a.bin) (manufacturer Xiaomi, not [Azarton](https://github.com/pvvx/ATC_MiThermometer/issues/114)!)
* [CGG1 Custom Firmware Version 3.7](https://github.com/pvvx/ATC_MiThermometer/raw/master/CGG1_v37a.bin) ([CGG1-M](https://github.com/pvvx/ATC_MiThermometer/issues/41#issuecomment-812803456))
* [CGDK2 Custom Firmware Version 3.7](https://github.com/pvvx/ATC_MiThermometer/raw/master/CGDK2_v37a.bin) ([CGDK2](https://pvvx.github.io/CGDK2))
* [LYWSD03MMC Custom Firmware Version 3.7](https://github.com/pvvx/ATC_MiThermometer/raw/master/ATC_v37b.bin) (HW: B1.4, B1.6, B1.7, B1.9, B2.0)
* [MHO-C401 Custom Firmware Version 3.7](https://github.com/pvvx/ATC_MiThermometer/raw/master/MHO_C401_v37b.bin) (manufacturer Xiaomi, not [Azarton](https://github.com/pvvx/ATC_MiThermometer/issues/114)!)
* [CGG1 Custom Firmware Version 3.7](https://github.com/pvvx/ATC_MiThermometer/raw/master/CGG1_v37b.bin) ([CGG1-M](https://github.com/pvvx/ATC_MiThermometer/issues/41#issuecomment-812803456))
* [CGDK2 Custom Firmware Version 3.7](https://github.com/pvvx/ATC_MiThermometer/raw/master/CGDK2_v37b.bin) ([CGDK2](https://pvvx.github.io/CGDK2))

**Original Manufacturer Firmware Version**

Expand Down Expand Up @@ -157,7 +157,8 @@ In case you want to go back to the original firmware, you can download them here
| 3.4 | Correct Hardware Version Setting for [LYWSD03MMC B1.7](https://github.com/pvvx/ATC_MiThermometer/issues/145) |
| 3.5 | Correction of moisture readings for SHT4x sensors. [Rounding off sensor values on display.](https://github.com/pvvx/ATC_MiThermometer/issues/163). Saving HW string B2.0 on LYWSD03MMC. Eliminated [battery voltage noise](https://github.com/pvvx/ATC_MiThermometer/issues/180) in CGG1-M. CGG1 - correction of the battery charge display. Added CGDK2 and modified [(DIY) variant of CGDK2-2](https://pvvx.github.io/CGDK2/CGDK2-2/). |
| 3.6 | Additional parameters: Support BT5.0 PHY, Channel Selection Algorithm 2, Correct RF-TX Power in suspend |
| 3.7 | Added [format 'HA BLE' (DIY sensors)](https://github.com/custom-components/ble_monitor/issues/548), reed switch operation mode: Switch and Count |
| 3.7 | Added [format 'HA BLE'](https://github.com/custom-components/ble_monitor/issues/548), reed switch operation mode: Switch and Count |
| 3.7b | Full support for "HA BLE" encryption |

## Applications

Expand Down
2 changes: 1 addition & 1 deletion firmware.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{"version": 55,
"custom":["ATC_v37a.bin", "MHO_C401_v37a.bin", "CGG1_v37a.bin", "ATC_v37a.bin", "ATC_v37a.bin", "ATC_v37a.bin", "CGDK2_v37a.bin", "?"],
"custom":["ATC_v37b.bin", "MHO_C401_v37b.bin", "CGG1_v37b.bin", "ATC_v37b.bin", "ATC_v37b.bin", "ATC_v37b.bin", "CGDK2_v37b.bin", "?"],
"original":["Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0130.bin","Original_OTA_Xiaomi_MHO_C401_v1.0.0_0010.bin","Original_OTA_CGG1_v1.0.1_0093.bin","Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0130.bin","Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0130.bin","Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0130.bin", "Original_OTA_CGDK2_v1.1.1_0217.bin", "?"]}
52 changes: 47 additions & 5 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#if USE_MIHOME_BEACON
#include "mi_beacon.h"
#endif
#if USE_HA_BLE_BEACON
#include "ha_ble_beacon.h"
#endif

void app_enter_ota_mode(void);

Expand All @@ -30,9 +33,6 @@ RAM uint8_t show_stage; // count/stage update lcd code buffer
RAM lcd_flg_t lcd_flg;

RAM measured_data_t measured_data;
//RAM int16_t last_temp; // x0.1 C
//RAM uint16_t last_humi; // x1 %
//RAM uint8_t battery_level; // 0..100%

RAM volatile uint8_t tx_measures;

Expand All @@ -55,6 +55,10 @@ RAM uint32_t utc_time_tick_step = CLOCK_16M_SYS_TIMER_CLK_1S; // adjust time clo
#define utc_time_tick_step CLOCK_16M_SYS_TIMER_CLK_1S
#endif

#if USE_SECURITY_BEACON
RAM uint8_t bindkey[16];
#endif

RAM scomfort_t cmf;
const scomfort_t def_cmf = {
.t = {2100,2600}, // x0.01 C
Expand Down Expand Up @@ -127,6 +131,10 @@ void set_hw_version(void) {
else
cfg.hw_cfg.shtc3 = 0; // = 0 - sensor SHT4x or ?
#if DEVICE_TYPE == DEVICE_LYWSD03MMC
#if USE_DEVICE_INFO_CHR_UUID
#else
uint8_t my_HardStr[4];
#endif
if (flash_read_cfg(&my_HardStr, EEP_ID_HWV, sizeof(my_HardStr)) != sizeof(my_HardStr)
|| my_HardStr[0] != 'B'
|| my_HardStr[2] != '.' ) {
Expand Down Expand Up @@ -249,7 +257,7 @@ _attribute_ram_code_ void WakeupLowPowerCb(int par) {
if (cfg.averaging_measurements)
write_memo();
#endif
#if USE_MIHOME_BEACON
#if USE_MIHOME_BEACON && USE_SECURITY_BEACON
if ((cfg.flg.advertising_type == ADV_TYPE_MI) && cfg.flg2.adv_crypto)
mi_beacon_summ();
#endif
Expand Down Expand Up @@ -339,6 +347,37 @@ uint32_t get_mi_hw_version(void) {
return hw;
}
#endif // DEVICE_TYPE == DEVICE_LYWSD03MMC

#if USE_SECURITY_BEACON
void bindkey_init(void) {
#if USE_MIHOME_BEACON
uint32_t faddr = find_mi_keys(MI_KEYTBIND_ID, 1);
if (faddr) {
memcpy(&bindkey, &keybuf.data[12], sizeof(bindkey));
faddr = find_mi_keys(MI_KEYSEQNUM_ID, 1);
if (faddr)
memcpy(&adv_buf.send_count, &keybuf.data, sizeof(adv_buf.send_count)); // BLE_GAP_AD_TYPE_FLAGS
} else {
if (flash_read_cfg(&bindkey, EEP_ID_KEY, sizeof(bindkey))
!= sizeof(bindkey)) {
generateRandomNum(sizeof(bindkey), (unsigned char *) &bindkey);
flash_write_cfg(&bindkey, EEP_ID_KEY, sizeof(bindkey));
}
}
mi_beacon_init();
#else
if (flash_read_cfg(&bindkey, EEP_ID_KEY, sizeof(bindkey))
!= sizeof(bindkey)) {
generateRandomNum(sizeof(bindkey), (unsigned char *) &bindkey);
flash_write_cfg(&bindkey, EEP_ID_KEY, sizeof(bindkey));
}
#endif // USE_MIHOME_BEACON
#if USE_HA_BLE_BEACON
ha_ble_beacon_init();
#endif
}
#endif // USE_SECURITY_BEACON

//------------------ user_init_normal -------------------
void user_init_normal(void) {//this will get executed one time after power up
bool next_start = false;
Expand Down Expand Up @@ -385,7 +424,7 @@ void user_init_normal(void) {//this will get executed one time after power up
#endif
#if DEVICE_TYPE == DEVICE_LYWSD03MMC
if (hw_ver)
flash_write_cfg(&hw_ver, EEP_ID_HWV, sizeof(hw_ver));
flash_write_cfg(&hw_ver, EEP_ID_HWV, sizeof(hw_ver));
#endif
}
#if USE_WK_RDS_COUNTER
Expand All @@ -407,6 +446,9 @@ void user_init_normal(void) {//this will get executed one time after power up
sensor_go_sleep();
#else
start_measure_sensor_low_power();
#endif
#if USE_SECURITY_BEACON
bindkey_init();
#endif
check_battery();
WakeupLowPowerCb(0);
Expand Down
5 changes: 5 additions & 0 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ typedef struct _comfort_t {
uint16_t h[2];
}scomfort_t, * pcomfort_t;

#if USE_SECURITY_BEACON
extern uint8_t bindkey[16];
void bindkey_init(void);
#endif

extern scomfort_t cmf;
#if BLE_SECURITY_ENABLE
extern uint32_t pincode;
Expand Down
31 changes: 17 additions & 14 deletions src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ extern "C" {
#define DEVICE_CGG1 0x0B48 // E-Ink display CGG1-M "Qingping Temp & RH Monitor"
#define DEVICE_CGDK2 0x066F // LCD display "Qingping Temp & RH Monitor Lite"

#define DEVICE_TYPE DEVICE_LYWSD03MMC // DEVICE_LYWSD03MMC or DEVICE_MHO_C401 or DEVICE_CGG1 or DEVICE_CGDK2
#define DEVICE_TYPE DEVICE_MHO_C401 // DEVICE_LYWSD03MMC or DEVICE_MHO_C401 or DEVICE_CGG1 or DEVICE_CGDK2

/* Special DIY version - Voltage Logger:
* Temperature 0..36.00 = ADC pin PB7 input 0..3.6V, LYWSD03MMC pcb mark "B1"
* Humidity 0..36.00 = ADC pin PC4 input 0..3.6V, LYWSD03MMC pcb mark "P9"
* Set DIY_ADC_TO_TH 1 */
#define DIY_ADC_TO_TH 0
#define BLE_SECURITY_ENABLE 1 // = 1 support pin-code

#define BLE_SECURITY_ENABLE 1
#define BLE_HOST_SMP_ENABLE BLE_SECURITY_ENABLE

#define USE_TRIGGER_OUT 1 // use trigger out (GPIO_PA5)
#define USE_CLOCK 1 // = 1 display clock, = 0 smile blinking
#define USE_TIME_ADJUST 1 // = 1 time correction enabled
#define USE_FLASH_MEMO 1 // = 1 flash logger enable
#define USE_WK_RDS_COUNTER 1 // = 1 wake up when the reed switch is triggered + pulse counter (Not completed! Only: CGDK2, LYWSD03MMC)
#define USE_HA_BLE_FORMAT 1 // if USE_WK_RDS_COUNTER = 1, https://github.com/custom-components/ble_monitor/issues/548
#define USE_TRIGGER_OUT 1 // = 1 use trigger out (GPIO_PA5)
#define USE_WK_RDS_COUNTER USE_TRIGGER_OUT // = 1 wake up when the reed switch is triggered + pulse counter

#define USE_SECURITY_BEACON 1 // = 1 support encryption beacon (bindkey)
#define USE_HA_BLE_BEACON 1 // = 1 https://github.com/custom-components/ble_monitor/issues/548
#define USE_MIHOME_BEACON 1 // = 1 Compatible with MiHome beacon

#define USE_DEVICE_INFO_CHR_UUID 1 // = 1 enable Device Information Characteristics

/* Special DIY version - Voltage Logger:
* Temperature 0..36.00 = ADC pin PB7 input 0..3.6V, LYWSD03MMC pcb mark "B1"
* Humidity 0..36.00 = ADC pin PC4 input 0..3.6V, LYWSD03MMC pcb mark "P9"
* Set DIY_ADC_TO_TH 1 */
#define DIY_ADC_TO_TH 0

#define USE_MIHOME_SERVICE 0 // = 1 MiHome service compatibility (missing in current version! Set = 0!)
#define USE_MIHOME_BEACON 1 // = 1 Compatible with MiHome beacon encryption
#define USE_NEW_OTA 0 // = 1 keeping the old firmware, erasing the region when updating (test version only!)

#define UART_PRINT_DEBUG_ENABLE 0 // =1 use u_printf() (PA7/SWS), source: SDK/components/application/print/u_printf.c

#if DEVICE_TYPE == DEVICE_MHO_C401
Expand Down Expand Up @@ -412,6 +413,8 @@ enum{
/* flash sector address with binding information */
#define CFG_ADR_BIND 0x74000 //no master, slave device (blt_config.h)

#define BLE_HOST_SMP_ENABLE BLE_SECURITY_ENABLE

#include "vendor/common/default_config.h"

#if defined(__cplusplus)
Expand Down
Loading

0 comments on commit aeff2e5

Please sign in to comment.