-
Notifications
You must be signed in to change notification settings - Fork 1
/
CBlueTooth.cpp
123 lines (99 loc) · 2.14 KB
/
CBlueTooth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2012 Alexandros Nikolopoulos <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*/
#include "CBlueTooth.h"
#include "CLog.h"
CBlueTooth::CBlueTooth(map<string, string> settings)
{
m_IsConnected = false;
m_host = settings["host"];
m_Socket = 0;
}
CBlueTooth::~CBlueTooth()
{
if(m_IsConnected == false)
Disconnect();
}
int
CBlueTooth::Connect(void)
{
if(m_host.empty())
return false;
struct sockaddr_rc addr = { 0 };
m_Socket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba (m_host.c_str(), &addr.rc_bdaddr);
sleep(3);
int status = connect (m_Socket, (struct sockaddr *) &addr, sizeof (addr));
if (status < 0)
{
syslog(LOG_ERR, "BlueTooth Connect error: %s\n", strerror(errno));
return false;
}
m_IsConnected = true;
/* Get my Mac */
struct sockaddr_rc mymac = { 0 };
unsigned int mymac_size = sizeof(mymac);
getsockname(m_Socket,(struct sockaddr *) &mymac, &mymac_size);
baswap(&m_LocalAddr, &mymac.rc_bdaddr);
return true;
}
int
CBlueTooth::Disconnect(void)
{
close (m_Socket);
m_IsConnected = false;
return true;
}
int
CBlueTooth::Send(uint8_t *message, int length)
{
Log.hexDump(LOG_DEBUG, message, length);
int rv = write(m_Socket, message, length);
if(rv == -1)
{
syslog(LOG_ERR, "BlueTooth write error: %s\n", strerror(errno));
return false;
}
else
return true;
}
int
CBlueTooth::Receive(uint8_t *message, int length, time_t timeout)
{
int rv = 0;
int p = 0;
time_t StartTime = time(NULL);
while(true)
{
rv = read(m_Socket, message+p, length-p);
if(rv == -1)
{
syslog(LOG_ERR, "BlueTooth read error: %s\n", strerror(errno));
return rv;
}
else if(rv == 0 && p == 0)
{
usleep(1000);
continue;
}
else
{
p += rv;
if(time(NULL) >= StartTime || p == length)
return rv;
}
}
return rv;
}
string
CBlueTooth::GetMyAddress(void)
{
//return &m_LocalAddr;
}