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 
16 #include "request_config.h"
17 
18 #include <parcel.h>
19 
20 #include "constant_definition.h"
21 
22 namespace OHOS {
23 namespace Location {
RequestConfig()24 RequestConfig::RequestConfig()
25 {
26     scenario_ = SCENE_UNSET;
27     priority_ = PRIORITY_FAST_FIRST_FIX;
28     timeInterval_ = 1; // no time interval limit for reporting location
29     distanceInterval_ = 0.0; // no distance interval limit for reporting location
30     maxAccuracy_ = 0.0; // no accuracy limit for reporting location
31     fixNumber_ = 0; // no fix size limit for reporting location
32     timeOut_ = DEFAULT_TIMEOUT_5S;
33     timestamp_ = 0;
34 }
35 
RequestConfig(const int scenario)36 RequestConfig::RequestConfig(const int scenario) : scenario_(scenario)
37 {
38     priority_ = PRIORITY_UNSET;
39     timeInterval_ = 1; // no time interval limit for reporting location
40     distanceInterval_ = 0.0; // no distance interval limit for reporting location
41     maxAccuracy_ = 0.0; // no accuracy limit for reporting location
42     fixNumber_ = 0; // no fix size limit for reporting location
43     timeOut_ = DEFAULT_TIMEOUT_5S;
44     timestamp_ = 0;
45 }
46 
Set(RequestConfig & requestConfig)47 void RequestConfig::Set(RequestConfig& requestConfig)
48 {
49     scenario_ = requestConfig.GetScenario();
50     priority_ = requestConfig.GetPriority();
51     timeInterval_ = requestConfig.GetTimeInterval();
52     distanceInterval_ = requestConfig.GetDistanceInterval();
53     maxAccuracy_ = requestConfig.GetMaxAccuracy();
54     fixNumber_ = requestConfig.GetFixNumber();
55     timeOut_ = requestConfig.GetTimeOut();
56     timestamp_ = requestConfig.GetTimeStamp();
57 }
58 
IsSame(RequestConfig & requestConfig)59 bool RequestConfig::IsSame(RequestConfig& requestConfig)
60 {
61     if (scenario_ != requestConfig.GetScenario()) {
62         return false;
63     }
64     if (scenario_ != SCENE_UNSET) {
65         return true;
66     }
67     return priority_ == requestConfig.GetPriority();
68 }
69 
ReadFromParcel(Parcel & parcel)70 void RequestConfig::ReadFromParcel(Parcel& parcel)
71 {
72     scenario_ = parcel.ReadInt32();
73     priority_ = parcel.ReadInt32();
74     timeInterval_ = parcel.ReadInt32();
75     distanceInterval_ = parcel.ReadDouble();
76     maxAccuracy_ = parcel.ReadFloat();
77     fixNumber_ = parcel.ReadInt32();
78     timeOut_ = parcel.ReadInt32();
79 }
80 
Unmarshalling(Parcel & parcel)81 std::unique_ptr<RequestConfig> RequestConfig::Unmarshalling(Parcel& parcel)
82 {
83     std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
84     requestConfig->ReadFromParcel(parcel);
85     return requestConfig;
86 }
87 
Marshalling(Parcel & parcel) const88 bool RequestConfig::Marshalling(Parcel& parcel) const
89 {
90     return parcel.WriteInt32(scenario_) &&
91            parcel.WriteInt32(priority_) &&
92            parcel.WriteInt32(timeInterval_) &&
93            parcel.WriteDouble(distanceInterval_) &&
94            parcel.WriteFloat(maxAccuracy_) &&
95            parcel.WriteInt32(fixNumber_) &&
96            parcel.WriteInt32(timeOut_);
97 }
98 
ToString() const99 std::string RequestConfig::ToString() const
100 {
101     std::string str = "scenario : " + std::to_string(scenario_) +
102         ", location priority : " + std::to_string(priority_) +
103         ", timeInterval : " + std::to_string(timeInterval_) +
104         ", distanceInterval : " + std::to_string(distanceInterval_) +
105         ", maxAccuracy : " + std::to_string(maxAccuracy_) +
106         ", fixNumber : " + std::to_string(fixNumber_) +
107         ", timeOut : " + std::to_string(timeOut_);
108     return str;
109 }
110 } // namespace Location
111 } // namespace OHOS