base: add SystemErrorCodeToString() function.

Pulls the Windows error string generation out of adb into libbase so
that it can be used by fastboot as well. Also makes a Unix equivalent
that just wraps strerror() so that upcoming fastboot error reporting
code can be platform-independent.

The intent here is just to provide a portable way to report an error to
the user. More general cross-platform error handling is out of scope.

Bug: http://b/26236380
Change-Id: I5a784a844775949562d069bb41dcb0ebd13a32bc
diff --git a/adb/usb_windows.cpp b/adb/usb_windows.cpp
index 8d3501e..e79008f 100644
--- a/adb/usb_windows.cpp
+++ b/adb/usb_windows.cpp
@@ -27,6 +27,8 @@
 #include <windows.h>
 #include <winerror.h>
 
+#include <android-base/errors.h>
+
 #include "adb.h"
 #include "transport.h"
 
@@ -221,7 +223,7 @@
   if (!instance) {
     // This is such a common API call that this should never fail.
     fatal("GetModuleHandleW failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 
   WNDCLASSEXW wndclass;
@@ -232,14 +234,14 @@
   wndclass.lpszClassName = kPowerNotificationWindowClassName;
   if (!RegisterClassExW(&wndclass)) {
     fatal("RegisterClassExW failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 
   if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
                        L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0,
                        NULL, NULL, instance, NULL)) {
     fatal("CreateWindowExW failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 
   MSG msg;
@@ -285,7 +287,7 @@
   ret->adb_interface = AdbCreateInterfaceByName(interface_name);
   if (NULL == ret->adb_interface) {
     D("AdbCreateInterfaceByName failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -296,7 +298,7 @@
                                    AdbOpenSharingModeReadWrite);
   if (NULL == ret->adb_read_pipe) {
     D("AdbOpenDefaultBulkReadEndpoint failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -307,7 +309,7 @@
                                     AdbOpenSharingModeReadWrite);
   if (NULL == ret->adb_write_pipe) {
     D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -319,7 +321,7 @@
                       false);
   if (0 == name_len) {
     D("AdbGetInterfaceName returned name length of zero: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -335,7 +337,7 @@
                            &name_len,
                            false)) {
     D("AdbGetInterfaceName failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     goto fail;
   }
 
@@ -370,7 +372,7 @@
                             &written,
                             time_out)) {
     D("AdbWriteEndpointSync failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     err = EIO;
     goto fail;
   }
@@ -394,7 +396,7 @@
                               &written,
                               time_out)) {
       D("AdbWriteEndpointSync of zero length packet failed: %s",
-        SystemErrorCodeToString(GetLastError()).c_str());
+        android::base::SystemErrorCodeToString(GetLastError()).c_str());
       err = EIO;
       goto fail;
     }
@@ -431,7 +433,7 @@
     if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
                              time_out)) {
       D("AdbReadEndpointSync failed: %s",
-        SystemErrorCodeToString(GetLastError()).c_str());
+        android::base::SystemErrorCodeToString(GetLastError()).c_str());
       err = EIO;
       goto fail;
     }
@@ -460,7 +462,7 @@
 static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
   if (!AdbCloseHandle(adb_handle)) {
     D("AdbCloseHandle(%p) failed: %s", adb_handle,
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 }
 
@@ -539,7 +541,7 @@
   if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
                                  &device_desc)) {
     D("AdbGetUsbDeviceDescriptor failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     return 0;
   }
 
@@ -549,7 +551,7 @@
   if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
                                     &interf_desc)) {
     D("AdbGetUsbInterfaceDescriptor failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     return 0;
   }
 
@@ -569,7 +571,7 @@
         D("device zero_mask: 0x%x", handle->zero_mask);
       } else {
         D("AdbGetEndpointInformation failed: %s",
-          SystemErrorCodeToString(GetLastError()).c_str());
+          android::base::SystemErrorCodeToString(GetLastError()).c_str());
       }
     }
 
@@ -591,7 +593,7 @@
 
   if (NULL == enum_handle) {
     D("AdbEnumInterfaces failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
     return;
   }
 
@@ -627,7 +629,7 @@
             }
           } else {
             D("cannot get serial number: %s",
-              SystemErrorCodeToString(GetLastError()).c_str());
+              android::base::SystemErrorCodeToString(GetLastError()).c_str());
             usb_cleanup_handle(handle);
             free(handle);
           }
@@ -644,7 +646,7 @@
   if (GetLastError() != ERROR_NO_MORE_ITEMS) {
     // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
     D("AdbNextInterface failed: %s",
-      SystemErrorCodeToString(GetLastError()).c_str());
+      android::base::SystemErrorCodeToString(GetLastError()).c_str());
   }
 
   _adb_close_handle(enum_handle);