Skip to content

Commit

Permalink
Fix bug in output and static analyzer warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Sapd committed Apr 1, 2024
1 parent 0547392 commit 1e9e1e5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 48 deletions.
5 changes: 2 additions & 3 deletions src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ static inline bool has_capability(int device_capabilities, enum capabilities cap

struct capability_detail {
// Usage page, only used when usageid is not 0; HID Protocol specific
int usagepage;
uint16_t usagepage;
// Used instead of interface when not 0, and only used on Windows currently; HID Protocol specific
int usageid;
uint16_t usageid;
/// Interface ID - zero means first enumerated interface!
int interface;
};
Expand Down Expand Up @@ -117,7 +117,6 @@ typedef struct {
void* param;
bool should_process;
FeatureResult result;
const char* name;
} FeatureRequest;

/** @brief Defines equalizer custom setings
Expand Down
48 changes: 26 additions & 22 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static FeatureResult handle_feature(struct device* device_found, hid_device** de

switch (cap) {
case CAP_SIDETONE:
ret = device_found->send_sidetone(*device_handle, *(int*)param);
ret = device_found->send_sidetone(*device_handle, (uint8_t) * (int*)param);
break;

case CAP_BATTERY_STATUS: {
Expand Down Expand Up @@ -252,15 +252,15 @@ static FeatureResult handle_feature(struct device* device_found, hid_device** de
}

case CAP_NOTIFICATION_SOUND:
ret = device_found->notifcation_sound(*device_handle, *(int*)param);
ret = device_found->notifcation_sound(*device_handle, (uint8_t) * (int*)param);
break;

case CAP_LIGHTS:
ret = device_found->switch_lights(*device_handle, *(int*)param);
ret = device_found->switch_lights(*device_handle, (uint8_t) * (int*)param);
break;

case CAP_INACTIVE_TIME:
ret = device_found->send_inactive_time(*device_handle, *(int*)param);
ret = device_found->send_inactive_time(*device_handle, (uint8_t) * (int*)param);
break;

case CAP_CHATMIX_STATUS:
Expand All @@ -279,31 +279,32 @@ static FeatureResult handle_feature(struct device* device_found, hid_device** de
return result;

case CAP_VOICE_PROMPTS:
ret = device_found->switch_voice_prompts(*device_handle, *(int*)param);
ret = device_found->switch_voice_prompts(*device_handle, (uint8_t) * (int*)param);
break;

case CAP_ROTATE_TO_MUTE:
ret = device_found->switch_rotate_to_mute(*device_handle, *(int*)param);
ret = device_found->switch_rotate_to_mute(*device_handle, (uint8_t) * (int*)param);
break;

case CAP_EQUALIZER_PRESET:
ret = device_found->send_equalizer_preset(*device_handle, *(int*)param);
ret = device_found->send_equalizer_preset(*device_handle, (uint8_t) * (int*)param);
break;

case CAP_EQUALIZER:
ret = device_found->send_equalizer(*device_handle, (struct equalizer_settings*)param);
break;

case CAP_MICROPHONE_MUTE_LED_BRIGHTNESS:
ret = device_found->send_microphone_mute_led_brightness(*device_handle, *(int*)param);
ret = device_found->send_microphone_mute_led_brightness(*device_handle, (uint8_t) * (int*)param);
break;

case CAP_MICROPHONE_VOLUME:
ret = device_found->send_microphone_volume(*device_handle, *(int*)param);
ret = device_found->send_microphone_volume(*device_handle, (uint8_t) * (int*)param);
break;

case NUM_CAPABILITIES:
ret = -99; // silence warning
UNUSED(ret);

assert(0);
break;
Expand Down Expand Up @@ -472,6 +473,7 @@ volatile sig_atomic_t follow = false;

void interruptHandler(int signal_number)
{
UNUSED(signal_number);
follow = false;
}

Expand Down Expand Up @@ -673,6 +675,7 @@ int main(int argc, char* argv[])
print_capabilities = 1;
} else {
// User issued an invalid option (stdlib will make an error message automatically)
free(read_buffer);
return 1;
}
break;
Expand Down Expand Up @@ -732,6 +735,7 @@ int main(int argc, char* argv[])
break;
default:
fprintf(stderr, "Invalid argument %c\n", c);
free(read_buffer);
return 1;
}
}
Expand Down Expand Up @@ -788,18 +792,18 @@ int main(int argc, char* argv[])
#endif

FeatureRequest featureRequests[] = {
{ CAP_SIDETONE, CAPABILITYTYPE_ACTION, &sidetone_loudness, sidetone_loudness != -1 },
{ CAP_LIGHTS, CAPABILITYTYPE_ACTION, &lights, lights != -1 },
{ CAP_NOTIFICATION_SOUND, CAPABILITYTYPE_ACTION, &notification_sound, notification_sound != -1 },
{ CAP_BATTERY_STATUS, CAPABILITYTYPE_INFO, &request_battery, request_battery == 1 },
{ CAP_INACTIVE_TIME, CAPABILITYTYPE_ACTION, &inactive_time, inactive_time != -1 },
{ CAP_CHATMIX_STATUS, CAPABILITYTYPE_INFO, &request_chatmix, request_chatmix == 1 },
{ CAP_VOICE_PROMPTS, CAPABILITYTYPE_ACTION, &voice_prompts, voice_prompts != -1 },
{ CAP_ROTATE_TO_MUTE, CAPABILITYTYPE_ACTION, &rotate_to_mute, rotate_to_mute != -1 },
{ CAP_EQUALIZER_PRESET, CAPABILITYTYPE_ACTION, &equalizer_preset, equalizer_preset != -1 },
{ CAP_MICROPHONE_MUTE_LED_BRIGHTNESS, CAPABILITYTYPE_ACTION, &microphone_mute_led_brightness, microphone_mute_led_brightness != -1 },
{ CAP_MICROPHONE_VOLUME, CAPABILITYTYPE_ACTION, &microphone_volume, microphone_volume != -1 },
{ CAP_EQUALIZER, CAPABILITYTYPE_ACTION, equalizer, equalizer != NULL },
{ CAP_SIDETONE, CAPABILITYTYPE_ACTION, &sidetone_loudness, sidetone_loudness != -1, {} },
{ CAP_LIGHTS, CAPABILITYTYPE_ACTION, &lights, lights != -1, {} },
{ CAP_NOTIFICATION_SOUND, CAPABILITYTYPE_ACTION, &notification_sound, notification_sound != -1, {} },
{ CAP_BATTERY_STATUS, CAPABILITYTYPE_INFO, &request_battery, request_battery == 1, {} },
{ CAP_INACTIVE_TIME, CAPABILITYTYPE_ACTION, &inactive_time, inactive_time != -1, {} },
{ CAP_CHATMIX_STATUS, CAPABILITYTYPE_INFO, &request_chatmix, request_chatmix == 1, {} },
{ CAP_VOICE_PROMPTS, CAPABILITYTYPE_ACTION, &voice_prompts, voice_prompts != -1, {} },
{ CAP_ROTATE_TO_MUTE, CAPABILITYTYPE_ACTION, &rotate_to_mute, rotate_to_mute != -1, {} },
{ CAP_EQUALIZER_PRESET, CAPABILITYTYPE_ACTION, &equalizer_preset, equalizer_preset != -1, {} },
{ CAP_MICROPHONE_MUTE_LED_BRIGHTNESS, CAPABILITYTYPE_ACTION, &microphone_mute_led_brightness, microphone_mute_led_brightness != -1, {} },
{ CAP_MICROPHONE_VOLUME, CAPABILITYTYPE_ACTION, &microphone_volume, microphone_volume != -1, {} },
{ CAP_EQUALIZER, CAPABILITYTYPE_ACTION, equalizer, equalizer != NULL, {} },
};
int numFeatures = sizeof(featureRequests) / sizeof(featureRequests[0]);
assert(numFeatures == NUM_CAPABILITIES);
Expand Down Expand Up @@ -828,7 +832,7 @@ int main(int argc, char* argv[])

if ((device_found.capabilities & B(CAP_BATTERY_STATUS)) == B(CAP_BATTERY_STATUS)) {
device_handle = dynamic_connect(&hid_path, device_handle, &device_found, CAP_BATTERY_STATUS);
if (!device_handle | !(device_handle))
if (!device_handle)
return 1;

BatteryInfo info = device_found.request_battery(device_handle);
Expand Down
58 changes: 35 additions & 23 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ static void addError(HeadsetInfo* info, const char* source, const char* message)
*/
static void addAction(HeadsetInfo* info, enum capabilities capability, const char* device, Status status, int value, const char* error_message)
{
UNUSED(value);

if (info->action_count < MAX_ACTIONS) {
info->actions[info->action_count].capability = capabilities_str_enum[capability];
info->actions[info->action_count].capability_str = capabilities_str[capability];
Expand Down Expand Up @@ -105,7 +107,13 @@ void output(DeviceList* deviceList, bool print_capabilities, OutputType output)
int num_devices = deviceList ? deviceList->num_devices : 0;

HeadsetControlStatus status = initializeStatus(num_devices);
HeadsetInfo* infos = calloc(num_devices, sizeof(HeadsetInfo));
HeadsetInfo* infos = NULL;
if (num_devices > 0) {
infos = calloc(num_devices, sizeof(HeadsetInfo));
}

// Supress static analysis warning
assert(infos != NULL && status.device_count > 0 || infos == NULL && status.device_count == 0);

Check failure on line 116 in src/output.c

View workflow job for this annotation

GitHub Actions / ubuntu-latest-compile

suggest parentheses around ‘&&’ within ‘||’ [-Werror=parentheses]

Check failure on line 116 in src/output.c

View workflow job for this annotation

GitHub Actions / ubuntu-latest-compile

suggest parentheses around ‘&&’ within ‘||’ [-Werror=parentheses]

Check failure on line 116 in src/output.c

View workflow job for this annotation

GitHub Actions / ubuntu-latest-compile

suggest parentheses around ‘&&’ within ‘||’ [-Werror=parentheses]

Check failure on line 116 in src/output.c

View workflow job for this annotation

GitHub Actions / ubuntu-latest-compile

suggest parentheses around ‘&&’ within ‘||’ [-Werror=parentheses]

// Iterate through all devices
for (int deviceIndex = 0; deviceIndex < num_devices; deviceIndex++) {
Expand Down Expand Up @@ -286,7 +294,7 @@ void output_json(HeadsetControlStatus* status, HeadsetInfo* infos)
json_print_key_value("hidapi_version", status->hid_version, 2);
printf(",\n");

if (infos->action_count > 0) {
if (infos && infos->action_count > 0) {
printf(" \"actions\": [\n");
for (int i = 0; i < infos->action_count; i++) {
printf(" {\n");
Expand Down Expand Up @@ -454,7 +462,7 @@ void output_yaml(HeadsetControlStatus* status, HeadsetInfo* infos)
yaml_print("api_version", status->api_version, 0);
yaml_print("hidapi_version", status->hid_version, 0);

if (infos->action_count > 0) {
if (infos && infos->action_count > 0) {
yaml_print("actions", "", 0);

for (int i = 0; i < infos->action_count; i++) {
Expand Down Expand Up @@ -533,11 +541,11 @@ const char* env_format_key(const char* str)
{
static char result[128];
int i = 0, j = 0;
while (str[i] != '\0' && j < sizeof(result) - 1) {
while (str[i] != '\0' && j < (int)(sizeof(result) - 1)) {
if (str[i] == ' ' || str[i] == '-') {
result[j++] = '_';
} else {
result[j++] = toupper((unsigned char)str[i]);
result[j++] = (unsigned char)toupper((unsigned char)str[i]);
}
i++;
}
Expand All @@ -552,29 +560,33 @@ void output_env(HeadsetControlStatus* status, HeadsetInfo* infos)
env_print("HEADSETCONTROL_API_VERSION", status->api_version);
env_print("HEADSETCONTROL_HIDAPI_VERSION", status->hid_version);

env_printint("ACTION_COUNT", infos->action_count);
for (int i = 0; i < infos->action_count; i++) {
char prefix[64];
sprintf(prefix, "ACTION_%d", i);
if (infos) {
env_printint("ACTION_COUNT", infos->action_count);
for (int i = 0; i < infos->action_count; i++) {
char prefix[64];
sprintf(prefix, "ACTION_%d", i);

char key[128];
char key[128];

sprintf(key, "%s_CAPABILITY", prefix);
env_print(key, infos->actions[i].capability);
sprintf(key, "%s_DEVICE", prefix);
env_print(key, infos->actions[i].device);
sprintf(key, "%s_STATUS", prefix);
env_print(key, status_to_string(infos->actions[i].status));
sprintf(key, "%s_CAPABILITY", prefix);
env_print(key, infos->actions[i].capability);
sprintf(key, "%s_DEVICE", prefix);
env_print(key, infos->actions[i].device);
sprintf(key, "%s_STATUS", prefix);
env_print(key, status_to_string(infos->actions[i].status));

if (infos->actions[i].value > 0) {
sprintf(key, "%s_VALUE", prefix);
env_printint(key, infos->actions[i].value);
}
if (infos->actions[i].value > 0) {
sprintf(key, "%s_VALUE", prefix);
env_printint(key, infos->actions[i].value);
}

if (infos->actions[i].error_message != NULL && strlen(infos->actions[i].error_message) > 0) {
sprintf(key, "%s_ERROR_MESSAGE", prefix);
env_print(key, infos->actions[i].error_message);
if (infos->actions[i].error_message != NULL && strlen(infos->actions[i].error_message) > 0) {
sprintf(key, "%s_ERROR_MESSAGE", prefix);
env_print(key, infos->actions[i].error_message);
}
}
} else {
env_printint("ACTION_COUNT", 0);
}

env_printint("DEVICE_COUNT", status->device_count);
Expand Down
3 changes: 3 additions & 0 deletions src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <stdarg.h>

// For unused variables
#define UNUSED(x) (void)x;

/** @brief Maps a value x from a given range to another range
*
* The input x is mapped from the range in_min and in_max
Expand Down

0 comments on commit 1e9e1e5

Please sign in to comment.