xref: /haiku/headers/os/bluetooth/bdaddrUtils.h (revision b55a57da7173b9af0432bd3e148d03f06161d036)
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 
11 #include <bluetooth/bluetooth.h>
12 #include <bluetooth/bluetooth_util.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(bdaddr_t *ba1, bdaddr_t *ba2)
38 	{
39 		return (bacmp(ba1, ba2) == 0);
40 	}
41 
42 
43 	static char* ToString(const bdaddr_t bdaddr)
44 	{
45 		// TODO: not safe
46 		static char str[18];
47 
48 		sprintf(str,"%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",bdaddr.b[5],
49 				bdaddr.b[4], bdaddr.b[3], bdaddr.b[2], bdaddr.b[1],
50 				bdaddr.b[0]);
51 		return str;
52 	}
53 
54 
55 	static bdaddr_t FromString(const char * addr)
56 	{
57 		int b0, b1, b2, b3, b4, b5;
58 
59 		if (addr != NULL) {
60 			size_t count = sscanf(addr, "%2X:%2X:%2X:%2X:%2X:%2X",
61 						&b0, &b1, &b2, &b3, &b4, &b5);
62 
63 			if (count == 6)
64 				return ((bdaddr_t) {{b0, b1, b2, b3, b4, b5}});
65 		}
66 
67 		return NullAddress();
68 	}
69 
70 };
71 
72 }
73 
74 #ifndef _BT_USE_EXPLICIT_NAMESPACE
75 using Bluetooth::bdaddrUtils;
76 #endif
77 
78 
79 #endif // _BDADDR_UTILS_H
80