Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 17 | #define LOG_TAG "mediametrics::Item" |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 18 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 23 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 24 | #include <mutex> |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 25 | #include <set> |
Andy Hung | 5088e1a | 2021-11-11 09:17:21 -0800 | [diff] [blame] | 26 | #include <unordered_map> |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 27 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 28 | #include <binder/Parcel.h> |
Mark Punzalan | 3ebe121 | 2022-05-17 08:15:49 +0000 | [diff] [blame] | 29 | #include <cutils/multiuser.h> |
Andy Hung | 7d39108 | 2020-04-18 15:03:51 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 31 | #include <utils/Errors.h> |
| 32 | #include <utils/Log.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 33 | #include <utils/SortedVector.h> |
| 34 | #include <utils/threads.h> |
| 35 | |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 36 | #include <android/media/BnMediaMetricsService.h> // for direct Binder access |
| 37 | #include <android/media/IMediaMetricsService.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 38 | #include <binder/IServiceManager.h> |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 39 | #include <media/MediaMetricsItem.h> |
Ray Essick | 79a89ef | 2017-04-24 15:52:54 -0700 | [diff] [blame] | 40 | #include <private/android_filesystem_config.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 41 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 42 | // Max per-property string size before truncation in toString(). |
| 43 | // Do not make too large, as this is used for dumpsys purposes. |
| 44 | static constexpr size_t kMaxPropertyStringSize = 4096; |
| 45 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 46 | namespace android::mediametrics { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 47 | |
| 48 | #define DEBUG_SERVICEACCESS 0 |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 49 | #define DEBUG_API 0 |
| 50 | #define DEBUG_ALLOCATIONS 0 |
| 51 | |
| 52 | // after this many failed attempts, we stop trying [from this process] and just say that |
| 53 | // the service is off. |
| 54 | #define SVC_TRIES 2 |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 55 | |
Andy Hung | 5088e1a | 2021-11-11 09:17:21 -0800 | [diff] [blame] | 56 | static const std::unordered_map<std::string, int32_t>& getErrorStringMap() { |
| 57 | // DO NOT MODIFY VALUES (OK to add new ones). |
| 58 | // This may be found in frameworks/av/media/libmediametrics/include/MediaMetricsConstants.h |
| 59 | static std::unordered_map<std::string, int32_t> map{ |
| 60 | {"", NO_ERROR}, |
Andy Hung | 73dc2f9 | 2021-12-07 21:50:04 -0800 | [diff] [blame] | 61 | {AMEDIAMETRICS_PROP_STATUS_VALUE_OK, NO_ERROR}, |
| 62 | {AMEDIAMETRICS_PROP_STATUS_VALUE_ARGUMENT, BAD_VALUE}, |
| 63 | {AMEDIAMETRICS_PROP_STATUS_VALUE_IO, DEAD_OBJECT}, |
| 64 | {AMEDIAMETRICS_PROP_STATUS_VALUE_MEMORY, NO_MEMORY}, |
| 65 | {AMEDIAMETRICS_PROP_STATUS_VALUE_SECURITY, PERMISSION_DENIED}, |
| 66 | {AMEDIAMETRICS_PROP_STATUS_VALUE_STATE, INVALID_OPERATION}, |
| 67 | {AMEDIAMETRICS_PROP_STATUS_VALUE_TIMEOUT, WOULD_BLOCK}, |
| 68 | {AMEDIAMETRICS_PROP_STATUS_VALUE_UNKNOWN, UNKNOWN_ERROR}, |
Andy Hung | 5088e1a | 2021-11-11 09:17:21 -0800 | [diff] [blame] | 69 | }; |
| 70 | return map; |
| 71 | } |
| 72 | |
Andy Hung | 73dc2f9 | 2021-12-07 21:50:04 -0800 | [diff] [blame] | 73 | status_t statusStringToStatus(const char *error) { |
Andy Hung | 5088e1a | 2021-11-11 09:17:21 -0800 | [diff] [blame] | 74 | const auto& map = getErrorStringMap(); |
Greg Kaiser | 9620663 | 2021-11-29 06:52:17 -0800 | [diff] [blame] | 75 | if (error == nullptr || error[0] == '\0') return NO_ERROR; |
Andy Hung | 5088e1a | 2021-11-11 09:17:21 -0800 | [diff] [blame] | 76 | auto it = map.find(error); |
| 77 | if (it != map.end()) { |
| 78 | return it->second; |
| 79 | } |
| 80 | return UNKNOWN_ERROR; |
| 81 | } |
| 82 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 83 | mediametrics::Item* mediametrics::Item::convert(mediametrics_handle_t handle) { |
| 84 | mediametrics::Item *item = (android::mediametrics::Item *) handle; |
Ray Essick | bf536ac | 2019-08-26 11:04:28 -0700 | [diff] [blame] | 85 | return item; |
| 86 | } |
| 87 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 88 | mediametrics_handle_t mediametrics::Item::convert(mediametrics::Item *item ) { |
Ray Essick | bf536ac | 2019-08-26 11:04:28 -0700 | [diff] [blame] | 89 | mediametrics_handle_t handle = (mediametrics_handle_t) item; |
| 90 | return handle; |
| 91 | } |
| 92 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 93 | mediametrics::Item::~Item() { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 94 | if (DEBUG_ALLOCATIONS) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 95 | ALOGD("Destroy mediametrics::Item @ %p", this); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 96 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 99 | mediametrics::Item &mediametrics::Item::setTimestamp(nsecs_t ts) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 100 | mTimestamp = ts; |
| 101 | return *this; |
| 102 | } |
| 103 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 104 | nsecs_t mediametrics::Item::getTimestamp() const { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 105 | return mTimestamp; |
| 106 | } |
| 107 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 108 | mediametrics::Item &mediametrics::Item::setPid(pid_t pid) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 109 | mPid = pid; |
| 110 | return *this; |
| 111 | } |
| 112 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 113 | pid_t mediametrics::Item::getPid() const { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 114 | return mPid; |
| 115 | } |
| 116 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 117 | mediametrics::Item &mediametrics::Item::setUid(uid_t uid) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 118 | mUid = uid; |
| 119 | return *this; |
| 120 | } |
| 121 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 122 | uid_t mediametrics::Item::getUid() const { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 123 | return mUid; |
| 124 | } |
| 125 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 126 | mediametrics::Item &mediametrics::Item::setPkgName(const std::string &pkgName) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 127 | mPkgName = pkgName; |
| 128 | return *this; |
| 129 | } |
| 130 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 131 | mediametrics::Item &mediametrics::Item::setPkgVersionCode(int64_t pkgVersionCode) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 132 | mPkgVersionCode = pkgVersionCode; |
| 133 | return *this; |
| 134 | } |
| 135 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 136 | int64_t mediametrics::Item::getPkgVersionCode() const { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 137 | return mPkgVersionCode; |
| 138 | } |
| 139 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 140 | // remove indicated keys and their values |
| 141 | // return value is # keys removed |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 142 | size_t mediametrics::Item::filter(size_t n, const char *attrs[]) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 143 | size_t zapped = 0; |
| 144 | for (size_t i = 0; i < n; ++i) { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 145 | zapped += mProps.erase(attrs[i]); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 146 | } |
| 147 | return zapped; |
| 148 | } |
| 149 | |
| 150 | // remove any keys NOT in the provided list |
| 151 | // return value is # keys removed |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 152 | size_t mediametrics::Item::filterNot(size_t n, const char *attrs[]) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 153 | std::set<std::string> check(attrs, attrs + n); |
| 154 | size_t zapped = 0; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 155 | for (auto it = mProps.begin(); it != mProps.end();) { |
| 156 | if (check.find(it->first) != check.end()) { |
| 157 | ++it; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 158 | } else { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 159 | it = mProps.erase(it); |
| 160 | ++zapped; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | return zapped; |
| 164 | } |
| 165 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 166 | // Parcel / serialize things for binder calls |
| 167 | // |
| 168 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 169 | status_t mediametrics::Item::readFromParcel(const Parcel& data) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 170 | int32_t version; |
| 171 | status_t status = data.readInt32(&version); |
| 172 | if (status != NO_ERROR) return status; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 173 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 174 | switch (version) { |
| 175 | case 0: |
| 176 | return readFromParcel0(data); |
| 177 | default: |
| 178 | ALOGE("%s: unsupported parcel version: %d", __func__, version); |
| 179 | return INVALID_OPERATION; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 183 | status_t mediametrics::Item::readFromParcel0(const Parcel& data) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 184 | const char *s = data.readCString(); |
| 185 | mKey = s == nullptr ? "" : s; |
| 186 | int32_t pid, uid; |
| 187 | status_t status = data.readInt32(&pid) ?: data.readInt32(&uid); |
| 188 | if (status != NO_ERROR) return status; |
| 189 | mPid = (pid_t)pid; |
| 190 | mUid = (uid_t)uid; |
| 191 | s = data.readCString(); |
| 192 | mPkgName = s == nullptr ? "" : s; |
| 193 | int32_t count; |
| 194 | int64_t version, timestamp; |
| 195 | status = data.readInt64(&version) ?: data.readInt64(×tamp) ?: data.readInt32(&count); |
| 196 | if (status != NO_ERROR) return status; |
| 197 | if (count < 0) return BAD_VALUE; |
| 198 | mPkgVersionCode = version; |
| 199 | mTimestamp = timestamp; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 200 | for (int i = 0; i < count; i++) { |
| 201 | Prop prop; |
| 202 | status_t status = prop.readFromParcel(data); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 203 | if (status != NO_ERROR) return status; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 204 | mProps[prop.getName()] = std::move(prop); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 205 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 206 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 209 | status_t mediametrics::Item::writeToParcel(Parcel *data) const { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 210 | if (data == nullptr) return BAD_VALUE; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 211 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 212 | const int32_t version = 0; |
| 213 | status_t status = data->writeInt32(version); |
| 214 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 215 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 216 | switch (version) { |
| 217 | case 0: |
| 218 | return writeToParcel0(data); |
| 219 | default: |
| 220 | ALOGE("%s: unsupported parcel version: %d", __func__, version); |
| 221 | return INVALID_OPERATION; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 225 | status_t mediametrics::Item::writeToParcel0(Parcel *data) const { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 226 | status_t status = |
| 227 | data->writeCString(mKey.c_str()) |
| 228 | ?: data->writeInt32(mPid) |
| 229 | ?: data->writeInt32(mUid) |
| 230 | ?: data->writeCString(mPkgName.c_str()) |
| 231 | ?: data->writeInt64(mPkgVersionCode) |
| 232 | ?: data->writeInt64(mTimestamp); |
| 233 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 234 | |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 235 | data->writeInt32((int32_t)mProps.size()); |
| 236 | for (auto &prop : *this) { |
| 237 | status = prop.writeToParcel(data); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 238 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 239 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 240 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 243 | const char *mediametrics::Item::toCString() { |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame] | 244 | std::string val = toString(); |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 245 | return strdup(val.c_str()); |
| 246 | } |
| 247 | |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame] | 248 | /* |
| 249 | * Similar to audio_utils/clock.h but customized for displaying mediametrics time. |
| 250 | */ |
| 251 | |
| 252 | void nsToString(int64_t ns, char *buffer, size_t bufferSize, PrintFormat format) |
| 253 | { |
| 254 | if (bufferSize == 0) return; |
| 255 | |
| 256 | const int one_second = 1000000000; |
| 257 | const time_t sec = ns / one_second; |
| 258 | struct tm tm; |
| 259 | |
| 260 | // Supported on bionic, glibc, and macOS, but not mingw. |
| 261 | if (localtime_r(&sec, &tm) == NULL) { |
| 262 | buffer[0] = '\0'; |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | switch (format) { |
| 267 | default: |
| 268 | case kPrintFormatLong: |
| 269 | if (snprintf(buffer, bufferSize, "%02d-%02d %02d:%02d:%02d.%03d", |
| 270 | tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range |
| 271 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 272 | (int)(ns % one_second / 1000000)) < 0) { |
| 273 | buffer[0] = '\0'; // null terminate on format error, which should not happen |
| 274 | } |
| 275 | break; |
| 276 | case kPrintFormatShort: |
| 277 | if (snprintf(buffer, bufferSize, "%02d:%02d:%02d.%03d", |
| 278 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 279 | (int)(ns % one_second / 1000000)) < 0) { |
| 280 | buffer[0] = '\0'; // null terminate on format error, which should not happen |
| 281 | } |
| 282 | break; |
| 283 | } |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 284 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 285 | |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame] | 286 | std::string mediametrics::Item::toString() const { |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 287 | std::string result; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 288 | char buffer[kMaxPropertyStringSize]; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 289 | |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame] | 290 | snprintf(buffer, sizeof(buffer), "{%s, (%s), (%s, %d, %d)", |
| 291 | mKey.c_str(), |
| 292 | timeStringFromNs(mTimestamp, kPrintFormatLong).time, |
| 293 | mPkgName.c_str(), mPid, mUid |
| 294 | ); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 295 | result.append(buffer); |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame] | 296 | bool first = true; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 297 | for (auto &prop : *this) { |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 298 | prop.toStringBuffer(buffer, sizeof(buffer)); |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame] | 299 | result += first ? ", (" : ", "; |
| 300 | result += buffer; |
| 301 | first = false; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 302 | } |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame] | 303 | result.append(")}"); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 304 | return result; |
| 305 | } |
| 306 | |
| 307 | // for the lazy, we offer methods that finds the service and |
| 308 | // calls the appropriate daemon |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 309 | bool mediametrics::Item::selfrecord() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 310 | ALOGD_IF(DEBUG_API, "%s: delivering %s", __func__, this->toString().c_str()); |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 311 | |
| 312 | char *str; |
| 313 | size_t size; |
| 314 | status_t status = writeToByteString(&str, &size); |
| 315 | if (status == NO_ERROR) { |
| 316 | status = submitBuffer(str, size); |
George Burgess IV | 14dadb1 | 2021-02-03 14:04:09 -0800 | [diff] [blame] | 317 | free(str); |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 318 | } |
| 319 | if (status != NO_ERROR) { |
| 320 | ALOGW("%s: failed to record: %s", __func__, this->toString().c_str()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 321 | return false; |
| 322 | } |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 323 | return true; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 326 | //static |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 327 | bool BaseItem::isEnabled() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 328 | // completely skip logging from certain UIDs. We do this here |
| 329 | // to avoid the multi-second timeouts while we learn that |
| 330 | // sepolicy will not let us find the service. |
| 331 | // We do this only for a select set of UIDs |
| 332 | // The sepolicy protection is still in place, we just want a faster |
| 333 | // response from this specific, small set of uids. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 334 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 335 | // This is checked only once in the lifetime of the process. |
| 336 | const uid_t uid = getuid(); |
| 337 | switch (uid) { |
| 338 | case AID_RADIO: // telephony subsystem, RIL |
| 339 | return false; |
Ahaan Ugale | b5aee61 | 2021-05-26 14:50:13 -0700 | [diff] [blame] | 340 | default: |
| 341 | // Some isolated processes can access the audio system; see |
| 342 | // AudioSystem::setAudioFlingerBinder (currently only the HotwordDetectionService). Instead |
| 343 | // of also allowing access to the MediaMetrics service, it's simpler to just disable it for |
| 344 | // now. |
| 345 | // TODO(b/190151205): Either allow the HotwordDetectionService to access MediaMetrics or |
| 346 | // make this disabling specific to that process. |
Mark Punzalan | 3ebe121 | 2022-05-17 08:15:49 +0000 | [diff] [blame] | 347 | uid_t appid = multiuser_get_app_id(uid); |
| 348 | if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) { |
Ahaan Ugale | b5aee61 | 2021-05-26 14:50:13 -0700 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | break; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 354 | int enabled = property_get_int32(Item::EnabledProperty, -1); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 355 | if (enabled == -1) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 356 | enabled = property_get_int32(Item::EnabledPropertyPersist, -1); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 357 | } |
| 358 | if (enabled == -1) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 359 | enabled = Item::EnabledProperty_default; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 360 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 361 | return enabled > 0; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 364 | // monitor health of our connection to the metrics service |
| 365 | class MediaMetricsDeathNotifier : public IBinder::DeathRecipient { |
| 366 | virtual void binderDied(const wp<IBinder> &) { |
| 367 | ALOGW("Reacquire service connection on next request"); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 368 | BaseItem::dropInstance(); |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 369 | } |
| 370 | }; |
| 371 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 372 | static sp<MediaMetricsDeathNotifier> sNotifier; |
| 373 | // static |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 374 | sp<media::IMediaMetricsService> BaseItem::sMediaMetricsService; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 375 | static std::mutex sServiceMutex; |
| 376 | static int sRemainingBindAttempts = SVC_TRIES; |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 377 | |
| 378 | // static |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 379 | void BaseItem::dropInstance() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 380 | std::lock_guard _l(sServiceMutex); |
| 381 | sRemainingBindAttempts = SVC_TRIES; |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 382 | sMediaMetricsService = nullptr; |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 385 | // static |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 386 | status_t BaseItem::submitBuffer(const char *buffer, size_t size) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 387 | ALOGD_IF(DEBUG_API, "%s: delivering %zu bytes", __func__, size); |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 388 | |
| 389 | // Validate size |
| 390 | if (size > std::numeric_limits<int32_t>::max()) return BAD_VALUE; |
| 391 | |
| 392 | // Do we have the service available? |
| 393 | sp<media::IMediaMetricsService> svc = getService(); |
| 394 | if (svc == nullptr) return NO_INIT; |
| 395 | |
| 396 | ::android::status_t status = NO_ERROR; |
| 397 | if constexpr (/* DISABLES CODE */ (false)) { |
| 398 | // THIS PATH IS FOR REFERENCE ONLY. |
| 399 | // It is compiled so that any changes to IMediaMetricsService::submitBuffer() |
| 400 | // will lead here. If this code is changed, the else branch must |
| 401 | // be changed as well. |
| 402 | // |
| 403 | // Use the AIDL calling interface - this is a bit slower as a byte vector must be |
| 404 | // constructed. As the call is one-way, the only a transaction error occurs. |
| 405 | status = svc->submitBuffer({buffer, buffer + size}).transactionError(); |
| 406 | } else { |
| 407 | // Use the Binder calling interface - this direct implementation avoids |
| 408 | // malloc/copy/free for the vector and reduces the overhead for logging. |
| 409 | // We based this off of the AIDL generated file: |
| 410 | // out/soong/.intermediates/frameworks/av/media/libmediametrics/mediametricsservice-aidl-unstable-cpp-source/gen/android/media/IMediaMetricsService.cpp |
| 411 | // TODO: Create an AIDL C++ back end optimized form of vector writing. |
| 412 | ::android::Parcel _aidl_data; |
| 413 | ::android::Parcel _aidl_reply; // we don't care about this as it is one-way. |
| 414 | |
| 415 | status = _aidl_data.writeInterfaceToken(svc->getInterfaceDescriptor()); |
| 416 | if (status != ::android::OK) goto _aidl_error; |
| 417 | |
| 418 | status = _aidl_data.writeInt32(static_cast<int32_t>(size)); |
| 419 | if (status != ::android::OK) goto _aidl_error; |
| 420 | |
| 421 | status = _aidl_data.write(buffer, static_cast<int32_t>(size)); |
| 422 | if (status != ::android::OK) goto _aidl_error; |
| 423 | |
| 424 | status = ::android::IInterface::asBinder(svc)->transact( |
| 425 | ::android::media::BnMediaMetricsService::TRANSACTION_submitBuffer, |
| 426 | _aidl_data, &_aidl_reply, ::android::IBinder::FLAG_ONEWAY); |
| 427 | |
| 428 | // AIDL permits setting a default implementation for additional functionality. |
| 429 | // See go/aog/713984. This is not used here. |
| 430 | // if (status == ::android::UNKNOWN_TRANSACTION |
| 431 | // && ::android::media::IMediaMetricsService::getDefaultImpl()) { |
| 432 | // status = ::android::media::IMediaMetricsService::getDefaultImpl() |
| 433 | // ->submitBuffer(immutableByteVectorFromBuffer(buffer, size)) |
| 434 | // .transactionError(); |
| 435 | // } |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 436 | } |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 437 | |
| 438 | if (status == NO_ERROR) return NO_ERROR; |
| 439 | |
| 440 | _aidl_error: |
| 441 | ALOGW("%s: failed(%d) to record: %zu bytes", __func__, status, size); |
| 442 | return status; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 445 | //static |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 446 | sp<media::IMediaMetricsService> BaseItem::getService() { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 447 | static const char *servicename = "media.metrics"; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 448 | static const bool enabled = isEnabled(); // singleton initialized |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 449 | |
| 450 | if (enabled == false) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 451 | ALOGD_IF(DEBUG_SERVICEACCESS, "disabled"); |
| 452 | return nullptr; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 453 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 454 | std::lock_guard _l(sServiceMutex); |
| 455 | // think of remainingBindAttempts as telling us whether service == nullptr because |
| 456 | // (1) we haven't tried to initialize it yet |
| 457 | // (2) we've tried to initialize it, but failed. |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 458 | if (sMediaMetricsService == nullptr && sRemainingBindAttempts > 0) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 459 | const char *badness = ""; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 460 | sp<IServiceManager> sm = defaultServiceManager(); |
| 461 | if (sm != nullptr) { |
| 462 | sp<IBinder> binder = sm->getService(String16(servicename)); |
| 463 | if (binder != nullptr) { |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 464 | sMediaMetricsService = interface_cast<media::IMediaMetricsService>(binder); |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 465 | sNotifier = new MediaMetricsDeathNotifier(); |
| 466 | binder->linkToDeath(sNotifier); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 467 | } else { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 468 | badness = "did not find service"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 469 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 470 | } else { |
| 471 | badness = "No Service Manager access"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 472 | } |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 473 | if (sMediaMetricsService == nullptr) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 474 | if (sRemainingBindAttempts > 0) { |
| 475 | sRemainingBindAttempts--; |
| 476 | } |
| 477 | ALOGD_IF(DEBUG_SERVICEACCESS, "%s: unable to bind to service %s: %s", |
| 478 | __func__, servicename, badness); |
| 479 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 480 | } |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 481 | return sMediaMetricsService; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 484 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 485 | status_t mediametrics::Item::writeToByteString(char **pbuffer, size_t *plength) const |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 486 | { |
| 487 | if (pbuffer == nullptr || plength == nullptr) |
| 488 | return BAD_VALUE; |
| 489 | |
| 490 | // get size |
| 491 | const size_t keySizeZeroTerminated = strlen(mKey.c_str()) + 1; |
| 492 | if (keySizeZeroTerminated > UINT16_MAX) { |
| 493 | ALOGW("%s: key size %zu too large", __func__, keySizeZeroTerminated); |
| 494 | return INVALID_OPERATION; |
| 495 | } |
| 496 | const uint16_t version = 0; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 497 | const uint32_t header_size = |
| 498 | sizeof(uint32_t) // total size |
| 499 | + sizeof(header_size) // header size |
| 500 | + sizeof(version) // encoding version |
| 501 | + sizeof(uint16_t) // key size |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 502 | + keySizeZeroTerminated // key, zero terminated |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 503 | + sizeof(int32_t) // pid |
| 504 | + sizeof(int32_t) // uid |
| 505 | + sizeof(int64_t) // timestamp |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 506 | ; |
| 507 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 508 | uint32_t size = header_size |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 509 | + sizeof(uint32_t) // # properties |
| 510 | ; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 511 | for (auto &prop : *this) { |
| 512 | const size_t propSize = prop.getByteStringSize(); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 513 | if (propSize > UINT16_MAX) { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 514 | ALOGW("%s: prop %s size %zu too large", __func__, prop.getName(), propSize); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 515 | return INVALID_OPERATION; |
| 516 | } |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 517 | if (__builtin_add_overflow(size, propSize, &size)) { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 518 | ALOGW("%s: item size overflow at property %s", __func__, prop.getName()); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 519 | return INVALID_OPERATION; |
| 520 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 523 | // since we fill every byte in the buffer (there is no padding), |
| 524 | // malloc is used here instead of calloc. |
| 525 | char * const build = (char *)malloc(size); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 526 | if (build == nullptr) return NO_MEMORY; |
| 527 | |
| 528 | char *filling = build; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 529 | char *buildmax = build + size; |
| 530 | if (insert((uint32_t)size, &filling, buildmax) != NO_ERROR |
| 531 | || insert(header_size, &filling, buildmax) != NO_ERROR |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 532 | || insert(version, &filling, buildmax) != NO_ERROR |
| 533 | || insert((uint16_t)keySizeZeroTerminated, &filling, buildmax) != NO_ERROR |
| 534 | || insert(mKey.c_str(), &filling, buildmax) != NO_ERROR |
| 535 | || insert((int32_t)mPid, &filling, buildmax) != NO_ERROR |
| 536 | || insert((int32_t)mUid, &filling, buildmax) != NO_ERROR |
| 537 | || insert((int64_t)mTimestamp, &filling, buildmax) != NO_ERROR |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 538 | || insert((uint32_t)mProps.size(), &filling, buildmax) != NO_ERROR) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 539 | ALOGE("%s:could not write header", __func__); // shouldn't happen |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 540 | free(build); |
| 541 | return INVALID_OPERATION; |
| 542 | } |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 543 | for (auto &prop : *this) { |
| 544 | if (prop.writeToByteString(&filling, buildmax) != NO_ERROR) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 545 | free(build); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 546 | // shouldn't happen |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 547 | ALOGE("%s:could not write prop %s", __func__, prop.getName()); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 548 | return INVALID_OPERATION; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (filling != buildmax) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 553 | ALOGE("%s: problems populating; wrote=%d planned=%d", |
| 554 | __func__, (int)(filling - build), (int)size); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 555 | free(build); |
| 556 | return INVALID_OPERATION; |
| 557 | } |
| 558 | *pbuffer = build; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 559 | *plength = size; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 560 | return NO_ERROR; |
| 561 | } |
| 562 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 563 | status_t mediametrics::Item::readFromByteString(const char *bufferptr, size_t length) |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 564 | { |
| 565 | if (bufferptr == nullptr) return BAD_VALUE; |
| 566 | |
| 567 | const char *read = bufferptr; |
| 568 | const char *readend = bufferptr + length; |
| 569 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 570 | uint32_t size; |
| 571 | uint32_t header_size; |
| 572 | uint16_t version; |
| 573 | uint16_t key_size; |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 574 | std::string key; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 575 | int32_t pid; |
| 576 | int32_t uid; |
| 577 | int64_t timestamp; |
| 578 | uint32_t propCount; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 579 | if (extract(&size, &read, readend) != NO_ERROR |
| 580 | || extract(&header_size, &read, readend) != NO_ERROR |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 581 | || extract(&version, &read, readend) != NO_ERROR |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 582 | || extract(&key_size, &read, readend) != NO_ERROR |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 583 | || extract(&key, &read, readend) != NO_ERROR |
| 584 | || extract(&pid, &read, readend) != NO_ERROR |
| 585 | || extract(&uid, &read, readend) != NO_ERROR |
| 586 | || extract(×tamp, &read, readend) != NO_ERROR |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 587 | || size > length |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 588 | || key.size() + 1 != key_size |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 589 | || header_size > size) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 590 | ALOGW("%s: invalid header", __func__); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 591 | return INVALID_OPERATION; |
| 592 | } |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 593 | mKey = std::move(key); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 594 | const size_t pos = read - bufferptr; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 595 | if (pos > header_size) { |
| 596 | ALOGW("%s: invalid header pos:%zu > header_size:%u", |
| 597 | __func__, pos, header_size); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 598 | return INVALID_OPERATION; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 599 | } else if (pos < header_size) { |
| 600 | ALOGW("%s: mismatched header pos:%zu < header_size:%u, advancing", |
| 601 | __func__, pos, header_size); |
| 602 | read += (header_size - pos); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 603 | } |
| 604 | if (extract(&propCount, &read, readend) != NO_ERROR) { |
| 605 | ALOGD("%s: cannot read prop count", __func__); |
| 606 | return INVALID_OPERATION; |
| 607 | } |
| 608 | mPid = pid; |
| 609 | mUid = uid; |
| 610 | mTimestamp = timestamp; |
| 611 | for (size_t i = 0; i < propCount; ++i) { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 612 | Prop prop; |
| 613 | if (prop.readFromByteString(&read, readend) != NO_ERROR) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 614 | ALOGW("%s: cannot read prop %zu", __func__, i); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 615 | return INVALID_OPERATION; |
| 616 | } |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 617 | mProps[prop.getName()] = std::move(prop); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 618 | } |
| 619 | return NO_ERROR; |
| 620 | } |
| 621 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 622 | status_t mediametrics::Item::Prop::readFromParcel(const Parcel& data) |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 623 | { |
| 624 | const char *key = data.readCString(); |
| 625 | if (key == nullptr) return BAD_VALUE; |
| 626 | int32_t type; |
| 627 | status_t status = data.readInt32(&type); |
| 628 | if (status != NO_ERROR) return status; |
| 629 | switch (type) { |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 630 | case mediametrics::kTypeInt32: { |
| 631 | int32_t value; |
| 632 | status = data.readInt32(&value); |
| 633 | if (status != NO_ERROR) return status; |
| 634 | mElem = value; |
| 635 | } break; |
| 636 | case mediametrics::kTypeInt64: { |
| 637 | int64_t value; |
| 638 | status = data.readInt64(&value); |
| 639 | if (status != NO_ERROR) return status; |
| 640 | mElem = value; |
| 641 | } break; |
| 642 | case mediametrics::kTypeDouble: { |
| 643 | double value; |
| 644 | status = data.readDouble(&value); |
| 645 | if (status != NO_ERROR) return status; |
| 646 | mElem = value; |
| 647 | } break; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 648 | case mediametrics::kTypeCString: { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 649 | const char *s = data.readCString(); |
| 650 | if (s == nullptr) return BAD_VALUE; |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 651 | mElem = s; |
| 652 | } break; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 653 | case mediametrics::kTypeRate: { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 654 | std::pair<int64_t, int64_t> rate; |
| 655 | status = data.readInt64(&rate.first) |
| 656 | ?: data.readInt64(&rate.second); |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 657 | if (status != NO_ERROR) return status; |
| 658 | mElem = rate; |
| 659 | } break; |
| 660 | case mediametrics::kTypeNone: { |
| 661 | mElem = std::monostate{}; |
| 662 | } break; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 663 | default: |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 664 | ALOGE("%s: reading bad item type: %d", __func__, type); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 665 | return BAD_VALUE; |
| 666 | } |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 667 | setName(key); |
| 668 | return NO_ERROR; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 671 | status_t mediametrics::Item::Prop::readFromByteString( |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 672 | const char **bufferpptr, const char *bufferptrmax) |
| 673 | { |
| 674 | uint16_t len; |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 675 | std::string name; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 676 | uint8_t type; |
| 677 | status_t status = extract(&len, bufferpptr, bufferptrmax) |
| 678 | ?: extract(&type, bufferpptr, bufferptrmax) |
| 679 | ?: extract(&name, bufferpptr, bufferptrmax); |
| 680 | if (status != NO_ERROR) return status; |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 681 | switch (type) { |
| 682 | case mediametrics::kTypeInt32: { |
| 683 | int32_t value; |
| 684 | status = extract(&value, bufferpptr, bufferptrmax); |
| 685 | if (status != NO_ERROR) return status; |
| 686 | mElem = value; |
| 687 | } break; |
| 688 | case mediametrics::kTypeInt64: { |
| 689 | int64_t value; |
| 690 | status = extract(&value, bufferpptr, bufferptrmax); |
| 691 | if (status != NO_ERROR) return status; |
| 692 | mElem = value; |
| 693 | } break; |
| 694 | case mediametrics::kTypeDouble: { |
| 695 | double value; |
| 696 | status = extract(&value, bufferpptr, bufferptrmax); |
| 697 | if (status != NO_ERROR) return status; |
| 698 | mElem = value; |
| 699 | } break; |
| 700 | case mediametrics::kTypeRate: { |
| 701 | std::pair<int64_t, int64_t> value; |
| 702 | status = extract(&value.first, bufferpptr, bufferptrmax) |
| 703 | ?: extract(&value.second, bufferpptr, bufferptrmax); |
| 704 | if (status != NO_ERROR) return status; |
| 705 | mElem = value; |
| 706 | } break; |
| 707 | case mediametrics::kTypeCString: { |
| 708 | std::string value; |
| 709 | status = extract(&value, bufferpptr, bufferptrmax); |
| 710 | if (status != NO_ERROR) return status; |
| 711 | mElem = std::move(value); |
| 712 | } break; |
| 713 | case mediametrics::kTypeNone: { |
| 714 | mElem = std::monostate{}; |
| 715 | } break; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 716 | default: |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 717 | ALOGE("%s: found bad prop type: %d, name %s", |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 718 | __func__, (int)type, mName.c_str()); // no payload sent |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 719 | return BAD_VALUE; |
| 720 | } |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 721 | mName = name; |
| 722 | return NO_ERROR; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 725 | } // namespace android::mediametrics |