From b84720c04001991f184dd0129714b07544919c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 24 Jun 2023 15:58:37 +0800 Subject: [PATCH] Bluetooth: improvements 1. fix memleaks 2. fix `show-disconnected` option parsing 3. print `[disconnected]` for disconnected device --- src/modules/bluetooth/bluetooth.c | 45 ++++++++++++++++++------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/modules/bluetooth/bluetooth.c b/src/modules/bluetooth/bluetooth.c index c41bf3da4..bbd5e0e11 100644 --- a/src/modules/bluetooth/bluetooth.c +++ b/src/modules/bluetooth/bluetooth.c @@ -16,7 +16,10 @@ static void printDevice(FFBluetoothOptions* options, const FFBluetoothDevice* de if(device->battery > 0) printf(" (%d%%)", device->battery); - putchar('\n'); + if(!device->connected) + puts(" [disconnected]"); + else + putchar('\n'); } else { @@ -37,29 +40,29 @@ void ffPrintBluetooth(FFBluetoothOptions* options) if(error) { ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, "%s", error); - return; } - - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothDevice*)); - - FF_LIST_FOR_EACH(FFBluetoothDevice, device, devices) + else { - if(!device->connected && !options->showDisconnected) - continue; + FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothDevice*)); - *(FFBluetoothDevice**)ffListAdd(&filtered) = device; - } + FF_LIST_FOR_EACH(FFBluetoothDevice, device, devices) + { + if(!device->connected && !options->showDisconnected) + continue; - if(filtered.length == 0) - { - ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, "No bluetooth devices found"); - return; - } + *(FFBluetoothDevice**)ffListAdd(&filtered) = device; + } - for(uint32_t i = 0; i < filtered.length; i++) - { - uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1); - printDevice(options, *(FFBluetoothDevice**)ffListGet(&filtered, i), index); + if(filtered.length == 0) + { + ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, "No bluetooth devices found"); + } + + for(uint32_t i = 0; i < filtered.length; i++) + { + uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1); + printDevice(options, *(FFBluetoothDevice**)ffListGet(&filtered, i), index); + } } FF_LIST_FOR_EACH(FFBluetoothDevice, device, devices) @@ -85,7 +88,11 @@ bool ffParseBluetoothCommandOptions(FFBluetoothOptions* options, const char* key return true; if (ffStrEqualsIgnCase(subKey, "show-disconnected")) + { options->showDisconnected = ffOptionParseBoolean(value); + return true; + } + return false; }