From 4c9a89463d671941629d4249c95f37c96f1ab851 Mon Sep 17 00:00:00 2001 From: jhchun Date: Fri, 10 Jul 2026 17:14:49 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A0=84=EC=9B=90=20=EB=B2=84=ED=8A=BC=20?= =?UTF-8?q?=EB=A1=B1=ED=94=84=EB=A0=88=EC=8A=A4=20=EB=B3=B8=EB=93=9C=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 전원 켜진 상태에서 15초 이상 전원 버튼 누르고 있는 경우 본드 삭제 - system workqueue 사용 --- src/main.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 56828ea..774a5cb 100644 --- a/src/main.c +++ b/src/main.c @@ -6,9 +6,14 @@ #include #include #include +#include #include #include #include +#if IS_ENABLED(CONFIG_BT_SMP) +#include +#include +#endif #include "main.h" #include "debug_print.h" @@ -43,6 +48,7 @@ LOG_MODULE_REGISTER(vesiscan, LOG_LEVEL_INF); /* 전원 버튼 상태머신 (5ms 폴링) */ #define BOOT_THRESHOLD 300 // 5ms x 400 = 2초 -> 초기화 시간 고려 체감상 2초에 맞춤 +#define BOND_RESET_THRESHOLD (15000 / POWER_ON_DELAY) /* 전원 제어 GPIO */ static const struct gpio_dt_spec power_hold = GPIO_DT_SPEC_GET(POWER_HOLD_NODE, gpios); @@ -78,8 +84,10 @@ static bool dfu_reset_resume_request; // DFU 완료 리셋 후 자동 복 static uint32_t boot_reset_reason; // RESETREAS 원본 값 static struct k_work adv_start_work; // advertising 시작 work static struct k_work adv_stop_work; // advertising 중지 work +static struct k_work bond_reset_work; // 15s power-button bond reset work static bool dfu_led_active; // DFU 업로드 중 초록 LED ON static const char *power_off_pending_reason = "none"; +static bool bond_reset_pending; static enum mgmt_cb_return dfu_status_cb(uint32_t event, enum mgmt_cb_return prev_status, @@ -170,6 +178,34 @@ static void adv_stop_work_handler(struct k_work *work) ble_advertising_stop(); } +static void bond_reset_work_handler(struct k_work *work) +{ + ARG_UNUSED(work); + + DBG_ERR("[BTN] 15s long press -> BLE bond reset\r\n"); + battery_timer_stop(); + ble_advertising_stop(); + +#if IS_ENABLED(CONFIG_BT_SMP) + int err = bt_unpair(BT_ID_DEFAULT, NULL); + if (err) + { + DBG_ERR("[BTN] bt_unpair failed err=%d\r\n", err); + } + else + { + bond_data_delete = true; + DBG_PRINTF("[BTN] BLE bond data deleted\r\n"); + } +#else + bond_data_delete = true; + DBG_PRINTF("[BTN] BLE bond delete skipped (BT_SMP disabled)\r\n"); +#endif + + k_msleep(100); + sys_reboot(SYS_REBOOT_COLD); +} + /* BLE RX 콜백 */ static void ble_rx_handler(const uint8_t *data, uint16_t len) { @@ -413,7 +449,6 @@ static void main_s(struct k_timer *timer) if (cnt_s == BOOT_THRESHOLD) // 2초 도달: 래치 + 부팅 완료 { device_on = true; - cnt_s = 0; // 카운터 리셋: 안 하면 다음 틱에서 ON→OFF 분기가 cnt_s >= 200 조건을 즉시 만족하여 전원 OFF됨 power_control_handler(ON, "button-2s-latch"); led_set_state(LED_STATE_ADVERTISING); k_work_submit(&adv_start_work); @@ -425,18 +460,39 @@ static void main_s(struct k_timer *timer) /* 전원 OFF 시퀀스 (ON → OFF) */ else { - if (!boot_btn_released) // 부팅 시 눌렀던 버튼을 아직 안 놓음 → 대기 + if (!boot_btn_released) // 부팅 때 누른 버튼이 아직 안 놓였으면 대기 { if (!button_pressed) { boot_btn_released = true; + cnt_s = 0; + } + else + { + cnt_s++; + + if ((cnt_s >= BOND_RESET_THRESHOLD) && !bond_reset_pending) + { + bond_reset_pending = true; + k_work_submit(&bond_reset_work); + return; + } } } else if (button_pressed) // 버튼 새로 누르고 있음 { cnt_s++; - if (cnt_s >= BOOT_THRESHOLD) // 2초 이상 → 전원 OFF + if ((cnt_s >= BOND_RESET_THRESHOLD) && !bond_reset_pending) + { + bond_reset_pending = true; + k_work_submit(&bond_reset_work); + return; + } + } + else // 버튼 놓음 -> 카운터 리셋 + { + if (cnt_s >= BOOT_THRESHOLD) { battery_timer_stop(); k_work_submit(&adv_stop_work);