Merge remote-tracking branch 'goog/upstream-master' into nos-merge-upstream

Bug: 63388215

* goog/upstream-master:
  Fix Android build
  wait_for_interrupt() should be able to time out
  Add LOG_TAG for libnos_transport functions
  Remove protobuf build hack.
  The Linux citadel_updater accepts a default device
  Add function to reset a device.

Change-Id: I0eda9d26092aa8e8e5aafaf86c87aadc338bdf79
diff --git a/citadel/updater/updater.cpp b/citadel/updater/updater.cpp
index fbbcd8d..1cff116 100644
--- a/citadel/updater/updater.cpp
+++ b/citadel/updater/updater.cpp
@@ -85,7 +85,9 @@
   {"enable_rw",   0, NULL, OPT_ENABLE_RW},
   {"change_pw",   0, NULL, OPT_CHANGE_PW},
   {"erase",       1, NULL, OPT_ERASE},
+#ifndef ANDROID
   {"device",      1, NULL, OPT_DEVICE},
+#endif
   {"help",        0, NULL, 'h'},
   {NULL, 0, NULL, 0},
 };
@@ -110,7 +112,7 @@
     "inactive copy (A/B) of each stage (RO/RW) can be modified.\n"
     "The tool will update the correct copies automatically.\n"
     "\n"
-    "You must specify the actions to perform. With no options,\n"
+    "You must specify the actions to perform. With no actions,\n"
     "this help message is displayed.\n"
     "\n"
     "Actions:\n"
@@ -127,6 +129,15 @@
     "\n\n"
     "      --erase=CODE  Erase all user secrets and reboot.\n"
     "                    This skips all other actions.\n"
+#ifndef ANDROID
+    "\n"
+    "Options:\n"
+    "\n"
+    "      --device=SN   Connect to the FDTI device with the given\n"
+    "                    serial number (try \"lsusb -v\"). A default\n"
+    "                    can be specified with the CITADEL_DEVICE\n"
+    "                    environment variable.\n"
+#endif
     "\n",
     progname);
 }
@@ -490,13 +501,19 @@
   char *e = 0;
 
   this_prog= strrchr(argv[0], '/');
-        if (this_prog) {
+  if (this_prog) {
     this_prog++;
   } else {
     this_prog = argv[0];
   }
 
-        opterr = 0;        /* quiet, you */
+#ifndef ANDROID
+  options.device = secure_getenv("CITADEL_DEVICE");
+  if (options.device)
+    fprintf(stderr, "-- $CITADEL_DEVICE=%s --\n", options.device);
+#endif
+
+  opterr = 0;        /* quiet, you */
   while ((i = getopt_long(argc, argv,
         short_opts, long_opts, &idx)) != -1) {
     switch (i) {
diff --git a/libnos/generator/test/Android.bp b/libnos/generator/test/Android.bp
index 51e8706..d854692 100644
--- a/libnos/generator/test/Android.bp
+++ b/libnos/generator/test/Android.bp
@@ -49,11 +49,11 @@
         "test.cpp",
         "nos/generator/test/test.proto",
     ],
-    defaults: ["libnosprotos_defaults"],
+    defaults: ["nos_proto_defaults"],
     proto: {
         type: "full",
+        canonical_path_from_root: false,
         include_dirs: [
-            ".",
             "external/protobuf/src",
             "external/nos/host/generic/nugget/proto",
         ],
diff --git a/libnos_datagram/include/nos/device.h b/libnos_datagram/include/nos/device.h
index 21366cc..d3d6055 100644
--- a/libnos_datagram/include/nos/device.h
+++ b/libnos_datagram/include/nos/device.h
@@ -42,9 +42,26 @@
   int (*write)(void *ctx, uint32_t command, const uint8_t *buf, uint32_t len);
 
   /**
-   * Block until an event has happened on the device.
+   * Block until an event has happened on the device, or until timed out.
+   *
+   * Values for msecs
+   *  <0 wait forever
+   *   0 return immediately (why?)
+   *  >0 timeout after this many milliseconds
+   *
+   * Returns:
+   *  <0 on error
+   *   0 timed out
+   *  >0 interrupt occurred
    */
-  void (*wait_for_interrupt)(void *ctx);
+  int (*wait_for_interrupt)(void *ctx, int msecs);
+
+  /**
+   * Reset the device.
+   *
+   * Return 0 on success and a negative value on failure.
+   */
+  int (*reset)(void *ctx);
 
   /**
    * Close the connection to the device.
diff --git a/libnos_transport/transport.c b/libnos_transport/transport.c
index 9c115d6..c328c18 100644
--- a/libnos_transport/transport.c
+++ b/libnos_transport/transport.c
@@ -32,12 +32,13 @@
 
 #ifdef ANDROID
 /* Logging for Android */
+#define LOG_TAG "libnos_transport"
 #include <sys/types.h>
 #include <log/log.h>
 
 #define NLOGE(...) ALOGE(__VA_ARGS__)
-#define NLOGV(...) do { if (VERBOSE_LOG) { ALOGV(__VA_ARGS__); } } while (0)
-#define NLOGD(...) do { if (DEBUG_LOG) { ALOGD(__VA_ARGS__); } } while (0)
+#define NLOGV(...) ALOGV(__VA_ARGS__)
+#define NLOGD(...) ALOGD(__VA_ARGS__)
 
 extern int usleep (uint32_t usec);
 
@@ -83,7 +84,6 @@
   return 0;
 }
 
-
 uint32_t nos_call_application(const struct nos_device *dev,
                               uint8_t app_id, uint16_t params,
                               const uint8_t *args, uint32_t arg_len,
@@ -124,8 +124,7 @@
     if (get_status(dev, app_id, &status, &ulen) != 0) {
       return APP_ERROR_IO;
     }
-    NLOGV("%d: query status 0x%08x  ulen 0x%04x\n",__LINE__,
-          status, ulen);
+    NLOGV("%d: query status 0x%08x  ulen 0x%04x",__LINE__, status, ulen);
 
     /* It's ignoring us and is still not ready, so it's broken */
     if (status != APP_STATUS_IDLE) {
@@ -150,7 +149,7 @@
     if (args && ulen)
       memcpy(buf, args, ulen);
 
-    NLOGV("Write command 0x%08x, bytes 0x%x\n", command, ulen);
+    NLOGV("Write command 0x%08x, bytes 0x%x", command, ulen);
 
     if (0 != dev->ops.write(dev->ctx, command, buf, ulen)) {
       NLOGE("Failed to send datagram to device");
@@ -170,14 +169,14 @@
   if (get_status(dev, app_id, &status, &ulen) != 0) {
     return APP_ERROR_IO;
   }
-  NLOGV("%d: query status 0x%08x  ulen 0x%04x\n", __LINE__, status, ulen);
+  NLOGV("%d: query status 0x%08x  ulen 0x%04x", __LINE__, status, ulen);
   if (status & APP_STATUS_DONE)
     /* Yep, problems. It should still be idle. */
     goto reply;
 
   /* Now tell the app to do whatever */
   command = CMD_ID(app_id) | CMD_PARAM(params);
-  NLOGV("Write command 0x%08x\n", command);
+  NLOGV("Write command 0x%08x", command);
   if (0 != dev->ops.write(dev->ctx, command, 0, 0)) {
       NLOGE("Failed to send command datagram to device");
       return APP_ERROR_IO;
@@ -188,11 +187,10 @@
     if (get_status(dev, app_id, &status, &ulen) != 0) {
       return APP_ERROR_IO;
     }
-    NLOGD("%d:  poll status 0x%08x  ulen 0x%04x\n", __LINE__,
-          status, ulen);
+    NLOGD("%d:  poll status 0x%08x  ulen 0x%04x", __LINE__, status, ulen);
     poll_count++;
   } while (!(status & APP_STATUS_DONE));
-  NLOGV("polled %d times, status 0x%08x  ulen 0x%04x\n", poll_count,
+  NLOGV("polled %d times, status 0x%08x  ulen 0x%04x", poll_count,
         status, ulen);
 
 reply:
@@ -213,8 +211,7 @@
        * a constant. For other buses, it may not be.
        */
       gimme = MIN(left, MAX_DEVICE_TRANSFER);
-      NLOGV("Read command 0x%08x, bytes 0x%x\n",
-            command, gimme);
+      NLOGV("Read command 0x%08x, bytes 0x%x", command, gimme);
       if (0 != dev->ops.read(dev->ctx, command, buf, gimme)) {
         NLOGE("Failed to receive datagram from device");
         return APP_ERROR_IO;
diff --git a/nugget/proto/Android.bp b/nugget/proto/Android.bp
index 4a785ba..e507ad6 100644
--- a/nugget/proto/Android.bp
+++ b/nugget/proto/Android.bp
@@ -14,44 +14,19 @@
 // limitations under the License.
 //
 
-cc_defaults {
-    name: "libnosprotos_defaults",
-    defaults: ["nos_proto_defaults"],
-    cflags: [
-        // HACK: Work around the limitations of protobuf in the Android build.
-        // When importing a file, the symbols generated in the importee don't
-        // match the expectations of the importer. These are case-by-case
-        // defines to solve the problem.
-
-
-        "-Dprotobuf_AddDesc_nugget_2fprotobuf_2foptions_2eproto=protobuf_AddDesc_external_2fnos_2fhost_2fgeneric_2fnugget_2fproto_2fnugget_2fprotobuf_2foptions_2eproto",
-
-        // keymaster
-        "-Dprotobuf_AddDesc_nugget_2fapp_2fkeymaster_2fkeymaster_5ftypes_2eproto=protobuf_AddDesc_external_2fnos_2fhost_2fgeneric_2fnugget_2fproto_2fnugget_2fapp_2fkeymaster_2fkeymaster_5ftypes_2eproto",
-        "-Dprotobuf_AddDesc_nugget_2fapp_2fkeymaster_2fkeymaster_5fdefs_2eproto=protobuf_AddDesc_external_2fnos_2fhost_2fgeneric_2fnugget_2fproto_2fnugget_2fapp_2fkeymaster_2fkeymaster_5fdefs_2eproto",
-
-        // protoapi
-        "-Dprotobuf_AddDesc_nugget_2fapp_2fprotoapi_2fgchips_5ftypes_2eproto=protobuf_AddDesc_external_2fnos_2fhost_2fgeneric_2fnugget_2fproto_2fnugget_2fapp_2fprotoapi_2fgchips_5ftypes_2eproto",
-        "-Dprotobuf_AddDesc_nugget_2fapp_2fprotoapi_2fheader_2eproto=protobuf_AddDesc_external_2fnos_2fhost_2fgeneric_2fnugget_2fproto_2fnugget_2fapp_2fprotoapi_2fheader_2eproto",
-    ],
-}
-
 cc_library {
     name: "libnosprotos",
     srcs: ["**/*.proto"],
     exclude_srcs: ["google/protobuf/descriptor.proto"], // Used for bazel build
     defaults: [
-        "libnosprotos_defaults",
+        "nos_proto_defaults",
         "nos_cc_host_supported_defaults",
     ],
     proto: {
         type: "full",
+        canonical_path_from_root: false,
         export_proto_headers: true,
         include_dirs: [
-            // HACK: This must be the first path to force generated files to
-            // have the path expected by soong
-            ".",
-
             "external/nos/host/generic/nugget/proto",
             "external/protobuf/src",
         ],