• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 #include <android-base/logging.h>
18 #include <android/binder_process.h>
19 #include <iface/iface.h>
20 
21 using ::android::sp;
22 
23 class MyFoo : public IFoo {
doubleNumber(int32_t in,int32_t * out)24     binder_status_t doubleNumber(int32_t in, int32_t* out) override {
25         *out = 2 * in;
26         LOG(INFO) << "doubleNumber (" << in << ") => " << *out;
27         return STATUS_OK;
28     }
29 
die()30     binder_status_t die() override {
31         LOG(FATAL) << "IFoo::die called!";
32         return STATUS_UNKNOWN_ERROR;
33     }
34 };
35 
service(const char * instance)36 int service(const char* instance) {
37     ABinderProcess_setThreadPoolMaxThreadCount(0);
38 
39     // Strong reference to MyFoo kept by service manager.
40     binder_status_t status = (new MyFoo)->addService(instance);
41 
42     if (status != STATUS_OK) {
43         LOG(FATAL) << "Could not register: " << status << " " << instance;
44     }
45 
46     ABinderProcess_joinThreadPool();
47 
48     return 1;  // should not return
49 }
50 
main()51 int main() {
52     if (fork() == 0) {
53         return service(IFoo::kInstanceNameToDieFor);
54     }
55 
56     return service(IFoo::kSomeInstanceName);
57 }
58