blob: 1ac2f6e039b75ed9461831ab04790903a5512139 [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;
Kangping Dong1cec48f2023-08-21 18:48:14 +080031import com.android.server.thread.ThreadNetworkService;
paulhu802ab972021-12-14 01:30:22 +000032
Aaron Huang96011892020-06-27 07:18:23 +080033/**
34 * Connectivity service initializer for core networking. This is called by system server to create
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090035 * a new instance of connectivity services.
Aaron Huang96011892020-06-27 07:18:23 +080036 */
37public final class ConnectivityServiceInitializer extends SystemService {
38 private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName();
Tyler Wearb37f5512021-10-01 13:22:00 -070039 private final ConnectivityNativeService mConnectivityNative;
Aaron Huang96011892020-06-27 07:18:23 +080040 private final ConnectivityService mConnectivity;
Aaron Huang2e778ee2022-01-06 19:30:43 +080041 private final IpSecService mIpSecService;
paulhu802ab972021-12-14 01:30:22 +000042 private final NsdService mNsdService;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090043 private final NearbyService mNearbyService;
Xiao Ma0a171c02022-01-23 16:14:51 +000044 private final EthernetServiceImpl mEthernetServiceImpl;
Kangping Dong1cec48f2023-08-21 18:48:14 +080045 private final ThreadNetworkService mThreadNetworkService;
Aaron Huang96011892020-06-27 07:18:23 +080046
47 public ConnectivityServiceInitializer(Context context) {
48 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000049 // Load JNI libraries used by ConnectivityService and its dependencies
50 System.loadLibrary("service-connectivity");
Xiao Ma0a171c02022-01-23 16:14:51 +000051 mEthernetServiceImpl = createEthernetService(context);
junyulaie7c7d2a2021-01-26 15:29:15 +080052 mConnectivity = new ConnectivityService(context);
Aaron Huang2e778ee2022-01-06 19:30:43 +080053 mIpSecService = createIpSecService(context);
Tyler Wearb37f5512021-10-01 13:22:00 -070054 mConnectivityNative = createConnectivityNativeService(context);
paulhu802ab972021-12-14 01:30:22 +000055 mNsdService = createNsdService(context);
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090056 mNearbyService = createNearbyService(context);
Kangping Dong1cec48f2023-08-21 18:48:14 +080057 mThreadNetworkService = createThreadNetworkService(context);
Aaron Huang96011892020-06-27 07:18:23 +080058 }
59
60 @Override
61 public void onStart() {
Xiao Ma799d0002022-03-16 03:25:25 +000062 if (mEthernetServiceImpl != null) {
Xiao Ma0a171c02022-01-23 16:14:51 +000063 Log.i(TAG, "Registering " + Context.ETHERNET_SERVICE);
64 publishBinderService(Context.ETHERNET_SERVICE, mEthernetServiceImpl,
65 /* allowIsolated= */ false);
66 }
67
Aaron Huang96011892020-06-27 07:18:23 +080068 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
69 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080070 /* allowIsolated= */ false);
Aaron Huang2e778ee2022-01-06 19:30:43 +080071
72 if (mIpSecService != null) {
73 Log.i(TAG, "Registering " + Context.IPSEC_SERVICE);
74 publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false);
75 }
76
Tyler Wearb37f5512021-10-01 13:22:00 -070077 if (mConnectivityNative != null) {
78 Log.i(TAG, "Registering " + ConnectivityNativeService.SERVICE_NAME);
79 publishBinderService(ConnectivityNativeService.SERVICE_NAME, mConnectivityNative,
80 /* allowIsolated= */ false);
81 }
82
paulhu802ab972021-12-14 01:30:22 +000083 if (mNsdService != null) {
84 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
85 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
86 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090087
88 if (mNearbyService != null) {
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090089 Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE);
90 publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService,
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090091 /* allowIsolated= */ false);
92 }
Xiao Ma0a171c02022-01-23 16:14:51 +000093
Kangping Dong1cec48f2023-08-21 18:48:14 +080094 if (mThreadNetworkService != null) {
95 Log.i(TAG, "Registering " + ThreadNetworkManager.SERVICE_NAME);
96 publishBinderService(ThreadNetworkManager.SERVICE_NAME, mThreadNetworkService,
97 /* allowIsolated= */ false);
98 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090099 }
100
101 @Override
102 public void onBootPhase(int phase) {
103 if (mNearbyService != null) {
104 mNearbyService.onBootPhase(phase);
105 }
Xiao Ma0a171c02022-01-23 16:14:51 +0000106
107 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY && mEthernetServiceImpl != null) {
108 mEthernetServiceImpl.start();
109 }
Kangping Dong1cec48f2023-08-21 18:48:14 +0800110
111 if (mThreadNetworkService != null) {
112 mThreadNetworkService.onBootPhase(phase);
113 }
paulhu802ab972021-12-14 01:30:22 +0000114 }
115
Aaron Huang2e778ee2022-01-06 19:30:43 +0800116 /**
117 * Return IpSecService instance, or null if current SDK is lower than T.
118 */
119 private IpSecService createIpSecService(final Context context) {
120 if (!SdkLevel.isAtLeastT()) return null;
121
122 return new IpSecService(context);
123 }
124
Tyler Wearb37f5512021-10-01 13:22:00 -0700125 /**
126 * Return ConnectivityNativeService instance, or null if current SDK is lower than T.
127 */
128 private ConnectivityNativeService createConnectivityNativeService(final Context context) {
129 if (!SdkLevel.isAtLeastT()) return null;
130 try {
131 return new ConnectivityNativeService(context);
132 } catch (UnsupportedOperationException e) {
133 Log.d(TAG, "Unable to get ConnectivityNative service", e);
134 return null;
135 }
136 }
137
paulhu802ab972021-12-14 01:30:22 +0000138 /** Return NsdService instance or null if current SDK is lower than T */
139 private NsdService createNsdService(final Context context) {
140 if (!SdkLevel.isAtLeastT()) return null;
paulhu1b35e822022-04-08 14:48:41 +0800141
142 return NsdService.create(context);
Aaron Huang96011892020-06-27 07:18:23 +0800143 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900144
145 /** Return Nearby service instance or null if current SDK is lower than T */
146 private NearbyService createNearbyService(final Context context) {
147 if (!SdkLevel.isAtLeastT()) return null;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +0900148 try {
149 return new NearbyService(context);
150 } catch (UnsupportedOperationException e) {
151 // Nearby is not yet supported in all branches
152 // TODO: remove catch clause when it is available.
153 Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE);
154 return null;
155 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900156 }
Xiao Ma0a171c02022-01-23 16:14:51 +0000157
158 /**
159 * Return EthernetServiceImpl instance or null if current SDK is lower than T or Ethernet
160 * service isn't necessary.
161 */
162 private EthernetServiceImpl createEthernetService(final Context context) {
163 if (!SdkLevel.isAtLeastT() || !mConnectivity.deviceSupportsEthernet(context)) {
164 return null;
165 }
166 return EthernetService.create(context);
167 }
Kangping Dong1cec48f2023-08-21 18:48:14 +0800168
169 /**
170 * Returns Thread network service instance if supported.
171 * Thread is supported if all of below are satisfied:
172 * 1. the FEATURE_THREAD_NETWORK is available
173 * 2. the SDK level is V+, or SDK level is U and the device is a TV
174 */
175 @Nullable
176 private ThreadNetworkService createThreadNetworkService(final Context context) {
177 final PackageManager pm = context.getPackageManager();
178 if (!pm.hasSystemFeature(ThreadNetworkManager.FEATURE_NAME)) {
179 return null;
180 }
181 if (!SdkLevel.isAtLeastU()) {
182 return null;
183 }
184 if (!SdkLevel.isAtLeastV() && !pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
185 return null;
186 }
187 return new ThreadNetworkService(context);
188 }
Aaron Huang96011892020-06-27 07:18:23 +0800189}