From 1649f2fc4ad61b9002ae7db5eb70dc1a0399c55c Mon Sep 17 00:00:00 2001 From: Trond Einar Snekvik Date: Thu, 6 May 2021 10:08:51 +0200 Subject: [PATCH] samples: bluetooth: iso_receive: Convert to new k_work API Converts the sample to the new k_work API, and introduces a blink boolean to catch cancel failures. Signed-off-by: Trond Einar Snekvik --- samples/bluetooth/iso_receive/src/main.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/samples/bluetooth/iso_receive/src/main.c b/samples/bluetooth/iso_receive/src/main.c index 22b731968a7..3b421b8762b 100644 --- a/samples/bluetooth/iso_receive/src/main.c +++ b/samples/bluetooth/iso_receive/src/main.c @@ -45,16 +45,21 @@ static K_SEM_DEFINE(sem_big_sync_lost, 0, 1); #define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios) #define BLINK_ONOFF K_MSEC(500) -static struct device const *dev; -static struct k_delayed_work blink_work; -static bool led_is_on; +static struct device const *dev; +static struct k_work_delayable blink_work; +static bool led_is_on; +static bool blink; static void blink_timeout(struct k_work *work) { + if (!blink) { + return; + } + led_is_on = !led_is_on; gpio_pin_set(dev, PIN, (int)led_is_on); - k_delayed_work_submit(&blink_work, BLINK_ONOFF); + k_work_schedule(&blink_work, BLINK_ONOFF); } #endif @@ -288,7 +293,7 @@ void main(void) } printk("done.\n"); - k_delayed_work_init(&blink_work, blink_timeout); + k_work_init_delayable(&blink_work, blink_timeout); #endif /* HAS_LED */ /* Initialize the Bluetooth Subsystem */ @@ -320,8 +325,9 @@ void main(void) #if defined(HAS_LED) printk("Start blinking LED...\n"); led_is_on = false; + blink = true; gpio_pin_set(dev, PIN, (int)led_is_on); - k_delayed_work_submit(&blink_work, BLINK_ONOFF); + k_work_reschedule(&blink_work, BLINK_ONOFF); #endif /* HAS_LED */ printk("Waiting for periodic advertising...\n"); @@ -417,7 +423,11 @@ big_sync_create: #if defined(HAS_LED) printk("Stop blinking LED.\n"); - k_delayed_work_cancel(&blink_work); + blink = false; + /* If this fails, we'll exit early in the handler because blink + * is false. + */ + k_work_cancel_delayable(&blink_work); /* Keep LED on */ led_is_on = true;