当前位置:   article > 正文

Android13 以太网和WIFI共存

以太网和wifi共存

一、引言

        在实际的项目开发中,特别用到以太网相关功能时,往往会涉及wifi和以太网同时打开的情况,android原生对于这块的兼容很少,只会有一个生效,会优先使用以太网,故此需要改变系统机制做兼容。

二、实现部分

        思路:其实我们要做的无非就是让底层认为两者共存时现在就是要使用wifi上网的就行,那么我们就得在网络通路上做文章,也就是要配置“网络共存路由表”,大体上关于上层网络涉及的包就是Connectivity与system目录下的netd两个包,我们可以从这两个包的源码开始查看。

        2.1、上层系统服务部分

1.ConnectivityService.java类的unneeded方法中过滤以太网和wifi的检测,使能共存

  1. +++ b/packages/modules/Connectivity/service/src/com/android/server/ConnectivityService.java
  2. @@ -322,11 +322,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
  3. private static final String TRAFFICCONTROLLER_ARG = "trafficcontroller";
  4. private static final boolean DBG = true;
  5. private static final boolean DDBG = Log.isLoggable(TAG, Log.DEBUG);
  6. private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE);
  7. private static final boolean LOGD_BLOCKED_NETWORKINFO = true;
  8. /**
  9. * Default URL to use for {@link #getCaptivePortalServerUrl()}. This should not be changed
  10. * by OEMs for configuration purposes, as this value is overridden by
  11. @@ -4601,6 +4610,21 @@ public class ConnectivityService extends IConnectivityManager.Stub
  12. private boolean unneeded(NetworkAgentInfo nai, UnneededFor reason) {
  13. ensureRunningOnConnectivityServiceThread();
  14. + // added by cgt ethernet and wifi coexist start
  15. + boolean needed = nai.isWIFI();
  16. + needed = nai.isETHERNET();
  17. + Log.d(TAG, "---> unneeded: needed=" + needed);
  18. + if (needed) {
  19. + return false;
  20. + }
  21. + // added by cgt ethernet and wifi coexist end
  22. if (!nai.everConnected || nai.isVPN() || nai.isInactive()
  23. || nai.getScore().getKeepConnectedReason() != NetworkScore.KEEP_CONNECTED_NONE) {
  24. return false;

2.NetworkAgentInfo.java类中添加判断当前网络是否是以太网或者wifi的方法

  1. +++ b/packages/modules/Connectivity/service/src/com/android/server/connectivity/NetworkAgentInfo.java
  2. @@ -943,6 +943,18 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo>, NetworkRa
  3. return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
  4. }
  5. + // added by cgt ethernet and wifi coexist start
  6. + /** Whether this network is a WIFI. */
  7. + public boolean isWIFI() {
  8. + return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
  9. + }
  10. +
  11. + /** Whether this network is a ETHERNET. */
  12. + public boolean isETHERNET() {
  13. + return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
  14. + }
  15. + // added by cgt ethernet and wifi coexist end
  16. +
  17. /**
  18. * Whether this network should propagate the capabilities from its underlying networks.
  19. * Currently only true for VPNs.

3.NetworkRanker.java类中配置wifi优先并且在mightBeat方法中过滤wifi和以太网 

  1. +++ b/packages/modules/Connectivity/service/src/com/android/server/connectivity/NetworkRanker.java
  2. @@ -46,10 +46,18 @@ import java.util.Collection;
  3. import java.util.List;
  4. import java.util.function.Predicate;
  5. +// added by cgt ethernet and wifi coexist start
  6. +import android.util.Log;
  7. +// added by cgt ethernet and wifi coexist end
  8. +
  9. /**
  10. * A class that knows how to find the best network matching a request out of a list of networks.
  11. */
  12. public class NetworkRanker {
  13. + // added by cgt ethernet and wifi coexist start
  14. + private static final String TAG = "NetworkRanker";
  15. + // added by cgt ethernet and wifi coexist end
  16. +
  17. // Historically the legacy ints have been 0~100 in principle (though the highest score in
  18. // AOSP has always been 90). This is relied on by VPNs that send a legacy score of 101.
  19. public static final int LEGACY_INT_MAX = 100;
  20. @@ -80,9 +88,25 @@ public class NetworkRanker {
  21. }
  22. // Transport preference order, if it comes down to that.
  23. + // added by cgt ethernet and wifi coexist start
  24. - private static final int[] PREFERRED_TRANSPORTS_ORDER = { TRANSPORT_ETHERNET, TRANSPORT_WIFI,
  25. + //private static final int[] PREFERRED_TRANSPORTS_ORDER = { TRANSPORT_ETHERNET, TRANSPORT_WIFI,
  26. + // TRANSPORT_BLUETOOTH, TRANSPORT_CELLULAR };
  27. + private static int[] PREFERRED_TRANSPORTS_ORDER = { TRANSPORT_ETHERNET, TRANSPORT_WIFI,
  28. + TRANSPORT_BLUETOOTH, TRANSPORT_CELLULAR };
  29. + private static int[] PREFERRED_TRANSPORTS_ORDER_COEXIST = { TRANSPORT_WIFI, TRANSPORT_ETHERNET,
  30. TRANSPORT_BLUETOOTH, TRANSPORT_CELLULAR };
  31. + static {
  32. + PREFERRED_TRANSPORTS_ORDER = PREFERRED_TRANSPORTS_ORDER_COEXIST;
  33. + }
  34. + // added by cgt ethernet and wifi coexist end
  35. +
  36. // Function used to partition a list into two working areas depending on whether they
  37. // satisfy a predicate. All items satisfying the predicate will be put in |positive|, all
  38. // items that don't will be put in |negative|.
  39. @@ -310,6 +334,20 @@ public class NetworkRanker {
  40. // is always better than no network.
  41. if (null == champion) return true;
  42. // If there is no champion, the offer can always beat.
  43. + // added by cgt ethernet and wifi coexist start
  44. + boolean needed = contestant.getCapsNoCopy().hasTransport(TRANSPORT_WIFI);
  45. + needed = contestant.getCapsNoCopy().hasTransport(TRANSPORT_ETHERNET);
  46. + Log.d(TAG, "---> mightBeat: needed=" + needed);
  47. + if (needed) {
  48. + return true;
  49. + }
  50. + // added by cgt ethernet and wifi coexist end
  51. // Otherwise rank them.
  52. final ArrayList<Scoreable> candidates = new ArrayList<>();
  53. candidates.add(champion);

        

        2.2、上层netd服务部分

1.RouteController.cpp类中的Init方法配置一条新的直通的路由信息取代原生的配置

  1. diff --git a/system/netd/server/RouteController.cpp b/system/netd/server/RouteController.cpp
  2. index d2af9a375a2..22377105e1f 100644
  3. --- a/system/netd/server/RouteController.cpp
  4. +++ b/system/netd/server/RouteController.cpp
  5. @@ -40,6 +40,10 @@
  6. #include "netid_client.h"
  7. #include "netutils/ifc.h"
  8. +// added by cgt ethernet and wifi coexist start
  9. +#include <android-base/properties.h>
  10. +// added by cgt ethernet and wifi coexist end
  11. +
  12. using android::base::StartsWith;
  13. using android::base::StringPrintf;
  14. using android::base::WriteStringToFile;
  15. @@ -773,6 +777,18 @@ int RouteController::configureDummyNetwork() {
  16. return 0;
  17. }
  18. +// added by cgt ethernet and wifi coexist start
  19. +// Add a new rule to look up the 'main' table, with the same selectors as the "default network"
  20. +// rule, but with a lower priority. We will never create routes in the main table; it should only be
  21. +// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
  22. +// This is necessary, for example, when adding a route through a directly-connected gateway: in
  23. +// order to add the route, there must already be a directly-connected route that covers the gateway.
  24. +[[nodiscard]] static int addDirectlyConnectedRule() {
  25. + return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
  26. + MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
  27. +}
  28. +// added by cgt ethernet and wifi coexist end
  29. +
  30. // Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
  31. // relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
  32. // behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
  33. @@ -1259,6 +1275,13 @@ int RouteController::Init(unsigned localNetId) {
  34. if (int ret = addLocalNetworkRules(localNetId)) {
  35. return ret;
  36. }
  37. + // added by cgt ethernet and wifi coexist start
  38. + if (int ret = addDirectlyConnectedRule()) {
  39. + return ret;
  40. + }
  41. + // added by cgt ethernet and wifi coexist end
  42. if (int ret = addUnreachableRule()) {
  43. return ret;
  44. }

2.RouteController.h类中定义一个使用的变量

  1. diff --git a/system/netd/server/RouteController.h b/system/netd/server/RouteController.h
  2. index ff41678d551..2604266995c 100644
  3. --- a/system/netd/server/RouteController.h
  4. +++ b/system/netd/server/RouteController.h
  5. @@ -82,6 +82,9 @@ constexpr int32_t RULE_PRIORITY_UID_DEFAULT_NETWORK = 29000;
  6. // the network, it will not work. That will potentially cause a user-visible error.
  7. constexpr int32_t RULE_PRIORITY_UID_DEFAULT_UNREACHABLE = 30000;
  8. constexpr int32_t RULE_PRIORITY_DEFAULT_NETWORK = 31000;
  9. +// added by cgt ethernet and wifi coexist start
  10. +constexpr int32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 9999;
  11. +// added by cgt ethernet and wifi coexist end
  12. constexpr int32_t RULE_PRIORITY_UNREACHABLE = 32000;
  13. // clang-format on

三、结论

        至此wifi以太网共存分享就完成了,其实都是项目促使我们去实现这些功能,每当有新需求时不要去抗拒,而是觉得自我又要得到提升,保持学习才能成长。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号