PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/android/upstream/com/android/internal/telephony/DataCallState.java

https://bitbucket.org/festevezga/xobotos
Java | 237 lines | 184 code | 22 blank | 31 comment | 47 complexity | b001c6c74b210c9260796851834cbde2 MD5 | raw file
  1. /*
  2. * Copyright (C) 2009 Qualcomm Innovation Center, Inc. All Rights Reserved.
  3. * Copyright (C) 2009 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.android.internal.telephony;
  18. import android.net.LinkAddress;
  19. import android.net.LinkProperties;
  20. import android.net.NetworkUtils;
  21. import android.net.RouteInfo;
  22. import android.os.SystemProperties;
  23. import android.util.Log;
  24. import com.android.internal.telephony.DataConnection.FailCause;
  25. import java.net.Inet4Address;
  26. import java.net.InetAddress;
  27. import java.net.UnknownHostException;
  28. /**
  29. * This is RIL_Data_Call_Response_v5 from ril.h
  30. * TODO: Rename to DataCallResponse.
  31. */
  32. public class DataCallState {
  33. private final boolean DBG = true;
  34. private final String LOG_TAG = "GSM";
  35. public int version = 0;
  36. public int status = 0;
  37. public int cid = 0;
  38. public int active = 0;
  39. public String type = "";
  40. public String ifname = "";
  41. public String [] addresses = new String[0];
  42. public String [] dnses = new String[0];
  43. public String[] gateways = new String[0];
  44. public int suggestedRetryTime = -1;
  45. /**
  46. * Class returned by onSetupConnectionCompleted.
  47. */
  48. public enum SetupResult {
  49. SUCCESS,
  50. ERR_BadCommand,
  51. ERR_UnacceptableParameter,
  52. ERR_GetLastErrorFromRil,
  53. ERR_Stale,
  54. ERR_RilError;
  55. public FailCause mFailCause;
  56. SetupResult() {
  57. mFailCause = FailCause.fromInt(0);
  58. }
  59. @Override
  60. public String toString() {
  61. return name() + " SetupResult.mFailCause=" + mFailCause;
  62. }
  63. }
  64. @Override
  65. public String toString() {
  66. StringBuffer sb = new StringBuffer();
  67. sb.append("DataCallState: {")
  68. .append("version=").append(version)
  69. .append(" status=").append(status)
  70. .append(" retry=").append(suggestedRetryTime)
  71. .append(" cid=").append(cid)
  72. .append(" active=").append(active)
  73. .append(" type=").append(type)
  74. .append("' ifname='").append(ifname);
  75. sb.append("' addresses=[");
  76. for (String addr : addresses) {
  77. sb.append(addr);
  78. sb.append(",");
  79. }
  80. if (addresses.length > 0) sb.deleteCharAt(sb.length()-1);
  81. sb.append("] dnses=[");
  82. for (String addr : dnses) {
  83. sb.append(addr);
  84. sb.append(",");
  85. }
  86. if (dnses.length > 0) sb.deleteCharAt(sb.length()-1);
  87. sb.append("] gateways=[");
  88. for (String addr : gateways) {
  89. sb.append(addr);
  90. sb.append(",");
  91. }
  92. if (gateways.length > 0) sb.deleteCharAt(sb.length()-1);
  93. sb.append("]}");
  94. return sb.toString();
  95. }
  96. public SetupResult setLinkProperties(LinkProperties linkProperties,
  97. boolean okToUseSystemPropertyDns) {
  98. SetupResult result;
  99. // Start with clean network properties and if we have
  100. // a failure we'll clear again at the bottom of this code.
  101. if (linkProperties == null)
  102. linkProperties = new LinkProperties();
  103. else
  104. linkProperties.clear();
  105. if (status == FailCause.NONE.getErrorCode()) {
  106. String propertyPrefix = "net." + ifname + ".";
  107. try {
  108. // set interface name
  109. linkProperties.setInterfaceName(ifname);
  110. // set link addresses
  111. if (addresses != null && addresses.length > 0) {
  112. for (String addr : addresses) {
  113. LinkAddress la;
  114. int addrPrefixLen;
  115. String [] ap = addr.split("/");
  116. if (ap.length == 2) {
  117. addr = ap[0];
  118. addrPrefixLen = Integer.parseInt(ap[1]);
  119. } else {
  120. addrPrefixLen = 0;
  121. }
  122. InetAddress ia;
  123. try {
  124. ia = NetworkUtils.numericToInetAddress(addr);
  125. } catch (IllegalArgumentException e) {
  126. throw new UnknownHostException("Non-numeric ip addr=" + addr);
  127. }
  128. if (! ia.isAnyLocalAddress()) {
  129. if (addrPrefixLen == 0) {
  130. // Assume point to point
  131. addrPrefixLen = (ia instanceof Inet4Address) ? 32 : 128;
  132. }
  133. if (DBG) Log.d(LOG_TAG, "addr/pl=" + addr + "/" + addrPrefixLen);
  134. la = new LinkAddress(ia, addrPrefixLen);
  135. linkProperties.addLinkAddress(la);
  136. }
  137. }
  138. } else {
  139. throw new UnknownHostException("no address for ifname=" + ifname);
  140. }
  141. // set dns servers
  142. if (dnses != null && dnses.length > 0) {
  143. for (String addr : dnses) {
  144. InetAddress ia;
  145. try {
  146. ia = NetworkUtils.numericToInetAddress(addr);
  147. } catch (IllegalArgumentException e) {
  148. throw new UnknownHostException("Non-numeric dns addr=" + addr);
  149. }
  150. if (! ia.isAnyLocalAddress()) {
  151. linkProperties.addDns(ia);
  152. }
  153. }
  154. } else if (okToUseSystemPropertyDns){
  155. String dnsServers[] = new String[2];
  156. dnsServers[0] = SystemProperties.get(propertyPrefix + "dns1");
  157. dnsServers[1] = SystemProperties.get(propertyPrefix + "dns2");
  158. for (String dnsAddr : dnsServers) {
  159. InetAddress ia;
  160. try {
  161. ia = NetworkUtils.numericToInetAddress(dnsAddr);
  162. } catch (IllegalArgumentException e) {
  163. throw new UnknownHostException("Non-numeric dns addr=" + dnsAddr);
  164. }
  165. if (! ia.isAnyLocalAddress()) {
  166. linkProperties.addDns(ia);
  167. }
  168. }
  169. } else {
  170. throw new UnknownHostException("Empty dns response and no system default dns");
  171. }
  172. // set gateways
  173. if ((gateways == null) || (gateways.length == 0)) {
  174. String sysGateways = SystemProperties.get(propertyPrefix + "gw");
  175. if (sysGateways != null) {
  176. gateways = sysGateways.split(" ");
  177. } else {
  178. gateways = new String[0];
  179. }
  180. }
  181. for (String addr : gateways) {
  182. InetAddress ia;
  183. try {
  184. ia = NetworkUtils.numericToInetAddress(addr);
  185. } catch (IllegalArgumentException e) {
  186. throw new UnknownHostException("Non-numeric gateway addr=" + addr);
  187. }
  188. if (! ia.isAnyLocalAddress()) {
  189. linkProperties.addRoute(new RouteInfo(ia));
  190. }
  191. }
  192. result = SetupResult.SUCCESS;
  193. } catch (UnknownHostException e) {
  194. Log.d(LOG_TAG, "setLinkProperties: UnknownHostException " + e);
  195. e.printStackTrace();
  196. result = SetupResult.ERR_UnacceptableParameter;
  197. }
  198. } else {
  199. if (version < 4) {
  200. result = SetupResult.ERR_GetLastErrorFromRil;
  201. } else {
  202. result = SetupResult.ERR_RilError;
  203. }
  204. }
  205. // An error occurred so clear properties
  206. if (result != SetupResult.SUCCESS) {
  207. if(DBG) {
  208. Log.d(LOG_TAG, "setLinkProperties: error clearing LinkProperties " +
  209. "status=" + status + " result=" + result);
  210. }
  211. linkProperties.clear();
  212. }
  213. return result;
  214. }
  215. }