blob: 37df29d5de6a402238c007a7d83f5cb5f2b333b1 [file] [log] [blame]
keunyoungca515072015-07-10 12:21:47 -07001/*
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 */
16package com.android.car;
17
Pavel Maltsev0d07c762016-11-03 16:40:15 -070018import static android.os.SystemClock.elapsedRealtime;
19
20import android.annotation.Nullable;
keunyoungca515072015-07-10 12:21:47 -070021import android.app.Service;
keunyoungca515072015-07-10 12:21:47 -070022import android.content.Intent;
Yao Chene33f07e2016-07-26 12:02:51 -070023import android.content.pm.PackageManager;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070024import android.hardware.vehicle.V2_0.IVehicle;
Yao Chene33f07e2016-07-26 12:02:51 -070025import android.os.Binder;
keunyoungca515072015-07-10 12:21:47 -070026import android.os.IBinder;
Vitalii Tomkiv973d2a22016-04-13 17:05:38 -070027import android.os.SystemProperties;
keunyoungca515072015-07-10 12:21:47 -070028import android.util.Log;
29
Yao Chene33f07e2016-07-26 12:02:51 -070030import java.io.FileDescriptor;
31import java.io.PrintWriter;
32
keunyoungca515072015-07-10 12:21:47 -070033public class CarService extends Service {
34
Pavel Maltsev0d07c762016-11-03 16:40:15 -070035 /** Default vehicle HAL service name. */
36 private static final String VEHICLE_SERVICE_NAME = "Vehicle";
37
38 private static final long WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS = 10_000;
39
keunyoungca515072015-07-10 12:21:47 -070040 private ICarImpl mICarImpl;
41
42 @Override
43 public void onCreate() {
44 Log.i(CarLog.TAG_SERVICE, "Service onCreate");
Pavel Maltsev0d07c762016-11-03 16:40:15 -070045 IVehicle vehicle = getVehicle(WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS);
46 if (vehicle == null) {
47 throw new IllegalStateException("Vehicle HAL service is not available.");
48 }
49
50 mICarImpl = new ICarImpl(this, vehicle, SystemInterface.getDefault(this));
51 mICarImpl.init();
Vitalii Tomkiv973d2a22016-04-13 17:05:38 -070052 SystemProperties.set("boot.car_service_created", "1");
keunyoungca515072015-07-10 12:21:47 -070053 super.onCreate();
54 }
55
56 @Override
57 public void onDestroy() {
58 Log.i(CarLog.TAG_SERVICE, "Service onDestroy");
Pavel Maltsev0d07c762016-11-03 16:40:15 -070059 mICarImpl.release();
keunyoungca515072015-07-10 12:21:47 -070060 super.onDestroy();
61 }
62
63 @Override
64 public int onStartCommand(Intent intent, int flags, int startId) {
65 // keep it alive.
66 return START_STICKY;
67 }
68
69 @Override
70 public IBinder onBind(Intent intent) {
71 return mICarImpl;
72 }
73
74 @Override
75 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Yao Chene33f07e2016-07-26 12:02:51 -070076 if (checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
77 != PackageManager.PERMISSION_GRANTED) {
78 writer.println("Permission Denial: can't dump CarService from from pid="
79 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
80 + " without permission " + android.Manifest.permission.DUMP);
81 return;
82 }
83 if (args == null || args.length == 0) {
84 writer.println("*dump car service*");
Pavel Maltsev0d07c762016-11-03 16:40:15 -070085 mICarImpl.dump(writer);
Yao Chene33f07e2016-07-26 12:02:51 -070086 } else {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070087 mICarImpl.execShellCmd(args, writer);
Yao Chene33f07e2016-07-26 12:02:51 -070088 }
keunyoungca515072015-07-10 12:21:47 -070089 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -070090
91 @Nullable
92 private IVehicle getVehicle(long waitMilliseconds) {
93 IVehicle vehicle = getVehicle();
94 long start = elapsedRealtime();
95 while (vehicle == null && (start + waitMilliseconds) > elapsedRealtime()) {
96 try {
97 Thread.sleep(100);
98 } catch (InterruptedException e) {
99 throw new RuntimeException("Sleep was interrupted", e);
100 }
101
102 vehicle = getVehicle();
103 }
104 return vehicle;
105 }
106
107 @Nullable
108 private IVehicle getVehicle() {
109 return IVehicle.getService(VEHICLE_SERVICE_NAME);
110 }
Yao Chene33f07e2016-07-26 12:02:51 -0700111}