Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INCLUDES
- #ifdef bt_en
- #include <initguid.h>
- #include <winsock2.h>
- #include <ws2bth.h>
- #include <strsafe.h>
- #endif
- VARIABLES / FUNCTIONS
- #ifdef bt_en
- int iMaxCxnCycles = 0, iOutputLevel = 2;
- pthread_t bt_thread;
- int btRet;
- ULONGLONG remoteAddress = 0x66464769;
- ULONG ulRetCode = 0;
- WSADATA WSAData = {0};
- SOCKET LocalSocket = INVALID_SOCKET;
- SOCKADDR_BTH SockAddrBthServer = {0};
- char recvbuf[1];
- int recvbuflen = 1;
- int iResult;
- unsigned int bt_status, bt_param1, bt_param2, bt_message, bt_channel, bt_pitchBend;
- void connect_to_bt();
- void *bt_check_thread(void *);
- void process_bt_data();
- ULONG RunClientMode(IN ULONGLONG ululRemoteBthAddr, IN int iMaxCxnCycles = 1);
- #endif
- IN THE CONSTRUCTOR..
- #ifdef bt_en
- connect_to_bt();
- #endif
- BLUETOOTH FUNCTIONS..
- void *bt_check_thread(void * data)
- {
- iResult = recv(LocalSocket, recvbuf, recvbuflen, 0);
- if ( iResult > 0 ) {
- printf("\nBytes received: %d\n", iResult);
- if (iResult == 1) {
- bt_status = (unsigned int)(recvbuf[0] & 0x000000FF);
- bt_message = (bt_status>>4) & 0x0F;
- bt_channel = bt_status & 0x0F;
- //bt_param1 = (unsigned int)recvbuf[1];
- //bt_param2 = (unsigned int)recvbuf[2];
- if (bt_message == 0x9) {
- recv(LocalSocket, recvbuf, recvbuflen, 0);
- bt_param1 = recvbuf[0];
- recv(LocalSocket, recvbuf, recvbuflen, 0);
- bt_param2 = recvbuf[0];
- cout << "Note On\n";
- cout << "Note Num: " << dec << bt_param1 << "\n";
- cout << "Velocity: " << dec << bt_param2 << "\n";
- } else if (bt_message == 0xE) {
- recv(LocalSocket, recvbuf, recvbuflen, 0);
- bt_param1 = recvbuf[0];
- recv(LocalSocket, recvbuf, recvbuflen, 0);
- bt_param2 = recvbuf[0];
- cout << "Pitch Bend\n";
- bt_pitchBend = (bt_param1 | bt_param2 << 8);
- cout << "Pitch Bend: 0x" << hex << bt_pitchBend << "\n";
- } else if (bt_message == 0x8) {
- recv(LocalSocket, recvbuf, recvbuflen, 0);
- bt_param1 = recvbuf[0];
- recv(LocalSocket, recvbuf, recvbuflen, 0);
- bt_param2 = recvbuf[0];
- cout << "Note Off\n";
- cout << "Note Num: " << dec << bt_param1 << "\n";
- cout << "Velocity: " << dec << bt_param2 << "\n";
- }
- }
- } else if ( iResult == 0 ) {
- printf("Connection closed\n");
- } else {
- printf("recv failed: %d\n", WSAGetLastError());
- }
- }
- void connect_to_bt() {
- ulRetCode = WSAStartup(MAKEWORD(2, 2), &WSAData);
- if ( ulRetCode == 0 ) { // "zero" per SDK
- cout << "Winsock v2.2 Initialized.\n";
- } else {
- cout << "Unable to initialize Winsock version 2.2\n";
- }
- SockAddrBthServer.addressFamily = AF_BTH;
- SockAddrBthServer.btAddr = (BTH_ADDR) remoteAddress;
- SockAddrBthServer.serviceClassId = g_guidServiceClass;
- SockAddrBthServer.port = 1; //eTar-BT has uses port 1
- LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
- if ( LocalSocket == INVALID_SOCKET ) {
- cout << "socket() call failed. WSAGetLastError = " << WSAGetLastError() << "\n";
- } else {
- cout << "socket() call succeeded. Socket = " << LocalSocket << "\n";
- }
- if ( SOCKET_ERROR == connect(LocalSocket,
- (struct sockaddr *) &SockAddrBthServer,
- sizeof(SOCKADDR_BTH)) ) {
- cout << "connect() call failed. WSAGetLastError=" << WSAGetLastError() << "\n";
- } else {
- cout << "connect() call succeeded.\n";
- }
- // Create independent threads each of which will execute function
- btRet = pthread_create(&bt_thread, NULL, bt_check_thread, NULL);
- if (btRet != 0){
- if (btRet == EINVAL){
- cout << "The value specified for the argument is not correct.\n";
- } else if (btRet == EAGAIN) {
- cout << "The system didn't have enough resources to create another thread\nor the maximum number of threads for this job has been reached.\n";
- } else if (btRet == EBUSY) {
- cout << "The system cannot allow thread creation in this process at this time.\n";
- } else {
- cout << "pthread_create() not successful.\nError #: " << btRet << "\n";
- }
- } else if (btRet == 0 ) {
- cout << "pthread_create() successful.\nBegin accepting strums..\n";
- }
- }
Add Comment
Please, Sign In to add comment