| 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 | * 04/01/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 19 | * * removed WIIMOTE_CMP_LEN macro and wiimote_findfirst prototype |
|---|
| 20 | * |
|---|
| 21 | * 03/05/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 22 | * * added wiimote parameter to wiimote_err prototype |
|---|
| 23 | * |
|---|
| 24 | * 03/01/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 25 | * * Initial ChangeLog |
|---|
| 26 | * * type audit (stdint, const, char booleans) |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #ifndef WIIMOTE_INTERNAL_H |
|---|
| 30 | #define WIIMOTE_INTERNAL_H |
|---|
| 31 | |
|---|
| 32 | #include <stdint.h> |
|---|
| 33 | #include <pthread.h> |
|---|
| 34 | #include "wiimote.h" |
|---|
| 35 | |
|---|
| 36 | /* Bluetooth magic numbers */ |
|---|
| 37 | #define BT_TRANS_MASK 0xF0 |
|---|
| 38 | #define BT_TRANS_HANDSHAKE 0x00 |
|---|
| 39 | #define BT_TRANS_SET_REPORT 0x50 |
|---|
| 40 | #define BT_TRANS_DATA 0xA0 |
|---|
| 41 | #define BT_TRANS_DATAC 0xB0 |
|---|
| 42 | |
|---|
| 43 | #define BT_PARAM_MASK 0x0F |
|---|
| 44 | /* HANDSHAKE params */ |
|---|
| 45 | #define BT_PARAM_SUCCESSFUL 0x00 |
|---|
| 46 | #define BT_PARAM_NOT_READY 0x01 |
|---|
| 47 | #define BT_PARAM_ERR_INVALID_REPORT_ID 0x02 |
|---|
| 48 | #define BT_PARAM_ERR_UNSUPPORTED_REQUEST 0x03 |
|---|
| 49 | #define BT_PARAM_ERR_INVALID_PARAMETER 0x04 |
|---|
| 50 | #define BT_PARAM_ERR_UNKNOWN 0x0E |
|---|
| 51 | #define BT_PARAM_ERR_FATAL 0x0F |
|---|
| 52 | /* SET_REPORT, DATA, DATAC params */ |
|---|
| 53 | #define BT_PARAM_INPUT 0x01 |
|---|
| 54 | #define BT_PARAM_OUTPUT 0x02 |
|---|
| 55 | #define BT_PARAM_FEATURE 0x03 |
|---|
| 56 | |
|---|
| 57 | /* Wiimote specific magic numbers */ |
|---|
| 58 | #define WIIMOTE_NAME "Nintendo RVL-CNT-01" |
|---|
| 59 | #define WIIMOTE_CLASS_0 0x04 |
|---|
| 60 | #define WIIMOTE_CLASS_1 0x25 |
|---|
| 61 | #define WIIMOTE_CLASS_2 0x00 |
|---|
| 62 | |
|---|
| 63 | /* Wiimote port/channel/PSMs */ |
|---|
| 64 | #define CTL_PSM 17 |
|---|
| 65 | #define INT_PSM 19 |
|---|
| 66 | |
|---|
| 67 | /* Report numbers */ |
|---|
| 68 | #define RPT_LED_RUMBLE 0x11 |
|---|
| 69 | #define RPT_RPT_MODE 0x12 |
|---|
| 70 | #define RPT_IR_ENABLE1 0x13 |
|---|
| 71 | #define RPT_SPEAKER_ENABLE 0x14 |
|---|
| 72 | #define RPT_STATUS_REQ 0x15 |
|---|
| 73 | #define RPT_WRITE 0x16 |
|---|
| 74 | #define RPT_READ_REQ 0x17 |
|---|
| 75 | #define RPT_SPEAKER_DATA 0x18 |
|---|
| 76 | #define RPT_SPEAKER_MUTE 0x19 |
|---|
| 77 | #define RPT_IR_ENABLE2 0x1A |
|---|
| 78 | #define RPT_STATUS 0x20 |
|---|
| 79 | #define RPT_READ_DATA 0x21 |
|---|
| 80 | #define RPT_WRITE_ACK 0x22 |
|---|
| 81 | #define RPT_BTN 0x30 |
|---|
| 82 | #define RPT_BTN_ACC 0x31 |
|---|
| 83 | #define RPT_BTN_EXT8 0x32 |
|---|
| 84 | #define RPT_BTN_ACC_IR12 0x33 |
|---|
| 85 | #define RPT_BTN_EXT19 0x34 |
|---|
| 86 | #define RPT_BTN_ACC_EXT16 0x35 |
|---|
| 87 | #define RPT_BTN_IR10_EXT9 0x36 |
|---|
| 88 | #define RPT_BTN_ACC_IR10_EXT6 0x37 |
|---|
| 89 | #define RPT_EXT21 0x3D |
|---|
| 90 | #define RPT_BTN_ACC_IR36_1 0x3E |
|---|
| 91 | #define RPT_BTN_ACC_IR36_2 0x3F |
|---|
| 92 | |
|---|
| 93 | /* Button Mask (masks unknown bits in button bytes) */ |
|---|
| 94 | #define BTN_MASK_0 0x1F |
|---|
| 95 | #define BTN_MASK_1 0x9F |
|---|
| 96 | #define NUNCHUK_BTN_MASK 0x03 |
|---|
| 97 | |
|---|
| 98 | /* Extension Values */ |
|---|
| 99 | #define EXT_NONE 0x2E |
|---|
| 100 | #define EXT_PARTIAL 0xFF |
|---|
| 101 | #define EXT_NUNCHUK 0x00 |
|---|
| 102 | #define EXT_CLASSIC 0x01 |
|---|
| 103 | |
|---|
| 104 | /* IR Enable blocks */ |
|---|
| 105 | #define MARCAN_IR_BLOCK_1 "\x00\x00\x00\x00\x00\x00\x90\x00\xC0" |
|---|
| 106 | #define MARCAN_IR_BLOCK_2 "\x40\x00" |
|---|
| 107 | #define CLIFF_IR_BLOCK_1 "\x02\x00\x00\x71\x01\x00\xAA\x00\x64" |
|---|
| 108 | #define CLIFF_IR_BLOCK_2 "\x63\x03" |
|---|
| 109 | |
|---|
| 110 | /* Extension Decode */ |
|---|
| 111 | #define DECODE(a) (((a ^ 0x17)+0x17)&0xFF) |
|---|
| 112 | |
|---|
| 113 | enum write_seq_type { |
|---|
| 114 | WRITE_SEQ_RPT, |
|---|
| 115 | WRITE_SEQ_MEM |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | /* send_report flags */ |
|---|
| 119 | #define SEND_RPT_NO_RUMBLE 0x01 |
|---|
| 120 | |
|---|
| 121 | struct write_seq { |
|---|
| 122 | enum write_seq_type type; |
|---|
| 123 | uint32_t report_offset; |
|---|
| 124 | const void *data; |
|---|
| 125 | uint16_t len; |
|---|
| 126 | uint8_t flags; |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | #define SEQ_LEN(seq) (sizeof(seq)/sizeof(struct write_seq)) |
|---|
| 130 | |
|---|
| 131 | enum rw_status { |
|---|
| 132 | RW_NONE, |
|---|
| 133 | RW_PENDING, |
|---|
| 134 | RW_READY, |
|---|
| 135 | RW_ERROR |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | struct mesg_array { |
|---|
| 139 | int count; |
|---|
| 140 | union wiimote_mesg *mesg[WIIMOTE_MAX_MESG_COUNT]; |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | struct wiimote { |
|---|
| 144 | int id; |
|---|
| 145 | int ctl_socket; |
|---|
| 146 | int int_socket; |
|---|
| 147 | uint8_t led_rumble_state; |
|---|
| 148 | uint8_t rpt_mode_flags; |
|---|
| 149 | uint16_t buttons; |
|---|
| 150 | enum wiimote_ext_type extension; |
|---|
| 151 | wiimote_mesg_callback_t *mesg_callback; |
|---|
| 152 | pthread_t int_listen_thread; |
|---|
| 153 | pthread_t dispatch_thread; |
|---|
| 154 | struct queue *dispatch_queue; |
|---|
| 155 | pthread_mutex_t wiimote_mutex; |
|---|
| 156 | pthread_mutex_t rw_mutex; |
|---|
| 157 | pthread_cond_t rw_cond; |
|---|
| 158 | pthread_mutex_t rw_cond_mutex; |
|---|
| 159 | enum rw_status rw_status; |
|---|
| 160 | void *read_buf; |
|---|
| 161 | uint16_t read_len; |
|---|
| 162 | uint16_t read_received; |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | /* prototypes */ |
|---|
| 166 | void *int_listen(struct wiimote *wiimote); |
|---|
| 167 | void *dispatch(struct wiimote *wiimote); |
|---|
| 168 | |
|---|
| 169 | int update_rpt_mode(struct wiimote *wiimote, int8_t flags); |
|---|
| 170 | |
|---|
| 171 | void wiimote_err(struct wiimote *wiimote, const char *str, ...); |
|---|
| 172 | int verify_handshake(struct wiimote *wiimote); |
|---|
| 173 | int send_report(struct wiimote *wiimote, uint8_t flags, uint8_t report, |
|---|
| 174 | size_t len, const void *data); |
|---|
| 175 | int exec_write_seq(struct wiimote *wiimote, unsigned int len, |
|---|
| 176 | struct write_seq *seq); |
|---|
| 177 | void free_mesg_array(struct mesg_array *array); |
|---|
| 178 | |
|---|
| 179 | #endif |
|---|
| 180 | |
|---|