blob: fa86f3965aae77079927a7dd25df399ddb32d521 [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;
Xiao Ma0a171c02022-01-23 16:14:51 +000024import com.android.server.ethernet.EthernetService;
25import com.android.server.ethernet.EthernetServiceImpl;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090026import com.android.server.nearby.NearbyService;
paulhu802ab972021-12-14 01:30:22 +000027
Aaron Huang96011892020-06-27 07:18:23 +080028/**
29 * Connectivity service initializer for core networking. This is called by system server to create
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090030 * a new instance of connectivity services.
Aaron Huang96011892020-06-27 07:18:23 +080031 */
32public final class ConnectivityServiceInitializer extends SystemService {
33 private static final String TAG = ConnectivityServiceInitializer.class.getSimpleName();
34 private final ConnectivityService mConnectivity;
Aaron Huang2e778ee2022-01-06 19:30:43 +080035 private final IpSecService mIpSecService;
paulhu802ab972021-12-14 01:30:22 +000036 private final NsdService mNsdService;
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090037 private final NearbyService mNearbyService;
Xiao Ma0a171c02022-01-23 16:14:51 +000038 private final EthernetServiceImpl mEthernetServiceImpl;
Aaron Huang96011892020-06-27 07:18:23 +080039
40 public ConnectivityServiceInitializer(Context context) {
41 super(context);
Remi NGUYEN VANe724f632021-01-08 01:19:44 +000042 // Load JNI libraries used by ConnectivityService and its dependencies
43 System.loadLibrary("service-connectivity");
Xiao Ma0a171c02022-01-23 16:14:51 +000044 mEthernetServiceImpl = createEthernetService(context);
junyulaie7c7d2a2021-01-26 15:29:15 +080045 mConnectivity = new ConnectivityService(context);
Aaron Huang2e778ee2022-01-06 19:30:43 +080046 mIpSecService = createIpSecService(context);
paulhu802ab972021-12-14 01:30:22 +000047 mNsdService = createNsdService(context);
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090048 mNearbyService = createNearbyService(context);
Aaron Huang96011892020-06-27 07:18:23 +080049 }
50
51 @Override
52 public void onStart() {
Xiao Ma799d0002022-03-16 03:25:25 +000053 if (mEthernetServiceImpl != null) {
Xiao Ma0a171c02022-01-23 16:14:51 +000054 Log.i(TAG, "Registering " + Context.ETHERNET_SERVICE);
55 publishBinderService(Context.ETHERNET_SERVICE, mEthernetServiceImpl,
56 /* allowIsolated= */ false);
57 }
58
Aaron Huang96011892020-06-27 07:18:23 +080059 Log.i(TAG, "Registering " + Context.CONNECTIVITY_SERVICE);
60 publishBinderService(Context.CONNECTIVITY_SERVICE, mConnectivity,
Chiachang Wang96e1a0b2021-03-09 14:55:31 +080061 /* allowIsolated= */ false);
Aaron Huang2e778ee2022-01-06 19:30:43 +080062
63 if (mIpSecService != null) {
64 Log.i(TAG, "Registering " + Context.IPSEC_SERVICE);
65 publishBinderService(Context.IPSEC_SERVICE, mIpSecService, /* allowIsolated= */ false);
66 }
67
paulhu802ab972021-12-14 01:30:22 +000068 if (mNsdService != null) {
69 Log.i(TAG, "Registering " + Context.NSD_SERVICE);
70 publishBinderService(Context.NSD_SERVICE, mNsdService, /* allowIsolated= */ false);
71 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090072
73 if (mNearbyService != null) {
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +090074 Log.i(TAG, "Registering " + ConstantsShim.NEARBY_SERVICE);
75 publishBinderService(ConstantsShim.NEARBY_SERVICE, mNearbyService,
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090076 /* allowIsolated= */ false);
77 }
Xiao Ma0a171c02022-01-23 16:14:51 +000078
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +090079 }
80
81 @Override
82 public void onBootPhase(int phase) {
83 if (mNearbyService != null) {
84 mNearbyService.onBootPhase(phase);
85 }
Xiao Ma0a171c02022-01-23 16:14:51 +000086
87 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY && mEthernetServiceImpl != null) {
88 mEthernetServiceImpl.start();
89 }
paulhu802ab972021-12-14 01:30:22 +000090 }
91
Aaron Huang2e778ee2022-01-06 19:30:43 +080092 /**
93 * Return IpSecService instance, or null if current SDK is lower than T.
94 */
95 private IpSecService createIpSecService(final Context context) {
96 if (!SdkLevel.isAtLeastT()) return null;
97
98 return new IpSecService(context);
99 }
100
paulhu802ab972021-12-14 01:30:22 +0000101 /** Return NsdService instance or null if current SDK is lower than T */
102 private NsdService createNsdService(final Context context) {
103 if (!SdkLevel.isAtLeastT()) return null;
104 try {
105 return NsdService.create(context);
106 } catch (InterruptedException e) {
107 Log.d(TAG, "Unable to get NSD service", e);
108 return null;
109 }
Aaron Huang96011892020-06-27 07:18:23 +0800110 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900111
112 /** Return Nearby service instance or null if current SDK is lower than T */
113 private NearbyService createNearbyService(final Context context) {
114 if (!SdkLevel.isAtLeastT()) return null;
Remi NGUYEN VAN46d7ddb2022-03-04 15:59:51 +0900115 try {
116 return new NearbyService(context);
117 } catch (UnsupportedOperationException e) {
118 // Nearby is not yet supported in all branches
119 // TODO: remove catch clause when it is available.
120 Log.i(TAG, "Skipping unsupported service " + ConstantsShim.NEARBY_SERVICE);
121 return null;
122 }
Remi NGUYEN VAN5906c8b2022-01-20 18:40:29 +0900123 }
Xiao Ma0a171c02022-01-23 16:14:51 +0000124
125 /**
126 * Return EthernetServiceImpl instance or null if current SDK is lower than T or Ethernet
127 * service isn't necessary.
128 */
129 private EthernetServiceImpl createEthernetService(final Context context) {
130 if (!SdkLevel.isAtLeastT() || !mConnectivity.deviceSupportsEthernet(context)) {
131 return null;
132 }
133 return EthernetService.create(context);
134 }
Aaron Huang96011892020-06-27 07:18:23 +0800135}