Use enums for error codes

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
TheJackiMonster 2023-09-08 01:12:31 +02:00
parent 791413179c
commit 5bcf6efe57
No known key found for this signature in database
GPG key ID: D850A5F772E880F9
7 changed files with 127 additions and 78 deletions

View file

@ -56,7 +56,7 @@ int main(int argc, const char** argv) {
device3_clear(dev3);
while (dev3) {
if (device3_read(dev3, -1) < 0) {
if (device3_read(dev3, -1) != DEVICE3_ERROR_NO_ERROR) {
break;
}
}

View file

@ -55,7 +55,7 @@ int main(int argc, const char** argv) {
device4_clear(dev4);
while (dev4) {
if (device4_read(dev4, 1000) < 0) {
if (device4_read(dev4, 1000) != DEVICE4_ERROR_NO_ERROR) {
break;
}
}

View file

@ -46,6 +46,23 @@
extern "C" {
#endif
enum device3_error_t {
DEVICE3_ERROR_NO_ERROR = 0,
DEVICE3_ERROR_NO_DEVICE = 1,
DEVICE3_ERROR_NO_HANDLE = 2,
DEVICE3_ERROR_NO_ALLOCATION = 3,
DEVICE3_ERROR_WRONG_SIZE = 4,
DEVICE3_ERROR_FILE_NOT_OPEN = 5,
DEVICE3_ERROR_FILE_NOT_CLOSED = 6,
DEVICE3_ERROR_LOADING_FAILED = 7,
DEVICE3_ERROR_SAVING_FAILED = 8,
DEVICE3_ERROR_UNPLUGGED = 9,
DEVICE3_ERROR_UNEXPECTED = 10,
DEVICE3_ERROR_WRONG_SIGNATURE = 11,
DEVICE3_ERROR_INVALID_VALUE = 12,
DEVICE3_ERROR_UNKNOWN = 13,
};
struct __attribute__((__packed__)) device3_packet_t {
uint8_t signature [2];
uint8_t temperature [2];
@ -70,9 +87,9 @@ struct __attribute__((__packed__)) device3_packet_t {
};
enum device3_event_t {
DEVICE3_EVENT_UNKNOWN = 0,
DEVICE3_EVENT_INIT = 1,
DEVICE3_EVENT_UPDATE = 2,
DEVICE3_EVENT_UNKNOWN = 0,
DEVICE3_EVENT_INIT = 1,
DEVICE3_EVENT_UPDATE = 2,
};
struct device3_ahrs_t;
@ -91,6 +108,7 @@ struct device3_quat_t {
float w;
};
typedef enum device3_error_t device3_error_type;
typedef struct device3_packet_t device3_packet_type;
typedef enum device3_event_t device3_event_type;
@ -128,17 +146,17 @@ typedef struct device3_t device3_type;
device3_type* device3_open(device3_event_callback callback);
void device3_reset_calibration(device3_type* device);
device3_error_type device3_reset_calibration(device3_type* device);
int device3_load_calibration(device3_type* device, const char* path);
device3_error_type device3_load_calibration(device3_type* device, const char* path);
int device3_save_calibration(device3_type* device, const char* path);
device3_error_type device3_save_calibration(device3_type* device, const char* path);
void device3_clear(device3_type* device);
device3_error_type device3_clear(device3_type* device);
int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool accel, bool magnet);
device3_error_type device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool accel, bool magnet);
int device3_read(device3_type* device, int timeout);
device3_error_type device3_read(device3_type* device, int timeout);
device3_vec3_type device3_get_earth_acceleration(const device3_ahrs_type* ahrs);
@ -148,7 +166,7 @@ device3_quat_type device3_get_orientation(const device3_ahrs_type* ahrs);
device3_vec3_type device3_get_euler(device3_quat_type quat);
void device3_close(device3_type* device);
device3_error_type device3_close(device3_type* device);
#ifdef __cplusplus
} // extern "C"

View file

@ -97,6 +97,19 @@
extern "C" {
#endif
enum device4_error_t {
DEVICE4_ERROR_NO_ERROR = 0,
DEVICE4_ERROR_NO_DEVICE = 1,
DEVICE4_ERROR_NO_HANDLE = 2,
DEVICE4_ERROR_NO_ACTIVATION = 3,
DEVICE4_ERROR_WRONG_SIZE = 4,
DEVICE4_ERROR_UNPLUGGED = 5,
DEVICE4_ERROR_UNEXPECTED = 6,
DEVICE4_ERROR_WRONG_HEAD = 7,
DEVICE4_ERROR_INVALID_LENGTH = 8,
DEVICE4_ERROR_UNKNOWN = 8,
};
struct __attribute__((__packed__)) device4_packet_t {
uint8_t head;
uint32_t checksum;
@ -119,6 +132,7 @@ enum device4_event_t {
DEVICE4_EVENT_MESSAGE = 5,
};
typedef enum device4_error_t device4_error_type;
typedef struct device4_packet_t device4_packet_type;
typedef enum device4_event_t device4_event_type;
typedef void (*device4_event_callback)(
@ -150,13 +164,13 @@ typedef struct device4_t device4_type;
device4_type* device4_open(device4_event_callback callback);
void device4_clear(device4_type* device);
device4_error_type device4_clear(device4_type* device);
int device4_read(device4_type* device, int timeout);
device4_error_type device4_read(device4_type* device, int timeout);
bool device4_update_mcu_firmware(device4_type* device, const char* path);
device4_error_type device4_update_mcu_firmware(device4_type* device, const char* path);
void device4_close(device4_type* device);
device4_error_type device4_close(device4_type* device);
#ifdef __cplusplus
} // extern "C"

View file

@ -319,15 +319,15 @@ device3_type* device3_open(device3_event_callback callback) {
return device;
}
void device3_reset_calibration(device3_type* device) {
device3_error_type device3_reset_calibration(device3_type* device) {
if (!device) {
device3_error("No device");
return;
return DEVICE3_ERROR_NO_DEVICE;
}
if (!device->calibration) {
device3_error("Not allocated");
return;
return DEVICE3_ERROR_NO_ALLOCATION;
}
device->calibration->gyroscopeMisalignment = FUSION_IDENTITY_MATRIX;
@ -349,68 +349,74 @@ void device3_reset_calibration(device3_type* device) {
device->calibration->noises.element.w = 0.0f;
}
int device3_load_calibration(device3_type* device, const char* path) {
device3_error_type device3_load_calibration(device3_type* device, const char* path) {
if (!device) {
device3_error("No device");
return -1;
return DEVICE3_ERROR_NO_DEVICE;
}
if (!device->calibration) {
device3_error("Not allocated");
return -2;
return DEVICE3_ERROR_NO_ALLOCATION;
}
FILE* file = fopen(path, "rb");
if (!file) {
device3_error("No file opened");
return -3;
return DEVICE3_ERROR_FILE_NOT_OPEN;
}
device3_error_type result = DEVICE3_ERROR_NO_ERROR;
size_t count;
count = fread(device->calibration, 1, sizeof(device3_calibration_type), file);
if (sizeof(device3_calibration_type) != count) {
device3_error("Not fully loaded");
result = DEVICE3_ERROR_LOADING_FAILED;
}
if (0 != fclose(file)) {
device3_error("No file closed");
return -4;
return DEVICE3_ERROR_FILE_NOT_CLOSED;
}
return 0;
return result;
}
int device3_save_calibration(device3_type* device, const char* path) {
device3_error_type device3_save_calibration(device3_type* device, const char* path) {
if (!device) {
device3_error("No device");
return -1;
return DEVICE3_ERROR_NO_DEVICE;
}
if (!device->calibration) {
device3_error("Not allocated");
return -2;
return DEVICE3_ERROR_NO_ALLOCATION;
}
FILE* file = fopen(path, "wb");
if (!file) {
device3_error("No file opened");
return -3;
return DEVICE3_ERROR_FILE_NOT_OPEN;
}
device3_error_type result = DEVICE3_ERROR_NO_ERROR;
size_t count;
count = fwrite(device->calibration, 1, sizeof(device3_calibration_type), file);
if (sizeof(device3_calibration_type) != count) {
device3_error("Not fully saved");
result = DEVICE3_ERROR_SAVING_FAILED;
}
if (0 != fclose(file)) {
device3_error("No file closed");
return -4;
return DEVICE3_ERROR_FILE_NOT_CLOSED;
}
return 0;
return result;
}
static void device3_callback(device3_type* device,
@ -607,24 +613,24 @@ static void apply_calibration(const device3_type* device,
*magnetometer = FusionAxesSwap(*magnetometer, alignment);
}
void device3_clear(device3_type* device) {
device3_read(device, 10);
device3_error_type device3_clear(device3_type* device) {
return device3_read(device, 10);
}
int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool accel, bool magnet) {
device3_error_type device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool accel, bool magnet) {
if (!device) {
device3_error("No device");
return -1;
return DEVICE3_ERROR_NO_DEVICE;
}
if (!device->handle) {
device3_error("No handle");
return -2;
return DEVICE3_ERROR_NO_HANDLE;
}
if (MAX_PACKET_SIZE != sizeof(device3_packet_type)) {
device3_error("Not proper size");
return -3;
return DEVICE3_ERROR_WRONG_SIZE;
}
device3_packet_type packet;
@ -649,7 +655,7 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool
if (transferred == -1) {
device3_error("Device may be unplugged");
return -4;
return DEVICE3_ERROR_UNPLUGGED;
}
if (transferred == 0) {
@ -657,8 +663,8 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool
}
if (MAX_PACKET_SIZE != transferred) {
device3_error("Not expected issue");
return -5;
device3_error("Unexpected packet size");
return DEVICE3_ERROR_UNEXPECTED;
}
if ((packet.signature[0] != 0x01) || (packet.signature[1] != 0x02)) {
@ -729,23 +735,23 @@ int device3_calibrate(device3_type* device, uint32_t iterations, bool gyro, bool
}
}
return 0;
return DEVICE3_ERROR_NO_ERROR;
}
int device3_read(device3_type* device, int timeout) {
device3_error_type device3_read(device3_type* device, int timeout) {
if (!device) {
device3_error("No device");
return -1;
return DEVICE3_ERROR_NO_DEVICE;
}
if (!device->handle) {
device3_error("No handle");
return -2;
return DEVICE3_ERROR_NO_HANDLE;
}
if (MAX_PACKET_SIZE != sizeof(device3_packet_type)) {
device3_error("Not proper size");
return -3;
return DEVICE3_ERROR_WRONG_SIZE;
}
device3_packet_type packet;
@ -760,28 +766,28 @@ int device3_read(device3_type* device, int timeout) {
if (transferred == -1) {
device3_error("Device may be unplugged");
return -4;
return DEVICE3_ERROR_UNPLUGGED;
}
if (transferred == 0) {
return 1;
return DEVICE3_ERROR_NO_ERROR;
}
if (MAX_PACKET_SIZE != transferred) {
device3_error("Unexpected packet size");
return -5;
return DEVICE3_ERROR_UNEXPECTED;
}
const uint64_t timestamp = packet.timestamp;
if ((packet.signature[0] == 0xaa) && (packet.signature[1] == 0x53)) {
device3_callback(device, timestamp, DEVICE3_EVENT_INIT);
return 0;
return DEVICE3_ERROR_NO_ERROR;
}
if ((packet.signature[0] != 0x01) || (packet.signature[1] != 0x02)) {
device3_error("Not matching signature");
return -6;
return DEVICE3_ERROR_WRONG_SIGNATURE;
}
const uint64_t delta = timestamp - device->last_timestamp;
@ -820,12 +826,12 @@ int device3_read(device3_type* device, int timeout) {
// the gyro/accel/magnet readings
if (isnan(orientation.x) || isnan(orientation.y) || isnan(orientation.z) || isnan(orientation.w)) {
device3_error("Invalid orientation reading");
return -7;
return DEVICE3_ERROR_INVALID_VALUE;
}
}
device3_callback(device, timestamp, DEVICE3_EVENT_UPDATE);
return 0;
return DEVICE3_ERROR_NO_ERROR;
}
device3_vec3_type device3_get_earth_acceleration(const device3_ahrs_type* ahrs) {
@ -870,10 +876,10 @@ device3_vec3_type device3_get_euler(device3_quat_type quat) {
return e;
}
void device3_close(device3_type* device) {
device3_error_type device3_close(device3_type* device) {
if (!device) {
device3_error("No device");
return;
return DEVICE3_ERROR_NO_DEVICE;
}
if (device->calibration) {
@ -895,4 +901,6 @@ void device3_close(device3_type* device) {
free(device);
hid_exit();
return DEVICE3_ERROR_NO_ERROR;
}

View file

@ -301,24 +301,24 @@ static void device4_callback(device4_type* device,
device->callback(timestamp, event, brightness, msg);
}
void device4_clear(device4_type* device) {
device4_read(device, 10);
device4_error_type device4_clear(device4_type* device) {
return device4_read(device, 10);
}
int device4_read(device4_type* device, int timeout) {
device4_error_type device4_read(device4_type* device, int timeout) {
if (!device) {
device4_error("No device");
return -1;
return DEVICE4_ERROR_NO_DEVICE;
}
if (!device->handle) {
device4_error("No handle");
return -2;
return DEVICE4_ERROR_NO_HANDLE;
}
if (MAX_PACKET_SIZE != sizeof(device4_packet_type)) {
device4_error("Not proper size");
return -3;
return DEVICE4_ERROR_WRONG_SIZE;
}
device4_packet_type packet;
@ -331,18 +331,23 @@ int device4_read(device4_type* device, int timeout) {
timeout
);
if (transferred == -1) {
device4_error("Device may be unplugged");
return DEVICE4_ERROR_UNPLUGGED;
}
if (transferred == 0) {
return 1;
return DEVICE4_ERROR_NO_ERROR;
}
if (MAX_PACKET_SIZE != transferred) {
device4_error("Reading failed");
return -4;
device4_error("Unexpected packet size");
return DEVICE4_ERROR_UNEXPECTED;
}
if (packet.head != PACKET_HEAD) {
device4_error("Wrong packet");
return -5;
device4_error("Wrong packet head");
return DEVICE4_ERROR_WRONG_HEAD;
}
const uint32_t timestamp = packet.timestamp;
@ -427,7 +432,7 @@ int device4_read(device4_type* device, int timeout) {
if (data_len + text_len != packet.length) {
device4_error("Not matching length");
return -6;
return DEVICE4_ERROR_INVALID_LENGTH;
}
device4_callback(
@ -453,24 +458,25 @@ int device4_read(device4_type* device, int timeout) {
break;
}
return 0;
return DEVICE4_ERROR_NO_ERROR;
}
bool device4_update_mcu_firmware(device4_type* device, const char* path) {
device4_error_type device4_update_mcu_firmware(device4_type* device, const char* path) {
if (!device) {
return false;
device4_error("No device");
return DEVICE4_ERROR_NO_DEVICE;
}
if (!device->activated) {
device4_error("Device is not activated");
return false;
return DEVICE4_ERROR_NO_ACTIVATION;
}
size_t firmware_len = 0;
uint8_t* firmware = NULL;
FILE* file = fopen(path, "rb");
bool result = false;
bool result = DEVICE4_ERROR_UNKNOWN;
if (file) {
fseek(file, 0, SEEK_END);
@ -500,13 +506,13 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) {
if (!send_payload_action(device, DEVICE4_MSG_R_ACTIVATION_TIME, 0, NULL)) {
device4_error("Requesting activation time failed");
return device;
goto cleanup;
}
uint8_t activated;
if (!recv_payload_msg(device, DEVICE4_MSG_R_ACTIVATION_TIME, 1, &activated)) {
device4_error("Receiving activation time failed");
return device;
goto cleanup;
}
if (!activated) {
@ -546,7 +552,7 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) {
goto jump_to_app;
}
result = true;
result = DEVICE4_ERROR_NO_ERROR;
jump_to_app:
if (!do_payload_action(device, DEVICE4_MSG_W_BOOT_JUMP_TO_APP, 0, NULL)) {
@ -562,9 +568,10 @@ cleanup:
return result;
}
void device4_close(device4_type* device) {
device4_error_type device4_close(device4_type* device) {
if (!device) {
return;
device4_error("No device");
return DEVICE4_ERROR_NO_DEVICE;
}
if (device->handle) {
@ -574,4 +581,6 @@ void device4_close(device4_type* device) {
free(device);
hid_exit();
return DEVICE4_ERROR_NO_ERROR;
}

View file

@ -106,7 +106,7 @@ int main(int argc, const char** argv) {
device3_clear(dev3);
while (dev3) {
if (device3_read(dev3, 0) < 0) {
if (device3_read(dev3, 0) != DEVICE3_ERROR_NO_ERROR) {
break;
}
}
@ -118,7 +118,7 @@ int main(int argc, const char** argv) {
device4_clear(dev4);
while (dev4) {
if (device4_read(dev4, 0) < 0) {
if (device4_read(dev4, 0) != DEVICE4_ERROR_NO_ERROR) {
break;
}
}