• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong DID 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 "codec_death_recipient.h"
17 #include <hdf_remote_service.h>
18 #include <map>
19 #include <mutex>
20 #include <securec.h>
21 #include <set>
22 #include <unistd.h>
23 #include "codec_log_wrapper.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 static std::map<uint64_t *, uint32_t> g_addrPidMap;
30 static std::map<uint32_t, std::set<uint32_t>> g_pidCompsMap;
31 static std::mutex g_mutex;
32 
RegisterService(struct CodecCallbackType * callbacks,uint32_t componentId,struct CodecComponentNode * codecNode)33 bool RegisterService(struct CodecCallbackType *callbacks, uint32_t componentId,
34                      struct CodecComponentNode *codecNode)
35 {
36     std::lock_guard<std::mutex> lk(g_mutex);
37     uint32_t remotePid =  static_cast<uint32_t>(HdfRemoteGetCallingPid());
38     auto comps = g_pidCompsMap.find(remotePid);
39     if (comps != g_pidCompsMap.end()) {
40         CODEC_LOGE("RemoteService had been added deathRecipient!");
41         comps->second.insert(componentId);
42         return false;
43     }
44 
45     uint64_t *addr = reinterpret_cast<uint64_t *>(callbacks->remote);
46     std::set<uint32_t> compIds;
47     compIds.insert(componentId);
48     g_pidCompsMap.emplace(std::make_pair(remotePid, compIds));
49     g_addrPidMap.emplace(std::make_pair(addr, remotePid));
50     return true;
51 }
52 
CleanMapperOfDiedService(struct HdfRemoteService * remote,uint32_t * compIds,uint32_t * size)53 int32_t CleanMapperOfDiedService(struct HdfRemoteService *remote, uint32_t *compIds, uint32_t *size)
54 {
55     std::lock_guard<std::mutex> lk(g_mutex);
56 
57     uint64_t *addr = reinterpret_cast<uint64_t *>(remote);
58     auto addrPid = g_addrPidMap.find(addr);
59     if (addrPid == g_addrPidMap.end()) {
60         CODEC_LOGE("RemoteService no mapper in g_addrPidMap!");
61         return HDF_FAILURE;
62     }
63 
64     uint32_t remotePid = addrPid->second;
65     auto comps = g_pidCompsMap.find(remotePid);
66     if (comps == g_pidCompsMap.end()) {
67         CODEC_LOGE("RemoteService no mapper in g_pidCompsMap!");
68         return HDF_FAILURE;
69     }
70 
71     std::set<uint32_t> ids = comps->second;
72     uint32_t index = 0;
73     *size = ids.size();
74     for (auto id = ids.begin(); id != ids.end(); id++) {
75         compIds[index++] = *id;
76     }
77 
78     g_addrPidMap.erase(addrPid);
79     g_pidCompsMap.erase(comps);
80     CODEC_LOGE("clean service mapper success!");
81     return HDF_SUCCESS;
82 }
83 
RemoveDestoryedComponent(uint32_t componentId)84 void RemoveDestoryedComponent(uint32_t componentId)
85 {
86     std::lock_guard<std::mutex> lk(g_mutex);
87 
88     uint32_t remotePid =  static_cast<uint32_t>(HdfRemoteGetCallingPid());
89     auto comps = g_pidCompsMap.find(remotePid);
90     if (comps != g_pidCompsMap.end()) {
91         comps->second.erase(componentId);
92     }
93 }
94 #ifdef __cplusplus
95 };
96 #endif
97