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 CHECK_SIZE_RETURN(dmListenerMap_, ERR_DM_FAILED);
34 dmListenerMap_[pkgName] = *svcId;
35 return DM_OK;
36 }
37
GetListenerByPkgName(std::string & pkgName,CommonSvcId * svcId)38 int32_t IpcServerListenermgr::GetListenerByPkgName(std::string &pkgName, CommonSvcId *svcId)
39 {
40 if (pkgName == "" || svcId == nullptr) {
41 LOGE("invalid param");
42 return ERR_DM_FAILED;
43 }
44 std::lock_guard<std::mutex> autoLock(lock_);
45 std::map<std::string, CommonSvcId>::iterator iter = dmListenerMap_.find(pkgName);
46 if (iter == dmListenerMap_.end()) {
47 LOGE("listener not found for pkg:%{public}s", pkgName.c_str());
48 return ERR_DM_FAILED;
49 }
50 *svcId = iter->second;
51 return DM_OK;
52 }
53
UnregisterListener(std::string & pkgName)54 int32_t IpcServerListenermgr::UnregisterListener(std::string &pkgName)
55 {
56 std::lock_guard<std::mutex> autoLock(lock_);
57 dmListenerMap_.erase(pkgName);
58 return DM_OK;
59 }
60
GetAllListeners()61 const std::map<std::string, CommonSvcId> &IpcServerListenermgr::GetAllListeners()
62 {
63 return dmListenerMap_;
64 }
65 } // namespace DistributedHardware
66 } // namespace OHOS
67