basic test code for BTstackManager, state machine BT activation done

This commit is contained in:
matthias.ringwald 2010-02-20 19:24:50 +00:00
parent 160f793aff
commit e08d54c228
6 changed files with 270 additions and 24 deletions

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.${PRODUCT_NAME:identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>NSMainNibFile</key>
<string>MainWindow</string>
</dict>
</plist>

View File

@ -46,18 +46,31 @@
// TODO enumerate BTstackError type
typedef int BTstackError;
typedef enum {
kDeactivated = 1,
kW4SysBTState,
kW4SysBTDisabled,
kW4Activated,
kActivated,
kW4Deactivated
} ManagerState;
@protocol BTstackManagerDelegate;
@interface BTstackManager : NSObject {
@private
id<BTstackManagerDelegate> _delegate;
NSMutableDictionary *deviceInfo;
BOOL connectedToDaemon;
ManagerState state;
}
// shared instance
+(BTstackManager *) sharedInstance;
// Activation
-(void) activate;
-(void) deactivate;
-(BTstackError) activate;
-(BTstackError) deactivate;
// Discovery
-(BTstackError) startDiscovery;
@ -66,13 +79,13 @@ typedef int BTstackError;
-(BTDevice*) deviceAtIndex:(int)index;
// Connections
-(void) createL2CAPChannelAtAddress:(bd_addr_t) address withPSM:(uint16_t)psm authenticated:(BOOL)authentication;
-(void) closeL2CAPChannelWithID:(uint16_t) channelID;
-(void) sendL2CAPPacketForChannelID:(uint16_t)channelID;
-(BTstackError) createL2CAPChannelAtAddress:(bd_addr_t) address withPSM:(uint16_t)psm authenticated:(BOOL)authentication;
-(BTstackError) closeL2CAPChannelWithID:(uint16_t) channelID;
-(BTstackError) sendL2CAPPacketForChannelID:(uint16_t)channelID;
-(void) createRFCOMMConnectionAtAddress:(bd_addr_t) address withChannel:(uint16_t)psm authenticated:(BOOL)authentication;
-(void) closeRFCOMMConnectionWithID:(uint16_t) connectionID;
-(void) sendRFCOMMPacketForChannelID:(uint16_t)connectionID;
-(BTstackError) createRFCOMMConnectionAtAddress:(bd_addr_t) address withChannel:(uint16_t)psm authenticated:(BOOL)authentication;
-(BTstackError) closeRFCOMMConnectionWithID:(uint16_t) connectionID;
-(BTstackError) sendRFCOMMPacketForChannelID:(uint16_t)connectionID;
// TODO add l2cap and rfcomm incoming commands

View File

@ -42,7 +42,7 @@
#include <dlfcn.h>
#define INQUIRY_INTERVAL 3
// #define LASER_KB
#define LASER_KB
// fix compare for 3.0
#ifndef __IPHONE_3_0

View File

@ -34,22 +34,47 @@
#import <btstack/btstack.h>
#import "../../RFCOMM/rfcomm.h"
#define BTstackManagerID @"ch.ringwald.btstack"
static BTstackManager * btstackManager = nil;
@interface BTstackManager (privat)
- (void) handlePacketWithType:(uint8_t) packet_type
forChannel:(uint16_t) channel
andData:(uint8_t *)packet
withLen:(uint16_t) size;
- (void)handlePacketWithType:(uint8_t) packet_type forChannel:(uint16_t) channel andData:(uint8_t *)packet withLen:(uint16_t) size;
- (void)readDeviceInfo;
- (void)storeDeviceInfo;
@end
// needed for libBTstack
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
if (btstackManager) {
[btstackManager handlePacketWithType:packet_type forChannel:channel andData:packet withLen:size];
}
[btstackManager handlePacketWithType:packet_type forChannel:channel andData:packet withLen:size];
}
// dummy implementation of BTstackManagerDelegate protocol to avoid respondsToSelector call
@implementation NSObject (BTstackManagerDelegateDummy)
-(void) activated {};
-(void) activationFailed:(BTstackError)error {};
-(void) deactivated {};
-(BOOL) disableSystemBluetooth { return YES; }; // default: YES
-(void) deviceInfo:(BTDevice*)device {};
-(void) discoveryStopped {};
-(void) discoveryInquiry{};
-(void) discoveryQueryRemoteName:(int)deviceIndex{};
-(NSString*) pinForAddress:(bd_addr_t)addr { return @"0000"; }; // default: "0000"
-(void) l2capChannelCreatedAtAddress:(bd_addr_t)addr withPSM:(uint16_t)psm asID:(uint16_t)channelID {};
-(void) l2capChannelCreateFailedAtAddress:(bd_addr_t)addr withPSM:(uint16_t)psm error:(BTstackError)error{};
-(void) l2capChannelClosedForChannelID:(uint16_t)channelID{};
-(void) l2capDataReceivedForChannelID:(uint16_t)channelID withData:(uint8_t *)packet ofLen:(uint16_t)size{};
-(void) rfcommConnectionCreatedAtAddress:(bd_addr_t)addr forChannel:(uint16_t)channel asID:(uint16_t)connectionID{};
-(void) rfcommConnectionCreateFailedAtAddress:(bd_addr_t)addr forChannel:(uint16_t)channel error:(BTstackError)error{};
-(void) rfcommConnectionClosedForConnectionID:(uint16_t)connectionID{};
-(void) rfcommDataReceivedForConnectionID:(uint16_t)connectionID withData:(uint8_t *)packet ofLen:(uint16_t)size{};
// TODO add l2cap and rfcomm incoming events
-(void) handlePacketWithType:(uint8_t) packet_type forChannel:(uint16_t) channel andData:(uint8_t *)packet withLen:(uint16_t) size{};
@end
@implementation BTstackManager
@synthesize delegate = _delegate;
@ -58,7 +83,14 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
self = [super init];
if (!self) return self;
// Cocoa compatible run loop
state = kDeactivated;
connectedToDaemon = NO;
_delegate = nil;
// read device database
[self readDeviceInfo];
// Use Cocoa run loop
run_loop_init(RUN_LOOP_COCOA);
// our packet handler
@ -74,18 +106,143 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
return btstackManager;
}
-(void) activate {
bt_open();
bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
// Activation
-(BTstackError) activate {
BTstackError err = 0;
if (!connectedToDaemon) {
err = bt_open();
if (err) return BTSTACK_CONNECTION_TO_BTDAEMON_FAILED;
}
connectedToDaemon = YES;
// check system BT
state = kW4SysBTState;
bt_send_cmd(&btstack_get_system_bluetooth_enabled);
return err;
}
-(void) enableRFCOMM {
// rfcomm_register_packet_handler(packet_handler);
-(BTstackError) deactivate {
return 0;
}
// Discovery
-(BTstackError) startDiscovery {
return 0;
};
-(BTstackError) stopDiscovery{
return 0;
};
-(int) numberOfDevicesFound{
return 0;
};
-(BTDevice*) deviceAtIndex:(int)index{
return 0;
};
// Connections
-(BTstackError) createL2CAPChannelAtAddress:(bd_addr_t) address withPSM:(uint16_t)psm authenticated:(BOOL)authentication {
return 0;
};
-(BTstackError) closeL2CAPChannelWithID:(uint16_t) channelID {
return 0;
};
-(BTstackError) sendL2CAPPacketForChannelID:(uint16_t)channelID {
return 0;
};
-(BTstackError) createRFCOMMConnectionAtAddress:(bd_addr_t) address withChannel:(uint16_t)psm authenticated:(BOOL)authentication {
return 0;
};
-(BTstackError) closeRFCOMMConnectionWithID:(uint16_t) connectionID {
return 0;
};
-(BTstackError) sendRFCOMMPacketForChannelID:(uint16_t)connectionID {
return 0;
};
- (void) handlePacketWithType:(uint8_t)packet_type forChannel:(uint16_t)channel andData:(uint8_t *)packet withLen:(uint16_t) size {
if (delegate) {
[delegate handlePacketWithType:packet_type forChannel:channel andData:packet withLen:size];
switch (state) {
case kDeactivated:
break;
case kW4SysBTState:
case kW4SysBTDisabled:
// BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED
if ( packet_type == HCI_EVENT_PACKET
&& packet[0] == BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED){
if (packet[2]){
// system bt on - first time try to disable it
if ( state == kW4SysBTState) {
if (_delegate == nil || [_delegate disableSystemBluetooth]){
state = kW4SysBTDisabled;
bt_send_cmd(&btstack_set_system_bluetooth_enabled, 0);
} else {
state = kDeactivated;
[_delegate activationFailed:BTSTACK_ACTIVATION_FAILED_SYSTEM_BLUETOOTH];
}
} else {
state = kDeactivated;
[_delegate activationFailed:BTSTACK_ACTIVATION_FAILED_UNKNOWN];
}
} else {
state = kW4Activated;
bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON);
}
}
break;
case kW4Activated:
if (packet_type == HCI_EVENT_PACKET){
switch (packet[0]){
case BTSTACK_EVENT_STATE:
if (packet[2] == HCI_STATE_WORKING){
state = kActivated;
[_delegate activated];
}
break;
case BTSTACK_EVENT_POWERON_FAILED:
[_delegate activationFailed:BTSTACK_ACTIVATION_POWERON_FAILED];
state = kDeactivated;
break;
default:
break;
}
}
break;
default:
break;
}
[_delegate handlePacketWithType:packet_type forChannel:channel andData:packet withLen:size];
}
- (void)readDeviceInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defaults persistentDomainForName:BTstackManagerID];
deviceInfo = [NSMutableDictionary dictionaryWithCapacity:([dict count]+5)];
// copy entries
for (id key in dict) {
NSDictionary *value = [dict objectForKey:key];
NSMutableDictionary *deviceEntry = [NSMutableDictionary dictionaryWithCapacity:[value count]];
[deviceEntry addEntriesFromDictionary:value];
[deviceInfo setObject:deviceEntry forKey:key];
}
}
- (void)storeDeviceInfo{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setPersistentDomain:deviceInfo forName:BTstackManagerID];
[defaults synchronize];
// NSLog(@"store prefs %@", deviceInfo );
// NSLog(@"prefs name %@", prefsName);
// NSLog(@"Persistence Domain names %@", [defaults persistentDomainNames]);
}
@end

View File

@ -0,0 +1,16 @@
//
// TestBTstackManager.h
//
// Created by Matthias Ringwald on 19.02.10.
//
#import <Foundation/Foundation.h>
#import <BTstack/BTstackManager.h>
@class BTstackManager;
@interface TestBTstackManager : NSObject<BTstackManagerDelegate>{
BTstackManager *bt;
}
@end

View File

@ -0,0 +1,38 @@
//
// TestBTstackManager.m
//
// Created by Matthias Ringwald on 19.02.10.
//
#import "TestBTstackManager.h"
@implementation TestBTstackManager
-(id) init {
return self;
}
-(void) test{
bt = [BTstackManager sharedInstance];
[bt setDelegate:self];
BTstackError err = [bt activate];
if (err) NSLog(@"activate err 0x%02x!", err);
}
-(void) activated{
NSLog(@"activated!");
}
-(void) activationFailed:(BTstackError)error{
NSLog(@"activationFailed error 0x%02x!", error);
};
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
TestBTstackManager * test = [[TestBTstackManager alloc] init];
[test test];
CFRunLoopRun();
[test release];
[pool release];
return 0;
}