2008-07-03 00:01:02 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
|
2008-08-01 01:08:27 +04:00
|
|
|
* Copyright 2008 Mika Lindqvist, monni1995_at_gmail.com
|
2008-07-03 00:01:02 +04:00
|
|
|
* All rights reserved. Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _BDADDR_UTILS_H
|
|
|
|
#define _BDADDR_UTILS_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-06-15 00:04:10 +04:00
|
|
|
#include <string.h>
|
2008-07-03 00:01:02 +04:00
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
|
|
|
|
namespace Bluetooth {
|
|
|
|
|
|
|
|
class bdaddrUtils {
|
|
|
|
|
2008-08-03 01:15:02 +04:00
|
|
|
public:
|
2008-08-15 23:08:48 +04:00
|
|
|
static inline bdaddr_t NullAddress()
|
2008-08-03 01:15:02 +04:00
|
|
|
{
|
|
|
|
return ((bdaddr_t) {{0, 0, 0, 0, 0, 0}});
|
|
|
|
}
|
2008-08-01 01:08:27 +04:00
|
|
|
|
|
|
|
|
2008-08-15 23:08:48 +04:00
|
|
|
static inline bdaddr_t LocalAddress()
|
2008-08-03 01:15:02 +04:00
|
|
|
{
|
|
|
|
return ((bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}});
|
|
|
|
}
|
2008-08-01 01:08:27 +04:00
|
|
|
|
|
|
|
|
2008-08-15 23:08:48 +04:00
|
|
|
static inline bdaddr_t BroadcastAddress()
|
2008-08-03 01:15:02 +04:00
|
|
|
{
|
|
|
|
return ((bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}});
|
|
|
|
}
|
2008-08-01 01:08:27 +04:00
|
|
|
|
|
|
|
|
2010-06-15 00:04:10 +04:00
|
|
|
static bool Compare(const bdaddr_t* ba1, const bdaddr_t* ba2)
|
2008-08-03 01:15:02 +04:00
|
|
|
{
|
2010-06-15 00:04:10 +04:00
|
|
|
return (memcmp(ba1, ba2, sizeof(bdaddr_t)) == 0);
|
2008-08-03 01:15:02 +04:00
|
|
|
}
|
2008-08-01 01:08:27 +04:00
|
|
|
|
|
|
|
|
2010-06-15 00:04:10 +04:00
|
|
|
static void Copy(bdaddr_t* dst, const bdaddr_t* src)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, sizeof(bdaddr_t));
|
|
|
|
}
|
|
|
|
|
2008-08-15 23:08:48 +04:00
|
|
|
static char* ToString(const bdaddr_t bdaddr)
|
2008-08-03 01:15:02 +04:00
|
|
|
{
|
|
|
|
// TODO: not safe
|
|
|
|
static char str[18];
|
2008-08-01 01:08:27 +04:00
|
|
|
|
2009-03-12 00:26:21 +03:00
|
|
|
sprintf(str,"%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",bdaddr.b[5],
|
|
|
|
bdaddr.b[4], bdaddr.b[3], bdaddr.b[2], bdaddr.b[1],
|
|
|
|
bdaddr.b[0]);
|
2008-08-03 01:15:02 +04:00
|
|
|
return str;
|
|
|
|
}
|
2008-07-03 00:01:02 +04:00
|
|
|
|
2008-08-01 01:08:27 +04:00
|
|
|
|
2008-08-03 01:15:02 +04:00
|
|
|
static bdaddr_t FromString(const char * addr)
|
|
|
|
{
|
|
|
|
int b0, b1, b2, b3, b4, b5;
|
2008-08-01 01:08:27 +04:00
|
|
|
|
2008-08-03 01:15:02 +04:00
|
|
|
if (addr != NULL) {
|
|
|
|
size_t count = sscanf(addr, "%2X:%2X:%2X:%2X:%2X:%2X",
|
2010-06-15 00:04:10 +04:00
|
|
|
&b5, &b4, &b3, &b2, &b1, &b0);
|
2008-08-01 01:08:27 +04:00
|
|
|
|
2008-08-03 01:15:02 +04:00
|
|
|
if (count == 6)
|
|
|
|
return ((bdaddr_t) {{b0, b1, b2, b3, b4, b5}});
|
|
|
|
}
|
2008-07-03 00:01:02 +04:00
|
|
|
|
2008-08-03 01:15:02 +04:00
|
|
|
return NullAddress();
|
2008-08-15 23:08:48 +04:00
|
|
|
}
|
2008-07-03 00:01:02 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-06-15 00:04:10 +04:00
|
|
|
|
2008-07-03 00:01:02 +04:00
|
|
|
#ifndef _BT_USE_EXPLICIT_NAMESPACE
|
|
|
|
using Bluetooth::bdaddrUtils;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-08-03 01:15:02 +04:00
|
|
|
#endif // _BDADDR_UTILS_H
|