1 /*
2  * Copyright (c) 2022-2024 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 "ipc_server_listenermgr.h"
17 
18 #include "dm_constants.h"
19 #include "dm_log.h"
20 
21 namespace OHOS {
22 namespace DistributedHardware {
23 DM_IMPLEMENT_SINGLE_INSTANCE(IpcServerListenermgr);
24 
RegisterListener(std::string & pkgName,const CommonSvcId * svcId)25 int32_t IpcServerListenermgr::RegisterListener(std::string &pkgName, const CommonSvcId *svcId)
26 {
27     if (pkgName == "" || svcId == nullptr) {
28         LOGE("invalid param");
29         return ERR_DM_FAILED;
30     }
31     LOGI("new listener register:%{public}s", pkgName.c_str());
32     std::lock_guard<std::mutex> autoLock(lock_);
33     dmListenerMap_[pkgName] = *svcId;
34     return DM_OK;
35 }
36 
GetListenerByPkgName(std::string & pkgName,CommonSvcId * svcId)37 int32_t IpcServerListenermgr::GetListenerByPkgName(std::string &pkgName, CommonSvcId *svcId)
38 {
39     if (pkgName == "" || svcId == nullptr) {
40         LOGE("invalid param");
41         return ERR_DM_FAILED;
42     }
43     std::lock_guard<std::mutex> autoLock(lock_);
44     std::map<std::string, CommonSvcId>::iterator iter = dmListenerMap_.find(pkgName);
45     if (iter == dmListenerMap_.end()) {
46         LOGE("listener not found for pkg:%{public}s", pkgName.c_str());
47         return ERR_DM_FAILED;
48     }
49     *svcId = iter->second;
50     return DM_OK;
51 }
52 
UnregisterListener(std::string & pkgName)53 int32_t IpcServerListenermgr::UnregisterListener(std::string &pkgName)
54 {
55     std::lock_guard<std::mutex> autoLock(lock_);
56     dmListenerMap_.erase(pkgName);
57     return DM_OK;
58 }
59 
GetAllListeners()60 const std::map<std::string, CommonSvcId> &IpcServerListenermgr::GetAllListeners()
61 {
62     return dmListenerMap_;
63 }
64 } // namespace DistributedHardware
65 } // namespace OHOS
66