blob: 410a5af421dc387af6218355b002854b701971e2 [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>();
tsaichristinee83b5f22022-09-26 18:33:05 -070072 if (!repeatedIntValue) {
73 AStatsEvent_writeInt32Array(event, {}, 0);
74 break;
75 }
tsaichristine5f5f5792022-09-09 16:26:31 -070076 AStatsEvent_writeInt32Array(event, repeatedIntValue->data(),
77 repeatedIntValue->size());
78 break;
79 }
80 case VendorAtomValue::repeatedLongValue: {
81 const std::optional<std::vector<int64_t>>& repeatedLongValue =
82 atomValue.get<VendorAtomValue::repeatedLongValue>();
tsaichristinee83b5f22022-09-26 18:33:05 -070083 if (!repeatedLongValue) {
84 AStatsEvent_writeInt64Array(event, {}, 0);
85 break;
86 }
tsaichristine5f5f5792022-09-09 16:26:31 -070087 AStatsEvent_writeInt64Array(event, repeatedLongValue->data(),
88 repeatedLongValue->size());
89 break;
90 }
91 case VendorAtomValue::repeatedFloatValue: {
92 const std::optional<std::vector<float>>& repeatedFloatValue =
93 atomValue.get<VendorAtomValue::repeatedFloatValue>();
tsaichristinee83b5f22022-09-26 18:33:05 -070094 if (!repeatedFloatValue) {
95 AStatsEvent_writeFloatArray(event, {}, 0);
96 break;
97 }
tsaichristine5f5f5792022-09-09 16:26:31 -070098 AStatsEvent_writeFloatArray(event, repeatedFloatValue->data(),
99 repeatedFloatValue->size());
100 break;
101 }
102 case VendorAtomValue::repeatedStringValue: {
103 const std::optional<std::vector<std::optional<std::string>>>& repeatedStringValue =
104 atomValue.get<VendorAtomValue::repeatedStringValue>();
tsaichristinee83b5f22022-09-26 18:33:05 -0700105 if (!repeatedStringValue) {
106 AStatsEvent_writeStringArray(event, {}, 0);
107 break;
108 }
tsaichristine5f5f5792022-09-09 16:26:31 -0700109 const std::vector<std::optional<std::string>>& repeatedStringVector =
110 *repeatedStringValue;
111 const char* cStringArray[repeatedStringVector.size()];
112
113 for (int i = 0; i < repeatedStringVector.size(); ++i) {
tsaichristinee83b5f22022-09-26 18:33:05 -0700114 cStringArray[i] = repeatedStringVector[i].has_value()
115 ? repeatedStringVector[i]->c_str()
116 : "";
tsaichristine5f5f5792022-09-09 16:26:31 -0700117 }
118
119 AStatsEvent_writeStringArray(event, cStringArray, repeatedStringVector.size());
120 break;
121 }
122 case VendorAtomValue::repeatedBoolValue: {
123 const std::optional<std::vector<bool>>& repeatedBoolValue =
124 atomValue.get<VendorAtomValue::repeatedBoolValue>();
tsaichristinee83b5f22022-09-26 18:33:05 -0700125 if (!repeatedBoolValue) {
126 AStatsEvent_writeBoolArray(event, {}, 0);
127 break;
128 }
tsaichristine5f5f5792022-09-09 16:26:31 -0700129 const std::vector<bool>& repeatedBoolVector = *repeatedBoolValue;
130 bool boolArray[repeatedBoolValue->size()];
131
132 for (int i = 0; i < repeatedBoolVector.size(); ++i) {
133 boolArray[i] = repeatedBoolVector[i];
134 }
135
136 AStatsEvent_writeBoolArray(event, boolArray, repeatedBoolVector.size());
137 break;
138 }
139 case VendorAtomValue::byteArrayValue: {
140 const std::optional<std::vector<uint8_t>>& byteArrayValue =
141 atomValue.get<VendorAtomValue::byteArrayValue>();
tsaichristinee83b5f22022-09-26 18:33:05 -0700142 if (!byteArrayValue) {
143 AStatsEvent_writeByteArray(event, {}, 0);
144 break;
145 }
tsaichristine5f5f5792022-09-09 16:26:31 -0700146 AStatsEvent_writeByteArray(event, byteArrayValue->data(), byteArrayValue->size());
147 break;
148 }
Vova Sharaienko3092c962021-02-01 23:38:12 +0000149 }
150 }
151 AStatsEvent_build(event);
152 const int ret = AStatsEvent_write(event);
153 AStatsEvent_release(event);
154
155 return ret <= 0 ?
156 ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(ret, "report atom failed") :
157 ndk::ScopedAStatus::ok();
158}
159
160} // namespace stats
161} // namespace frameworks
162} // namespace android
163} // namespace aidl