Merge "Store the system time when NITZ is received." am: 236f668034 am: 0406228af4
am: a2e8af50ea

Change-Id: I74c796651711d5d94d1690018b68a6bd35476251
diff --git a/include/telephony/ril.h b/include/telephony/ril.h
index 3d40b8f..8377308 100644
--- a/include/telephony/ril.h
+++ b/include/telephony/ril.h
@@ -2401,8 +2401,8 @@
  *  SUCCESS
  *  RADIO_NOT_AVAILABLE (radio resetting)
  *  PASSWORD_INCORRECT
- *     (code is invalid)
  *  SIM_ABSENT
+ *     (code is invalid)
  *  INTERNAL_ERR
  *  NO_MEMORY
  *  NO_RESOURCES
@@ -3055,7 +3055,7 @@
  *    RADIO_NOT_AVAILABLE, OP_NOT_ALLOWED_BEFORE_REG_TO_NW,
  *    OP_NOT_ALLOWED_DURING_VOICE_CALL, REQUEST_NOT_SUPPORTED,
  *    INVALID_ARGUMENTS, INTERNAL_ERR, NO_MEMORY, NO_RESOURCES,
- *    SIM_ABSENT and CANCELLED
+ *    CANCELLED and SIM_ABSENT
  *
  * See also: RIL_REQUEST_DEACTIVATE_DATA_CALL
  */
diff --git a/libril/RilSapSocket.cpp b/libril/RilSapSocket.cpp
index 664e6a9..211371e 100644
--- a/libril/RilSapSocket.cpp
+++ b/libril/RilSapSocket.cpp
@@ -55,10 +55,9 @@
         sap_socket->onRequestComplete(t,e,response,responselen);
     } else {
         RLOGE("Invalid socket id");
-        if (request->curr->payload) {
-            free(request->curr->payload);
+        if (request->curr) {
+            free(request->curr);
         }
-        free(request->curr);
         free(request);
     }
 }
@@ -234,6 +233,12 @@
 void RilSapSocket::onRequestComplete(RIL_Token t, RIL_Errno e, void *response,
         size_t response_len) {
     SapSocketRequest* request= (SapSocketRequest*)t;
+
+    if (!request || !request->curr) {
+        RLOGE("RilSapSocket::onRequestComplete: request/request->curr is NULL");
+        return;
+    }
+
     MsgHeader *hdr = request->curr;
 
     MsgHeader rsp;
diff --git a/libril/sap_service.cpp b/libril/sap_service.cpp
index b945174..cd5b137 100644
--- a/libril/sap_service.cpp
+++ b/libril/sap_service.cpp
@@ -106,11 +106,13 @@
 
 Return<void> SapImpl::addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen,
         uint8_t *reqPtr) {
-    msg->payload = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + reqLen);
-    if (msg->payload == NULL) {
+    pb_bytes_array_t *payload = (pb_bytes_array_t *) malloc(sizeof(pb_bytes_array_t) - 1 + reqLen);
+    if (payload == NULL) {
         sendFailedResponse(msg->id, msg->token, 2, reqPtr, msg);
         return Void();
     }
+
+    msg->payload = payload;
     msg->payload->size = reqLen;
     memcpy(msg->payload->bytes, reqPtr, reqLen);
 
@@ -120,7 +122,7 @@
         sapSocket->dispatchRequest(msg);
     } else {
         RLOGE("SapImpl::addPayloadAndDispatchRequest: sapSocket is null");
-        sendFailedResponse(msg->id, msg->token, 3, msg->payload, reqPtr, msg);
+        sendFailedResponse(msg->id, msg->token, 3, payload, reqPtr, msg);
         return Void();
     }
     free(msg->payload);
diff --git a/reference-ril/reference-ril.c b/reference-ril/reference-ril.c
index 3ab7bee..1ba2508 100644
--- a/reference-ril/reference-ril.c
+++ b/reference-ril/reference-ril.c
@@ -50,7 +50,10 @@
 #define MAX_AT_RESPONSE 0x1000
 
 /* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
-#define PPP_TTY_PATH "eth0"
+// This is used if Wifi is not supported, plain old eth0
+#define PPP_TTY_PATH_ETH0 "eth0"
+// This is used if Wifi is supported to separate radio and wifi interface
+#define PPP_TTY_PATH_RADIO0 "radio0"
 
 // Default MTU value
 #define DEFAULT_MTU 1500
@@ -508,6 +511,13 @@
     RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
 }
 
+static bool hasWifi()
+{
+    char propValue[PROP_VALUE_MAX];
+    return __system_property_get("ro.kernel.qemu.wifi", propValue) != 0 &&
+           strcmp("1", propValue) == 0;
+}
+
 static void requestOrSendDataCallList(RIL_Token *t)
 {
     ATResponse *p_response;
@@ -515,6 +525,8 @@
     int err;
     int n = 0;
     char *out;
+    char propValue[PROP_VALUE_MAX];
+    bool has_wifi = hasWifi();
 
     err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
     if (err != 0 || p_response->success == 0) {
@@ -620,9 +632,15 @@
         if (err < 0)
             goto error;
 
-        int ifname_size = strlen(PPP_TTY_PATH) + 1;
-        responses[i].ifname = alloca(ifname_size);
-        strlcpy(responses[i].ifname, PPP_TTY_PATH, ifname_size);
+        if (has_wifi) {
+            int ifname_size = strlen(PPP_TTY_PATH_RADIO0) + 1;
+            responses[i].ifname = alloca(ifname_size);
+            strlcpy(responses[i].ifname, PPP_TTY_PATH_RADIO0, ifname_size);
+        } else {
+            int ifname_size = strlen(PPP_TTY_PATH_ETH0) + 1;
+            responses[i].ifname = alloca(ifname_size);
+            strlcpy(responses[i].ifname, PPP_TTY_PATH_ETH0, ifname_size);
+        }
 
         err = at_tok_nextstr(&line, &out);
         if (err < 0)
@@ -666,7 +684,10 @@
             }
             responses[i].dnses = dnslist;
 
-            responses[i].gateways = "10.0.2.2 fe80::2";
+            /* There is only one gateway in the emulator. If WiFi is
+             * configured the interface visible to RIL will be behind a NAT
+             * where the gateway is different. */
+            responses[i].gateways = has_wifi ? "192.168.200.1" : "10.0.2.2";
             responses[i].mtu = DEFAULT_MTU;
         }
         else {
diff --git a/rild/Android.mk b/rild/Android.mk
index 7ac505c..57a9d48 100644
--- a/rild/Android.mk
+++ b/rild/Android.mk
@@ -1,5 +1,7 @@
 # Copyright 2006 The Android Open Source Project
 
+ifndef ENABLE_VENDOR_RIL_SERVICE
+
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
@@ -31,3 +33,4 @@
 
 include $(BUILD_EXECUTABLE)
 
+endif