Skip to content

Commit

Permalink
ver 3.1 Fix security attributes (for pincode)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed Apr 23, 2021
1 parent c181ac9 commit e83919e
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 51 deletions.
Binary file added ATC_Thermometer31.bin
Binary file not shown.
Binary file added CGG1_v31.bin
Binary file not shown.
Binary file added MHO_C401_v31.bin
Binary file not shown.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ You can directly update/flash the firmware without downloading the binaries belo

**Custom Firmware Versions:**

* [LYWSD03MMC Custom Firmware Version 3.0](https://github.com/pvvx/ATC_MiThermometer/raw/master/ATC_Thermometer30.bin)
* [MHO-C401 Custom Firmware Version 3.0](https://github.com/pvvx/ATC_MiThermometer/raw/master/MHO_C401_v30.bin)
* [CGG1 Custom Firmware Version 3.0](https://github.com/pvvx/ATC_MiThermometer/raw/master/CGG1_v30.bin)
* [LYWSD03MMC Custom Firmware Version 3.1](https://github.com/pvvx/ATC_MiThermometer/raw/master/ATC_Thermometer31.bin)
* [MHO-C401 Custom Firmware Version 3.1](https://github.com/pvvx/ATC_MiThermometer/raw/master/MHO_C401_v31.bin)
* [CGG1 Custom Firmware Version 3.1](https://github.com/pvvx/ATC_MiThermometer/raw/master/CGG1_v31.bin)

**Original Manufacturer Firmware Version**

Expand Down Expand Up @@ -136,7 +136,8 @@ In case you want to go back to the original firmware, you can download them here
| 2.7 | Reducing power consumption of MHO-C401 (EDP update), adding version for CGG1
| 2.8 | Added saving bindkey to EEP if mi-keys are erased, reduced TX power to 0 dB for defaults
| 2.9 | Added additional id flags to advertising packages
| 3.0 | Added toggle support for advertising package structures for third-party software written by unqualified authors
| 3.0 | Added toggle support for advertising package structures for third-party software
| 3.1 | Fix security attributes for pincode


## Applications
Expand Down
4 changes: 2 additions & 2 deletions firmware.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{"version": 48,
"custom":["ATC_Thermometer30.bin", "MHO_C401_v30.bin", "CGG1_v30.bin"],
{"version": 49,
"custom":["ATC_Thermometer31.bin", "MHO_C401_v31.bin", "CGG1_v31.bin"],
"original":["Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0109.bin","Original_OTA_Xiaomi_MHO_C401_v1.0.0_0010.bin","Original_OTA_CGG1_v1.0.1_0093.bin"]}
4 changes: 2 additions & 2 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

void app_enter_ota_mode(void);

RAM uint32_t chow_tick_clk; // count chow validity time, in clock
RAM uint32_t chow_tick_sec; // count chow validity time, in sec
RAM uint32_t chow_tick_clk; // count show validity time, in clock
RAM uint32_t chow_tick_sec; // count show validity time, in sec
RAM uint8_t show_stage; // count/stage update lcd code buffer
RAM lcd_flg_t lcd_flg;

Expand Down
4 changes: 2 additions & 2 deletions src/app_att.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ RAM attribute_t my_Attributes[] = {

void my_att_init(void) {
if(pincode) {
my_Attributes[RxTx_CMD_OUT_DP_H].perm = ATT_PERMISSIONS_AUTHOR_RDWR;
my_Attributes[OTA_CMD_OUT_DP_H].perm = ATT_PERMISSIONS_AUTHOR_RDWR;
my_Attributes[RxTx_CMD_OUT_DP_H].perm = ATT_PERMISSIONS_SECURE_CONN_RDWR;
my_Attributes[OTA_CMD_OUT_DP_H].perm = ATT_PERMISSIONS_SECURE_CONN_RDWR;
} else {
my_Attributes[RxTx_CMD_OUT_DP_H].perm = ATT_PERMISSIONS_RDWR;
my_Attributes[OTA_CMD_OUT_DP_H].perm = ATT_PERMISSIONS_RDWR;
Expand Down
2 changes: 1 addition & 1 deletion src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern "C" {
#endif

#define VERSION 0x30 // BCD format (0x34 -> '3.4')
#define VERSION 0x31 // BCD format (0x34 -> '3.4')
#define EEP_SUP_VER 0x09 // EEP data minimum supported version

#define DEVICE_LYWSD03MMC 0x055B // LCD display LYWSD03MMC
Expand Down
73 changes: 35 additions & 38 deletions src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@

#include "battery.h"

RAM uint16_t batt_vol_mv;
uint8_t adc_hw_initialized = 0;
#define ADC_BUF_COUNT 8
volatile unsigned int adc_dat_buf[ADC_BUF_COUNT];

uint8_t adc_hw_initialized = 0;
volatile unsigned int adc_dat_buf[8];

_attribute_ram_code_ static void adc_bat_init(void)
{
_attribute_ram_code_ static void adc_bat_init(void) {
adc_power_on_sar_adc(0);
gpio_set_output_en(GPIO_PB5, 1);
gpio_set_input_en(GPIO_PB5, 0);
Expand All @@ -25,64 +23,63 @@ _attribute_ram_code_ static void adc_bat_init(void)
adc_set_ref_voltage(ADC_MISC_CHN, ADC_VREF_1P2V);
adc_set_tsample_cycle_chn_misc(SAMPLING_CYCLES_6);
adc_set_ain_pre_scaler(ADC_PRESCALER_1F8);
// adc_power_on_sar_adc(1);
// adc_power_on_sar_adc(1);
}

// Process takes about 83 μs.
_attribute_ram_code_ uint16_t get_battery_mv(void)
{
_attribute_ram_code_ uint16_t get_battery_mv(void) {
uint16_t temp;
int i,j;
if(!adc_hw_initialized){
int i, j;
if (!adc_hw_initialized) {
adc_hw_initialized = 1;
adc_bat_init();
}
adc_power_on_sar_adc(1); // + 0.4 mA
adc_power_on_sar_adc(1); // + 0.4 mA
adc_reset_adc_module();
u32 t0 = clock_time();

uint16_t adc_sample[8] = {0};
u32 adc_result;
for(i=0;i<8;i++){
uint16_t adc_sample[8] = { 0 };
u32 adc_average;
for (i = 0; i < ADC_BUF_COUNT; i++) {
adc_dat_buf[i] = 0;
}
while(!clock_time_exceed(t0, 25)); //wait at least 2 sample cycle(f = 96K, T = 10.4us)
adc_config_misc_channel_buf((uint16_t *)adc_dat_buf, 8<<2);
while (!clock_time_exceed(t0, 25))
; //wait at least 2 sample cycle(f = 96K, T = 10.4us)
adc_config_misc_channel_buf((uint16_t *) adc_dat_buf, sizeof(adc_dat_buf));
dfifo_enable_dfifo2();
for(i=0;i<8;i++){
while(!adc_dat_buf[i]);
if(adc_dat_buf[i] & BIT(13)){
for (i = 0; i < ADC_BUF_COUNT; i++) {
while (!adc_dat_buf[i])
;
if (adc_dat_buf[i] & BIT(13)) {
adc_sample[i] = 0;
} else {
adc_sample[i] = ((uint16_t) adc_dat_buf[i] & 0x1FFF);
}
else{
adc_sample[i] = ((uint16_t)adc_dat_buf[i] & 0x1FFF);
}
if(i){
if(adc_sample[i] < adc_sample[i-1]){
if (i) {
if (adc_sample[i] < adc_sample[i - 1]) {
temp = adc_sample[i];
adc_sample[i] = adc_sample[i-1];
for(j=i-1;j>=0 && adc_sample[j] > temp;j--){
adc_sample[j+1] = adc_sample[j];
adc_sample[i] = adc_sample[i - 1];
for (j = i - 1; j >= 0 && adc_sample[j] > temp; j--) {
adc_sample[j + 1] = adc_sample[j];
}
adc_sample[j+1] = temp;
adc_sample[j + 1] = temp;
}
}
}
dfifo_disable_dfifo2();
u32 adc_average = (adc_sample[2] + adc_sample[3] + adc_sample[4] + adc_sample[5])/4;
adc_result = adc_average;
batt_vol_mv = (adc_result * adc_vref_cfg.adc_vref) >> 10; // adc_vref default: 1175 (mV)
adc_power_on_sar_adc(0); // - 0.4 mA

return batt_vol_mv;
adc_average = (adc_sample[2] + adc_sample[3] + adc_sample[4]
+ adc_sample[5]) / 4;
return (adc_average * adc_vref_cfg.adc_vref) >> 10; // adc_vref default: 1175 (mV)
}

// 2200..3100 mv - 0..100%
_attribute_ram_code_ uint8_t get_battery_level(uint16_t battery_mv){
_attribute_ram_code_ uint8_t get_battery_level(uint16_t battery_mv) {
uint8_t battery_level = 0;
if(battery_mv > MIN_VBAT_MV) {
battery_level = (battery_mv - MIN_VBAT_MV)/((MAX_VBAT_MV - MIN_VBAT_MV)/100);
if(battery_level > 100)
if (battery_mv > MIN_VBAT_MV) {
battery_level = (battery_mv - MIN_VBAT_MV) / ((MAX_VBAT_MV
- MIN_VBAT_MV) / 100);
if (battery_level > 100)
battery_level = 100;
}
return battery_level;
Expand Down
2 changes: 1 addition & 1 deletion src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ __attribute__((optimize("-Os"))) void init_ble(void) {
if(pincode) {
//bls_smp_configParingSecurityInfoStorageAddr(0x074000);
//bls_smp_eraseAllParingInformation();
//blc_smp_param_setBondingDeviceMaxNumber(SMP_BONDING_DEVICE_MAX_NUM);
//blc_smp_param_setBondingDeviceMaxNumber(SMP_BONDING_DEVICE_MAX_NUM); //if not set, default is : SMP_BONDING_DEVICE_MAX_NUM
#if 0
//set security level: "LE_Security_Mode_1_Level_2"
blc_smp_setSecurityLevel(Unauthenticated_Paring_with_Encryption); //if not set, default is : LE_Security_Mode_1_Level_2(Unauthenticated_Paring_with_Encryption)
Expand Down
2 changes: 1 addition & 1 deletion test_adv_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def main():
decrypt_aes_ccm(binkey, pkt);

print()
print("====== Test deccode ------------------------------------------")
print("====== Test decode -------------------------------------------")
# 0 1 2 3 4 5 6 7 8 9 14 15 26
# 1a 16 95fe 5858 5b05 a8 ed5e0b38c1a4 0239ff0e350000002f044957
# pid cnt mac crypt data mic
Expand Down

0 comments on commit e83919e

Please sign in to comment.