2024-01-20 13:32:17 +08:00
|
|
|
#include "common/percent.h"
|
2023-03-13 17:36:48 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-03-13 17:36:48 +08:00
|
|
|
#include "detection/bluetooth/bluetooth.h"
|
|
|
|
|
#include "modules/bluetooth/bluetooth.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2024-07-08 15:21:12 +08:00
|
|
|
#define FF_BLUETOOTH_NUM_FORMAT_ARGS 6
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2023-08-30 13:13:11 +08:00
|
|
|
static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* device, uint8_t index)
|
2023-03-13 17:36:48 +08:00
|
|
|
{
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2024-01-20 13:32:17 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateCopy(&device->name);
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2024-01-20 13:32:17 +08:00
|
|
|
if (device->battery > 0 && device->battery <= 100)
|
|
|
|
|
{
|
|
|
|
|
if (buffer.length)
|
|
|
|
|
ffStrbufAppendC(&buffer, ' ');
|
2024-05-08 15:25:59 +08:00
|
|
|
ffPercentAppendNum(&buffer, device->battery, options->percent, buffer.length > 0, &options->moduleArgs);
|
2024-01-20 13:32:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!device->connected)
|
|
|
|
|
ffStrbufAppendS(&buffer, " [disconnected]");
|
2024-01-21 23:00:23 +08:00
|
|
|
|
|
|
|
|
ffStrbufPutTo(&buffer, stdout);
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-08 15:21:12 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY percentageNum = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendNum(&percentageNum, device->battery, options->percent, false, &options->moduleArgs);
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY percentageBar = ffStrbufCreate();
|
|
|
|
|
ffPercentAppendBar(&percentageBar, device->battery, options->percent, &options->moduleArgs);
|
2024-01-20 13:32:17 +08:00
|
|
|
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_BLUETOOTH_NUM_FORMAT_ARGS, ((FFformatarg[]) {
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &device->name, "name"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &device->address, "address"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &device->type, "type"},
|
2024-07-08 15:21:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &percentageNum, "battery-percentage"},
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_BOOL, &device->connected, "connected"},
|
2024-07-08 15:21:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &percentageBar, "battery-percentage-bar"},
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintBluetooth(FFBluetoothOptions* options)
|
2023-03-13 17:36:48 +08:00
|
|
|
{
|
2023-08-30 13:13:11 +08:00
|
|
|
FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof (FFBluetoothResult));
|
2023-06-24 10:59:53 +08:00
|
|
|
const char* error = ffDetectBluetooth(&devices);
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2023-06-12 21:50:25 +08:00
|
|
|
if(error)
|
2023-03-13 17:36:48 +08:00
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
2023-06-24 15:58:37 +08:00
|
|
|
else
|
2023-03-13 17:36:48 +08:00
|
|
|
{
|
2023-08-30 13:13:11 +08:00
|
|
|
FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothResult*));
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2023-08-30 13:13:11 +08:00
|
|
|
FF_LIST_FOR_EACH(FFBluetoothResult, device, devices)
|
2023-06-24 15:58:37 +08:00
|
|
|
{
|
|
|
|
|
if(!device->connected && !options->showDisconnected)
|
|
|
|
|
continue;
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2023-08-30 13:13:11 +08:00
|
|
|
*(FFBluetoothResult**)ffListAdd(&filtered) = device;
|
2023-06-24 15:58:37 +08:00
|
|
|
}
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2023-06-24 15:58:37 +08:00
|
|
|
if(filtered.length == 0)
|
|
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No bluetooth devices found");
|
2023-06-24 15:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < filtered.length; i++)
|
|
|
|
|
{
|
|
|
|
|
uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1);
|
2023-08-30 13:13:11 +08:00
|
|
|
printDevice(options, *(FFBluetoothResult**)ffListGet(&filtered, i), index);
|
2023-06-24 15:58:37 +08:00
|
|
|
}
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
2023-06-12 21:50:25 +08:00
|
|
|
|
2023-08-30 13:13:11 +08:00
|
|
|
FF_LIST_FOR_EACH(FFBluetoothResult, device, devices)
|
2023-06-12 21:50:25 +08:00
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&device->name);
|
|
|
|
|
ffStrbufDestroy(&device->type);
|
|
|
|
|
ffStrbufDestroy(&device->address);
|
|
|
|
|
}
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseBluetoothCommandOptions(FFBluetoothOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_BLUETOOTH_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "show-disconnected"))
|
2023-06-24 15:58:37 +08:00
|
|
|
{
|
2023-03-13 17:36:48 +08:00
|
|
|
options->showDisconnected = ffOptionParseBoolean(value);
|
2023-06-24 15:58:37 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-20 18:40:12 +08:00
|
|
|
if (ffPercentParseCommandOptions(key, subKey, value, &options->percent))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-03-13 17:36:48 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseBluetoothJsonObject(FFBluetoothOptions* options, yyjson_val* module)
|
2023-03-13 17:36:48 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-03-13 17:36:48 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
2023-06-10 15:01:54 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "showDisconnected"))
|
|
|
|
|
{
|
|
|
|
|
options->showDisconnected = yyjson_get_bool(val);
|
|
|
|
|
continue;
|
2023-03-15 15:24:40 +08:00
|
|
|
}
|
2023-08-17 16:09:41 +08:00
|
|
|
|
2024-01-20 18:40:12 +08:00
|
|
|
if (ffPercentParseJsonObject(key, val, &options->percent))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-30 11:21:57 +08:00
|
|
|
|
2023-10-23 16:34:33 +08:00
|
|
|
void ffGenerateBluetoothJsonConfig(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyBluetoothOptions))) FFBluetoothOptions defaultOptions;
|
|
|
|
|
ffInitBluetoothOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
|
|
|
|
|
if (options->showDisconnected != defaultOptions.showDisconnected)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "showDisconnected", options->showDisconnected);
|
2024-01-20 18:40:12 +08:00
|
|
|
|
|
|
|
|
ffPercentGenerateJsonConfig(doc, module, defaultOptions.percent, options->percent);
|
2023-10-23 16:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGenerateBluetoothJsonResult(FF_MAYBE_UNUSED FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-08-30 11:21:57 +08:00
|
|
|
{
|
2023-08-30 13:13:11 +08:00
|
|
|
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBluetoothResult));
|
2023-08-30 11:21:57 +08:00
|
|
|
|
|
|
|
|
const char* error = ffDetectBluetooth(&results);
|
|
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 13:56:19 +08:00
|
|
|
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFBluetoothResult, item, results)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "address", &item->address);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "battery", item->battery);
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, obj, "connected", item->connected);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "type", &item->type);
|
2023-08-30 11:21:57 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-30 13:13:11 +08:00
|
|
|
FF_LIST_FOR_EACH(FFBluetoothResult, device, results)
|
2023-08-30 11:21:57 +08:00
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&device->name);
|
|
|
|
|
ffStrbufDestroy(&device->type);
|
|
|
|
|
ffStrbufDestroy(&device->address);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
|
|
|
|
void ffPrintBluetoothHelpFormat(void)
|
|
|
|
|
{
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_BLUETOOTH_MODULE_NAME, "{1} ({4})", FF_BLUETOOTH_NUM_FORMAT_ARGS, ((const char* []) {
|
2024-05-27 16:41:37 +08:00
|
|
|
"Name - name",
|
|
|
|
|
"Address - address",
|
|
|
|
|
"Type - type",
|
2024-07-08 15:21:12 +08:00
|
|
|
"Battery percentage number - battery-percentage",
|
2024-05-27 16:41:37 +08:00
|
|
|
"Is connected - connected",
|
2024-07-08 15:21:12 +08:00
|
|
|
"Battery percentage bar - battery-percentage-bar",
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-10-07 13:40:40 +08:00
|
|
|
}
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitBluetoothOptions(FFBluetoothOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_BLUETOOTH_MODULE_NAME,
|
2023-11-17 14:12:17 +08:00
|
|
|
"List bluetooth devices",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParseBluetoothCommandOptions,
|
|
|
|
|
ffParseBluetoothJsonObject,
|
|
|
|
|
ffPrintBluetooth,
|
|
|
|
|
ffGenerateBluetoothJsonResult,
|
|
|
|
|
ffPrintBluetoothHelpFormat,
|
2023-10-23 16:34:33 +08:00
|
|
|
ffGenerateBluetoothJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2023-10-23 13:32:18 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
options->showDisconnected = false;
|
2024-02-26 15:12:04 +08:00
|
|
|
options->percent = (FFColorRangeConfig) { 50, 20 };
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyBluetoothOptions(FFBluetoothOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|