+ /* We need to see the begin sequence first. When we receive `ESCAPE
+ * PKT_BEGIN', we set `package_buffer_pos' to zero to signal that
+ * the beginning of the package has been found. */
+
+ escape_flag = 0;
+ for (ssize_t i = 0; i < receive_buffer_length; i++) {
+ /* Check if previous byte was the escape byte. */
+ if (escape_flag == 1) {
+ escape_flag = 0;
+ /* escape escape = single escape */
+ if ((receive_buffer[i] == ESCAPE) && (package_buffer_pos >= 0)) {
+ package_buffer[package_buffer_pos] = ESCAPE;
+ package_buffer_pos++;
+ } else if (receive_buffer[i] == PKT_BEGIN) {
+ package_buffer_pos = 0;
+ } else if (receive_buffer[i] == PKT_END) {
+ end_flag = 1;
+ break;
+ } else {
+ DEBUG("ted plugin: Unknown escaped byte: %#x",
+ (unsigned int)receive_buffer[i]);
+ }
+ } else if (receive_buffer[i] == ESCAPE) {
+ escape_flag = 1;
+ }
+ /* if we are in a package add byte to buffer
+ * otherwise throw away */
+ else if (package_buffer_pos >= 0) {
+ package_buffer[package_buffer_pos] = receive_buffer[i];
+ package_buffer_pos++;
+ }
+ } /* for (i = 0; i < receive_buffer_length; i++) */
+ } /* while (end_flag == 0) */
+
+ /* Check for errors inside the loop. */
+ if ((end_flag == 0) || (package_buffer_pos != EXPECTED_PACKAGE_LENGTH))
+ return (-1);
+
+ /*
+ * Power is at positions 247 and 248 (LSB first) in [10kW].
+ * Voltage is at positions 251 and 252 (LSB first) in [.1V].
+ *
+ * Power is in 10 Watt steps
+ * Voltage is in volts
+ */
+ *ret_power = 10.0 * (double)((((int)package_buffer[248]) * 256) +
+ ((int)package_buffer[247]));
+ *ret_voltage = 0.1 * (double)((((int)package_buffer[252]) * 256) +
+ ((int)package_buffer[251]));
+
+ /* success */
+ return (0);