Guest User

Untitled

a guest
Jun 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. INCLUDES
  2. #ifdef bt_en
  3.     #include <initguid.h>
  4.     #include <winsock2.h>
  5.     #include <ws2bth.h>
  6.     #include <strsafe.h>
  7. #endif
  8.  
  9. VARIABLES / FUNCTIONS
  10. #ifdef bt_en
  11. int  iMaxCxnCycles = 0, iOutputLevel = 2;
  12. pthread_t bt_thread;
  13. int btRet;
  14. ULONGLONG remoteAddress = 0x66464769;
  15. ULONG       ulRetCode = 0;
  16. WSADATA     WSAData = {0};
  17. SOCKET          LocalSocket = INVALID_SOCKET;
  18. SOCKADDR_BTH    SockAddrBthServer = {0};
  19. char recvbuf[1];
  20. int recvbuflen = 1;
  21. int iResult;
  22. unsigned int bt_status, bt_param1, bt_param2, bt_message, bt_channel, bt_pitchBend;
  23.  
  24. void connect_to_bt();
  25. void *bt_check_thread(void *);
  26. void process_bt_data();
  27. ULONG RunClientMode(IN ULONGLONG ululRemoteBthAddr, IN int iMaxCxnCycles = 1);
  28. #endif
  29.  
  30. IN THE CONSTRUCTOR..
  31. #ifdef bt_en
  32.     connect_to_bt();
  33. #endif
  34.  
  35. BLUETOOTH FUNCTIONS..
  36. void *bt_check_thread(void * data)
  37. {
  38.     iResult = recv(LocalSocket, recvbuf, recvbuflen, 0);
  39.     if ( iResult > 0 ) {
  40.         printf("\nBytes received: %d\n", iResult);
  41.  
  42.         if (iResult == 1) {
  43.             bt_status = (unsigned int)(recvbuf[0] & 0x000000FF);
  44.             bt_message = (bt_status>>4) & 0x0F;
  45.             bt_channel = bt_status & 0x0F;
  46.             //bt_param1 = (unsigned int)recvbuf[1];
  47.             //bt_param2 = (unsigned int)recvbuf[2];
  48.  
  49.             if (bt_message == 0x9) {
  50.                 recv(LocalSocket, recvbuf, recvbuflen, 0);
  51.                 bt_param1 = recvbuf[0];
  52.                 recv(LocalSocket, recvbuf, recvbuflen, 0);
  53.                 bt_param2 = recvbuf[0];
  54.                 cout << "Note On\n";
  55.                 cout << "Note Num: " << dec << bt_param1 << "\n";
  56.                 cout << "Velocity: " << dec << bt_param2 << "\n";
  57.             } else if (bt_message == 0xE) {
  58.                 recv(LocalSocket, recvbuf, recvbuflen, 0);
  59.                 bt_param1 = recvbuf[0];
  60.                 recv(LocalSocket, recvbuf, recvbuflen, 0);
  61.                 bt_param2 = recvbuf[0];
  62.                 cout << "Pitch Bend\n";
  63.                 bt_pitchBend = (bt_param1 | bt_param2 << 8);
  64.                 cout << "Pitch Bend: 0x" << hex << bt_pitchBend << "\n";
  65.             } else if (bt_message == 0x8) {
  66.                 recv(LocalSocket, recvbuf, recvbuflen, 0);
  67.                 bt_param1 = recvbuf[0];
  68.                 recv(LocalSocket, recvbuf, recvbuflen, 0);
  69.                 bt_param2 = recvbuf[0];
  70.                 cout << "Note Off\n";
  71.                 cout << "Note Num: " << dec << bt_param1 << "\n";
  72.                 cout << "Velocity: " << dec << bt_param2 << "\n";
  73.             }
  74.                    
  75.         }
  76.            
  77.     } else if ( iResult == 0 ) {
  78.         printf("Connection closed\n");
  79.     } else {
  80.         printf("recv failed: %d\n", WSAGetLastError());
  81.     }
  82. }
  83.  
  84. void connect_to_bt() {
  85.     ulRetCode = WSAStartup(MAKEWORD(2, 2), &WSAData);
  86.         if ( ulRetCode == 0 ) { // "zero" per SDK
  87.         cout << "Winsock v2.2 Initialized.\n";
  88.         } else {
  89.             cout << "Unable to initialize Winsock version 2.2\n";
  90.     }
  91.  
  92.     SockAddrBthServer.addressFamily = AF_BTH;
  93.         SockAddrBthServer.btAddr = (BTH_ADDR) remoteAddress;
  94.         SockAddrBthServer.serviceClassId = g_guidServiceClass;
  95.         SockAddrBthServer.port = 1; //eTar-BT has uses port 1
  96.  
  97.     LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
  98.     if ( LocalSocket == INVALID_SOCKET ) {
  99.             cout << "socket() call failed. WSAGetLastError = " << WSAGetLastError() << "\n";
  100.         } else {
  101.         cout << "socket() call succeeded. Socket = " << LocalSocket << "\n";
  102.     }
  103.  
  104.     if ( SOCKET_ERROR == connect(LocalSocket,
  105.                                     (struct sockaddr *) &SockAddrBthServer,
  106.                                     sizeof(SOCKADDR_BTH)) ) {
  107.             cout << "connect() call failed. WSAGetLastError=" << WSAGetLastError() << "\n";
  108.         } else {
  109.         cout << "connect() call succeeded.\n";
  110.     }
  111.  
  112.     // Create independent threads each of which will execute function
  113.     btRet = pthread_create(&bt_thread, NULL, bt_check_thread, NULL);
  114.  
  115.     if (btRet != 0){
  116.         if (btRet == EINVAL){
  117.             cout << "The value specified for the argument is not correct.\n";
  118.         } else if (btRet == EAGAIN) {
  119.             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";
  120.         } else if (btRet == EBUSY) {
  121.             cout << "The system cannot allow thread creation in this process at this time.\n";
  122.         } else {
  123.             cout << "pthread_create() not successful.\nError #: " << btRet << "\n";
  124.         }
  125.     } else if (btRet == 0 ) {
  126.             cout << "pthread_create() successful.\nBegin accepting strums..\n";
  127.     }
  128. }
Add Comment
Please, Sign In to add comment