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