173 lines
4.5 KiB
C++
Executable File
173 lines
4.5 KiB
C++
Executable File
#include <iostream>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <bluetooth/bluetooth.h>
|
|
#include <bluetooth/rfcomm.h>
|
|
#include <bluetooth/hci.h>
|
|
#include <bluetooth/hci_lib.h>
|
|
using namespace std;
|
|
|
|
|
|
void log(string log_str, struct tm * timeinfo)
|
|
{
|
|
char logname[35];
|
|
memset(logname,0,sizeof(logname));
|
|
sprintf(logname,"/home/lucas/.LOG/BTlog%.2i%.2i.txt",timeinfo->tm_mon+1,timeinfo->tm_mday);
|
|
system("find /home/lucas/.LOG/BTlog* -mtime +10 -type f -delete");
|
|
ofstream logfile(logname, ios::app);
|
|
if(logfile.is_open())
|
|
{
|
|
logfile << timeinfo->tm_mon<<"."<<timeinfo->tm_mday<<";"<<timeinfo->tm_hour<<":"<<timeinfo->tm_min<<":"<<timeinfo->tm_sec<<": BTserver: "<< log_str <<"\n";
|
|
logfile.close();
|
|
}
|
|
}
|
|
|
|
int exec_cmd(int cmd_value,struct tm *timeinfo){
|
|
string key,cmd=string("irsend SEND_ONCE SAMSUNG ");
|
|
switch(cmd_value){
|
|
case 1://ON
|
|
key=string("KEY_0"); break;
|
|
case 2://Up
|
|
key=string("KEY_UP"); break;
|
|
case 3://OFF
|
|
key=string("KEY_POWER"); break;
|
|
case 4://Left
|
|
key=string("KEY_LEFT"); break;
|
|
case 5://Enter
|
|
key=string("KEY_ENTER"); break;
|
|
case 6://Right
|
|
key=string("KEY_RIGHT"); break;
|
|
case 7://Volume UP
|
|
key=string("KEY_VOLUMEUP"); break;
|
|
case 8://Down
|
|
key=string("KEY_DOWN"); break;
|
|
case 9://Volume Down
|
|
key=string("KEY_VOLUMEDOWN"); break;
|
|
case 10://Source
|
|
key=string("KEY_MEDIA"); break;
|
|
case 11://Play
|
|
key=string("KEY_PLAY"); break;
|
|
case 12://Pause
|
|
key=string("KEY_PAUSE"); break;
|
|
case 13://Mute
|
|
key=string("KEY_MUTE"); break;
|
|
case 14://Mute
|
|
key=string("KEY_BACK"); break;
|
|
case 15://Mute
|
|
key=string("KEY_EXIT"); break;
|
|
default: log(to_string(cmd_value),timeinfo); return 1;
|
|
}
|
|
cmd.append(key);
|
|
if(system(cmd.c_str())==0)
|
|
{
|
|
log(key,timeinfo);
|
|
return 0;
|
|
}
|
|
else return -1;
|
|
return -1;
|
|
}
|
|
int setBTdevice(int device, struct tm * timeinfo)
|
|
{
|
|
int s;
|
|
struct sockaddr_rc loc_addr = { 0 };
|
|
struct hci_dev_info di;
|
|
// allocate socket
|
|
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
|
|
|
// bind socket to port 1 of the first available
|
|
// local bluetooth adapter
|
|
if(hci_devinfo(device, &di) < 0)
|
|
{
|
|
log("HCI device info failed",timeinfo);
|
|
exit(1);
|
|
}
|
|
string dev_log=string("Local device: ")+ batostr(&di.bdaddr);
|
|
log(dev_log,timeinfo);
|
|
loc_addr.rc_bdaddr = di.bdaddr;
|
|
loc_addr.rc_family = AF_BLUETOOTH;
|
|
loc_addr.rc_channel = (uint8_t) 1;
|
|
if(bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0)
|
|
{
|
|
log("Socket bind Error",timeinfo);
|
|
exit(1);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
|
|
if(argc < 2)
|
|
{
|
|
cout << "Usage:\n<1/0> BTdevice"<<endl;
|
|
return 1;
|
|
}
|
|
struct sockaddr_rc rem_addr = { 0 };
|
|
socklen_t opt = sizeof(rem_addr);
|
|
struct tm * timeinfo;
|
|
time_t rawtime;
|
|
time ( &rawtime );
|
|
timeinfo= localtime ( &rawtime );
|
|
log("Start Bluetooth Server",timeinfo);
|
|
string LF_phone=string("34:14:5F:68:FC:EC");
|
|
char buf[1024] = { 0 };
|
|
int sock, client, bytes_read,device=atoi(argv[1]);
|
|
sock=setBTdevice(device,timeinfo);
|
|
if(sock==0) log("Socket Failure",timeinfo);
|
|
|
|
// put socket into listening mode
|
|
while(1)
|
|
{
|
|
listen(sock, 1);
|
|
log("Device in Listening Mode",timeinfo);
|
|
// accept one connection
|
|
client = accept(sock, (struct sockaddr *)&rem_addr, &opt);
|
|
if(client < 0)
|
|
{
|
|
log("Client accept Error",timeinfo);
|
|
exit(1);
|
|
}
|
|
|
|
string dev_addr;
|
|
ba2str( &rem_addr.rc_bdaddr,buf );
|
|
dev_addr.assign(buf);
|
|
|
|
if(dev_addr.compare(LF_phone)!=0)
|
|
{
|
|
log("Illegal Connection Attempt",timeinfo);
|
|
close(client);
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
string log_str=string("Accepted connection from: ")+dev_addr;
|
|
log(log_str,timeinfo);
|
|
memset(buf, 0, sizeof(buf));
|
|
// if(string())
|
|
// read data from the client
|
|
bytes_read = read(client, buf, sizeof(buf));
|
|
while( bytes_read > 0 )
|
|
{
|
|
time ( &rawtime );
|
|
timeinfo= localtime ( &rawtime );
|
|
bytes_read = read(client, buf, sizeof(buf));
|
|
if(exec_cmd(atoi(buf),timeinfo)<0)
|
|
{
|
|
log_str.assign("Remote Command Failed: " + string(buf));
|
|
log(log_str,timeinfo);
|
|
break;
|
|
}
|
|
memset(buf, 0, sizeof(buf));
|
|
}
|
|
close(client);
|
|
log_str.assign("Disconnected from: "+dev_addr);
|
|
log(log_str,timeinfo);
|
|
}
|
|
}
|
|
// close connection
|
|
close(sock);
|
|
return 0;
|
|
}
|