• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
25 namespace aidl {
26 namespace android {
27 namespace frameworks {
28 namespace stats {
29 
StatsHal()30 StatsHal::StatsHal() {}
31 
reportVendorAtom(const VendorAtom & vendorAtom)32 ndk::ScopedAStatus StatsHal::reportVendorAtom(const VendorAtom& vendorAtom) {
33     std::string reverseDomainName = (std::string) vendorAtom.reverseDomainName;
34     if (vendorAtom.atomId < 100000 || vendorAtom.atomId >= 200000) {
35         ALOGE("Atom ID %ld is not a valid vendor atom ID", (long) vendorAtom.atomId);
36         return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
37             -1, "Not a valid vendor atom ID");
38     }
39     if (reverseDomainName.length() > 50) {
40         ALOGE("Vendor atom reverse domain name %s is too long.", reverseDomainName.c_str());
41         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;
65         }
66     }
67     AStatsEvent_build(event);
68     const int ret = AStatsEvent_write(event);
69     AStatsEvent_release(event);
70 
71     return ret <= 0 ?
72             ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(ret, "report atom failed") :
73             ndk::ScopedAStatus::ok();
74 }
75 
76 }  // namespace stats
77 }  // namespace frameworks
78 }  // namespace android
79 }  // namespace aidl
80