// Copyright 2024 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

syntax = "proto3";

package chromiumos.test.lab.api.passport;

import "google/protobuf/duration.proto";

option go_package = "go.chromium.org/chromiumos/config/go/test/lab/api/passport";

service UsbTesterService {
  // Probe all testers connected to the host device.
  rpc GetTesters(GetTestersRequest) returns (GetTestersReply) {}

  // Get the value of a certain capability, eg: data role, power role etc ...
  rpc GetTesterCapability(GetUsbTesterCapabilityRequest)
      returns (GetUsbTesterCapabilityReply) {}

  // Set the value of a certain capability, eg: data role, power role etc ...
  rpc SetTesterCapability(SetUsbTesterCapabilityRequest)
      returns (SetUsbTesterCapabilityReply) {}

  // Get the display port alternate mode information.
  rpc GetDpInfo(GetDpInfoRequest) returns (GetDpInfoReply) {}

  // Get the power delivery objects
  rpc GetPdos(GetPdosRequest) returns (GetPdosReply) {}

  // Simulate the physical disconnect and reconnect of the cable between the
  // tester and the DUT.
  rpc ReplugCable(DoCableReplugRequest) returns (DoCableReplugReply) {}

  // This method is used to do a hard reset/power cycle.
  rpc HardResetTester(HardResetTesterRequest) returns (HardResetTesterReply) {}

  // This method is used to issue power delivery resets.
  rpc ResetPd(ResetPdRequest) returns (ResetPdReply) {}

  // This method is used to open the serial of the USB tester being used.
  rpc OpenTester(OpenTesterRequest) returns (OpenTesterReply) {}

  // This method is used to close the serial of the USB tester being used.
  rpc CloseTester(CloseTesterRequest) returns (CloseTesterReply) {}

  // This method is used to get the active test port on the testing device.
  rpc GetActivePort(GetActivePortRequest) returns (GetActivePortReply) {}

  // This method is used to set the active test port on the testing device.
  rpc SetActivePort(SetActivePortRequest) returns (SetActivePortReply) {}

  // This method is used to load an EDID.
  rpc LoadEdid(LoadEdidRequest) returns (LoadEdidReply) {}

  // This method is used send a VDM HPDs
  rpc SendVdmHpd(SendVdmHpdRequest) returns (SendVdmHpdReply) {}

  // Simulate a key press. ATM this will simulate the "G" key press.
  rpc SimulateKeyPress(SimulateKeyPressRequest)
      returns (SimulateKeyPressReply) {}
}

// Request to do hard reset.
message HardResetTesterRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;
}

// Reply message for the HardResetTesterReply message
message HardResetTesterReply {
  // Error code indicating the success or failure of the open operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;
  // Human-readable error message providing more details about any errors
  // encountered.
  string error_msg = 2;
}

// Request to do a simulated cable replug.
message DoCableReplugRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;
}

// Reply message for the DoCableReplugRequest message
message DoCableReplugReply {
  // Error code indicating the success or failure of the open operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;
  // Human-readable error message providing more details about any errors
  // encountered.
  string error_msg = 2;
}

// Request to open the serial of the usb tester.
message OpenTesterRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;
}

// Reply to OpenTesterRequest.
message OpenTesterReply {
  // Error code indicating the success or failure of the open operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;
  // Human-readable error message providing more details about any errors
  // encountered.
  string error_msg = 2;
}

// Request to close the serial of the usb tester.
message CloseTesterRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;
}

// Reply to CloseTesterRequest.
message CloseTesterReply {
  // Error code indicating the success or failure of the close operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;
  // Human-readable error message providing more details about any errors
  // encountered.
  string error_msg = 2;
}

// Struct for representing a usb tester.
message UsbTester {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;

  // This field indicates the manufacturer of the usb tester.
  string name = 2;
}

// Request to retrieve a list of USB testers.
message GetTestersRequest {}

// Reply for GetTestersRequest.
message GetTestersReply {
  repeated UsbTester testers = 1;
}

// A USB tester capability is used to describe the attributes of a
// USB tester.Examples: power role, data rola, vbus voltage, etc ...
enum Capability {
  CAPABILITY_NOT_SET = 0;
  PIN_ASSIGNMENT = 1;
  USB_CHANNEL = 2;
  POWER_ROLE = 3;
  DATA_ROLE = 4;
  ACTIVE_CC = 5;
  CABLE_MODE = 6;
  INIT_PD_STATE = 7;
  CURRENT_LOAD = 9;
  SRC_PULL_UP = 10;
  SNK_PDO_COUNT = 11;
  SRC_PDO_COUNT = 12;
  VBUS_VOLTAGE = 13;
  VBUS_CURRENT = 14;
  VBUS_CURRENT_LANE = 15;
  GND_CURRENT_LANE = 16;
  VBUS_EPU_VOLTAGE = 17;
  VBUS_CC1 = 18;
  VBUS_CC2 = 19;
  VBUS_SBU1 = 20;
  VBUS_SBU2 = 21;
  DATA_SWAP_POLICY = 22;
  POWER_SWAP_POLICY = 23;
  VCONN_SWAP_POLICY = 24;
  CONSTRAINED_POWER = 25;
  POWER_DELIVERY = 26;
  DISPLAY_PORT_AM = 27;
  USB_PATH = 28;
  TRY_BEHAVIOUR = 29;
  NON_PD_CURRENT = 30;
}

enum TryBehaviour {
  TRY_BEHAVIOUR_NOT_SET = 0;
  TRY_SRC = 1;
  TRY_SNK = 2;
}

enum ActiveCc {
  ACTIVE_CC_NOT_SET = 0;
  CC1 = 1;
  CC2 = 2;
}

enum PinAssignment {
  PIN_ASSIGNMENT_NOT_SET = 0;
  C = 1;
  D = 2;
}

enum PowerRole {
  POWER_ROLE_NOT_SET = 0;
  SNK = 1;
  SRC = 2;
}

enum DataRole {
  DATA_ROLE_NOT_SET = 0;
  DATA_UFP = 1;
  DATA_DFP = 2;
}

enum UsbChannel {
  USB_CHANNEL_NOT_SET = 0;
  USB_2_HS = 1;
  USB_3_AND_2_HS = 2;
}

enum CableMode {
  CABLE_MODE_NOT_SET = 0;
  NORMAL = 1;
  ELEC_TEST = 2;
}

enum InitPdState {
  INIT_PD_STATE_NOT_SET = 0;
  PD_UFP = 1;
  PD_DFP = 2;
  PD_DRP = 3;
}

enum UsbPath {
  USB_PATH_NOT_SET = 0;
  USB_PATH_INTERNAL = 1;
  USB_PATH_EXTERNAL = 2;
}

enum PortState {
  PORT_STATE_NOT_SET = 0;
  PORT_STATE_ON = 1;
  PORT_STATE_OFF = 2;
}

enum VdmHpd {
  VDM_HPD_NOT_SET = 0;
  VDM_HPD_IRQ = 1;
}

// Request to get the value of a capability.
message GetUsbTesterCapabilityRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;

  // Indicates what capability the operation is targeting.
  Capability capability = 2;
}

// Reply of GetUsbTesterCapabilityRequest.
message GetUsbTesterCapabilityReply {
  // Error code indicating the success or failure of the open operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;

  // Human-readable error message providing more details about any errors
  // encountered.
  optional string error_msg = 2;

  // The value to be set. This can be either be a discrete value for well
  // defined capabilities (power role, data role etc .. ) or a just a plain
  // number for capabilities that can take on a big and not well defined range
  // of values.
  oneof value {
    ActiveCc active_cc = 3;
    PinAssignment pin_mode = 4;
    PowerRole power_role = 5;
    DataRole data_role = 6;
    UsbChannel usb_channel = 7;
    CableMode cable_mode = 8;
    InitPdState init_pd_state = 9;
    int64 non_descrete = 10;
    bool data_role_swap_policy = 13;
    bool power_role_swap_policy = 14;
    bool vconn_swap_policy = 15;
    bool constrained_power = 16;
    bool power_delivery = 17;
    bool display_port_am = 18;
    UsbPath usb_path = 19;
    TryBehaviour try_behaviour = 20;
  }
}

message SetUsbTesterCapabilityRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;

  // Specifies the delay before doing the operation. This is optional. If left
  // empty it will default to 0.
  optional google.protobuf.Duration delay = 2;
  // Specifies the max duration of the operation. This is optional. If left
  // empty it will default to 0.
  optional google.protobuf.Duration timeout = 3;

  // Indicate what capability the operation is targeting.
  Capability capability = 4;

  // The value to be set. This can be either be a discrete value for well
  // defined capabilities (power role, data role etc .. ) or a just a plain
  // number for capabilities that can take on a big and not well defined range
  // of values.
  oneof value {
    ActiveCc active_cc = 5;
    PinAssignment pin_mode = 6;
    PowerRole power_role = 7;
    DataRole data_role = 8;
    UsbChannel usb_channel = 9;
    CableMode cable_mode = 10;
    InitPdState init_pd_state = 11;
    int64 non_descrete = 12;
    bool data_role_swap_policy = 13;
    bool power_role_swap_policy = 14;
    bool vconn_swap_policy = 15;
    bool constrained_power = 16;
    bool power_delivery = 17;
    bool display_port_am = 18;
    UsbPath usb_path = 19;
    TryBehaviour try_behaviour = 20;
  }
}

message SetUsbTesterCapabilityReply {
  // Error code indicating the success or failure of the open operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 2;

  // Human-readable error message providing more details about any errors
  // encountered.
  optional string error_msg = 3;
}

enum DpLinkRate {
  DP_LINK_RATE_NOT_SET = 0;
  RBR = 1;
  HBR = 2;
  HBR2 = 3;
  HBR3 = 4;
}

enum DpColorDepth {
  DP_COLOR_DEPTH = 0;
  BIT6 = 1;
  BIT8 = 2;
  BIT10 = 3;
  BIT12 = 4;
  BIT16 = 5;
}

enum DpColorMode {
  DP_COLOR_MODE_NOT_SET = 0;
  RGB = 1;
  YCBCR444 = 2;
  YCBCR422 = 3;
  YCBCR420 = 4;
}

message GetDpInfoRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;

  // Specifies the delay before doing the operation. This is optional. If left
  // empty it will default to 0.
  optional google.protobuf.Duration delay = 2;

  // Specifies the max duration of the operation. This is optional. If left
  // empty it will default to 0.
  optional google.protobuf.Duration timeout = 3;
}

message GetDpInfoReply {
  // Error code indicating the success or failure of the operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;

  // Human-readable error message providing more details about any errors
  // encountered.
  optional string error_msg = 2;

  // Link rate of the DP link, rbr, hbr, etc ...
  optional DpLinkRate link_rate = 3;

  // Value corresponding to number of lanes.
  optional int32 lane_count = 4;

  // Value in HZ.
  optional int32 pixel_clock = 5;

  // Value in pixels.
  optional int32 h_active = 6;

  // Value in pixels.
  optional int32 v_active = 7;

  // Value in pixels.
  optional int32 h_total = 8;

  // Value in pixels.
  optional int32 v_total = 9;

  // Value in HZ.
  optional double framerate = 10;

  // DP link color mode.
  optional DpColorMode color_mode = 11;

  // DP link color depth.
  optional DpColorDepth color_depth = 12;

  // Value in kHZ.
  optional int32 audio_sample_rate = 13;
}

// Data used in a GetActivePort call.
message GetActivePortRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;
}

// Data returned as part of a GetActivePort call.
message GetActivePortReply {
  // Error code indicating the success or failure of the operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;

  // Human-readable error message providing more details about any errors
  // encountered.
  optional string error_msg = 2;

  // The id of the port that is active.
  uint32 port_id = 3;

  // The maximum number of ports on the device.
  uint32 max_num_ports = 4;

  // The state of the port (on/off)
  PortState state = 5;
}

// Data used in a SetActivePort call.
message SetActivePortRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;

  // The id of the port that we want to use for testing.
  uint32 port_id = 2;

  // The state of the port (on/off). If left unset the port state will be left
  // in untouched.
  PortState state = 3;
}

// Data returned as part of a SetActivePort call.
message SetActivePortReply {
  // Error code indicating the success or failure of the operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;

  // Human-readable error message providing more details about any errors
  // encountered.
  optional string error_msg = 2;
}

message LoadEdidRequest {
  // Id (or serial) of the usb tester for which the request is intended.
  string id = 1;

  // The edid to be loaded.
  bytes edid = 2;
}

message LoadEdidReply {
  // Error code indicating the success or failure of the operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;

  // Human-readable error message providing more details about any errors
  // encountered.
  optional string error_msg = 2;
}

// Defines a request message for resetting the Power Delivery (PD) communication
// on a USB tester device.
message ResetPdRequest {
  // Unique identifier (e.g., serial number) of the target USB tester.
  string id = 1;

  // Specifies the type of reset to perform.
  // If true, a soft reset of the PD communication will be initiated.
  // If false, a hard reset (potentially involving power cycling the PD PHY)
  // will be performed.
  bool soft = 2;
}

// Defines the reply message sent in response to a ResetPdRequest.
message ResetPdReply {
  // Numerical error code indicating the outcome of the reset operation.
  // A value of 0 signifies successful execution.
  // Non-zero values indicate specific error conditions.
  int64 err_code = 1;

  // Optional human-readable message providing additional context or details
  // in case an error occurred during the reset process. This field will
  // typically be populated when err_code is not 0.
  optional string error_msg = 2;
}

message GetPdosRequest {
  string id = 1;
}

message GetPdosReply {
  // Error code indicating the success or failure of the operation.
  // 0 indicates success, while other values represent specific errors.
  int64 err_code = 1;

  // Human-readable error message providing more details about any errors
  // encountered.
  optional string error_msg = 2;

  repeated int64 src_pdos = 3;

  repeated int64 snk_pdos = 4;
}

message SendVdmHpdRequest {
  // Unique identifier (e.g., serial number) of the target USB tester.
  string id = 1;

  // The type of vdm hpd to apply.
  VdmHpd vdm_hpd = 2;
}

// Defines the reply message sent in response to a SendVdmHpdRequest.
message SendVdmHpdReply {
  // Numerical error code indicating the outcome of the reset operation.
  // A value of 0 signifies successful execution.
  // Non-zero values indicate specific error conditions.
  int64 err_code = 1;

  // Optional human-readable message providing additional context or details
  // in case an error occurred during the reset process. This field will
  // typically be populated when err_code is not 0.
  optional string error_msg = 2;
}

message SimulateKeyPressRequest {
  // Unique identifier (e.g., serial number) of the target USB tester.
  string id = 1;
}

// Defines the reply message sent in response to a SimulateKeyPressRequest.
message SimulateKeyPressReply {
  // Numerical error code indicating the outcome of the reset operation.
  // A value of 0 signifies successful execution.
  // Non-zero values indicate specific error conditions.
  int64 err_code = 1;

  // Optional human-readable message providing additional context or details
  // in case an error occurred during the reset process. This field will
  // typically be populated when err_code is not 0.
  optional string error_msg = 2;
}
