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 char* ToString(const linkkey_t lk) 28 { 29 // TODO: not safe 30 static char str[50]; 31 32 sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:" 33 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", 34 lk.l[0], lk.l[1], lk.l[2], lk.l[3], lk.l[4], lk.l[5], 35 lk.l[6], lk.l[7], lk.l[8], lk.l[9], lk.l[10], lk.l[11], 36 lk.l[12], lk.l[13], lk.l[14], lk.l[15]); 37 38 return str; 39 } 40 41 static linkkey_t FromString(const char *lkstr) 42 { 43 if (lkstr != NULL) { 44 uint8 l0, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15; 45 size_t count = sscanf(lkstr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:" 46 "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhxs", &l0, &l1, &l2, &l3, 47 &l4, &l5, &l6, &l7, &l8, &l9, &l10, &l11, &l12, &l13, 48 &l14, &l15); 49 50 if (count == 16) { 51 return (linkkey_t){{l0, l1, l2, l3, l4, l5, l6, l7, l8, 52 l9, l10, l11, l12, l13, l14, l15}}; 53 } 54 } 55 56 return NullKey(); 57 } 58 }; 59 60 } 61 62 #ifndef _BT_USE_EXPLICIT_NAMESPACE 63 using Bluetooth::LinkKeyUtils; 64 #endif 65 66 #endif // _LINKKEY_UTILS_H 67