• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <stdlib.h>
17 #include <string.h>
18 
19 #include "dbinder_service.h"
20 #include "ipc_skeleton.h"
21 #include "rpc_errno.h"
22 #include "rpc_log.h"
23 #include "serializer.h"
24 #include "utils_list.h"
25 
26 typedef struct {
27     UTILS_DL_LIST list;
28     int32_t saId;
29     SvcIdentity *sid;
30 } SvcInfo;
31 
32 static UTILS_DL_LIST *g_saList = NULL;
33 
34 enum {
35     GET_SYSTEM_ABILITY_TRANSACTION = 1,
36     ADD_SYSTEM_ABILITY_TRANSACTION = 2,
37     GET_REMOTE_SYSTEM_ABILITY_TRANSACTION = 3,
38     ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION = 4,
39 };
40 
AddSystemAbility(int32_t saId,SvcIdentity * sid)41 int32_t AddSystemAbility(int32_t saId, SvcIdentity *sid)
42 {
43     RPC_LOG_INFO("AddSystemAbility called.... handle = %d", sid->handle);
44     RPC_LOG_INFO("AddSystemAbility called.... cookie = %u", sid->cookie);
45     if (g_saList == NULL) {
46         return ERR_FAILED;
47     }
48 
49     SvcInfo* node = (SvcInfo *)calloc(1, sizeof(SvcInfo));
50     if (node == NULL) {
51         RPC_LOG_ERROR("AddSystemAbility node calloc failed");
52         return ERR_FAILED;
53     }
54     node->saId = saId;
55     node->sid = sid;
56     UtilsListAdd(g_saList, &node->list);
57     return ERR_NONE;
58 }
59 
GetSystemAbility(int32_t saId,const char * deviceId,SvcIdentity * sid)60 int32_t GetSystemAbility(int32_t saId, const char* deviceId, SvcIdentity *sid)
61 {
62     SvcInfo* node = NULL;
63     SvcInfo* next = NULL;
64     UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, g_saList, SvcInfo, list)
65     {
66         if (node->saId == saId) {
67             sid->handle = node->sid->handle;
68             sid->token = node->sid->token;
69             sid->cookie = node->sid->cookie;
70             RPC_LOG_INFO("find sa, said = %d, handle = %d, cookie = %u", saId, sid->handle, sid->cookie);
71             return ERR_NONE;
72         }
73     }
74     return ERR_FAILED;
75 }
76 
AddRemoteSystemAbility(int32_t saId,SvcIdentity * sid)77 int32_t AddRemoteSystemAbility(int32_t saId, SvcIdentity *sid)
78 {
79     if (AddSystemAbility(saId, sid) == ERR_FAILED) {
80         RPC_LOG_ERROR("AddSystemAbility failed");
81         return ERR_FAILED;
82     }
83 
84     const char *name = "16";
85     if (RegisterRemoteProxy(name, strlen(name), saId) != ERR_NONE) {
86         RPC_LOG_ERROR("RegisterRemoteProxy failed");
87         return ERR_FAILED;
88     }
89 
90     return ERR_NONE;
91 }
92 
GetRemoteSystemAbility(IpcIo * data,SvcIdentity * sid)93 int32_t GetRemoteSystemAbility(IpcIo *data, SvcIdentity *sid)
94 {
95     int32_t saId;
96     ReadInt32(data, &saId);
97     size_t len;
98     const char *deviceId = (const char *)ReadString(data, &len);
99 
100     const char *name = "16";
101     uint32_t nameLen = 2;
102     uint32_t idLen = (uint32_t)strlen(deviceId);
103     RPC_LOG_INFO("GetRemoteSystemAbility start");
104 
105     int32_t ret = MakeRemoteBinder(name, nameLen, deviceId, idLen, (uintptr_t)saId, 0, (void *)sid);
106     if (ret != ERR_NONE) {
107         RPC_LOG_ERROR("MakeRemoteBinder failed");
108     }
109     RPC_LOG_INFO("GetRemoteSystemAbility handle=%d, cookie=%u", sid->handle, sid->cookie);
110 
111     return ret;
112 }
113 
RemoteRequest(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)114 int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
115 {
116     int32_t result = ERR_NONE;
117     RPC_LOG_INFO("OnRemoteRequest called.... code = %u", code);
118     switch (code) {
119         case GET_SYSTEM_ABILITY_TRANSACTION: {
120             int32_t saId;
121             ReadInt32(data, &saId);
122             SvcIdentity sid;
123             result = GetSystemAbility(saId, "", &sid);
124             WriteRemoteObject(reply, &sid);
125             break;
126         }
127         case ADD_SYSTEM_ABILITY_TRANSACTION: {
128             int32_t saId;
129             ReadInt32(data, &saId);
130             SvcIdentity *sid = (SvcIdentity *)malloc(sizeof(SvcIdentity));
131             if (sid == NULL) {
132                 result = ERR_FAILED;
133                 break;
134             }
135             ReadRemoteObject(data, sid);
136             result = AddSystemAbility(saId, sid);
137             break;
138         }
139         case GET_REMOTE_SYSTEM_ABILITY_TRANSACTION: {
140             SvcIdentity sid;
141             result = GetRemoteSystemAbility(data, &sid);
142             WriteRemoteObject(reply, &sid);
143             break;
144         }
145         case ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION: {
146             int32_t saId;
147             ReadInt32(data, &saId);
148             SvcIdentity *sid = (SvcIdentity *)malloc(sizeof(SvcIdentity));
149             if (sid == NULL) {
150                 result = ERR_FAILED;
151                 break;
152             }
153             ReadRemoteObject(data, sid);
154             result = AddRemoteSystemAbility(saId, sid);
155             break;
156         }
157         default:
158             RPC_LOG_ERROR("unknown code %d", code);
159             break;
160     }
161     return result;
162 }
163 
main(int argc,char * argv[])164 int main(int argc, char *argv[])
165 {
166     (void)argc;
167     (void)argv;
168     RPC_LOG_INFO("Enter System Ability Manager .... ");
169 
170     g_saList = (UTILS_DL_LIST *)calloc(1, sizeof(UTILS_DL_LIST));
171     UtilsListInit(g_saList);
172 
173     IpcObjectStub objectStub = {
174         .func = RemoteRequest,
175         .isRemote = false
176     };
177 
178     SvcIdentity target = {
179         .handle = 0,
180         .cookie = (uintptr_t)&objectStub
181     };
182 
183     if (SetContextObject(target) != ERR_NONE) {
184         RPC_LOG_ERROR("SAMGR register samgr failed");
185         return -1;
186     }
187 
188     StartDBinderService();
189     RPC_LOG_INFO("StartDBinderService finished");
190 
191     JoinWorkThread();
192     return -1;
193 }