keep RemoteName and LinkKey in NSDictionary - used for name lookup and automatic link authentication

This commit is contained in:
matthias.ringwald 2009-12-12 20:28:36 +00:00
parent a51168ce9f
commit 3ce394c4bc
2 changed files with 61 additions and 32 deletions

View File

@ -39,10 +39,8 @@
#include <btstack/hci_cmds.h> // for HCI_STATE #include <btstack/hci_cmds.h> // for HCI_STATE
#define PREFS_SHARED_SUITE "ch.ringwald.BTstack" #define PREFS_REMOTE_NAME @"RemoteName"
#define PREFS_DEVICES "Devices" #define PREFS_LINK_KEY @"LinkKey"
#define PREFS_NAME "Name"
#define PREFS_LINK_KEY "LinkKey"
@class BTDevice; @class BTDevice;
@protocol BTInquiryDelegate; @protocol BTInquiryDelegate;
@ -66,8 +64,8 @@ typedef enum {
bool allowSelection; bool allowSelection;
bool showIcons; bool showIcons;
// used to store link keys and remote names (soon :) // used to store link keys and remote names
// NSMutableDictionary *deviceInfo; NSMutableDictionary *deviceInfo;
// hacks // hacks
bool stopRemoteNameGathering; bool stopRemoteNameGathering;
@ -91,7 +89,7 @@ typedef enum {
@property (nonatomic, assign) bool showIcons; @property (nonatomic, assign) bool showIcons;
@property (nonatomic, retain) NSMutableArray *devices; @property (nonatomic, retain) NSMutableArray *devices;
@property (nonatomic, retain) id<BTInquiryDelegate> delegate; @property (nonatomic, retain) id<BTInquiryDelegate> delegate;
// @property (nonatomic, retain) NSMutableDictionary *deviceInfo; @property (nonatomic, retain) NSMutableDictionary *deviceInfo;
@end @end
@protocol BTInquiryDelegate @protocol BTInquiryDelegate

View File

@ -74,7 +74,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
@implementation BTInquiryViewController @implementation BTInquiryViewController
@synthesize devices; @synthesize devices;
// @synthesize deviceInfo; @synthesize deviceInfo;
@synthesize delegate; @synthesize delegate;
@synthesize allowSelection; @synthesize allowSelection;
@synthesize showIcons; @synthesize showIcons;
@ -99,22 +99,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
devices = [[NSMutableArray alloc] init]; devices = [[NSMutableArray alloc] init];
inqView = self; inqView = self;
// create suite preferences
//CFStringRef testValue = CFSTR("TEST!!!");
//CFPreferencesSetAppValue (CFSTR(PREFS_DEVICES), testValue, CFSTR(PREFS_DEVICES));
//CFPreferencesAppSynchronize (CFSTR(PREFS_SHARED_SUITE));
// preferences
/* NSMutableDictionary *emptyDeviceInfo = [NSMutableDictionary dictionary];
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
[defaultValues setObject:emptyDeviceInfo forKey:@PREFS_DEVICES];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
[[NSUserDefaults standardUserDefaults] addSuiteNamed:@PREFS_SHARED_SUITE];
[self setDeviceInfo:[[NSUserDefaults standardUserDefaults] objectForKey:@PREFS_DEVICES] ];
*/
// check for the one missing method // check for the one missing method
onSDK20 = 0; onSDK20 = 0;
// [UITableViewCell instancesRespondToSelector:@selector(initWithFrame:reuseIdentifier:)]; // [UITableViewCell instancesRespondToSelector:@selector(initWithFrame:reuseIdentifier:)];
@ -188,6 +173,15 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
[dev setClassOfDevice:READ_BT_24(packet, 3 + numResponses*(6+1+1+1) + i*3)]; [dev setClassOfDevice:READ_BT_24(packet, 3 + numResponses*(6+1+1+1) + i*3)];
[dev setClockOffset:(READ_BT_16(packet, 3 + numResponses*(6+1+1+1+3) + i*2) & 0x7fff)]; [dev setClockOffset:(READ_BT_16(packet, 3 + numResponses*(6+1+1+1+3) + i*2) & 0x7fff)];
// hexdump(packet, size); // hexdump(packet, size);
// get name from deviceInfo
if (deviceInfo) {
NSMutableDictionary * deviceDict = [deviceInfo objectForKey:[dev addressString]];
if (deviceDict){
dev.name = [deviceDict objectForKey:PREFS_REMOTE_NAME];
}
}
NSLog(@"adding %@", [dev toString] ); NSLog(@"adding %@", [dev toString] );
[devices addObject:dev]; [devices addObject:dev];
@ -205,7 +199,16 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
if (!dev) break; if (!dev) break;
[dev setConnectionState:kBluetoothConnectionNotConnected]; [dev setConnectionState:kBluetoothConnectionNotConnected];
if (packet[2] == 0) { if (packet[2] == 0) {
[dev setName:[NSString stringWithUTF8String:(const char *) &packet[9]]]; NSString *remoteName = [NSString stringWithUTF8String:(const char *) &packet[9]];
[dev setName:remoteName];
if (deviceInfo) {
NSMutableDictionary *deviceDict = [deviceInfo objectForKey:[dev addressString]];
if (!deviceDict){
deviceDict = [NSMutableDictionary dictionaryWithCapacity:3];
[deviceInfo setObject:deviceDict forKey:[dev addressString]];
}
[deviceDict setObject:remoteName forKey:PREFS_REMOTE_NAME];
}
if (delegate) { if (delegate) {
[delegate deviceDetected:self device:dev]; [delegate deviceDetected:self device:dev];
} }
@ -244,27 +247,55 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
break; break;
case HCI_EVENT_INQUIRY_COMPLETE: case HCI_EVENT_INQUIRY_COMPLETE:
NSLog(@"Inquiry complete");
// reset name check // reset name check
remoteNameIndex = 0; remoteNameIndex = 0;
[self getNextRemoteName]; [self getNextRemoteName];
break; break;
case HCI_EVENT_LINK_KEY_NOTIFICATION:
if (deviceInfo) {
bt_flip_addr(event_addr, &packet[2]);
NSString *devAddress = [BTDevice stringForAddress:&event_addr];
NSData *linkKey = [NSData dataWithBytes:&packet[8] length:16];
NSMutableDictionary * deviceDict = [deviceInfo objectForKey:devAddress];
if (!deviceDict){
deviceDict = [NSMutableDictionary dictionaryWithCapacity:3];
[deviceInfo setObject:deviceDict forKey:[dev addressString]];
}
[deviceDict setObject:linkKey forKey:PREFS_LINK_KEY];
NSLog(@"Adding link key for %@, value %@", devAddress, linkKey);
}
break;
case HCI_EVENT_LINK_KEY_REQUEST: case HCI_EVENT_LINK_KEY_REQUEST: {
// link key request // link key request
NSData *linkKey = nil;
bt_flip_addr(event_addr, &packet[2]); bt_flip_addr(event_addr, &packet[2]);
bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr); // get link key from deviceInfo
break; if (deviceInfo) {
NSString *devAddress = [BTDevice stringForAddress:&event_addr];
NSMutableDictionary * deviceDict = [deviceInfo objectForKey:devAddress];
if (deviceDict){
linkKey = [deviceDict objectForKey:PREFS_LINK_KEY];
}
}
if (linkKey) {
bt_send_cmd(&hci_link_key_request_reply, &event_addr, [linkKey bytes]);
} else {
bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
}
break;
}
default: default:
break; break;
// hexdump(packet, size);
break;
} }
break;
default: default:
break; break;
} }
// forward to client app // forward to client app
(*clientHandler)(packet_type, channel, packet, size); (*clientHandler)(packet_type, channel, packet, size);