blob: fe2845449f7c3dfe3edbc5ee92d93e77f40cb6d7 [file] [log] [blame]
Vova Sharaienko3092c962021-02-01 23:38:12 +00001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define DEBUG false // STOPSHIP if true
18#define LOG_TAG "StatsAidl"
19
20#include <log/log.h>
21#include <statslog.h>
22
23#include "StatsAidl.h"
24
25namespace aidl {
26namespace android {
27namespace frameworks {
28namespace stats {
29
30StatsHal::StatsHal() {}
31
32ndk::ScopedAStatus StatsHal::reportVendorAtom(const VendorAtom& vendorAtom) {
Vova Sharaienko3092c962021-02-01 23:38:12 +000033 if (vendorAtom.atomId < 100000 || vendorAtom.atomId >= 200000) {
34 ALOGE("Atom ID %ld is not a valid vendor atom ID", (long) vendorAtom.atomId);
35 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
36 -1, "Not a valid vendor atom ID");
37 }
Vova Sharaienko5764fc12022-12-09 02:40:35 +000038 if (vendorAtom.reverseDomainName.length() > 50) {
39 ALOGE("Vendor atom reverse domain name %s is too long.",
40 vendorAtom.reverseDomainName.c_str());
Vova Sharaienko3092c962021-02-01 23:38:12 +000041 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
42 -1, "Vendor atom reverse domain name is too long");
43 }
44 AStatsEvent* event = AStatsEvent_obtain();
45 AStatsEvent_setAtomId(event, vendorAtom.atomId);
46 AStatsEvent_writeString(event, vendorAtom.reverseDomainName.c_str());
47 for (const auto& atomValue : vendorAtom.values) {
48 switch (atomValue.getTag()) {
49 case VendorAtomValue::intValue:
50 AStatsEvent_writeInt32(event,
51 atomValue.get<VendorAtomValue::intValue>());
52 break;
53 case VendorAtomValue::longValue:
54 AStatsEvent_writeInt64(event,
55 atomValue.get<VendorAtomValue::longValue>());
56 break;
57 case VendorAtomValue::floatValue:
58 AStatsEvent_writeFloat(event,
59 atomValue.get<VendorAtomValue::floatValue>());
60 break;
61 case VendorAtomValue::stringValue:
62 AStatsEvent_writeString(event,
63 atomValue.get<VendorAtomValue::stringValue>().c_str());
64 break;
Vova Sharaienko5540ebc2022-04-08 05:53:43 +000065 case VendorAtomValue::boolValue:
66 AStatsEvent_writeBool(event,
67 atomValue.get<VendorAtomValue::boolValue>());
68 break;
tsaichristine5f5f5792022-09-09 16:26:31 -070069 case VendorAtomValue::repeatedIntValue: {
70 const std::optional<std::vector<int>>& repeatedIntValue =
71 atomValue.get<VendorAtomValue::repeatedIntValue>();
72 AStatsEvent_writeInt32Array(event, repeatedIntValue->data(),
73 repeatedIntValue->size());
74 break;
75 }
76 case VendorAtomValue::repeatedLongValue: {
77 const std::optional<std::vector<int64_t>>& repeatedLongValue =
78 atomValue.get<VendorAtomValue::repeatedLongValue>();
79 AStatsEvent_writeInt64Array(event, repeatedLongValue->data(),
80 repeatedLongValue->size());
81 break;
82 }
83 case VendorAtomValue::repeatedFloatValue: {
84 const std::optional<std::vector<float>>& repeatedFloatValue =
85 atomValue.get<VendorAtomValue::repeatedFloatValue>();
86 AStatsEvent_writeFloatArray(event, repeatedFloatValue->data(),
87 repeatedFloatValue->size());
88 break;
89 }
90 case VendorAtomValue::repeatedStringValue: {
91 const std::optional<std::vector<std::optional<std::string>>>& repeatedStringValue =
92 atomValue.get<VendorAtomValue::repeatedStringValue>();
93 const std::vector<std::optional<std::string>>& repeatedStringVector =
94 *repeatedStringValue;
95 const char* cStringArray[repeatedStringVector.size()];
96
97 for (int i = 0; i < repeatedStringVector.size(); ++i) {
98 cStringArray[i] = repeatedStringVector[i]->c_str();
99 }
100
101 AStatsEvent_writeStringArray(event, cStringArray, repeatedStringVector.size());
102 break;
103 }
104 case VendorAtomValue::repeatedBoolValue: {
105 const std::optional<std::vector<bool>>& repeatedBoolValue =
106 atomValue.get<VendorAtomValue::repeatedBoolValue>();
107 const std::vector<bool>& repeatedBoolVector = *repeatedBoolValue;
108 bool boolArray[repeatedBoolValue->size()];
109
110 for (int i = 0; i < repeatedBoolVector.size(); ++i) {
111 boolArray[i] = repeatedBoolVector[i];
112 }
113
114 AStatsEvent_writeBoolArray(event, boolArray, repeatedBoolVector.size());
115 break;
116 }
117 case VendorAtomValue::byteArrayValue: {
118 const std::optional<std::vector<uint8_t>>& byteArrayValue =
119 atomValue.get<VendorAtomValue::byteArrayValue>();
120
121 AStatsEvent_writeByteArray(event, byteArrayValue->data(), byteArrayValue->size());
122 break;
123 }
Vova Sharaienko3092c962021-02-01 23:38:12 +0000124 }
125 }
126 AStatsEvent_build(event);
127 const int ret = AStatsEvent_write(event);
128 AStatsEvent_release(event);
129
130 return ret <= 0 ?
131 ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(ret, "report atom failed") :
132 ndk::ScopedAStatus::ok();
133}
134
135} // namespace stats
136} // namespace frameworks
137} // namespace android
138} // namespace aidl