| 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_findfirst (moved to bluetooth.c) |
|---|
| 20 | * |
|---|
| 21 | * 03/27/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 22 | * * moved wiimote_findfirst to bluetooth.c |
|---|
| 23 | * |
|---|
| 24 | * 03/14/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 25 | * * audited error checking (coda and error handler sections) |
|---|
| 26 | * |
|---|
| 27 | * 03/05/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 28 | * * created wiimote_err_func variable |
|---|
| 29 | * * created wiimote_err_default |
|---|
| 30 | * * added wiimote parameter to wiimote_err definition and calls |
|---|
| 31 | * |
|---|
| 32 | * 03/01/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 33 | * * Initial ChangeLog |
|---|
| 34 | * * type audit (stdint, const, char booleans) |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | #include <stdarg.h> |
|---|
| 38 | #include <stdint.h> |
|---|
| 39 | #include <stdio.h> |
|---|
| 40 | #include <stdlib.h> |
|---|
| 41 | #include <string.h> |
|---|
| 42 | #include <unistd.h> |
|---|
| 43 | #include "wiimote_internal.h" |
|---|
| 44 | |
|---|
| 45 | static wiimote_err_t wiimote_err_default; |
|---|
| 46 | |
|---|
| 47 | static wiimote_err_t *wiimote_err_func = &wiimote_err_default; |
|---|
| 48 | |
|---|
| 49 | int wiimote_set_err(wiimote_err_t *err) |
|---|
| 50 | { |
|---|
| 51 | /* TODO: assuming pointer assignment is atomic operation */ |
|---|
| 52 | /* if it is, and the user doesn't care about race conditions, we don't |
|---|
| 53 | * either */ |
|---|
| 54 | wiimote_err_func = err; |
|---|
| 55 | return 0; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | static void wiimote_err_default(int id, const char *str, ...) |
|---|
| 59 | { |
|---|
| 60 | va_list ap; |
|---|
| 61 | |
|---|
| 62 | va_start(ap, str); |
|---|
| 63 | vfprintf(stderr, str, ap); |
|---|
| 64 | fprintf(stderr, "\n"); |
|---|
| 65 | va_end(ap); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void wiimote_err(struct wiimote *wiimote, const char *str, ...) |
|---|
| 69 | { |
|---|
| 70 | va_list ap; |
|---|
| 71 | |
|---|
| 72 | if (wiimote_err_func) { |
|---|
| 73 | va_start(ap, str); |
|---|
| 74 | if (wiimote) { |
|---|
| 75 | (*wiimote_err_func)(wiimote->id, str, ap); |
|---|
| 76 | } |
|---|
| 77 | else { |
|---|
| 78 | (*wiimote_err_func)(-1, str, ap); |
|---|
| 79 | } |
|---|
| 80 | va_end(ap); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | int verify_handshake(struct wiimote *wiimote) |
|---|
| 85 | { |
|---|
| 86 | unsigned char handshake; |
|---|
| 87 | if (read(wiimote->ctl_socket, &handshake, 1) != 1) { |
|---|
| 88 | wiimote_err(wiimote, "Error on read handshake"); |
|---|
| 89 | return -1; |
|---|
| 90 | } |
|---|
| 91 | else if ((handshake & BT_TRANS_MASK) != BT_TRANS_HANDSHAKE) { |
|---|
| 92 | wiimote_err(wiimote, "Handshake expected, non-handshake received"); |
|---|
| 93 | return -1; |
|---|
| 94 | } |
|---|
| 95 | else if ((handshake & BT_PARAM_MASK) != BT_PARAM_SUCCESSFUL) { |
|---|
| 96 | wiimote_err(wiimote, "Non-successful handshake"); |
|---|
| 97 | return -1; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | return 0; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | #define SEND_RPT_BUF_LEN 23 |
|---|
| 104 | int send_report(struct wiimote *wiimote, uint8_t flags, uint8_t report, |
|---|
| 105 | size_t len, const void *data) |
|---|
| 106 | { |
|---|
| 107 | unsigned char buf[SEND_RPT_BUF_LEN]; |
|---|
| 108 | |
|---|
| 109 | if ((len+2) > SEND_RPT_BUF_LEN) { |
|---|
| 110 | return -1; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | buf[0] = BT_TRANS_SET_REPORT | BT_PARAM_OUTPUT; |
|---|
| 114 | buf[1] = report; |
|---|
| 115 | memcpy(buf+2, data, len); |
|---|
| 116 | if (!(flags & SEND_RPT_NO_RUMBLE)) { |
|---|
| 117 | buf[2] |= wiimote->led_rumble_state & 0x01; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | if (write(wiimote->ctl_socket, buf, len+2) != (len+2)) { |
|---|
| 121 | return -1; |
|---|
| 122 | } |
|---|
| 123 | else if (verify_handshake(wiimote)) { |
|---|
| 124 | return -1; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | return 0; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | int exec_write_seq(struct wiimote *wiimote, unsigned int len, |
|---|
| 131 | struct write_seq *seq) |
|---|
| 132 | { |
|---|
| 133 | int i; |
|---|
| 134 | |
|---|
| 135 | for (i=0; i < len; i++) { |
|---|
| 136 | switch (seq[i].type) { |
|---|
| 137 | case WRITE_SEQ_RPT: |
|---|
| 138 | if (send_report(wiimote, seq[i].flags, seq[i].report_offset, |
|---|
| 139 | seq[i].len, seq[i].data)) { |
|---|
| 140 | return -1; |
|---|
| 141 | } |
|---|
| 142 | break; |
|---|
| 143 | case WRITE_SEQ_MEM: |
|---|
| 144 | if (wiimote_write(wiimote, seq[i].flags, seq[i].report_offset, |
|---|
| 145 | seq[i].len, seq[i].data)) { |
|---|
| 146 | return -1; |
|---|
| 147 | } |
|---|
| 148 | break; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | return 0; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | void free_mesg_array(struct mesg_array *array) |
|---|
| 156 | { |
|---|
| 157 | int i; |
|---|
| 158 | |
|---|
| 159 | for (i=0; i < array->count; i++) { |
|---|
| 160 | free(array->mesg[i]); |
|---|
| 161 | } |
|---|
| 162 | free(array); |
|---|
| 163 | } |
|---|
| 164 | |
|---|