| 1 |
/* Copyright (C) 2007 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 2 |
* |
|---|
| 3 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 4 |
* it under the terms of the GNU General Public License as published by |
|---|
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 6 |
* (at your option) any later version. |
|---|
| 7 |
* |
|---|
| 8 |
* This program is distributed in the hope that it will be useful, |
|---|
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 |
* GNU General Public License for more details. |
|---|
| 12 |
* |
|---|
| 13 |
* You should have received a copy of the GNU General Public License |
|---|
| 14 |
* along with this program; if not, write to the Free Software |
|---|
| 15 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 16 |
* |
|---|
| 17 |
* ChangeLog: |
|---|
| 18 |
* 2007-05-16 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 19 |
* * remove error_pipe |
|---|
| 20 |
* * add struct mesg_array to process_error |
|---|
| 21 |
* |
|---|
| 22 |
* 2007-05-14 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 23 |
* * added timestamp to mesg_array |
|---|
| 24 |
* |
|---|
| 25 |
* 2007-04-24 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 26 |
* * rewrite for API overhaul |
|---|
| 27 |
* |
|---|
| 28 |
* 2007-04-09 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 29 |
* * renamed wiimote to libcwiid, renamed structures accordingly |
|---|
| 30 |
* |
|---|
| 31 |
* 2007-04-04 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 32 |
* * moved RW error state to separate member |
|---|
| 33 |
* |
|---|
| 34 |
* 2007-04-01 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 35 |
* * removed CWIID_CMP_LEN macro and cwiid_findfirst prototype |
|---|
| 36 |
* |
|---|
| 37 |
* 2007-03-05 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 38 |
* * added wiimote parameter to cwiid_err prototype |
|---|
| 39 |
* |
|---|
| 40 |
* 2007-03-01 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 41 |
* * Initial ChangeLog |
|---|
| 42 |
* * type audit (stdint, const, char booleans) |
|---|
| 43 |
*/ |
|---|
| 44 |
|
|---|
| 45 |
#ifndef CWIID_INTERNAL_H |
|---|
| 46 |
#define CWIID_INTERNAL_H |
|---|
| 47 |
|
|---|
| 48 |
#include <stdint.h> |
|---|
| 49 |
#include <pthread.h> |
|---|
| 50 |
#include <sys/types.h> /* ssize_t */ |
|---|
| 51 |
#include "cwiid.h" |
|---|
| 52 |
|
|---|
| 53 |
/* Bluetooth magic numbers */ |
|---|
| 54 |
#define BT_TRANS_MASK 0xF0 |
|---|
| 55 |
#define BT_TRANS_HANDSHAKE 0x00 |
|---|
| 56 |
#define BT_TRANS_SET_REPORT 0x50 |
|---|
| 57 |
#define BT_TRANS_DATA 0xA0 |
|---|
| 58 |
#define BT_TRANS_DATAC 0xB0 |
|---|
| 59 |
|
|---|
| 60 |
#define BT_PARAM_MASK 0x0F |
|---|
| 61 |
/* HANDSHAKE params */ |
|---|
| 62 |
#define BT_PARAM_SUCCESSFUL 0x00 |
|---|
| 63 |
#define BT_PARAM_NOT_READY 0x01 |
|---|
| 64 |
#define BT_PARAM_ERR_INVALID_REPORT_ID 0x02 |
|---|
| 65 |
#define BT_PARAM_ERR_UNSUPPORTED_REQUEST 0x03 |
|---|
| 66 |
#define BT_PARAM_ERR_INVALID_PARAMETER 0x04 |
|---|
| 67 |
#define BT_PARAM_ERR_UNKNOWN 0x0E |
|---|
| 68 |
#define BT_PARAM_ERR_FATAL 0x0F |
|---|
| 69 |
/* SET_REPORT, DATA, DATAC params */ |
|---|
| 70 |
#define BT_PARAM_INPUT 0x01 |
|---|
| 71 |
#define BT_PARAM_OUTPUT 0x02 |
|---|
| 72 |
#define BT_PARAM_FEATURE 0x03 |
|---|
| 73 |
|
|---|
| 74 |
/* Wiimote specific magic numbers */ |
|---|
| 75 |
#define WIIMOTE_NAME "Nintendo RVL-CNT-01" |
|---|
| 76 |
#define WIIMOTE_CLASS_0 0x04 |
|---|
| 77 |
#define WIIMOTE_CLASS_1 0x25 |
|---|
| 78 |
#define WIIMOTE_CLASS_2 0x00 |
|---|
| 79 |
|
|---|
| 80 |
/* Wiimote port/channel/PSMs */ |
|---|
| 81 |
#define CTL_PSM 17 |
|---|
| 82 |
#define INT_PSM 19 |
|---|
| 83 |
|
|---|
| 84 |
/* Report numbers */ |
|---|
| 85 |
#define RPT_LED_RUMBLE 0x11 |
|---|
| 86 |
#define RPT_RPT_MODE 0x12 |
|---|
| 87 |
#define RPT_IR_ENABLE1 0x13 |
|---|
| 88 |
#define RPT_SPEAKER_ENABLE 0x14 |
|---|
| 89 |
#define RPT_STATUS_REQ 0x15 |
|---|
| 90 |
#define RPT_WRITE 0x16 |
|---|
| 91 |
#define RPT_READ_REQ 0x17 |
|---|
| 92 |
#define RPT_SPEAKER_DATA 0x18 |
|---|
| 93 |
#define RPT_SPEAKER_MUTE 0x19 |
|---|
| 94 |
#define RPT_IR_ENABLE2 0x1A |
|---|
| 95 |
#define RPT_STATUS 0x20 |
|---|
| 96 |
#define RPT_READ_DATA 0x21 |
|---|
| 97 |
#define RPT_WRITE_ACK 0x22 |
|---|
| 98 |
#define RPT_BTN 0x30 |
|---|
| 99 |
#define RPT_BTN_ACC 0x31 |
|---|
| 100 |
#define RPT_BTN_EXT8 0x32 |
|---|
| 101 |
#define RPT_BTN_ACC_IR12 0x33 |
|---|
| 102 |
#define RPT_BTN_EXT19 0x34 |
|---|
| 103 |
#define RPT_BTN_ACC_EXT16 0x35 |
|---|
| 104 |
#define RPT_BTN_IR10_EXT9 0x36 |
|---|
| 105 |
#define RPT_BTN_ACC_IR10_EXT6 0x37 |
|---|
| 106 |
#define RPT_EXT21 0x3D |
|---|
| 107 |
#define RPT_BTN_ACC_IR36_1 0x3E |
|---|
| 108 |
#define RPT_BTN_ACC_IR36_2 0x3F |
|---|
| 109 |
|
|---|
| 110 |
/* Button Mask (masks unknown bits in button bytes) */ |
|---|
| 111 |
#define BTN_MASK_0 0x1F |
|---|
| 112 |
#define BTN_MASK_1 0x9F |
|---|
| 113 |
#define NUNCHUK_BTN_MASK 0x03 |
|---|
| 114 |
|
|---|
| 115 |
/* Extension Values */ |
|---|
| 116 |
#define EXT_NONE 0x2E |
|---|
| 117 |
#define EXT_PARTIAL 0xFF |
|---|
| 118 |
#define EXT_NUNCHUK 0x00 |
|---|
| 119 |
#define EXT_CLASSIC 0x01 |
|---|
| 120 |
|
|---|
| 121 |
/* IR Enable blocks */ |
|---|
| 122 |
#define MARCAN_IR_BLOCK_1 "\x00\x00\x00\x00\x00\x00\x90\x00\xC0" |
|---|
| 123 |
#define MARCAN_IR_BLOCK_2 "\x40\x00" |
|---|
| 124 |
#define CLIFF_IR_BLOCK_1 "\x02\x00\x00\x71\x01\x00\xAA\x00\x64" |
|---|
| 125 |
#define CLIFF_IR_BLOCK_2 "\x63\x03" |
|---|
| 126 |
|
|---|
| 127 |
/* Extension Decode */ |
|---|
| 128 |
#define DECODE(a) (((a ^ 0x17)+0x17)&0xFF) |
|---|
| 129 |
|
|---|
| 130 |
/* Write Sequences */ |
|---|
| 131 |
enum write_seq_type { |
|---|
| 132 |
WRITE_SEQ_RPT, |
|---|
| 133 |
WRITE_SEQ_MEM |
|---|
| 134 |
}; |
|---|
| 135 |
|
|---|
| 136 |
/* send_report flags */ |
|---|
| 137 |
#define SEND_RPT_NO_RUMBLE 0x01 |
|---|
| 138 |
|
|---|
| 139 |
struct write_seq { |
|---|
| 140 |
enum write_seq_type type; |
|---|
| 141 |
uint32_t report_offset; |
|---|
| 142 |
const void *data; |
|---|
| 143 |
uint16_t len; |
|---|
| 144 |
uint8_t flags; |
|---|
| 145 |
}; |
|---|
| 146 |
|
|---|
| 147 |
#define SEQ_LEN(seq) (sizeof(seq)/sizeof(struct write_seq)) |
|---|
| 148 |
|
|---|
| 149 |
/* Message arrays */ |
|---|
| 150 |
struct mesg_array { |
|---|
| 151 |
uint8_t count; |
|---|
| 152 |
struct timespec timestamp; |
|---|
| 153 |
union cwiid_mesg array[CWIID_MAX_MESG_COUNT]; |
|---|
| 154 |
}; |
|---|
| 155 |
|
|---|
| 156 |
/* RW State/Mesg */ |
|---|
| 157 |
enum rw_status { |
|---|
| 158 |
RW_IDLE, |
|---|
| 159 |
RW_READ, |
|---|
| 160 |
RW_WRITE, |
|---|
| 161 |
RW_CANCEL |
|---|
| 162 |
}; |
|---|
| 163 |
|
|---|
| 164 |
struct rw_mesg { |
|---|
| 165 |
enum rw_status type; |
|---|
| 166 |
uint8_t error; |
|---|
| 167 |
uint32_t offset; |
|---|
| 168 |
uint8_t len; |
|---|
| 169 |
char data[16]; |
|---|
| 170 |
}; |
|---|
| 171 |
|
|---|
| 172 |
/* Wiimote struct */ |
|---|
| 173 |
struct wiimote { |
|---|
| 174 |
int flags; |
|---|
| 175 |
int ctl_socket; |
|---|
| 176 |
int int_socket; |
|---|
| 177 |
pthread_t router_thread; |
|---|
| 178 |
pthread_t status_thread; |
|---|
| 179 |
pthread_t mesg_callback_thread; |
|---|
| 180 |
int mesg_pipe[2]; |
|---|
| 181 |
int status_pipe[2]; |
|---|
| 182 |
int rw_pipe[2]; |
|---|
| 183 |
struct cwiid_state state; |
|---|
| 184 |
enum rw_status rw_status; |
|---|
| 185 |
cwiid_mesg_callback_t *mesg_callback; |
|---|
| 186 |
pthread_mutex_t state_mutex; |
|---|
| 187 |
pthread_mutex_t rw_mutex; |
|---|
| 188 |
pthread_mutex_t rpt_mutex; |
|---|
| 189 |
int id; |
|---|
| 190 |
const void *data; |
|---|
| 191 |
}; |
|---|
| 192 |
|
|---|
| 193 |
/* prototypes */ |
|---|
| 194 |
/* thread.c */ |
|---|
| 195 |
void *router_thread(struct wiimote *wiimote); |
|---|
| 196 |
void *status_thread(struct wiimote *wiimote); |
|---|
| 197 |
void *mesg_callback_thread(struct wiimote *wiimote); |
|---|
| 198 |
|
|---|
| 199 |
/* util.c */ |
|---|
| 200 |
void cwiid_err(struct wiimote *wiimote, const char *str, ...); |
|---|
| 201 |
int verify_handshake(struct wiimote *wiimote); |
|---|
| 202 |
int send_report(struct wiimote *wiimote, uint8_t flags, uint8_t report, |
|---|
| 203 |
size_t len, const void *data); |
|---|
| 204 |
int exec_write_seq(struct wiimote *wiimote, unsigned int len, |
|---|
| 205 |
struct write_seq *seq); |
|---|
| 206 |
int full_read(int fd, void *buf, size_t len); |
|---|
| 207 |
int write_mesg_array(struct wiimote *wiimote, struct mesg_array *ma); |
|---|
| 208 |
int read_mesg_array(int fd, struct mesg_array *ma); |
|---|
| 209 |
int cancel_rw(struct wiimote *wiimote); |
|---|
| 210 |
int cancel_mesg_callback(struct wiimote *wiimote); |
|---|
| 211 |
|
|---|
| 212 |
/* process.c */ |
|---|
| 213 |
int process_error(struct wiimote *, ssize_t, struct mesg_array *); |
|---|
| 214 |
int process_status(struct wiimote *, const unsigned char *, |
|---|
| 215 |
struct mesg_array *); |
|---|
| 216 |
int process_btn(struct wiimote *, const unsigned char *, struct mesg_array *); |
|---|
| 217 |
int process_acc(struct wiimote *, const unsigned char *, struct mesg_array *); |
|---|
| 218 |
int process_ir10(struct wiimote *, const unsigned char *, struct mesg_array *); |
|---|
| 219 |
int process_ir12(struct wiimote *, const unsigned char *, struct mesg_array *); |
|---|
| 220 |
int process_ext(struct wiimote *, unsigned char *, unsigned char, |
|---|
| 221 |
struct mesg_array *); |
|---|
| 222 |
int process_read(struct wiimote *, unsigned char *); |
|---|
| 223 |
int process_write(struct wiimote *, unsigned char *); |
|---|
| 224 |
|
|---|
| 225 |
/* state.c */ |
|---|
| 226 |
int update_state(struct wiimote *wiimote, struct mesg_array *ma); |
|---|
| 227 |
int update_rpt_mode(struct wiimote *wiimote, int8_t rpt_mode); |
|---|
| 228 |
|
|---|
| 229 |
#endif |
|---|