From 4df6f1743a9f0d27b954eb3b43c43a4ef38235cb Mon Sep 17 00:00:00 2001 From: Frank Stettner Date: Sun, 5 Sep 2021 15:10:20 +0200 Subject: [PATCH] zketech-ebd-usb: Style fixes --- src/hardware/zketech-ebd-usb/api.c | 14 +- src/hardware/zketech-ebd-usb/protocol.c | 224 +++++++++++++----------- src/hardware/zketech-ebd-usb/protocol.h | 21 ++- 3 files changed, 143 insertions(+), 116 deletions(-) diff --git a/src/hardware/zketech-ebd-usb/api.c b/src/hardware/zketech-ebd-usb/api.c index 7aeb8c8e..59d9d800 100644 --- a/src/hardware/zketech-ebd-usb/api.c +++ b/src/hardware/zketech-ebd-usb/api.c @@ -86,7 +86,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devc = g_malloc0(sizeof(struct dev_context)); g_mutex_init(&devc->rw_mutex); devc->current_limit = 0; - devc->voltage_limit = 0; + devc->uvc_threshold = 0; devc->running = FALSE; devc->load_activated = FALSE; sr_sw_limits_init(&devc->limits); @@ -98,7 +98,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) if (ret < 0) { sr_warn("Could not receive message!"); ret = SR_ERR; - } else if (0 == ret) { + } else if (ret == 0) { sr_warn("No message received!"); ret = SR_ERR; } @@ -147,7 +147,7 @@ static int config_get(uint32_t key, GVariant **data, *data = g_variant_new_double(fvalue); return ret; case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD: - ret = ebd_get_voltage_limit(sdi, &fvalue); + ret = ebd_get_uvc_threshold(sdi, &fvalue); if (ret == SR_OK) *data = g_variant_new_double(fvalue); return ret; @@ -180,7 +180,7 @@ static int config_set(uint32_t key, GVariant *data, value = g_variant_get_double(data); if (value < 0.0 || value > 21.0) return SR_ERR_ARG; - return ebd_set_voltage_limit(sdi, value); + return ebd_set_uvc_threshold(sdi, value); default: return SR_ERR_NA; } @@ -192,7 +192,8 @@ static int config_list(uint32_t key, GVariant **data, switch (key) { case SR_CONF_SCAN_OPTIONS: case SR_CONF_DEVICE_OPTIONS: - return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, + devopts); case SR_CONF_CURRENT_LIMIT: *data = std_gvar_min_max_step(0.0, 4.0, 0.001); break; @@ -231,9 +232,8 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi) if (sdi) { devc = sdi->priv; - if (devc->load_activated) { + if (devc->load_activated) ebd_loadtoggle(sdi->conn, devc); - } ebd_stop(sdi->conn, devc); } diff --git a/src/hardware/zketech-ebd-usb/protocol.c b/src/hardware/zketech-ebd-usb/protocol.c index 9751e910..980c8c04 100644 --- a/src/hardware/zketech-ebd-usb/protocol.c +++ b/src/hardware/zketech-ebd-usb/protocol.c @@ -24,10 +24,12 @@ /* Log a byte-array as hex values. */ static void log_buf(const char *message, uint8_t buf[], size_t count) { + size_t i; char buffer[count * 2 + 1]; - for (size_t j = 0; j < count; j++) - sprintf(&buffer[2 * j], "%02X", buf[j]); + for (i = 0; i < count; i++) { + sprintf(&buffer[2 * i], "%02X", buf[i]); + } buffer[count * 2] = 0; @@ -35,7 +37,8 @@ static void log_buf(const char *message, uint8_t buf[], size_t count) } /* Send a command to the device. */ -static int send_cmd(struct sr_serial_dev_inst *serial, uint8_t buf[], size_t count) +static int send_cmd(struct sr_serial_dev_inst *serial, uint8_t buf[], + size_t count) { int ret; @@ -46,7 +49,11 @@ static int send_cmd(struct sr_serial_dev_inst *serial, uint8_t buf[], size_t cou sr_err("Error sending command: %d.", ret); return ret; } - g_usleep(10000); // wait between bytes to prevent data loss at the receiving side + /* + * Wait between bytes to prevent data loss at the receiving + * side. + */ + g_usleep(10000); } return (ret == (int)count) ? SR_OK : SR_ERR; @@ -72,12 +79,14 @@ static void encode_value(float current, uint8_t *hi, uint8_t *lo, float divisor) /* Send updated configuration values to the load. */ static int send_cfg(struct sr_serial_dev_inst *serial, struct dev_context *devc) { - uint8_t send[] = { 0xfa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8 }; + uint8_t send[] = + { 0xfa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8 }; encode_value(devc->current_limit, &send[2], &send[3], 1000.0); - encode_value(devc->voltage_limit, &send[4], &send[5], 100.0); + encode_value(devc->uvc_threshold, &send[4], &send[5], 100.0); - send[8] = send[1] ^ send[2] ^ send[3] ^ send[4] ^ send[5] ^ send[6] ^ send[7]; + send[8] = send[1] ^ send[2] ^ send[3] ^ send[4] ^ send[5] + ^ send[6] ^ send[7]; return send_cmd(serial, send, 10); } @@ -85,25 +94,27 @@ static int send_cfg(struct sr_serial_dev_inst *serial, struct dev_context *devc) /* Send the init/connect sequence; drive starts sending voltage and current. */ SR_PRIV int ebd_init(struct sr_serial_dev_inst *serial, struct dev_context *devc) { - uint8_t init[] = { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 }; + uint8_t init[] = + { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 }; (void)devc; - int ret = send_cmd(serial, init, 10); - - return ret; + return send_cmd(serial, init, 10); } /* Start the load functionality. */ -SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial, struct dev_context *devc) +SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial, + struct dev_context *devc) { - uint8_t start[] = { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 }; int ret; + uint8_t start[] = + { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 }; encode_value(devc->current_limit, &start[2], &start[3], 1000.0); - encode_value(devc->voltage_limit, &start[4], &start[5], 100.0); + encode_value(devc->uvc_threshold, &start[4], &start[5], 100.0); - start[8] = start[1] ^ start[2] ^ start[3] ^ start[4] ^ start[5] ^ start[6] ^ start[7]; + start[8] = start[1] ^ start[2] ^ start[3] ^ start[4] ^ start[5] + ^ start[6] ^ start[7]; sr_info("Activating load"); ret = send_cmd(serial, start, 10); @@ -111,7 +122,7 @@ SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial, struct dev_context return ret; sr_dbg("current limit: %.03f", devc->current_limit); - sr_dbg("under-voltage threshold: %.02f", devc->voltage_limit); + sr_dbg("under-voltage threshold: %.02f", devc->uvc_threshold); if (ebd_current_is0(devc)) return SR_OK; @@ -119,42 +130,48 @@ SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial, struct dev_context } /* Toggle the load functionality. */ -SR_PRIV int ebd_loadtoggle(struct sr_serial_dev_inst *serial, struct dev_context *devc) +SR_PRIV int ebd_loadtoggle(struct sr_serial_dev_inst *serial, + struct dev_context *devc) { - int ret; - uint8_t toggle[] = { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 }; + uint8_t toggle[] = + { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 }; (void) devc; sr_info("Toggling load"); - ret = send_cmd(serial, toggle, 10); - - return ret; + return send_cmd(serial, toggle, 10); } /* Stop the drive. */ SR_PRIV int ebd_stop(struct sr_serial_dev_inst *serial, struct dev_context *devc) { - uint8_t stop[] = { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 }; - int ret; + uint8_t stop[] = + { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 }; (void) devc; - ret = send_cmd(serial, stop, 10); - - return ret; + return send_cmd(serial, stop, 10); } -/** Receive a complete message - * @param[in] serial Serial port from which to read the packet - * @param[in] length Buffer length - * @param[out] buf Buffer to write packet to - * @return packet length (0 = timeout, -1 = error) +/** + * Receive a complete message. + * + * @param[in] serial Serial port from which to read the packet + * @param[in] length Buffer length + * @param[out] buf Buffer to write packet to + * + * @return packet length (0 = timeout, -1 = error) */ -SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, int length, uint8_t *buf) +SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, size_t length, + uint8_t *buf) { - // check parameters - if (NULL == serial) { + int ret; + gboolean message_complete; + size_t turn, max_turns; + size_t message_length; + + /* Check parameters. */ + if (serial == NULL) { sr_err("Serial device to receive packet missing."); return -1; } @@ -162,56 +179,63 @@ SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, int length, uint sr_err("Packet buffer not large enough."); return -1; } - if (NULL == buf) { + if (buf == NULL) { sr_err("Packet buffer missing."); return -1; } - int ret; - gboolean message_complete = FALSE; - int message_length = 0; - unsigned int turn = 0; - const unsigned int TURNS = 200; + message_complete = FALSE; + turn = 0; + max_turns = 200; + message_length = 0; buf[message_length] = 0; - // try to read data - while (!message_complete && turn < TURNS) { - // wait for header byte + + /* Try to read data. */ + while (!message_complete && turn < max_turns) { + /* Wait for header byte. */ message_length = 0; - while (MSG_FRAME_BEGIN != buf[0] && turn < TURNS) { - ret = serial_read_blocking(serial, &buf[0], 1, serial_timeout(serial, 1)); + while (buf[0] != MSG_FRAME_BEGIN && turn < max_turns) { + ret = serial_read_blocking(serial, &buf[0], 1, + serial_timeout(serial, 1)); if (ret < 0) { sr_err("Error %d reading byte.", ret); return ret; - } else if (1 == ret) { - if (MSG_FRAME_BEGIN != buf[message_length]) { - sr_warn("Not frame begin byte %02x received", buf[message_length]); + } else if (ret == 1) { + if (buf[message_length] != MSG_FRAME_BEGIN) { + sr_warn("Not frame begin byte %02x received", + buf[message_length]); } else { - sr_dbg("Message header received: %02x", buf[message_length]); + sr_dbg("Message header received: %02x", + buf[message_length]); message_length += ret; } } turn++; } - // read until end byte - while (MSG_FRAME_END != buf[message_length - 1] && message_length < length && turn < TURNS) { - ret = serial_read_blocking(serial, &buf[message_length], 1, serial_timeout(serial, 1)); + /* Read until end byte. */ + while (buf[message_length - 1] != MSG_FRAME_END + && message_length < length && turn < max_turns) { + + ret = serial_read_blocking(serial, &buf[message_length], + 1, serial_timeout(serial, 1)); if (ret < 0) { sr_err("Error %d reading byte.", ret); return ret; - } else if (1 == ret) { - if (MSG_FRAME_BEGIN == buf[message_length]) { + } else if (ret == 1) { + if (buf[message_length] == MSG_FRAME_BEGIN) { sr_warn("Frame begin before end received"); message_length = 1; } else { - sr_dbg("Message data received: %02x", buf[message_length]); + sr_dbg("Message data received: %02x", + buf[message_length]); message_length += ret; } } turn++; } - // verify frame - if (turn < TURNS) { - if (MSG_FRAME_END == buf[message_length - 1]) { + /* Verify frame. */ + if (turn < max_turns) { + if (buf[message_length - 1] == MSG_FRAME_END) { message_complete = TRUE; sr_dbg("Message end received"); } else { @@ -224,7 +248,7 @@ SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, int length, uint if (message_complete && message_length > 2) { ret = message_length; - } else if (turn >= TURNS) { + } else if (turn >= max_turns) { ret = 0; } else { ret = -1; @@ -233,7 +257,7 @@ SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, int length, uint } static void ebd_send_value(const struct sr_dev_inst *sdi, struct sr_channel *ch, - float value, enum sr_mq mq, enum sr_unit unit, int digits) + float value, enum sr_mq mq, enum sr_unit unit, int digits) { struct sr_datafeed_packet packet; struct sr_datafeed_analog analog; @@ -260,12 +284,11 @@ SR_PRIV int ebd_receive_data(int fd, int revents, void *cb_data) struct sr_dev_inst *sdi; struct dev_context *devc; struct sr_serial_dev_inst *serial; - struct sr_datafeed_packet packet; - struct sr_datafeed_analog analog; - struct sr_analog_encoding encoding; - struct sr_analog_meaning meaning; - struct sr_analog_spec spec; - float voltage, voltage_dp, voltage_dm, current, current_limit, voltage_limit; + float current, current_limit; + float voltage, voltage_dp, voltage_dm, uvc_threshold; + uint8_t reply[MSG_MAX_LEN]; + int ret, i; + uint8_t checksum; (void)revents; (void)fd; @@ -278,83 +301,82 @@ SR_PRIV int ebd_receive_data(int fd, int revents, void *cb_data) serial = sdi->conn; current_limit = devc->current_limit; - voltage_limit = devc->voltage_limit; + uvc_threshold = devc->uvc_threshold; - uint8_t reply[MSG_MAX_LEN]; - int ret = ebd_read_message(serial, MSG_MAX_LEN, reply); + ret = ebd_read_message(serial, MSG_MAX_LEN, reply); /* Tests for correct message. */ - if (-1 == ret) { + if (ret == -1) { sr_err("Can't receive messages"); return SR_ERR; - } else if (0 == ret) { + } else if (ret == 0) { sr_err("No messages received"); devc->running = FALSE; return 0; - } else if (ret != 19 || (reply[1] != 0x00 && reply[1] != 0x0a && reply[1] != 0x64 && reply[1] != 0x6e)) { + } else if (ret != 19 || + (reply[1] != 0x00 && reply[1] != 0x0a && reply[1] != 0x64 && reply[1] != 0x6e)) { + sr_info("Not measurement message received"); return ret; } /* Verify checksum */ - uint8_t checksum = 0; - for (int i = 1; i < ret - 1; i++) { + checksum = 0; + for (i = 1; i < ret - 1; i++) { checksum ^= reply[i]; } - if (0 != checksum) { + if (checksum != 0) { sr_warn("Invalid checksum"); - return ret; /* Don't exit on wrong checksum, the device can recover */ + /* Don't exit on wrong checksum, the device can recover */ + return ret; } devc->running = TRUE; - if (0x00 == reply[1] || 0x64 == reply[1]) { + if (reply[1] == 0x00 || reply[1] == 0x64) devc->load_activated = FALSE; - } else if (0x0a == reply[1] || 0x6e == reply[1]) { + else if (reply[1] == 0x0a || reply[1] == 0x6e) devc->load_activated = TRUE; - } /* Calculate values. */ current = decode_value(reply[2], reply[3], 10000.0); voltage = decode_value(reply[4], reply[5], 1000.0); voltage_dp = decode_value(reply[6], reply[7], 1000.0); voltage_dm = decode_value(reply[8], reply[9], 1000.0); - if (0x0a == reply[1]) { + if (reply[1] == 0x0a) { current_limit = decode_value(reply[10], reply[11], 1000.0); - voltage_limit = decode_value(reply[12], reply[13], 100.0); + uvc_threshold = decode_value(reply[12], reply[13], 100.0); } sr_dbg("VBUS current %.04f A", current); sr_dbg("VBUS voltage %.03f V", voltage); sr_dbg("D+ voltage %.03f V", voltage_dp); sr_dbg("D- voltage %.03f V", voltage_dm); - if (0x0a == reply[1]) { + if (reply[1] == 0x0a) { sr_dbg("Current limit %.03f A", current_limit); - sr_dbg("Voltage limit %.03f A", voltage_limit); + sr_dbg("UVC threshold %.03f V", uvc_threshold); } - // update load state + /* Update load state. */ if (devc->load_activated && ebd_current_is0(devc)) { ebd_loadtoggle(serial, devc); } else if (!devc->load_activated && !ebd_current_is0(devc)) { ebd_loadstart(serial, devc); - } else if (devc->load_activated && (current_limit != devc->current_limit || voltage_limit != devc->voltage_limit)) { - sr_dbg("Adjusting limit from %.03f A %.03f V to %.03f A %.03f V", current_limit, voltage_limit, devc->current_limit, devc->voltage_limit); + } else if (devc->load_activated && + (current_limit != devc->current_limit || uvc_threshold != devc->uvc_threshold)) { + + sr_dbg("Adjusting limit from %.03f A %.03f V to %.03f A %.03f V", + current_limit, uvc_threshold, devc->current_limit, + devc->uvc_threshold); send_cfg(serial, devc); } /* Begin frame. */ std_session_send_df_frame_begin(sdi); - sr_analog_init(&analog, &encoding, &meaning, &spec, 4); - - packet.type = SR_DF_ANALOG; - packet.payload = &analog; - analog.num_samples = 1; - /* Values */ - ebd_send_value(sdi, sdi->channels->data, current, + ebd_send_value(sdi, sdi->channels->data, voltage, SR_MQ_VOLTAGE, SR_UNIT_VOLT, 3); - ebd_send_value(sdi, sdi->channels->next->data, voltage, + ebd_send_value(sdi, sdi->channels->next->data, current, SR_MQ_CURRENT, SR_UNIT_AMPERE, 4); ebd_send_value(sdi, sdi->channels->next->next->data, voltage_dp, SR_MQ_VOLTAGE, SR_UNIT_VOLT, 3); @@ -427,7 +449,7 @@ SR_PRIV int ebd_set_current_limit(const struct sr_dev_inst *sdi, float current) return ret; } -SR_PRIV int ebd_get_voltage_limit(const struct sr_dev_inst *sdi, float *voltage) +SR_PRIV int ebd_get_uvc_threshold(const struct sr_dev_inst *sdi, float *voltage) { struct dev_context *devc; @@ -435,13 +457,13 @@ SR_PRIV int ebd_get_voltage_limit(const struct sr_dev_inst *sdi, float *voltage) return SR_ERR; g_mutex_lock(&devc->rw_mutex); - *voltage = devc->voltage_limit; + *voltage = devc->uvc_threshold; g_mutex_unlock(&devc->rw_mutex); return SR_OK; } -SR_PRIV int ebd_set_voltage_limit(const struct sr_dev_inst *sdi, float voltage) +SR_PRIV int ebd_set_uvc_threshold(const struct sr_dev_inst *sdi, float voltage) { struct dev_context *devc; int ret; @@ -450,15 +472,15 @@ SR_PRIV int ebd_set_voltage_limit(const struct sr_dev_inst *sdi, float voltage) return SR_ERR; g_mutex_lock(&devc->rw_mutex); - devc->voltage_limit = voltage; + devc->uvc_threshold = voltage; if (!devc->running) { - sr_dbg("Setting voltage limit later."); + sr_dbg("Setting uvc threshold later."); g_mutex_unlock(&devc->rw_mutex); return SR_OK; } - sr_dbg("Setting voltage limit to %fV.", voltage); + sr_dbg("Setting uvc threshold to %fV.", voltage); if (devc->load_activated) { if (ebd_current_is0(devc)) { diff --git a/src/hardware/zketech-ebd-usb/protocol.h b/src/hardware/zketech-ebd-usb/protocol.h index 0d8ebecf..d0c8389c 100644 --- a/src/hardware/zketech-ebd-usb/protocol.h +++ b/src/hardware/zketech-ebd-usb/protocol.h @@ -36,26 +36,31 @@ struct dev_context { struct sr_sw_limits limits; GMutex rw_mutex; float current_limit; - float voltage_limit; + float uvc_threshold; gboolean running; gboolean load_activated; }; /* Communication via serial. */ -SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, int length, uint8_t *buf); +SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, size_t length, + uint8_t *buf); /* Commands. */ -SR_PRIV int ebd_init(struct sr_serial_dev_inst *serial, struct dev_context *devc); -SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial, struct dev_context *devc); +SR_PRIV int ebd_init(struct sr_serial_dev_inst *serial, + struct dev_context *devc); +SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial, + struct dev_context *devc); SR_PRIV int ebd_receive_data(int fd, int revents, void *cb_data); -SR_PRIV int ebd_stop(struct sr_serial_dev_inst *serial, struct dev_context *devc); -SR_PRIV int ebd_loadtoggle(struct sr_serial_dev_inst *serial, struct dev_context *devc); +SR_PRIV int ebd_stop(struct sr_serial_dev_inst *serial, + struct dev_context *devc); +SR_PRIV int ebd_loadtoggle(struct sr_serial_dev_inst *serial, + struct dev_context *devc); /* Configuration. */ SR_PRIV int ebd_get_current_limit(const struct sr_dev_inst *sdi, float *current); SR_PRIV int ebd_set_current_limit(const struct sr_dev_inst *sdi, float current); -SR_PRIV int ebd_get_voltage_limit(const struct sr_dev_inst *sdi, float *voltage); -SR_PRIV int ebd_set_voltage_limit(const struct sr_dev_inst *sdi, float voltage); +SR_PRIV int ebd_get_uvc_threshold(const struct sr_dev_inst *sdi, float *voltage); +SR_PRIV int ebd_set_uvc_threshold(const struct sr_dev_inst *sdi, float voltage); SR_PRIV gboolean ebd_current_is0(struct dev_context *devc); #endif -- 2.30.2