Wifi exchange view

Change-Id: I0595cf90c9e3e14a1c8dee51e1e618a1c90879bd
diff --git a/Android.bp b/Android.bp
index 179aafc..dd75563 100644
--- a/Android.bp
+++ b/Android.bp
@@ -63,6 +63,7 @@
         "common/vsoc/lib/lock_common.cpp",
         "common/vsoc/lib/region_view.cpp",
         "common/vsoc/lib/wifi_exchange_layout.cpp",
+        "common/vsoc/lib/wifi_exchange_view.cpp",
         "host/vsoc/lib/host_lock.cpp",
         "host/vsoc/lib/region_control.cpp",
         "host/vsoc/lib/region_view.cpp",
diff --git a/common/commands/wificlient/Android.bp b/common/commands/wificlient/Android.bp
index ccad72b..f8044a6 100644
--- a/common/commands/wificlient/Android.bp
+++ b/common/commands/wificlient/Android.bp
@@ -17,6 +17,7 @@
         "cuttlefish_glog",
         "cuttlefish_kernel_headers",
     ],
+    compile_multilib: "64",
     target: {
         linux: {
             host_ldlibs: ["-lrt"],
diff --git a/common/libs/wifi/Android.bp b/common/libs/wifi/Android.bp
index 85b5d68..b9d45b1 100644
--- a/common/libs/wifi/Android.bp
+++ b/common/libs/wifi/Android.bp
@@ -12,12 +12,14 @@
     static_libs: [
         "libbase",
         "libnl",
+        "vsoc_lib",
     ],
     header_libs: [
         "cuttlefish_common_headers",
         "cuttlefish_glog",
         "cuttlefish_kernel_headers",
     ],
+    compile_multilib: "64",
     target: {
         linux: {
             host_ldlibs: ["-lrt"],
diff --git a/common/vsoc/lib/wifi_exchange_view.cpp b/common/vsoc/lib/wifi_exchange_view.cpp
new file mode 100644
index 0000000..ca28a19
--- /dev/null
+++ b/common/vsoc/lib/wifi_exchange_view.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2017 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 "common/vsoc/lib/wifi_exchange_view.h"
+
+namespace vsoc {
+namespace wifi {
+
+bool WifiExchangeView::Send(const void* buffer, size_t length) {
+#ifdef CUTTLEFISH_HOST
+  return data()->guest_ingress.Write(this, static_cast<const char*>(buffer),
+                                     length) == length;
+#else
+  return data()->guest_egress.Write(this, static_cast<const char*>(buffer),
+                                    length) == length;
+#endif
+}
+
+intptr_t WifiExchangeView::Recv(void* buffer, size_t max_length) {
+#ifdef CUTTLEFISH_HOST
+  return data()->guest_egress.Read(this, static_cast<char*>(buffer),
+                                   max_length);
+#else
+  return data()->guest_ingress.Read(this, static_cast<char*>(buffer),
+                                    max_length);
+#endif
+}
+
+void WifiExchangeView::SetGuestMACAddress(const uint8_t* mac_address) {
+  memcpy(data()->mac_address, mac_address, ETH_ALEN);
+}
+
+void WifiExchangeView::GetGuestMACAddress(uint8_t* mac_address) {
+  memcpy(mac_address, data()->mac_address, ETH_ALEN);
+}
+
+}  // namespace wifi
+}  // namespace vsoc
diff --git a/common/vsoc/lib/wifi_exchange_view.h b/common/vsoc/lib/wifi_exchange_view.h
new file mode 100644
index 0000000..83d7e6a
--- /dev/null
+++ b/common/vsoc/lib/wifi_exchange_view.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+#pragma once
+
+#include "common/vsoc/lib/typed_region_view.h"
+#include "common/vsoc/shm/wifi_exchange_layout.h"
+#include "uapi/vsoc_shm.h"
+
+namespace vsoc {
+namespace wifi {
+
+class WifiExchangeView
+    : public vsoc::TypedRegionView<vsoc::layout::wifi::WifiExchangeLayout> {
+ public:
+  // Send netlink packet to peer.
+  // returns true, if operation was successful.
+  bool Send(const void* buffer, size_t length);
+
+  // Receive netlink packet from peer.
+  // Returns number of bytes read, or negative value, if failed.
+  intptr_t Recv(void* buffer, size_t max_length);
+
+  // Set guest MAC address.
+  void SetGuestMACAddress(const uint8_t* mac_address);
+  void GetGuestMACAddress(uint8_t* mac_address);
+
+  void SetConfigReady();
+  void WaitConfigReady();
+};
+
+}  // namespace wifi
+}  // namespace vsoc
diff --git a/common/vsoc/shm/version.h b/common/vsoc/shm/version.h
index 728ed85..54baf34 100644
--- a/common/vsoc/shm/version.h
+++ b/common/vsoc/shm/version.h
@@ -119,7 +119,10 @@
 }  // namespace
 constexpr size_t WifiExchangeLayout_size =
     65548 + // sizeof(CircularPacketQueue<16, 8192>) - forward
-    65548;  // sizeof(CircularPacketQueue<16, 8192>) - reverse
+    65548 + // sizeof(CircularPacketQueue<16, 8192>) - reverse
+    4 +     // Lock config_lock_
+    2 +     // bool config_ready_ (and even address alignment)
+    6;      // uint8_t[6] MAC address.
 }  // namespace wifi
 
 
diff --git a/common/vsoc/shm/wifi_exchange_layout.h b/common/vsoc/shm/wifi_exchange_layout.h
index 8d63b86..10888cd 100644
--- a/common/vsoc/shm/wifi_exchange_layout.h
+++ b/common/vsoc/shm/wifi_exchange_layout.h
@@ -14,9 +14,11 @@
  * limitations under the License.
  */
 #pragma once
+#include <linux/if_ether.h>
 
 #include "common/vsoc/shm/base.h"
 #include "common/vsoc/shm/circqueue.h"
+#include "common/vsoc/shm/lock.h"
 #include "common/vsoc/shm/version.h"
 
 // Memory layout for wifi packet exchange region.
@@ -30,6 +32,13 @@
   // Traffic originating from guest that proceeds towards host.
   CircularPacketQueue<16, 8192> guest_egress;
 
+  // config_lock_ manages access to configuration section
+  SpinLock config_lock_;
+  // config_ready_ indicates whether config section is ready to be accessed.
+  bool config_ready_;
+  // Desired MAC address for guest device.
+  uint8_t mac_address[ETH_ALEN];
+
   static const char* region_name;
 };