/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

syntax = "proto2";

package android.os.statsd.threadnetwork;

import "frameworks/proto_logging/stats/atoms.proto";
import "frameworks/proto_logging/stats/atom_field_options.proto";

option java_package = "com.android.os.threadnetwork";
option java_multiple_files = true;

extend Atom {
    optional ThreadnetworkTelemetryDataReported threadnetwork_telemetry_data_reported = 738
    [(module) = "threadnetwork"];
    optional ThreadnetworkTopoEntryRepeated threadnetwork_topo_entry_repeated = 739
    [(module) = "threadnetwork"];
    optional ThreadnetworkDeviceInfoReported threadnetwork_device_info_reported = 740
    [(module) = "threadnetwork"];
}

// Thread Telemetry data definition.
message ThreadnetworkTelemetryDataReported {
  message WpanStats {
    optional int32 phy_rx = 1;
    optional int32 phy_tx = 2;
    optional int32 mac_unicast_rx = 3;
    optional int32 mac_unicast_tx = 4;
    optional int32 mac_broadcast_rx = 5;
    optional int32 mac_broadcast_tx = 6;
    optional int32 mac_tx_ack_req = 7;
    optional int32 mac_tx_no_ack_req = 8;
    optional int32 mac_tx_acked = 9;
    optional int32 mac_tx_data = 10;
    optional int32 mac_tx_data_poll = 11;
    optional int32 mac_tx_beacon = 12;
    optional int32 mac_tx_beacon_req = 13;
    optional int32 mac_tx_other_pkt = 14;
    optional int32 mac_tx_retry = 15;
    optional int32 mac_rx_data = 16;
    optional int32 mac_rx_data_poll = 17;
    optional int32 mac_rx_beacon = 18;
    optional int32 mac_rx_beacon_req = 19;
    optional int32 mac_rx_other_pkt = 20;
    optional int32 mac_rx_filter_whitelist = 21;
    optional int32 mac_rx_filter_dest_addr = 22;
    optional int32 mac_tx_fail_cca = 23;
    optional int32 mac_rx_fail_decrypt = 24;
    optional int32 mac_rx_fail_no_frame = 25;
    optional int32 mac_rx_fail_unknown_neighbor = 26;
    optional int32 mac_rx_fail_invalid_src_addr = 27;
    optional int32 mac_rx_fail_fcs = 28;
    optional int32 mac_rx_fail_other = 29;
    optional int32 ip_tx_success = 30;
    optional int32 ip_rx_success = 31;
    optional int32 ip_tx_failure = 32;
    optional int32 ip_rx_failure = 33;
    optional uint32 node_type = 34;
    optional uint32 channel = 35;
    optional int32 radio_tx_power = 36;
    optional float mac_cca_fail_rate = 37;
  }

  message WpanTopoFull {
    optional uint32 rloc16 = 1;
    optional uint32 router_id = 2;
    optional uint32 leader_router_id = 3;
    optional uint32 leader_rloc16 = 4; // replaced optional bytes leader_address = 5;
    optional uint32 leader_weight = 5;
    optional uint32 leader_local_weight = 6;
    optional uint32 preferred_router_id = 7;
    optional uint32 partition_id = 8;
    optional uint32 child_table_size = 9;
    optional uint32 neighbor_table_size = 10;
    optional int32 instant_rssi = 11;
    optional bool has_extended_pan_id = 12;
    optional bool is_active_br = 13;
    optional bool is_active_srp_server = 14;
    optional uint32 sum_on_link_prefix_changes = 15;
  }

  enum NodeType {
    NODE_TYPE_UNSPECIFIED = 0;
    NODE_TYPE_ROUTER = 1;
    NODE_TYPE_END = 2;
    NODE_TYPE_SLEEPY_END = 3;
    NODE_TYPE_MINIMAL_END = 4;

    NODE_TYPE_OFFLINE = 5;
    NODE_TYPE_DISABLED = 6;
    NODE_TYPE_DETACHED = 7;

    NODE_TYPE_NL_LURKER = 0x10;
    NODE_TYPE_COMMISSIONER = 0x20;
    NODE_TYPE_LEADER = 0x40;
  }

  message PacketsAndBytes {
    optional int64 packet_count = 1;
    optional int64 byte_count = 2;
  }

  message Nat64TrafficCounters {
    optional int64 ipv4_to_ipv6_packets = 1;
    optional int64 ipv4_to_ipv6_bytes = 2;
    optional int64 ipv6_to_ipv4_packets = 3;
    optional int64 ipv6_to_ipv4_bytes = 4;
  }

  message Nat64ProtocolCounters {
    optional Nat64TrafficCounters tcp = 1;
    optional Nat64TrafficCounters udp = 2;
    optional Nat64TrafficCounters icmp = 3;
  }

  message Nat64PacketCounters {
    optional int64 ipv4_to_ipv6_packets = 1;
    optional int64 ipv6_to_ipv4_packets = 2;
  }

  message Nat64ErrorCounters {
    optional Nat64PacketCounters unknown = 1;
    optional Nat64PacketCounters illegal_packet = 2;
    optional Nat64PacketCounters unsupported_protocol = 3;
    optional Nat64PacketCounters no_mapping = 4;
  }

  message BorderRoutingCounters {
    // The number of Router Advertisement packets received by otbr-agent on the
    // infra link
    optional int64 ra_rx = 1;

    // The number of Router Advertisement packets successfully transmitted by
    // otbr-agent on the infra link.
    optional int64 ra_tx_success = 2;

    // The number of Router Advertisement packets failed to transmit by
    // otbr-agent on the infra link.
    optional int64 ra_tx_failure = 3;

    // The number of Router Solicitation packets received by otbr-agent on the
    // infra link
    optional int64 rs_rx = 4;

    // The number of Router Solicitation packets successfully transmitted by
    // otbr-agent on the infra link.
    optional int64 rs_tx_success = 5;

    // The number of Router Solicitation packets failed to transmit by
    // otbr-agent on the infra link.
    optional int64 rs_tx_failure = 6;

    // The counters for inbound unicast packets
    optional PacketsAndBytes inbound_unicast = 7;

    // The counters for inbound multicast packets
    optional PacketsAndBytes inbound_multicast = 8;

    // The counters for outbound unicast packets
    optional PacketsAndBytes outbound_unicast = 9;

    // The counters for outbound multicast packets
    optional PacketsAndBytes outbound_multicast = 10;

    // The inbound and outbound NAT64 traffic through the border router
    optional Nat64ProtocolCounters nat64_protocol_counters = 11;

    // Error counters for NAT64 translator on the border router
    optional Nat64ErrorCounters nat64_error_counters = 12;
  }

  message SrpServerRegistrationInfo {
    // The number of active hosts/services registered on the SRP server.
    optional uint32 fresh_count = 1;

    // The number of hosts/services in 'Deleted' state on the SRP server.
    optional uint32 deleted_count = 2;

    // The sum of lease time in milliseconds of all active hosts/services on the
    // SRP server.
    optional uint64 lease_time_total_ms = 3;

    // The sum of key lease time in milliseconds of all active hosts/services on
    // the SRP server.
    optional uint64 key_lease_time_total_ms = 4;

    // The sum of remaining lease time in milliseconds of all active
    // hosts/services on the SRP server.
    optional uint64 remaining_lease_time_total_ms = 5;

    // The sum of remaining key lease time in milliseconds of all active
    // hosts/services on the SRP server.
    optional uint64 remaining_key_lease_time_total_ms = 6;
  }

  message SrpServerResponseCounters {
    // The number of successful responses
    optional uint32 success_count = 1;

    // The number of server failure responses
    optional uint32 server_failure_count = 2;

    // The number of format error responses
    optional uint32 format_error_count = 3;

    // The number of 'name exists' responses
    optional uint32 name_exists_count = 4;

    // The number of refused responses
    optional uint32 refused_count = 5;

    // The number of other responses
    optional uint32 other_count = 6;
  }

  enum SrpServerState {
    SRP_SERVER_STATE_UNSPECIFIED = 0;
    SRP_SERVER_STATE_DISABLED = 1;
    SRP_SERVER_STATE_RUNNING = 2;
    SRP_SERVER_STATE_STOPPED = 3;
  }

  // The address mode used by the SRP server
  enum SrpServerAddressMode {
    SRP_SERVER_ADDRESS_MODE_UNSPECIFIED = 0;
    SRP_SERVER_ADDRESS_MODE_UNICAST = 1;
    SRP_SERVER_ADDRESS_MODE_STATE_ANYCAST = 2;
  }

  enum UpstreamDnsQueryState {
    UPSTREAMDNS_QUERY_STATE_UNSPECIFIED = 0;
    UPSTREAMDNS_QUERY_STATE_ENABLED = 1;
    UPSTREAMDNS_QUERY_STATE_DISABLED = 2;
  }

  message SrpServerInfo {
    // The state of the SRP server
    optional SrpServerState state = 1;

    // Listening port number
    optional uint32 port = 2;
    // The address mode {unicast, anycast} of the SRP server
    optional SrpServerAddressMode address_mode = 3;

    // The registration information of hosts on the SRP server
    optional SrpServerRegistrationInfo hosts = 4;

    // The registration information of services on the SRP server
    optional SrpServerRegistrationInfo services = 5;

    // The counters of response codes sent by the SRP server
    optional SrpServerResponseCounters response_counters = 6;
  }

  message DnsServerResponseCounters {
    // The number of successful responses
    optional uint32 success_count = 1;

    // The number of server failure responses
    optional uint32 server_failure_count = 2;

    // The number of format error responses
    optional uint32 format_error_count = 3;

    // The number of name error responses
    optional uint32 name_error_count = 4;

    // The number of 'not implemented' responses
    optional uint32 not_implemented_count = 5;

    // The number of other responses
    optional uint32 other_count = 6;

    // The number of queries handled by Upstream DNS server.
    optional uint32 upstream_dns_queries = 7;

    // The number of responses handled by Upstream DNS server.
    optional uint32 upstream_dns_responses = 8;

    // The number of upstream DNS failures.
    optional uint32 upstream_dns_failures = 9;
  }

  message DnsServerInfo {
    // The counters of response codes sent by the DNS server
    optional DnsServerResponseCounters response_counters = 1;

    // The number of DNS queries resolved at the local SRP server
    optional uint32 resolved_by_local_srp_count = 2;

    // The state of upstream DNS query
    optional UpstreamDnsQueryState upstream_dns_query_state = 3;
  }

  message MdnsResponseCounters {
    // The number of successful responses
    optional uint32 success_count = 1;

    // The number of 'not found' responses
    optional uint32 not_found_count = 2;

    // The number of 'invalid arg' responses
    optional uint32 invalid_args_count = 3;

    // The number of 'duplicated' responses
    optional uint32 duplicated_count = 4;

    // The number of 'not implemented' responses
    optional uint32 not_implemented_count = 5;

    // The number of unknown error responses
    optional uint32 unknown_error_count = 6;

    // The number of aborted responses
    optional uint32 aborted_count = 7;

    // The number of invalid state responses
    optional uint32 invalid_state_count = 8;
  }

  message MdnsInfo {
    // The response counters of host registrations
    optional MdnsResponseCounters host_registration_responses = 1;

    // The response counters of service registrations
    optional MdnsResponseCounters service_registration_responses = 2;

    // The response counters of host resolutions
    optional MdnsResponseCounters host_resolution_responses = 3;

    // The response counters of service resolutions
    optional MdnsResponseCounters service_resolution_responses = 4;

    // The EMA (Exponential Moving Average) latencies of mDNS operations

    // The EMA latency of host registrations in milliseconds
    optional uint32 host_registration_ema_latency_ms = 5;

    // The EMA latency of service registrations in milliseconds
    optional uint32 service_registration_ema_latency_ms = 6;

    // The EMA latency of host resolutions in milliseconds
    optional uint32 host_resolution_ema_latency_ms = 7;

    // The EMA latency of service resolutions in milliseconds
    optional uint32 service_resolution_ema_latency_ms = 8;
  }

  enum Nat64State {
    NAT64_STATE_UNSPECIFIED = 0;
    NAT64_STATE_DISABLED = 1;
    NAT64_STATE_NOT_RUNNING = 2;
    NAT64_STATE_IDLE = 3;
    NAT64_STATE_ACTIVE = 4;
  }

  message BorderRoutingNat64State {
    optional Nat64State prefix_manager_state = 1;
    optional Nat64State translator_state = 2;
  }

  message TrelPacketCounters {
    // The number of packets successfully transmitted through TREL
    optional uint64 trel_tx_packets = 1;

    // The number of bytes successfully transmitted through TREL
    optional uint64 trel_tx_bytes = 2;

    // The number of packet transmission failures through TREL
    optional uint64 trel_tx_packets_failed = 3;

    // The number of packets successfully received through TREL
    optional uint64 trel_rx_packets = 4;

    // The number of bytes successfully received through TREL
    optional uint64 trel_rx_bytes = 5;
  }

  message TrelInfo {
    // Whether TREL is enabled.
    optional bool is_trel_enabled = 1;

    // The number of TREL peers.
    optional uint32 num_trel_peers = 2;

    // TREL packet counters
    optional TrelPacketCounters counters = 3;
  }

  message BorderAgentCounters {
    // The number of ePSKc activations
    optional uint32 epskc_activations = 1;

    // The number of ePSKc deactivations due to cleared via API
    optional uint32 epskc_deactivation_clears = 2;

    // The number of ePSKc deactivations due to timeout
    optional uint32 epskc_deactivation_timeouts = 3;

    // The number of ePSKc deactivations due to max connection attempts reached
    optional uint32 epskc_deactivation_max_attempts = 4;

    // The number of ePSKc deactivations due to commissioner disconnected
    optional uint32 epskc_deactivation_disconnects = 5;

    // The number of ePSKc activation failures caused by invalid border agent
    // state
    optional uint32 epskc_invalid_ba_state_errors = 6;

    // The number of ePSKc activation failures caused by invalid argument
    optional uint32 epskc_invalid_args_errors = 7;

    // The number of ePSKc activation failures caused by failed to start secure
    // session
    optional uint32 epskc_start_secure_session_errors = 8;

    // The number of successful secure session establishment with ePSKc
    optional uint32 epskc_secure_session_successes = 9;

    // The number of failed secure session establishement with ePSKc
    optional uint32 epskc_secure_session_failures = 10;

    // The number of active commissioner petitioned over secure session
    // establishment with ePSKc
    optional uint32 epskc_commissioner_petitions = 11;

    // The number of successful secure session establishment with PSKc
    optional uint32 pskc_secure_session_successes = 12;

    // The number of failed secure session establishement with PSKc
    optional uint32 pskc_secure_session_failures = 13;

    // The number of active commissioner petitioned over secure session
    // establishment with PSKc
    optional uint32 pskc_commissioner_petitions = 14;

    // The number of MGMT_ACTIVE_GET.req received
    optional uint32 mgmt_active_get_reqs = 15;

    // The number of MGMT_PENDING_GET.req received
    optional uint32 mgmt_pending_get_reqs = 16;
  }


  enum EpskcDeactivatedReason {
    EPSKC_DEACTIVATED_REASON_UNKNOWN = 0;
    EPSKC_DEACTIVATED_REASON_LOCAL_CLOSE =
        1;  ///< Deactivated by a call to the API.
    EPSKC_DEACTIVATED_REASON_REMOTE_CLOSE = 2;  ///< Disconnected by the peer.
    EPSKC_DEACTIVATED_REASON_SESSION_ERROR =
        3;  ///< Disconnected due to some error.
    EPSKC_DEACTIVATED_REASON_SESSION_TIMEOUT =
        4;  ///< Disconnected due to timeout.
    EPSKC_DEACTIVATED_REASON_MAX_ATTEMPTS =
        5;  ///< Max allowed attempts reached.
    EPSKC_DEACTIVATED_REASON_EPSKC_TIMEOUT = 6;  ///< The ePSKc mode timed out.
  }

  message BorderAgentEpskcJourneyInfo {
    // The timestamps for the ePSKc journey events are relative to the Thread
    // stack's last uptime on the device. The duration of various phases, such
    // as the period between activation and deactivation, will be determined
    // using these timestamps.

    // Thread devices consistently upload these timestamps as uint32. If a
    // rounding error occurs where the deactivation time precedes the activation
    // time, the duration will be calculated by adding a uint32 value.
    optional uint32 activated_msec = 1;
    optional uint32 connected_msec = 2;
    optional uint32 petitioned_msec = 3;
    optional uint32 retrieved_active_dataset_msec = 4;
    optional uint32 retrieved_pending_dataset_msec = 5;
    optional uint32 keep_alive_msec = 6;
    optional uint32 deactivated_msec = 7;
    optional EpskcDeactivatedReason deactivated_reason = 8;
  }

  message BorderAgentInfo {
    // The border agent counters
    optional BorderAgentCounters border_agent_counters = 1;

    // The border agent epskc journey info
    repeated BorderAgentEpskcJourneyInfo border_agent_epskc_journey_info = 2;
  }

  message WpanBorderRouter {
    // Border routing counters
    optional BorderRoutingCounters border_routing_counters = 1;

    // Information about the SRP server
    optional SrpServerInfo srp_server = 2;

    // Information about the DNS server
    optional DnsServerInfo dns_server = 3;

    // Information about the mDNS publisher
    optional MdnsInfo mdns = 4;

    // Information about the state of components of NAT64
    optional BorderRoutingNat64State nat64_state = 5;

    // Information about TREL.
    optional TrelInfo trel_info = 6;

    // Information about the Border Agent
    optional BorderAgentInfo border_agent_info = 7;

    // Whether multi-AIL is detected.
    optional bool multi_ail_detected = 8;
  }

  message RcpStabilityStatistics {
    optional uint32 rcp_timeout_count = 1;
    optional uint32 rcp_reset_count = 2;
    optional uint32 rcp_restoration_count = 3;
    optional uint32 spinel_parse_error_count = 4;
    optional int32 rcp_firmware_update_count = 5;
    optional uint32 thread_stack_uptime = 6;
  }

  message RcpInterfaceStatistics {
    optional uint32 rcp_interface_type = 1;
    optional uint64 transferred_frames_count = 2;
    optional uint64 transferred_valid_frames_count = 3;
    optional uint64 transferred_garbage_frames_count = 4;
    optional uint64 rx_frames_count = 5;
    optional uint64 rx_bytes_count = 6;
    optional uint64 tx_frames_count = 7;
    optional uint64 tx_bytes_count = 8;
  }

  message WpanRcp {
    optional RcpStabilityStatistics rcp_stability_statistics = 1;
    optional RcpInterfaceStatistics rcp_interface_statistics = 2;
  }

  message CoexMetrics {
    optional uint32 count_tx_request = 1;
    optional uint32 count_tx_grant_immediate = 2;
    optional uint32 count_tx_grant_wait = 3;
    optional uint32 count_tx_grant_wait_activated = 4;
    optional uint32 count_tx_grant_wait_timeout = 5;
    optional uint32 count_tx_grant_deactivated_during_request = 6;
    optional uint32 tx_average_request_to_grant_time_us = 7;
    optional uint32 count_rx_request = 8;
    optional uint32 count_rx_grant_immediate = 9;
    optional uint32 count_rx_grant_wait = 10;
    optional uint32 count_rx_grant_wait_activated = 11;
    optional uint32 count_rx_grant_wait_timeout = 12;
    optional uint32 count_rx_grant_deactivated_during_request = 13;
    optional uint32 count_rx_grant_none = 14;
    optional uint32 rx_average_request_to_grant_time_us = 15;
  }

  optional WpanStats wpan_stats = 1 [(log_mode) = MODE_BYTES];
  optional WpanTopoFull wpan_topo_full = 2 [(log_mode) = MODE_BYTES];
  optional WpanBorderRouter wpan_border_router = 3 [(log_mode) = MODE_BYTES];
  optional WpanRcp wpan_rcp = 4 [(log_mode) = MODE_BYTES];
  optional CoexMetrics coex_metrics = 5 [(log_mode) = MODE_BYTES];
}

message ThreadnetworkTopoEntryRepeated {
  message TopoEntry {
    // 0~15: uint16_t rloc_16
    // 16~31: uint16_t version Thread version of the neighbor
    optional uint32 combo_telemetry1 = 1;
    // 0~7: uint8_t link_quality_in
    // 8~15: int8_t average_rssi
    // 16~23: int8_t last_rssi
    // 24~31: uint8_t network_data_version
    optional uint32 combo_telemetry2 = 2;
    optional uint32 age_sec = 3;
    // Each bit on the flag represents a bool flag
    // 0: rx_on_when_idle
    // 1: full_function
    // 2: secure_data_request
    // 3: full_network_data
    // 4: is_child
    optional uint32 topo_entry_flags = 4;
    optional uint32 link_frame_counter = 5;
    optional uint32 mle_frame_counter = 6;
    optional uint32 timeout_sec = 7;
    // 0~15: uint16_t frame_error_rate. Frame error rate (0xffff->100%). Requires error tracking feature.
    // 16~31: uint16_t message_error_rate. (IPv6) msg error rate (0xffff->100%). Requires error tracking feature.
    optional uint32 combo_telemetry3 = 8;
  }

  message TopoEntryRepeated {
    repeated TopoEntry topo_entries = 1;
  }

  optional TopoEntryRepeated topo_entry_repeated = 1 [(log_mode) = MODE_BYTES];
}

message ThreadnetworkDeviceInfoReported {
  // OpenThread host build version.
  optional string ot_host_version = 1;

  // OpenThread RCP build version.
  optional string ot_rcp_version = 2;

  // Thread protocol version.
  optional int32 thread_version = 3;

  // Thread Daemon version.
  optional string thread_daemon_version = 4;
}
