Skip to content

Add wireless controller syncing functionality to WPAD/LWBT #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gc/bte/bd_addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ struct bd_addr {
(bdaddr)->addr[3] = d; \
(bdaddr)->addr[4] = e; \
(bdaddr)->addr[5] = f; }while(0)

#define BD_ADDR_FROM_BYTES(bdaddr, bytes) do{ \
(bdaddr)->addr[0] = bytes[5]; \
(bdaddr)->addr[1] = bytes[4]; \
(bdaddr)->addr[2] = bytes[3]; \
(bdaddr)->addr[3] = bytes[2]; \
(bdaddr)->addr[4] = bytes[1]; \
(bdaddr)->addr[5] = bytes[0]; }while(0)

#define BYTES_FROM_BD_ADDR(bytes, bdaddr) do{ \
bytes[0] = (bdaddr)->addr[5]; \
bytes[1] = (bdaddr)->addr[4]; \
bytes[2] = (bdaddr)->addr[3]; \
bytes[3] = (bdaddr)->addr[2]; \
bytes[4] = (bdaddr)->addr[1]; \
bytes[5] = (bdaddr)->addr[0]; }while(0)

//TODO: USE memcmp????
#define bd_addr_cmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \
((addr1)->addr[1] == (addr2)->addr[1]) && \
Expand Down
40 changes: 38 additions & 2 deletions gc/bte/bte.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
#define HIDP_PROTO_BOOT 0x00
#define HIDP_PROTO_REPORT 0x01

#define BD_NAME_LEN 248
#define BD_MAX_INQUIRY_DEVS 255

enum pair_mode {
PAIR_MODE_NORMAL,
PAIR_MODE_TEMPORARY
};

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
Expand All @@ -88,12 +96,24 @@ struct inquiry_info_ex
u16 co;
};

struct inquiry_res
{
u8 count;
struct inquiry_info_ex *info;
};

struct linkkey_info
{
struct bd_addr bdaddr;
u8 key[16];
};

struct pad_info
{
struct bd_addr bdaddr;
u8 name[BD_NAME_LEN];
};

struct bte_pcb
{
u8 err;
Expand All @@ -112,6 +132,7 @@ struct bte_pcb


s32 (*recv)(void *arg,void *buffer,u16 len);
s32 (*conn_req)(void *arg,struct bte_pcb *pcb,struct bd_addr *bdaddr,u8 *cod,u8 link_type,u8 err);
s32 (*conn_cfm)(void *arg,struct bte_pcb *pcb,u8 err);
s32 (*disconn_cfm)(void *arg,struct bte_pcb *pcb,u8 err);
};
Expand All @@ -120,26 +141,41 @@ typedef s32 (*btecallback)(s32 result,void *userdata);

void BTE_Init(void);
void BTE_Shutdown(void);
void BTE_Close(void);
s32 BTE_InitCore(btecallback cb);
s32 BTE_ApplyPatch(btecallback cb);
s32 BTE_InitSub(btecallback cb);
s32 BTE_ReadStoredLinkKey(struct linkkey_info *keys,u8 max_cnt,btecallback cb);
s32 BTE_ReadBdAddr(struct bd_addr *bdaddr, btecallback cb);
s32 BTE_SetEvtFilter(u8 filter_type,u8 filter_cond_type,u8 *cond, btecallback cb);
s32 BTE_ReadRemoteName(struct bd_addr *bdaddr, btecallback cb);
s32 BTE_Inquiry(u8 max_cnt,u8 flush, btecallback cb);
s32 BTE_PeriodicInquiry(u8 max_cnt,u8 flush,btecallback cb);
s32 BTE_ExitPeriodicInquiry(void);
s32 BTE_LinkKeyRequestReply(struct bd_addr *bdaddr,u8 *key);
s32 BTE_LinkKeyRequestNegativeReply(struct bd_addr *bdaddr);
void (*BTE_SetDisconnectCallback(void (*callback)(struct bd_addr *bdaddr,u8 reason)))(struct bd_addr *bdaddr,u8 reason);
void BTE_SetSyncButtonCallback(void (*callback)(u32 held));
void BTE_SetConnectionRequestCallback(s8 (*callback)(void *arg,struct bd_addr *bdaddr,u8 *cod,u8 link_type));
void BTE_SetLinkKeyRequestCallback(s8 (*callback)(void *arg,struct bd_addr *bdaddr));
void BTE_SetLinkKeyNotificationCallback(s8 (*callback)(void *arg,struct bd_addr *bdaddr,u8 *key));
u8 BTE_GetPairMode(void);
void BTE_WriteStoredLinkKey(struct bd_addr *bdaddr,u8 *key);
void BTE_ClearStoredLinkKeys(void);

struct bte_pcb* bte_new(void);
void bte_free(struct bte_pcb *pcb);
void bte_arg(struct bte_pcb *pcb,void *arg);
void bte_received(struct bte_pcb *pcb, s32 (*recv)(void *arg,void *buffer,u16 len));
void bte_disconnected(struct bte_pcb *pcb,s32 (disconn_cfm)(void *arg,struct bte_pcb *pcb,u8 err));

s32 bte_registerdeviceasync(struct bte_pcb *pcb,struct bd_addr *bdaddr,s32 (*conn_cfm)(void *arg,struct bte_pcb *pcb,u8 err));
s32 bte_connectdeviceasync(struct bte_pcb *pcb,struct bd_addr *bdaddr,s32 (*conn_cfm)(void *arg,struct bte_pcb *pcb,u8 err));

s32 bte_disconnect(struct bte_pcb *pcb);

//s32 bte_listen(struct bte_pcb *pcb,struct bd_addr *bdaddr,u8 psm);
//s32 bte_accept(struct bte_pcb *pcb,s32 (*recv)(void *arg,void *buffer,u16 len));
s32 bte_inquiry(struct inquiry_info *info,u8 max_cnt,u8 flush);
s32 bte_inquiry_ex(struct inquiry_info_ex *info,u8 max_cnt,u8 flush);
//s32 bte_connect(struct bte_pcb *pcb,struct bd_addr *bdaddr,u8 psm,s32 (*recv)(void *arg,void *buffer,u16 len));
//s32 bte_connect_ex(struct bte_pcb *pcb,struct inquiry_info_ex *info,u8 psm,s32 (*recv)(void *arg,void *buffer,u16 len));
s32 bte_senddata(struct bte_pcb *pcb,void *message,u16 len);
Expand Down
26 changes: 23 additions & 3 deletions gc/ogc/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ enum {
};

#define CONF_PAD_MAX_REGISTERED 10
#define CONF_PAD_MAX_ACTIVE 4
#define CONF_PAD_MAX_ACTIVE 6
#define CONF_PAD_MAX_WIIMOTES 4
#define CONF_PAD_TOTAL (CONF_PAD_MAX_REGISTERED + CONF_PAD_MAX_ACTIVE)

typedef struct _conf_pad_device conf_pad_device;

Expand All @@ -142,8 +144,21 @@ struct _conf_pads {
u8 num_registered;
conf_pad_device registered[CONF_PAD_MAX_REGISTERED];
conf_pad_device active[CONF_PAD_MAX_ACTIVE];
conf_pad_device balance_board;
conf_pad_device unknown;
} ATTRIBUTE_PACKED;

typedef struct _conf_pad_guest_device conf_pad_guest_device;

struct _conf_pad_guest_device {
u8 bdaddr[6];
char name[0x40];
u8 link_key[16];
} ATTRIBUTE_PACKED;

typedef struct _conf_pad_guests conf_pad_guests;

struct _conf_pad_guests {
u8 num_guests;
conf_pad_guest_device guests[CONF_PAD_MAX_ACTIVE];
} ATTRIBUTE_PACKED;

s32 CONF_Init(void);
Expand All @@ -164,6 +179,7 @@ s32 CONF_GetCounterBias(u32 *bias);
s32 CONF_GetScreenSaverMode(void);
s32 CONF_GetDisplayOffsetH(s8 *offset);
s32 CONF_GetPadDevices(conf_pads *pads);
s32 CONF_GetPadGuestDevices(conf_pad_guests *pads);
s32 CONF_GetNickName(u8 *nickname);
s32 CONF_GetAspectRatio(void);
s32 CONF_GetEULA(void);
Expand All @@ -174,6 +190,10 @@ s32 CONF_GetRegion(void);
s32 CONF_GetArea(void);
s32 CONF_GetVideo(void);

s32 CONF_SetPadDevices(const conf_pads *pads);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stupid minor change, but i'd put the Get & Set's together :)

s32 CONF_SetPadGuestDevices(const conf_pad_guests *pads);
s32 CONF_SaveChanges(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */
Expand Down
8 changes: 5 additions & 3 deletions gc/wiiuse/wiiuse.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,10 @@ typedef struct wiimote_t {
* @brief Wiimote listen structure.
*/
typedef struct wiimote_listen_t {
WCONST u8 name[0x40];
WCONST struct bd_addr bdaddr;
WCONST struct bte_pcb *sock;
WCONST struct wiimote_t *(*assign_cb)(struct bd_addr *bdaddr);
WCONST struct wiimote_t *(*assign_cb)(struct wiimote_listen_t *wml, u8 err);
WCONST struct wiimote_t *wm;
} wiimote_listen;
#endif
Expand Down Expand Up @@ -697,7 +698,8 @@ WIIUSE_EXPORT extern const char* wiiuse_version();
#ifndef GEKKO
WIIUSE_EXPORT extern struct wiimote_t** wiiuse_init(int wiimotes);
#else
WIIUSE_EXPORT extern int wiiuse_register(struct wiimote_listen_t *wml, struct bd_addr *bdaddr, struct wiimote_t *(*assign_cb)(struct bd_addr *bdaddr));
WIIUSE_EXPORT extern int wiiuse_register(struct wiimote_listen_t *wml, struct bd_addr *bdaddr, struct wiimote_t *(*assign_cb)(struct wiimote_listen_t *wml, u8 err));
WIIUSE_EXPORT extern int wiiuse_connect(struct wiimote_listen_t *wml, struct bd_addr *bdaddr, u8 *name, struct wiimote_t *(*assign_cb)(struct wiimote_listen_t *wml, u8 err));
WIIUSE_EXPORT extern struct wiimote_t** wiiuse_init(int wiimotes, wii_event_cb event_cb);
WIIUSE_EXPORT extern void wiiuse_sensorbar_enable(int enable);
#endif
Expand All @@ -721,7 +723,7 @@ WIIUSE_EXPORT extern int wiiuse_write_streamdata(struct wiimote_t *wm,ubyte *dat

/* connect.c */
WIIUSE_EXPORT extern int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout);
WIIUSE_EXPORT extern int wiiuse_connect(struct wiimote_t** wm, int wiimotes);
//WIIUSE_EXPORT extern int wiiuse_connect(struct wiimote_t** wm, int wiimotes);
WIIUSE_EXPORT extern void wiiuse_disconnect(struct wiimote_t* wm);

/* events.c */
Expand Down
15 changes: 13 additions & 2 deletions gc/wiiuse/wpad.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ enum {
WPAD_CHAN_2,
WPAD_CHAN_3,
WPAD_BALANCE_BOARD,
WPAD_MAX_WIIMOTES,
WPAD_CHAN_5,
WPAD_MAX_DEVICES,
};

// Compatibility with old apps
#define WPAD_MAX_WIIMOTES WPAD_MAX_DEVICES

#define WPAD_BUTTON_2 0x0001
#define WPAD_BUTTON_1 0x0002
Expand Down Expand Up @@ -166,6 +170,7 @@ typedef struct _wpad_encstatus

typedef void (*WPADDataCallback)(s32 chan, const WPADData *data);
typedef void (*WPADShutdownCallback)(s32 chan);
typedef void (*WPADSyncCallback)(u32 held);

s32 WPAD_Init(void);
s32 WPAD_ControlSpeaker(s32 chan,s32 enable);
Expand All @@ -182,10 +187,16 @@ s32 WPAD_SetEventBufs(s32 chan, WPADData *bufs, u32 cnt);
s32 WPAD_Disconnect(s32 chan);
s32 WPAD_IsSpeakerEnabled(s32 chan);
s32 WPAD_SendStreamData(s32 chan,void *buf,u32 len);
void WPAD_Shutdown(void);
s32 WPAD_Search(void);
s32 WPAD_StopSearch(void);
s32 WPAD_StartPairing(void);
s32 WPAD_WipeSavedControllers(void);
s32 WPAD_Shutdown(void);
void WPAD_SetIdleTimeout(u32 seconds);
void WPAD_SetPowerButtonCallback(WPADShutdownCallback cb);
void WPAD_SetBatteryDeadCallback(WPADShutdownCallback cb);
void WPAD_SetIdleTimeoutCallback(WPADShutdownCallback cb);
void WPAD_SetSyncButtonCallback(WPADSyncCallback cb);
s32 WPAD_ScanPads(void);
s32 WPAD_Rumble(s32 chan, int status);
s32 WPAD_SetIdleThresholds(s32 chan, s32 btns, s32 ir, s32 accel, s32 js, s32 wb, s32 mp);
Expand Down
104 changes: 104 additions & 0 deletions libogc/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ distribution.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <gcbool.h>
#include "ipc.h"
#include "asm.h"
#include "processor.h"
Expand All @@ -40,6 +41,7 @@ distribution.
static int __conf_inited = 0;
static u8 __conf_buffer[0x4000] ATTRIBUTE_ALIGN(32);
static char __conf_txt_buffer[0x101] ATTRIBUTE_ALIGN(32);
static int __conf_buffer_dirty = FALSE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use actual bool instead of the weird BOOL stuff


static const char __conf_file[] ATTRIBUTE_ALIGN(32) = "/shared2/sys/SYSCONF";
static const char __conf_txt_file[] ATTRIBUTE_ALIGN(32) = "/title/00000001/00000002/data/setting.txt";
Expand Down Expand Up @@ -207,6 +209,78 @@ s32 CONF_Get(const char *name, void *buffer, u32 length)
return len;
}

s32 CONF_Set(const char *name, const void *buffer, u32 length)
{
u8 *entry;
s32 len;
if(!__conf_inited) return CONF_ENOTINIT;

entry = __CONF_Find(name);
if(!entry) return CONF_ENOENT;

len = CONF_GetLength(name);
if(len<0) return len;
if(len!=length) return CONF_EBADVALUE;

switch(*entry>>5) {
case CONF_BIGARRAY:
memcpy(&entry[strlen(name)+3], buffer, len);
break;
case CONF_SMALLARRAY:
memcpy(&entry[strlen(name)+2], buffer, len);
break;
case CONF_BYTE:
case CONF_SHORT:
case CONF_LONG:
case CONF_BOOL:
memcpy(&entry[strlen(name)+1], buffer, len);
break;
default:
return CONF_ENOTIMPL;
}
__conf_buffer_dirty = TRUE;
return len;
}

int __CONF_WriteBuffer(void)
{
int ret, fd;

if (!__conf_inited)
return CONF_ENOTINIT;

if (!__conf_buffer_dirty)
return 0;

fd = IOS_Open(__conf_file, 2);
if (fd < 0)
return fd;

ret = IOS_Write(fd, __conf_buffer, 0x4000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would add a define with the value of 0x4000 and call it something like ConfFileSize. its just that the 0x4000 looks iffy at first, but upon closer inspection the file is always 0x4000 (or so wiibrew says).
so maybe the define + comment to explain would be good.
after that, maybe alter the reading too :D

IOS_Close(fd);
if (ret != 0x4000)
return CONF_EBADFILE;

__conf_buffer_dirty = FALSE;
return 0;
}

s32 CONF_SaveChanges(void)
{
s32 ret;
if (!__conf_inited)
return CONF_ENOTINIT;
ret = __CONF_WriteBuffer();
if (ret < 0)
return ret;

/*ret = __CONF_WriteTxtBuffer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in comments and not implemented, can be removed :)

if (ret < 0)
return ret;*/

return CONF_ERR_OK;
}

s32 CONF_GetShutdownMode(void)
{
u8 idleconf[2] = {0,0};
Expand Down Expand Up @@ -346,6 +420,36 @@ s32 CONF_GetPadDevices(conf_pads *pads)
return 0;
}

s32 CONF_SetPadDevices(const conf_pads *pads)
{
u8 count;

if (!pads) return CONF_EBADVALUE;
count = pads->num_registered;
if (count > CONF_PAD_MAX_REGISTERED) return CONF_EBADVALUE;
return CONF_Set("BT.DINF", pads, sizeof(conf_pads));
}

s32 CONF_GetPadGuestDevices(conf_pad_guests *pads)
{
int res;

res = CONF_Get("BT.CDIF", pads, sizeof(conf_pad_guests));
if(res < 0) return res;
if(res < sizeof(conf_pad_guests)) return CONF_EBADVALUE;
return 0;
}

s32 CONF_SetPadGuestDevices(const conf_pad_guests *pads)
{
u8 count;

if (!pads) return CONF_EBADVALUE;
count = pads->num_guests;
if (count > CONF_PAD_MAX_ACTIVE) return CONF_EBADVALUE;
return CONF_Set("BT.CDIF", pads, sizeof(conf_pad_guests));
}

s32 CONF_GetNickName(u8 *nickname)
{
int i, res;
Expand Down
Loading