/*
 * 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.telephony;

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

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

extend Atom {
    optional CellularRadioPowerStateChanged cellular_radio_power_state_changed = 713 [(module) = "telephony"];
    optional EmergencyNumbersInfo emergency_numbers_info = 10180 [(module) = "telephony"];
    optional DataNetworkValidation data_network_validation = 10207 [(module) = "telephony"];
    optional DataRatStateChanged data_rat_state_changed = 854 [(module) = "telephony"];
    optional ConnectedChannelChanged connected_channel_changed = 882 [(module) = "telephony"];
    optional SmsOtpRedacted sms_otp_redacted = 1136 [(module) = "telephony"];
    optional SmsOtpEvaluation sms_otp_evaluation = 1137 [(module) = "telephony"];
    // 10208 is reserved due to removing the old atom.
}

message CellularRadioPowerStateChanged {
    optional android.telephony.CellularRadioPowerStateEnum state = 1
            [(state_field_option).exclusive_state = true, (state_field_option).nested = false];
}

/**
 * pulled atom to capture all emergency numbers information stored.
 */
message EmergencyNumbersInfo {
    // flag for db version ignored
    optional bool is_db_version_ignored = 1;

    // version number read from assets
    optional int32 asset_version = 2;

    // version number from OTA updated DB
    optional int32 ota_version = 3;

    // the emergency number
    optional string number = 4;

    // iso code for the country
    optional string country_iso = 5;

    // Carrier mnc
    optional string mnc = 6;

    // call route associated with emergency number.
    optional android.telephony.CallRoute route = 7;

    // list of URNs associated with emergency number.
    repeated string urns = 8;

    // service categories of the emergency number.
    repeated android.telephony.ServiceCategory service_categories = 9;

    // sources of the captured emergency number.
    repeated android.telephony.Source sources = 10;
}

/**
 * Pulls information for a data network validation
 *
 * Each pull creates multiple atoms, one for each data network validation.
 *
 * Pulled from:
 *   frameworks/opt/telephony/src/java/com/android/internal/telephony/metrics/MetricsCollector.java
 */
message DataNetworkValidation {
    // Data RAT when network validation was performed
    // Used as a dimension
    optional android.telephony.NetworkTypeEnum network_type = 1;

    // APN type bitmask where network verification was performed
    // @ApnType in frameworks/base/telephony/java/android/telephony/Annotation.java
    optional int32 apn_type_bitmask = 2;

    // Signal strength when network validation was performed
    optional android.telephony.SignalStrengthEnum signal_strength = 3;

    // Result of network validation for one request
    optional android.telephony.NetworkValidationResult validation_result = 4;

    // The time in milliseconds from requesting network validation to receiving
    optional int64 elapsed_time_in_millis = 5;

    // Boolean that handover attempted during validation.
    optional bool handover_attempted = 6;

    // Number of network validation
    optional int32 network_validation_count = 7;
}

/**
 * Data connection changing event.
 *
 * Logged from:
 *   frameworks/opt/telephony/src/java/com/android/internal/telephony/metrics/DataConnectionStateTracker.java
 */
message DataRatStateChanged {
    enum DataRat {
        DATA_RAT_UNSPECIFIED = 0;
        NO_SIM = 1;
        DATA_RAT_2G = 2;
        DATA_RAT_3G = 3;
        DATA_RAT_4G_LTE = 4;
        DATA_RAT_5G_NSA_LTE = 5;
        DATA_RAT_5G_NSA_FR1 = 6;
        DATA_RAT_5G_NSA_FR2 = 7;
        DATA_RAT_5G_SA_FR1 = 8;
        DATA_RAT_5G_SA_FR2 = 9;
    }

    optional DataRat data_rat = 1 [
    (state_field_option).exclusive_state = true,
    (state_field_option).nested = false
  ];
}

/**
 * Physical channel changing event.
 *
 * Logged from:
 *   frameworks/opt/telephony/src/java/com/android/internal/telephony/metrics/DataConnectionStateTracker.java
 */
message ConnectedChannelChanged {
    enum ChannelCount {
        CHANNEL_COUNT_UNSPECIFIED = 0;
        // CHANNEL_COUNT_ONE indicates there is 0 or 1 connected channel
        CHANNEL_COUNT_ONE = 1;
        // CHANNEL_COUNT_TWO indicates there are 2 connected channels
        CHANNEL_COUNT_TWO = 2;
        CHANNEL_COUNT_THREE = 3;
        CHANNEL_COUNT_FOUR = 4;
        CHANNEL_COUNT_FIVE = 5;
    }

    optional ChannelCount connected_channel_count = 1 [
    (state_field_option).exclusive_state = true,
    (state_field_option).nested = false
  ];
}

/**
 *
 * Report a package is forbidden from receiving information about an SMS that contains an OTP
 * (One Time Password).
 *
 * Logged from:
 *
 * frameworks/opt/telephony/src/java/com/android/internal/telephony/InboundSmsHandler.java and
 * packages/providers/TelephonyProvider/src/com/android/providers/telephony/SmsProvider.java
 *
 * Estimated Logging Rate:
 * Peak: 1 per UID, per redaction location, on the device every 24 hours
 *
 */
message SmsOtpRedacted {
    // The UID who was restricted from receiving SMS data
    optional int32 redacted_uid = 1 [(is_uid) = true];
    // The type of OTP redaction algorithm used
    optional android.telephony.SmsOtpRedactionAlgorithm redaction_algorithm = 2;
    // The place where redaction took place
    optional android.telephony.SmsOtpRedactionLocation redaction_location = 3;
    // The number of times a redacted call for this uid, algorithm, and location happened in the
    // last 24 hours
    optional int32 event_count_24_hours = 4;
}

/**
 *
 * Logged from
 * frameworks/opt/telephony/src/java/com/android/internal/telephony/InboundSmsHandler.java and
 * packages/providers/TelephonyProvider/src/com/android/providers/telephony/SmsProvider.java
 * When an SMS is evaluated to see if it contains an OTP. These will be stored, and uploaded once
 * every 24 hours, in shuffled order.
 *
 * Estimated Logging Rate:
 * Peak: 2 times per SMS message per device
 */
message SmsOtpEvaluation {
    enum OtpEvaluationResult {
        // The message wasn't checked for an OTP
        EVALUATION_RESULT_NOT_CHECKED = 0;
        // The message was checked, but didn't contain an OTP
        EVALUATION_RESULT_NO_OTP = 1;
        // The message contained an OTP
        EVALUATION_RESULT_HAS_OTP = 2;
    }

    // The time a redaction check took. -1 for NOT_CHECKED
    optional int32 redaction_time_ms = 1;
    // The result of OTP evaluation
    optional OtpEvaluationResult result = 2;
    // The type of OTP redaction algorithm used
    optional android.telephony.SmsOtpRedactionAlgorithm redaction_algorithm = 3;
    // The place where redaction took place
    optional android.telephony.SmsOtpRedactionLocation redaction_location = 4;
}