Skip to content

Commit 3b5e9c7

Browse files
committed
Modules: return bool to indicate whether the detection is succeeded
1 parent a9aaa36 commit 3b5e9c7

File tree

147 files changed

+870
-539
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+870
-539
lines changed

src/common/option.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ typedef struct FFModuleBaseInfo
2929
// This is UB, because `void*` is not compatible with `FF*Options*`.
3030
// However we can't do it better unless we move to C++, so that `option` becomes a `this` pointer
3131
// https://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type
32+
3233
void (*initOptions)(void* options);
3334
void (*destroyOptions)(void* options);
3435
void (*parseJsonObject)(void* options, struct yyjson_val *module);
35-
void (*printModule)(void* options);
36-
void (*generateJsonResult)(void* options, struct yyjson_mut_doc* doc, struct yyjson_mut_val* module);
36+
bool (*printModule)(void* options); // true on success
37+
bool (*generateJsonResult)(void* options, struct yyjson_mut_doc* doc, struct yyjson_mut_val* module); // true on success
3738
void (*generateJsonConfig)(void* options, struct yyjson_mut_doc* doc, struct yyjson_mut_val* obj);
3839
FFModuleFormatArgList formatArgs;
3940
} FFModuleBaseInfo;

src/modules/battery/battery.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
125125
}
126126
}
127127

128-
void ffPrintBattery(FFBatteryOptions* options)
128+
bool ffPrintBattery(FFBatteryOptions* options)
129129
{
130130
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBatteryResult));
131131

@@ -134,12 +134,12 @@ void ffPrintBattery(FFBatteryOptions* options)
134134
if (error)
135135
{
136136
ffPrintError(FF_BATTERY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
137-
return;
137+
return false;
138138
}
139139
if(results.length == 0)
140140
{
141141
ffPrintError(FF_BATTERY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "No batteries found");
142-
return;
142+
return false;
143143
}
144144

145145
for(uint32_t i = 0; i < results.length; i++)
@@ -157,6 +157,7 @@ void ffPrintBattery(FFBatteryOptions* options)
157157
ffStrbufDestroy(&result->serial);
158158
ffStrbufDestroy(&result->manufactureDate);
159159
}
160+
return true;
160161
}
161162

162163
void ffParseBatteryJsonObject(FFBatteryOptions* options, yyjson_val* module)
@@ -198,15 +199,15 @@ void ffGenerateBatteryJsonConfig(FFBatteryOptions* options, yyjson_mut_doc* doc,
198199
ffPercentGenerateJsonConfig(doc, module, options->percent);
199200
}
200201

201-
void ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
202+
bool ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
202203
{
203204
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBatteryResult));
204205

205206
const char* error = ffDetectBattery(options, &results);
206207
if (error)
207208
{
208209
yyjson_mut_obj_add_str(doc, module, "error", error);
209-
return;
210+
return false;
210211
}
211212

212213
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
@@ -241,6 +242,8 @@ void ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc,
241242
ffStrbufDestroy(&battery->status);
242243
ffStrbufDestroy(&battery->serial);
243244
}
245+
246+
return true;
244247
}
245248

246249
void ffInitBatteryOptions(FFBatteryOptions* options)

src/modules/battery/battery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#define FF_BATTERY_MODULE_NAME "Battery"
66

7-
void ffPrintBattery(FFBatteryOptions* options);
7+
bool ffPrintBattery(FFBatteryOptions* options);
88
void ffInitBatteryOptions(FFBatteryOptions* options);
99
void ffDestroyBatteryOptions(FFBatteryOptions* options);
1010

src/modules/bios/bios.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include "modules/bios/bios.h"
55
#include "util/stringUtils.h"
66

7-
void ffPrintBios(FFBiosOptions* options)
7+
bool ffPrintBios(FFBiosOptions* options)
88
{
9+
bool success = false;
910
FFBiosResult bios;
1011
ffStrbufInit(&bios.date);
1112
ffStrbufInit(&bios.release);
@@ -66,13 +67,16 @@ void ffPrintBios(FFBiosOptions* options)
6667
FF_FORMAT_ARG(bios.type, "type"),
6768
}));
6869
}
70+
success = true;
6971

7072
exit:
7173
ffStrbufDestroy(&bios.date);
7274
ffStrbufDestroy(&bios.release);
7375
ffStrbufDestroy(&bios.vendor);
7476
ffStrbufDestroy(&bios.version);
7577
ffStrbufDestroy(&bios.type);
78+
79+
return success;
7680
}
7781

7882
void ffParseBiosJsonObject(FFBiosOptions* options, yyjson_val* module)
@@ -93,8 +97,9 @@ void ffGenerateBiosJsonConfig(FFBiosOptions* options, yyjson_mut_doc* doc, yyjso
9397
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
9498
}
9599

96-
void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
100+
bool ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
97101
{
102+
bool success = false;
98103
FFBiosResult bios;
99104
ffStrbufInit(&bios.date);
100105
ffStrbufInit(&bios.release);
@@ -116,13 +121,15 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
116121
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &bios.vendor);
117122
yyjson_mut_obj_add_strbuf(doc, obj, "version", &bios.version);
118123
yyjson_mut_obj_add_strbuf(doc, obj, "type", &bios.type);
124+
success = true;
119125

120126
exit:
121127
ffStrbufDestroy(&bios.date);
122128
ffStrbufDestroy(&bios.release);
123129
ffStrbufDestroy(&bios.vendor);
124130
ffStrbufDestroy(&bios.version);
125131
ffStrbufDestroy(&bios.type);
132+
return success;
126133
}
127134

128135
void ffInitBiosOptions(FFBiosOptions* options)

src/modules/bios/bios.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#define FF_BIOS_MODULE_NAME "BIOS"
66

7-
void ffPrintBios(FFBiosOptions* options);
7+
bool ffPrintBios(FFBiosOptions* options);
88
void ffInitBiosOptions(FFBiosOptions* options);
99
void ffDestroyBiosOptions(FFBiosOptions* options);
1010

src/modules/bluetooth/bluetooth.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,37 +56,36 @@ static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* de
5656
}
5757
}
5858

59-
void ffPrintBluetooth(FFBluetoothOptions* options)
59+
bool ffPrintBluetooth(FFBluetoothOptions* options)
6060
{
6161
FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof (FFBluetoothResult));
6262
const char* error = ffDetectBluetooth(options, &devices);
6363

6464
if(error)
6565
{
6666
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
67+
return false;
6768
}
68-
else
69-
{
70-
FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothResult*));
7169

72-
FF_LIST_FOR_EACH(FFBluetoothResult, device, devices)
73-
{
74-
if(!device->connected && !options->showDisconnected)
75-
continue;
70+
FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothResult*));
7671

77-
*(FFBluetoothResult**)ffListAdd(&filtered) = device;
78-
}
72+
FF_LIST_FOR_EACH(FFBluetoothResult, device, devices)
73+
{
74+
if(!device->connected && !options->showDisconnected)
75+
continue;
7976

80-
if(filtered.length == 0)
81-
{
82-
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No bluetooth devices found");
83-
}
77+
*(FFBluetoothResult**)ffListAdd(&filtered) = device;
78+
}
8479

85-
for(uint32_t i = 0; i < filtered.length; i++)
86-
{
87-
uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1);
88-
printDevice(options, *FF_LIST_GET(FFBluetoothResult*, filtered, i), index);
89-
}
80+
if(filtered.length == 0)
81+
{
82+
ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No bluetooth devices found");
83+
}
84+
85+
for(uint32_t i = 0; i < filtered.length; i++)
86+
{
87+
uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1);
88+
printDevice(options, *FF_LIST_GET(FFBluetoothResult*, filtered, i), index);
9089
}
9190

9291
FF_LIST_FOR_EACH(FFBluetoothResult, device, devices)
@@ -95,6 +94,7 @@ void ffPrintBluetooth(FFBluetoothOptions* options)
9594
ffStrbufDestroy(&device->type);
9695
ffStrbufDestroy(&device->address);
9796
}
97+
return true;
9898
}
9999

100100
void ffParseBluetoothJsonObject(FFBluetoothOptions* options, yyjson_val* module)
@@ -128,15 +128,15 @@ void ffGenerateBluetoothJsonConfig(FFBluetoothOptions* options, yyjson_mut_doc*
128128
ffPercentGenerateJsonConfig(doc, module, options->percent);
129129
}
130130

131-
void ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
131+
bool ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
132132
{
133133
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBluetoothResult));
134134

135135
const char* error = ffDetectBluetooth(options, &results);
136136
if (error)
137137
{
138138
yyjson_mut_obj_add_str(doc, module, "error", error);
139-
return;
139+
return false;
140140
}
141141

142142
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
@@ -157,6 +157,7 @@ void ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc*
157157
ffStrbufDestroy(&device->type);
158158
ffStrbufDestroy(&device->address);
159159
}
160+
return true;
160161
}
161162

162163
void ffInitBluetoothOptions(FFBluetoothOptions* options)

src/modules/bluetooth/bluetooth.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#define FF_BLUETOOTH_MODULE_NAME "Bluetooth"
66

7-
void ffPrintBluetooth(FFBluetoothOptions* options);
7+
bool ffPrintBluetooth(FFBluetoothOptions* options);
88
void ffInitBluetoothOptions(FFBluetoothOptions* options);
99
void ffDestroyBluetoothOptions(FFBluetoothOptions* options);
1010

src/modules/bluetoothradio/bluetoothradio.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,33 @@ static void printDevice(FFBluetoothRadioOptions* options, const FFBluetoothRadio
6767
}
6868
}
6969

70-
void ffPrintBluetoothRadio(FFBluetoothRadioOptions* options)
70+
bool ffPrintBluetoothRadio(FFBluetoothRadioOptions* options)
7171
{
7272
FF_LIST_AUTO_DESTROY radios = ffListCreate(sizeof (FFBluetoothRadioResult));
7373
const char* error = ffDetectBluetoothRadio(&radios);
7474

7575
if(error)
7676
{
7777
ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
78+
return false;
7879
}
79-
else
80+
81+
uint8_t index = 0;
82+
FF_LIST_FOR_EACH(FFBluetoothRadioResult, radio, radios)
83+
{
84+
if (!radio->enabled)
85+
continue;
86+
87+
index++;
88+
printDevice(options, radio, index);
89+
}
90+
91+
if (index == 0)
8092
{
81-
uint8_t index = 0;
82-
FF_LIST_FOR_EACH(FFBluetoothRadioResult, radio, radios)
83-
{
84-
if (!radio->enabled)
85-
continue;
86-
87-
index++;
88-
printDevice(options, radio, index);
89-
}
90-
91-
if (index == 0)
92-
{
93-
if (radios.length > 0)
94-
ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Bluetooth radios found but none enabled");
95-
else
96-
ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected");
97-
}
93+
if (radios.length > 0)
94+
ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Bluetooth radios found but none enabled");
95+
else
96+
ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected");
9897
}
9998

10099
FF_LIST_FOR_EACH(FFBluetoothRadioResult, radio, radios)
@@ -103,6 +102,7 @@ void ffPrintBluetoothRadio(FFBluetoothRadioOptions* options)
103102
ffStrbufDestroy(&radio->address);
104103
ffStrbufDestroy(&radio->vendor);
105104
}
105+
return true;
106106
}
107107

108108
void ffParseBluetoothRadioJsonObject(FFBluetoothRadioOptions* options, yyjson_val* module)
@@ -123,15 +123,15 @@ void ffGenerateBluetoothRadioJsonConfig(FFBluetoothRadioOptions* options, yyjson
123123
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
124124
}
125125

126-
void ffGenerateBluetoothRadioJsonResult(FF_MAYBE_UNUSED FFBluetoothRadioOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
126+
bool ffGenerateBluetoothRadioJsonResult(FF_MAYBE_UNUSED FFBluetoothRadioOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
127127
{
128128
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBluetoothRadioResult));
129129

130130
const char* error = ffDetectBluetoothRadio(&results);
131131
if (error)
132132
{
133133
yyjson_mut_obj_add_str(doc, module, "error", error);
134-
return;
134+
return false;
135135
}
136136

137137
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
@@ -161,6 +161,8 @@ void ffGenerateBluetoothRadioJsonResult(FF_MAYBE_UNUSED FFBluetoothRadioOptions*
161161
ffStrbufDestroy(&radio->address);
162162
ffStrbufDestroy(&radio->vendor);
163163
}
164+
165+
return true;
164166
}
165167

166168
void ffInitBluetoothRadioOptions(FFBluetoothRadioOptions* options)

src/modules/bluetoothradio/bluetoothradio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#define FF_BLUETOOTHRADIO_MODULE_NAME "BluetoothRadio"
66

7-
void ffPrintBluetoothRadio(FFBluetoothRadioOptions* options);
7+
bool ffPrintBluetoothRadio(FFBluetoothRadioOptions* options);
88
void ffInitBluetoothRadioOptions(FFBluetoothRadioOptions* options);
99
void ffDestroyBluetoothRadioOptions(FFBluetoothRadioOptions* options);
1010

src/modules/board/board.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include "modules/board/board.h"
55
#include "util/stringUtils.h"
66

7-
void ffPrintBoard(FFBoardOptions* options)
7+
bool ffPrintBoard(FFBoardOptions* options)
88
{
9+
bool success = false;
910
FFBoardResult result;
1011
ffStrbufInit(&result.name);
1112
ffStrbufInit(&result.vendor);
@@ -42,12 +43,14 @@ void ffPrintBoard(FFBoardOptions* options)
4243
FF_FORMAT_ARG(result.serial, "serial"),
4344
}));
4445
}
46+
success = true;
4547

4648
exit:
4749
ffStrbufDestroy(&result.name);
4850
ffStrbufDestroy(&result.vendor);
4951
ffStrbufDestroy(&result.version);
5052
ffStrbufDestroy(&result.serial);
53+
return success;
5154
}
5255

5356
void ffParseBoardJsonObject(FFBoardOptions* options, yyjson_val* module)
@@ -68,8 +71,9 @@ void ffGenerateBoardJsonConfig(FFBoardOptions* options, yyjson_mut_doc* doc, yyj
6871
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
6972
}
7073

71-
void ffGenerateBoardJsonResult(FF_MAYBE_UNUSED FFBoardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
74+
bool ffGenerateBoardJsonResult(FF_MAYBE_UNUSED FFBoardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
7275
{
76+
bool success = false;
7377
FFBoardResult board;
7478
ffStrbufInit(&board.name);
7579
ffStrbufInit(&board.vendor);
@@ -95,12 +99,14 @@ void ffGenerateBoardJsonResult(FF_MAYBE_UNUSED FFBoardOptions* options, yyjson_m
9599
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &board.vendor);
96100
yyjson_mut_obj_add_strbuf(doc, obj, "version", &board.version);
97101
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &board.serial);
102+
success = true;
98103

99104
exit:
100105
ffStrbufDestroy(&board.name);
101106
ffStrbufDestroy(&board.vendor);
102107
ffStrbufDestroy(&board.version);
103108
ffStrbufDestroy(&board.serial);
109+
return success;
104110
}
105111

106112
void ffInitBoardOptions(FFBoardOptions* options)

0 commit comments

Comments
 (0)