| 94 | | static int process_status(struct wiimote *wiimote, const unsigned char *data, |
| 95 | | struct mesg_array *mesg_array) |
| 96 | | { |
| 97 | | struct wiimote_status_mesg *mesg; |
| 98 | | |
| 99 | | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| 100 | | wiimote_err(wiimote, "Error allocating status mesg"); |
| 101 | | } |
| 102 | | mesg->type = WIIMOTE_MESG_STATUS; |
| 103 | | mesg->battery = data[5]; |
| 104 | | if (data[2] & 0x02) { |
| 105 | | /* dispatch will figure out what it is */ |
| 106 | | mesg->extension = WIIMOTE_EXT_UNKNOWN; |
| 107 | | } |
| 108 | | else { |
| 109 | | mesg->extension = WIIMOTE_EXT_NONE; |
| 110 | | } |
| 111 | | |
| 112 | | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| 113 | | mesg_array->count++; |
| 114 | | |
| 115 | | return 0; |
| 116 | | } |
| 117 | | |
| 118 | | static int process_btn(struct wiimote *wiimote, const unsigned char *data, |
| 119 | | struct mesg_array *mesg_array) |
| 120 | | { |
| 121 | | struct wiimote_btn_mesg *mesg; |
| 122 | | uint16_t buttons; |
| 123 | | |
| 124 | | buttons = (data[0] & BTN_MASK_0)<<8 | |
| 125 | | (data[1] & BTN_MASK_1); |
| 126 | | if (wiimote->buttons != buttons) { |
| 127 | | wiimote->buttons = buttons; |
| 128 | | |
| 129 | | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_BTN) { |
| 130 | | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| 131 | | wiimote_err(wiimote, "Error allocating btn mesg"); |
| 132 | | return -1; |
| 133 | | } |
| 134 | | mesg->type = WIIMOTE_MESG_BTN; |
| 135 | | mesg->buttons = buttons; |
| 136 | | |
| 137 | | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| 138 | | mesg_array->count++; |
| 139 | | } |
| 140 | | } |
| 141 | | |
| 142 | | return 0; |
| 143 | | } |
| 144 | | |
| 145 | | static int process_acc(struct wiimote *wiimote, const unsigned char *data, |
| 146 | | struct mesg_array *mesg_array) |
| 147 | | { |
| 148 | | struct wiimote_acc_mesg *mesg; |
| 149 | | |
| 150 | | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_ACC) { |
| 151 | | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| 152 | | wiimote_err(wiimote, "Error allocating acc mesg"); |
| 153 | | return -1; |
| 154 | | } |
| 155 | | mesg->type = WIIMOTE_MESG_ACC; |
| 156 | | mesg->x = data[0]; |
| 157 | | mesg->y = data[1]; |
| 158 | | mesg->z = data[2]; |
| 159 | | |
| 160 | | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| 161 | | mesg_array->count++; |
| 162 | | } |
| 163 | | |
| 164 | | return 0; |
| 165 | | } |
| 166 | | |
| 167 | | static int process_ir10(struct wiimote *wiimote, const unsigned char *data, |
| 168 | | struct mesg_array *mesg_array) |
| 169 | | { |
| 170 | | struct wiimote_ir_mesg *mesg; |
| 171 | | int i; |
| 172 | | const unsigned char *block; |
| 173 | | |
| 174 | | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_IR) { |
| 175 | | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| 176 | | wiimote_err(wiimote, "Error allocating ir mesg"); |
| 177 | | return -1; |
| 178 | | } |
| 179 | | mesg->type = WIIMOTE_MESG_IR; |
| 180 | | |
| 181 | | for (i=0, block=data; i < WIIMOTE_IR_SRC_COUNT; i+=2, block+=5) { |
| 182 | | if (block[0] == 0xFF) { |
| 183 | | mesg->src[i].valid = 0; |
| 184 | | } |
| 185 | | else { |
| 186 | | mesg->src[i].valid = 1; |
| 187 | | mesg->src[i].x = ((uint16_t)block[2] & 0x30)<<4 | |
| 188 | | (uint16_t)block[0]; |
| 189 | | mesg->src[i].y = ((uint16_t)block[2] & 0xC0)<<2 | |
| 190 | | (uint16_t)block[1]; |
| 191 | | mesg->src[i].size = -1; |
| 192 | | } |
| 193 | | |
| 194 | | if (block[3] == 0xFF) { |
| 195 | | mesg->src[i+1].valid = 0; |
| 196 | | } |
| 197 | | else { |
| 198 | | mesg->src[i+1].valid = 1; |
| 199 | | mesg->src[i+1].x = ((uint16_t)block[2] & 0x03)<<8 | |
| 200 | | (uint16_t)block[3]; |
| 201 | | mesg->src[i+1].y = ((uint16_t)block[2] & 0x0C)<<6 | |
| 202 | | (uint16_t)block[4]; |
| 203 | | mesg->src[i+1].size = -1; |
| 204 | | } |
| 205 | | } |
| 206 | | |
| 207 | | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| 208 | | mesg_array->count++; |
| 209 | | } |
| 210 | | |
| 211 | | return 0; |
| 212 | | } |
| 213 | | |
| 214 | | static int process_ir12(struct wiimote *wiimote, const unsigned char *data, |
| 215 | | struct mesg_array *mesg_array) |
| 216 | | { |
| 217 | | struct wiimote_ir_mesg *mesg; |
| 218 | | int i; |
| 219 | | const unsigned char *block; |
| 220 | | |
| 221 | | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_IR) { |
| 222 | | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| 223 | | wiimote_err(wiimote, "Error allocating ir mesg"); |
| 224 | | return -1; |
| 225 | | } |
| 226 | | mesg->type = WIIMOTE_MESG_IR; |
| 227 | | |
| 228 | | for (i=0, block=data; i < WIIMOTE_IR_SRC_COUNT; i++, block+=3) { |
| 229 | | if (block[0] == 0xFF) { |
| 230 | | mesg->src[i].valid = 0; |
| 231 | | } |
| 232 | | else { |
| 233 | | mesg->src[i].valid = 1; |
| 234 | | mesg->src[i].x = ((uint16_t)block[2] & 0x30)<<4 | |
| 235 | | (uint16_t)block[0]; |
| 236 | | mesg->src[i].y = ((uint16_t)block[2] & 0xC0)<<2 | |
| 237 | | (uint16_t)block[1]; |
| 238 | | mesg->src[i].size = block[2] & 0x0F; |
| 239 | | } |
| 240 | | } |
| 241 | | |
| 242 | | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| 243 | | mesg_array->count++; |
| 244 | | } |
| 245 | | |
| 246 | | return 0; |
| 247 | | } |
| 248 | | |
| 249 | | static int process_ext(struct wiimote *wiimote, unsigned char *data, |
| 250 | | unsigned char len, struct mesg_array *mesg_array) |
| 251 | | { |
| 252 | | struct wiimote_nunchuk_mesg *nunchuk_mesg; |
| 253 | | struct wiimote_classic_mesg *classic_mesg; |
| 254 | | int i; |
| 255 | | |
| 256 | | switch (wiimote->extension) { |
| 257 | | case WIIMOTE_EXT_NONE: |
| 258 | | wiimote_err(wiimote, |
| 259 | | "Extension report received with no extension present"); |
| 260 | | break; |
| 261 | | case WIIMOTE_EXT_UNKNOWN: |
| 262 | | break; |
| 263 | | case WIIMOTE_EXT_NUNCHUK: |
| 264 | | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_NUNCHUK) { |
| 265 | | if ((nunchuk_mesg = malloc(sizeof *nunchuk_mesg)) == NULL) { |
| 266 | | wiimote_err(wiimote, "Error allocating nunchuk mesg"); |
| 267 | | return -1; |
| 268 | | } |
| 269 | | |
| 270 | | nunchuk_mesg->type = WIIMOTE_MESG_NUNCHUK; |
| 271 | | nunchuk_mesg->stick_x = DECODE(data[0]); |
| 272 | | nunchuk_mesg->stick_y = DECODE(data[1]); |
| 273 | | nunchuk_mesg->acc_x = DECODE(data[2]); |
| 274 | | nunchuk_mesg->acc_y = DECODE(data[3]); |
| 275 | | nunchuk_mesg->acc_z = DECODE(data[4]); |
| 276 | | nunchuk_mesg->buttons = ~DECODE(data[5]) & NUNCHUK_BTN_MASK; |
| 277 | | |
| 278 | | mesg_array->mesg[mesg_array->count] = |
| 279 | | (union wiimote_mesg *)nunchuk_mesg; |
| 280 | | mesg_array->count++; |
| 281 | | } |
| 282 | | break; |
| 283 | | case WIIMOTE_EXT_CLASSIC: |
| 284 | | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_CLASSIC) { |
| 285 | | if ((classic_mesg = malloc(sizeof *classic_mesg)) == NULL) { |
| 286 | | wiimote_err(wiimote, "Error allocating classic mesg"); |
| 287 | | return -1; |
| 288 | | } |
| 289 | | |
| 290 | | for (i=0; i < 6; i++) { |
| 291 | | data[i] = DECODE(data[i]); |
| 292 | | } |
| 293 | | |
| 294 | | classic_mesg->type = WIIMOTE_MESG_CLASSIC; |
| 295 | | classic_mesg->l_stick_x = data[0] & 0x3F; |
| 296 | | classic_mesg->l_stick_y = data[1] & 0x3F; |
| 297 | | classic_mesg->r_stick_x = (data[0] & 0xC0)>>3 | |
| 298 | | (data[1] & 0xC0)>>5 | |
| 299 | | (data[2] & 0x80)>>7; |
| 300 | | classic_mesg->r_stick_y = data[2] & 0x1F; |
| 301 | | classic_mesg->l = (data[2] & 0x60)>>2 | |
| 302 | | (data[3] & 0xE0)>>5; |
| 303 | | classic_mesg->r = data[3] & 0x1F; |
| 304 | | classic_mesg->buttons = ~((uint16_t)data[4]<<8 | |
| 305 | | (uint16_t)data[5]); |
| 306 | | |
| 307 | | mesg_array->mesg[mesg_array->count] = |
| 308 | | (union wiimote_mesg *)classic_mesg; |
| 309 | | mesg_array->count++; |
| 310 | | } |
| 311 | | break; |
| 312 | | } |
| 313 | | |
| 314 | | return 0; |
| 315 | | } |
| | 100 | /* process_* messages (except read and write ) allocate and fill in |
| | 101 | * wiimote_*_mesg structs from raw wiimote data. Messages are then |
| | 102 | * placed at the end of mesg_array. */ |
| | 103 | |
| | 104 | static int process_status(struct wiimote *, const unsigned char *, |
| | 105 | struct mesg_array *); |
| | 106 | static int process_btn(struct wiimote *, const unsigned char *, |
| | 107 | struct mesg_array *); |
| | 108 | static int process_acc(struct wiimote *, const unsigned char *, |
| | 109 | struct mesg_array *); |
| | 110 | static int process_ir10(struct wiimote *, const unsigned char *, |
| | 111 | struct mesg_array *); |
| | 112 | static int process_ir12(struct wiimote *, const unsigned char *, |
| | 113 | struct mesg_array *); |
| | 114 | static int process_ext(struct wiimote *, unsigned char *, unsigned char, |
| | 115 | struct mesg_array *); |
| | 116 | |
| | 117 | /* process_read and process_write handle copying data between wiimote buffers |
| | 118 | * and r/w buffers, as well as communication between wiimote_read or |
| | 119 | * wiimote_write */ |
| | 120 | static int process_read(struct wiimote *, unsigned char *); |
| | 121 | static int process_write(struct wiimote *); |
| 434 | | if (wiimote->rw_status == RW_PENDING) { |
| 435 | | uint8_t data_len; |
| 436 | | uint8_t error; |
| 437 | | |
| 438 | | /* Extract error status and current packet length */ |
| 439 | | data_len = (buf[4]>>4)+1; |
| 440 | | error = buf[4] & 0x0F; |
| 441 | | /* Error if wiimote errors, or if packet is too long */ |
| 442 | | if (((data_len+wiimote->read_received)> |
| 443 | | wiimote->read_len) || error) { |
| 444 | | wiimote_err(wiimote, "Error in read data"); |
| 445 | | wiimote->rw_status = RW_ERROR; |
| 446 | | } |
| 447 | | else { |
| 448 | | /* Copy data into read_buf, update read data, signal |
| 449 | | * ready if we have enough data */ |
| 450 | | memcpy(wiimote->read_buf, buf+7, data_len); |
| 451 | | wiimote->read_buf += data_len; |
| 452 | | wiimote->read_received += data_len; |
| 453 | | if (wiimote->read_received == wiimote->read_len) { |
| 454 | | wiimote->rw_status = RW_READY; |
| 455 | | } |
| 456 | | } |
| 457 | | if (wiimote->rw_status != RW_PENDING) { |
| 458 | | /* Lock rw_cond_mutex, signal rw_cond, unlock |
| 459 | | * rw_cond_mutex */ |
| 460 | | if (pthread_mutex_lock(&wiimote->rw_cond_mutex)) { |
| 461 | | wiimote_err(wiimote, |
| 462 | | "Error locking rw_cond_mutex"); |
| 463 | | } |
| 464 | | else { |
| 465 | | if (pthread_cond_signal(&wiimote->rw_cond)) { |
| 466 | | wiimote_err(wiimote, |
| 467 | | "Error signaling rw_cond: " |
| 468 | | "deadlock warning"); |
| 469 | | } |
| 470 | | if (pthread_mutex_unlock( |
| 471 | | &wiimote->rw_cond_mutex)) { |
| 472 | | wiimote_err(wiimote, |
| 473 | | "Error unlocking rw_cond_mutex: " |
| 474 | | "deadlock warning"); |
| 475 | | } |
| 476 | | } |
| 477 | | } |
| 478 | | /* TODO: add button message */ |
| 479 | | } |
| 480 | | else { |
| 481 | | wiimote_err(wiimote, "Extraneous read data received"); |
| 482 | | } |
| | 243 | process_read(wiimote, &buf[4]); |
| | 244 | /* TODO: send button message */ |
| | 260 | static int process_status(struct wiimote *wiimote, const unsigned char *data, |
| | 261 | struct mesg_array *mesg_array) |
| | 262 | { |
| | 263 | struct wiimote_status_mesg *mesg; |
| | 264 | |
| | 265 | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| | 266 | wiimote_err(wiimote, "Error allocating status mesg"); |
| | 267 | return -1; |
| | 268 | } |
| | 269 | |
| | 270 | mesg->type = WIIMOTE_MESG_STATUS; |
| | 271 | mesg->battery = data[5]; |
| | 272 | if (data[2] & 0x02) { |
| | 273 | /* dispatch will figure out what it is */ |
| | 274 | mesg->extension = WIIMOTE_EXT_UNKNOWN; |
| | 275 | } |
| | 276 | else { |
| | 277 | mesg->extension = WIIMOTE_EXT_NONE; |
| | 278 | } |
| | 279 | |
| | 280 | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| | 281 | mesg_array->count++; |
| | 282 | |
| | 283 | return 0; |
| | 284 | } |
| | 285 | |
| | 286 | static int process_btn(struct wiimote *wiimote, const unsigned char *data, |
| | 287 | struct mesg_array *mesg_array) |
| | 288 | { |
| | 289 | struct wiimote_btn_mesg *mesg; |
| | 290 | uint16_t buttons; |
| | 291 | |
| | 292 | buttons = (data[0] & BTN_MASK_0)<<8 | |
| | 293 | (data[1] & BTN_MASK_1); |
| | 294 | if (wiimote->buttons != buttons) { |
| | 295 | wiimote->buttons = buttons; |
| | 296 | |
| | 297 | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_BTN) { |
| | 298 | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| | 299 | wiimote_err(wiimote, "Error allocating btn mesg"); |
| | 300 | return -1; |
| | 301 | } |
| | 302 | mesg->type = WIIMOTE_MESG_BTN; |
| | 303 | mesg->buttons = buttons; |
| | 304 | |
| | 305 | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| | 306 | mesg_array->count++; |
| | 307 | } |
| | 308 | } |
| | 309 | |
| | 310 | return 0; |
| | 311 | } |
| | 312 | |
| | 313 | static int process_acc(struct wiimote *wiimote, const unsigned char *data, |
| | 314 | struct mesg_array *mesg_array) |
| | 315 | { |
| | 316 | struct wiimote_acc_mesg *mesg; |
| | 317 | |
| | 318 | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_ACC) { |
| | 319 | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| | 320 | wiimote_err(wiimote, "Error allocating acc mesg"); |
| | 321 | return -1; |
| | 322 | } |
| | 323 | mesg->type = WIIMOTE_MESG_ACC; |
| | 324 | mesg->x = data[0]; |
| | 325 | mesg->y = data[1]; |
| | 326 | mesg->z = data[2]; |
| | 327 | |
| | 328 | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| | 329 | mesg_array->count++; |
| | 330 | } |
| | 331 | |
| | 332 | return 0; |
| | 333 | } |
| | 334 | |
| | 335 | static int process_ir10(struct wiimote *wiimote, const unsigned char *data, |
| | 336 | struct mesg_array *mesg_array) |
| | 337 | { |
| | 338 | struct wiimote_ir_mesg *mesg; |
| | 339 | int i; |
| | 340 | const unsigned char *block; |
| | 341 | |
| | 342 | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_IR) { |
| | 343 | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| | 344 | wiimote_err(wiimote, "Error allocating ir mesg"); |
| | 345 | return -1; |
| | 346 | } |
| | 347 | mesg->type = WIIMOTE_MESG_IR; |
| | 348 | |
| | 349 | for (i=0, block=data; i < WIIMOTE_IR_SRC_COUNT; i+=2, block+=5) { |
| | 350 | if (block[0] == 0xFF) { |
| | 351 | mesg->src[i].valid = 0; |
| | 352 | } |
| | 353 | else { |
| | 354 | mesg->src[i].valid = 1; |
| | 355 | mesg->src[i].x = ((uint16_t)block[2] & 0x30)<<4 | |
| | 356 | (uint16_t)block[0]; |
| | 357 | mesg->src[i].y = ((uint16_t)block[2] & 0xC0)<<2 | |
| | 358 | (uint16_t)block[1]; |
| | 359 | mesg->src[i].size = -1; |
| | 360 | } |
| | 361 | |
| | 362 | if (block[3] == 0xFF) { |
| | 363 | mesg->src[i+1].valid = 0; |
| | 364 | } |
| | 365 | else { |
| | 366 | mesg->src[i+1].valid = 1; |
| | 367 | mesg->src[i+1].x = ((uint16_t)block[2] & 0x03)<<8 | |
| | 368 | (uint16_t)block[3]; |
| | 369 | mesg->src[i+1].y = ((uint16_t)block[2] & 0x0C)<<6 | |
| | 370 | (uint16_t)block[4]; |
| | 371 | mesg->src[i+1].size = -1; |
| | 372 | } |
| | 373 | } |
| | 374 | |
| | 375 | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| | 376 | mesg_array->count++; |
| | 377 | } |
| | 378 | |
| | 379 | return 0; |
| | 380 | } |
| | 381 | |
| | 382 | static int process_ir12(struct wiimote *wiimote, const unsigned char *data, |
| | 383 | struct mesg_array *mesg_array) |
| | 384 | { |
| | 385 | struct wiimote_ir_mesg *mesg; |
| | 386 | int i; |
| | 387 | const unsigned char *block; |
| | 388 | |
| | 389 | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_IR) { |
| | 390 | if ((mesg = malloc(sizeof *mesg)) == NULL) { |
| | 391 | wiimote_err(wiimote, "Error allocating ir mesg"); |
| | 392 | return -1; |
| | 393 | } |
| | 394 | mesg->type = WIIMOTE_MESG_IR; |
| | 395 | |
| | 396 | for (i=0, block=data; i < WIIMOTE_IR_SRC_COUNT; i++, block+=3) { |
| | 397 | if (block[0] == 0xFF) { |
| | 398 | mesg->src[i].valid = 0; |
| | 399 | } |
| | 400 | else { |
| | 401 | mesg->src[i].valid = 1; |
| | 402 | mesg->src[i].x = ((uint16_t)block[2] & 0x30)<<4 | |
| | 403 | (uint16_t)block[0]; |
| | 404 | mesg->src[i].y = ((uint16_t)block[2] & 0xC0)<<2 | |
| | 405 | (uint16_t)block[1]; |
| | 406 | mesg->src[i].size = block[2] & 0x0F; |
| | 407 | } |
| | 408 | } |
| | 409 | |
| | 410 | mesg_array->mesg[mesg_array->count] = (union wiimote_mesg *)mesg; |
| | 411 | mesg_array->count++; |
| | 412 | } |
| | 413 | |
| | 414 | return 0; |
| | 415 | } |
| | 416 | |
| | 417 | static int process_ext(struct wiimote *wiimote, unsigned char *data, |
| | 418 | unsigned char len, struct mesg_array *mesg_array) |
| | 419 | { |
| | 420 | struct wiimote_nunchuk_mesg *nunchuk_mesg; |
| | 421 | struct wiimote_classic_mesg *classic_mesg; |
| | 422 | int i; |
| | 423 | |
| | 424 | switch (wiimote->extension) { |
| | 425 | case WIIMOTE_EXT_NONE: |
| | 426 | wiimote_err(wiimote, |
| | 427 | "Extension report received with no extension present"); |
| | 428 | break; |
| | 429 | case WIIMOTE_EXT_UNKNOWN: |
| | 430 | break; |
| | 431 | case WIIMOTE_EXT_NUNCHUK: |
| | 432 | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_NUNCHUK) { |
| | 433 | if ((nunchuk_mesg = malloc(sizeof *nunchuk_mesg)) == NULL) { |
| | 434 | wiimote_err(wiimote, "Error allocating nunchuk mesg"); |
| | 435 | return -1; |
| | 436 | } |
| | 437 | |
| | 438 | nunchuk_mesg->type = WIIMOTE_MESG_NUNCHUK; |
| | 439 | nunchuk_mesg->stick_x = DECODE(data[0]); |
| | 440 | nunchuk_mesg->stick_y = DECODE(data[1]); |
| | 441 | nunchuk_mesg->acc_x = DECODE(data[2]); |
| | 442 | nunchuk_mesg->acc_y = DECODE(data[3]); |
| | 443 | nunchuk_mesg->acc_z = DECODE(data[4]); |
| | 444 | nunchuk_mesg->buttons = ~DECODE(data[5]) & NUNCHUK_BTN_MASK; |
| | 445 | |
| | 446 | mesg_array->mesg[mesg_array->count] = |
| | 447 | (union wiimote_mesg *)nunchuk_mesg; |
| | 448 | mesg_array->count++; |
| | 449 | } |
| | 450 | break; |
| | 451 | case WIIMOTE_EXT_CLASSIC: |
| | 452 | if (wiimote->rpt_mode_flags & WIIMOTE_RPT_CLASSIC) { |
| | 453 | if ((classic_mesg = malloc(sizeof *classic_mesg)) == NULL) { |
| | 454 | wiimote_err(wiimote, "Error allocating classic mesg"); |
| | 455 | return -1; |
| | 456 | } |
| | 457 | |
| | 458 | for (i=0; i < 6; i++) { |
| | 459 | data[i] = DECODE(data[i]); |
| | 460 | } |
| | 461 | |
| | 462 | classic_mesg->type = WIIMOTE_MESG_CLASSIC; |
| | 463 | classic_mesg->l_stick_x = data[0] & 0x3F; |
| | 464 | classic_mesg->l_stick_y = data[1] & 0x3F; |
| | 465 | classic_mesg->r_stick_x = (data[0] & 0xC0)>>3 | |
| | 466 | (data[1] & 0xC0)>>5 | |
| | 467 | (data[2] & 0x80)>>7; |
| | 468 | classic_mesg->r_stick_y = data[2] & 0x1F; |
| | 469 | classic_mesg->l = (data[2] & 0x60)>>2 | |
| | 470 | (data[3] & 0xE0)>>5; |
| | 471 | classic_mesg->r = data[3] & 0x1F; |
| | 472 | classic_mesg->buttons = ~((uint16_t)data[4]<<8 | |
| | 473 | (uint16_t)data[5]); |
| | 474 | |
| | 475 | mesg_array->mesg[mesg_array->count] = |
| | 476 | (union wiimote_mesg *)classic_mesg; |
| | 477 | mesg_array->count++; |
| | 478 | } |
| | 479 | break; |
| | 480 | } |
| | 481 | |
| | 482 | return 0; |
| | 483 | } |
| | 484 | |
| | 485 | static int process_read(struct wiimote *wiimote, unsigned char *data) |
| | 486 | { |
| | 487 | uint8_t data_len; |
| | 488 | uint8_t error; |
| | 489 | int ret = 0; |
| | 490 | |
| | 491 | if (wiimote->rw_status == RW_PENDING) { |
| | 492 | /* Extract error status and current packet length */ |
| | 493 | data_len = (data[0]>>4)+1; |
| | 494 | error = data[0] & 0x0F; |
| | 495 | /* Error if wiimote errors, or if packet is too long */ |
| | 496 | if (((data_len + wiimote->read_received) > wiimote->read_len) || |
| | 497 | error) { |
| | 498 | wiimote_err(wiimote, "Error in read data"); |
| | 499 | wiimote->rw_status = RW_ERROR; |
| | 500 | ret = -1; |
| | 501 | } |
| | 502 | else { |
| | 503 | /* Copy data into read_buf, update read data, signal |
| | 504 | * ready if we have enough data */ |
| | 505 | memcpy(wiimote->read_buf, data+3, data_len); |
| | 506 | wiimote->read_buf += data_len; |
| | 507 | wiimote->read_received += data_len; |
| | 508 | if (wiimote->read_received == wiimote->read_len) { |
| | 509 | wiimote->rw_status = RW_READY; |
| | 510 | } |
| | 511 | } |
| | 512 | if (wiimote->rw_status != RW_PENDING) { |
| | 513 | /* Lock rw_cond_mutex, signal rw_cond, unlock |
| | 514 | * rw_cond_mutex */ |
| | 515 | if (pthread_mutex_lock(&wiimote->rw_cond_mutex)) { |
| | 516 | wiimote_err(wiimote, "Error locking rw_cond_mutex"); |
| | 517 | ret = -1; |
| | 518 | } |
| | 519 | else { |
| | 520 | if (pthread_cond_signal(&wiimote->rw_cond)) { |
| | 521 | wiimote_err(wiimote, "Error signaling rw_cond: " |
| | 522 | "deadlock warning"); |
| | 523 | ret = -1; |
| | 524 | } |
| | 525 | if (pthread_mutex_unlock( |
| | 526 | &wiimote->rw_cond_mutex)) { |
| | 527 | wiimote_err(wiimote, "Error unlocking rw_cond_mutex: " |
| | 528 | "deadlock warning"); |
| | 529 | ret = -1; |
| | 530 | } |
| | 531 | } |
| | 532 | } |
| | 533 | } |
| | 534 | else { |
| | 535 | wiimote_err(wiimote, "Extraneous read data received"); |
| | 536 | ret = 1; |
| | 537 | } |
| | 538 | |
| | 539 | return ret; |
| | 540 | } |
| | 541 | |
| | 542 | static int process_write(struct wiimote *wiimote) |
| | 543 | { |
| | 544 | int ret = 0; |
| | 545 | |
| | 546 | if (wiimote->rw_status == RW_PENDING) { |
| | 547 | wiimote->rw_status = RW_READY; |
| | 548 | /* Lock write_cond_mutex, signal write_cond, unlock |
| | 549 | * write_cond_mutex */ |
| | 550 | if (pthread_mutex_lock(&wiimote->rw_cond_mutex)) { |
| | 551 | wiimote_err(wiimote, "Error locking rw_cond_mutex"); |
| | 552 | ret = -1; |
| | 553 | } |
| | 554 | else { |
| | 555 | if (pthread_cond_signal(&wiimote->rw_cond)) { |
| | 556 | wiimote_err(wiimote, "Error signaling rw_cond: " |
| | 557 | "deadlock warning"); |
| | 558 | ret = -1; |
| | 559 | } |
| | 560 | if (pthread_mutex_unlock(&wiimote->rw_cond_mutex)) { |
| | 561 | wiimote_err(wiimote, "Error unlocking rw_cond_mutex: " |
| | 562 | "deadlock warning"); |
| | 563 | ret = -1; |
| | 564 | } |
| | 565 | } |
| | 566 | } |
| | 567 | else { |
| | 568 | wiimote_err(wiimote, "Extraneous write ack received"); |
| | 569 | ret = -1; |
| | 570 | } |
| | 571 | |
| | 572 | return ret; |
| | 573 | } |
| | 574 | |