Skip to content
Snippets Groups Projects
Commit 2e2cceaa authored by jdkruege's avatar jdkruege
Browse files

Start of bluetooth API, modifying code from a bluetooth connection example.

parent 7f26e3fe
No related branches found
No related tags found
No related merge requests found
Showing
with 247 additions and 11 deletions
......@@ -11,6 +11,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Communications\Bluetooth.cpp" />
<ClCompile Include="Communications.cpp" />
</ItemGroup>
<ItemGroup>
......
......@@ -18,6 +18,9 @@
<ClCompile Include="Communications.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Communications\Bluetooth.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="README.txt">
......
......@@ -64,7 +64,6 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Bluetooth.cpp" />
<ClCompile Include="Communications.cpp" />
</ItemGroup>
<ItemGroup>
......
......@@ -15,9 +15,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Bluetooth.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Communications.cpp">
<Filter>Source Files</Filter>
</ClCompile>
......
#include "Bluetooth.h"
Bluetooth::Bluetooth()
{
}
Bluetooth::~Bluetooth()
{
}
#pragma once
class Bluetooth
{
public:
Bluetooth();
~Bluetooth();
};
#include "Client.h"
bool Client::cInit(string destination)
{
if (destination.length == 18)
{
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t)1;
str2ba(dest, &addr.rc_bdaddr);
return true;
}
return false;
}
void Client::cConnect()
{
// allocate a socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// connect to server
status = connect(s, (sockaddr *)&addr, sizeof(addr));
}
bool Client::cCheckStatus()
{
if (status < 0) return false;
return true;
}
void Client::cSendMessage(string message)
{
status = write(s, message.c_str(), message.length);
}
void Client::cDisconnect()
{
close(s);
}
\ No newline at end of file
#pragma once
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
using namespace std;
namespace Client
{
sockaddr_rc addr;
int s, status;
string dest;
bool cInit(string destination);
void cConnect();
bool cCheckStatus();
void cSendMessage(string message);
void cDisconnect();
};
......@@ -11,12 +11,19 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Bluetooth.cpp" />
<ClCompile Include="Client.cpp" />
<ClCompile Include="Communications.cpp" />
<ClCompile Include="Sensors.cpp" />
<ClCompile Include="Server.cpp" />
<ClCompile Include="Source.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Bluetooth.h" />
<ClInclude Include="Client.h" />
<ClInclude Include="Communications.h" />
<ClInclude Include="Sensors.h" />
<ClInclude Include="Server.h" />
</ItemGroup>
<ItemGroup>
<Text Include="Makefile.txt" />
......
......@@ -21,6 +21,18 @@
<ClCompile Include="Sensors.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Bluetooth.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Server.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Source.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Communications.h">
......@@ -29,6 +41,15 @@
<ClInclude Include="Sensors.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Bluetooth.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Server.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Client.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="Makefile.txt">
......
#include "Server.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
void Server::launch()
{
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int s, client, bytes_read;
socklen_t opt = sizeof(rem_addr);
// allocate socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// bind socket to port 1 of the first available
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = *BDADDR_ANY;
loc_addr.rc_channel = (uint8_t)1;
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
// put socket into listening mode
listen(s, 1);
// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
ba2str(&rem_addr.rc_bdaddr, buf);
fprintf(stderr, "accepted connection from %s\n", buf);
memset(buf, 0, sizeof(buf));
// read data from the client
bytes_read = read(client, buf, sizeof(buf));
if (bytes_read > 0) {
printf("received [%s]\n", buf);
}
// close connection
close(client);
close(s);
return 0;
}
#pragma once
namespace Server
{
bool init();
void listen();
void disconnect();
void launch();
}
#include <stdio.h>
#include <iostream>
#include <string>
#include "Client.h"
#include "Server.h"
using namespace std;
using namespace Client;
using namespace Server;
int main(int argc, char **argv)
{
string choice;
cout << "Would you like to be Client or Server? ";
cin >> choice;
if (choice.compare("Client") == 0) clientSide();
else if (choice.compare("Server") == 0) serverSide();
else cout << "Wrong choice" << endl;
exit(0);
}
void clientSide()
{
string address;
bool stop = false;
string message = "";
cout << "MAC Address I need to connect to: ";
cin >> address;
if (!cInit(address))
{
cout << "That address is not properly formatted." << endl;
return;
}
cConnect();
cout << "Please input your message, if you would like to stop type \"STOP\"" << endl;
while (cCheckStatus() && !stop)
{
cin >> message;
if (message.compare("STOP")) stop = true;
}
if (!cCheckStatus())
{
perror("Oh no!");
}
cDisconnect();
return;
}
void serverSide()
{
}
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
VisualStudioVersion = 12.0.30324.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Client API", "Client API\Client API.vcxproj", "{F76D86D2-FD04-4813-AC90-7F8CD03FE37C}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Controller API", "Controller API\Controller API.vcxproj", "{C081C010-BF33-4DE3-AEDE-54975FC27D8D}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Communications", "Communications\Communications.vcxproj", "{EC81185B-84DB-4836-915B-68EB12CBEDC2}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Default Controller", "Default Controller\Default Controller.vcxproj", "{AC5B185E-4C05-4379-9A89-FF1D1CDA7775}"
EndProject
Global
......@@ -25,10 +23,6 @@ Global
{C081C010-BF33-4DE3-AEDE-54975FC27D8D}.Debug|Win32.Build.0 = Debug|Win32
{C081C010-BF33-4DE3-AEDE-54975FC27D8D}.Release|Win32.ActiveCfg = Release|Win32
{C081C010-BF33-4DE3-AEDE-54975FC27D8D}.Release|Win32.Build.0 = Release|Win32
{EC81185B-84DB-4836-915B-68EB12CBEDC2}.Debug|Win32.ActiveCfg = Debug|Win32
{EC81185B-84DB-4836-915B-68EB12CBEDC2}.Debug|Win32.Build.0 = Debug|Win32
{EC81185B-84DB-4836-915B-68EB12CBEDC2}.Release|Win32.ActiveCfg = Release|Win32
{EC81185B-84DB-4836-915B-68EB12CBEDC2}.Release|Win32.Build.0 = Release|Win32
{AC5B185E-4C05-4379-9A89-FF1D1CDA7775}.Debug|Win32.ActiveCfg = Debug|Win32
{AC5B185E-4C05-4379-9A89-FF1D1CDA7775}.Debug|Win32.Build.0 = Debug|Win32
{AC5B185E-4C05-4379-9A89-FF1D1CDA7775}.Release|Win32.ActiveCfg = Release|Win32
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment