Return errors on open functions

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
TheJackiMonster 2023-09-08 13:04:05 +02:00
parent 2bd57ee0dd
commit b074ab72fc
No known key found for this signature in database
GPG key ID: D850A5F772E880F9
8 changed files with 72 additions and 90 deletions

View file

@ -47,20 +47,13 @@ void test3(uint64_t timestamp,
}
int main(int argc, const char** argv) {
device3_type* dev3 = device3_open(test3);
if (!dev3) {
device3_type dev3;
if (DEVICE3_ERROR_NO_ERROR != device3_open(&dev3, test3)) {
return 1;
}
device3_clear(dev3);
while (dev3) {
if (device3_read(dev3, -1) != DEVICE3_ERROR_NO_ERROR) {
break;
}
}
device3_close(dev3);
device3_clear(&dev3);
while (DEVICE3_ERROR_NO_ERROR == device3_read(&dev3, -1));
device3_close(&dev3);
return 0;
}

View file

@ -46,20 +46,13 @@ void test4(uint64_t timestamp,
}
int main(int argc, const char** argv) {
device4_type* dev4 = device4_open(test4);
if (!dev4) {
device4_type dev4;
if (DEVICE4_ERROR_NO_ERROR != device4_open(&dev4, test4)) {
return 1;
}
device4_clear(dev4);
while (dev4) {
if (device4_read(dev4, 1000) != DEVICE4_ERROR_NO_ERROR) {
break;
}
}
device4_close(dev4);
device4_clear(&dev4);
while (DEVICE4_ERROR_NO_ERROR == device4_read(&dev4, -1));
device4_close(&dev4);
return 0;
}

View file

@ -67,14 +67,13 @@ int main(int argc, const char** argv) {
return 0;
}
device4_type* dev4 = device4_open(NULL);
if (!dev4) {
device4_type dev4;
if (DEVICE4_ERROR_NO_ERROR != device4_open(&dev4, NULL)) {
return 1;
}
device4_clear(dev4);
device4_update_mcu_firmware(dev4, path);
device4_close(dev4);
device4_clear(&dev4);
device4_update_mcu_firmware(&dev4, path);
device4_close(&dev4);
return 0;
}

View file

@ -60,7 +60,9 @@ enum device3_error_t {
DEVICE3_ERROR_UNEXPECTED = 10,
DEVICE3_ERROR_WRONG_SIGNATURE = 11,
DEVICE3_ERROR_INVALID_VALUE = 12,
DEVICE3_ERROR_UNKNOWN = 13,
DEVICE3_ERROR_NOT_INITIALIZED = 13,
DEVICE3_ERROR_PAYLOAD_FAILED = 14,
DEVICE3_ERROR_UNKNOWN = 15,
};
struct __attribute__((__packed__)) device3_packet_t {
@ -144,7 +146,7 @@ struct device3_t {
typedef struct device3_t device3_type;
device3_type* device3_open(device3_event_callback callback);
device3_error_type device3_open(device3_type* device, device3_event_callback callback);
device3_error_type device3_reset_calibration(device3_type* device);

View file

@ -107,7 +107,9 @@ enum device4_error_t {
DEVICE4_ERROR_UNEXPECTED = 6,
DEVICE4_ERROR_WRONG_HEAD = 7,
DEVICE4_ERROR_INVALID_LENGTH = 8,
DEVICE4_ERROR_UNKNOWN = 9,
DEVICE4_ERROR_NOT_INITIALIZED = 9,
DEVICE4_ERROR_PAYLOAD_FAILED = 10,
DEVICE4_ERROR_UNKNOWN = 11,
};
struct __attribute__((__packed__)) device4_packet_t {
@ -162,7 +164,7 @@ struct device4_t {
typedef struct device4_t device4_type;
device4_type* device4_open(device4_event_callback callback);
device4_error_type device4_open(device4_type* device, device4_event_callback callback);
device4_error_type device4_clear(device4_type* device);

View file

@ -181,12 +181,10 @@ static FusionQuaternion json_object_get_quaternion(struct json_object* obj) {
return quaternion;
}
device3_type* device3_open(device3_event_callback callback) {
device3_type* device = (device3_type*) malloc(sizeof(device3_type));
device3_error_type device3_open(device3_type* device, device3_event_callback callback) {
if (!device) {
device3_error("Not allocated");
return NULL;
device3_error("No device");
return DEVICE3_ERROR_NO_DEVICE;
}
memset(device, 0, sizeof(device3_type));
@ -196,7 +194,7 @@ device3_type* device3_open(device3_event_callback callback) {
if (0 != hid_init()) {
device3_error("Not initialized");
return device;
return DEVICE3_ERROR_NOT_INITIALIZED;
}
struct hid_device_info* info = hid_enumerate(
@ -218,14 +216,14 @@ device3_type* device3_open(device3_event_callback callback) {
if (!device->handle) {
device3_error("No handle");
return device;
return DEVICE3_ERROR_NO_HANDLE;
}
device3_clear(device);
if (!send_payload_msg(device, DEVICE3_MSG_GET_STATIC_ID, 0, NULL)) {
device3_error("Failed sending payload to get static id");
return device;
return DEVICE3_ERROR_PAYLOAD_FAILED;
}
uint32_t static_id = 0;
@ -240,7 +238,7 @@ device3_type* device3_open(device3_event_callback callback) {
if (!send_payload_msg(device, DEVICE3_MSG_GET_CAL_DATA_LENGTH, 0, NULL)) {
device3_error("Failed sending payload to get calibration data length");
return device;
return DEVICE3_ERROR_PAYLOAD_FAILED;
}
uint32_t calibration_len = 0;
@ -296,7 +294,7 @@ device3_type* device3_open(device3_event_callback callback) {
if (!send_payload_msg_signal(device, DEVICE3_MSG_START_IMU_DATA, 0x1)) {
device3_error("Failed sending payload to start imu data stream");
return device;
return DEVICE3_ERROR_PAYLOAD_FAILED;
}
const uint32_t SAMPLE_RATE = 1000;
@ -316,7 +314,7 @@ device3_type* device3_open(device3_event_callback callback) {
};
FusionAhrsSetSettings((FusionAhrs*) device->ahrs, &settings);
return device;
return DEVICE3_ERROR_NO_ERROR;
}
device3_error_type device3_reset_calibration(device3_type* device) {
@ -896,10 +894,9 @@ device3_error_type device3_close(device3_type* device) {
if (device->handle) {
hid_close(device->handle);
device->handle = NULL;
}
free(device);
memset(device, 0, sizeof(device3_type));
hid_exit();
return DEVICE3_ERROR_NO_ERROR;

View file

@ -165,12 +165,10 @@ static bool do_payload_action(device4_type* device, uint16_t msgid, uint8_t len,
return false;
}
device4_type* device4_open(device4_event_callback callback) {
device4_type* device = (device4_type*) malloc(sizeof(device4_type));
device4_error_type device4_open(device4_type* device, device4_event_callback callback) {
if (!device) {
device4_error("Not allocated");
return NULL;
device4_error("No device");
return DEVICE4_ERROR_NO_DEVICE;
}
memset(device, 0, sizeof(device4_type));
@ -180,7 +178,7 @@ device4_type* device4_open(device4_event_callback callback) {
if (0 != hid_init()) {
device4_error("Not initialized");
return device;
return DEVICE4_ERROR_NOT_INITIALIZED;
}
struct hid_device_info* info = hid_enumerate(
@ -202,57 +200,57 @@ device4_type* device4_open(device4_event_callback callback) {
if (!device->handle) {
device4_error("No handle");
return device;
return DEVICE4_ERROR_NO_HANDLE;
}
device4_clear(device);
if (!send_payload_action(device, DEVICE4_MSG_R_ACTIVATION_TIME, 0, NULL)) {
device4_error("Requesting activation time failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
uint8_t activated;
if (!recv_payload_msg(device, DEVICE4_MSG_R_ACTIVATION_TIME, 1, &activated)) {
device4_error("Receiving activation time failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
device->activated = (activated != 0);
if (!device->activated) {
device4_error("Device is not activated");
return device;
return DEVICE4_ERROR_NO_ACTIVATION;
}
if (!send_payload_action(device, DEVICE4_MSG_R_MCU_APP_FW_VERSION, 0, NULL)) {
device4_error("Requesting current MCU app firmware version");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
if (!recv_payload_msg(device, DEVICE4_MSG_R_MCU_APP_FW_VERSION, 41, (uint8_t*) device->mcu_app_fw_version)) {
device4_error("Receiving current MCU app firmware version failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
if (!send_payload_action(device, DEVICE4_MSG_R_DP7911_FW_VERSION, 0, NULL)) {
device4_error("Requesting current DP firmware version");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
if (!recv_payload_msg(device, DEVICE4_MSG_R_DP7911_FW_VERSION, 41, (uint8_t*) device->dp_fw_version)) {
device4_error("Receiving current DP firmware version failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
if (!send_payload_action(device, DEVICE4_MSG_R_DSP_APP_FW_VERSION, 0, NULL)) {
device4_error("Requesting current DSP app firmware version");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
if (!recv_payload_msg(device, DEVICE4_MSG_R_DSP_APP_FW_VERSION, 41, (uint8_t*) device->dsp_fw_version)) {
device4_error("Receiving current DSP app firmware version failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
#ifndef NDEBUG
@ -263,22 +261,22 @@ device4_type* device4_open(device4_event_callback callback) {
if (!send_payload_action(device, DEVICE4_MSG_R_BRIGHTNESS, 0, NULL)) {
device4_error("Requesting initial brightness failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
if (!recv_payload_msg(device, DEVICE4_MSG_R_BRIGHTNESS, 1, &device->brightness)) {
device4_error("Receiving initial brightness failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
if (!send_payload_action(device, DEVICE4_MSG_R_DISP_MODE, 0, NULL)) {
device4_error("Requesting display mode failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
if (!recv_payload_msg(device, DEVICE4_MSG_R_DISP_MODE, 1, &device->disp_mode)) {
device4_error("Receiving display mode failed");
return device;
return DEVICE4_ERROR_PAYLOAD_FAILED;
}
#ifndef NDEBUG
@ -286,7 +284,7 @@ device4_type* device4_open(device4_event_callback callback) {
printf("Disp-Mode: %d\n", device->disp_mode);
#endif
return device;
return DEVICE4_ERROR_NO_ERROR;
}
static void device4_callback(device4_type* device,
@ -576,10 +574,9 @@ device4_error_type device4_close(device4_type* device) {
if (device->handle) {
hid_close(device->handle);
device->handle = NULL;
}
free(device);
memset(device, 0, sizeof(device4_type));
hid_exit();
return DEVICE4_ERROR_NO_ERROR;

View file

@ -102,30 +102,29 @@ int main(int argc, const char** argv) {
}
if (pid == 0) {
device3_type* dev3 = device3_open(test3);
device3_clear(dev3);
while (dev3) {
if (device3_read(dev3, 0) != DEVICE3_ERROR_NO_ERROR) {
break;
}
device3_type dev3;
if (DEVICE3_ERROR_NO_ERROR != device3_open(&dev3, test3)) {
return 1;
}
device3_close(dev3);
device3_clear(&dev3);
while (DEVICE3_ERROR_NO_ERROR == device3_read(&dev3, 0));
device3_close(&dev3);
return 0;
} else {
device4_type* dev4 = device4_open(test4);
device4_clear(dev4);
while (dev4) {
if (device4_read(dev4, 0) != DEVICE4_ERROR_NO_ERROR) {
break;
}
}
device4_close(dev4);
int status = 0;
device4_type dev4;
if (DEVICE4_ERROR_NO_ERROR != device4_open(&dev4, test4)) {
status = 1;
goto exit;
}
device4_clear(&dev4);
while (DEVICE4_ERROR_NO_ERROR == device4_read(&dev4, 0));
device4_close(&dev4);
exit:
if (pid != waitpid(pid, &status, 0)) {
return 1;
}