summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 543965f905e81f194f55df3a4b2b70e9a79fc9fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/ioctl.h>

#include <errno.h>
#include <SDL2/SDL.h>

#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);
}