root/libcwiid/cwiid_internal.h

Revision 1a6f4671cbdc7bd54db8046818012c5e3a20264b, 6.3 kB (checked in by L. Donnie Smith <donnie.smith@…>, 2 years ago)

Basic support for cwiid_listen

  • Property mode set to 100644
Line 
1/* Copyright (C) 2007 L. Donnie Smith <donnie.smith@gatech.edu>
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 */
18
19#ifndef CWIID_INTERNAL_H
20#define CWIID_INTERNAL_H
21
22#include <stdint.h>
23#include <pthread.h>
24#include <sys/types.h>  /* ssize_t */
25#include "cwiid.h"
26
27#define DEFAULT_TIMEOUT 5
28
29/* Bluetooth magic numbers */
30#define BT_TRANS_MASK           0xF0
31#define BT_TRANS_HANDSHAKE      0x00
32#define BT_TRANS_SET_REPORT     0x50
33#define BT_TRANS_DATA           0xA0
34#define BT_TRANS_DATAC          0xB0
35
36#define BT_PARAM_MASK           0x0F
37/* HANDSHAKE params */
38#define BT_PARAM_SUCCESSFUL                                     0x00
39#define BT_PARAM_NOT_READY                                      0x01
40#define BT_PARAM_ERR_INVALID_REPORT_ID          0x02
41#define BT_PARAM_ERR_UNSUPPORTED_REQUEST        0x03
42#define BT_PARAM_ERR_INVALID_PARAMETER          0x04
43#define BT_PARAM_ERR_UNKNOWN                            0x0E
44#define BT_PARAM_ERR_FATAL                                      0x0F
45/* SET_REPORT, DATA, DATAC params */
46#define BT_PARAM_INPUT          0x01
47#define BT_PARAM_OUTPUT         0x02
48#define BT_PARAM_FEATURE        0x03
49
50/* Wiimote specific magic numbers */
51#define WIIMOTE_NAME "Nintendo RVL-CNT-01"
52#define WIIBALANCE_NAME "Nintendo RVL-WBC-01"
53#define WIIMOTE_CLASS_0 0x04
54#define WIIMOTE_CLASS_1 0x25
55#define WIIMOTE_CLASS_2 0x00
56
57/* Wiimote port/channel/PSMs */
58#define CTL_PSM 17
59#define INT_PSM 19
60
61/* Report numbers */
62#define RPT_LED_RUMBLE                  0x11
63#define RPT_RPT_MODE                    0x12
64#define RPT_IR_ENABLE1                  0x13
65#define RPT_SPEAKER_ENABLE              0x14
66#define RPT_STATUS_REQ                  0x15
67#define RPT_WRITE                               0x16
68#define RPT_READ_REQ                    0x17
69#define RPT_SPEAKER_DATA                0x18
70#define RPT_SPEAKER_MUTE                0x19
71#define RPT_IR_ENABLE2                  0x1A
72#define RPT_STATUS                              0x20
73#define RPT_READ_DATA                   0x21
74#define RPT_WRITE_ACK                   0x22
75#define RPT_BTN                                 0x30
76#define RPT_BTN_ACC                             0x31
77#define RPT_BTN_EXT8                    0x32
78#define RPT_BTN_ACC_IR12                0x33
79#define RPT_BTN_EXT19                   0x34
80#define RPT_BTN_ACC_EXT16               0x35
81#define RPT_BTN_IR10_EXT9               0x36
82#define RPT_BTN_ACC_IR10_EXT6   0x37
83#define RPT_EXT21                               0x3D
84#define RPT_BTN_ACC_IR36_1              0x3E
85#define RPT_BTN_ACC_IR36_2              0x3F
86
87/* Button Mask (masks unknown bits in button bytes) */
88#define BTN_MASK_0                      0x1F
89#define BTN_MASK_1                      0x9F
90#define NUNCHUK_BTN_MASK        0x03
91
92/* Extension Values */
93#define EXT_NONE                0x2E2E
94#define EXT_PARTIAL             0xFFFF
95#define EXT_NUNCHUK             0x0000
96#define EXT_CLASSIC             0x0101
97#define EXT_BALANCE             0x0402
98#define EXT_MOTIONPLUS  0x0405
99
100/* IR Enable blocks */
101#define MARCAN_IR_BLOCK_1                       "\x00\x00\x00\x00\x00\x00\x90\x00\xC0"
102#define MARCAN_IR_BLOCK_2                       "\x40\x00"
103#define CLIFF_IR_BLOCK_1                        "\x02\x00\x00\x71\x01\x00\xAA\x00\x64"
104#define CLIFF_IR_BLOCK_2                        "\x63\x03"
105#define MAX_SENSITIVITY_IR_BLOCK_1      "\x00\x00\x00\x00\x00\x00\x90\x00\x41"
106#define MAX_SENSITIVITY_IR_BLOCK_2      "\x40\x00"
107#define WII_L1_IR_BLOCK_1                       "\x02\x00\x00\x71\x01\x00\x64\x00\xFE"
108#define WII_L1_IR_BLOCK_2                       "\xFD\x05"
109#define WII_L2_IR_BLOCK_1                       "\x02\x00\x00\x71\x01\x00\x96\x00\xB4"
110#define WII_L2_IR_BLOCK_2                       "\xB3\x04"
111#define WII_L3_IR_BLOCK_1                       "\x02\x00\x00\x71\x01\x00\xAA\x00\x64"
112#define WII_L3_IR_BLOCK_2                       "\x63\x03"
113#define WII_L4_IR_BLOCK_1                       "\x02\x00\x00\x71\x01\x00\xC8\x00\x36"
114#define WII_L4_IR_BLOCK_2                       "\x35\x03"
115#define WII_L5_IR_BLOCK_1                       "\x02\x00\x00\x71\x01\x00\x72\x00\x20"
116#define WII_L5_IR_BLOCK_2                       "\x1F\x03"
117
118/* Write Sequences */
119enum write_seq_type {
120        WRITE_SEQ_RPT,
121        WRITE_SEQ_MEM
122};
123
124struct write_seq {
125        enum write_seq_type type;
126        uint32_t report_offset;
127        const void *data;
128        uint16_t len;
129        uint8_t flags;
130};
131
132#define SEQ_LEN(seq) (sizeof(seq)/sizeof(struct write_seq))
133
134/* Message arrays */
135struct mesg_array {
136        uint8_t count;
137        struct timespec timestamp;
138        union cwiid_mesg array[CWIID_MAX_MESG_COUNT];
139};
140
141/* RW State/Mesg */
142enum rw_status {
143        RW_IDLE,
144        RW_READ,
145        RW_WRITE,
146        RW_CANCEL
147};
148
149struct rw_mesg {
150        enum rw_status type;
151        uint8_t error;
152        uint32_t offset;
153        uint8_t len;
154        char data[16];
155};
156
157/* Wiimote struct */
158struct wiimote {
159        int flags;
160        int ctl_socket;
161        int int_socket;
162        pthread_t router_thread;
163        pthread_t status_thread;
164        pthread_t mesg_callback_thread;
165        int mesg_pipe[2];
166        int status_pipe[2];
167        int rw_pipe[2];
168        struct cwiid_state state;
169        enum rw_status rw_status;
170        cwiid_mesg_callback_t *mesg_callback;
171        pthread_mutex_t state_mutex;
172        pthread_mutex_t rw_mutex;
173        pthread_mutex_t rpt_mutex;
174        int id;
175        const void *data;
176};
177
178/* prototypes */
179cwiid_wiimote_t *cwiid_new(int ctl_socket, int int_socket, int flags);
180
181/* thread.c */
182void *router_thread(struct wiimote *wiimote);
183void *status_thread(struct wiimote *wiimote);
184void *mesg_callback_thread(struct wiimote *wiimote);
185
186/* util.c */
187void cwiid_err(struct wiimote *wiimote, const char *str, ...);
188int verify_handshake(struct wiimote *wiimote);
189int exec_write_seq(struct wiimote *wiimote, unsigned int len,
190                   struct write_seq *seq);
191int full_read(int fd, void *buf, size_t len);
192int write_mesg_array(struct wiimote *wiimote, struct mesg_array *ma);
193int read_mesg_array(int fd, struct mesg_array *ma);
194int cancel_rw(struct wiimote *wiimote);
195int cancel_mesg_callback(struct wiimote *wiimote);
196
197/* process.c */
198int process_error(struct wiimote *, ssize_t, struct mesg_array *);
199int process_status(struct wiimote *, const unsigned char *,
200                   struct mesg_array *);
201int process_btn(struct wiimote *, const unsigned char *, struct mesg_array *);
202int process_acc(struct wiimote *, const unsigned char *, struct mesg_array *);
203int process_ir10(struct wiimote *, const unsigned char *, struct mesg_array *);
204int process_ir12(struct wiimote *, const unsigned char *, struct mesg_array *);
205int process_ext(struct wiimote *, unsigned char *, unsigned char,
206                struct mesg_array *);
207int process_read(struct wiimote *, unsigned char *);
208int process_write(struct wiimote *, unsigned char *);
209
210/* state.c */
211int update_state(struct wiimote *wiimote, struct mesg_array *ma);
212int update_rpt_mode(struct wiimote *wiimote, int8_t rpt_mode);
213
214#endif
Note: See TracBrowser for help on using the browser.