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