Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6b1667c8 authored by Chenjie Yu's avatar Chenjie Yu
Browse files

add api to log BinaryPushStateChanged atom

This api is to log BinaryPushStateChanged.
Experiment id is added as a binary blob that is not expected to be
accessed on device side.
This cl is mainly for API.
Will add follow up cls for persisting train info on disk and make puller
for it.
Will address sepolicy in follow up cls.

Bug: 119633962
Bug: 119685453
Test: will add gts
Change-Id: I68b4246cf7e8079155e16121ca37a312b35a5328
parent e34c699d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -48985,6 +48985,7 @@ package android.util {
  }
  public final class StatsLog {
    method @RequiresPermission(allOf={android.Manifest.permission.DUMP, android.Manifest.permission.PACKAGE_USAGE_STATS}) public static boolean logBinaryPushStateChanged(@NonNull String, long, int, int, @NonNull long[]);
    method public static boolean logEvent(int);
    method public static boolean logStart(int);
    method public static boolean logStop(int);
+1 −0
Original line number Diff line number Diff line
@@ -238,6 +238,7 @@ cc_test {
        "tests/guardrail/StatsdStats_test.cpp",
        "tests/metrics/metrics_test_helper.cpp",
        "tests/statsd_test_util.cpp",
        "tests/storage/StorageManager_test.cpp",
        "tests/e2e/WakelockDuration_e2e_test.cpp",
        "tests/e2e/MetricActivation_e2e_test.cpp",
        "tests/e2e/MetricConditionLink_e2e_test.cpp",
+57 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/PermissionController.h>
#include <cutils/multiuser.h>
#include <dirent.h>
#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
#include <private/android_filesystem_config.h>
@@ -47,12 +48,16 @@ using namespace android;

using android::base::StringPrintf;
using android::util::FIELD_COUNT_REPEATED;
using android::util::FIELD_TYPE_INT64;
using android::util::FIELD_TYPE_MESSAGE;

namespace android {
namespace os {
namespace statsd {

const int FIELD_ID_EXPERIMENT_ID = 1;
const int FIELD_ID_EXPERIMENT_ID_MSG = 7;

constexpr const char* kPermissionDump = "android.permission.DUMP";
constexpr const char* kPermissionUsage = "android.permission.PACKAGE_USAGE_STATS";

@@ -1075,6 +1080,58 @@ Status StatsService::unregisterPullerCallback(int32_t atomTag, const String16& p
    return Status::ok();
}

Status StatsService::sendBinaryPushStateChangedAtom(const android::String16& trainName,
                                                    int64_t trainVersionCode, int options,
                                                    int32_t state,
                                                    const std::vector<int64_t>& experimentIds) {
    uid_t uid = IPCThreadState::self()->getCallingUid();
    // For testing
    if (uid == AID_ROOT || uid == AID_SYSTEM || uid == AID_SHELL) {
        return ok();
    }

    // Caller must be granted these permissions
    if (!checkCallingPermission(String16(kPermissionDump))) {
        return exception(binder::Status::EX_SECURITY,
                         StringPrintf("UID %d lacks permission %s", uid, kPermissionDump));
    }
    if (!checkCallingPermission(String16(kPermissionUsage))) {
        return exception(binder::Status::EX_SECURITY,
                         StringPrintf("UID %d lacks permission %s", uid, kPermissionUsage));
    }
    // TODO: add verifier permission

    userid_t userId = multiuser_get_user_id(uid);

    bool requiresStaging = options | IStatsManager::FLAG_REQUIRE_STAGING;
    bool rollbackEnabled = options | IStatsManager::FLAG_ROLLBACK_ENABLED;
    bool requiresLowLatencyMonitor = options | IStatsManager::FLAG_REQUIRE_LOW_LATENCY_MONITOR;

    ProtoOutputStream proto;
    uint64_t protoToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_EXPERIMENT_ID_MSG);
    for (const auto& expId : experimentIds) {
        proto.write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_EXPERIMENT_ID,
                    (long long)expId);
    }
    proto.end(protoToken);

    vector<uint8_t> buffer;
    buffer.resize(proto.size());
    size_t pos = 0;
    auto iter = proto.data();
    while (iter.readBuffer() != NULL) {
        size_t toRead = iter.currentToRead();
        std::memcpy(&(buffer[pos]), iter.readBuffer(), toRead);
        pos += toRead;
        iter.rp()->move(toRead);
    }
    LogEvent event(std::string(String8(trainName).string()), trainVersionCode, requiresStaging,
                   rollbackEnabled, requiresLowLatencyMonitor, state, buffer, userId);
    mProcessor->OnLogEvent(&event);
    StorageManager::writeTrainInfo(trainVersionCode, buffer);
    return Status::ok();
}

hardware::Return<void> StatsService::reportSpeakerImpedance(
        const SpeakerImpedance& speakerImpedance) {
    LogEvent event(getWallClockSec() * NS_PER_SEC, getElapsedRealtimeNs(), speakerImpedance);
+8 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
#include <android/frameworks/stats/1.0/types.h>
#include <android/os/BnStatsManager.h>
#include <android/os/IStatsCompanionService.h>
#include <android/os/IStatsManager.h>
#include <binder/IResultReceiver.h>
#include <utils/Looper.h>

@@ -185,6 +186,13 @@ public:
     */
    virtual Status unregisterPullerCallback(int32_t atomTag, const String16& packageName) override;

    /**
     * Binder call to log BinaryPushStateChanged atom.
     */
    virtual Status sendBinaryPushStateChangedAtom(
            const android::String16& trainName, int64_t trainVersionCode, int options,
            int32_t state, const std::vector<int64_t>& experimentIds) override;

    /**
     * Binder call to get SpeakerImpedance atom.
     */
+13 −7
Original line number Diff line number Diff line
@@ -3152,6 +3152,13 @@ message FlagFlipUpdateOccurred {
    optional int64 order_id = 2;
}

/**
 * Potential experiment ids that goes with a train install.
 */
message TrainExperimentIds {
    repeated int64 experiment_id = 1;
}

/*
 * Logs when a binary push state changes.
 * Logged by the installer via public api.
@@ -3178,8 +3185,14 @@ message BinaryPushStateChanged {
        INSTALL_FAILURE = 6;
        INSTALL_CANCELLED = 7;
        INSTALLER_ROLLBACK_REQUESTED = 8;
        INSTALLER_ROLLBACK_SUCCESS = 9;
        INSTALLER_ROLLBACK_FAILURE = 10;
    }
    optional State state = 6;
    // Possible experiment ids for monitoring this push.
    optional TrainExperimentIds experiment_ids = 7 [(log_mode) = MODE_BYTES];
    // user id
    optional int32 user_id = 8;
}

/** Represents USB port overheat event. */
@@ -5506,13 +5519,6 @@ message DeviceIdentifierAccessDenied {
    optional bool is_priv_app = 4;
}

/**
 * Potential experiment ids that goes with a train install.
 */
message TrainExperimentIds {
    repeated int64 experiment_id = 1;
}

/**
 * Pulls the ongoing mainline install train version code.
 * Pulled from StatsCompanionService
Loading