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 <String.h>
13
14#include <bluetooth/bluetooth.h>
15
16namespace Bluetooth {
17
18class bdaddrUtils {
19
20public:
21	static inline bdaddr_t NullAddress()
22	{
23		return ((bdaddr_t) {{0, 0, 0, 0, 0, 0}});
24	}
25
26
27	static inline bdaddr_t LocalAddress()
28	{
29		return ((bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}});
30	}
31
32
33	static inline bdaddr_t BroadcastAddress()
34	{
35		return ((bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}});
36	}
37
38
39	static bool Compare(const bdaddr_t& ba1, const bdaddr_t& ba2)
40	{
41		return (memcmp(&ba1, &ba2, sizeof(bdaddr_t)) == 0);
42	}
43
44
45	static void Copy(bdaddr_t& dst, const bdaddr_t& src)
46	{
47		memcpy(&dst, &src, sizeof(bdaddr_t));
48	}
49
50	static BString ToString(const bdaddr_t bdaddr)
51	{
52		BString str;
53
54		str.SetToFormat("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",bdaddr.b[5],
55				bdaddr.b[4], bdaddr.b[3], bdaddr.b[2], bdaddr.b[1],
56				bdaddr.b[0]);
57		return str;
58	}
59
60
61	static bdaddr_t FromString(const char * addr)
62	{
63		uint8 b0, b1, b2, b3, b4, b5;
64
65		if (addr != NULL) {
66			size_t count = sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
67						&b5, &b4, &b3, &b2, &b1, &b0);
68
69			if (count == 6)
70				return ((bdaddr_t) {{b0, b1, b2, b3, b4, b5}});
71		}
72
73		return NullAddress();
74	}
75
76};
77
78}
79
80
81#ifndef _BT_USE_EXPLICIT_NAMESPACE
82using Bluetooth::bdaddrUtils;
83#endif
84
85
86#endif // _BDADDR_UTILS_H
87