#include #include #include #include #include #include #include #include #include #include "main.h" #include "bluetooth.h" #include "io.h" int main(int argc, char *argv[]) { if (getuid() != 0){fprintf(stderr,"Root is required to bind to the bluetooth PSM ports.\n"); exit(1);} // bdaddr_t address = findBluetooth(); bdaddr_t address; str2ba("no",&address); int result = connectBluetooth(address); if (!result){fprintf(stderr,"Connecting to bluetooth failed!\n"); exit(1);} SDL_Init(0); /* When using a Wii Remote, all input reports are prepended with 0xa1 and all output reports are prepended with 0xa2 * Output reports are sent over the data pipe, which is also used to read input reports. * The control pipe can be used for certain reports on original Wii remotes, but such is bad practice. * Input reports are reports from the Wii remote to the Wii and output reports are reports from the Wii to the Wii remote */ struct state *remoteState; remoteState = &newState; initState(remoteState); /* The wiimote appears to spam a massive amount of random garbage with 0x3X reports pre "sync". * Such "feature" isn't implemented, hopefully no "server" expects it. */ uint8_t *buff = malloc(MAX+1); if (!buff){fprintf(stderr, "Malloc failed!\n"); exit(1);} /* When sync "succeeds", the mode changes to 0x20 without explanation */ remoteState->mode = 0x20; printf("DataSock value: %d\n", dataSock); printf("ControlSock value: %d\n", controlSock); int ret; /* communication is handled via a read/write loop */ while (1) { ret = read(dataSock, buff, MAX); if (ret == -1){fprintf(stderr,"Read error! Errno: %d, Error: %s\n",errno,strerror(errno));} else if (ret){dataRecieved(buff, ret, remoteState);} /* read() May return 0, in that case we just carry on */ usleep(1000); if (!skipWrite) { ret = writeCommand(remoteState); if (ret == -1){fprintf(stderr,"Write error! Errno: %d, Error: %s\n",errno,strerror(errno));} else {fprintf(stderr,"Actually wrote %d bytes!\n",ret);} usleep(1000); } else {skipWrite = false; fprintf(stderr, "Write skipped\n");} } free(buff); close(controlSock); close(dataSock); }