blob: 626c2eb68d2527d3f2ea8b8de9d401f0780831e5 [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
Aaron Huang96011892020-06-27 07:18:23 +080019import android.content.Context;
Aaron Huang96011892020-06-27 07:18:23 +080020import android.util.Log;
21
paulhu802ab972021-12-14 01:30:22 +000022import com.android.modules.utils.build.SdkLevel;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090023import com.android.networkstack.apishim.ConstantsShim;
Tyler Wearb37f5512021-10-01 13:22:00 -070024import com.android.server.connectivity.ConnectivityNativeService;
Xiao Ma0a171c02022-01-23 16:14:51 +000025import com.android.server.ethernet.EthernetService;
26import com.android.server.ethernet.EthernetServiceImpl;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090027import com.android.server.nearby.NearbyService;
paulhu802ab972021-12-14 01:30:22 +000028
Aaron Huang96011892020-06-27 07:18:23 +080029/**
30 * Connectivity service initializer for core networking. This is called by system server to create
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090031 * a new instance of connectivity services.
Aaron Huang96011892020-06-27 07:18:23 +080032 */
33public final class ConnectivityServiceInitializer extends SystemService {
34 private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName();
Tyler Wearb37f5512021-10-01 13:22:00 -070035 private final ConnectivityNativeService mConnectivityNative;
Aaron Huang96011892020-06-27 07:18:23 +080036 private final ConnectivityService mConnectivity;
Aaron Huang2e778ee2022-01-06 19:30:43 +080037 private final IpSecService mIpSecService;
paulhu802ab972021-12-14 01:30:22 +000038 private final NsdService mNsdService;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090039 private final NearbyService mNearbyService;
Xiao Ma0a171c02022-01-23 16:14:51 +000040 private final EthernetServiceImpl mEthernetServiceImpl;
Aaron Huang96011892020-06-27 07:18:23 +080041
42 public ConnectivityServiceInitializer(Context context) {
43 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000044 // Load JNI libraries used by ConnectivityService and its dependencies
45 System.loadLibrary("service-connectivity");
Xiao Ma0a171c02022-01-23 16:14:51 +000046 mEthernetServiceImpl = createEthernetService(context);
junyulaie7c7d2a2021-01-26 15:29:15 +080047 mConnectivity = new ConnectivityService(context);
Aaron Huang2e778ee2022-01-06 19:30:43 +080048 mIpSecService = createIpSecService(context);
Tyler Wearb37f5512021-10-01 13:22:00 -070049 mConnectivityNative = createConnectivityNativeService(context);
paulhu802ab972021-12-14 01:30:22 +000050 mNsdService = createNsdService(context);
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090051 mNearbyService = createNearbyService(context);
Aaron Huang96011892020-06-27 07:18:23 +080052 }
53
54 @Override
55 public void onStart() {
Xiao Ma799d0002022-03-16 03:25:25 +000056 if (mEthernetServiceImpl != null) {
Xiao Ma0a171c02022-01-23 16:14:51 +000057 Log.i(TAG, "Registering " + Context.ETHERNET_SERVICE);
58 publishBinderService(Context.ETHERNET_SERVICE, mEthernetServiceImpl,
59 /* allowIsolated= */ false);
60 }
61
Aaron Huang96011892020-06-27 07:18:23 +080062 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
63 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080064 /* allowIsolated= */ false);
Aaron Huang2e778ee2022-01-06 19:30:43 +080065
66 if (mIpSecService != null) {
67 Log.i(TAG, "Registering " + Context.IPSEC_SERVICE);
68 publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false);
69 }
70
Tyler Wearb37f5512021-10-01 13:22:00 -070071 if (mConnectivityNative != null) {
72 Log.i(TAG, "Registering " + ConnectivityNativeService.SERVICE_NAME);
73 publishBinderService(ConnectivityNativeService.SERVICE_NAME, mConnectivityNative,
74 /* allowIsolated= */ false);
75 }
76
paulhu802ab972021-12-14 01:30:22 +000077 if (mNsdService != null) {
78 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
79 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
80 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090081
82 if (mNearbyService != null) {
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090083 Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE);
84 publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService,
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090085 /* allowIsolated= */ false);
86 }
Xiao Ma0a171c02022-01-23 16:14:51 +000087
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090088 }
89
90 @Override
91 public void onBootPhase(int phase) {
92 if (mNearbyService != null) {
93 mNearbyService.onBootPhase(phase);
94 }
Xiao Ma0a171c02022-01-23 16:14:51 +000095
96 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY && mEthernetServiceImpl != null) {
97 mEthernetServiceImpl.start();
98 }
paulhu802ab972021-12-14 01:30:22 +000099 }
100
Aaron Huang2e778ee2022-01-06 19:30:43 +0800101 /**
102 * Return IpSecService instance, or null if current SDK is lower than T.
103 */
104 private IpSecService createIpSecService(final Context context) {
105 if (!SdkLevel.isAtLeastT()) return null;
106
107 return new IpSecService(context);
108 }
109
Tyler Wearb37f5512021-10-01 13:22:00 -0700110 /**
111 * Return ConnectivityNativeService instance, or null if current SDK is lower than T.
112 */
113 private ConnectivityNativeService createConnectivityNativeService(final Context context) {
114 if (!SdkLevel.isAtLeastT()) return null;
115 try {
116 return new ConnectivityNativeService(context);
117 } catch (UnsupportedOperationException e) {
118 Log.d(TAG, "Unable to get ConnectivityNative service", e);
119 return null;
120 }
121 }
122
paulhu802ab972021-12-14 01:30:22 +0000123 /** Return NsdService instance or null if current SDK is lower than T */
124 private NsdService createNsdService(final Context context) {
125 if (!SdkLevel.isAtLeastT()) return null;
paulhu1b35e822022-04-08 14:48:41 +0800126
127 return NsdService.create(context);
Aaron Huang96011892020-06-27 07:18:23 +0800128 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900129
130 /** Return Nearby service instance or null if current SDK is lower than T */
131 private NearbyService createNearbyService(final Context context) {
132 if (!SdkLevel.isAtLeastT()) return null;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +0900133 try {
134 return new NearbyService(context);
135 } catch (UnsupportedOperationException e) {
136 // Nearby is not yet supported in all branches
137 // TODO: remove catch clause when it is available.
138 Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE);
139 return null;
140 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900141 }
Xiao Ma0a171c02022-01-23 16:14:51 +0000142
143 /**
144 * Return EthernetServiceImpl instance or null if current SDK is lower than T or Ethernet
145 * service isn't necessary.
146 */
147 private EthernetServiceImpl createEthernetService(final Context context) {
148 if (!SdkLevel.isAtLeastT() || !mConnectivity.deviceSupportsEthernet(context)) {
149 return null;
150 }
151 return EthernetService.create(context);
152 }
Aaron Huang96011892020-06-27 07:18:23 +0800153}