Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Kangping Dong | 1cec48f | 2023-08-21 18:48:14 +0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 20 | import android.content.Context; |
Kangping Dong | 1cec48f | 2023-08-21 18:48:14 +0800 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
| 22 | import android.net.thread.ThreadNetworkManager; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 25 | import com.android.modules.utils.build.SdkLevel; |
Remi NGUYEN VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 26 | import com.android.networkstack.apishim.ConstantsShim; |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 27 | import com.android.server.connectivity.ConnectivityNativeService; |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 28 | import com.android.server.ethernet.EthernetService; |
| 29 | import com.android.server.ethernet.EthernetServiceImpl; |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 30 | import com.android.server.nearby.NearbyService; |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 31 | import com.android.server.remoteauth.RemoteAuthService; |
Kangping Dong | 1cec48f | 2023-08-21 18:48:14 +0800 | [diff] [blame] | 32 | import com.android.server.thread.ThreadNetworkService; |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 33 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 34 | /** |
| 35 | * Connectivity service initializer for core networking. This is called by system server to create |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 36 | * a new instance of connectivity services. |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 37 | */ |
| 38 | public final class ConnectivityServiceInitializer extends SystemService { |
| 39 | private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName(); |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 40 | private final ConnectivityNativeService mConnectivityNative; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 41 | private final ConnectivityService mConnectivity; |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 42 | private final IpSecService mIpSecService; |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 43 | private final NsdService mNsdService; |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 44 | private final NearbyService mNearbyService; |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 45 | private final EthernetServiceImpl mEthernetServiceImpl; |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 46 | private final RemoteAuthService mRemoteAuthService; |
Kangping Dong | 1cec48f | 2023-08-21 18:48:14 +0800 | [diff] [blame] | 47 | private final ThreadNetworkService mThreadNetworkService; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 48 | |
| 49 | public ConnectivityServiceInitializer(Context context) { |
| 50 | super(context); |
Remi NGUYEN VAN | e724f63 | 2021-01-08 01:19:44 +0000 | [diff] [blame] | 51 | // Load JNI libraries used by ConnectivityService and its dependencies |
| 52 | System.loadLibrary("service-connectivity"); |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 53 | mEthernetServiceImpl = createEthernetService(context); |
junyulai | e7c7d2a | 2021-01-26 15:29:15 +0800 | [diff] [blame] | 54 | mConnectivity = new ConnectivityService(context); |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 55 | mIpSecService = createIpSecService(context); |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 56 | mConnectivityNative = createConnectivityNativeService(context); |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 57 | mNsdService = createNsdService(context); |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 58 | mNearbyService = createNearbyService(context); |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 59 | mRemoteAuthService = createRemoteAuthService(context); |
Kangping Dong | 1cec48f | 2023-08-21 18:48:14 +0800 | [diff] [blame] | 60 | mThreadNetworkService = createThreadNetworkService(context); |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void onStart() { |
Xiao Ma | 799d000 | 2022-03-16 03:25:25 +0000 | [diff] [blame] | 65 | if (mEthernetServiceImpl != null) { |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 66 | Log.i(TAG, "Registering " + Context.ETHERNET_SERVICE); |
| 67 | publishBinderService(Context.ETHERNET_SERVICE, mEthernetServiceImpl, |
| 68 | /* allowIsolated= */ false); |
| 69 | } |
| 70 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 71 | Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE); |
| 72 | publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity, |
Chiachang Wang | 96e1a0b | 2021-03-09 14:55:31 +0800 | [diff] [blame] | 73 | /* allowIsolated= */ false); |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 74 | |
| 75 | if (mIpSecService != null) { |
| 76 | Log.i(TAG, "Registering " + Context.IPSEC_SERVICE); |
| 77 | publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false); |
| 78 | } |
| 79 | |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 80 | if (mConnectivityNative != null) { |
| 81 | Log.i(TAG, "Registering " + ConnectivityNativeService.SERVICE_NAME); |
| 82 | publishBinderService(ConnectivityNativeService.SERVICE_NAME, mConnectivityNative, |
| 83 | /* allowIsolated= */ false); |
| 84 | } |
| 85 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 86 | if (mNsdService != null) { |
| 87 | Log.i(TAG, "Registering " + Context.NSD_SERVICE); |
| 88 | publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false); |
| 89 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 90 | |
| 91 | if (mNearbyService != null) { |
Remi NGUYEN VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 92 | Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE); |
| 93 | publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService, |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 94 | /* allowIsolated= */ false); |
| 95 | } |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 96 | |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 97 | if (mRemoteAuthService != null) { |
Remi NGUYEN VAN | c41daa4 | 2023-08-14 15:34:20 +0900 | [diff] [blame] | 98 | Log.i(TAG, "Registering " + RemoteAuthService.SERVICE_NAME); |
| 99 | publishBinderService(RemoteAuthService.SERVICE_NAME, mRemoteAuthService, |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 100 | /* allowIsolated= */ false); |
| 101 | } |
Kangping Dong | 1cec48f | 2023-08-21 18:48:14 +0800 | [diff] [blame] | 102 | |
| 103 | if (mThreadNetworkService != null) { |
| 104 | Log.i(TAG, "Registering " + ThreadNetworkManager.SERVICE_NAME); |
| 105 | publishBinderService(ThreadNetworkManager.SERVICE_NAME, mThreadNetworkService, |
| 106 | /* allowIsolated= */ false); |
| 107 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void onBootPhase(int phase) { |
| 112 | if (mNearbyService != null) { |
| 113 | mNearbyService.onBootPhase(phase); |
| 114 | } |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 115 | |
| 116 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY && mEthernetServiceImpl != null) { |
| 117 | mEthernetServiceImpl.start(); |
| 118 | } |
Kangping Dong | 1cec48f | 2023-08-21 18:48:14 +0800 | [diff] [blame] | 119 | |
| 120 | if (mThreadNetworkService != null) { |
| 121 | mThreadNetworkService.onBootPhase(phase); |
| 122 | } |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 125 | /** |
| 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 Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 134 | /** |
| 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 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 147 | /** 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; |
paulhu | 1b35e82 | 2022-04-08 14:48:41 +0800 | [diff] [blame] | 150 | |
| 151 | return NsdService.create(context); |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 152 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 153 | |
| 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 VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 157 | 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 VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 165 | } |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 166 | |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 167 | /** 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 VAN | c41daa4 | 2023-08-14 15:34:20 +0900 | [diff] [blame] | 175 | Log.i(TAG, "Skipping unsupported service " + RemoteAuthService.SERVICE_NAME); |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 176 | return null; |
| 177 | } |
| 178 | } |
| 179 | |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 180 | /** |
| 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 Dong | 1cec48f | 2023-08-21 18:48:14 +0800 | [diff] [blame] | 190 | |
| 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 Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 211 | } |