Skip to content

Commit a8f170d

Browse files
committed
lib: location: Fix Wi-Fi scan fallback mechanism
If location is cancelled during Wi-Fi search, the semaphore blocking the location positioning workqueue is not released due to k_sem_reset() being used instead of k_sem_give(). This prevents subsequent location requests to start Wi-Fi scans. This commit changes k_sem_reset() to k_sem_give() and adds a test for this scenario. Signed-off-by: Simen S. Røstad <[email protected]>
1 parent ba0efe3 commit a8f170d

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/location/scan_wifi.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ void scan_wifi_execute(int32_t timeout, struct k_sem *wifi_scan_ready)
112112
#if defined(CONFIG_LOCATION_METHOD_WIFI_NET_IF_UPDOWN)
113113
ret = scan_wifi_startup_interface(wifi_iface);
114114
if (ret) {
115+
LOG_ERR("Failed to start Wi-Fi interface: %d", ret);
116+
k_sem_give(scan_wifi_ready);
117+
scan_wifi_ready = NULL;
115118
return;
116119
}
117120
#endif /* defined(CONFIG_LOCATION_METHOD_WIFI_NET_IF_UPDOWN) */
@@ -203,7 +206,7 @@ void scan_wifi_net_mgmt_event_handler(
203206
int scan_wifi_cancel(void)
204207
{
205208
if (scan_wifi_ready != NULL) {
206-
k_sem_reset(scan_wifi_ready);
209+
k_sem_give(scan_wifi_ready);
207210
scan_wifi_ready = NULL;
208211
}
209212
return 0;

tests/lib/location/src/location_test.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,59 @@ void test_location_wifi_timeout(void)
14351435
#endif
14361436
}
14371437

1438+
/* Test Wi-Fi location cancellation during scan to verify wifi scan semaphore is properly
1439+
* released and Wi-Fi scan can be restarted.
1440+
*/
1441+
void test_location_wifi_cancel_releases_semaphore(void)
1442+
{
1443+
#if defined(CONFIG_LOCATION_METHOD_WIFI)
1444+
int err;
1445+
struct location_config config = { 0 };
1446+
enum location_method methods[] = {LOCATION_METHOD_WIFI};
1447+
1448+
location_config_defaults_set(&config, 1, methods);
1449+
config.methods[0].wifi.timeout = 10000;
1450+
1451+
#if defined(CONFIG_LOCATION_DATA_DETAILS)
1452+
test_location_event_data[location_cb_expected].id = LOCATION_EVT_STARTED;
1453+
test_location_event_data[location_cb_expected].method = LOCATION_METHOD_WIFI;
1454+
location_cb_expected++;
1455+
#endif
1456+
1457+
net_mgmt_NET_REQUEST_WIFI_SCAN_expected = true;
1458+
1459+
__cmock_net_mgmt_NET_REQUEST_WIFI_SCAN_ExpectAndReturn(0);
1460+
1461+
err = location_request(&config);
1462+
TEST_ASSERT_EQUAL(0, err);
1463+
k_sleep(K_MSEC(10));
1464+
1465+
#if defined(CONFIG_LOCATION_DATA_DETAILS)
1466+
/* Wait for LOCATION_EVT_STARTED */
1467+
err = k_sem_take(&event_handler_called_sem, K_SECONDS(3));
1468+
TEST_ASSERT_EQUAL(0, err);
1469+
#endif
1470+
1471+
err = location_request_cancel();
1472+
TEST_ASSERT_EQUAL(0, err);
1473+
1474+
/* Next time location is requested, the Wi-Fi scan semaphore should be available again
1475+
* and scan can be started again.
1476+
*/
1477+
__cmock_net_mgmt_NET_REQUEST_WIFI_SCAN_ExpectAndReturn(0);
1478+
1479+
err = location_request(&config);
1480+
TEST_ASSERT_EQUAL(0, err);
1481+
k_sleep(K_MSEC(10));
1482+
1483+
#if defined(CONFIG_LOCATION_DATA_DETAILS)
1484+
/* Wait for LOCATION_EVT_STARTED */
1485+
err = k_sem_take(&event_handler_called_sem, K_SECONDS(3));
1486+
TEST_ASSERT_EQUAL(0, err);
1487+
#endif
1488+
#endif /* defined(CONFIG_LOCATION_METHOD_WIFI) */
1489+
}
1490+
14381491
/********* GENERAL ERROR TESTS ***********************/
14391492

14401493
/* Test location request with unknown method. */

0 commit comments

Comments
 (0)