1 /*
2  * Copyright (C) 2015 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 package com.android.car;
18 
19 
20 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.DUMP_INFO;
21 
22 import android.car.builtin.os.ServiceManagerHelper;
23 import android.car.builtin.os.SystemPropertiesHelper;
24 import android.car.builtin.os.TraceHelper;
25 import android.car.builtin.util.EventLogHelper;
26 import android.car.builtin.util.Slogf;
27 import android.car.builtin.util.TimingsTraceLog;
28 import android.content.Intent;
29 import android.os.IBinder;
30 import android.os.Process;
31 
32 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport;
33 import com.android.car.internal.ProxiedService;
34 import com.android.car.systeminterface.SystemInterface;
35 import com.android.internal.annotations.Keep;
36 
37 import java.io.FileDescriptor;
38 import java.io.PrintWriter;
39 
40 /** Implementation of CarService */
41 @Keep
42 public class CarServiceImpl extends ProxiedService {
43     public static final String CAR_SERVICE_INIT_TIMING_TAG = "CAR.InitTiming";
44     public static final int CAR_SERVICE_INIT_TIMING_MIN_DURATION_MS = 5;
45 
46     private ICarImpl mICarImpl;
47     private VehicleStub mVehicle;
48 
49     private String mVehicleInterfaceName;
50 
51     private final VehicleDeathRecipient mVehicleDeathRecipient = new VehicleDeathRecipient();
52 
53     @Override
onCreate()54     public void onCreate() {
55         TimingsTraceLog initTiming = new TimingsTraceLog(CAR_SERVICE_INIT_TIMING_TAG,
56                 TraceHelper.TRACE_TAG_CAR_SERVICE, CAR_SERVICE_INIT_TIMING_MIN_DURATION_MS);
57         initTiming.traceBegin("CarService.onCreate");
58 
59         initTiming.traceBegin("getVehicle");
60         mVehicle = VehicleStub.newVehicleStub();
61         initTiming.traceEnd(); // "getVehicle"
62 
63         EventLogHelper.writeCarServiceCreate(/* hasVhal= */ mVehicle.isValid());
64 
65         mVehicleInterfaceName = mVehicle.getInterfaceDescriptor();
66 
67         Slogf.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName);
68         EventLogHelper.writeCarServiceConnected(mVehicleInterfaceName);
69 
70         mICarImpl = new ICarImpl.Builder()
71                 .setServiceContext(this)
72                 .setBuiltInContext(getBuiltinPackageContext())
73                 .setVehicle(mVehicle)
74                 .setSystemInterface(
75                         SystemInterface.Builder.defaultSystemInterface(this).build())
76                 .setVehicleInterfaceName(mVehicleInterfaceName)
77                 .build();
78         mICarImpl.init();
79 
80         mVehicle.linkToDeath(mVehicleDeathRecipient);
81 
82         ServiceManagerHelper.addService("car_service", mICarImpl);
83         SystemPropertiesHelper.set("boot.car_service_created", "1");
84 
85         super.onCreate();
86 
87         initTiming.traceEnd(); // "CarService.onCreate"
88     }
89 
90     // onDestroy is best-effort and might not get called on shutdown/reboot. As such it is not
91     // suitable for permanently saving state or other need-to-happen operation. If you have a
92     // cleanup task that you want to make sure happens on shutdown/reboot, see OnShutdownReboot.
93     @Override
onDestroy()94     public void onDestroy() {
95         EventLogHelper.writeCarServiceDestroy(/* hasVhal= */ mVehicle.isValid());
96         Slogf.i(CarLog.TAG_SERVICE, "Service onDestroy");
97         mICarImpl.release();
98 
99         mVehicle.unlinkToDeath(mVehicleDeathRecipient);
100         mVehicle = null;
101 
102         super.onDestroy();
103     }
104 
105     @Override
onStartCommand(Intent intent, int flags, int startId)106     public int onStartCommand(Intent intent, int flags, int startId) {
107         // keep it alive.
108         return START_STICKY;
109     }
110 
111     @Override
onBind(Intent intent)112     public IBinder onBind(Intent intent) {
113         return mICarImpl;
114     }
115 
116     @Override
117     @ExcludeFromCodeCoverageGeneratedReport(reason = DUMP_INFO)
dump(FileDescriptor fd, PrintWriter writer, String[] args)118     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
119         // historically, the way to get a dumpsys from CarService has been to use
120         // "dumpsys activity service com.android.car/.CarService" - leaving this
121         // as a forward to car_service makes the previously well-known command still work
122         mICarImpl.dump(fd, writer, args);
123     }
124 
125     private static class VehicleDeathRecipient implements IVehicleDeathRecipient {
126 
127         @Override
serviceDied(long cookie)128         public void serviceDied(long cookie) {
129             EventLogHelper.writeCarServiceVhalDied(cookie);
130             Slogf.wtf(CarLog.TAG_SERVICE, "***Vehicle HAL died. Car service will restart***");
131             Process.killProcess(Process.myPid());
132         }
133 
134         @Override
binderDied()135         public void binderDied() {
136             EventLogHelper.writeCarServiceVhalDied(/*cookie=*/ 0);
137             Slogf.wtf(CarLog.TAG_SERVICE, "***Vehicle HAL died. Car service will restart***");
138             Process.killProcess(Process.myPid());
139         }
140     }
141 }
142