xref: /haiku/headers/os/bluetooth/LinkKeyUtils.h (revision aa3083e086e5a929c061c72983e09d916c548a38)
1 /*
2  * Copyright 2008 Mika Lindqvist, monni1995_at_gmail.com
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef _LINKKEY_UTILS_H
6 #define _LINKKEY_UTILS_H
7 
8 #include <stdio.h>
9 
10 #include <bluetooth/bluetooth.h>
11 
12 
13 namespace Bluetooth {
14 
15 class LinkKeyUtils {
16 public:
17 	static bool Compare(linkkey_t* lk1, linkkey_t* lk2)
18 	{
19 		return memcmp(lk1, lk2, sizeof(linkkey_t)) == 0;
20 	}
21 
22 	static linkkey_t NullKey()
23 	{
24 		return (linkkey_t){{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
25 	}
26 
27 	static BString ToString(const linkkey_t lk)
28 	{
29 		BString str;
30 
31 		str.SetToFormat("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:"
32 				"%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
33 				lk.l[0], lk.l[1], lk.l[2], lk.l[3], lk.l[4], lk.l[5],
34 				lk.l[6], lk.l[7], lk.l[8], lk.l[9], lk.l[10], lk.l[11],
35 				lk.l[12], lk.l[13], lk.l[14], lk.l[15]);
36 
37 		return str;
38 	}
39 
40 	static linkkey_t FromString(const char *lkstr)
41 	{
42 		if (lkstr != NULL) {
43 			uint8 l0, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15;
44 			size_t count = sscanf(lkstr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:"
45 							"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhxs", &l0, &l1, &l2, &l3,
46 							&l4, &l5, &l6, &l7, &l8, &l9, &l10, &l11, &l12, &l13,
47 							&l14, &l15);
48 
49 			if (count == 16) {
50 				return (linkkey_t){{l0, l1, l2, l3, l4, l5, l6, l7, l8,
51 					l9, l10, l11, l12, l13, l14, l15}};
52 			}
53 		}
54 
55 		return NullKey();
56 	}
57 };
58 
59 }
60 
61 #ifndef _BT_USE_EXPLICIT_NAMESPACE
62 using Bluetooth::LinkKeyUtils;
63 #endif
64 
65 #endif // _LINKKEY_UTILS_H
66