Skip to content

Commit

Permalink
Revision of switch formats of advertising beacon, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed May 24, 2021
1 parent fe6933b commit a01771a
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 89 deletions.
Binary file renamed ATC_Thermometer32a.bin → ATC_Thermometer32r.bin
Binary file not shown.
Binary file removed CGG1_v32a.bin
Binary file not shown.
Binary file added CGG1_v32r.bin
Binary file not shown.
Binary file renamed MHO_C401_v32a.bin → MHO_C401_v32r.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_Thermometer32a.bin", "MHO_C401_v32a.bin", "CGG1_v32a.bin"],
"custom":["ATC_Thermometer32r.bin", "MHO_C401_v32r.bin", "CGG1_v32r.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"]}
10 changes: 5 additions & 5 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const cfg_t def_cfg = {
.flg2.smiley = 0, // 0 = " " off
.flg.blinking_time_smile = false,
.flg.show_batt_enabled = false,
.flg.advertising_type = 3,
.flg.advertising_type = ADV_TYPE_DEFAULT,
.flg.tx_measures = false,
.advertising_interval = 40, // multiply by 62.5 ms = 2.5 sec
#if DEVICE_TYPE == DEVICE_LYWSD03MMC
Expand Down Expand Up @@ -189,13 +189,13 @@ _attribute_ram_code_ void WakeupLowPowerCb(int par) {
write_memo();
#endif
#if USE_MIHOME_BEACON
if((cfg.flg.advertising_type & 2) && cfg.flg2.mi_beacon)
if((cfg.flg.advertising_type & ADV_TYPE_MASK_REF) && cfg.flg2.mi_beacon)
mi_beacon_summ();
#endif
#if USE_TRIGGER_OUT && defined(GPIO_RDS)
test_trg_input();
#endif
set_adv_data(cfg.flg.advertising_type);
set_adv_data();
end_measure = 1;
}
timer_measure_cb = 0;
Expand Down Expand Up @@ -515,10 +515,10 @@ _attribute_ram_code_ void main_loop(void) {
bls_pm_setAppWakeupLowPower(0, 0);
}
}
if((cfg.flg.advertising_type & 2) // type 2 - Mi and 3 - all
if((cfg.flg.advertising_type & ADV_TYPE_MASK_REF) // type 2 - Mi and 3 - all
&& blc_ll_getCurrentState() == BLS_LINK_STATE_ADV) {
if(adv_old_count != adv_send_count)
set_adv_data(cfg.flg.advertising_type);
set_adv_data();
}
#if (DEVICE_TYPE == DEVICE_MHO_C401) || (DEVICE_TYPE == DEVICE_CGG1)
if(wrk_measure == 0 && stage_lcd) {
Expand Down
14 changes: 12 additions & 2 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@
#define EEP_ID_TIM (0x0ADA) // EEP ID time adjust
#define EEP_ID_KEY (0xBEAC) // EEP ID bkey

enum {
ADV_TYPE_ATC = 0,
ADV_TYPE_PVVX, // (default)
ADV_TYPE_MI,
ADV_TYPE_ALL
} ADV_TYPE_ENUM;

#define ADV_TYPE_MASK_REF 2 // advertising_type & ADV_TYPE_MASK_REF = ADV_TYPE_MI, ADV_TYPE_ALL -> refresh all beacon -> set_adv_data() in main cycle
#define ADV_TYPE_DEFAULT ADV_TYPE_PVVX

typedef struct __attribute__((packed)) _cfg_t {
struct __attribute__((packed)) {
uint8_t advertising_type : 2; // 0 - atc1441, 1 - Custom, 2 - Mi, 3 - all
uint8_t advertising_type : 2; // 0 - atc1441, 1 - Custom (pvvx), 2 - Mi, 3 - all
uint8_t comfort_smiley : 1;
uint8_t blinking_time_smile : 1; //(USE_CLOCK = 0 - smile, =1 time)
uint8_t temp_F_or_C : 1;
Expand Down Expand Up @@ -48,7 +58,7 @@ typedef struct __attribute__((packed)) _cfg_t {
* 6 = "^-^" sad
* 7 = "oOo" */
uint8_t smiley : 3; // 0..7
uint8_t mi_beacon : 1; // advertising uses mi crypto beacon
uint8_t mi_beacon : 1; // advertising uses crypto beacon
uint8_t adv_flags : 1; // advertising add flags
uint8_t reserved : 3;
} flg2;
Expand Down
34 changes: 17 additions & 17 deletions src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int otaWritePre(void * p) {
_attribute_ram_code_
int app_advertise_prepare_handler(rf_packet_adv_t * p) {
adv_send_count++;
// set_adv_data(cfg.flg.advertising_type);
// set_adv_data();
return 1; // = 1 ready to send ADV packet, = 0 not send ADV
}

Expand Down Expand Up @@ -313,14 +313,15 @@ __attribute__((optimize("-Os"))) void init_ble(void) {
ev_adv_timeout(0,0,0);
}

/* adv_type: 0 - atc1441, 1 - Custom, 2,3 - Mi */
_attribute_ram_code_
__attribute__((optimize("-Os")))
void set_adv_data(uint8_t adv_type) {
void set_adv_data() {
uint8_t adv_type = cfg.flg.advertising_type; // 0 - atc1441, 1 - pvvx, 2 - Mi, 3 - all
adv_old_count = adv_send_count;
if(adv_type == 3)
if(adv_type == ADV_TYPE_ALL)
adv_type = adv_old_count & 3;
if(adv_type == 1) {
/* adv_type: 0 - atc1441, 1 - Custom, 2,3 - Mi */
if(adv_type == ADV_TYPE_PVVX) {
if(cfg.flg2.mi_beacon)
pvvx_encrypt_beacon(measured_data.count);
else {
Expand All @@ -332,7 +333,7 @@ void set_adv_data(uint8_t adv_type) {
p->size = sizeof(adv_custom_t) - 2;
#endif
p->uid = GAP_ADTYPE_SERVICE_DATA_UUID_16BIT; // 16-bit UUID
p->UUID = 0x181A; // GATT Service 0x181A Environmental Sensing (little-endian)
p->UUID = ADV_CUSTOM_UUID16; // GATT Service 0x181A Environmental Sensing (little-endian)
p->temperature = measured_data.temp; // x0.01 C
p->humidity = measured_data.humi; // x0.01 %
p->battery_mv = measured_data.battery_mv; // x mV
Expand All @@ -342,18 +343,22 @@ void set_adv_data(uint8_t adv_type) {
p->flags = trg.flg_byte;
#endif
}
} else if(adv_type & 2) { // adv_type == 2 or 3
} else if(adv_type & ADV_TYPE_MASK_REF) { // adv_type == 2 or 3
#if USE_MIHOME_BEACON
if(cfg.flg2.mi_beacon)
mi_encrypt_beacon(measured_data.count >> 2);
if(cfg.flg2.mi_beacon) {
if(cfg.flg.advertising_type == ADV_TYPE_ALL)
mi_encrypt_beacon(measured_data.count);
else
mi_encrypt_beacon(measured_data.count >> 2);
}
else
#endif
{
padv_mi_t p = (padv_mi_t)&adv_buf.data;
memcpy(p->MAC, mac_public, 6);
p->size = sizeof(adv_mi_t) - 1;
p->uid = GAP_ADTYPE_SERVICE_DATA_UUID_16BIT; // 16-bit UUID
p->UUID = 0xFE95; // 16-bit UUID for Members 0xFE95 Xiaomi Inc.
p->UUID = ADV_XIAOMI_UUID16; // 16-bit UUID for Members 0xFE95 Xiaomi Inc.
#if 0
p->ctrl.word = 0;
p->ctrl.bit.version = 3; // XIAOMI_DEV_VERSION
Expand All @@ -374,23 +379,18 @@ void set_adv_data(uint8_t adv_type) {
p->data_id = XIAOMI_DATA_ID_Power & 0xff; // (lo byte XIAOMI_DATA_ID)
p->t0a.len1 = 1;
p->t0a.battery_level = battery_level; // Battery percentage, Range: 0-100
#if 0
// non-standard
p->t0a.len2 = 2;
p->t0a.battery_mv = measured_data.battery_mv; // x1 mV
#endif
}
#if USE_MIHOME_BEACON
}
#endif
} else { // adv_type == 0
} else { // adv_type == 0 == ADV_TYPE_ATC
if(cfg.flg2.mi_beacon)
atc_encrypt_beacon(measured_data.count);
else {
padv_atc1441_t p = (padv_atc1441_t)&adv_buf.data;
p->size = sizeof(adv_atc1441_t) - 1;
p->uid = GAP_ADTYPE_SERVICE_DATA_UUID_16BIT; // 16-bit UUID
p->UUID = 0x181A; // GATT Service 0x181A Environmental Sensing (little-endian)
p->UUID = ADV_CUSTOM_UUID16; // GATT Service 0x181A Environmental Sensing (little-endian)
p->MAC[0] = mac_public[5];
p->MAC[1] = mac_public[4];
p->MAC[2] = mac_public[3];
Expand Down
5 changes: 4 additions & 1 deletion src/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ extern uint8_t mac_public[6];
extern uint8_t mac_random_static[6];
extern uint8_t ble_name[12];

#define ADV_CUSTOM_UUID16 0x181A // 16-bit UUID Service 0x181A Environmental Sensing
#define ADV_XIAOMI_UUID16 0xFE95 // 16-bit UUID for Members 0xFE95 Xiaomi Inc.

#define XIAOMI_DEV_VERSION 5

enum { // mijia ble version 5
Expand Down Expand Up @@ -295,7 +298,7 @@ typedef enum

}ATT_HANDLE;

void set_adv_data(uint8_t adv_type); // 0 - atc1441, 1 - Custom, 2 - Mi, 3 - all
void set_adv_data(void);

extern u8 my_RxTx_Data[16];

Expand Down
Loading

0 comments on commit a01771a

Please sign in to comment.