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 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 19 | import android.content.Context; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 20 | import android.util.Log; |
| 21 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 22 | import com.android.modules.utils.build.SdkLevel; |
Remi NGUYEN VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 23 | import com.android.networkstack.apishim.ConstantsShim; |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 24 | import com.android.server.connectivity.ConnectivityNativeService; |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 25 | import com.android.server.ethernet.EthernetService; |
| 26 | import com.android.server.ethernet.EthernetServiceImpl; |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 27 | import com.android.server.nearby.NearbyService; |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 28 | import com.android.server.remoteauth.RemoteAuthService; |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 29 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 30 | /** |
| 31 | * 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] | 32 | * a new instance of connectivity services. |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 33 | */ |
| 34 | public final class ConnectivityServiceInitializer extends SystemService { |
| 35 | private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName(); |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 36 | private final ConnectivityNativeService mConnectivityNative; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 37 | private final ConnectivityService mConnectivity; |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 38 | private final IpSecService mIpSecService; |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 39 | private final NsdService mNsdService; |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 40 | private final NearbyService mNearbyService; |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 41 | private final EthernetServiceImpl mEthernetServiceImpl; |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 42 | private final RemoteAuthService mRemoteAuthService; |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 43 | |
| 44 | public ConnectivityServiceInitializer(Context context) { |
| 45 | super(context); |
Remi NGUYEN VAN | e724f63 | 2021-01-08 01:19:44 +0000 | [diff] [blame] | 46 | // Load JNI libraries used by ConnectivityService and its dependencies |
| 47 | System.loadLibrary("service-connectivity"); |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 48 | mEthernetServiceImpl = createEthernetService(context); |
junyulai | e7c7d2a | 2021-01-26 15:29:15 +0800 | [diff] [blame] | 49 | mConnectivity = new ConnectivityService(context); |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 50 | mIpSecService = createIpSecService(context); |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 51 | mConnectivityNative = createConnectivityNativeService(context); |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 52 | mNsdService = createNsdService(context); |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 53 | mNearbyService = createNearbyService(context); |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 54 | mRemoteAuthService = createRemoteAuthService(context); |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void onStart() { |
Xiao Ma | 799d000 | 2022-03-16 03:25:25 +0000 | [diff] [blame] | 59 | if (mEthernetServiceImpl != null) { |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 60 | Log.i(TAG, "Registering " + Context.ETHERNET_SERVICE); |
| 61 | publishBinderService(Context.ETHERNET_SERVICE, mEthernetServiceImpl, |
| 62 | /* allowIsolated= */ false); |
| 63 | } |
| 64 | |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 65 | Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE); |
| 66 | publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity, |
Chiachang Wang | 96e1a0b | 2021-03-09 14:55:31 +0800 | [diff] [blame] | 67 | /* allowIsolated= */ false); |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 68 | |
| 69 | if (mIpSecService != null) { |
| 70 | Log.i(TAG, "Registering " + Context.IPSEC_SERVICE); |
| 71 | publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false); |
| 72 | } |
| 73 | |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 74 | if (mConnectivityNative != null) { |
| 75 | Log.i(TAG, "Registering " + ConnectivityNativeService.SERVICE_NAME); |
| 76 | publishBinderService(ConnectivityNativeService.SERVICE_NAME, mConnectivityNative, |
| 77 | /* allowIsolated= */ false); |
| 78 | } |
| 79 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 80 | if (mNsdService != null) { |
| 81 | Log.i(TAG, "Registering " + Context.NSD_SERVICE); |
| 82 | publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false); |
| 83 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 84 | |
| 85 | if (mNearbyService != null) { |
Remi NGUYEN VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 86 | Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE); |
| 87 | publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService, |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 88 | /* allowIsolated= */ false); |
| 89 | } |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 90 | |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 91 | if (mRemoteAuthService != null) { |
Remi NGUYEN VAN | c41daa4 | 2023-08-14 15:34:20 +0900 | [diff] [blame] | 92 | Log.i(TAG, "Registering " + RemoteAuthService.SERVICE_NAME); |
| 93 | publishBinderService(RemoteAuthService.SERVICE_NAME, mRemoteAuthService, |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 94 | /* allowIsolated= */ false); |
| 95 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void onBootPhase(int phase) { |
| 100 | if (mNearbyService != null) { |
| 101 | mNearbyService.onBootPhase(phase); |
| 102 | } |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 103 | |
| 104 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY && mEthernetServiceImpl != null) { |
| 105 | mEthernetServiceImpl.start(); |
| 106 | } |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Aaron Huang | 2e778ee | 2022-01-06 19:30:43 +0800 | [diff] [blame] | 109 | /** |
| 110 | * Return IpSecService instance, or null if current SDK is lower than T. |
| 111 | */ |
| 112 | private IpSecService createIpSecService(final Context context) { |
| 113 | if (!SdkLevel.isAtLeastT()) return null; |
| 114 | |
| 115 | return new IpSecService(context); |
| 116 | } |
| 117 | |
Tyler Wear | b37f551 | 2021-10-01 13:22:00 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Return ConnectivityNativeService instance, or null if current SDK is lower than T. |
| 120 | */ |
| 121 | private ConnectivityNativeService createConnectivityNativeService(final Context context) { |
| 122 | if (!SdkLevel.isAtLeastT()) return null; |
| 123 | try { |
| 124 | return new ConnectivityNativeService(context); |
| 125 | } catch (UnsupportedOperationException e) { |
| 126 | Log.d(TAG, "Unable to get ConnectivityNative service", e); |
| 127 | return null; |
| 128 | } |
| 129 | } |
| 130 | |
paulhu | 802ab97 | 2021-12-14 01:30:22 +0000 | [diff] [blame] | 131 | /** Return NsdService instance or null if current SDK is lower than T */ |
| 132 | private NsdService createNsdService(final Context context) { |
| 133 | if (!SdkLevel.isAtLeastT()) return null; |
paulhu | 1b35e82 | 2022-04-08 14:48:41 +0800 | [diff] [blame] | 134 | |
| 135 | return NsdService.create(context); |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 136 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 137 | |
| 138 | /** Return Nearby service instance or null if current SDK is lower than T */ |
| 139 | private NearbyService createNearbyService(final Context context) { |
| 140 | if (!SdkLevel.isAtLeastT()) return null; |
Remi NGUYEN VAN | 46d7ddb | 2022-03-04 15:59:51 +0900 | [diff] [blame] | 141 | try { |
| 142 | return new NearbyService(context); |
| 143 | } catch (UnsupportedOperationException e) { |
| 144 | // Nearby is not yet supported in all branches |
| 145 | // TODO: remove catch clause when it is available. |
| 146 | Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE); |
| 147 | return null; |
| 148 | } |
Remi NGUYEN VAN | 5906c8b | 2022-01-20 18:40:29 +0900 | [diff] [blame] | 149 | } |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 150 | |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 151 | /** Return RemoteAuth service instance */ |
| 152 | private RemoteAuthService createRemoteAuthService(final Context context) { |
| 153 | if (!SdkLevel.isAtLeastV()) return null; |
| 154 | try { |
| 155 | return new RemoteAuthService(context); |
| 156 | } catch (UnsupportedOperationException e) { |
| 157 | // RemoteAuth is not yet supported in all branches |
| 158 | // TODO: remove catch clause when it is available. |
Remi NGUYEN VAN | c41daa4 | 2023-08-14 15:34:20 +0900 | [diff] [blame] | 159 | Log.i(TAG, "Skipping unsupported service " + RemoteAuthService.SERVICE_NAME); |
Igor Zaslavsky | ec344f4 | 2023-08-08 04:28:45 +0000 | [diff] [blame] | 160 | return null; |
| 161 | } |
| 162 | } |
| 163 | |
Xiao Ma | 0a171c0 | 2022-01-23 16:14:51 +0000 | [diff] [blame] | 164 | /** |
| 165 | * Return EthernetServiceImpl instance or null if current SDK is lower than T or Ethernet |
| 166 | * service isn't necessary. |
| 167 | */ |
| 168 | private EthernetServiceImpl createEthernetService(final Context context) { |
| 169 | if (!SdkLevel.isAtLeastT() || !mConnectivity.deviceSupportsEthernet(context)) { |
| 170 | return null; |
| 171 | } |
| 172 | return EthernetService.create(context); |
| 173 | } |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 174 | } |