1 /*
2 * Copyright (C) 2022 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 #ifndef FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_PROPERTY_H_
16 #define FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_PROPERTY_H_
17 #include <memory>
18 #include <stdint.h>
19 #include <time.h>
20 #include <string>
21 #include <vector>
22 
23 #include "mtp_constants.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class Property {
28 public:
29     class Value {
30     public:
31         Value();
32 
33         void Dump(uint32_t valueType);
34         std::string ToString(uint32_t valueType);
35 
36         union {
37             uint128_t ui128;
38             int128_t i128;
39             uint64_t ui64;
40             int64_t i64;
41             uint32_t ui32;
42             int32_t i32;
43             uint16_t ui16;
44             int16_t i16;
45             uint8_t ui8;
46             int8_t i8;
47         } bin_;
48         std::shared_ptr<std::string> str_;
49 
50     private:
51         bool BinToString(uint32_t valueType, std::string &outStr);
52         bool StrToString(uint32_t valueType, std::string &outStr);
53     };
54 
55     Property();
56     Property(uint16_t propCode, uint16_t propType, bool propWriteable = false, int value = 0);
57     virtual ~Property();
58 
59     uint16_t GetPropertyCode() const;
60     uint16_t GetDataType() const;
61 
62     bool Read(const std::vector<uint8_t> &buffer, size_t &offset);
63     void Write(std::vector<uint8_t> &buffer);
64     void SetDefaultValue(const std::shared_ptr<std::string> &str);
65     void SetCurrentValue(const std::shared_ptr<std::string> &str);
66     const std::shared_ptr<Value> GetCurrentValue();
67     void SetFormRange(int min, int max, int step);
68     void SetFormEnum(const std::vector<int> &values);
69     void SetFormDateTime();
70 
71     bool IsDeviceProperty() const;
72     bool IsArrayType() const;
73     void Dump();
74 
75     uint32_t handle_ {0}; // not a element for property
76     uint16_t code_ {0};
77     uint16_t type_ {MTP_TYPE_UNDEFINED_CODE};
78     bool writeable_ {false};
79     std::shared_ptr<Value> defaultValue;
80     std::shared_ptr<Value> currentValue;
81     std::shared_ptr<std::vector<Value>> defaultValues; // for array types
82     std::shared_ptr<std::vector<Value>> currentValues; // for array types
83 
84     enum Form : uint8_t {
85         None = 0,
86         Range = 1,
87         Enum = 2,
88         DateTime = 3,
89     };
90 
91     uint32_t groupCode_ {0};
92     uint8_t formFlag_ {Form::None};
93     std::shared_ptr<Value> minValue; // for range form
94     std::shared_ptr<Value> maxValue;
95     std::shared_ptr<Value> stepSize;
96     std::shared_ptr<std::vector<Value>> enumValues; // for enum form
97 
98 private:
99     bool ReadValueData(const std::vector<uint8_t> &buffer, size_t &offset);
100     bool ReadFormData(const std::vector<uint8_t> &buffer, size_t &offset);
101     void WriteValueData(std::vector<uint8_t> &buffer);
102     void WriteFormData(std::vector<uint8_t> &buffer);
103 
104     bool ReadValue(const std::vector<uint8_t> &buffer, size_t &offset, Value &value);
105     bool ReadValueEx(const std::vector<uint8_t> &buffer, size_t &offset, Value &value);
106     void WriteValue(std::vector<uint8_t> &buffer, const Value &value);
107     void WriteValueEx(std::vector<uint8_t> &buffer, const Value &value);
108 
109     bool ReadArrayValues(const std::vector<uint8_t> &buffer, size_t &offset,
110         std::shared_ptr<std::vector<Value>> &values);
111     void WriteArrayValues(std::vector<uint8_t> &buffer, const std::shared_ptr<std::vector<Value>> &values);
112 
113     void DumpValue(uint8_t indent, const std::shared_ptr<Value> &value, const std::string &name);
114     void DumpValues(uint8_t indent, const std::shared_ptr<std::vector<Value>> &values, const std::string &name);
115     void DumpForm(uint8_t indent);
116 };
117 } // namespace Media
118 } // namespace OHOS
119 #endif // FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_PROPERTY_H_
120