1 /*
2 * Copyright 2020 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
17 #include <ui/DeviceProductInfo.h>
18
19 #include <android-base/stringprintf.h>
20 #include <ui/FlattenableHelpers.h>
21 #include <utils/Log.h>
22
23 #define RETURN_IF_ERROR(op) \
24 if (const status_t status = (op); status != OK) return status;
25
26 namespace android {
27
28 using base::StringAppendF;
29
getFlattenedSize() const30 size_t DeviceProductInfo::getFlattenedSize() const {
31 return FlattenableHelpers::getFlattenedSize(name) +
32 FlattenableHelpers::getFlattenedSize(manufacturerPnpId) +
33 FlattenableHelpers::getFlattenedSize(productId) +
34 FlattenableHelpers::getFlattenedSize(manufactureOrModelDate) +
35 FlattenableHelpers::getFlattenedSize(relativeAddress);
36 }
37
flatten(void * buffer,size_t size) const38 status_t DeviceProductInfo::flatten(void* buffer, size_t size) const {
39 if (size < getFlattenedSize()) {
40 return NO_MEMORY;
41 }
42 RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, name));
43 RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, manufacturerPnpId));
44 RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, productId));
45 RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, manufactureOrModelDate));
46 RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, relativeAddress));
47 return OK;
48 }
49
unflatten(void const * buffer,size_t size)50 status_t DeviceProductInfo::unflatten(void const* buffer, size_t size) {
51 RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &name));
52 RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &manufacturerPnpId));
53 RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &productId));
54 RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &manufactureOrModelDate));
55 RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &relativeAddress));
56 return OK;
57 }
58
dump(std::string & result) const59 void DeviceProductInfo::dump(std::string& result) const {
60 StringAppendF(&result, "{name=\"%s\", ", name.c_str());
61 StringAppendF(&result, "manufacturerPnpId=%s, ", manufacturerPnpId.data());
62 StringAppendF(&result, "productId=%s, ", productId.c_str());
63
64 if (const auto* model = std::get_if<ModelYear>(&manufactureOrModelDate)) {
65 StringAppendF(&result, "modelYear=%u, ", model->year);
66 } else if (const auto* manufactureWeekAndYear =
67 std::get_if<ManufactureWeekAndYear>(&manufactureOrModelDate)) {
68 StringAppendF(&result, "manufactureWeek=%u, ", manufactureWeekAndYear->week);
69 StringAppendF(&result, "manufactureYear=%d, ", manufactureWeekAndYear->year);
70 } else if (const auto* manufactureYear =
71 std::get_if<ManufactureYear>(&manufactureOrModelDate)) {
72 StringAppendF(&result, "manufactureYear=%d, ", manufactureYear->year);
73 } else {
74 ALOGE("Unknown alternative for variant DeviceProductInfo::ManufactureOrModelDate");
75 }
76
77 result.append("relativeAddress=[");
78 for (size_t i = 0; i < relativeAddress.size(); i++) {
79 if (i != 0) {
80 result.append(", ");
81 }
82 StringAppendF(&result, "%u", relativeAddress[i]);
83 }
84 result.append("]}");
85 }
86
87 } // namespace android
88