summaryrefslogtreecommitdiff
path: root/media/eco/ECOData.cpp
blob: 3d7b533821c4f774884795f07fb73978247d8284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "ECOData"

#include "eco/ECOData.h"

#include <binder/Parcel.h>
#include <inttypes.h>
#include <utils/Errors.h>
#include <utils/Log.h>

#include <string>

#include "eco/ECODataKey.h"
#include "eco/ECOUtils.h"

namespace android {
namespace media {
namespace eco {

using namespace ::android;

status_t ECOData::readFromParcel(const Parcel* parcel) {
    if (parcel == nullptr) {
        ALOGE("readFromParcel failed. Parcel pointer can not be null");
        return BAD_VALUE;
    }

    // Reads the data type and time.
    RETURN_STATUS_IF_ERROR(parcel->readInt32(&mDataType));
    RETURN_STATUS_IF_ERROR(parcel->readInt64(&mDataTimeUs));

    // Reads the number of items.
    uint32_t numOfItems = 0;
    RETURN_STATUS_IF_ERROR(parcel->readUint32(&numOfItems));

    // Reads the key-value pairs one by one.
    for (size_t i = 0; i < numOfItems; ++i) {
        // Reads the name of the key.
        const char* name = parcel->readCString();
        if (name == NULL) {
            ALOGE("Failed reading name for the key. Parsing aborted.");
            return NAME_NOT_FOUND;
        }

        int32_t type;
        RETURN_STATUS_IF_ERROR(parcel->readInt32(&type));
        switch (static_cast<ValueType>(type)) {
        case kTypeInt32: {
            int32_t value32;
            RETURN_STATUS_IF_ERROR(parcel->readInt32(&value32));
            setInt32(std::string(name), value32);
            break;
        }
        case kTypeInt64: {
            int64_t value64;
            RETURN_STATUS_IF_ERROR(parcel->readInt64(&value64));
            setInt64(std::string(name), value64);
            break;
        }
        case kTypeSize: {
            int32_t valueSize;
            RETURN_STATUS_IF_ERROR(parcel->readInt32(&valueSize));
            setInt32(std::string(name), valueSize);
            break;
        }
        case kTypeFloat: {
            float valueFloat;
            RETURN_STATUS_IF_ERROR(parcel->readFloat(&valueFloat));
            setFloat(std::string(name), valueFloat);
            break;
        }
        case kTypeDouble: {
            double valueDouble;
            RETURN_STATUS_IF_ERROR(parcel->readDouble(&valueDouble));
            setDouble(std::string(name), valueDouble);
            break;
        }
        case kTypeString: {
            const char* valueStr = parcel->readCString();
            if (valueStr == NULL) {
                ALOGE("Failed reading name for the key. Parsing aborted.");
                return NAME_NOT_FOUND;
            }
            setString(std::string(name), valueStr);
            break;
        }
        case kTypeInt8: {
            int8_t value8;
            RETURN_STATUS_IF_ERROR(parcel->readByte(&value8));
            setInt8(std::string(name), value8);
            break;
        }
        default: {
            return BAD_TYPE;
        }
        }
    }

    return NO_ERROR;
}

status_t ECOData::writeToParcel(Parcel* parcel) const {
    if (parcel == nullptr) {
        ALOGE("writeToParcel failed. Parcel pointer can not be null");
        return BAD_VALUE;
    }

    // Writes out the data type and time.
    RETURN_STATUS_IF_ERROR(parcel->writeInt32(mDataType));
    RETURN_STATUS_IF_ERROR(parcel->writeInt64(mDataTimeUs));

    // Writes out number of items.
    RETURN_STATUS_IF_ERROR(parcel->writeUint32(int32_t(mKeyValueStore.size())));

    // Writes out the key-value pairs one by one.
    for (const auto& it : mKeyValueStore) {
        // Writes out the key.
        RETURN_STATUS_IF_ERROR(parcel->writeCString(it.first.c_str()));

        // Writes out the data type.
        const ECODataValueType& value = it.second;
        RETURN_STATUS_IF_ERROR(parcel->writeInt32(static_cast<int32_t>(value.index())));
        switch (static_cast<ValueType>(value.index())) {
        case kTypeInt32:
            RETURN_STATUS_IF_ERROR(parcel->writeInt32(std::get<int32_t>(it.second)));
            break;

        case kTypeInt64:
            RETURN_STATUS_IF_ERROR(parcel->writeInt64(std::get<int64_t>(it.second)));
            break;

        case kTypeSize:
            RETURN_STATUS_IF_ERROR(parcel->writeUint32(std::get<size_t>(it.second)));
            break;

        case kTypeFloat:
            RETURN_STATUS_IF_ERROR(parcel->writeFloat(std::get<float>(it.second)));
            break;

        case kTypeDouble:
            RETURN_STATUS_IF_ERROR(parcel->writeDouble(std::get<double>(it.second)));
            break;

        case kTypeString:
            RETURN_STATUS_IF_ERROR(parcel->writeCString(std::get<std::string>(it.second).c_str()));
            break;

        case kTypeInt8:
            RETURN_STATUS_IF_ERROR(parcel->writeByte(std::get<int8_t>(it.second)));
            break;

        default:
            return BAD_TYPE;
        }
    }

    return NO_ERROR;
}

int32_t ECOData::getDataType() const {
    return mDataType;
}

int64_t ECOData::getDataTimeUs() const {
    return mDataTimeUs;
}

// Inserts a new key into store if the key does not exist yet. Otherwise, this will override the
// existing key's value.
ECODataStatus ECOData::setString(const std::string& key, const std::string& value) {
    if (key.empty() || value.empty()) {
        return ECODataStatus::INVALID_ARGUMENT;
    }

    mKeyValueStore[key] = value;

    // TODO(hkuang): Check the valueType is valid for the key.
    return ECODataStatus::OK;
}

ECODataStatus ECOData::findString(const std::string& key, std::string* value) const {
    if (key.empty()) {
        return ECODataStatus::INVALID_ARGUMENT;
    }

    // Check if the key exists.
    if (mKeyValueStore.find(key) == mKeyValueStore.end()) {
        return ECODataStatus::KEY_NOT_EXIST;
    }

    // Safely access the value.
    const std::string& entryValue = std::get<std::string>(mKeyValueStore.at(key));
    value->assign(entryValue);

    return ECODataStatus::OK;
}

// Inserts a new key into store if the key does not exist yet. Otherwise, this will override the
// existing key's value.
template <typename T>
ECODataStatus ECOData::setValue(const std::string& key, T value) {
    if (key.empty()) {
        return ECODataStatus::INVALID_ARGUMENT;
    }

    mKeyValueStore[key] = value;
    return ECODataStatus::OK;
}

template <typename T>
ECODataStatus ECOData::findValue(const std::string& key, T* out) const {
    if (key.empty() || out == nullptr) {
        return ECODataStatus::INVALID_ARGUMENT;
    }

    if (mKeyValueStore.find(key) == mKeyValueStore.end()) {
        return ECODataStatus::KEY_NOT_EXIST;
    }

    // Safely access the value.
    *out = std::get<T>(mKeyValueStore.at(key));

    return ECODataStatus::OK;
}

ECODataStatus ECOData::setInt32(const std::string& key, int32_t value) {
    return setValue<int32_t>(key, value);
}

ECODataStatus ECOData::findInt32(const std::string& key, int32_t* out) const {
    return findValue<int32_t>(key, out);
}

ECODataStatus ECOData::setInt64(const std::string& key, int64_t value) {
    return setValue<int64_t>(key, value);
}

ECODataStatus ECOData::findInt64(const std::string& key, int64_t* out) const {
    return findValue<int64_t>(key, out);
}

ECODataStatus ECOData::setDouble(const std::string& key, double value) {
    return setValue<double>(key, value);
}

ECODataStatus ECOData::findDouble(const std::string& key, double* out) const {
    return findValue<double>(key, out);
}

ECODataStatus ECOData::setSize(const std::string& key, size_t value) {
    return setValue<size_t>(key, value);
}

ECODataStatus ECOData::findSize(const std::string& key, size_t* out) const {
    return findValue<size_t>(key, out);
}

ECODataStatus ECOData::setFloat(const std::string& key, float value) {
    return setValue<float>(key, value);
}

ECODataStatus ECOData::findFloat(const std::string& key, float* out) const {
    return findValue<float>(key, out);
}

ECODataStatus ECOData::setInt8(const std::string& key, int8_t value) {
    return setValue<int8_t>(key, value);
}

ECODataStatus ECOData::findInt8(const std::string& key, int8_t* out) const {
    return findValue<int8_t>(key, out);
}

ECODataStatus ECOData::set(const std::string& key, const ECOData::ECODataValueType& value) {
    if (key.empty()) {
        return ECODataStatus::INVALID_ARGUMENT;
    }
    mKeyValueStore[key] = value;
    return ECODataStatus::OK;
}

ECODataStatus ECOData::find(const std::string& key, ECOData::ECODataValueType* out) const {
    if (key.empty() || out == nullptr) {
        return ECODataStatus::INVALID_ARGUMENT;
    }

    if (mKeyValueStore.find(key) == mKeyValueStore.end()) {
        return ECODataStatus::KEY_NOT_EXIST;
    }

    // Safely access the value.
    *out = mKeyValueStore.at(key);

    return ECODataStatus::OK;
}

std::string ECOData::getDataTypeString() const {
    switch (mDataType) {
    case DATA_TYPE_UNKNOWN:
        return "DATA_TYPE_UNKNOWN";
    case DATA_TYPE_STATS:
        return "DATA_TYPE_STATS";
    case DATA_TYPE_INFO:
        return "DATA_TYPE_INFO";
    case DATA_TYPE_STATS_PROVIDER_CONFIG:
        return "DATA_TYPE_STATS_PROVIDER_CONFIG";
    case DATA_TYPE_INFO_LISTENER_CONFIG:
        return "DATA_TYPE_INFO_LISTENER_CONFIG";
    }
    return {};
}

// TODO(hkuang): Add test for this.
bool ECODataKeyValueIterator::hasNext() {
    if (mIterator == mKeyValueStore.end()) return false;

    if (!mBeginReturned) {
        // mIterator has been initialized to the beginning and
        // hasn't been returned. Do not advance:
        mBeginReturned = true;
    } else {
        std::advance(mIterator, 1);
    }
    return mIterator != mKeyValueStore.end();
}

// TODO(hkuang): Add test for this.
ECOData::ECODataKeyValuePair ECODataKeyValueIterator::next() const {
    return ECOData::ECODataKeyValuePair(mIterator->first, mIterator->second);
}

std::string ECOData::debugString() const {
    std::string s = "ECOData(type = ";

    std::string tmp;
    switch (mDataType) {
    case DATA_TYPE_UNKNOWN:
        tmp = "Unknown";
        break;
    case DATA_TYPE_STATS:
        tmp = "Stats";
        break;
    case DATA_TYPE_INFO:
        tmp = "Info";
        break;
    case DATA_TYPE_STATS_PROVIDER_CONFIG:
        tmp = "Stats provider config";
        break;
    case DATA_TYPE_INFO_LISTENER_CONFIG:
        tmp = "Info listener config";
        break;
    default:
        break;
    }
    s.append(tmp);
    s.append(") = {\n  ");

    // Writes out the key-value pairs one by one.
    for (const auto& it : mKeyValueStore) {
        const size_t SIZE = 100;
        char keyValue[SIZE];
        const ECODataValueType& value = it.second;
        switch (static_cast<ValueType>(value.index())) {
        case kTypeInt32:
            snprintf(keyValue, SIZE, "int32_t %s = %d, ", it.first.c_str(),
                     std::get<int32_t>(it.second));
            break;
        case kTypeInt64:
            snprintf(keyValue, SIZE, "int64_t %s = %" PRId64 ", ", it.first.c_str(),
                     std::get<int64_t>(it.second));
            break;
        case kTypeSize:
            snprintf(keyValue, SIZE, "size_t %s = %zu, ", it.first.c_str(),
                     std::get<size_t>(it.second));
            break;
        case kTypeFloat:
            snprintf(keyValue, SIZE, "float %s = %f, ", it.first.c_str(),
                     std::get<float>(it.second));
            break;
        case kTypeDouble:
            snprintf(keyValue, SIZE, "double %s = %f, ", it.first.c_str(),
                     std::get<double>(it.second));
            break;
        case kTypeString:
            snprintf(keyValue, SIZE, "string %s = %s, ", it.first.c_str(),
                     std::get<std::string>(it.second).c_str());
            break;
        case kTypeInt8:
            snprintf(keyValue, SIZE, "int8_t %s = %d, ", it.first.c_str(),
                     std::get<int8_t>(it.second));
            break;
        default:
            break;
        }
        s.append(keyValue);
    }

    s.append("\n }");

    return s;
}

std::string ECOData::toString() const {
    return debugString();
}

}  // namespace eco
}  // namespace media
}  // namespace android