• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_RIL_BASE_PARCEL_H
17 #define OHOS_RIL_BASE_PARCEL_H
18 
19 #include <cassert>
20 
21 #include "desensitize_string_stream.h"
22 #include "parcel.h"
23 
24 #include "hril_types.h"
25 #include "telephony_log_wrapper.h"
26 
27 namespace OHOS {
28 namespace Telephony {
29 // Serialize the underlying set of handler functions.
30 namespace BaseParcel {
31 // Read specific data from the parcel object.
32 bool Read(Parcel &parcel, int8_t &value);
33 bool Read(Parcel &parcel, uint8_t &value);
34 bool Read(Parcel &parcel, int32_t &value);
35 bool Read(Parcel &parcel, uint32_t &value);
36 bool Read(Parcel &parcel, int64_t &value);
37 bool Read(Parcel &parcel, uint64_t &value);
38 bool Read(Parcel &parcel, bool &value);
39 bool Read(Parcel &parcel, std::string &value);
40 // Reads only 32 bits of data. Such as: enum.
41 template<typename E>
Read(Parcel & parcel,E & e)42 bool Read(Parcel &parcel, E &e)
43 {
44     static_assert(sizeof(E) == sizeof(int32_t) && !std::is_pointer<E>::value,
45         "This interface is an enum-specific interface,"
46         " please use the enumeration type(type size == sizeof(int32_t)).");
47     return Read(parcel, *((int32_t *)&e));
48 }
49 // Write the data to the parcel object.
50 bool Write(Parcel &parcel, const int8_t &value);
51 bool Write(Parcel &parcel, const uint8_t &value);
52 bool Write(Parcel &parcel, const int32_t &value);
53 bool Write(Parcel &parcel, const uint32_t &value);
54 bool Write(Parcel &parcel, const int64_t &value);
55 bool Write(Parcel &parcel, const uint64_t &value);
56 bool Write(Parcel &parcel, const bool &value);
57 bool Write(Parcel &parcel, const std::string &value);
58 // Writes only 32 bits of data. Such as: enum.
59 template<typename E>
Write(Parcel & parcel,E && e)60 bool Write(Parcel &parcel, E &&e)
61 {
62     static_assert(sizeof(E) == sizeof(int32_t) && !std::is_pointer<E>::value,
63         "This interface is an enum-specific interface,"
64         " please use the enumeration type(type size == sizeof(int32_t)).");
65     return Write(parcel, *((const int32_t *)&e));
66 }
67 
68 /**
69  * @brief Write the data set from Parcel.
70  * @tparam ValueTypes ValueTypes Support data types: uint8_t, int32_t, int64_t, bool, std::string.
71  * @param parcel Serialized data structure.
72  * @param vals Output data set.
73  *      1st param: slotId
74  *      2nd param: serialId
75  * @return true success
76  * @return false failed
77  */
78 template<typename... ValueTypes>
WriteVals(Parcel & parcel,ValueTypes &&...vals)79 bool WriteVals(Parcel &parcel, ValueTypes &&...vals)
80 {
81     // Write data with the return value "and".
82     return (Write(parcel, std::forward<ValueTypes>(vals)) && ...);
83 }
84 
85 /**
86  * @brief Read the data set from Parcel
87  * @tparam ValueTypes Support data types: uint8_t, int32_t, int64_t, bool, std::string
88  * @param parcel Serialized data structure
89  * @param vals Outgoing data reference
90  * @return true success
91  * @return false failed
92  */
93 template<typename... ValueTypes>
ReadVals(Parcel & parcel,ValueTypes &&...vals)94 bool ReadVals(Parcel &parcel, ValueTypes &&...vals)
95 {
96     // Read data with the return value "and".
97     return (Read(parcel, std::forward<ValueTypes>(vals)) && ...);
98 }
99 } // namespace BaseParcel
100 
101 class HrilBaseParcel : public virtual Parcelable {
102 public:
103     // Formatted output for serialized structures.
104     virtual const char *ToString() const;
105 
106 protected:
107     virtual bool ReadBaseUint8(Parcel &parcel, uint8_t &value);
108     virtual bool ReadBaseInt32(Parcel &parcel, int32_t &value);
109     virtual bool ReadBaseInt64(Parcel &parcel, int64_t &value);
110     virtual bool ReadBaseBool(Parcel &parcel, bool &value);
111     virtual bool ReadBaseString(Parcel &parcel, std::string &value);
112     virtual bool WriteBaseUint8(Parcel &parcel, uint8_t value) const;
113     virtual bool WriteBaseInt32(Parcel &parcel, int32_t value) const;
114     virtual bool WriteBaseInt64(Parcel &parcel, int64_t value) const;
115     virtual bool WriteBaseBool(Parcel &parcel, bool value) const;
116     virtual bool WriteBaseString(Parcel &parcel, std::string value) const;
117 
118     template<typename... ValueTypes>
Read(Parcel & parcel,ValueTypes &&...vals)119     bool Read(Parcel &parcel, ValueTypes &&...vals)
120     {
121         return BaseParcel::ReadVals(parcel, std::forward<ValueTypes>(vals)...);
122     }
123 
124     template<typename... ValueTypes>
Write(Parcel & parcel,ValueTypes &&...vals)125     bool Write(Parcel &parcel, ValueTypes &&...vals) const
126     {
127         return BaseParcel::WriteVals(parcel, std::forward<ValueTypes>(vals)...);
128     }
129 
130     // String streams: thread variables. Used to optimize execution efficiency.
131     std::stringstream &StringStream(void) const;
132     // String: Thread variable. Used to optimize execution efficiency.
133     std::string &String(void) const;
134 };
135 
136 // Empty serialization class.
137 struct HrilEmptyParcel : public HrilBaseParcel {
ReadFromParcelHrilEmptyParcel138     bool ReadFromParcel(Parcel &parcel)
139     {
140         return true;
141     }
MarshallingHrilEmptyParcel142     virtual bool Marshalling(Parcel &parcel) const override
143     {
144         return true;
145     }
UnMarshallingHrilEmptyParcel146     std::shared_ptr<HrilEmptyParcel> UnMarshalling(Parcel &parcel)
147     {
148         return std::shared_ptr<HrilEmptyParcel>(nullptr);
149     }
DumpHrilEmptyParcel150     void Dump(std::string, int32_t) {}
151 };
152 
153 // Primitive type serialization class.
154 template<typename T>
155 struct HRilCommonParcel : public HrilBaseParcel {
156     T data;
157 
HRilCommonParcelHRilCommonParcel158     HRilCommonParcel() {}
159     // copy constructor
HRilCommonParcelHRilCommonParcel160     HRilCommonParcel(const T &d) : data(d) {}
HRilCommonParcelHRilCommonParcel161     HRilCommonParcel(const T &&d) : data(d) {}
162     // Only the std::string template type is supported.
HRilCommonParcelHRilCommonParcel163     HRilCommonParcel(const char *s)
164     {
165         if (s != nullptr) {
166             data = s;
167         }
168     }
169     // The std::string template type is not supported.
HRilCommonParcelHRilCommonParcel170     HRilCommonParcel(const uint8_t *buf, size_t bufLen)
171     {
172         static_assert(std::is_class<T>::value == 0,
173             "This constructor does not support the std::string type,"
174             " please use the HRilStringParcel(const std::string &) constructor.");
175         assert((bufLen % sizeof(T)) == 0);
176 
177         if (buf != nullptr) {
178             data = *(T *)buf;
179         } else {
180             data = 0;
181         }
182     }
183     HRilCommonParcel &operator=(const T &t)
184     {
185         data = t;
186         return *this;
187     }
ReadFromParcelHRilCommonParcel188     bool ReadFromParcel(Parcel &parcel)
189     {
190         return Read(parcel, data);
191     }
MarshallingHRilCommonParcel192     virtual bool Marshalling(Parcel &parcel) const override
193     {
194         return Write(parcel, data);
195     }
UnMarshallingHRilCommonParcel196     std::shared_ptr<HRilCommonParcel<T>> UnMarshalling(Parcel &parcel)
197     {
198         return std::make_shared<HRilCommonParcel<T>>();
199     }
DumpHRilCommonParcel200     void Dump(std::string, int32_t) {}
ToStringHRilCommonParcel201     virtual const char *ToString() const override
202     {
203         DesensitizeStringStream dss(String(), StringStream());
204         dss << DSS::Dese << data;
205         return *dss;
206     }
207 };
208 
209 // Basic type serialization type extension.
210 using HRilUint8Parcel = struct HRilCommonParcel<uint8_t>;
211 using HRilInt32Parcel = struct HRilCommonParcel<int32_t>;
212 using HRilInt64Parcel = struct HRilCommonParcel<int64_t>;
213 using HRilBoolParcel = struct HRilCommonParcel<bool>;
214 using HRilStringParcel = struct HRilCommonParcel<std::string>;
215 
216 static constexpr int32_t TELEPHONY_PARCEL_MAX_COUNT = 1024;
217 } // namespace Telephony
218 } // namespace OHOS
219 #endif // OHOS_RIL_BASE_PARCEL_H