Initialize cuttlefish's ril from properties instead of VSoC region

On boot the launcher sets a set of cuttlefish_ril_... properties which
the ril hal reads to initialize itself.

This change applies both to vsoc and virtio hardware types.

Bug: 124012805
Test: build & run locally
Change-Id: I59dfb8791ff39b93ed821d54b299271a65fbab21
diff --git a/common/libs/constants/ril.h b/common/libs/constants/ril.h
new file mode 100644
index 0000000..b9c3e8a
--- /dev/null
+++ b/common/libs/constants/ril.h
@@ -0,0 +1,31 @@
+#pragma once
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if defined(CUTTLEFISH_HOST)
+#define CF_PROPERTY_PREFIX "androidboot"
+#else
+#define CF_PROPERTY_PREFIX "ro.boot"
+#endif
+
+#define CUTTLEFISH_RIL_ADDR_PROPERTY CF_PROPERTY_PREFIX ".cuttlefish_ril_addr"
+#define CUTTLEFISH_RIL_GATEWAY_PROPERTY \
+  CF_PROPERTY_PREFIX ".cuttlefish_ril_gateway"
+#define CUTTLEFISH_RIL_DNS_PROPERTY CF_PROPERTY_PREFIX ".cuttlefish_ril_dns"
+#define CUTTLEFISH_RIL_BROADCAST_PROPERTY \
+  CF_PROPERTY_PREFIX ".cuttlefish_ril_broadcast"
+#define CUTTLEFISH_RIL_PREFIXLEN_PROPERTY \
+  CF_PROPERTY_PREFIX ".cuttlefish_ril_prefixlen"
diff --git a/guest/hals/ril/cuttlefish_ril.cpp b/guest/hals/ril/cuttlefish_ril.cpp
index 80de442..929933b 100644
--- a/guest/hals/ril/cuttlefish_ril.cpp
+++ b/guest/hals/ril/cuttlefish_ril.cpp
@@ -17,6 +17,7 @@
 #include "guest/hals/ril/cuttlefish_ril.h"
 
 #include <cutils/properties.h>
+#include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
 #include <time.h>
@@ -26,10 +27,10 @@
 #include <string>
 #include <vector>
 
+#include "common/libs/constants/ril.h"
 #include "common/libs/net/netlink_client.h"
 #include "common/libs/net/network_interface.h"
 #include "common/libs/net/network_interface_manager.h"
-#include "common/vsoc/lib/ril_region_view.h"
 #include "guest/libs/platform_support/api_level_fixes.h"
 
 #define CUTTLEFISH_RIL_VERSION_STRING "Android Cuttlefish RIL 1.0"
@@ -61,6 +62,49 @@
   RUIM_NETWORK_PERSONALIZATION = 11
 } SIM_Status;
 
+class RilConfig {
+ public:
+  static void InitRilConfig();
+
+  static char* address_and_prefixlength() {
+    return RilConfig::global_ril_config_.address_and_prefixlength_;
+  }
+
+  static char* dns() {
+    return RilConfig::global_ril_config_.dns_;
+  }
+
+  static char* gateway() {
+    return RilConfig::global_ril_config_.gateway_;
+  }
+
+  static char* ipaddr() {
+    return RilConfig::global_ril_config_.ipaddr_;
+  }
+
+  static int prefixlen() {
+    return RilConfig::global_ril_config_.prefixlen_;
+  }
+
+  static char* broadcast() {
+    return RilConfig::global_ril_config_.broadcast_;
+  }
+
+ private:
+  RilConfig() = default;
+  RilConfig(const RilConfig&) = default;
+
+  char ipaddr_[16]; // xxx.xxx.xxx.xxx\0 = 16 bytes
+  char gateway_[16];
+  char dns_[16];
+  char broadcast_[16];
+  char address_and_prefixlength_[19]; // <ipaddr>/dd
+  int prefixlen_;
+
+  static RilConfig global_ril_config_;
+};
+RilConfig RilConfig::global_ril_config_;
+
 static const struct RIL_Env* gce_ril_env;
 
 static const struct timeval TIMEVAL_SIMPOLL = {3, 0};
@@ -129,6 +173,43 @@
   return false;
 }
 
+static bool ReadStringProperty(char* dst, const char* key, size_t max_size) {
+  char buffer[PROPERTY_VALUE_MAX];
+  auto res = property_get(key, buffer, NULL);
+  if (res < 0) {
+    ALOGE("Failed to read property %s", key);
+    return false;
+  }
+  if (res > static_cast<int>(max_size - 1)) {
+    ALOGE("Invalid value in property %s: value too long: %s", key, buffer);
+    return false;
+  }
+  snprintf(dst, res + 1, "%s", buffer);
+  return true;
+}
+
+void RilConfig::InitRilConfig() {
+  RilConfig tmp_config;
+  ReadStringProperty(&tmp_config.ipaddr_[0], CUTTLEFISH_RIL_ADDR_PROPERTY,
+                     sizeof(tmp_config.ipaddr_));
+  ReadStringProperty(&tmp_config.gateway_[0],
+                     CUTTLEFISH_RIL_GATEWAY_PROPERTY,
+                     sizeof(tmp_config.gateway_));
+  ReadStringProperty(&tmp_config.dns_[0], CUTTLEFISH_RIL_DNS_PROPERTY,
+                     sizeof(tmp_config.dns_));
+  ReadStringProperty(&tmp_config.broadcast_[0],
+                     CUTTLEFISH_RIL_BROADCAST_PROPERTY,
+                     sizeof(tmp_config.broadcast_));
+  tmp_config.prefixlen_ =
+      property_get_int32(CUTTLEFISH_RIL_PREFIXLEN_PROPERTY, 30);
+
+  snprintf(&tmp_config.address_and_prefixlength_[0],
+           sizeof(tmp_config.address_and_prefixlength_), "%s/%d",
+           tmp_config.ipaddr_, tmp_config.prefixlen_);
+
+  RilConfig::global_ril_config_ = tmp_config;
+}
+
 // TearDownNetworkInterface disables network interface.
 // This call returns true, if operation was successful.
 bool TearDownNetworkInterface() {
@@ -186,13 +267,11 @@
         break;
     }
 
-    auto ril_region_view = vsoc::ril::RilRegionView::GetInstance();
-
     responses[index].ifname = (char*)"rmnet0";
     responses[index].addresses =
-      const_cast<char*>(ril_region_view->address_and_prefix_length());
-    responses[index].dnses = (char*)ril_region_view->data()->dns;
-    responses[index].gateways = (char*)ril_region_view->data()->gateway;
+      const_cast<char*>(RilConfig::address_and_prefixlength());
+    responses[index].dnses = RilConfig::dns();
+    responses[index].gateways = RilConfig::gateway();
 #if VSOC_PLATFORM_SDK_AFTER(N_MR1)
     responses[index].pcscf = (char*)"";
     responses[index].mtu = 1440;
@@ -314,10 +393,8 @@
   }
 
   if (gDataCalls.empty()) {
-    auto ril_region_view = vsoc::ril::RilRegionView::GetInstance();
-    SetUpNetworkInterface(ril_region_view->data()->ipaddr,
-                          ril_region_view->data()->prefixlen,
-                          ril_region_view->data()->broadcast);
+    SetUpNetworkInterface(RilConfig::ipaddr(), RilConfig::prefixlen(),
+                          RilConfig::broadcast());
   }
 
   gDataCalls[gNextDataCallId] = call;
@@ -2472,6 +2549,8 @@
   time(&gce_ril_start_time);
   gce_ril_env = env;
 
+  RilConfig::InitRilConfig();
+
   TearDownNetworkInterface();
 
   init_modem_supported_network_types();
diff --git a/host/commands/launch/Android.bp b/host/commands/launch/Android.bp
index d055973..c7c0859 100644
--- a/host/commands/launch/Android.bp
+++ b/host/commands/launch/Android.bp
@@ -18,7 +18,7 @@
     srcs: [
         "main.cc",
         "screen_region_handler.cc",
-        "ril_region_handler.cc",
+        "ril_config.cc",
         "vsoc_shared_memory.cc",
         "wifi_region_handler.cc",
         "boot_image_unpacker.cc",
diff --git a/host/commands/launch/flags.cc b/host/commands/launch/flags.cc
index dbb44bf..797c1ea 100644
--- a/host/commands/launch/flags.cc
+++ b/host/commands/launch/flags.cc
@@ -12,6 +12,7 @@
 #include "host/commands/launch/data_image.h"
 #include "host/commands/launch/launch.h"
 #include "host/commands/launch/launcher_defs.h"
+#include "host/commands/launch/ril_config.h"
 #include "host/libs/vm_manager/crosvm_manager.h"
 #include "host/libs/vm_manager/qemu_manager.h"
 #include "host/libs/vm_manager/vm_manager.h"
@@ -349,6 +350,7 @@
 
   tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
   tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
+  ConfigureRil(&tmp_config_obj);
 
   tmp_config_obj.set_wifi_bridge_name(FLAGS_wifi_interface);
   tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
diff --git a/host/commands/launch/pre_launch_initializers.h b/host/commands/launch/pre_launch_initializers.h
index 925cdf8..c4788db 100644
--- a/host/commands/launch/pre_launch_initializers.h
+++ b/host/commands/launch/pre_launch_initializers.h
@@ -25,14 +25,12 @@
 // To add initializers for more regions declare here, implement in its own
 // source file and call from PreLaunchInitializers::Initialize().
 void InitializeScreenRegion(const vsoc::CuttlefishConfig& config);
-void InitializeRilRegion(const vsoc::CuttlefishConfig& config);
 void InitializeWifiRegion(const vsoc::CuttlefishConfig& config);
 
 class PreLaunchInitializers {
  public:
   static void Initialize(const vsoc::CuttlefishConfig& config) {
     InitializeScreenRegion(config);
-    InitializeRilRegion(config);
     InitializeWifiRegion(config);
   }
 };
diff --git a/host/commands/launch/ril_region_handler.cc b/host/commands/launch/ril_config.cc
similarity index 76%
rename from host/commands/launch/ril_region_handler.cc
rename to host/commands/launch/ril_config.cc
index b3bb00a..195256a 100644
--- a/host/commands/launch/ril_region_handler.cc
+++ b/host/commands/launch/ril_config.cc
@@ -20,11 +20,13 @@
 #include <string.h>
 
 #include <memory>
+#include <sstream>
 #include <string>
 
-#include "common/vsoc/lib/ril_region_view.h"
-#include "host/commands/launch/pre_launch_initializers.h"
-#include "host/libs/config/cuttlefish_config.h"
+#include <glog/logging.h>
+
+#include "common/libs/constants/ril.h"
+#include "host/commands/launch/ril_config.h"
 
 namespace {
 
@@ -119,31 +121,31 @@
     return ret;
   }
 };
+
+template <typename T>
+std::string BuildPropertyDefinition(const std::string& prop_name,
+                                  const T& prop_value) {
+  std::ostringstream stream;
+  stream << prop_name << "=" << prop_value;
+  return stream.str();
+}
 }  // namespace
 
-void InitializeRilRegion(const vsoc::CuttlefishConfig& config) {
+void ConfigureRil(vsoc::CuttlefishConfig* config) {
   NetConfig netconfig;
-  if (!netconfig.ObtainConfig(config.mobile_bridge_name())) {
+  if (!netconfig.ObtainConfig(config->mobile_bridge_name())) {
     LOG(ERROR) << "Unable to obtain the network configuration";
     return;
   }
 
-  auto region =
-      vsoc::ril::RilRegionView::GetInstance(vsoc::GetDomain().c_str());
-
-  if (!region) {
-    LOG(ERROR) << "Ril region was not found";
-    return;
-  }
-
-  auto dest = region->data();
-
-  snprintf(dest->ipaddr, sizeof(dest->ipaddr), "%s",
-           netconfig.ril_ipaddr.c_str());
-  snprintf(dest->gateway, sizeof(dest->gateway), "%s",
-           netconfig.ril_gateway.c_str());
-  snprintf(dest->dns, sizeof(dest->dns), "%s", netconfig.ril_dns.c_str());
-  snprintf(dest->broadcast, sizeof(dest->broadcast), "%s",
-           netconfig.ril_broadcast.c_str());
-  dest->prefixlen = netconfig.ril_prefixlen;
+  config->add_kernel_cmdline(BuildPropertyDefinition(
+      CUTTLEFISH_RIL_ADDR_PROPERTY, netconfig.ril_ipaddr));
+  config->add_kernel_cmdline(BuildPropertyDefinition(
+      CUTTLEFISH_RIL_GATEWAY_PROPERTY, netconfig.ril_gateway));
+  config->add_kernel_cmdline(BuildPropertyDefinition(
+      CUTTLEFISH_RIL_DNS_PROPERTY, netconfig.ril_dns));
+  config->add_kernel_cmdline(BuildPropertyDefinition(
+      CUTTLEFISH_RIL_BROADCAST_PROPERTY, netconfig.ril_broadcast));
+  config->add_kernel_cmdline(BuildPropertyDefinition(
+      CUTTLEFISH_RIL_PREFIXLEN_PROPERTY, netconfig.ril_prefixlen));
 }
diff --git a/host/commands/launch/ril_config.h b/host/commands/launch/ril_config.h
new file mode 100644
index 0000000..49a43e8
--- /dev/null
+++ b/host/commands/launch/ril_config.h
@@ -0,0 +1,21 @@
+#pragma once
+
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "host/libs/config/cuttlefish_config.h"
+
+void ConfigureRil(vsoc::CuttlefishConfig* config);
\ No newline at end of file