/*
* wiiuse
*
* Written By:
* Michael Laforest < para >
* Email: < thepara (--AT--) g m a i l [--DOT--] com >
*
* Copyright 2006-2007
*
* This file is part of wiiuse.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* $Header$
*
*/
/**
* @file
* @brief Handles device I/O for *nix.
*/
#ifndef WIN32
#include
#include
#include
//osx inlcude
#import
#import
#import
#import
IOBluetoothDeviceInquiry *bti;
IOBluetoothDevice * btd;
IOBluetoothL2CAPChannel * _ichan;
IOBluetoothL2CAPChannel * _cchan;
#include "definitions.h"
#include "wiiuse_internal.h"
#include "io.h"
byte DataFromWiimote[MAX_PAYLOAD];
static int wiiuse_connect_single(struct wiimote_t* wm, char* address);
@interface SearchBT: NSObject {}
-(void) deviceInquiryComplete: (IOBluetoothDeviceInquiry*) sender
error: (IOReturn) error
aborted: (BOOL) aborted;
-(void) deviceInquiryDeviceFound: (IOBluetoothDeviceInquiry*) sender
device: (IOBluetoothDevice*) device;
@end
@interface ConnectBT: NSObject {}
-(void) connectToWiimotes;
- (IOBluetoothL2CAPChannel *) openL2CAPChannelWithPSM:(BluetoothL2CAPPSM) psm delegate:(id) delegate;
@end
@implementation ConnectBT
-(void)writeToWiimote: (void *)data length:(UInt16)length
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[_cchan writeSync:data length:length];
usleep(10000);
[pool release];
}
#pragma mark -
#pragma mark Bluetooth
- (void) l2capChannelOpenComplete:(IOBluetoothL2CAPChannel*) l2capChannel status:(IOReturn) error
{
//channel opened
//something to do here ?
}
- (void)l2capChannelData:(IOBluetoothL2CAPChannel *) l2capChannel data:(byte *)BtData length:(NSUInteger)length {
//here we got data from wiimote
for (int o=0; ounid);
/* do the handshake */
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
wiiuse_handshake(wm, NULL, 0);
wiiuse_set_report_type(wm);
[pool release];
return 1;
}
/**
* @brief Disconnect a wiimote.
*
* @param wm Pointer to a wiimote_t structure.
*
* @see wiiuse_connect()
*
* Note that this will not free the wiimote structure.
*/
void wiiuse_disconnect(struct wiimote_t* wm) {
if (!wm || WIIMOTE_IS_CONNECTED(wm))
return;
//TODO
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
}
int wiiuse_io_read(struct wiimote_t* wm) {
if (!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED))
return 0;
/* if this wiimote is not connected, skip it */
if (!WIIMOTE_IS_CONNECTED(wm))
return 0;
//run the main loop to get bt data
CFRunLoopRun();
memcpy(wm->event_buf,DataFromWiimote,sizeof(wm->event_buf));
memcpy(wm->event_buf, &wm->event_buf[1], sizeof(wm->event_buf) - 1);
return 1;
}
int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len) {
if(buf[0] != (WM_SET_REPORT | WM_BT_OUTPUT))
{
// Linux and OSX need this, Windows strips it out
// Only packets from Dolphin don't have the start
// Wiiuse uses ifdefs to add the first byte without you ever knowing it
// Should find out a nice way of doing this, getting windows to stop stripping the packets would be nice
memcpy(buf + 1, buf, len - 1);
buf[0] = (WM_SET_REPORT | WM_BT_OUTPUT);
}
[cbt writeToWiimote:buf length:len];
return 1;
}
#endif /* ifndef WIN32 */