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 | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 17 | #define LOG_TAG "MediaAnalyticsItem" |
| 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> |
| 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Log.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 30 | #include <utils/SortedVector.h> |
| 31 | #include <utils/threads.h> |
| 32 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 33 | #include <binder/IServiceManager.h> |
| 34 | #include <media/IMediaAnalyticsService.h> |
| 35 | #include <media/MediaAnalyticsItem.h> |
Ray Essick | 79a89ef | 2017-04-24 15:52:54 -0700 | [diff] [blame] | 36 | #include <private/android_filesystem_config.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | #define DEBUG_SERVICEACCESS 0 |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 41 | #define DEBUG_API 0 |
| 42 | #define DEBUG_ALLOCATIONS 0 |
| 43 | |
| 44 | // after this many failed attempts, we stop trying [from this process] and just say that |
| 45 | // the service is off. |
| 46 | #define SVC_TRIES 2 |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 47 | |
Ray Essick | bf536ac | 2019-08-26 11:04:28 -0700 | [diff] [blame] | 48 | MediaAnalyticsItem* MediaAnalyticsItem::convert(mediametrics_handle_t handle) { |
| 49 | MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle; |
| 50 | return item; |
| 51 | } |
| 52 | |
| 53 | mediametrics_handle_t MediaAnalyticsItem::convert(MediaAnalyticsItem *item ) { |
| 54 | mediametrics_handle_t handle = (mediametrics_handle_t) item; |
| 55 | return handle; |
| 56 | } |
| 57 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 58 | MediaAnalyticsItem::~MediaAnalyticsItem() { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 59 | if (DEBUG_ALLOCATIONS) { |
| 60 | ALOGD("Destroy MediaAnalyticsItem @ %p", this); |
| 61 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 62 | clear(); |
| 63 | } |
| 64 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 65 | void MediaAnalyticsItem::clear() { |
| 66 | |
| 67 | // clean allocated storage from key |
| 68 | mKey.clear(); |
| 69 | |
| 70 | // clean attributes |
| 71 | // contents of the attributes |
Ray Essick | 58f5873 | 2017-10-02 10:56:18 -0700 | [diff] [blame] | 72 | for (size_t i = 0 ; i < mPropCount; i++ ) { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 73 | mProps[i].clear(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 74 | } |
| 75 | // the attribute records themselves |
| 76 | if (mProps != NULL) { |
| 77 | free(mProps); |
| 78 | mProps = NULL; |
| 79 | } |
| 80 | mPropSize = 0; |
| 81 | mPropCount = 0; |
| 82 | |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | // make a deep copy of myself |
| 87 | MediaAnalyticsItem *MediaAnalyticsItem::dup() { |
| 88 | MediaAnalyticsItem *dst = new MediaAnalyticsItem(this->mKey); |
| 89 | |
| 90 | if (dst != NULL) { |
| 91 | // key as part of constructor |
| 92 | dst->mPid = this->mPid; |
| 93 | dst->mUid = this->mUid; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 94 | dst->mPkgName = this->mPkgName; |
| 95 | dst->mPkgVersionCode = this->mPkgVersionCode; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 96 | dst->mTimestamp = this->mTimestamp; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 97 | |
| 98 | // properties aka attributes |
| 99 | dst->growProps(this->mPropCount); |
| 100 | for(size_t i=0;i<mPropCount;i++) { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 101 | dst->mProps[i] = this->mProps[i]; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 102 | } |
| 103 | dst->mPropCount = this->mPropCount; |
| 104 | } |
| 105 | |
| 106 | return dst; |
| 107 | } |
| 108 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 109 | MediaAnalyticsItem &MediaAnalyticsItem::setTimestamp(nsecs_t ts) { |
| 110 | mTimestamp = ts; |
| 111 | return *this; |
| 112 | } |
| 113 | |
| 114 | nsecs_t MediaAnalyticsItem::getTimestamp() const { |
| 115 | return mTimestamp; |
| 116 | } |
| 117 | |
| 118 | MediaAnalyticsItem &MediaAnalyticsItem::setPid(pid_t pid) { |
| 119 | mPid = pid; |
| 120 | return *this; |
| 121 | } |
| 122 | |
| 123 | pid_t MediaAnalyticsItem::getPid() const { |
| 124 | return mPid; |
| 125 | } |
| 126 | |
| 127 | MediaAnalyticsItem &MediaAnalyticsItem::setUid(uid_t uid) { |
| 128 | mUid = uid; |
| 129 | return *this; |
| 130 | } |
| 131 | |
| 132 | uid_t MediaAnalyticsItem::getUid() const { |
| 133 | return mUid; |
| 134 | } |
| 135 | |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 136 | MediaAnalyticsItem &MediaAnalyticsItem::setPkgName(const std::string &pkgName) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 137 | mPkgName = pkgName; |
| 138 | return *this; |
| 139 | } |
| 140 | |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 141 | MediaAnalyticsItem &MediaAnalyticsItem::setPkgVersionCode(int64_t pkgVersionCode) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 142 | mPkgVersionCode = pkgVersionCode; |
| 143 | return *this; |
| 144 | } |
| 145 | |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 146 | int64_t MediaAnalyticsItem::getPkgVersionCode() const { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 147 | return mPkgVersionCode; |
| 148 | } |
| 149 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 150 | |
| 151 | // find the proper entry in the list |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 152 | size_t MediaAnalyticsItem::findPropIndex(const char *name) const |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 153 | { |
| 154 | size_t i = 0; |
| 155 | for (; i < mPropCount; i++) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 156 | if (mProps[i].isNamed(name)) break; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 157 | } |
| 158 | return i; |
| 159 | } |
| 160 | |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 161 | MediaAnalyticsItem::Prop *MediaAnalyticsItem::findProp(const char *name) const { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 162 | const size_t i = findPropIndex(name); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 163 | if (i < mPropCount) { |
| 164 | return &mProps[i]; |
| 165 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 166 | return nullptr; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 169 | // consider this "find-or-allocate". |
| 170 | // caller validates type and uses clearPropValue() accordingly |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 171 | MediaAnalyticsItem::Prop *MediaAnalyticsItem::allocateProp(const char *name) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 172 | const size_t i = findPropIndex(name); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 173 | if (i < mPropCount) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 174 | return &mProps[i]; // already have it, return |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 177 | Prop *prop = allocateProp(); // get a new prop |
| 178 | if (prop == nullptr) return nullptr; |
| 179 | prop->setName(name); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 180 | return prop; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 183 | MediaAnalyticsItem::Prop *MediaAnalyticsItem::allocateProp() { |
| 184 | if (mPropCount == mPropSize && growProps() == false) { |
| 185 | ALOGE("%s: failed allocation for new properties", __func__); |
| 186 | return nullptr; |
| 187 | } |
| 188 | return &mProps[mPropCount++]; |
| 189 | } |
| 190 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 191 | // used within the summarizers; return whether property existed |
| 192 | bool MediaAnalyticsItem::removeProp(const char *name) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 193 | const size_t i = findPropIndex(name); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 194 | if (i < mPropCount) { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 195 | mProps[i].clear(); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 196 | if (i != mPropCount-1) { |
| 197 | // in the middle, bring last one down to fill gap |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 198 | mProps[i].swap(mProps[mPropCount-1]); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 199 | } |
| 200 | mPropCount--; |
| 201 | return true; |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 206 | // remove indicated keys and their values |
| 207 | // return value is # keys removed |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 208 | size_t MediaAnalyticsItem::filter(size_t n, const char *attrs[]) { |
| 209 | size_t zapped = 0; |
| 210 | for (size_t i = 0; i < n; ++i) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 211 | const char *name = attrs[i]; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 212 | size_t j = findPropIndex(name); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 213 | if (j >= mPropCount) { |
| 214 | // not there |
| 215 | continue; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 216 | } else if (j + 1 == mPropCount) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 217 | // last one, shorten |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 218 | zapped++; |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 219 | mProps[j].clear(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 220 | mPropCount--; |
| 221 | } else { |
| 222 | // in the middle, bring last one down and shorten |
| 223 | zapped++; |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 224 | mProps[j].clear(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 225 | mProps[j] = mProps[mPropCount-1]; |
| 226 | mPropCount--; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | return zapped; |
| 230 | } |
| 231 | |
| 232 | // remove any keys NOT in the provided list |
| 233 | // return value is # keys removed |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 234 | size_t MediaAnalyticsItem::filterNot(size_t n, const char *attrs[]) { |
| 235 | std::set<std::string> check(attrs, attrs + n); |
| 236 | size_t zapped = 0; |
| 237 | for (size_t j = 0; j < mPropCount;) { |
| 238 | if (check.find(mProps[j].getName()) != check.end()) { |
| 239 | ++j; |
| 240 | continue; |
| 241 | } |
| 242 | if (j + 1 == mPropCount) { |
| 243 | // last one, shorten |
| 244 | zapped++; |
| 245 | mProps[j].clear(); |
| 246 | mPropCount--; |
| 247 | break; |
| 248 | } else { |
| 249 | // in the middle, bring last one down and shorten |
| 250 | zapped++; |
| 251 | mProps[j].clear(); |
| 252 | mProps[j] = mProps[mPropCount-1]; |
| 253 | mPropCount--; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | return zapped; |
| 257 | } |
| 258 | |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 259 | bool MediaAnalyticsItem::growProps(int increment) |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 260 | { |
| 261 | if (increment <= 0) { |
| 262 | increment = kGrowProps; |
| 263 | } |
| 264 | int nsize = mPropSize + increment; |
| 265 | Prop *ni = (Prop *)realloc(mProps, sizeof(Prop) * nsize); |
| 266 | |
| 267 | if (ni != NULL) { |
| 268 | for (int i = mPropSize; i < nsize; i++) { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 269 | new (&ni[i]) Prop(); // placement new |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 270 | } |
| 271 | mProps = ni; |
| 272 | mPropSize = nsize; |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 273 | return true; |
| 274 | } else { |
| 275 | ALOGW("MediaAnalyticsItem::growProps fails"); |
| 276 | return false; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 277 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Parcel / serialize things for binder calls |
| 281 | // |
| 282 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 283 | status_t MediaAnalyticsItem::readFromParcel(const Parcel& data) { |
| 284 | int32_t version; |
| 285 | status_t status = data.readInt32(&version); |
| 286 | if (status != NO_ERROR) return status; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 287 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 288 | switch (version) { |
| 289 | case 0: |
| 290 | return readFromParcel0(data); |
| 291 | default: |
| 292 | ALOGE("%s: unsupported parcel version: %d", __func__, version); |
| 293 | return INVALID_OPERATION; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 297 | status_t MediaAnalyticsItem::readFromParcel0(const Parcel& data) { |
| 298 | const char *s = data.readCString(); |
| 299 | mKey = s == nullptr ? "" : s; |
| 300 | int32_t pid, uid; |
| 301 | status_t status = data.readInt32(&pid) ?: data.readInt32(&uid); |
| 302 | if (status != NO_ERROR) return status; |
| 303 | mPid = (pid_t)pid; |
| 304 | mUid = (uid_t)uid; |
| 305 | s = data.readCString(); |
| 306 | mPkgName = s == nullptr ? "" : s; |
| 307 | int32_t count; |
| 308 | int64_t version, timestamp; |
| 309 | status = data.readInt64(&version) ?: data.readInt64(×tamp) ?: data.readInt32(&count); |
| 310 | if (status != NO_ERROR) return status; |
| 311 | if (count < 0) return BAD_VALUE; |
| 312 | mPkgVersionCode = version; |
| 313 | mTimestamp = timestamp; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 314 | for (int i = 0; i < count ; i++) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 315 | Prop *prop = allocateProp(); |
| 316 | status_t status = prop->readFromParcel(data); |
| 317 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 318 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 319 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 322 | status_t MediaAnalyticsItem::writeToParcel(Parcel *data) const { |
| 323 | if (data == nullptr) return BAD_VALUE; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 324 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 325 | const int32_t version = 0; |
| 326 | status_t status = data->writeInt32(version); |
| 327 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 328 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 329 | switch (version) { |
| 330 | case 0: |
| 331 | return writeToParcel0(data); |
| 332 | default: |
| 333 | ALOGE("%s: unsupported parcel version: %d", __func__, version); |
| 334 | return INVALID_OPERATION; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 338 | status_t MediaAnalyticsItem::writeToParcel0(Parcel *data) const { |
| 339 | status_t status = |
| 340 | data->writeCString(mKey.c_str()) |
| 341 | ?: data->writeInt32(mPid) |
| 342 | ?: data->writeInt32(mUid) |
| 343 | ?: data->writeCString(mPkgName.c_str()) |
| 344 | ?: data->writeInt64(mPkgVersionCode) |
| 345 | ?: data->writeInt64(mTimestamp); |
| 346 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 347 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 348 | data->writeInt32((int32_t)mPropCount); |
| 349 | for (size_t i = 0 ; i < mPropCount; ++i) { |
| 350 | status = mProps[i].writeToParcel(data); |
| 351 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 352 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 353 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 356 | const char *MediaAnalyticsItem::toCString() { |
| 357 | return toCString(PROTO_LAST); |
| 358 | } |
| 359 | |
| 360 | const char * MediaAnalyticsItem::toCString(int version) { |
| 361 | std::string val = toString(version); |
| 362 | return strdup(val.c_str()); |
| 363 | } |
| 364 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 365 | std::string MediaAnalyticsItem::toString() const { |
Ray Essick | 5b77bd2 | 2018-01-23 16:11:06 -0800 | [diff] [blame] | 366 | return toString(PROTO_LAST); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 367 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 368 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 369 | std::string MediaAnalyticsItem::toString(int version) const { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 370 | |
| 371 | // v0 : released with 'o' |
| 372 | // v1 : bug fix (missing pid/finalized separator), |
| 373 | // adds apk name, apk version code |
| 374 | |
| 375 | if (version <= PROTO_FIRST) { |
| 376 | // default to original v0 format, until proper parsers are in place |
| 377 | version = PROTO_V0; |
| 378 | } else if (version > PROTO_LAST) { |
| 379 | version = PROTO_LAST; |
| 380 | } |
| 381 | |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 382 | std::string result; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 383 | char buffer[512]; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 384 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 385 | if (version == PROTO_V0) { |
| 386 | result = "("; |
| 387 | } else { |
| 388 | snprintf(buffer, sizeof(buffer), "[%d:", version); |
| 389 | result.append(buffer); |
| 390 | } |
| 391 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 392 | // same order as we spill into the parcel, although not required |
| 393 | // key+session are our primary matching criteria |
| 394 | result.append(mKey.c_str()); |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 395 | result.append(":0:"); // sessionID |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 396 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 397 | snprintf(buffer, sizeof(buffer), "%d:", mUid); |
| 398 | result.append(buffer); |
| 399 | |
| 400 | if (version >= PROTO_V1) { |
| 401 | result.append(mPkgName); |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 402 | snprintf(buffer, sizeof(buffer), ":%" PRId64 ":", mPkgVersionCode); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 403 | result.append(buffer); |
| 404 | } |
| 405 | |
| 406 | // in 'o' (v1) , the separator between pid and finalized was omitted |
| 407 | if (version <= PROTO_V0) { |
| 408 | snprintf(buffer, sizeof(buffer), "%d", mPid); |
| 409 | } else { |
| 410 | snprintf(buffer, sizeof(buffer), "%d:", mPid); |
| 411 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 412 | result.append(buffer); |
| 413 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 414 | snprintf(buffer, sizeof(buffer), "%d:", 0 /* finalized */); // TODO: remove this. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 415 | result.append(buffer); |
| 416 | snprintf(buffer, sizeof(buffer), "%" PRId64 ":", mTimestamp); |
| 417 | result.append(buffer); |
| 418 | |
| 419 | // set of items |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 420 | int count = mPropCount; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 421 | snprintf(buffer, sizeof(buffer), "%d:", count); |
| 422 | result.append(buffer); |
| 423 | for (int i = 0 ; i < count; i++ ) { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 424 | mProps[i].toString(buffer, sizeof(buffer)); |
| 425 | result.append(buffer); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 428 | if (version == PROTO_V0) { |
| 429 | result.append(")"); |
| 430 | } else { |
| 431 | result.append("]"); |
| 432 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 433 | |
| 434 | return result; |
| 435 | } |
| 436 | |
| 437 | // for the lazy, we offer methods that finds the service and |
| 438 | // calls the appropriate daemon |
| 439 | bool MediaAnalyticsItem::selfrecord() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 440 | ALOGD_IF(DEBUG_API, "%s: delivering %s", __func__, this->toString().c_str()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 441 | sp<IMediaAnalyticsService> svc = getInstance(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 442 | if (svc != NULL) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 443 | status_t status = svc->submit(this); |
| 444 | if (status != NO_ERROR) { |
| 445 | ALOGW("%s: failed to record: %s", __func__, this->toString().c_str()); |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 446 | return false; |
| 447 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 448 | return true; |
| 449 | } else { |
| 450 | return false; |
| 451 | } |
| 452 | } |
| 453 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 454 | //static |
| 455 | bool MediaAnalyticsItem::isEnabled() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 456 | // completely skip logging from certain UIDs. We do this here |
| 457 | // to avoid the multi-second timeouts while we learn that |
| 458 | // sepolicy will not let us find the service. |
| 459 | // We do this only for a select set of UIDs |
| 460 | // The sepolicy protection is still in place, we just want a faster |
| 461 | // response from this specific, small set of uids. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 462 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 463 | // This is checked only once in the lifetime of the process. |
| 464 | const uid_t uid = getuid(); |
| 465 | switch (uid) { |
| 466 | case AID_RADIO: // telephony subsystem, RIL |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | int enabled = property_get_int32(MediaAnalyticsItem::EnabledProperty, -1); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 471 | if (enabled == -1) { |
| 472 | enabled = property_get_int32(MediaAnalyticsItem::EnabledPropertyPersist, -1); |
| 473 | } |
| 474 | if (enabled == -1) { |
| 475 | enabled = MediaAnalyticsItem::EnabledProperty_default; |
| 476 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 477 | return enabled > 0; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 480 | // monitor health of our connection to the metrics service |
| 481 | class MediaMetricsDeathNotifier : public IBinder::DeathRecipient { |
| 482 | virtual void binderDied(const wp<IBinder> &) { |
| 483 | ALOGW("Reacquire service connection on next request"); |
| 484 | MediaAnalyticsItem::dropInstance(); |
| 485 | } |
| 486 | }; |
| 487 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 488 | static sp<MediaMetricsDeathNotifier> sNotifier; |
| 489 | // static |
| 490 | sp<IMediaAnalyticsService> MediaAnalyticsItem::sAnalyticsService; |
| 491 | static std::mutex sServiceMutex; |
| 492 | static int sRemainingBindAttempts = SVC_TRIES; |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 493 | |
| 494 | // static |
| 495 | void MediaAnalyticsItem::dropInstance() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 496 | std::lock_guard _l(sServiceMutex); |
| 497 | sRemainingBindAttempts = SVC_TRIES; |
| 498 | sAnalyticsService = nullptr; |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 501 | //static |
| 502 | sp<IMediaAnalyticsService> MediaAnalyticsItem::getInstance() { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 503 | static const char *servicename = "media.metrics"; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 504 | static const bool enabled = isEnabled(); // singleton initialized |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 505 | |
| 506 | if (enabled == false) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 507 | ALOGD_IF(DEBUG_SERVICEACCESS, "disabled"); |
| 508 | return nullptr; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 509 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 510 | std::lock_guard _l(sServiceMutex); |
| 511 | // think of remainingBindAttempts as telling us whether service == nullptr because |
| 512 | // (1) we haven't tried to initialize it yet |
| 513 | // (2) we've tried to initialize it, but failed. |
| 514 | if (sAnalyticsService == nullptr && sRemainingBindAttempts > 0) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 515 | const char *badness = ""; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 516 | sp<IServiceManager> sm = defaultServiceManager(); |
| 517 | if (sm != nullptr) { |
| 518 | sp<IBinder> binder = sm->getService(String16(servicename)); |
| 519 | if (binder != nullptr) { |
| 520 | sAnalyticsService = interface_cast<IMediaAnalyticsService>(binder); |
| 521 | sNotifier = new MediaMetricsDeathNotifier(); |
| 522 | binder->linkToDeath(sNotifier); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 523 | } else { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 524 | badness = "did not find service"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 525 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 526 | } else { |
| 527 | badness = "No Service Manager access"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 528 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 529 | if (sAnalyticsService == nullptr) { |
| 530 | if (sRemainingBindAttempts > 0) { |
| 531 | sRemainingBindAttempts--; |
| 532 | } |
| 533 | ALOGD_IF(DEBUG_SERVICEACCESS, "%s: unable to bind to service %s: %s", |
| 534 | __func__, servicename, badness); |
| 535 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 536 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 537 | return sAnalyticsService; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 540 | // merge the info from 'incoming' into this record. |
| 541 | // we finish with a union of this+incoming and special handling for collisions |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 542 | bool MediaAnalyticsItem::merge(MediaAnalyticsItem *incoming) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 543 | |
| 544 | // if I don't have key or session id, take them from incoming |
| 545 | // 'this' should never be missing both of them... |
| 546 | if (mKey.empty()) { |
| 547 | mKey = incoming->mKey; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 550 | // for each attribute from 'incoming', resolve appropriately |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 551 | int nattr = incoming->mPropCount; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 552 | for (int i = 0 ; i < nattr; i++ ) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 553 | Prop *iprop = &incoming->mProps[i]; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 554 | const char *p = iprop->mName; |
| 555 | size_t len = strlen(p); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 556 | |
| 557 | // should ignore a zero length name... |
| 558 | if (len == 0) { |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | Prop *oprop = findProp(iprop->mName); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 563 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 564 | if (oprop == NULL) { |
| 565 | // no oprop, so we insert the new one |
| 566 | oprop = allocateProp(p); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 567 | if (oprop != NULL) { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 568 | *oprop = *iprop; |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 569 | } else { |
| 570 | ALOGW("dropped property '%s'", iprop->mName); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 571 | } |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 572 | } else { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 573 | *oprop = *iprop; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | // not sure when we'd return false... |
| 578 | return true; |
| 579 | } |
| 580 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 581 | namespace { |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 582 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 583 | template <typename T> |
| 584 | status_t insert(const T& val, char **bufferpptr, char *bufferptrmax) |
| 585 | { |
| 586 | const size_t size = sizeof(val); |
| 587 | if (*bufferpptr + size > bufferptrmax) { |
| 588 | ALOGE("%s: buffer exceeded with size %zu", __func__, size); |
| 589 | return BAD_VALUE; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 590 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 591 | memcpy(*bufferpptr, &val, size); |
| 592 | *bufferpptr += size; |
| 593 | return NO_ERROR; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 596 | template <> |
| 597 | status_t insert(const char * const& val, char **bufferpptr, char *bufferptrmax) |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 598 | { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 599 | const size_t size = strlen(val) + 1; |
| 600 | if (size > UINT16_MAX || *bufferpptr + size > bufferptrmax) { |
| 601 | ALOGE("%s: buffer exceeded with size %zu", __func__, size); |
| 602 | return BAD_VALUE; |
| 603 | } |
| 604 | memcpy(*bufferpptr, val, size); |
| 605 | *bufferpptr += size; |
| 606 | return NO_ERROR; |
| 607 | } |
| 608 | |
| 609 | template <> |
| 610 | __unused |
| 611 | status_t insert(char * const& val, char **bufferpptr, char *bufferptrmax) |
| 612 | { |
| 613 | return insert((const char *)val, bufferpptr, bufferptrmax); |
| 614 | } |
| 615 | |
| 616 | template <typename T> |
| 617 | status_t extract(T *val, const char **bufferpptr, const char *bufferptrmax) |
| 618 | { |
| 619 | const size_t size = sizeof(*val); |
| 620 | if (*bufferpptr + size > bufferptrmax) { |
| 621 | ALOGE("%s: buffer exceeded with size %zu", __func__, size); |
| 622 | return BAD_VALUE; |
| 623 | } |
| 624 | memcpy(val, *bufferpptr, size); |
| 625 | *bufferpptr += size; |
| 626 | return NO_ERROR; |
| 627 | } |
| 628 | |
| 629 | template <> |
| 630 | status_t extract(char **val, const char **bufferpptr, const char *bufferptrmax) |
| 631 | { |
| 632 | const char *ptr = *bufferpptr; |
| 633 | while (*ptr != 0) { |
| 634 | if (ptr >= bufferptrmax) { |
| 635 | ALOGE("%s: buffer exceeded", __func__); |
| 636 | } |
| 637 | ++ptr; |
| 638 | } |
| 639 | const size_t size = (ptr - *bufferpptr) + 1; |
| 640 | *val = (char *)malloc(size); |
| 641 | memcpy(*val, *bufferpptr, size); |
| 642 | *bufferpptr += size; |
| 643 | return NO_ERROR; |
| 644 | } |
| 645 | |
| 646 | } // namespace |
| 647 | |
| 648 | status_t MediaAnalyticsItem::writeToByteString(char **pbuffer, size_t *plength) const |
| 649 | { |
| 650 | if (pbuffer == nullptr || plength == nullptr) |
| 651 | return BAD_VALUE; |
| 652 | |
| 653 | // get size |
| 654 | const size_t keySizeZeroTerminated = strlen(mKey.c_str()) + 1; |
| 655 | if (keySizeZeroTerminated > UINT16_MAX) { |
| 656 | ALOGW("%s: key size %zu too large", __func__, keySizeZeroTerminated); |
| 657 | return INVALID_OPERATION; |
| 658 | } |
| 659 | const uint16_t version = 0; |
| 660 | const uint32_t header_len = |
| 661 | sizeof(uint32_t) // overall length |
| 662 | + sizeof(header_len) // header length |
| 663 | + sizeof(version) // encoding version |
| 664 | + sizeof(uint16_t) // key length |
| 665 | + keySizeZeroTerminated // key, zero terminated |
| 666 | + sizeof(int32_t) // pid |
| 667 | + sizeof(int32_t) // uid |
| 668 | + sizeof(int64_t) // timestamp |
| 669 | ; |
| 670 | |
| 671 | uint32_t len = header_len |
| 672 | + sizeof(uint32_t) // # properties |
| 673 | ; |
| 674 | for (size_t i = 0 ; i < mPropCount; ++i) { |
| 675 | const size_t size = mProps[i].getByteStringSize(); |
| 676 | if (size > UINT_MAX - 1) { |
| 677 | ALOGW("%s: prop %zu has size %zu", __func__, i, size); |
| 678 | return INVALID_OPERATION; |
| 679 | } |
| 680 | len += size; |
| 681 | } |
| 682 | |
| 683 | // TODO: consider package information and timestamp. |
| 684 | |
| 685 | // now that we have a size... let's allocate and fill |
| 686 | char *build = (char *)calloc(1 /* nmemb */, len); |
| 687 | if (build == nullptr) return NO_MEMORY; |
| 688 | |
| 689 | char *filling = build; |
| 690 | char *buildmax = build + len; |
| 691 | if (insert(len, &filling, buildmax) != NO_ERROR |
| 692 | || insert(header_len, &filling, buildmax) != NO_ERROR |
| 693 | || insert(version, &filling, buildmax) != NO_ERROR |
| 694 | || insert((uint16_t)keySizeZeroTerminated, &filling, buildmax) != NO_ERROR |
| 695 | || insert(mKey.c_str(), &filling, buildmax) != NO_ERROR |
| 696 | || insert((int32_t)mPid, &filling, buildmax) != NO_ERROR |
| 697 | || insert((int32_t)mUid, &filling, buildmax) != NO_ERROR |
| 698 | || insert((int64_t)mTimestamp, &filling, buildmax) != NO_ERROR |
| 699 | || insert((uint32_t)mPropCount, &filling, buildmax) != NO_ERROR) { |
| 700 | ALOGD("%s:could not write header", __func__); |
| 701 | free(build); |
| 702 | return INVALID_OPERATION; |
| 703 | } |
| 704 | for (size_t i = 0 ; i < mPropCount; ++i) { |
| 705 | if (mProps[i].writeToByteString(&filling, buildmax) != NO_ERROR) { |
| 706 | free(build); |
| 707 | ALOGD("%s:could not write prop %zu of %zu", __func__, i, mPropCount); |
| 708 | return INVALID_OPERATION; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | if (filling != buildmax) { |
| 713 | ALOGE("problems populating; wrote=%d planned=%d", |
| 714 | (int)(filling - build), len); |
| 715 | free(build); |
| 716 | return INVALID_OPERATION; |
| 717 | } |
| 718 | *pbuffer = build; |
| 719 | *plength = len; |
| 720 | return NO_ERROR; |
| 721 | } |
| 722 | |
| 723 | status_t MediaAnalyticsItem::readFromByteString(const char *bufferptr, size_t length) |
| 724 | { |
| 725 | if (bufferptr == nullptr) return BAD_VALUE; |
| 726 | |
| 727 | const char *read = bufferptr; |
| 728 | const char *readend = bufferptr + length; |
| 729 | |
| 730 | uint32_t len; |
| 731 | uint32_t header_len; |
| 732 | int16_t version; |
| 733 | int16_t key_len; |
| 734 | char *key = nullptr; |
| 735 | int32_t pid; |
| 736 | int32_t uid; |
| 737 | int64_t timestamp; |
| 738 | uint32_t propCount; |
| 739 | if (extract(&len, &read, readend) != NO_ERROR |
| 740 | || extract(&header_len, &read, readend) != NO_ERROR |
| 741 | || extract(&version, &read, readend) != NO_ERROR |
| 742 | || extract(&key_len, &read, readend) != NO_ERROR |
| 743 | || extract(&key, &read, readend) != NO_ERROR |
| 744 | || extract(&pid, &read, readend) != NO_ERROR |
| 745 | || extract(&uid, &read, readend) != NO_ERROR |
| 746 | || extract(×tamp, &read, readend) != NO_ERROR |
| 747 | || len > length |
| 748 | || header_len > len) { |
| 749 | free(key); |
| 750 | ALOGD("%s: invalid header", __func__); |
| 751 | return INVALID_OPERATION; |
| 752 | } |
| 753 | mKey = key; |
| 754 | free(key); |
| 755 | const size_t pos = read - bufferptr; |
| 756 | if (pos > header_len) { |
| 757 | ALOGD("%s: invalid header pos:%zu > header_len:%u", |
| 758 | __func__, pos, header_len); |
| 759 | return INVALID_OPERATION; |
| 760 | } else if (pos < header_len) { |
| 761 | ALOGD("%s: mismatched header pos:%zu < header_len:%u, advancing", |
| 762 | __func__, pos, header_len); |
| 763 | read += (header_len - pos); |
| 764 | } |
| 765 | if (extract(&propCount, &read, readend) != NO_ERROR) { |
| 766 | ALOGD("%s: cannot read prop count", __func__); |
| 767 | return INVALID_OPERATION; |
| 768 | } |
| 769 | mPid = pid; |
| 770 | mUid = uid; |
| 771 | mTimestamp = timestamp; |
| 772 | for (size_t i = 0; i < propCount; ++i) { |
| 773 | Prop *prop = allocateProp(); |
| 774 | if (prop->readFromByteString(&read, readend) != NO_ERROR) { |
| 775 | ALOGD("%s: cannot read prop %zu", __func__, i); |
| 776 | return INVALID_OPERATION; |
| 777 | } |
| 778 | } |
| 779 | return NO_ERROR; |
| 780 | } |
| 781 | |
| 782 | status_t MediaAnalyticsItem::Prop::writeToParcel(Parcel *data) const |
| 783 | { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 784 | switch (mType) { |
| 785 | case kTypeInt32: |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 786 | return data->writeCString(mName) |
| 787 | ?: data->writeInt32(mType) |
| 788 | ?: data->writeInt32(u.int32Value); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 789 | case kTypeInt64: |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 790 | return data->writeCString(mName) |
| 791 | ?: data->writeInt32(mType) |
| 792 | ?: data->writeInt64(u.int64Value); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 793 | case kTypeDouble: |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 794 | return data->writeCString(mName) |
| 795 | ?: data->writeInt32(mType) |
| 796 | ?: data->writeDouble(u.doubleValue); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 797 | case kTypeRate: |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 798 | return data->writeCString(mName) |
| 799 | ?: data->writeInt32(mType) |
| 800 | ?: data->writeInt64(u.rate.first) |
| 801 | ?: data->writeInt64(u.rate.second); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 802 | case kTypeCString: |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 803 | return data->writeCString(mName) |
| 804 | ?: data->writeInt32(mType) |
| 805 | ?: data->writeCString(u.CStringValue); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 806 | default: |
| 807 | ALOGE("%s: found bad type: %d, name %s", __func__, mType, mName); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 808 | return BAD_VALUE; |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 812 | status_t MediaAnalyticsItem::Prop::readFromParcel(const Parcel& data) |
| 813 | { |
| 814 | const char *key = data.readCString(); |
| 815 | if (key == nullptr) return BAD_VALUE; |
| 816 | int32_t type; |
| 817 | status_t status = data.readInt32(&type); |
| 818 | if (status != NO_ERROR) return status; |
| 819 | switch (type) { |
| 820 | case kTypeInt32: |
| 821 | status = data.readInt32(&u.int32Value); |
| 822 | break; |
| 823 | case kTypeInt64: |
| 824 | status = data.readInt64(&u.int64Value); |
| 825 | break; |
| 826 | case kTypeDouble: |
| 827 | status = data.readDouble(&u.doubleValue); |
| 828 | break; |
| 829 | case kTypeCString: { |
| 830 | const char *s = data.readCString(); |
| 831 | if (s == nullptr) return BAD_VALUE; |
| 832 | set(s); |
| 833 | break; |
| 834 | } |
| 835 | case kTypeRate: { |
| 836 | std::pair<int64_t, int64_t> rate; |
| 837 | status = data.readInt64(&rate.first) |
| 838 | ?: data.readInt64(&rate.second); |
| 839 | if (status == NO_ERROR) { |
| 840 | set(rate); |
| 841 | } |
| 842 | break; |
| 843 | } |
| 844 | default: |
| 845 | ALOGE("%s: reading bad item type: %d", __func__, mType); |
| 846 | return BAD_VALUE; |
| 847 | } |
| 848 | if (status == NO_ERROR) { |
| 849 | setName(key); |
| 850 | mType = (Type)type; |
| 851 | } |
| 852 | return status; |
| 853 | } |
| 854 | |
| 855 | void MediaAnalyticsItem::Prop::toString(char *buffer, size_t length) const |
| 856 | { |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 857 | switch (mType) { |
| 858 | case kTypeInt32: |
| 859 | snprintf(buffer, length, "%s=%d:", mName, u.int32Value); |
| 860 | break; |
| 861 | case MediaAnalyticsItem::kTypeInt64: |
| 862 | snprintf(buffer, length, "%s=%lld:", mName, (long long)u.int64Value); |
| 863 | break; |
| 864 | case MediaAnalyticsItem::kTypeDouble: |
| 865 | snprintf(buffer, length, "%s=%e:", mName, u.doubleValue); |
| 866 | break; |
| 867 | case MediaAnalyticsItem::kTypeRate: |
| 868 | snprintf(buffer, length, "%s=%lld/%lld:", |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 869 | mName, (long long)u.rate.first, (long long)u.rate.second); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 870 | break; |
| 871 | case MediaAnalyticsItem::kTypeCString: |
| 872 | // TODO sanitize string for ':' '=' |
| 873 | snprintf(buffer, length, "%s=%s:", mName, u.CStringValue); |
| 874 | break; |
| 875 | default: |
| 876 | ALOGE("%s: bad item type: %d for %s", __func__, mType, mName); |
| 877 | if (length > 0) buffer[0] = 0; |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 882 | size_t MediaAnalyticsItem::Prop::getByteStringSize() const |
| 883 | { |
| 884 | const size_t header = |
| 885 | sizeof(uint16_t) // length |
| 886 | + sizeof(uint8_t) // type |
| 887 | + strlen(mName) + 1; // mName + 0 termination |
| 888 | size_t payload = 0; |
| 889 | switch (mType) { |
| 890 | case MediaAnalyticsItem::kTypeInt32: |
| 891 | payload = sizeof(u.int32Value); |
| 892 | break; |
| 893 | case MediaAnalyticsItem::kTypeInt64: |
| 894 | payload = sizeof(u.int64Value); |
| 895 | break; |
| 896 | case MediaAnalyticsItem::kTypeDouble: |
| 897 | payload = sizeof(u.doubleValue); |
| 898 | break; |
| 899 | case MediaAnalyticsItem::kTypeRate: |
| 900 | payload = sizeof(u.rate.first) + sizeof(u.rate.second); |
| 901 | break; |
| 902 | case MediaAnalyticsItem::kTypeCString: |
| 903 | payload = strlen(u.CStringValue) + 1; |
| 904 | break; |
| 905 | default: |
| 906 | ALOGE("%s: found bad prop type: %d, name %s", |
| 907 | __func__, mType, mName); // no payload computed |
| 908 | break; |
| 909 | } |
| 910 | return header + payload; |
| 911 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 912 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 913 | // TODO: fold into a template later. |
| 914 | status_t MediaAnalyticsItem::writeToByteString( |
| 915 | const char *name, int32_t value, char **bufferpptr, char *bufferptrmax) |
| 916 | { |
| 917 | const size_t len = 2 + 1 + strlen(name) + 1 + sizeof(value); |
| 918 | if (len > UINT16_MAX) return BAD_VALUE; |
| 919 | return insert((uint16_t)len, bufferpptr, bufferptrmax) |
| 920 | ?: insert((uint8_t)kTypeInt32, bufferpptr, bufferptrmax) |
| 921 | ?: insert(name, bufferpptr, bufferptrmax) |
| 922 | ?: insert(value, bufferpptr, bufferptrmax); |
| 923 | } |
| 924 | |
| 925 | status_t MediaAnalyticsItem::writeToByteString( |
| 926 | const char *name, int64_t value, char **bufferpptr, char *bufferptrmax) |
| 927 | { |
| 928 | const size_t len = 2 + 1 + strlen(name) + 1 + sizeof(value); |
| 929 | if (len > UINT16_MAX) return BAD_VALUE; |
| 930 | return insert((uint16_t)len, bufferpptr, bufferptrmax) |
| 931 | ?: insert((uint8_t)kTypeInt64, bufferpptr, bufferptrmax) |
| 932 | ?: insert(name, bufferpptr, bufferptrmax) |
| 933 | ?: insert(value, bufferpptr, bufferptrmax); |
| 934 | } |
| 935 | |
| 936 | status_t MediaAnalyticsItem::writeToByteString( |
| 937 | const char *name, double value, char **bufferpptr, char *bufferptrmax) |
| 938 | { |
| 939 | const size_t len = 2 + 1 + strlen(name) + 1 + sizeof(value); |
| 940 | if (len > UINT16_MAX) return BAD_VALUE; |
| 941 | return insert((uint16_t)len, bufferpptr, bufferptrmax) |
| 942 | ?: insert((uint8_t)kTypeDouble, bufferpptr, bufferptrmax) |
| 943 | ?: insert(name, bufferpptr, bufferptrmax) |
| 944 | ?: insert(value, bufferpptr, bufferptrmax); |
| 945 | } |
| 946 | |
| 947 | status_t MediaAnalyticsItem::writeToByteString( |
| 948 | const char *name, const std::pair<int64_t, int64_t> &value, char **bufferpptr, char *bufferptrmax) |
| 949 | { |
| 950 | const size_t len = 2 + 1 + strlen(name) + 1 + 8 + 8; |
| 951 | if (len > UINT16_MAX) return BAD_VALUE; |
| 952 | return insert((uint16_t)len, bufferpptr, bufferptrmax) |
| 953 | ?: insert((uint8_t)kTypeRate, bufferpptr, bufferptrmax) |
| 954 | ?: insert(name, bufferpptr, bufferptrmax) |
| 955 | ?: insert(value.first, bufferpptr, bufferptrmax) |
| 956 | ?: insert(value.second, bufferpptr, bufferptrmax); |
| 957 | } |
| 958 | |
| 959 | status_t MediaAnalyticsItem::writeToByteString( |
| 960 | const char *name, char * const &value, char **bufferpptr, char *bufferptrmax) |
| 961 | { |
| 962 | const size_t len = 2 + 1 + strlen(name) + 1 + strlen(value) + 1; |
| 963 | if (len > UINT16_MAX) return BAD_VALUE; |
| 964 | return insert((uint16_t)len, bufferpptr, bufferptrmax) |
| 965 | ?: insert((uint8_t)kTypeCString, bufferpptr, bufferptrmax) |
| 966 | ?: insert(name, bufferpptr, bufferptrmax) |
| 967 | ?: insert(value, bufferpptr, bufferptrmax); |
| 968 | } |
| 969 | |
| 970 | status_t MediaAnalyticsItem::writeToByteString( |
| 971 | const char *name, const none_t &, char **bufferpptr, char *bufferptrmax) |
| 972 | { |
| 973 | const size_t len = 2 + 1 + strlen(name) + 1; |
| 974 | if (len > UINT16_MAX) return BAD_VALUE; |
| 975 | return insert((uint16_t)len, bufferpptr, bufferptrmax) |
| 976 | ?: insert((uint8_t)kTypeCString, bufferpptr, bufferptrmax) |
| 977 | ?: insert(name, bufferpptr, bufferptrmax); |
| 978 | } |
| 979 | |
| 980 | status_t MediaAnalyticsItem::Prop::writeToByteString( |
| 981 | char **bufferpptr, char *bufferptrmax) const |
| 982 | { |
| 983 | switch (mType) { |
| 984 | case kTypeInt32: |
| 985 | return MediaAnalyticsItem::writeToByteString(mName, u.int32Value, bufferpptr, bufferptrmax); |
| 986 | case kTypeInt64: |
| 987 | return MediaAnalyticsItem::writeToByteString(mName, u.int64Value, bufferpptr, bufferptrmax); |
| 988 | case kTypeDouble: |
| 989 | return MediaAnalyticsItem::writeToByteString(mName, u.doubleValue, bufferpptr, bufferptrmax); |
| 990 | case kTypeRate: |
| 991 | return MediaAnalyticsItem::writeToByteString(mName, u.rate, bufferpptr, bufferptrmax); |
| 992 | case kTypeCString: |
| 993 | return MediaAnalyticsItem::writeToByteString(mName, u.CStringValue, bufferpptr, bufferptrmax); |
| 994 | case kTypeNone: |
| 995 | return MediaAnalyticsItem::writeToByteString(mName, none_t{}, bufferpptr, bufferptrmax); |
| 996 | default: |
| 997 | ALOGE("%s: found bad prop type: %d, name %s", |
| 998 | __func__, mType, mName); // no payload sent |
| 999 | return BAD_VALUE; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | status_t MediaAnalyticsItem::Prop::readFromByteString( |
| 1004 | const char **bufferpptr, const char *bufferptrmax) |
| 1005 | { |
| 1006 | uint16_t len; |
| 1007 | char *name; |
| 1008 | uint8_t type; |
| 1009 | status_t status = extract(&len, bufferpptr, bufferptrmax) |
| 1010 | ?: extract(&type, bufferpptr, bufferptrmax) |
| 1011 | ?: extract(&name, bufferpptr, bufferptrmax); |
| 1012 | if (status != NO_ERROR) return status; |
| 1013 | if (mName != nullptr) { |
| 1014 | free(mName); |
| 1015 | } |
| 1016 | mName = name; |
| 1017 | if (mType == kTypeCString) { |
| 1018 | free(u.CStringValue); |
| 1019 | u.CStringValue = nullptr; |
| 1020 | } |
| 1021 | mType = (Type)type; |
| 1022 | switch (mType) { |
| 1023 | case kTypeInt32: |
| 1024 | return extract(&u.int32Value, bufferpptr, bufferptrmax); |
| 1025 | case kTypeInt64: |
| 1026 | return extract(&u.int64Value, bufferpptr, bufferptrmax); |
| 1027 | case kTypeDouble: |
| 1028 | return extract(&u.doubleValue, bufferpptr, bufferptrmax); |
| 1029 | case kTypeRate: |
| 1030 | return extract(&u.rate.first, bufferpptr, bufferptrmax) |
| 1031 | ?: extract(&u.rate.second, bufferpptr, bufferptrmax); |
| 1032 | case kTypeCString: |
| 1033 | status = extract(&u.CStringValue, bufferpptr, bufferptrmax); |
| 1034 | if (status != NO_ERROR) mType = kTypeNone; |
| 1035 | return status; |
| 1036 | case kTypeNone: |
| 1037 | return NO_ERROR; |
| 1038 | default: |
| 1039 | mType = kTypeNone; |
| 1040 | ALOGE("%s: found bad prop type: %d, name %s", |
| 1041 | __func__, mType, mName); // no payload sent |
| 1042 | return BAD_VALUE; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | } // namespace android |