OSDN Git Service

Add a function to check if a string represents a bdaddr
authorSharvil Nanavati <sharvil@google.com>
Sat, 16 Aug 2014 01:39:16 +0000 (18:39 -0700)
committerAndre Eisenbach <eisenbach@google.com>
Mon, 16 Mar 2015 23:51:28 +0000 (16:51 -0700)
btif/include/btif_util.h
btif/src/btif_util.c

index 9711b26..f127585 100644 (file)
@@ -66,6 +66,7 @@ const char* dump_rc_event(UINT8 event);
 const char* dump_rc_notification_event_id(UINT8 event_id);
 const char* dump_rc_pdu(UINT8 pdu);
 
+bool str_is_bdaddr(const char *str);
 bool str2bd(const char *str, bt_bdaddr_t *addr);
 char *bd2str(const bt_bdaddr_t *addr, bdstr_t *bdstr);
 
index bdffa75..f0ff2e7 100644 (file)
 **   Logging helper functions
 *****************************************************************************/
 
+bool str_is_bdaddr(const char *string) {
+  size_t len = strlen(string);
+  if (len != 17)
+    return false;
+
+  for (size_t i = 0; i < len; ++i) {
+    // Every 3rd char must be ':'.
+    if (((i + 1) % 3) == 0 && string[i] != ':')
+      return false;
+
+    // All other chars must be a hex digit.
+    if (((i + 1) % 3) != 0 && !isxdigit(string[i]))
+      return false;
+  }
+  return true;
+}
+
 bool str2bd(const char *str, bt_bdaddr_t *addr) {
   uint8_t *ptr = addr->address;
   return sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",