1 /**
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "RouterSvc.h"
17 
18 #include <android/binder_interface_utils.h>
19 #include <android/binder_manager.h>
20 #include <binder/IServiceManager.h>
21 
22 #include "PipeQuery.h"
23 #include "PipeRegistration.h"
24 #include "Registry.h"
25 
26 namespace android {
27 namespace automotive {
28 namespace computepipe {
29 namespace router {
30 namespace V1_0 {
31 namespace implementation {
32 
33 const static char kRouterName[] = "router";
34 
parseArgs(int argc,char ** argv)35 Error RouterSvc::parseArgs(int argc, char** argv) {
36     (void)argc;
37     (void)argv;
38     return OK;
39 }
40 
initSvc()41 Error RouterSvc::initSvc() {
42     mRegistry = std::make_shared<RouterRegistry>();
43     auto ret = initRegistrationEngine();
44     if (ret != OK) {
45         return ret;
46     }
47     ret = initQueryEngine();
48     if (ret != OK) {
49         return ret;
50     }
51     return OK;
52 }
53 
initRegistrationEngine()54 Error RouterSvc::initRegistrationEngine() {
55     mRegisterEngine = ndk::SharedRefBase::make<PipeRegistration>(mRegistry);
56     if (!mRegisterEngine) {
57         ALOGE("unable to allocate registration engine");
58         return NOMEM;
59     }
60     std::string name = std::string() + mRegisterEngine->getIfaceName() + "/" + kRouterName;
61     auto status = AServiceManager_addService(mRegisterEngine->asBinder().get(), name.c_str());
62     if (status != STATUS_OK) {
63         ALOGE("unable to add registration service %s", name.c_str());
64         return INTERNAL_ERR;
65     }
66     return OK;
67 }
68 
initQueryEngine()69 Error RouterSvc::initQueryEngine() {
70     mQueryEngine = ndk::SharedRefBase::make<PipeQuery>(mRegistry);
71     if (!mQueryEngine) {
72         ALOGE("unable to allocate query service");
73         return NOMEM;
74     }
75     std::string name = std::string() + mQueryEngine->getIfaceName() + "/" + kRouterName;
76     auto status = AServiceManager_addService(mQueryEngine->asBinder().get(), name.c_str());
77     if (status != STATUS_OK) {
78         ALOGE("unable to add query service %s", name.c_str());
79         return INTERNAL_ERR;
80     }
81     return OK;
82 }
83 }  // namespace implementation
84 }  // namespace V1_0
85 }  // namespace router
86 }  // namespace computepipe
87 }  // namespace automotive
88 }  // namespace android
89