/**
 * multipleRobot.c
 * Written to connect to two specific robots to beep. Connections are established one at a time.
 * Connections are established through sockets.
 * Compile with: gcc -lbluetooth -o multipleRobot multipleRobot.c MyroC.c
 * Code pieces taken from rfcomm-serial-client.c by Henry M. Walker
 *
 * Dilan Ustek
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
   struct sockaddr_rc addr = { 0 };
    int s, status;
    char dest[18] = "00:1E:19:01:0E:A7";
    char message[9];
    
    // allocate a socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

    // set the connection parameters (who to connect to)
    addr.rc_family = AF_BLUETOOTH;
    addr.rc_channel = 1;
    str2ba( dest, &addr.rc_bdaddr );

    // connect to server
     status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

    
    struct sockaddr_rc addr2 = { 0 };
    int s2, status2;
    char dest2[18] = "00:1E:19:01:0D:D1";
    char message2[9];

    // allocate a socket
    s2 = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

    // set the connection parameters (who to connect to)
    addr2.rc_family = AF_BLUETOOTH;
    addr2.rc_channel = 1;
    str2ba( dest2, &addr2.rc_bdaddr );

    // connect to server
    status2 = connect(s2, (struct sockaddr *)&addr2, sizeof(addr2));
   
    // send a message
    if( 0 == status2 ) {
      int duration2, frequency2;
      message2[0]=113; // set speaker
      duration2 = 3000; // 3000 milleseconds = 3 seconds
      message2[1]= duration2 >> 8;  // high-order 8 bits
      message2[2]= duration2 % 256;  // low-order 8 bits
      frequency2 = 640; // A near middle C
      message2[3]= frequency2 >> 8; // high-order 8 bits
      message2[4]= frequency2 % 256;// low-order 8 bits
      message2[5]= -1 ; // end of data set
      message2[6]= 0;   // not used
      message2[7]= 0;   // not used
      message2[8]= 0;   // not used

      status2 = send(s2, message2, 9, 0);


  int duration, frequency;
      message[0]=113; // set speaker
      duration = 2000; // 2000 milleseconds = 2 seconds
      message[1]= duration >> 8;  // high-order 8 bits
      message[2]= duration % 256;  // low-order 8 bits
      frequency = 880; // A above middle C
      message[3]= frequency >> 8; // high-order 8 bits
      message[4]= frequency % 256;// low-order 8 bits
      frequency =  400;
      message[5]= frequency >> 8 ; // end of data set
      message[6]= frequency % 256;   // not used
      message[7]= -1;   // not used
      message[8]= 0;   // not used

      status = send(s, message, 9, 0);

      }
    

    if( status2 < 0 ) perror("uh oh");

    close(s2);
    return 0;

}
