Skip to content

Commit

Permalink
add test ext_adv
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed Jan 15, 2023
1 parent 3066f24 commit 4b7665a
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 114 deletions.
Binary file modified LR_v99.bin
Binary file not shown.
21 changes: 13 additions & 8 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ const cfg_t def_cfg = {
.flg.advertising_type = ADV_TYPE_DEFAULT,
.flg.tx_measures = false,
.flg2.smiley = 0, // 0 = " " off
.flg2.bt5hgy = 1, // support BT5.0 Coded PHY
.flg2.chalg2 = 1, // ChannelSelectionAlgorithm 2
.flg2.bt5phy = 1, // support BT5.0
.advertising_interval = 40, // multiply by 62.5 ms = 2.5 sec
#if DEVICE_TYPE == DEVICE_LYWSD03MMC
.flg.comfort_smiley = true,
Expand Down Expand Up @@ -192,6 +191,11 @@ void set_hw_version(void) {
}

__attribute__((optimize("-Os"))) void test_config(void) {
if (cfg.flg2.longrange) {
cfg.flg2.bt5phy = 1;
cfg.flg2.ext_adv = 1;
} else if (cfg.flg2.ext_adv)
cfg.flg2.bt5phy = 1;
if (cfg.rf_tx_power & BIT(7)) {
if (cfg.rf_tx_power < RF_POWER_N25p18dBm)
cfg.rf_tx_power = RF_POWER_N25p18dBm;
Expand Down Expand Up @@ -316,7 +320,6 @@ _attribute_ram_code_ void WakeupLowPowerCb(int par) {
rds_input_off();
}
#endif
//set_adv_data();
end_measure = 1;

wrk_measure = 0;
Expand Down Expand Up @@ -351,10 +354,6 @@ _attribute_ram_code_ static void suspend_enter_cb(u8 e, u8 *p, int n) {
//--- check battery
_attribute_ram_code_ void check_battery(void) {
measured_data.battery_mv = get_battery_mv();
if (measured_data.battery_mv < 2000) {
if (!(cfg.hw_cfg.shtc3 && timer_measure_cb))
low_vbat();
}
measured_data.battery_level = get_battery_level(measured_data.battery_mv);
}

Expand Down Expand Up @@ -414,7 +413,7 @@ void user_init_normal(void) {//this will get executed one time after power up
adc_power_on_sar_adc(0); // - 0.4 mA
lpc_power_down();
if (get_battery_mv() < MIN_VBAT_MV) { // 2.2V
init_sensor(); // SHTC3 go SLEEP
sensor_go_sleep(); // SHTC3 go SLEEP
cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER,
clock_time() + 120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes
}
Expand Down Expand Up @@ -464,6 +463,12 @@ void user_init_normal(void) {//this will get executed one time after power up
#if USE_WK_RDS_COUNTER
rds_init();
#endif
if(analog_read(DEEP_ANA_REG0) != 0x55) {
cfg.flg2.ext_adv = 0;
cfg.flg2.longrange = 0;
analog_write(DEEP_ANA_REG0, 0x55);
}

test_config();
memcpy(&ext, &def_ext, sizeof(ext));
init_ble();
Expand Down
8 changes: 5 additions & 3 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ enum {
} ADV_TYPE_ENUM;
#define ADV_TYPE_DEFAULT ADV_TYPE_PVVX

#define MASK_FLG2_REBOOT 0xe0
#define MASK_FLG2_EXT_ADV 0xc0
typedef struct __attribute__((packed)) _cfg_t {
struct __attribute__((packed)) {
uint8_t advertising_type : 2; // 0 - atc1441, 1 - Custom (pvvx), 2 - Mi, 3 - HA_BLE
Expand Down Expand Up @@ -64,9 +66,9 @@ typedef struct __attribute__((packed)) _cfg_t {
uint8_t smiley : 3; // 0..7
uint8_t adv_crypto : 1; // advertising uses crypto beacon
uint8_t adv_flags : 1; // advertising add flags
uint8_t bt5hgy : 1; // support BT5.0 Coded PHY
uint8_t chalg2 : 1; // ChannelSelectionAlgorithm 2
uint8_t reserved : 1;
uint8_t bt5phy : 1; // support BT5.0 Coded PHY (сбрасывается после отключения питания)
uint8_t ext_adv : 1; // support extension advertise (сбрасывается после отключения питания)
uint8_t longrange : 1; // advertising in LongRange mode (сбрасывается после отключения питания)
} flg2;
int8_t temp_offset; // Set temp offset, -12,5 - +12,5 °C (-125..125)
int8_t humi_offset; // Set humi offset, -12,5 - +12,5 % (-125..125)
Expand Down
50 changes: 29 additions & 21 deletions src/app_att.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@

static const u16 clientCharacterCfgUUID = GATT_UUID_CLIENT_CHAR_CFG; // 2902

//static const u16 extReportRefUUID = GATT_UUID_EXT_REPORT_REF; // 2907 External Report Reference
//static const u16 extReportRefUUID = GATT_UUID_EXT_REPORT_REF; // 2907

//static const u16 reportRefUUID = GATT_UUID_REPORT_REF; // 2908 Report Reference
//static const u16 reportRefUUID = GATT_UUID_REPORT_REF; // 2908

//static const u16 characterPresentFormatUUID = GATT_UUID_CHAR_PRESENT_FORMAT; // 2904 Characteristic Present Format
static const u16 characterPresentFormatUUID = GATT_UUID_CHAR_PRESENT_FORMAT; // 2904

static const u16 userdesc_UUID = GATT_UUID_CHAR_USER_DESC; // 2901

static const u16 serviceChangeUUID = GATT_UUID_SERVICE_CHANGE; // 2a05
static const u16 serviceChangeUUID = GATT_UUID_SERVICE_CHANGE; // 2a05 https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.gatt.service_changed.xml

static const u16 my_primaryServiceUUID = GATT_UUID_PRIMARY_SERVICE; // 2800

static const u16 my_characterUUID = GATT_UUID_CHARACTER; // 2803

static const u16 my_devServiceUUID = SERVICE_UUID_DEVICE_INFORMATION;
static const u16 my_devServiceUUID = SERVICE_UUID_DEVICE_INFORMATION; // 180A

static const u16 my_PnPUUID = CHARACTERISTIC_UUID_PNP_ID;
//static const u16 my_PnPUUID = CHARACTERISTIC_UUID_PNP_ID; // 2A50

static const u16 my_devNameUUID = GATT_UUID_DEVICE_NAME;
static const u16 my_devNameUUID = GATT_UUID_DEVICE_NAME; // 2A00

static const u16 my_gapServiceUUID = SERVICE_UUID_GENERIC_ACCESS;
static const u16 my_gapServiceUUID = SERVICE_UUID_GENERIC_ACCESS; // 1800

static const u16 my_appearanceUIID = GATT_UUID_APPEARANCE;
static const u16 my_appearanceUIID = GATT_UUID_APPEARANCE; // 2A01

static const u16 my_periConnParamUUID = GATT_UUID_PERI_CONN_PARAM;
static const u16 my_periConnParamUUID = GATT_UUID_PERI_CONN_PARAM; // 2A04

// https://github.com/sputnikdev/bluetooth-gatt-parser/blob/master/src/main/resources/gatt/characteristic/org.bluetooth.characteristic.gap.appearance.xml
static const u16 my_appearance = 768; // value="Generic Thermometer"

static const u16 my_gattServiceUUID = SERVICE_UUID_GENERIC_ATTRIBUTE;
static const u16 my_gattServiceUUID = SERVICE_UUID_GENERIC_ATTRIBUTE; // 1801

#if USE_DEVICE_INFO_CHR_UUID

Expand Down Expand Up @@ -93,7 +93,7 @@ static const u8 my_SoftStr[] = {'V','0'+(VERSION>>4),'.','0'+(VERSION&0x0f)}; //
static const u8 my_ManStr[] = {"miaomiaoce.com"};
#elif DEVICE_TYPE == DEVICE_LYWSD03MMC
static const u8 my_ModelStr[] = {"LYWSD03MMC"};
static const u8 my_SerialStr[] = {"F1.0-CFMK-LB-ZCXTJ--"};
static const u8 my_SerialStr[] = {"F1.0-CFMK-LB-ZCXTJ--"}; // B1.5 "F2.0-CFMK-LB-JHBD---", B1.9 "F1.0-CFMK-LB-FLD----"
static const u8 my_FirmStr[] = {"github.com/pvvx"}; // "1.0.0_0109"
RAM u8 my_HardStr[4];// = {"B1.4"};
static const u8 my_SoftStr[] = {'V','0'+(VERSION>>4),'.','0'+(VERSION&0x0f)}; // "0109"
Expand Down Expand Up @@ -123,9 +123,9 @@ static const u8 my_ManStr[] = {"Qingping Technology (Beijing) Co., Ltd."};
//------------------
#endif // USE_DEVICE_INFO_CHR_UUID

RAM gap_periConnectParams_t my_periConnParameters = {8, 40, 0, 1000};
RAM gap_periConnectParams_t my_periConnParameters = {CON_INERVAL_LAT, CON_INERVAL_LAT, 0, 8000};

static u16 serviceChangeVal = 0;
static u32 serviceChangeVal = 0; // uint16 1..65535 "Start of Affected Attribute Handle Range", uint16 1..65535 "End of Affected Attribute Handle Range"
static u16 serviceChangeCCC = 0;

//////////////////////// Battery /////////////////////////////////////////////////
Expand Down Expand Up @@ -172,7 +172,11 @@ static const u8 my_appearanceCharVal[5] = {
U16_LO(GATT_UUID_APPEARANCE), U16_HI(GATT_UUID_APPEARANCE)
};
static const u8 my_periConnParamCharVal[5] = {
#ifdef CHG_CONN_PARAM
CHAR_PROP_READ | CHAR_PROP_WRITE,
#else
CHAR_PROP_READ,
#endif
U16_LO(CONN_PARAM_DP_H), U16_HI(CONN_PARAM_DP_H),
U16_LO(GATT_UUID_PERI_CONN_PARAM), U16_HI(GATT_UUID_PERI_CONN_PARAM)
};
Expand Down Expand Up @@ -321,7 +325,11 @@ RAM attribute_t my_Attributes[] = {
{0,ATT_PERMISSIONS_READ,2,sizeof(my_appearanceCharVal),(u8*)(&my_characterUUID),(u8*)(my_appearanceCharVal), 0},
{0,ATT_PERMISSIONS_READ,2,sizeof(my_appearance),(u8*)(&my_appearanceUIID),(u8*)(&my_appearance), 0},
{0,ATT_PERMISSIONS_READ,2,sizeof(my_periConnParamCharVal),(u8*)(&my_characterUUID),(u8*)(my_periConnParamCharVal), 0},
#ifdef CHG_CONN_PARAM
{0,ATT_PERMISSIONS_RDWR,2,sizeof(my_periConnParameters),(u8*)(&my_periConnParamUUID),(u8*)(&my_periConnParameters), 0, chgConnParameters},
#else
{0,ATT_PERMISSIONS_READ,2,sizeof(my_periConnParameters),(u8*)(&my_periConnParamUUID),(u8*)(&my_periConnParameters), 0},
#endif
// 0008 - 000b gatt
{4,ATT_PERMISSIONS_READ,2,2,(u8*)(&my_primaryServiceUUID), (u8*)(&my_gattServiceUUID), 0},
{0,ATT_PERMISSIONS_READ,2,sizeof(my_serviceChangeCharVal),(u8*)(&my_characterUUID),(u8*)(my_serviceChangeCharVal), 0},
Expand Down Expand Up @@ -374,7 +382,7 @@ RAM attribute_t my_Attributes[] = {
{4,ATT_PERMISSIONS_READ, 2,16,(u8*)(&my_primaryServiceUUID),(u8*)(&my_OtaServiceUUID), 0},
{0,ATT_PERMISSIONS_READ,2,sizeof(my_OtaCharVal),(u8*)(&my_characterUUID),(u8*)(my_OtaCharVal), 0}, //prop
{0,ATT_PERMISSIONS_RDWR,16,sizeof(my_OtaData),(u8*)(&my_OtaUUID),(&my_OtaData), &otaWritePre, &otaRead}, //value
{0,ATT_PERMISSIONS_READ,2,sizeof (my_OtaName),(u8*)(&userdesc_UUID),(u8*)(my_OtaName), 0},
{0,ATT_PERMISSIONS_READ,2,sizeof(my_OtaName),(u8*)(&userdesc_UUID),(u8*)(my_OtaName), 0},
////////////////////////////////////// RxTx ////////////////////////////////////////////////////
// 002B - 002E RxTx Communication
{4,ATT_PERMISSIONS_READ,2,2,(u8*)(&my_primaryServiceUUID), (u8*)(&my_RxTx_ServiceUUID), 0},
Expand All @@ -390,33 +398,33 @@ RAM attribute_t my_Attributes[] = {

{0,ATT_PERMISSIONS_AUTHOR_READ, 2, 1,(u8*)(&my_characterUUID), (u8*)(&mi_auth_prop), 0}, //prop
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_auth_buf),(u8*)(&mi_auth_uuid), (u8*)(mi_auth_buf), 0, 0}, //value
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof (mi_auth_str),(u8*)(&userdesc_UUID), (u8*)(mi_auth_str), 0}, // Authentication
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof(mi_auth_str),(u8*)(&userdesc_UUID), (u8*)(mi_auth_str), 0}, // Authentication
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_auth_ccc),(u8*)(&clientCharacterCfgUUID), (u8*)(&mi_auth_ccc), 0}, //value

{0,ATT_PERMISSIONS_AUTHOR_READ, 2, 1,(u8*)(&my_characterUUID), (u8*)(&mi_ota_ctrl_prop), 0}, //prop
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_ota_ctrl_buf),(u8*)(&mi_ota_ctrl_uuid), (u8*)(mi_ota_ctrl_buf), 0, 0}, //value
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof (mi_ota_ctrl_str),(u8*)(&userdesc_UUID), (u8*)(mi_ota_ctrl_str), 0}, // ota_ctrl
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof(mi_ota_ctrl_str),(u8*)(&userdesc_UUID), (u8*)(mi_ota_ctrl_str), 0}, // ota_ctrl
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_ota_ctrl_ccc),(u8*)(&clientCharacterCfgUUID), (u8*)(mi_ota_ctrl_ccc), 0}, //value

{0,ATT_PERMISSIONS_AUTHOR_READ, 2, 1,(u8*)(&my_characterUUID), (u8*)(&mi_ota_data_prop), 0}, //prop
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_ota_data_buf),(u8*)(&mi_ota_data_uuid), (mi_ota_data_buf), 0, 0}, //value
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof (mi_ota_data_str),(u8*)(&userdesc_UUID), (u8*)(mi_ota_data_str), 0}, // ota_data
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof(mi_ota_data_str),(u8*)(&userdesc_UUID), (u8*)(mi_ota_data_str), 0}, // ota_data
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_ota_data_ccc),(u8*)(&clientCharacterCfgUUID), (u8*)(mi_ota_data_ccc), 0}, //value

{0,ATT_PERMISSIONS_AUTHOR_READ, 2, 1,(u8*)(&my_characterUUID), (u8*)(&mi_standard_prop), 0}, //prop
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_standard_buf),(u8*)(&mi_standard_uuid), (u8*)(mi_standard_buf), 0, 0}, //value
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof (mi_standard_str),(u8*)(&userdesc_UUID), (u8*)(mi_standard_str), 0}, // standard
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof(mi_standard_str),(u8*)(&userdesc_UUID), (u8*)(mi_standard_str), 0}, // standard
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_standard_ccc),(u8*)(&clientCharacterCfgUUID), (u8*)(mi_standard_ccc), 0}, //value
/////////////////////////////////// Mi STDIO Service ///////////////////////////////////
{9,ATT_PERMISSIONS_AUTHOR_READ, 2,16,(u8*)(&my_primaryServiceUUID), (u8*)(&mi_primary_stdio_uuid), 0}, // 0x6D,0x69,0x2E,0x6D,0x69,0x6F,0x74,0x2E,0x62,0x6C,0x65,0x00,0x00,0x01,0x00,0x00
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, 1,(u8*)(&my_characterUUID), (u8*)(&mi_stdio_rx_prop), 0}, //prop
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 16,sizeof(mi_stdio_rx_buf),(u8*)(&mi_stdio_rx_uuid), (u8*)(mi_stdio_rx_buf), 0, 0}, //value 0x6D,0x69,0x2E,0x6D,0x69,0x6F,0x74,0x2E,0x62,0x6C,0x65,0x00,0x01,0x01,0x00,0x00
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof (mi_stdio_rx_str),(u8*)(&userdesc_UUID), (u8*)(mi_stdio_rx_str), 0}, // STDIO_RX
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof(mi_stdio_rx_str),(u8*)(&userdesc_UUID), (u8*)(mi_stdio_rx_str), 0}, // STDIO_RX
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_stdio_rx_ccc),(u8*)(&clientCharacterCfgUUID), (u8*)(mi_stdio_rx_ccc), 0}, //value

{0,ATT_PERMISSIONS_AUTHOR_READ, 2, 1,(u8*)(&my_characterUUID), (u8*)(&mi_stdio_tx_prop), 0}, //prop
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 16, sizeof(mi_stdio_tx_buf),(u8*)(&mi_stdio_tx_uuid), (u8*)(mi_stdio_tx_buf), 0, 0}, //value 0x6D,0x69,0x2E,0x6D,0x69,0x6F,0x74,0x2E,0x62,0x6C,0x65,0x00,0x02,0x01,0x00,0x00
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof (mi_stdio_tx_str),(u8*)(&userdesc_UUID), (u8*)(mi_stdio_tx_str), 0}, // STDIO_TX
{0,ATT_PERMISSIONS_AUTHOR_READ, 2, sizeof(mi_stdio_tx_str),(u8*)(&userdesc_UUID), (u8*)(mi_stdio_tx_str), 0}, // STDIO_TX
{0,ATT_PERMISSIONS_AUTHOR_RDWR, 2, sizeof(mi_stdio_tx_ccc),(u8*)(&clientCharacterCfgUUID), (u8*)(mi_stdio_tx_ccc), 0}, //value
#else // USE_MIHOME_SERVICE
//Mi 0x95FE
Expand Down
12 changes: 10 additions & 2 deletions src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ extern "C" {
#define DEVICE_LYWSD03MMC 0x055B // LCD display LYWSD03MMC
#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_CGG1_ver 2022 // =2022 - CGG1-M version 2022 or = 0 - CGG1-M version 2020,2021
#define DEVICE_CGG1_ver 2022 // =2022 - CGG1-M version 2022, or = 0 - CGG1-M version 2020,2021
#define DEVICE_CGDK2 0x066F // LCD display "Qingping Temp & RH Monitor Lite"

#ifndef DEVICE_TYPE
#define DEVICE_TYPE DEVICE_LYWSD03MMC // DEVICE_LYWSD03MMC or DEVICE_MHO_C401 or DEVICE_CGG1 (+ set DEVICE_CGG1_ver) or DEVICE_CGDK2
#endif

#define BLE_SECURITY_ENABLE 1 // = 1 support pin-code
#define BLE_EXT_ADV 0 // = 1 support extension advertise (Test Only!)

#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_TRIGGER_OUT 1 // = 1 use trigger out (GPIO_PA5)
#if BLE_EXT_ADV
#define USE_WK_RDS_COUNTER 0 // пока не поддерживается в ext_adv
#else
#define USE_WK_RDS_COUNTER USE_TRIGGER_OUT // = 1 wake up when the reed switch is triggered + pulse counter
#endif


#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
Expand Down Expand Up @@ -390,7 +396,7 @@ extern "C" {
#endif // DEVICE_TYPE == ?

#if UART_PRINT_DEBUG_ENABLE
#define PRINT_BAUD_RATE 1500000 // real 1000000
#define PRINT_BAUD_RATE 1500000 // real ~1000000
#define DEBUG_INFO_TX_PIN GPIO_PA7 // SWS
#define PA7_DATA_OUT 1
#define PA7_OUTPUT_ENABLE 1
Expand Down Expand Up @@ -437,6 +443,8 @@ enum{

#define BLE_HOST_SMP_ENABLE BLE_SECURITY_ENABLE

//#define CHG_CONN_PARAM // test

#include "vendor/common/default_config.h"

#if defined(__cplusplus)
Expand Down
Loading

0 comments on commit 4b7665a

Please sign in to comment.