diff options
author | Git Gud <git@endianness.com> | 2020-10-20 15:45:33 +1100 |
---|---|---|
committer | Git Gud <git@endianness.com> | 2020-10-20 15:45:33 +1100 |
commit | 5360f7912266bdc6354adc86ba3cc53f0ae2041f (patch) | |
tree | 861b49b310ad069af71a7b9aa01a0c8954dadb3d | |
parent | a15647af18372d853cc316d3bc1b97c8293b7897 (diff) | |
download | wiimote-5360f7912266bdc6354adc86ba3cc53f0ae2041f.tar.gz wiimote-5360f7912266bdc6354adc86ba3cc53f0ae2041f.tar.bz2 wiimote-5360f7912266bdc6354adc86ba3cc53f0ae2041f.zip |
more improvements...
-rw-r--r-- | main.c | 141 |
1 files changed, 109 insertions, 32 deletions
@@ -17,6 +17,21 @@ int controlSock, dataSock; +//#define CONTROL 0x0011 +//#define DATA 0x0013 + +//are 0x11 or 0x13 even valid? +//"All PSM values shall be ODD, that is, the least significant bit of the least significant octet must be 1. +//Also, all PSM values shall have the least significant bit of the most significant octet equal to 0." +//Looks like they are... + +//slapping 0x10 on here allows accept() to work, unlike 0x11, which complains of permission denied without root access... +//Also, any server connecting to 0x11 (that's right, the server does something a client would usually do) manages to connect automagically to something, just never to this program... +#define CONTROL 0x1011 +#define DATA 0x1013 + + + #define MAX 40 struct state @@ -93,8 +108,9 @@ bdaddr_t findBluetooth() int connectBluetooth(bdaddr_t address) { - int hci_sock = hci_open_dev(hci_get_route(NULL)); - if (hci_sock < 0){fprintf(stderr,"Could not open device!\n");} +// int device_id = hci_get_route(NULL); +// int hci_sock = hci_open_dev(device_id); +// if (device_id < 0 || hci_sock < 0){fprintf(stderr,"Could not open device!\n"); return 0;} unsigned int ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5; @@ -102,6 +118,7 @@ int connectBluetooth(bdaddr_t address) bdaddr_t bdaddr; +/* if (hci_create_connection(hci_sock, &address, htobs(ptype), 0, 0, &handle, 0) < 0) { perror("HCI create connection failed"); @@ -120,44 +137,102 @@ int connectBluetooth(bdaddr_t address) close(hci_sock); return 0; } - +*/ int status; + //Establishing a HID connection with the Wii Remote can be done on PSM 0x11 for the control pipe using the Bluetooth L2CAP protocol. - struct sockaddr_l2 control = {0}; - controlSock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + struct sockaddr_l2 control = {0}, remote_addr = {0};//share the remote address struct + struct bt_security bt_sec; + controlSock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + if (controlSock < 0){fprintf(stderr,"Opening control socket failed!\n"); return 0;} + + char buff[19]; + socklen_t opt = sizeof(remote_addr); // set the connection parameters (who to connect to) + control.l2_psm = htobs(CONTROL); + bacpy(&control.l2_bdaddr, BDADDR_ANY);//accept any, whatever control.l2_family = AF_BLUETOOTH; - control.l2_psm = htobs(0x11); - control.l2_bdaddr = address; - printf("First\n"); - fflush(stdout); - status = connect(controlSock, (struct sockaddr *)&control, sizeof(control));//while the control pipe isn't used, we still gotta open it - if(status < 0){fprintf(stderr,"Failed to connect to control pipe!: "); perror(""); close(hci_sock); return 0;} - printf("Second\n"); + int result = bind(controlSock, (struct sockaddr *)&control, sizeof(control)); + if (result < 0){fprintf(stderr,"Bind failed with error: %d & %s!\n", result, strerror(errno)); return 0;} + + + + memset(&bt_sec, 0, sizeof(bt_sec)); + bt_sec.level = BT_SECURITY_MEDIUM; + result = setsockopt(controlSock, SOL_BLUETOOTH, BT_SECURITY, &bt_sec, sizeof(bt_sec)); + if (result != 0){fprintf(stderr,"Setting security level failed!\n"); return 0;} + + + result = listen(controlSock, 1); + if (result < 0){fprintf(stderr,"Listen failed!\n"); return 0;} + + + printf("Before\n"); fflush(stdout); + status = accept(controlSock, (struct sockaddr *)&remote_addr, &opt); + if(status < 0){fprintf(stderr,"Failed to accept connection to control pipe!: "); perror(""); return 0;} + + + ba2str(&remote_addr.l2_bdaddr, buff); + printf("Accepted control connection from: %s\n", buff); + + /* + //Set receive buffer size + if (rcvbuf && setsockopt(nsk, SOL_SOCKET, SO_RCVBUF, &rcvbuf,sizeof(rcvbuf)) < 0) + { + fprintf(stderr("Can't set rcv buf size: %s (%d)", strerror(errno), errno); + exit(1); + } + + optlen = sizeof(rcvbuf); + if (getsockopt(nsk, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen) < 0) + { + fprintf(stderr,"Can't get rcv buf size: %s (%d)", strerror(errno), errno); + exit(1); + } + */ + + struct sockaddr_l2 data = {0}, rem_addr = {0};//share the remote address struct + socklen_t socklen = sizeof(rem_addr); + //Establishing a HID connection with the Wii Remote can be done on PSM 0x13 for the data pipe using the Bluetooth L2CAP protocol. - struct sockaddr_l2 data = {0}; - dataSock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + dataSock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + if (dataSock < 0){fprintf(stderr,"Opening control socket failed!\n"); return 0;} // set the connection parameters (who to connect to) + data.l2_psm = htobs(DATA); + bacpy(&data.l2_bdaddr, BDADDR_ANY);//accept any, whatever data.l2_family = AF_BLUETOOTH; - data.l2_psm = htobs(0x13); - data.l2_bdaddr = address; - printf("Third\n"); - fflush(stdout); + result = bind(dataSock, (struct sockaddr *)&data, sizeof(data)); + if (result < 0){fprintf(stderr,"Bind failed with error: %d & %d!\n", result, errno); return 0;} + + + memset(&bt_sec, 0, sizeof(bt_sec)); + bt_sec.level = BT_SECURITY_MEDIUM; + result = setsockopt(dataSock, SOL_BLUETOOTH, BT_SECURITY, &bt_sec, sizeof(bt_sec)); + if (result != 0){fprintf(stderr,"Setting security level failed!\n"); return 0;} - status = connect(dataSock, (struct sockaddr *)&data, sizeof(data)); - if(status < 0){fprintf(stderr,"Failed to connect to data pipe!: "); perror(""); close(hci_sock); return 0;} - printf("Fourth\n"); + result = listen(dataSock, 1); + if (result < 0){fprintf(stderr,"Listen failed!\n"); return 0;} + + + printf("Before 2: Electric boogaloo\n"); fflush(stdout); + status = accept(dataSock, (struct sockaddr *)&rem_addr, &socklen); + if(status < 0){fprintf(stderr,"Failed to accept connection to data pipe!: "); perror(""); return 0;} + + + ba2str(&remote_addr.l2_bdaddr, buff); + printf("Accepted data connection from: %s\n", buff); + printf("You win *claps*\n"); return 1; @@ -191,18 +266,11 @@ int main(int argc, char **argv) // bdaddr_t address = findBluetooth(); bdaddr_t address; - str2ba("no",&address); + str2ba("00:19:86:00:04:3F",&address); int result = 0; - do - { - result = connectBluetooth(address); - sleep(3); - } - while (!result); - + result = connectBluetooth(address); - exit(1); //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 @@ -224,12 +292,21 @@ int main(int argc, char **argv) //Our data here is: a1 20 00 00 14 00 00 8d (LF == LED 1 enabled and speaker enabled), (VV, battery level 141) //send the sync conformation report and repeat if al the bytes have not been written. +// sync: size = write(dataSock,"\xa1\x20\x00\x00\x14\x00\x00\x8d",8); + char buff[MAX+1]; + sync: size = write(dataSock,"\xa1\x20\x00\x00\x14\x00\x00\x8d",8); - if (size < 8){fprintf(stderr,"Writing connection confirmation failed!\n"); goto sync;} + if (size < 8) + { + fprintf(stderr,"Writing connection confirmation failed with error: %d!\n", size); + size = read(dataSock,buff,MAX); + fprintf(stderr, "%d bytes read from data sock!\n", size); + sleep(3); + goto sync; + } //the Wii must then send output report 0x12 to change the data reporting mode, as above was sent without request - char buff[MAX+1]; changemode: size = read(dataSock, buff, MAX); if (size < 4){fprintf(stderr,"Reading data report mode failed!\n"); goto changemode;} /* |