1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "device_profile_utils.h"
17
18 #include "parcel_helper.h"
19
20 namespace OHOS {
21 namespace DeviceProfile {
22 namespace {
23 const std::string TAG = "DeviceProfileUtils";
24
25 constexpr int32_t NON_ANONYMIZED_LENGTH = 6;
26 const std::string EMPTY_DEVICE_ID = "";
27 }
28
WriteProfileEvents(const std::list<ProfileEvent> & profileEvents,Parcel & parcel)29 bool DeviceProfileUtils::WriteProfileEvents(const std::list<ProfileEvent>& profileEvents, Parcel& parcel)
30 {
31 size_t size = profileEvents.size();
32 PARCEL_WRITE_HELPER_RET(parcel, Uint32, static_cast<uint32_t>(size), false);
33 for (auto profileEvent : profileEvents) {
34 PARCEL_WRITE_HELPER_RET(parcel, Uint32, static_cast<uint32_t>(profileEvent), false);
35 }
36 return true;
37 }
38
ReadProfileEvents(Parcel & parcel,std::list<ProfileEvent> & profileEvents)39 bool DeviceProfileUtils::ReadProfileEvents(Parcel& parcel, std::list<ProfileEvent>& profileEvents)
40 {
41 uint32_t numEvents = parcel.ReadUint32();
42 for (uint32_t i = 0; i < numEvents; i++) {
43 ProfileEvent profileEvent = static_cast<ProfileEvent>(parcel.ReadUint32());
44 if (profileEvent >= EVENT_PROFILE_END || profileEvent == EVENT_UNKNOWN) {
45 return false;
46 }
47 profileEvents.emplace_back(profileEvent);
48 }
49 return true;
50 }
51
AnonymizeDeviceId(const std::string & deviceId)52 std::string DeviceProfileUtils::AnonymizeDeviceId(const std::string& deviceId)
53 {
54 if (deviceId.length() < NON_ANONYMIZED_LENGTH) {
55 return EMPTY_DEVICE_ID;
56 }
57 std::string anonDeviceId = deviceId.substr(0, NON_ANONYMIZED_LENGTH);
58 anonDeviceId.append("******");
59 return anonDeviceId;
60 }
61 } // namespace DeviceProfile
62 } // namespace OHOS