// @file sns_dae.proto
//
// Defines all high-level messages sent to/from the Nano sensor environment
//
// Copyright (c) 2017,2018 Qualcomm Technologies, Inc.
// All Rights Reserved.
// Confidential and Proprietary - Qualcomm Technologies, Inc.
syntax = "proto2";
import "nanopb.proto";

import "sns_std_type.proto";
import "sns_interrupt.proto";
import "sns_async_com_port.proto";


enum sns_dae_msgid {
  option (nanopb_enumopt).long_names = false;

  // to initialize the nano sensor before it can be used
  SNS_DAE_MSGID_SNS_DAE_SET_STATIC_CONFIG    = 600;

  // provides nano sensor with S4S config info after the initial S4S synch period
  SNS_DAE_MSGID_SNS_DAE_S4S_DYNAMIC_CONFIG   = 601;

  // to start nano sensor streaming
  SNS_DAE_MSGID_SNS_DAE_SET_STREAMING_CONFIG = 602;

  // no message body
  // to flush FIFO data before changing ODR and/or WM
  // no data event will be generated
  SNS_DAE_MSGID_SNS_DAE_FLUSH_HW             = 603;

  // no message body
  // to request nano sensor to generate data event from the available data
  // HW flush will NOT be done
  SNS_DAE_MSGID_SNS_DAE_FLUSH_DATA_EVENTS    = 604;

  // no message body
  // to stop nano sensor streaming
  SNS_DAE_MSGID_SNS_DAE_PAUSE_SAMPLING       = 605;

  // no message body
  // to stop S4S schedule
  SNS_DAE_MSGID_SNS_DAE_PAUSE_S4S_SCHED      = 606;

  // response from DAE sensor for each request
  SNS_DAE_MSGID_SNS_DAE_RESP                 = 800;

  // message from DAE sensor to deliver all available data
  SNS_DAE_MSGID_SNS_DAE_DATA_EVENT           = 1200;

  // message from DAE sensor to deliver unhandle interrupts
  SNS_DAE_MSGID_SNS_DAE_INTERRUPT_EVENT      = 1201;
}

enum sns_dae_timestamp_type {
  // Unknown timestamp type
  SNS_DAE_TIMESTAMP_TYPE_UNKNOWN = 0;

  // The timestamp corresponds to an interrupt. Timestamp captured with hardware
  // timestamping engine
  SNS_DAE_TIMESTAMP_TYPE_HW_IRQ = 1;

  // The timestamp corresponds to an interrupt. Timestamp captured with software
  // within the interrupt handler
  SNS_DAE_TIMESTAMP_TYPE_SW_IRQ = 2;

  // The timestamp is the current system time when the data was read. The data
  // was read due to a system timer firing (for polling & S4S sensors)
  SNS_DAE_TIMESTAMP_TYPE_TIMER = 3;

  // The timestamp is the current system time when the data was read. The data
  // was read due to a flush command.
  SNS_DAE_TIMESTAMP_TYPE_SYSTEM_TIME = 4;
}

// S4S static information
//
// This contains S4S information which does not change during the life of the S4S sensor
message sns_s4s_static_info {
  // Register address of the S4S ST command
  required int32   st_reg_addr = 1;

  // Optional data byte which needs to be sent along with ST
  optional int32   st_reg_data = 2;

  // Register address of the S4S DT command
  required int32   dt_reg_addr = 3;
}


// Triaxial sensor input to output axis conversion
message sensor_to_phone_conversion {

  // input axis index (x=0, y=1, z=2)
  required uint32 ipaxis = 1;

  // output axis index the above input axis maps to
  required uint32 opaxis = 2;

  // whether the input needs to be negated when
  // translating to the output axis
  required bool invert = 3;
}

// Accel static informtaion
//
// Accel configuration setting that is not expected to change at run time
message sns_accel_static_info {

  // Factory calibration: offset, in g's (DEPRECATED - moved to sns_accel_dynamic_info)
  repeated float offset_cal = 1 [(nanopb).max_count = 3, deprecated = true];

  // Factory calibration: scale factor (DEPRECATED - moved to sns_accel_dynamic_info)
  repeated float scale_cal = 2[(nanopb).max_count = 3, deprecated = true];

  // range in G
  required int32 accel_range = 3;

  // axis map to convert from sensor to phone 
  repeated sensor_to_phone_conversion axis_map = 4 [(nanopb).max_count = 3];
}


// Accel dynamic informtaion
//
// This data may be updated each time the accel configuration changes
message sns_accel_dynamic_info {
  // ODR
  required float odr = 1;

  // How many initial samples after ODR change are invalid
  required uint32 num_initial_invalid_samples = 2;

  // Factory calibration: offset, in g's
  repeated float offset_cal = 3 [(nanopb).max_count = 3];

  // Factory calibration: scale factor
  repeated float scale_cal = 4 [(nanopb).max_count = 3];

}

// Polling config info
message sns_polling_config_info {

  // Absolute time, in 19.2MHz ticks since boot, at which to poll the sensor
  required fixed64 polling_offset = 1;

  // interval between polling
  required fixed64 polling_interval_ticks = 2;
}

// Sensor static configuration
//
// This data may not change for the life of the sensor, and should be configured
// at initilization/boot time.
message sns_dae_set_static_config {

  // name of the function table for the nano version of the physical sensor
  required string func_table_name = 1 [(nanopb).max_size = 32];

  // Operational mode of the sensor:
  // 0 --> Polling
  // 1 --> Interrupt
  // 2 --> IBI (I3C In Band Interrupt)
  required int32 interrupt = 2;

  // x/y/z axis map for this sensor (DEPRECATED - moved to sns_accel_static_info)
  repeated int32           axis_map = 3 [(nanopb).max_count = 3, deprecated = true];

  // irq_config required if interrupt == 1
  optional sns_interrupt_req     irq_config = 4;

  // ibi_config required if interrupt == 2
  optional sns_ibi_req           ibi_config = 8;

  // s4s_config required if sensor supports S4S
  optional sns_s4s_static_info s4s_config = 5;

  // port configuration
  required sns_async_com_port_config ascp_config = 6;

  // Only for accel sensors
  optional sns_accel_static_info accel_info = 7;
}

// S4S dynamic information
//
// This contains S4S information which may change with S4S configuration
message sns_dae_s4s_dynamic_config {

  // Absolute time, in 19.2MHz ticks since boot, of the start of an S4S T_PH period
  required fixed64 ideal_sync_offset = 1;

  // Time, in 19.2MHz ticks, in one S4S T_PH period
  required fixed64 sync_interval = 2;

  // S4S resolution ratio (RR), as defined in the S4S spec. The final S4S RR will
  // be 2^-(11+resolution_ratio).
  required int32  resolution_ratio = 3;

  // Minimum time, in 19.2MHz ticks, after the S4S T_PH period to send ST/DT
  // messages. Will depend on clock accuracy of the sensor, as well as current
  // sync_interval
  required int32  st_delay = 4;
}

message sns_dae_set_streaming_config {

  // minimum number of samples between data events; may still receive fewer samples
  required uint32 dae_watermark = 1;

  // discard data older than this limit (relative to current system time)
  optional fixed64 data_age_limit_ticks = 2;

  // required for polling sensors
  optional sns_polling_config_info polling_config = 3;

  // Only for accel sensors
  optional sns_accel_dynamic_info accel_info = 4;

  // Expected number of bytes read from sensor for each call to the DAE environment
  // "get_data" driver function.
  // If not included, default value is 100 bytes.
  optional uint32 expected_get_data_bytes = 5;
}

message sns_dae_resp {
  // Message ID this error code is associated with
  required int32              msg_id = 1;

  // result of the request associated with this response
  required sns_std_error      err    = 2;
}

message sns_dae_data_event {
  // Timestamp: For interrupt based sensors, this is the timestamp of the IRQ.
  // For polling sensors, this is the timestamp of when get_data() was called
  required fixed64 timestamp = 1;

  // Timestamp type
  optional sns_dae_timestamp_type timestamp_type = 3;

  // Format of the sensor_data is vendor specific, and up to the nano sensor
  // implementation.
  required bytes  sensor_data = 2;
}

message sns_dae_interrupt_event {
  // timestamp of the IRQ.
  required fixed64 timestamp = 1;

  // Format of registers are vendor specific, and up to the nano sensor implementation.
  required bytes registers = 2 [(nanopb).max_size = 8];
}

