blob: 6402b3f1d2bb14a44ee17cf89f4c7d5674e4a951 [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
namespace battor {
// Control characters in the BattOr protocol.
enum BattOrControlByte : uint8_t {
// Indicates the start of a message in the protocol. All other instances of
// this byte must be escaped (with BATTOR_SPECIAL_BYTE_ESCAPE).
// Indicates the end of a message in the protocol. All other instances of
// this byte must be escaped (with BATTOR_SPECIAL_BYTE_ESCAPE).
// Indicates that the next byte should not be interpreted as a special
// character, but should instead be interpreted as itself.
// Types of BattOr messages that can be sent.
enum BattOrMessageType : uint8_t {
// Indicates a control message sent from the client to the BattOr to tell the
// BattOr to do something.
// Indicates a control message ack sent from the BattOr back to the client to
// signal that the BattOr received the control message.
// Indicates that the message contains Voltage and current measurements.
// TODO(charliea): Figure out what this is.
// Types of BattOr control messages that can be sent.
enum BattOrControlMessageType : uint8_t {
// Tells the BattOr to initialize itself.
// Tells the BattOr to reset itself.
// Tells the BattOr to run a self test.
// Tells the BattOr to send its EEPROM contents over the serial connection.
// Sets the current measurement's gain.
// Tells the BattOr to start taking samples and sending them over the
// connection.
// Tells the BattOr to start taking samples and storing them on its SD card.
// Tells the BattOr to start streaming the samples stored on its SD card over
// the connection.
// Tells the BattOr to send back the number of samples it's collected so far.
// This is used for syncing the clocks between the agent and the BattOr.
// The gain level for the BattOr to use.
enum BattOrGain : uint8_t { BATTOR_GAIN_LOW = 0, BATTOR_GAIN_HIGH };
// The data types below are packed to ensure byte-compatibility with the BattOr
// firmware.
#pragma pack(push, 1)
// See: BattOrMessageType::BATTOR_MESSAGE_TYPE_CONTROL above.
struct BattOrControlMessage {
BattOrControlMessageType type;
uint16_t param1;
uint16_t param2;
// See: BattOrMessageType::BATTOR_MESSAGE_TYPE_CONTROL_ACK above.
struct BattOrControlMessageAck {
BattOrControlMessageType type;
uint8_t param;
// TODO(charliea, aschulman): Write better descriptions for the EEPROM fields
// when we actually start doing the math to convert raw BattOr readings to
// accurate ones.
// The BattOr's EEPROM is persistent storage that contains information that we
// need in order to convert raw BattOr readings into accurate voltage and
// current measurements.
struct BattOrEEPROM {
uint8_t magic[4];
uint16_t version;
char serial_num[20];
uint32_t timestamp;
float r1;
float r2;
float r3;
float low_gain;
float low_gain_correction_factor;
float low_gain_correction_offset;
uint16_t low_gain_amppot;
float high_gain;
float high_gain_correction_factor;
float high_gain_correction_offset;
uint16_t high_gain_amppot;
uint32_t sd_sample_rate;
uint16_t sd_tdiv;
uint16_t sd_tovf;
uint16_t sd_filpot;
uint32_t uart_sr;
uint16_t uart_tdiv;
uint16_t uart_tovf;
uint16_t uart_filpot;
uint32_t crc32;
// The BattOrFrameHeader begins every frame containing BattOr samples.
struct BattOrFrameHeader {
// The number of frames that have preceded this one.
uint32_t sequence_number;
// The number of bytes of raw samples in this frame.
uint16_t length;
// A single BattOr sample. These samples are raw because they come directly from
// the BattOr's analog to digital converter and comprise only part of the
// equation to calculate meaningful voltage and current measurements.
struct RawBattOrSample {
int16_t voltage_raw;
int16_t current_raw;
// A single BattOr sample after timestamp assignment and conversion to unitful
// numbers.
struct BattOrSample {
double time_ms;
double voltage_mV;
double current_mA;
#pragma pack(pop)
} // namespace battor