| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 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 |  | 
| Marin Shalamanov | e5cceea | 2020-04-30 18:13:10 +0200 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> | 
| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 20 | #include <ui/FlattenableHelpers.h> | 
| Marin Shalamanov | e5cceea | 2020-04-30 18:13:10 +0200 | [diff] [blame] | 21 | #include <utils/Log.h> | 
| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 22 |  | 
| Marin Shalamanov | 896e630 | 2020-04-06 16:11:25 +0200 | [diff] [blame] | 23 | #define RETURN_IF_ERROR(op) \ | 
|  | 24 | if (const status_t status = (op); status != OK) return status; | 
|  | 25 |  | 
| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 26 | namespace android { | 
|  | 27 |  | 
| Marin Shalamanov | e5cceea | 2020-04-30 18:13:10 +0200 | [diff] [blame] | 28 | using base::StringAppendF; | 
|  | 29 |  | 
| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 30 | size_t DeviceProductInfo::getFlattenedSize() const { | 
| Marin Shalamanov | 896e630 | 2020-04-06 16:11:25 +0200 | [diff] [blame] | 31 | return FlattenableHelpers::getFlattenedSize(name) + | 
|  | 32 | FlattenableHelpers::getFlattenedSize(manufacturerPnpId) + | 
|  | 33 | FlattenableHelpers::getFlattenedSize(productId) + | 
|  | 34 | FlattenableHelpers::getFlattenedSize(manufactureOrModelDate) + | 
|  | 35 | FlattenableHelpers::getFlattenedSize(relativeAddress); | 
| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
|  | 38 | status_t DeviceProductInfo::flatten(void* buffer, size_t size) const { | 
|  | 39 | if (size < getFlattenedSize()) { | 
|  | 40 | return NO_MEMORY; | 
|  | 41 | } | 
| Marin Shalamanov | 896e630 | 2020-04-06 16:11:25 +0200 | [diff] [blame] | 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; | 
| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
|  | 50 | status_t DeviceProductInfo::unflatten(void const* buffer, size_t size) { | 
| Marin Shalamanov | 896e630 | 2020-04-06 16:11:25 +0200 | [diff] [blame] | 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; | 
| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Marin Shalamanov | e5cceea | 2020-04-30 18:13:10 +0200 | [diff] [blame] | 59 | 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 |  | 
| Marin Shalamanov | 359a7e7 | 2020-02-17 17:03:07 +0100 | [diff] [blame] | 87 | } // namespace android |