1 /*
2 * Copyright (C) 2019 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 LOG_NDEBUG 0
18 #define LOG_TAG "statsd_audiopolicy"
19 #include <utils/Log.h>
20
21 #include <dirent.h>
22 #include <inttypes.h>
23 #include <pthread.h>
24 #include <pwd.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include <statslog.h>
33
34 #include "MediaAnalyticsService.h"
35 #include "frameworks/base/core/proto/android/stats/mediametrics/mediametrics.pb.h"
36 #include "iface_statsd.h"
37
38 namespace android {
39
statsd_audiopolicy(MediaAnalyticsItem * item)40 bool statsd_audiopolicy(MediaAnalyticsItem *item)
41 {
42 if (item == NULL) return false;
43
44 // these go into the statsd wrapper
45 nsecs_t timestamp = item->getTimestamp();
46 std::string pkgName = item->getPkgName();
47 int64_t pkgVersionCode = item->getPkgVersionCode();
48 int64_t mediaApexVersion = 0;
49
50
51 // the rest into our own proto
52 //
53 ::android::stats::mediametrics::AudioPolicyData metrics_proto;
54
55 // flesh out the protobuf we'll hand off with our data
56 //
57 //int32 char kAudioPolicyStatus[] = "android.media.audiopolicy.status";
58 int32_t status = -1;
59 if (item->getInt32("android.media.audiopolicy.status", &status)) {
60 metrics_proto.set_status(status);
61 }
62 //string char kAudioPolicyRqstSrc[] = "android.media.audiopolicy.rqst.src";
63 char *rqst_src = NULL;
64 if (item->getCString("android.media.audiopolicy.rqst.src", &rqst_src)) {
65 metrics_proto.set_request_source(rqst_src);
66 }
67 //string char kAudioPolicyRqstPkg[] = "android.media.audiopolicy.rqst.pkg";
68 char *rqst_pkg = NULL;
69 if (item->getCString("android.media.audiopolicy.rqst.pkg", &rqst_pkg)) {
70 metrics_proto.set_request_package(rqst_pkg);
71 }
72 //int32 char kAudioPolicyRqstSession[] = "android.media.audiopolicy.rqst.session";
73 int32_t rqst_session = -1;
74 if (item->getInt32("android.media.audiopolicy.rqst.session", &rqst_session)) {
75 metrics_proto.set_request_session(rqst_session);
76 }
77 //string char kAudioPolicyRqstDevice[] = "android.media.audiopolicy.rqst.device";
78 char *rqst_device = NULL;
79 if (item->getCString("android.media.audiopolicy.rqst.device", &rqst_device)) {
80 metrics_proto.set_request_device(rqst_device);
81 }
82
83 //string char kAudioPolicyActiveSrc[] = "android.media.audiopolicy.active.src";
84 char *active_src = NULL;
85 if (item->getCString("android.media.audiopolicy.active.src", &active_src)) {
86 metrics_proto.set_active_source(active_src);
87 }
88 //string char kAudioPolicyActivePkg[] = "android.media.audiopolicy.active.pkg";
89 char *active_pkg = NULL;
90 if (item->getCString("android.media.audiopolicy.active.pkg", &active_pkg)) {
91 metrics_proto.set_active_package(active_pkg);
92 }
93 //int32 char kAudioPolicyActiveSession[] = "android.media.audiopolicy.active.session";
94 int32_t active_session = -1;
95 if (item->getInt32("android.media.audiopolicy.active.session", &active_session)) {
96 metrics_proto.set_active_session(active_session);
97 }
98 //string char kAudioPolicyActiveDevice[] = "android.media.audiopolicy.active.device";
99 char *active_device = NULL;
100 if (item->getCString("android.media.audiopolicy.active.device", &active_device)) {
101 metrics_proto.set_active_device(active_device);
102 }
103
104
105 std::string serialized;
106 if (!metrics_proto.SerializeToString(&serialized)) {
107 ALOGE("Failed to serialize audipolicy metrics");
108 return false;
109 }
110
111 if (enabled_statsd) {
112 android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
113 (void)android::util::stats_write(android::util::MEDIAMETRICS_AUDIOPOLICY_REPORTED,
114 timestamp, pkgName.c_str(), pkgVersionCode,
115 mediaApexVersion,
116 bf_serialized);
117
118 } else {
119 ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
120 }
121
122 // must free the strings that we were given
123 free(rqst_src);
124 free(rqst_pkg);
125 free(rqst_device);
126 free(active_src);
127 free(active_pkg);
128 free(active_device);
129
130 return true;
131 }
132
133 };
134