blob: 62af0f71955482d241221d76a6c0fbe8ed1ffdc6 [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
2 * Copyright (C) 2016 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
Ray Essickf27e9872019-12-07 06:28:46 -080017#define LOG_TAG "mediametrics::Item"
Ray Essick3938dc62016-11-01 08:56:56 -070018
Ray Essick3938dc62016-11-01 08:56:56 -070019#include <inttypes.h>
Ray Essickb5fac8e2016-12-12 11:33:56 -080020#include <stdlib.h>
21#include <string.h>
22#include <sys/types.h>
Ray Essick3938dc62016-11-01 08:56:56 -070023
Andy Hunga87e69c2019-10-18 10:07:40 -070024#include <mutex>
Andy Hung3253f2d2019-10-21 14:50:07 -070025#include <set>
Andy Hunga87e69c2019-10-18 10:07:40 -070026
Ray Essick3938dc62016-11-01 08:56:56 -070027#include <binder/Parcel.h>
28#include <utils/Errors.h>
29#include <utils/Log.h>
Ray Essick3938dc62016-11-01 08:56:56 -070030#include <utils/SortedVector.h>
31#include <utils/threads.h>
32
Ray Essick3938dc62016-11-01 08:56:56 -070033#include <binder/IServiceManager.h>
Ray Essickf27e9872019-12-07 06:28:46 -080034#include <media/IMediaMetricsService.h>
35#include <media/MediaMetricsItem.h>
Ray Essick79a89ef2017-04-24 15:52:54 -070036#include <private/android_filesystem_config.h>
Ray Essick3938dc62016-11-01 08:56:56 -070037
Andy Hung1efc9c62019-12-03 13:43:33 -080038// Max per-property string size before truncation in toString().
39// Do not make too large, as this is used for dumpsys purposes.
40static constexpr size_t kMaxPropertyStringSize = 4096;
41
Ray Essickf27e9872019-12-07 06:28:46 -080042namespace android::mediametrics {
Ray Essick3938dc62016-11-01 08:56:56 -070043
44#define DEBUG_SERVICEACCESS 0
Ray Essickb5fac8e2016-12-12 11:33:56 -080045#define DEBUG_API 0
46#define DEBUG_ALLOCATIONS 0
47
48// after this many failed attempts, we stop trying [from this process] and just say that
49// the service is off.
50#define SVC_TRIES 2
Ray Essick3938dc62016-11-01 08:56:56 -070051
Ray Essickf27e9872019-12-07 06:28:46 -080052mediametrics::Item* mediametrics::Item::convert(mediametrics_handle_t handle) {
53 mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essickbf536ac2019-08-26 11:04:28 -070054 return item;
55}
56
Ray Essickf27e9872019-12-07 06:28:46 -080057mediametrics_handle_t mediametrics::Item::convert(mediametrics::Item *item ) {
Ray Essickbf536ac2019-08-26 11:04:28 -070058 mediametrics_handle_t handle = (mediametrics_handle_t) item;
59 return handle;
60}
61
Ray Essickf27e9872019-12-07 06:28:46 -080062mediametrics::Item::~Item() {
Ray Essickb5fac8e2016-12-12 11:33:56 -080063 if (DEBUG_ALLOCATIONS) {
Ray Essickf27e9872019-12-07 06:28:46 -080064 ALOGD("Destroy mediametrics::Item @ %p", this);
Ray Essickb5fac8e2016-12-12 11:33:56 -080065 }
Ray Essickb5fac8e2016-12-12 11:33:56 -080066}
67
Ray Essickf27e9872019-12-07 06:28:46 -080068mediametrics::Item &mediametrics::Item::setTimestamp(nsecs_t ts) {
Ray Essick3938dc62016-11-01 08:56:56 -070069 mTimestamp = ts;
70 return *this;
71}
72
Ray Essickf27e9872019-12-07 06:28:46 -080073nsecs_t mediametrics::Item::getTimestamp() const {
Ray Essick3938dc62016-11-01 08:56:56 -070074 return mTimestamp;
75}
76
Ray Essickf27e9872019-12-07 06:28:46 -080077mediametrics::Item &mediametrics::Item::setPid(pid_t pid) {
Ray Essick3938dc62016-11-01 08:56:56 -070078 mPid = pid;
79 return *this;
80}
81
Ray Essickf27e9872019-12-07 06:28:46 -080082pid_t mediametrics::Item::getPid() const {
Ray Essick3938dc62016-11-01 08:56:56 -070083 return mPid;
84}
85
Ray Essickf27e9872019-12-07 06:28:46 -080086mediametrics::Item &mediametrics::Item::setUid(uid_t uid) {
Ray Essick3938dc62016-11-01 08:56:56 -070087 mUid = uid;
88 return *this;
89}
90
Ray Essickf27e9872019-12-07 06:28:46 -080091uid_t mediametrics::Item::getUid() const {
Ray Essick3938dc62016-11-01 08:56:56 -070092 return mUid;
93}
94
Ray Essickf27e9872019-12-07 06:28:46 -080095mediametrics::Item &mediametrics::Item::setPkgName(const std::string &pkgName) {
Ray Essickf65f4212017-08-31 11:41:19 -070096 mPkgName = pkgName;
97 return *this;
98}
99
Ray Essickf27e9872019-12-07 06:28:46 -0800100mediametrics::Item &mediametrics::Item::setPkgVersionCode(int64_t pkgVersionCode) {
Ray Essickf65f4212017-08-31 11:41:19 -0700101 mPkgVersionCode = pkgVersionCode;
102 return *this;
103}
104
Ray Essickf27e9872019-12-07 06:28:46 -0800105int64_t mediametrics::Item::getPkgVersionCode() const {
Ray Essickf65f4212017-08-31 11:41:19 -0700106 return mPkgVersionCode;
107}
108
Ray Essick3938dc62016-11-01 08:56:56 -0700109// remove indicated keys and their values
110// return value is # keys removed
Ray Essickf27e9872019-12-07 06:28:46 -0800111size_t mediametrics::Item::filter(size_t n, const char *attrs[]) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700112 size_t zapped = 0;
113 for (size_t i = 0; i < n; ++i) {
Andy Hung47e58d62019-12-06 18:40:19 -0800114 zapped += mProps.erase(attrs[i]);
Ray Essick3938dc62016-11-01 08:56:56 -0700115 }
116 return zapped;
117}
118
119// remove any keys NOT in the provided list
120// return value is # keys removed
Ray Essickf27e9872019-12-07 06:28:46 -0800121size_t mediametrics::Item::filterNot(size_t n, const char *attrs[]) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700122 std::set<std::string> check(attrs, attrs + n);
123 size_t zapped = 0;
Andy Hung47e58d62019-12-06 18:40:19 -0800124 for (auto it = mProps.begin(); it != mProps.end();) {
125 if (check.find(it->first) != check.end()) {
126 ++it;
Andy Hung3253f2d2019-10-21 14:50:07 -0700127 } else {
Andy Hung47e58d62019-12-06 18:40:19 -0800128 it = mProps.erase(it);
129 ++zapped;
Ray Essick3938dc62016-11-01 08:56:56 -0700130 }
131 }
132 return zapped;
133}
134
Ray Essick3938dc62016-11-01 08:56:56 -0700135// Parcel / serialize things for binder calls
136//
137
Ray Essickf27e9872019-12-07 06:28:46 -0800138status_t mediametrics::Item::readFromParcel(const Parcel& data) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700139 int32_t version;
140 status_t status = data.readInt32(&version);
141 if (status != NO_ERROR) return status;
Ray Essickba8c4842019-01-18 11:35:33 -0800142
Andy Hung3253f2d2019-10-21 14:50:07 -0700143 switch (version) {
144 case 0:
145 return readFromParcel0(data);
146 default:
147 ALOGE("%s: unsupported parcel version: %d", __func__, version);
148 return INVALID_OPERATION;
Ray Essickba8c4842019-01-18 11:35:33 -0800149 }
150}
151
Ray Essickf27e9872019-12-07 06:28:46 -0800152status_t mediametrics::Item::readFromParcel0(const Parcel& data) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700153 const char *s = data.readCString();
154 mKey = s == nullptr ? "" : s;
155 int32_t pid, uid;
156 status_t status = data.readInt32(&pid) ?: data.readInt32(&uid);
157 if (status != NO_ERROR) return status;
158 mPid = (pid_t)pid;
159 mUid = (uid_t)uid;
160 s = data.readCString();
161 mPkgName = s == nullptr ? "" : s;
162 int32_t count;
163 int64_t version, timestamp;
164 status = data.readInt64(&version) ?: data.readInt64(&timestamp) ?: data.readInt32(&count);
165 if (status != NO_ERROR) return status;
166 if (count < 0) return BAD_VALUE;
167 mPkgVersionCode = version;
168 mTimestamp = timestamp;
Andy Hung47e58d62019-12-06 18:40:19 -0800169 for (int i = 0; i < count; i++) {
170 Prop prop;
171 status_t status = prop.readFromParcel(data);
Andy Hung3253f2d2019-10-21 14:50:07 -0700172 if (status != NO_ERROR) return status;
Andy Hung47e58d62019-12-06 18:40:19 -0800173 mProps[prop.getName()] = std::move(prop);
Ray Essick3938dc62016-11-01 08:56:56 -0700174 }
Andy Hung3253f2d2019-10-21 14:50:07 -0700175 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700176}
177
Ray Essickf27e9872019-12-07 06:28:46 -0800178status_t mediametrics::Item::writeToParcel(Parcel *data) const {
Andy Hung3253f2d2019-10-21 14:50:07 -0700179 if (data == nullptr) return BAD_VALUE;
Ray Essickba8c4842019-01-18 11:35:33 -0800180
Andy Hung3253f2d2019-10-21 14:50:07 -0700181 const int32_t version = 0;
182 status_t status = data->writeInt32(version);
183 if (status != NO_ERROR) return status;
Ray Essick3938dc62016-11-01 08:56:56 -0700184
Andy Hung3253f2d2019-10-21 14:50:07 -0700185 switch (version) {
186 case 0:
187 return writeToParcel0(data);
188 default:
189 ALOGE("%s: unsupported parcel version: %d", __func__, version);
190 return INVALID_OPERATION;
Ray Essickba8c4842019-01-18 11:35:33 -0800191 }
192}
193
Ray Essickf27e9872019-12-07 06:28:46 -0800194status_t mediametrics::Item::writeToParcel0(Parcel *data) const {
Andy Hung3253f2d2019-10-21 14:50:07 -0700195 status_t status =
196 data->writeCString(mKey.c_str())
197 ?: data->writeInt32(mPid)
198 ?: data->writeInt32(mUid)
199 ?: data->writeCString(mPkgName.c_str())
200 ?: data->writeInt64(mPkgVersionCode)
201 ?: data->writeInt64(mTimestamp);
202 if (status != NO_ERROR) return status;
Ray Essick3938dc62016-11-01 08:56:56 -0700203
Andy Hung47e58d62019-12-06 18:40:19 -0800204 data->writeInt32((int32_t)mProps.size());
205 for (auto &prop : *this) {
206 status = prop.writeToParcel(data);
Andy Hung3253f2d2019-10-21 14:50:07 -0700207 if (status != NO_ERROR) return status;
Ray Essick3938dc62016-11-01 08:56:56 -0700208 }
Andy Hung3253f2d2019-10-21 14:50:07 -0700209 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700210}
211
Ray Essickf27e9872019-12-07 06:28:46 -0800212const char *mediametrics::Item::toCString() {
Ray Essick20147322018-11-17 09:08:39 -0800213 return toCString(PROTO_LAST);
214}
215
Ray Essickf27e9872019-12-07 06:28:46 -0800216const char * mediametrics::Item::toCString(int version) {
Ray Essick20147322018-11-17 09:08:39 -0800217 std::string val = toString(version);
218 return strdup(val.c_str());
219}
220
Ray Essickf27e9872019-12-07 06:28:46 -0800221std::string mediametrics::Item::toString() const {
Ray Essick5b77bd22018-01-23 16:11:06 -0800222 return toString(PROTO_LAST);
Ray Essickf65f4212017-08-31 11:41:19 -0700223}
Ray Essick3938dc62016-11-01 08:56:56 -0700224
Ray Essickf27e9872019-12-07 06:28:46 -0800225std::string mediametrics::Item::toString(int version) const {
Ray Essick783bd0d2018-01-11 11:10:35 -0800226 std::string result;
Andy Hung1efc9c62019-12-03 13:43:33 -0800227 char buffer[kMaxPropertyStringSize];
Ray Essick3938dc62016-11-01 08:56:56 -0700228
Andy Hung1efc9c62019-12-03 13:43:33 -0800229 snprintf(buffer, sizeof(buffer), "[%d:%s:%d:%d:%lld:%s:%zu:",
230 version, mKey.c_str(), mPid, mUid, (long long)mTimestamp,
Andy Hung47e58d62019-12-06 18:40:19 -0800231 mPkgName.c_str(), mProps.size());
Ray Essickf65f4212017-08-31 11:41:19 -0700232 result.append(buffer);
Andy Hung47e58d62019-12-06 18:40:19 -0800233 for (auto &prop : *this) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800234 prop.toStringBuffer(buffer, sizeof(buffer));
Andy Hungaeef7882019-10-18 15:18:14 -0700235 result.append(buffer);
Ray Essick3938dc62016-11-01 08:56:56 -0700236 }
Andy Hung1efc9c62019-12-03 13:43:33 -0800237 result.append("]");
Ray Essick3938dc62016-11-01 08:56:56 -0700238 return result;
239}
240
241// for the lazy, we offer methods that finds the service and
242// calls the appropriate daemon
Ray Essickf27e9872019-12-07 06:28:46 -0800243bool mediametrics::Item::selfrecord() {
Andy Hunga87e69c2019-10-18 10:07:40 -0700244 ALOGD_IF(DEBUG_API, "%s: delivering %s", __func__, this->toString().c_str());
Andy Hung47e58d62019-12-06 18:40:19 -0800245 sp<IMediaMetricsService> svc = getService();
Ray Essick3938dc62016-11-01 08:56:56 -0700246 if (svc != NULL) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700247 status_t status = svc->submit(this);
248 if (status != NO_ERROR) {
249 ALOGW("%s: failed to record: %s", __func__, this->toString().c_str());
Ray Essick2ab3c432017-10-02 09:29:49 -0700250 return false;
251 }
Ray Essick3938dc62016-11-01 08:56:56 -0700252 return true;
253 } else {
254 return false;
255 }
256}
257
Ray Essick3938dc62016-11-01 08:56:56 -0700258//static
Andy Hung1efc9c62019-12-03 13:43:33 -0800259bool BaseItem::isEnabled() {
Andy Hunga87e69c2019-10-18 10:07:40 -0700260 // completely skip logging from certain UIDs. We do this here
261 // to avoid the multi-second timeouts while we learn that
262 // sepolicy will not let us find the service.
263 // We do this only for a select set of UIDs
264 // The sepolicy protection is still in place, we just want a faster
265 // response from this specific, small set of uids.
Ray Essick3938dc62016-11-01 08:56:56 -0700266
Andy Hunga87e69c2019-10-18 10:07:40 -0700267 // This is checked only once in the lifetime of the process.
268 const uid_t uid = getuid();
269 switch (uid) {
270 case AID_RADIO: // telephony subsystem, RIL
271 return false;
272 }
273
Ray Essickf27e9872019-12-07 06:28:46 -0800274 int enabled = property_get_int32(Item::EnabledProperty, -1);
Ray Essick3938dc62016-11-01 08:56:56 -0700275 if (enabled == -1) {
Ray Essickf27e9872019-12-07 06:28:46 -0800276 enabled = property_get_int32(Item::EnabledPropertyPersist, -1);
Ray Essick3938dc62016-11-01 08:56:56 -0700277 }
278 if (enabled == -1) {
Ray Essickf27e9872019-12-07 06:28:46 -0800279 enabled = Item::EnabledProperty_default;
Ray Essick3938dc62016-11-01 08:56:56 -0700280 }
Andy Hunga87e69c2019-10-18 10:07:40 -0700281 return enabled > 0;
Ray Essick3938dc62016-11-01 08:56:56 -0700282}
283
Ray Essick2ab3c432017-10-02 09:29:49 -0700284// monitor health of our connection to the metrics service
285class MediaMetricsDeathNotifier : public IBinder::DeathRecipient {
286 virtual void binderDied(const wp<IBinder> &) {
287 ALOGW("Reacquire service connection on next request");
Andy Hung1efc9c62019-12-03 13:43:33 -0800288 BaseItem::dropInstance();
Ray Essick2ab3c432017-10-02 09:29:49 -0700289 }
290};
291
Andy Hunga87e69c2019-10-18 10:07:40 -0700292static sp<MediaMetricsDeathNotifier> sNotifier;
293// static
Ray Essickf27e9872019-12-07 06:28:46 -0800294sp<IMediaMetricsService> BaseItem::sMediaMetricsService;
Andy Hunga87e69c2019-10-18 10:07:40 -0700295static std::mutex sServiceMutex;
296static int sRemainingBindAttempts = SVC_TRIES;
Ray Essick2ab3c432017-10-02 09:29:49 -0700297
298// static
Andy Hung1efc9c62019-12-03 13:43:33 -0800299void BaseItem::dropInstance() {
Andy Hunga87e69c2019-10-18 10:07:40 -0700300 std::lock_guard _l(sServiceMutex);
301 sRemainingBindAttempts = SVC_TRIES;
Ray Essickf27e9872019-12-07 06:28:46 -0800302 sMediaMetricsService = nullptr;
Ray Essick2ab3c432017-10-02 09:29:49 -0700303}
304
Andy Hung1efc9c62019-12-03 13:43:33 -0800305// static
306bool BaseItem::submitBuffer(const char *buffer, size_t size) {
307/*
Ray Essickf27e9872019-12-07 06:28:46 -0800308 mediametrics::Item item;
Andy Hung1efc9c62019-12-03 13:43:33 -0800309 status_t status = item.readFromByteString(buffer, size);
310 ALOGD("%s: status:%d, size:%zu, item:%s", __func__, status, size, item.toString().c_str());
311 return item.selfrecord();
312 */
313
314 ALOGD_IF(DEBUG_API, "%s: delivering %zu bytes", __func__, size);
Andy Hung47e58d62019-12-06 18:40:19 -0800315 sp<IMediaMetricsService> svc = getService();
Andy Hung1efc9c62019-12-03 13:43:33 -0800316 if (svc != nullptr) {
317 const status_t status = svc->submitBuffer(buffer, size);
318 if (status != NO_ERROR) {
319 ALOGW("%s: failed(%d) to record: %zu bytes", __func__, status, size);
320 return false;
321 }
322 return true;
323 }
324 return false;
325}
326
Ray Essick3938dc62016-11-01 08:56:56 -0700327//static
Andy Hung47e58d62019-12-06 18:40:19 -0800328sp<IMediaMetricsService> BaseItem::getService() {
Ray Essickd38e1742017-01-23 15:17:06 -0800329 static const char *servicename = "media.metrics";
Andy Hunga87e69c2019-10-18 10:07:40 -0700330 static const bool enabled = isEnabled(); // singleton initialized
Ray Essick3938dc62016-11-01 08:56:56 -0700331
332 if (enabled == false) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700333 ALOGD_IF(DEBUG_SERVICEACCESS, "disabled");
334 return nullptr;
Ray Essick3938dc62016-11-01 08:56:56 -0700335 }
Andy Hunga87e69c2019-10-18 10:07:40 -0700336 std::lock_guard _l(sServiceMutex);
337 // think of remainingBindAttempts as telling us whether service == nullptr because
338 // (1) we haven't tried to initialize it yet
339 // (2) we've tried to initialize it, but failed.
Ray Essickf27e9872019-12-07 06:28:46 -0800340 if (sMediaMetricsService == nullptr && sRemainingBindAttempts > 0) {
Ray Essick3938dc62016-11-01 08:56:56 -0700341 const char *badness = "";
Andy Hunga87e69c2019-10-18 10:07:40 -0700342 sp<IServiceManager> sm = defaultServiceManager();
343 if (sm != nullptr) {
344 sp<IBinder> binder = sm->getService(String16(servicename));
345 if (binder != nullptr) {
Ray Essickf27e9872019-12-07 06:28:46 -0800346 sMediaMetricsService = interface_cast<IMediaMetricsService>(binder);
Andy Hunga87e69c2019-10-18 10:07:40 -0700347 sNotifier = new MediaMetricsDeathNotifier();
348 binder->linkToDeath(sNotifier);
Ray Essick3938dc62016-11-01 08:56:56 -0700349 } else {
Andy Hunga87e69c2019-10-18 10:07:40 -0700350 badness = "did not find service";
Ray Essick3938dc62016-11-01 08:56:56 -0700351 }
Andy Hunga87e69c2019-10-18 10:07:40 -0700352 } else {
353 badness = "No Service Manager access";
Ray Essick3938dc62016-11-01 08:56:56 -0700354 }
Ray Essickf27e9872019-12-07 06:28:46 -0800355 if (sMediaMetricsService == nullptr) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700356 if (sRemainingBindAttempts > 0) {
357 sRemainingBindAttempts--;
358 }
359 ALOGD_IF(DEBUG_SERVICEACCESS, "%s: unable to bind to service %s: %s",
360 __func__, servicename, badness);
361 }
Ray Essick3938dc62016-11-01 08:56:56 -0700362 }
Ray Essickf27e9872019-12-07 06:28:46 -0800363 return sMediaMetricsService;
Ray Essick3938dc62016-11-01 08:56:56 -0700364}
365
Andy Hung1efc9c62019-12-03 13:43:33 -0800366
Ray Essickf27e9872019-12-07 06:28:46 -0800367status_t mediametrics::Item::writeToByteString(char **pbuffer, size_t *plength) const
Andy Hung3253f2d2019-10-21 14:50:07 -0700368{
369 if (pbuffer == nullptr || plength == nullptr)
370 return BAD_VALUE;
371
372 // get size
373 const size_t keySizeZeroTerminated = strlen(mKey.c_str()) + 1;
374 if (keySizeZeroTerminated > UINT16_MAX) {
375 ALOGW("%s: key size %zu too large", __func__, keySizeZeroTerminated);
376 return INVALID_OPERATION;
377 }
378 const uint16_t version = 0;
Andy Hung1efc9c62019-12-03 13:43:33 -0800379 const uint32_t header_size =
380 sizeof(uint32_t) // total size
381 + sizeof(header_size) // header size
382 + sizeof(version) // encoding version
383 + sizeof(uint16_t) // key size
Andy Hung3253f2d2019-10-21 14:50:07 -0700384 + keySizeZeroTerminated // key, zero terminated
Andy Hung1efc9c62019-12-03 13:43:33 -0800385 + sizeof(int32_t) // pid
386 + sizeof(int32_t) // uid
387 + sizeof(int64_t) // timestamp
Andy Hung3253f2d2019-10-21 14:50:07 -0700388 ;
389
Andy Hung1efc9c62019-12-03 13:43:33 -0800390 uint32_t size = header_size
Andy Hung3253f2d2019-10-21 14:50:07 -0700391 + sizeof(uint32_t) // # properties
392 ;
Andy Hung47e58d62019-12-06 18:40:19 -0800393 for (auto &prop : *this) {
394 const size_t propSize = prop.getByteStringSize();
Andy Hung1efc9c62019-12-03 13:43:33 -0800395 if (propSize > UINT16_MAX) {
Andy Hung47e58d62019-12-06 18:40:19 -0800396 ALOGW("%s: prop %s size %zu too large", __func__, prop.getName(), propSize);
Andy Hung3253f2d2019-10-21 14:50:07 -0700397 return INVALID_OPERATION;
398 }
Andy Hung1efc9c62019-12-03 13:43:33 -0800399 if (__builtin_add_overflow(size, propSize, &size)) {
Andy Hung47e58d62019-12-06 18:40:19 -0800400 ALOGW("%s: item size overflow at property %s", __func__, prop.getName());
Andy Hung1efc9c62019-12-03 13:43:33 -0800401 return INVALID_OPERATION;
402 }
Andy Hung3253f2d2019-10-21 14:50:07 -0700403 }
404
Andy Hung1efc9c62019-12-03 13:43:33 -0800405 // since we fill every byte in the buffer (there is no padding),
406 // malloc is used here instead of calloc.
407 char * const build = (char *)malloc(size);
Andy Hung3253f2d2019-10-21 14:50:07 -0700408 if (build == nullptr) return NO_MEMORY;
409
410 char *filling = build;
Andy Hung1efc9c62019-12-03 13:43:33 -0800411 char *buildmax = build + size;
412 if (insert((uint32_t)size, &filling, buildmax) != NO_ERROR
413 || insert(header_size, &filling, buildmax) != NO_ERROR
Andy Hung3253f2d2019-10-21 14:50:07 -0700414 || insert(version, &filling, buildmax) != NO_ERROR
415 || insert((uint16_t)keySizeZeroTerminated, &filling, buildmax) != NO_ERROR
416 || insert(mKey.c_str(), &filling, buildmax) != NO_ERROR
417 || insert((int32_t)mPid, &filling, buildmax) != NO_ERROR
418 || insert((int32_t)mUid, &filling, buildmax) != NO_ERROR
419 || insert((int64_t)mTimestamp, &filling, buildmax) != NO_ERROR
Andy Hung47e58d62019-12-06 18:40:19 -0800420 || insert((uint32_t)mProps.size(), &filling, buildmax) != NO_ERROR) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800421 ALOGE("%s:could not write header", __func__); // shouldn't happen
Andy Hung3253f2d2019-10-21 14:50:07 -0700422 free(build);
423 return INVALID_OPERATION;
424 }
Andy Hung47e58d62019-12-06 18:40:19 -0800425 for (auto &prop : *this) {
426 if (prop.writeToByteString(&filling, buildmax) != NO_ERROR) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700427 free(build);
Andy Hung1efc9c62019-12-03 13:43:33 -0800428 // shouldn't happen
Andy Hung47e58d62019-12-06 18:40:19 -0800429 ALOGE("%s:could not write prop %s", __func__, prop.getName());
Andy Hung3253f2d2019-10-21 14:50:07 -0700430 return INVALID_OPERATION;
431 }
432 }
433
434 if (filling != buildmax) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800435 ALOGE("%s: problems populating; wrote=%d planned=%d",
436 __func__, (int)(filling - build), (int)size);
Andy Hung3253f2d2019-10-21 14:50:07 -0700437 free(build);
438 return INVALID_OPERATION;
439 }
440 *pbuffer = build;
Andy Hung1efc9c62019-12-03 13:43:33 -0800441 *plength = size;
Andy Hung3253f2d2019-10-21 14:50:07 -0700442 return NO_ERROR;
443}
444
Ray Essickf27e9872019-12-07 06:28:46 -0800445status_t mediametrics::Item::readFromByteString(const char *bufferptr, size_t length)
Andy Hung3253f2d2019-10-21 14:50:07 -0700446{
447 if (bufferptr == nullptr) return BAD_VALUE;
448
449 const char *read = bufferptr;
450 const char *readend = bufferptr + length;
451
Andy Hung1efc9c62019-12-03 13:43:33 -0800452 uint32_t size;
453 uint32_t header_size;
454 uint16_t version;
455 uint16_t key_size;
Andy Hungb7aadb32019-12-09 19:40:42 -0800456 std::string key;
Andy Hung3253f2d2019-10-21 14:50:07 -0700457 int32_t pid;
458 int32_t uid;
459 int64_t timestamp;
460 uint32_t propCount;
Andy Hung1efc9c62019-12-03 13:43:33 -0800461 if (extract(&size, &read, readend) != NO_ERROR
462 || extract(&header_size, &read, readend) != NO_ERROR
Andy Hung3253f2d2019-10-21 14:50:07 -0700463 || extract(&version, &read, readend) != NO_ERROR
Andy Hung1efc9c62019-12-03 13:43:33 -0800464 || extract(&key_size, &read, readend) != NO_ERROR
Andy Hung3253f2d2019-10-21 14:50:07 -0700465 || extract(&key, &read, readend) != NO_ERROR
466 || extract(&pid, &read, readend) != NO_ERROR
467 || extract(&uid, &read, readend) != NO_ERROR
468 || extract(&timestamp, &read, readend) != NO_ERROR
Andy Hung1efc9c62019-12-03 13:43:33 -0800469 || size > length
Andy Hungb7aadb32019-12-09 19:40:42 -0800470 || key.size() + 1 != key_size
Andy Hung1efc9c62019-12-03 13:43:33 -0800471 || header_size > size) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800472 ALOGW("%s: invalid header", __func__);
Andy Hung3253f2d2019-10-21 14:50:07 -0700473 return INVALID_OPERATION;
474 }
Andy Hungb7aadb32019-12-09 19:40:42 -0800475 mKey = std::move(key);
Andy Hung3253f2d2019-10-21 14:50:07 -0700476 const size_t pos = read - bufferptr;
Andy Hung1efc9c62019-12-03 13:43:33 -0800477 if (pos > header_size) {
478 ALOGW("%s: invalid header pos:%zu > header_size:%u",
479 __func__, pos, header_size);
Andy Hung3253f2d2019-10-21 14:50:07 -0700480 return INVALID_OPERATION;
Andy Hung1efc9c62019-12-03 13:43:33 -0800481 } else if (pos < header_size) {
482 ALOGW("%s: mismatched header pos:%zu < header_size:%u, advancing",
483 __func__, pos, header_size);
484 read += (header_size - pos);
Andy Hung3253f2d2019-10-21 14:50:07 -0700485 }
486 if (extract(&propCount, &read, readend) != NO_ERROR) {
487 ALOGD("%s: cannot read prop count", __func__);
488 return INVALID_OPERATION;
489 }
490 mPid = pid;
491 mUid = uid;
492 mTimestamp = timestamp;
493 for (size_t i = 0; i < propCount; ++i) {
Andy Hung47e58d62019-12-06 18:40:19 -0800494 Prop prop;
495 if (prop.readFromByteString(&read, readend) != NO_ERROR) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800496 ALOGW("%s: cannot read prop %zu", __func__, i);
Andy Hung3253f2d2019-10-21 14:50:07 -0700497 return INVALID_OPERATION;
498 }
Andy Hung47e58d62019-12-06 18:40:19 -0800499 mProps[prop.getName()] = std::move(prop);
Andy Hung3253f2d2019-10-21 14:50:07 -0700500 }
501 return NO_ERROR;
502}
503
Ray Essickf27e9872019-12-07 06:28:46 -0800504status_t mediametrics::Item::Prop::readFromParcel(const Parcel& data)
Andy Hung3253f2d2019-10-21 14:50:07 -0700505{
506 const char *key = data.readCString();
507 if (key == nullptr) return BAD_VALUE;
508 int32_t type;
509 status_t status = data.readInt32(&type);
510 if (status != NO_ERROR) return status;
511 switch (type) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800512 case mediametrics::kTypeInt32: {
513 int32_t value;
514 status = data.readInt32(&value);
515 if (status != NO_ERROR) return status;
516 mElem = value;
517 } break;
518 case mediametrics::kTypeInt64: {
519 int64_t value;
520 status = data.readInt64(&value);
521 if (status != NO_ERROR) return status;
522 mElem = value;
523 } break;
524 case mediametrics::kTypeDouble: {
525 double value;
526 status = data.readDouble(&value);
527 if (status != NO_ERROR) return status;
528 mElem = value;
529 } break;
Andy Hung47e58d62019-12-06 18:40:19 -0800530 case mediametrics::kTypeCString: {
Andy Hung3253f2d2019-10-21 14:50:07 -0700531 const char *s = data.readCString();
532 if (s == nullptr) return BAD_VALUE;
Andy Hungb7aadb32019-12-09 19:40:42 -0800533 mElem = s;
534 } break;
Andy Hung47e58d62019-12-06 18:40:19 -0800535 case mediametrics::kTypeRate: {
Andy Hung3253f2d2019-10-21 14:50:07 -0700536 std::pair<int64_t, int64_t> rate;
537 status = data.readInt64(&rate.first)
538 ?: data.readInt64(&rate.second);
Andy Hungb7aadb32019-12-09 19:40:42 -0800539 if (status != NO_ERROR) return status;
540 mElem = rate;
541 } break;
542 case mediametrics::kTypeNone: {
543 mElem = std::monostate{};
544 } break;
Andy Hung3253f2d2019-10-21 14:50:07 -0700545 default:
Andy Hungb7aadb32019-12-09 19:40:42 -0800546 ALOGE("%s: reading bad item type: %d", __func__, type);
Andy Hung3253f2d2019-10-21 14:50:07 -0700547 return BAD_VALUE;
548 }
Andy Hungb7aadb32019-12-09 19:40:42 -0800549 setName(key);
550 return NO_ERROR;
Andy Hung3253f2d2019-10-21 14:50:07 -0700551}
552
Ray Essickf27e9872019-12-07 06:28:46 -0800553status_t mediametrics::Item::Prop::readFromByteString(
Andy Hung3253f2d2019-10-21 14:50:07 -0700554 const char **bufferpptr, const char *bufferptrmax)
555{
556 uint16_t len;
Andy Hungb7aadb32019-12-09 19:40:42 -0800557 std::string name;
Andy Hung3253f2d2019-10-21 14:50:07 -0700558 uint8_t type;
559 status_t status = extract(&len, bufferpptr, bufferptrmax)
560 ?: extract(&type, bufferpptr, bufferptrmax)
561 ?: extract(&name, bufferpptr, bufferptrmax);
562 if (status != NO_ERROR) return status;
Andy Hungb7aadb32019-12-09 19:40:42 -0800563 switch (type) {
564 case mediametrics::kTypeInt32: {
565 int32_t value;
566 status = extract(&value, bufferpptr, bufferptrmax);
567 if (status != NO_ERROR) return status;
568 mElem = value;
569 } break;
570 case mediametrics::kTypeInt64: {
571 int64_t value;
572 status = extract(&value, bufferpptr, bufferptrmax);
573 if (status != NO_ERROR) return status;
574 mElem = value;
575 } break;
576 case mediametrics::kTypeDouble: {
577 double value;
578 status = extract(&value, bufferpptr, bufferptrmax);
579 if (status != NO_ERROR) return status;
580 mElem = value;
581 } break;
582 case mediametrics::kTypeRate: {
583 std::pair<int64_t, int64_t> value;
584 status = extract(&value.first, bufferpptr, bufferptrmax)
585 ?: extract(&value.second, bufferpptr, bufferptrmax);
586 if (status != NO_ERROR) return status;
587 mElem = value;
588 } break;
589 case mediametrics::kTypeCString: {
590 std::string value;
591 status = extract(&value, bufferpptr, bufferptrmax);
592 if (status != NO_ERROR) return status;
593 mElem = std::move(value);
594 } break;
595 case mediametrics::kTypeNone: {
596 mElem = std::monostate{};
597 } break;
Andy Hung3253f2d2019-10-21 14:50:07 -0700598 default:
Andy Hung3253f2d2019-10-21 14:50:07 -0700599 ALOGE("%s: found bad prop type: %d, name %s",
Andy Hungb7aadb32019-12-09 19:40:42 -0800600 __func__, (int)type, mName.c_str()); // no payload sent
Andy Hung3253f2d2019-10-21 14:50:07 -0700601 return BAD_VALUE;
602 }
Andy Hungb7aadb32019-12-09 19:40:42 -0800603 mName = name;
604 return NO_ERROR;
Andy Hung3253f2d2019-10-21 14:50:07 -0700605}
606
Ray Essickf27e9872019-12-07 06:28:46 -0800607} // namespace android::mediametrics