当前位置:   article > 正文

Android 13 有线以太网静态ip保存逻辑梳理分析_解决rockchip平台android13系统以太网设置静态ip保存不了问题

解决rockchip平台android13系统以太网设置静态ip保存不了问题

源码环境:高通Android 13

这里特别说明,Android13中,ipconfig.txt配置文件目录有改变

以前:/data/misc/ethernet/ipconfig.txt
最新的有线网配置文件保存目录:
/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt

一、Android 版本影响
首先就是Android 11 与 12 的有线网络版本差距还是比较大的,源码目录也变了,之前目录frameworks/opt/net/ethernet/java/com/android/server/ethernet,现在改成了packages/modules/Connectivity/service-t/src/com/android/server/ethernet,所以如果之前只在11上面适配过,那么对于12来说,适配还是需要花费一点功夫,具体的差异在之后的部分会记录,但是12与13的有线网络差异就较小了,并且看起来,13的以太网接口以及逻辑比12来说更为完善。

二、配置以太网所需要的类
配置以太网所需要的java类大概有以下几个,其实这几个类从Android9开始就是以太网配置的主要java类了

(1)、frameworks/base/core/java/android/net/EthernetManager.java

EthernetManager.java此是上层管理以太网的类,我们常通过context.getSystemService(Context.ETHERNET_SERVICE)获得他的实例对象

(2)、packages/modules/Connectivity/framework/src/android/net/IpConfiguration.java

这个类主要就是用来配置IP状态的,包括动态和静态

(3)、packages/modules/Connectivity/framework/src/android/net/StaticIpConfiguration.java

这个类主要就是用来配置静态IP的,这个类之前也是在frameworks/base/core/java/android/net/路径下,12里面也移到了packages/modules/Connectivity/framework/src/android/net/下

三、有线以太网静态ip保存逻辑从源码逐步分析    

  1. 1、packages/modules/Connectivity/framework-t/src/android/net/EthernetManager.java
  2. public void setConfiguration(@NonNull String iface, @NonNull IpConfiguration config) {
  3. try {
  4. mService.setConfiguration(iface, config);//这里调用的是EthernetServiceImpl.java中的setConfiguration
  5. } catch (RemoteException e) {
  6. throw e.rethrowFromSystemServer();
  7. }
  8. }
  9. 2、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetServiceImpl.java
  10. /**
  11. * Set Ethernet configuration
  12. */
  13. @Override
  14. public void setConfiguration(String iface, IpConfiguration config) {
  15. throwIfEthernetNotStarted();
  16. PermissionUtils.enforceNetworkStackPermission(mContext);
  17. if (mTracker.isRestrictedInterface(iface)) {
  18. PermissionUtils.enforceRestrictedNetworkPermission(mContext, TAG);
  19. }
  20. // TODO: this does not check proxy settings, gateways, etc.
  21. // Fix this by making IpConfiguration a complete representation of static configuration.
  22. mTracker.updateIpConfiguration(iface, new IpConfiguration(config));//这里更新保存
  23. }
  24. 3、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetTracker.java
  25. EthernetTracker中主要做了以下两件事 :
  26. (1). 首先更新 ip config的,这个和静态ip相关;
  27. (2). 根据iface,调用addInterface创建interface
  28. void updateIpConfiguration(String iface, IpConfiguration ipConfiguration) {
  29. if (DBG) {
  30. Log.i(TAG, "updateIpConfiguration, iface: " + iface + ", cfg: " + ipConfiguration);
  31. }
  32. writeIpConfiguration(iface, ipConfiguration);
  33. mHandler.post(() -> {
  34. mFactory.updateInterface(iface, ipConfiguration, null, null);
  35. broadcastInterfaceStateChange(iface);
  36. });
  37. }
  38. private void writeIpConfiguration(@NonNull final String iface,
  39. @NonNull final IpConfiguration ipConfig) {
  40. mConfigStore.write(iface, ipConfig);//这里调用了EthernetConfigStore.java中的write
  41. mIpConfigurations.put(iface, ipConfig);
  42. }
  43. 4、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetConfigStore.java
  44. private static final String CONFIG_FILE = "ipconfig.txt";
  45. private static final String FILE_PATH = "/misc/ethernet/";
  46. private static final String APEX_IP_CONFIG_FILE_PATH = ApexEnvironment.getApexEnvironment(
  47. TETHERING_MODULE_NAME).getDeviceProtectedDataDir() + FILE_PATH;
  48. public void write(String iface, IpConfiguration config) {
  49. write(iface, config, APEX_IP_CONFIG_FILE_PATH + CONFIG_FILE);//注意:这里Android13调整了保存路径,全路径:/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt
  50. //Android 10版本保存路径为:/data/misc/ethernet/ipconfig.txt
  51. }
  52. void write(String iface, IpConfiguration config, String filepath) {
  53. boolean modified;
  54. synchronized (mSync) {
  55. if (config == null) {
  56. modified = mIpConfigurations.remove(iface) != null;
  57. } else {
  58. IpConfiguration oldConfig = mIpConfigurations.put(iface, config);
  59. modified = !config.equals(oldConfig);
  60. }
  61. if (modified) {
  62. mStore.writeIpConfigurations(filepath, mIpConfigurations);//这里调用的是IpConfigStore.java
  63. }
  64. }
  65. }
  66. 5、packages/modules/Connectivity/service-t/src/com/android/server/net/IpConfigStore.java
  67. /**
  68. * Write the IP configuration associated to the target networks to the destination path.
  69. */
  70. public void writeIpConfigurations(String filePath,
  71. ArrayMap<String, IpConfiguration> networks) {
  72. mWriter.write(filePath, out -> {
  73. out.writeInt(IPCONFIG_FILE_VERSION);
  74. for (int i = 0; i < networks.size(); i++) {
  75. writeConfig(out, networks.keyAt(i), networks.valueAt(i));
  76. }
  77. });
  78. }
  79. private static boolean writeConfig(DataOutputStream out, String configKey,
  80. IpConfiguration config) throws IOException {
  81. return writeConfig(out, configKey, config, IPCONFIG_FILE_VERSION);
  82. }
  83. //这里最终完成静态ip写到配置文件:/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt
  84. public static boolean writeConfig(DataOutputStream out, String configKey,
  85. IpConfiguration config, int version) throws IOException {
  86. boolean written = false;
  87. try {
  88. switch (config.getIpAssignment()) {
  89. case STATIC:
  90. out.writeUTF(IP_ASSIGNMENT_KEY);
  91. out.writeUTF(config.getIpAssignment().toString());
  92. StaticIpConfiguration staticIpConfiguration = config.getStaticIpConfiguration();
  93. if (staticIpConfiguration != null) {
  94. if (staticIpConfiguration.getIpAddress() != null) {
  95. LinkAddress ipAddress = staticIpConfiguration.getIpAddress();
  96. out.writeUTF(LINK_ADDRESS_KEY);
  97. out.writeUTF(ipAddress.getAddress().getHostAddress());
  98. out.writeInt(ipAddress.getPrefixLength());
  99. }
  100. if (staticIpConfiguration.getGateway() != null) {
  101. out.writeUTF(GATEWAY_KEY);
  102. out.writeInt(0); // Default route.
  103. out.writeInt(1); // Have a gateway.
  104. out.writeUTF(staticIpConfiguration.getGateway().getHostAddress());
  105. }
  106. for (InetAddress inetAddr : staticIpConfiguration.getDnsServers()) {
  107. out.writeUTF(DNS_KEY);
  108. out.writeUTF(inetAddr.getHostAddress());
  109. }
  110. }
  111. written = true;
  112. break;
  113. case DHCP:
  114. out.writeUTF(IP_ASSIGNMENT_KEY);
  115. out.writeUTF(config.getIpAssignment().toString());
  116. written = true;
  117. break;
  118. case UNASSIGNED:
  119. /* Ignore */
  120. break;
  121. default:
  122. loge("Ignore invalid ip assignment while writing");
  123. break;
  124. }
  125. switch (config.getProxySettings()) {
  126. case STATIC:
  127. ProxyInfo proxyProperties = config.getHttpProxy();
  128. String exclusionList = ProxyUtils.exclusionListAsString(
  129. proxyProperties.getExclusionList());
  130. out.writeUTF(PROXY_SETTINGS_KEY);
  131. out.writeUTF(config.getProxySettings().toString());
  132. out.writeUTF(PROXY_HOST_KEY);
  133. out.writeUTF(proxyProperties.getHost());
  134. out.writeUTF(PROXY_PORT_KEY);
  135. out.writeInt(proxyProperties.getPort());
  136. if (exclusionList != null) {
  137. out.writeUTF(EXCLUSION_LIST_KEY);
  138. out.writeUTF(exclusionList);
  139. }
  140. written = true;
  141. break;
  142. case PAC:
  143. ProxyInfo proxyPacProperties = config.getHttpProxy();
  144. out.writeUTF(PROXY_SETTINGS_KEY);
  145. out.writeUTF(config.getProxySettings().toString());
  146. out.writeUTF(PROXY_PAC_FILE);
  147. out.writeUTF(proxyPacProperties.getPacFileUrl().toString());
  148. written = true;
  149. break;
  150. case NONE:
  151. out.writeUTF(PROXY_SETTINGS_KEY);
  152. out.writeUTF(config.getProxySettings().toString());
  153. written = true;
  154. break;
  155. case UNASSIGNED:
  156. /* Ignore */
  157. break;
  158. default:
  159. loge("Ignore invalid proxy settings while writing");
  160. break;
  161. }
  162. if (written) {
  163. out.writeUTF(ID_KEY);
  164. if (version < 3) {
  165. out.writeInt(Integer.valueOf(configKey));
  166. } else {
  167. out.writeUTF(configKey);
  168. }
  169. }
  170. } catch (NullPointerException e) {
  171. loge("Failure in writing " + config + e);
  172. }
  173. out.writeUTF(EOS);
  174. return written;
  175. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/527996
推荐阅读
相关标签
  

闽ICP备14008679号