1 /* 2 * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com 3 * Copyright 2008 Mika Lindqvist, monni1995_at_gmail.com 4 * All rights reserved. Distributed under the terms of the MIT License. 5 */ 6 #ifndef _BDADDR_UTILS_H 7 #define _BDADDR_UTILS_H 8 9 #include <stdio.h> 10 #include <string.h> 11 12 #include <bluetooth/bluetooth.h> 13 14 namespace Bluetooth { 15 16 class bdaddrUtils { 17 18 public: 19 static inline bdaddr_t NullAddress() 20 { 21 return ((bdaddr_t) {{0, 0, 0, 0, 0, 0}}); 22 } 23 24 25 static inline bdaddr_t LocalAddress() 26 { 27 return ((bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}}); 28 } 29 30 31 static inline bdaddr_t BroadcastAddress() 32 { 33 return ((bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}); 34 } 35 36 37 static bool Compare(const bdaddr_t& ba1, const bdaddr_t& ba2) 38 { 39 return (memcmp(&ba1, &ba2, sizeof(bdaddr_t)) == 0); 40 } 41 42 43 static void Copy(bdaddr_t& dst, const bdaddr_t& src) 44 { 45 memcpy(&dst, &src, sizeof(bdaddr_t)); 46 } 47 48 static char* ToString(const bdaddr_t bdaddr) 49 { 50 // TODO: not safe 51 static char str[18]; 52 53 sprintf(str,"%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",bdaddr.b[5], 54 bdaddr.b[4], bdaddr.b[3], bdaddr.b[2], bdaddr.b[1], 55 bdaddr.b[0]); 56 return str; 57 } 58 59 60 static bdaddr_t FromString(const char * addr) 61 { 62 int b0, b1, b2, b3, b4, b5; 63 64 if (addr != NULL) { 65 size_t count = sscanf(addr, "%2X:%2X:%2X:%2X:%2X:%2X", 66 &b5, &b4, &b3, &b2, &b1, &b0); 67 68 if (count == 6) 69 return ((bdaddr_t) {{b0, b1, b2, b3, b4, b5}}); 70 } 71 72 return NullAddress(); 73 } 74 75 }; 76 77 } 78 79 80 #ifndef _BT_USE_EXPLICIT_NAMESPACE 81 using Bluetooth::bdaddrUtils; 82 #endif 83 84 85 #endif // _BDADDR_UTILS_H 86