+ size_t i;
+ char buffer[count * 2 + 1];
+
+ for (i = 0; i < count; i++) {
+ sprintf(&buffer[2 * i], "%02X", buf[i]);
+ }
+
+ buffer[count * 2] = 0;
+
+ sr_dbg("%s: %s [%zu bytes]", message, buffer, count);
+}
+
+/* Send a command to the device. */
+static int send_cmd(struct sr_serial_dev_inst *serial, uint8_t buf[],
+ size_t count)
+{
+ int ret;
+
+ log_buf("Sending", buf, count);
+ for (size_t byte = 0; byte < count; byte++) {
+ ret = serial_write_blocking(serial, &buf[byte], 1, 0);
+ if (ret < 0) {
+ sr_err("Error sending command: %d.", ret);
+ return ret;
+ }
+ /*
+ * Wait between bytes to prevent data loss at the receiving
+ * side.
+ */
+ g_usleep(10000);
+ }
+
+ return (ret == (int)count) ? SR_OK : SR_ERR;
+}
+
+/* Decode high byte and low byte into a float. */
+static float decode_value(uint8_t hi, uint8_t lo, float divisor)
+{
+ return ((float)hi * 240.0 + (float)lo) / divisor;
+}
+
+/* Encode a float into high byte and low byte. */
+static void encode_value(float current, uint8_t *hi, uint8_t *lo, float divisor)
+{
+ int value;
+
+ value = (int)(current * divisor);
+ sr_dbg("Value %d %d %d", value, value / 240, value % 240);
+ *hi = value / 240;
+ *lo = value % 240;
+}
+
+/* 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 };
+
+ encode_value(devc->current_limit, &send[2], &send[3], 1000.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];
+
+ return send_cmd(serial, send, 10);
+}
+
+/* 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 };
+
+ (void)devc;
+
+ 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)
+{
+ 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->uvc_threshold, &start[4], &start[5], 100.0);
+
+ 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);
+ if (ret)
+ return ret;
+
+ sr_dbg("current limit: %.03f", devc->current_limit);
+ sr_dbg("under-voltage threshold: %.02f", devc->uvc_threshold);
+ if (ebd_current_is0(devc))
+ return SR_OK;
+
+ return ret;
+}
+
+/* Toggle the load functionality. */
+SR_PRIV int ebd_loadtoggle(struct sr_serial_dev_inst *serial,
+ struct dev_context *devc)
+{
+ uint8_t toggle[] =
+ { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 };
+
+ (void) devc;
+
+ sr_info("Toggling load");
+ 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 };
+
+ (void) devc;
+
+ 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)
+ */
+SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, size_t length,
+ uint8_t *buf)
+{
+ 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;
+ }
+ if (length < 3) {
+ sr_err("Packet buffer not large enough.");
+ return -1;
+ }
+ if (buf == NULL) {
+ sr_err("Packet buffer missing.");
+ return -1;
+ }
+
+ message_complete = FALSE;
+ turn = 0;
+ max_turns = 200;
+ message_length = 0;
+ buf[message_length] = 0;
+
+ /* Try to read data. */
+ while (!message_complete && turn < max_turns) {
+ /* Wait for header byte. */
+ message_length = 0;
+ 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 (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]);
+ message_length += ret;
+ }
+ }
+ turn++;
+ }
+ /* 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 (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]);
+ message_length += ret;
+ }
+ }
+ turn++;
+ }
+ /* Verify frame. */
+ if (turn < max_turns) {
+ if (buf[message_length - 1] == MSG_FRAME_END) {
+ message_complete = TRUE;
+ sr_dbg("Message end received");
+ } else {
+ sr_warn("Frame end not received");
+ }
+ } else {
+ sr_warn("Invalid data and timeout");
+ }
+ }
+
+ if (message_complete && message_length > 2) {
+ ret = message_length;
+ } else if (turn >= max_turns) {
+ ret = 0;
+ } else {
+ ret = -1;
+ }
+ return ret;
+}
+
+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)
+{
+ 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;
+
+ sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
+ analog.meaning->channels = g_slist_append(NULL, ch);
+ analog.num_samples = 1;
+ analog.data = &value;
+ analog.meaning->mq = mq;
+ analog.meaning->unit = unit;
+ analog.meaning->mqflags = SR_MQFLAG_DC;
+
+ packet.type = SR_DF_ANALOG;
+ packet.payload = &analog;
+ sr_session_send(sdi, &packet);
+ g_slist_free(analog.meaning->channels);
+}
+
+SR_PRIV int ebd_receive_data(int fd, int revents, void *cb_data)
+{
+ struct sr_dev_inst *sdi;