2026-01-06 09:48:38 +08:00
|
|
|
#include "common/percent.h"
|
|
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
2026-05-28 15:58:08 +08:00
|
|
|
#include "common/strutil.h"
|
2023-03-13 17:36:48 +08:00
|
|
|
#include "detection/bluetooth/bluetooth.h"
|
|
|
|
|
#include "modules/bluetooth/bluetooth.h"
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* device, uint8_t index) {
|
2024-11-14 15:33:43 +08:00
|
|
|
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0) {
|
2026-08-14 23:33:54 +08:00
|
|
|
ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Bluetooth), index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2024-11-14 13:52:02 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
|
|
|
|
|
bool showBatteryLevel = device->battery > 0 && device->battery <= 100;
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (showBatteryLevel && (percentType & FF_PERCENTAGE_TYPE_BAR_BIT)) {
|
2024-11-14 13:52:02 +08:00
|
|
|
ffPercentAppendBar(&buffer, device->battery, options->percent, &options->moduleArgs);
|
|
|
|
|
ffStrbufAppendC(&buffer, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) {
|
2024-11-14 13:52:02 +08:00
|
|
|
ffStrbufAppend(&buffer, &device->name);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-11-14 13:52:02 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (showBatteryLevel && (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)) {
|
|
|
|
|
if (buffer.length) {
|
2024-01-20 13:32:17 +08:00
|
|
|
ffStrbufAppendC(&buffer, ' ');
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (!device->connected) {
|
2024-01-20 13:32:17 +08:00
|
|
|
ffStrbufAppendS(&buffer, " [disconnected]");
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-01-21 23:00:23 +08:00
|
|
|
|
|
|
|
|
ffStrbufPutTo(&buffer, stdout);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2024-07-08 15:21:12 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY percentageNum = ffStrbufCreate();
|
2026-03-29 09:28:49 +08:00
|
|
|
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
|
2024-11-14 15:33:43 +08:00
|
|
|
ffPercentAppendNum(&percentageNum, device->battery, options->percent, false, &options->moduleArgs);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-07-08 15:21:12 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY percentageBar = ffStrbufCreate();
|
2026-03-29 09:28:49 +08:00
|
|
|
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
|
2024-11-14 15:33:43 +08:00
|
|
|
ffPercentAppendBar(&percentageBar, device->battery, options->percent, &options->moduleArgs);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-01-20 13:32:17 +08:00
|
|
|
|
2026-08-14 23:33:54 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Bluetooth), index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_ARG(device->name, "name"),
|
|
|
|
|
FF_ARG(device->address, "address"),
|
|
|
|
|
FF_ARG(device->type, "type"),
|
|
|
|
|
FF_ARG(percentageNum, "battery-percentage"),
|
|
|
|
|
FF_ARG(device->connected, "connected"),
|
|
|
|
|
FF_ARG(percentageBar, "battery-percentage-bar"),
|
|
|
|
|
}));
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
bool ffPrintBluetooth(FFBluetoothOptions* options) {
|
2026-04-14 10:41:18 +08:00
|
|
|
FF_LIST_AUTO_DESTROY devices = ffListCreate();
|
2024-12-20 11:07:09 +08:00
|
|
|
const char* error = ffDetectBluetooth(options, &devices);
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (error) {
|
2026-08-14 23:33:54 +08:00
|
|
|
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Bluetooth), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 10:41:18 +08:00
|
|
|
if (devices.length == 0) {
|
2026-08-14 23:33:54 +08:00
|
|
|
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Bluetooth), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No bluetooth devices found");
|
2026-06-05 22:18:53 +08:00
|
|
|
return false;
|
2025-08-18 13:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 10:41:18 +08:00
|
|
|
uint8_t i = 1;
|
|
|
|
|
FF_LIST_FOR_EACH (FFBluetoothResult, device, devices) {
|
|
|
|
|
printDevice(options, device, devices.length == 0 ? 0 : i);
|
|
|
|
|
++i;
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
2023-06-12 21:50:25 +08:00
|
|
|
|
2026-03-29 09:28:49 +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);
|
|
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
return true;
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffParseBluetoothJsonObject(FFBluetoothOptions* options, yyjson_val* module) {
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2023-08-17 16:09:41 +08:00
|
|
|
size_t idx, max;
|
2026-03-29 09:28:49 +08:00
|
|
|
yyjson_obj_foreach (module, idx, max, key, val) {
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-03-13 17:36:48 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (unsafe_yyjson_equals_str(key, "showDisconnected")) {
|
2023-08-17 16:09:41 +08:00
|
|
|
options->showDisconnected = yyjson_get_bool(val);
|
|
|
|
|
continue;
|
2023-03-15 15:24:40 +08:00
|
|
|
}
|
2023-08-17 16:09:41 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (ffPercentParseJsonObject(key, val, &options->percent)) {
|
2024-01-20 18:40:12 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-01-20 18:40:12 +08:00
|
|
|
|
2026-08-14 23:33:54 +08:00
|
|
|
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Bluetooth), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2023-03-13 17:36:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-30 11:21:57 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffGenerateBluetoothJsonConfig(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2023-10-23 16:34:33 +08:00
|
|
|
|
2025-08-04 16:46:28 +08:00
|
|
|
yyjson_mut_obj_add_bool(doc, module, "showDisconnected", options->showDisconnected);
|
2023-10-23 16:34:33 +08:00
|
|
|
|
2025-08-04 16:46:28 +08:00
|
|
|
ffPercentGenerateJsonConfig(doc, module, options->percent);
|
2023-10-23 16:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
bool ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2026-04-14 10:41:18 +08:00
|
|
|
FF_LIST_AUTO_DESTROY results = ffListCreate();
|
2023-08-30 11:21:57 +08:00
|
|
|
|
2024-12-20 11:07:09 +08:00
|
|
|
const char* error = ffDetectBluetooth(options, &results);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (error) {
|
2023-08-30 11:21:57 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2023-08-30 11:21:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-03 13:56:19 +08:00
|
|
|
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_LIST_FOR_EACH (FFBluetoothResult, item, results) {
|
2024-05-03 13:56:19 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +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);
|
|
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
return true;
|
2023-08-30 11:21:57 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffInitBluetoothOptions(FFBluetoothOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
|
|
|
|
options->showDisconnected = false;
|
2026-03-30 10:27:23 +08:00
|
|
|
options->percent = (FFPercentageModuleConfig) { 50, 20, 0 };
|
2025-08-04 14:25:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffDestroyBluetoothOptions(FFBluetoothOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffBluetoothModuleInfo = {
|
2026-08-14 23:33:54 +08:00
|
|
|
.name = "Bluetooth",
|
2026-04-16 18:27:11 +08:00
|
|
|
.description = "List connected Bluetooth devices",
|
2026-08-14 20:38:58 +08:00
|
|
|
.displayName = {
|
|
|
|
|
.en = "Bluetooth",
|
|
|
|
|
.de = "Bluetooth",
|
|
|
|
|
.es = "Bluetooth",
|
|
|
|
|
.fr = "Bluetooth",
|
|
|
|
|
.it = "Bluetooth",
|
|
|
|
|
.ja = "ブルートゥース",
|
|
|
|
|
.ko = "블루투스",
|
|
|
|
|
.pl = "Bluetooth",
|
2026-08-16 10:37:06 +08:00
|
|
|
.pt = "Bluetooth",
|
2026-08-16 10:39:10 +08:00
|
|
|
.ru = "Блютуз",
|
2026-08-14 20:38:58 +08:00
|
|
|
.zh_CN = "蓝牙",
|
|
|
|
|
.zh_TW = "藍牙",
|
|
|
|
|
},
|
2025-08-03 18:39:41 +08:00
|
|
|
.initOptions = (void*) ffInitBluetoothOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyBluetoothOptions,
|
2024-12-11 10:47:02 +08:00
|
|
|
.parseJsonObject = (void*) ffParseBluetoothJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintBluetooth,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateBluetoothJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateBluetoothJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
2026-03-30 10:27:23 +08:00
|
|
|
{ "Name", "name" },
|
|
|
|
|
{ "Address", "address" },
|
|
|
|
|
{ "Type", "type" },
|
|
|
|
|
{ "Battery percentage number", "battery-percentage" },
|
|
|
|
|
{ "Is connected", "connected" },
|
|
|
|
|
{ "Battery percentage bar", "battery-percentage-bar" },
|
2026-07-31 10:56:13 +08:00
|
|
|
})),
|
|
|
|
|
.defaultOrder = 58,
|
2026-03-30 10:17:45 +08:00
|
|
|
};
|