rpc_samgr.c 5.3 KB
Newer Older
Y
yangguangzhao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <string.h>

#include "rpc_log.h"
#include "rpc_errno.h"
#include "ipc_skeleton.h"
#include "serializer.h"
#include "utils_list.h"
#include "dbinder_service.h"

typedef struct {
    UTILS_DL_LIST list;
    int32_t saId;
    SvcIdentity *sid;
} SvcInfo;

static UTILS_DL_LIST *g_saList = NULL;

enum {
    ADD_SYSTEM_ABILITY_TRANSACTION = 2,
    GET_REMOTE_SYSTEM_ABILITY_TRANSACTION = 3,
    ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION = 4,
};

int32_t AddSystemAbility(int32_t saId, SvcIdentity *sid)
{
    RPC_LOG_INFO("AddSystemAbility called.... handle = %d", sid->handle);
    RPC_LOG_INFO("AddSystemAbility called.... cookie = %u", sid->cookie);
    if (g_saList == NULL) {
        return ERR_FAILED;
    }

    SvcInfo* node = (SvcInfo *)calloc(1, sizeof(SvcInfo));
    if (node == NULL) {
        RPC_LOG_ERROR("AddSystemAbility node calloc failed");
        return ERR_FAILED;
    }
    node->saId = saId;
    node->sid = sid;
    UtilsListAdd(g_saList, &node->list);
    return ERR_NONE;
}

int32_t GetSystemAbility(int32_t saId, const char* deviceId, SvcIdentity *sid)
{
    SvcInfo* node = NULL;
    SvcInfo* next = NULL;
    UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, g_saList, SvcInfo, list)
    {
        if (node->saId == saId) {
            sid->handle = node->sid->handle;
            sid->token = node->sid->token;
            sid->cookie = node->sid->cookie;
            RPC_LOG_INFO("find sa, said = %d, handle = %d, cookie = %u", saId, sid->handle, sid->cookie);
            return ERR_NONE;
        }
    }
    return ERR_FAILED;
}

int32_t AddRemoteSystemAbility(int32_t saId, SvcIdentity *sid)
{
    if (AddSystemAbility(saId, sid) == ERR_FAILED) {
        RPC_LOG_ERROR("AddSystemAbility failed");
        return ERR_FAILED;
    }

    const char *name = "16";
    if (RegisterRemoteProxy(name, strlen(name), saId) != ERR_NONE) {
        RPC_LOG_ERROR("RegisterRemoteProxy failed");
        return ERR_FAILED;
    }

    return ERR_NONE;
}

int32_t GetRemoteSystemAbility(IpcIo *data, SvcIdentity *sid)
{
    int32_t saId;
    ReadInt32(data, &saId);
    size_t len;
    const char *deviceId = (const char *)ReadString(data, &len);

    const char *name = "16";
    uint32_t idLen = (uint32_t)strlen(deviceId);
    RPC_LOG_INFO("GetRemoteSystemAbility start");

    int32_t ret = MakeRemoteBinder(name, 2, deviceId, idLen, (uintptr_t)saId, 0, (void *)sid);
    if (ret != ERR_NONE) {
        RPC_LOG_ERROR("MakeRemoteBinder failed");
    }
    RPC_LOG_INFO("GetRemoteSystemAbility handle=%d, cookie=%u", sid->handle, sid->cookie);

    return ret;
}

int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
{
    int32_t result = ERR_NONE;
    RPC_LOG_INFO("OnRemoteRequest called.... code = %d", code);
    switch (code) {
        case GET_SYSTEM_ABILITY_TRANSACTION: {
            int32_t saId;
            ReadInt32(data, &saId);
            SvcIdentity sid;
            result = GetSystemAbility(saId, "", &sid);
            WriteRemoteObject(reply, &sid);
            break;
        }
        case ADD_SYSTEM_ABILITY_TRANSACTION: {
            int32_t saId;
            ReadInt32(data, &saId);
            SvcIdentity *sid = (SvcIdentity *)malloc(sizeof(SvcIdentity));
            if (sid == NULL) {
                result = ERR_FAILED;
                break;
            }
            ReadRemoteObject(data, sid);
            result = AddSystemAbility(saId, sid);
            break;
        }
        case GET_REMOTE_SYSTEM_ABILITY_TRANSACTION: {
            SvcIdentity sid;
            result = GetRemoteSystemAbility(data, &sid);
            WriteRemoteObject(reply, &sid);
            break;
        }
        case ADD_REMOTE_SYSTEM_ABILITY_TRANSACTION: {
            int32_t saId;
            ReadInt32(data, &saId);
            SvcIdentity *sid = (SvcIdentity *)malloc(sizeof(SvcIdentity));
            if (sid == NULL) {
                result = ERR_FAILED;
                break;
            }
            ReadRemoteObject(data, sid);
            result = AddRemoteSystemAbility(saId, sid);
            break;
        }
        default:
            RPC_LOG_ERROR("unknown code %d", code);
            break;
    }
    return result;
}

int main(int argc, char *argv[])
{
    RPC_LOG_INFO("Enter System Ability Manager .... ");

    g_saList = (UTILS_DL_LIST *)calloc(1, sizeof(UTILS_DL_LIST));
    UtilsListInit(g_saList);

    IpcObjectStub objectStub = {
        .func = RemoteRequest,
        .isRemote = false
    };

    SvcIdentity target = {
        .handle = 0,
        .cookie = (uintptr_t)&objectStub
    };

    if (SetContextObject(target) != ERR_NONE) {
        RPC_LOG_ERROR("SAMGR register samgr failed");
        return -1;
    }

    StartDBinderService();
    RPC_LOG_INFO("StartDBinderService finished");

    JoinWorkThread();
    return -1;
}