Skip to content

Commit

Permalink
https://github.com/pvvx/pvvx.github.io/issues/4#issue-904241526
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed May 28, 2021
1 parent ac6a4ab commit 866987d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 53 deletions.
Binary file renamed ATC_Thermometer32r.bin → ATC_Thermometer32n.bin
Binary file not shown.
Binary file renamed CGG1_v32r.bin → CGG1_v32n.bin
Binary file not shown.
Binary file renamed MHO_C401_v32r.bin → MHO_C401_v32n.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion firmware.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{"version": 50,
"custom":["ATC_Thermometer32r.bin", "MHO_C401_v32r.bin", "CGG1_v32r.bin"],
"custom":["ATC_Thermometer32n.bin", "MHO_C401_v32n.bin", "CGG1_v32n.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"]}
2 changes: 1 addition & 1 deletion src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern "C" {
#define DEVICE_MHO_C401 0x0387 // E-Ink display MHO-C401
#define DEVICE_CGG1 0x0B48 // E-Ink display CGG1-M "Qingping Temp & RH Monitor"

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

#define BLE_SECURITY_ENABLE 1
#define BLE_HOST_SMP_ENABLE BLE_SECURITY_ENABLE
Expand Down
93 changes: 50 additions & 43 deletions src/cmd_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#endif
#include "cmd_parser.h"

#define _flash_read(faddr,len,pbuf) flash_read_page(FLASH_BASE_ADDR + (uint32_t)faddr, len, (uint8_t *)pbuf)

#define TX_MAX_SIZE (ATT_MTU_SIZE-3) // = 20
#define FLASH_MIMAC_ADDR CFG_ADR_MAC // 0x76000
#define FLASH_MIKEYS_ADDR 0x78000
Expand All @@ -41,53 +43,58 @@ enum {
RAM blk_mi_keys_t keybuf;

#if DEVICE_TYPE == DEVICE_MHO_C401
uint8_t * find_mi_keys(uint16_t chk_id, uint8_t cnt) {
uint8_t * p = (uint8_t *)(FLASH_MIKEYS_ADDR);
uint8_t * pend = p + FLASH_SECTOR_SIZE;
uint32_t find_mi_keys(uint16_t chk_id, uint8_t cnt) {
uint32_t faddr = FLASH_MIKEYS_ADDR;
uint32_t faend = faddr + FLASH_SECTOR_SIZE;
pblk_mi_keys_t pk = &keybuf;
uint16_t id;
uint8_t len;
uint8_t fbuf[4];
do {
len = p[1];
id = p[2] | (p[3] << 8);
if(p[0] == 0xA5) {
p += 8;
_flash_read(faddr, sizeof(fbuf), &fbuf);
len = fbuf[1];
id = fbuf[2] | (fbuf[3] << 8);
if(fbuf[0] == 0xA5) {
faddr += 8;
if(len <= sizeof(keybuf.data)
&& len > 0
&& id == chk_id
&& --cnt == 0) {
pk->klen = len;
memcpy(&pk->data, p, len);
return p;
_flash_read(faddr, len, &pk->data);
return faddr;
}
}
p += len + 0x0f;
p = (uint8_t *)((uint32_t)(p) & 0xfffffff0);
} while(id != 0xffff || len != 0xff || p < pend);
return NULL;
faddr += len + 0x0f;
faddr &= 0xfffffff0;
} while(id != 0xffff || len != 0xff || faddr < faend);
return 0;
}
#else // DEVICE_LYWSD03MMC & DEVICE_CGG1
uint8_t * find_mi_keys(uint16_t chk_id, uint8_t cnt) {
uint8_t * p = (uint8_t *)(FLASH_MIKEYS_ADDR);
uint8_t * pend = p + FLASH_SECTOR_SIZE;
/* if return != 0 -> keybuf = keys */
uint32_t find_mi_keys(uint16_t chk_id, uint8_t cnt) {
uint32_t faddr = FLASH_MIKEYS_ADDR;
uint32_t faend = faddr + FLASH_SECTOR_SIZE;
pblk_mi_keys_t pk = &keybuf;
uint16_t id;
uint8_t len;
uint8_t fbuf[3];
do {
id = p[0] | (p[1] << 8);
len = p[2];
p += 3;
_flash_read(faddr, sizeof(fbuf), &fbuf);
id = fbuf[0] | (fbuf[1] << 8);
len = fbuf[2];
faddr += 3;
if(len <= sizeof(keybuf.data)
&& len > 0
&& id == chk_id
&& --cnt == 0) {
pk->klen = len;
memcpy(&pk->data, p, len);
return p;
_flash_read(faddr, len, &pk->data);
return faddr;
}
p += len;
} while(id != 0xffff || len != 0xff || p < pend);
return NULL;
faddr += len;
} while(id != 0xffff || len != 0xff || faddr < faend);
return 0;
}
#endif

Expand All @@ -110,35 +117,35 @@ void send_mi_no_key(void) {
keybuf.klen = 0;
bls_att_pushNotifyData(RxTx_CMD_OUT_DP_H, (u8 *) &keybuf, 2);
}

/* if pkey == NULL -> write new key, else: change deleted keys and current keys*/
uint8_t store_mi_keys(uint8_t klen, uint16_t key_id, uint8_t * pkey) {
uint8_t key_chk_cnt = 0;
uint8_t * pfoldkey = NULL;
uint8_t * pfnewkey;
uint8_t * p;
uint32_t faoldkey = 0;
uint32_t fanewkey;
uint32_t faddr;
if(pkey == NULL) {
while((p = find_mi_keys(MI_KEYDELETE_ID, ++key_chk_cnt)) != NULL) {
if(p && keybuf.klen == klen)
pfoldkey = p;
while((faddr = find_mi_keys(MI_KEYDELETE_ID, ++key_chk_cnt)) != 0) {
if(faddr && keybuf.klen == klen)
faoldkey = faddr;
}
};
if(pfoldkey || pkey) {
pfnewkey = find_mi_keys(key_id, 1);
if(pfnewkey && keybuf.klen == klen) {
if(faoldkey || pkey) {
fanewkey = find_mi_keys(key_id, 1);
if(fanewkey && keybuf.klen == klen) {
uint8_t backupsector[FLASH_SECTOR_SIZE];
memcpy(&backupsector,(uint8_t *)(FLASH_MIKEYS_ADDR), sizeof(backupsector));
_flash_read(FLASH_MIKEYS_ADDR, sizeof(backupsector), &backupsector);
if(pkey) {
if(memcmp(pfnewkey, pkey, keybuf.klen)) {
memcpy(&backupsector[(uint32_t)pfnewkey - FLASH_MIKEYS_ADDR], pkey, keybuf.klen);
if(memcmp(&backupsector[fanewkey - FLASH_MIKEYS_ADDR], pkey, keybuf.klen)) {
memcpy(&backupsector[fanewkey - FLASH_MIKEYS_ADDR], pkey, keybuf.klen);
flash_erase_sector(FLASH_MIKEYS_ADDR);
flash_write_all_size(FLASH_MIKEYS_ADDR, sizeof(backupsector), backupsector);
return 1;
}
} else {
if(memcmp(pfnewkey, pfoldkey, keybuf.klen)) {
memcpy(&backupsector[(uint32_t)pfoldkey - FLASH_MIKEYS_ADDR], pfnewkey, keybuf.klen);
memcpy(&keybuf.data, pfoldkey, keybuf.klen);
memcpy(&backupsector[(uint32_t)pfnewkey - FLASH_MIKEYS_ADDR], pfoldkey, keybuf.klen);
} else if (faoldkey) {
if(memcmp(&backupsector[fanewkey - FLASH_MIKEYS_ADDR], &backupsector[faoldkey - FLASH_MIKEYS_ADDR], keybuf.klen)) {
// memcpy(&keybuf.data, &backupsector[faoldkey - FLASH_MIKEYS_ADDR], keybuf.klen);
memcpy(&backupsector[faoldkey - FLASH_MIKEYS_ADDR], &backupsector[fanewkey - FLASH_MIKEYS_ADDR], keybuf.klen);
memcpy(&backupsector[fanewkey - FLASH_MIKEYS_ADDR], &keybuf.data, keybuf.klen);
flash_erase_sector(FLASH_MIKEYS_ADDR);
flash_write_all_size(FLASH_MIKEYS_ADDR, sizeof(backupsector), backupsector);
return 1;
Expand Down Expand Up @@ -315,7 +322,7 @@ __attribute__((optimize("-Os"))) void cmd_parser(void * p) {
get_mi_keys(MI_KEY_STAGE_MAC);
mi_key_stage = MI_KEY_STAGE_WAIT_SEND;
#if USE_MIHOME_BEACON
} else if (cmd == CMD_ID_BKEY) { // Get/set beacon bkey
} else if (cmd == CMD_ID_BKEY) { // Get/set beacon bindkey
if(len == sizeof(bindkey) + 1) {
memcpy(bindkey, &req->dat[1], sizeof(bindkey));
flash_write_cfg(bindkey, EEP_ID_KEY, sizeof(bindkey));
Expand Down
4 changes: 2 additions & 2 deletions src/cmd_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum {
CMD_ID_MI_KALL = 0x15, // Get all mi keys
CMD_ID_MI_REST = 0x16, // Restore prev mi token & bindkeys
CMD_ID_MI_CLR = 0x17, // Delete all mi keys
CMD_ID_BKEY = 0x18, // Get/set beacon bkey in EEP
CMD_ID_BKEY = 0x18, // Get/set beacon bindkey in EEP
CMD_ID_COMFORT = 0x20, // Get/set comfort parameters
CMD_ID_EXTDATA = 0x22, // Get/set show ext. data
CMD_ID_UTC_TIME = 0x23, // Get/set utc time (if USE_CLOCK = 1)
Expand Down Expand Up @@ -46,7 +46,7 @@ typedef struct __attribute__((packed)) _blk_mi_keys_t {
} blk_mi_keys_t, * pblk_mi_keys_t;
extern blk_mi_keys_t keybuf;

uint8_t * find_mi_keys(uint16_t chk_id, uint8_t cnt);
uint32_t find_mi_keys(uint16_t chk_id, uint8_t cnt);

uint8_t mi_key_stage;
uint8_t get_mi_keys(uint8_t chk_stage);
Expand Down
12 changes: 6 additions & 6 deletions src/mi_beacon.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ RAM mib_summ_data_t mib_summ_data;

/* Initializing data for mi beacon */
void mi_beacon_init(void) {
uint8_t *p_key = find_mi_keys(MI_KEYTBIND_ID, 1);
if(p_key) {
memcpy(&bindkey, p_key + 12, sizeof(bindkey));
p_key = find_mi_keys(MI_KEYSEQNUM_ID, 1);
if(p_key)
memcpy(&beacon_nonce.cnt32, p_key, 4); // BLE_GAP_AD_TYPE_FLAGS
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(&beacon_nonce.cnt32, &keybuf.data, 4); // BLE_GAP_AD_TYPE_FLAGS
} else {
if(flash_read_cfg(&bindkey, EEP_ID_KEY, sizeof(bindkey)) != sizeof(bindkey)) {
generateRandomNum(sizeof(bindkey), (unsigned char *)&bindkey);
Expand Down

0 comments on commit 866987d

Please sign in to comment.