blob: 003ec8cc89a13264af8ef101ae808223246a9265 [file] [log] [blame]
Aaron Huang96011892020-06-27 07:18:23 +08001/*
2 * Copyright (C) 2020 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
17package com.android.server;
18
Kangping Dong1cec48f2023-08-21 18:48:14 +080019import android.annotation.Nullable;
Aaron Huang96011892020-06-27 07:18:23 +080020import android.content.Context;
Kangping Dong1cec48f2023-08-21 18:48:14 +080021import android.content.pm.PackageManager;
22import android.net.thread.ThreadNetworkManager;
Aaron Huang96011892020-06-27 07:18:23 +080023import android.util.Log;
24
paulhu802ab972021-12-14 01:30:22 +000025import com.android.modules.utils.build.SdkLevel;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090026import com.android.networkstack.apishim.ConstantsShim;
Tyler Wearb37f5512021-10-01 13:22:00 -070027import com.android.server.connectivity.ConnectivityNativeService;
Xiao Ma0a171c02022-01-23 16:14:51 +000028import com.android.server.ethernet.EthernetService;
29import com.android.server.ethernet.EthernetServiceImpl;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090030import com.android.server.nearby.NearbyService;
Igor Zaslavskyec344f42023-08-08 04:28:45 +000031import com.android.server.remoteauth.RemoteAuthService;
Kangping Dong1cec48f2023-08-21 18:48:14 +080032import com.android.server.thread.ThreadNetworkService;
paulhu802ab972021-12-14 01:30:22 +000033
Aaron Huang96011892020-06-27 07:18:23 +080034/**
35 * Connectivity service initializer for core networking. This is called by system server to create
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090036 * a new instance of connectivity services.
Aaron Huang96011892020-06-27 07:18:23 +080037 */
38public final class ConnectivityServiceInitializer extends SystemService {
39 private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName();
Tyler Wearb37f5512021-10-01 13:22:00 -070040 private final ConnectivityNativeService mConnectivityNative;
Aaron Huang96011892020-06-27 07:18:23 +080041 private final ConnectivityService mConnectivity;
Aaron Huang2e778ee2022-01-06 19:30:43 +080042 private final IpSecService mIpSecService;
paulhu802ab972021-12-14 01:30:22 +000043 private final NsdService mNsdService;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090044 private final NearbyService mNearbyService;
Xiao Ma0a171c02022-01-23 16:14:51 +000045 private final EthernetServiceImpl mEthernetServiceImpl;
Igor Zaslavskyec344f42023-08-08 04:28:45 +000046 private final RemoteAuthService mRemoteAuthService;
Kangping Dong1cec48f2023-08-21 18:48:14 +080047 private final ThreadNetworkService mThreadNetworkService;
Aaron Huang96011892020-06-27 07:18:23 +080048
49 public ConnectivityServiceInitializer(Context context) {
50 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000051 // Load JNI libraries used by ConnectivityService and its dependencies
52 System.loadLibrary("service-connectivity");
Xiao Ma0a171c02022-01-23 16:14:51 +000053 mEthernetServiceImpl = createEthernetService(context);
junyulaie7c7d2a2021-01-26 15:29:15 +080054 mConnectivity = new ConnectivityService(context);
Aaron Huang2e778ee2022-01-06 19:30:43 +080055 mIpSecService = createIpSecService(context);
Tyler Wearb37f5512021-10-01 13:22:00 -070056 mConnectivityNative = createConnectivityNativeService(context);
paulhu802ab972021-12-14 01:30:22 +000057 mNsdService = createNsdService(context);
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090058 mNearbyService = createNearbyService(context);
Igor Zaslavskyec344f42023-08-08 04:28:45 +000059 mRemoteAuthService = createRemoteAuthService(context);
Kangping Dong1cec48f2023-08-21 18:48:14 +080060 mThreadNetworkService = createThreadNetworkService(context);
Aaron Huang96011892020-06-27 07:18:23 +080061 }
62
63 @Override
64 public void onStart() {
Xiao Ma799d0002022-03-16 03:25:25 +000065 if (mEthernetServiceImpl != null) {
Xiao Ma0a171c02022-01-23 16:14:51 +000066 Log.i(TAG, "Registering " + Context.ETHERNET_SERVICE);
67 publishBinderService(Context.ETHERNET_SERVICE, mEthernetServiceImpl,
68 /* allowIsolated= */ false);
69 }
70
Aaron Huang96011892020-06-27 07:18:23 +080071 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
72 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080073 /* allowIsolated= */ false);
Aaron Huang2e778ee2022-01-06 19:30:43 +080074
75 if (mIpSecService != null) {
76 Log.i(TAG, "Registering " + Context.IPSEC_SERVICE);
77 publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false);
78 }
79
Tyler Wearb37f5512021-10-01 13:22:00 -070080 if (mConnectivityNative != null) {
81 Log.i(TAG, "Registering " + ConnectivityNativeService.SERVICE_NAME);
82 publishBinderService(ConnectivityNativeService.SERVICE_NAME, mConnectivityNative,
83 /* allowIsolated= */ false);
84 }
85
paulhu802ab972021-12-14 01:30:22 +000086 if (mNsdService != null) {
87 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
88 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
89 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090090
91 if (mNearbyService != null) {
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090092 Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE);
93 publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService,
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090094 /* allowIsolated= */ false);
95 }
Xiao Ma0a171c02022-01-23 16:14:51 +000096
Igor Zaslavskyec344f42023-08-08 04:28:45 +000097 if (mRemoteAuthService != null) {
Remi NGUYEN VANc41daa42023-08-14 15:34:20 +090098 Log.i(TAG, "Registering " + RemoteAuthService.SERVICE_NAME);
99 publishBinderService(RemoteAuthService.SERVICE_NAME, mRemoteAuthService,
Igor Zaslavskyec344f42023-08-08 04:28:45 +0000100 /* allowIsolated= */ false);
101 }
Kangping Dong1cec48f2023-08-21 18:48:14 +0800102
103 if (mThreadNetworkService != null) {
104 Log.i(TAG, "Registering " + ThreadNetworkManager.SERVICE_NAME);
105 publishBinderService(ThreadNetworkManager.SERVICE_NAME, mThreadNetworkService,
106 /* allowIsolated= */ false);
107 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900108 }
109
110 @Override
111 public void onBootPhase(int phase) {
112 if (mNearbyService != null) {
113 mNearbyService.onBootPhase(phase);
114 }
Xiao Ma0a171c02022-01-23 16:14:51 +0000115
116 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY && mEthernetServiceImpl != null) {
117 mEthernetServiceImpl.start();
118 }
Kangping Dong1cec48f2023-08-21 18:48:14 +0800119
120 if (mThreadNetworkService != null) {
121 mThreadNetworkService.onBootPhase(phase);
122 }
paulhu802ab972021-12-14 01:30:22 +0000123 }
124
Aaron Huang2e778ee2022-01-06 19:30:43 +0800125 /**
126 * Return IpSecService instance, or null if current SDK is lower than T.
127 */
128 private IpSecService createIpSecService(final Context context) {
129 if (!SdkLevel.isAtLeastT()) return null;
130
131 return new IpSecService(context);
132 }
133
Tyler Wearb37f5512021-10-01 13:22:00 -0700134 /**
135 * Return ConnectivityNativeService instance, or null if current SDK is lower than T.
136 */
137 private ConnectivityNativeService createConnectivityNativeService(final Context context) {
138 if (!SdkLevel.isAtLeastT()) return null;
139 try {
140 return new ConnectivityNativeService(context);
141 } catch (UnsupportedOperationException e) {
142 Log.d(TAG, "Unable to get ConnectivityNative service", e);
143 return null;
144 }
145 }
146
paulhu802ab972021-12-14 01:30:22 +0000147 /** Return NsdService instance or null if current SDK is lower than T */
148 private NsdService createNsdService(final Context context) {
149 if (!SdkLevel.isAtLeastT()) return null;
paulhu1b35e822022-04-08 14:48:41 +0800150
151 return NsdService.create(context);
Aaron Huang96011892020-06-27 07:18:23 +0800152 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900153
154 /** Return Nearby service instance or null if current SDK is lower than T */
155 private NearbyService createNearbyService(final Context context) {
156 if (!SdkLevel.isAtLeastT()) return null;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +0900157 try {
158 return new NearbyService(context);
159 } catch (UnsupportedOperationException e) {
160 // Nearby is not yet supported in all branches
161 // TODO: remove catch clause when it is available.
162 Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE);
163 return null;
164 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900165 }
Xiao Ma0a171c02022-01-23 16:14:51 +0000166
Igor Zaslavskyec344f42023-08-08 04:28:45 +0000167 /** Return RemoteAuth service instance */
168 private RemoteAuthService createRemoteAuthService(final Context context) {
169 if (!SdkLevel.isAtLeastV()) return null;
170 try {
171 return new RemoteAuthService(context);
172 } catch (UnsupportedOperationException e) {
173 // RemoteAuth is not yet supported in all branches
174 // TODO: remove catch clause when it is available.
Remi NGUYEN VANc41daa42023-08-14 15:34:20 +0900175 Log.i(TAG, "Skipping unsupported service " + RemoteAuthService.SERVICE_NAME);
Igor Zaslavskyec344f42023-08-08 04:28:45 +0000176 return null;
177 }
178 }
179
Xiao Ma0a171c02022-01-23 16:14:51 +0000180 /**
181 * Return EthernetServiceImpl instance or null if current SDK is lower than T or Ethernet
182 * service isn't necessary.
183 */
184 private EthernetServiceImpl createEthernetService(final Context context) {
185 if (!SdkLevel.isAtLeastT() || !mConnectivity.deviceSupportsEthernet(context)) {
186 return null;
187 }
188 return EthernetService.create(context);
189 }
Kangping Dong1cec48f2023-08-21 18:48:14 +0800190
191 /**
192 * Returns Thread network service instance if supported.
193 * Thread is supported if all of below are satisfied:
194 * 1. the FEATURE_THREAD_NETWORK is available
195 * 2. the SDK level is V+, or SDK level is U and the device is a TV
196 */
197 @Nullable
198 private ThreadNetworkService createThreadNetworkService(final Context context) {
199 final PackageManager pm = context.getPackageManager();
200 if (!pm.hasSystemFeature(ThreadNetworkManager.FEATURE_NAME)) {
201 return null;
202 }
203 if (!SdkLevel.isAtLeastU()) {
204 return null;
205 }
206 if (!SdkLevel.isAtLeastV() && !pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
207 return null;
208 }
209 return new ThreadNetworkService(context);
210 }
Aaron Huang96011892020-06-27 07:18:23 +0800211}