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 "location_vendor_interface.h"
17 
18 #include <dlfcn.h>
19 #include <hdf_log.h>
20 
21 #include "gnss_interface_impl.h"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Location {
26 namespace {
27 const std::string VENDOR_NAME = "vendorGnssAdapter.so";
28 } // namespace
29 
30 std::mutex LocationVendorInterface::mutex_;
31 LocationVendorInterface* LocationVendorInterface::instance_ = nullptr;
32 
LocationVendorInterface()33 LocationVendorInterface::LocationVendorInterface()
34 {
35     Init();
36     HDF_LOGI("%{public}s constructed.", __func__);
37 }
38 
~LocationVendorInterface()39 LocationVendorInterface::~LocationVendorInterface()
40 {
41     CleanUp();
42     HDF_LOGI("%{public}s destructed.", __func__);
43 }
44 
GetInstance()45 LocationVendorInterface* LocationVendorInterface::GetInstance()
46 {
47     if (instance_ == nullptr) {
48         std::lock_guard<std::mutex> lock(mutex_);
49         if (instance_ == nullptr) {
50             instance_ = new LocationVendorInterface();
51         }
52     }
53     return instance_;
54 }
55 
DestroyInstance()56 void LocationVendorInterface::DestroyInstance()
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     if (instance_ != nullptr) {
60         delete instance_;
61         instance_ = nullptr;
62     }
63 }
64 
Init()65 void LocationVendorInterface::Init()
66 {
67     HDF_LOGI("%{public}s.", __func__);
68     vendorHandle_ = dlopen(VENDOR_NAME.c_str(), RTLD_LAZY);
69     if (!vendorHandle_) {
70         HDF_LOGE("%{public}s:dlopen %{public}s failed: %{public}s", __func__, VENDOR_NAME.c_str(), dlerror());
71         return;
72     }
73     GnssVendorDevice *gnssDevice = static_cast<GnssVendorDevice *>(dlsym(vendorHandle_, "GnssVendorInterface"));
74     if (gnssDevice == nullptr) {
75         HDF_LOGE("%{public}s:dlsym GnssInterface failed.", __func__);
76         return;
77     }
78     vendorInterface_ = gnssDevice->getGnssInterface();
79     if (vendorInterface_ == nullptr) {
80         HDF_LOGE("%{public}s:getGnssInterface failed.", __func__);
81         return;
82     }
83 }
84 
GetGnssVendorInterface() const85 const GnssVendorInterface *LocationVendorInterface::GetGnssVendorInterface() const
86 {
87     if (vendorInterface_ == nullptr) {
88         HDF_LOGE("%{public}s:GetGnssVendorInterface() failed.", __func__);
89     }
90     return vendorInterface_;
91 }
92 
GetModuleInterface(int moduleId) const93 const void *LocationVendorInterface::GetModuleInterface(int moduleId) const
94 {
95     auto vendorInterface = GetGnssVendorInterface();
96     if (vendorInterface == nullptr) {
97         HDF_LOGE("%{public}s:can not get vendorInterface.", __func__);
98         return nullptr;
99     }
100     auto moduleInterface = vendorInterface->getGnssModuleIface(moduleId);
101     if (moduleInterface == nullptr) {
102         HDF_LOGE("%{public}s:can not get moduleInterface.", __func__);
103     }
104     return moduleInterface;
105 }
106 
CleanUp()107 void LocationVendorInterface::CleanUp()
108 {
109     if (vendorInterface_ == nullptr) {
110         return;
111     }
112     vendorInterface_ = nullptr;
113     dlclose(vendorHandle_);
114     vendorHandle_ = nullptr;
115 }
116 } // Location
117 } // HDI
118 } // OHOS
119