Skip to content

Commit fcaae74

Browse files
authored
Merge pull request #9 from Kilemonn/remove-bluetooth-support
Remove bluetooth feature
2 parents 4dcfee1 + 738227a commit fcaae74

17 files changed

+21
-607
lines changed

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
#Test File:
2-
TestTCP
3-
TestUDP
4-
TestBluetooth
5-
61
# Prerequisites
72
*.d
83

CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ set(HEADERS
1111
src/socket/Socket.h
1212
src/socket/TCPSocket.h
1313
src/socket/UDPSocket.h
14-
src/socket/BluetoothSocket.h
1514
src/address/SocketAddress.h
1615
src/socketexceptions/BindingException.hpp
1716
src/socketexceptions/SocketException.hpp
1817
src/socketexceptions/TimeoutException.hpp
1918
src/socketexceptions/SocketError.h
2019

21-
src/enums/SocketType.h
2220
src/enums/InternetProtocolVersion.h
2321
)
2422

@@ -27,7 +25,6 @@ set(SOURCE
2725
src/socket/Socket.cpp
2826
src/socket/TCPSocket.cpp
2927
src/socket/UDPSocket.cpp
30-
src/socket/BluetoothSocket.cpp
3128
src/socketexceptions/SocketError.cpp
3229
src/address/SocketAddress.cpp
3330
)

Environment-Test-Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ FROM alpine:3.20.0 AS alpine
44

55
WORKDIR /alpine
66

7-
RUN apk update && apk upgrade && apk add g++ cmake make git bluez-dev glib-dev bluez
7+
# bluez-dev bluez
8+
RUN apk update && apk upgrade && apk add g++ cmake make git glib-dev
89

910
COPY ./src ./src
1011
COPY ./tests ./tests
@@ -22,7 +23,7 @@ FROM ubuntu:24.10 AS ubuntu
2223

2324
WORKDIR /ubuntu
2425

25-
RUN apt update && apt upgrade -y && apt install g++ make cmake git libbluetooth-dev libglib2.0-dev bluez -y
26+
RUN apt update && apt upgrade -y && apt install g++ make cmake git libglib2.0-dev -y
2627

2728
COPY ./src ./src
2829
COPY ./tests ./tests

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
# Cpp-SocketLibrary
22

3-
A ServerSocket and Socket library for Windows and Linux aiming to support both Wifi and Bluetooth communication.
3+
A ServerSocket and Socket library for Windows and Linux that supports Wifi communication.
44

55
## Getting Started
66

77
### Dependencies
88

99
- [CMake](https://cmake.org/download/) and `make`
1010

11-
The following **linux** dependencies are required:
12-
- `libbluetooth-dev`
13-
- `libglib2.0-dev`
14-
- `bluez`
15-
1611
### Building the Library and Running the Tests - Linux
1712

13+
- Make sure `libglib2.0-dev` is installed
14+
1815
1. To build the library, firstly run cmake: `cmake . -B build-linux` in the root directory of the repository (`CppSocketLibrary/`).
1916
2. Then move into the new `build-linux` folder: `cd build-linux`.
2017
3. Then you can run `make` to build the library.
@@ -35,7 +32,7 @@ The following **linux** dependencies are required:
3532
void tcpExample()
3633
{
3734
// Create a new Wifi ServerSocket
38-
kt::ServerSocket server(kt::SocketType::Wifi, 56756, 20, kt::InternetProtocolVersion::IPV6);
35+
kt::ServerSocket server(std::nullopt, 56756, 20, kt::InternetProtocolVersion::IPV6);
3936

4037
// Create new TCP socket
4138
kt::TCPSocket client("::1", server.getPort());
@@ -73,7 +70,7 @@ void udpExample()
7370
{
7471
// The socket receiving data must first be bound
7572
kt::UDPSocket socket;
76-
socket.bind(37893, kt::InternetProtocolVersion::IPV4);
73+
socket.bind(std::nullopt, 37893, kt::InternetProtocolVersion::IPV4);
7774

7875
kt::UDPSocket client;
7976
const std::string testString = "UDP test string";

src/enums/SocketType.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/serversocket/ServerSocket.cpp

Lines changed: 3 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "../socketexceptions/SocketException.hpp"
55
#include "../socketexceptions/BindingException.hpp"
66
#include "../socketexceptions/TimeoutException.hpp"
7-
#include "../enums/SocketType.h"
87
#include "../socketexceptions/SocketError.h"
98
#include "../address/SocketAddress.h"
109

@@ -34,36 +33,27 @@
3433
#include <netinet/in.h>
3534
#include <arpa/inet.h>
3635
#include <sys/ioctl.h>
37-
#include <bluetooth/hci.h>
38-
#include <bluetooth/hci_lib.h>
3936
#include <unistd.h>
4037

4138
#endif
4239

4340
namespace kt
4441
{
4542
/**
46-
* ServerSocket constructor. Creates a wifi/bluetooth ServerSocket and begins listening for connections.
43+
* ServerSocket constructor. Creates a wifi ServerSocket and begins listening for connections.
4744
*
48-
* @param type - Determines whether this ServerSocket is a wifi or bluetooth ServerSocket.
4945
* @param port - The port number for this server to communicate through. If value is not passed in a random, available port number will be assigned.
5046
* @param connectionBacklogSize - You can enter a value here to specify the length of the server connection pool. The default value is 20.
5147
*
5248
* @throw SocketException - If the ServerSocket is unable to be instanciated or begin listening.
5349
* @throw BindingException - If the ServerSocket is unable to bind to the specific port specified.
5450
*/
55-
kt::ServerSocket::ServerSocket(const kt::SocketType type, const std::optional<std::string>& localHostname, const unsigned short& port, const unsigned int& connectionBacklogSize, const kt::InternetProtocolVersion protocolVersion)
51+
kt::ServerSocket::ServerSocket(const std::optional<std::string>& localHostname, const unsigned short& port, const unsigned int& connectionBacklogSize, const kt::InternetProtocolVersion protocolVersion)
5652
{
5753
this->socketDescriptor = getInvalidSocketValue();
5854
this->port = port;
59-
this->type = type;
6055
this->protocolVersion = protocolVersion;
6156

62-
if (this->type == kt::SocketType::None)
63-
{
64-
throw SocketException("Failed to create ServerSocket with 'None' SocketType.");
65-
}
66-
6757
this->constructSocket(localHostname, connectionBacklogSize);
6858
}
6959

@@ -75,7 +65,6 @@ namespace kt
7565
kt::ServerSocket::ServerSocket(const kt::ServerSocket& socket)
7666
{
7767
this->port = socket.port;
78-
this->type = socket.type;
7968
this->protocolVersion = socket.protocolVersion;
8069
this->socketDescriptor = socket.socketDescriptor;
8170
this->serverAddress = socket.serverAddress;
@@ -91,7 +80,6 @@ namespace kt
9180
kt::ServerSocket& kt::ServerSocket::operator=(const kt::ServerSocket& socket)
9281
{
9382
this->port = socket.port;
94-
this->type = socket.type;
9583
this->protocolVersion = socket.protocolVersion;
9684
this->socketDescriptor = socket.socketDescriptor;
9785
this->serverAddress = socket.serverAddress;
@@ -101,73 +89,7 @@ namespace kt
10189

10290
void kt::ServerSocket::constructSocket(const std::optional<std::string>& localHostname, const unsigned int& connectionBacklogSize)
10391
{
104-
if (this->type == kt::SocketType::Wifi)
105-
{
106-
this->constructWifiSocket(localHostname, connectionBacklogSize);
107-
}
108-
else if (this->type == kt::SocketType::Bluetooth)
109-
{
110-
this->constructBluetoothSocket(connectionBacklogSize);
111-
this->setDiscoverable();
112-
}
113-
}
114-
115-
void kt::ServerSocket::constructBluetoothSocket(const unsigned int& connectionBacklogSize)
116-
{
117-
#ifdef _WIN32
118-
throw kt::SocketException("ServerSocket::constructBluetoothSocket(unsigned int) is not supported on Windows.");
119-
120-
/*SOCKADDR_BTH bluetoothAddress;
121-
122-
this->socketDescriptor = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
123-
124-
if (this->socketDescriptor == 0)
125-
{
126-
throw SocketException("Error establishing BT server socket: " + std::string(std::strerror(errno)));
127-
}
128-
129-
this->bluetoothAddress.addressFamily = AF_BTH;
130-
this->bluetoothAddress.btAddr = 0;
131-
this->bluetoothAddress.port = this->port;
132-
133-
if (bind(this->socketDescriptor, (sockaddr*)&this->bluetoothAddress, sizeof(SOCKADDR_BTH)) == -1)
134-
{
135-
throw BindingException("Error binding BT connection, the port " + std::to_string(this->port) + " is already being used: " + std::string(std::strerror(errno)) + ". WSA Error: " + std::to_string(WSAGetLastError()));
136-
}
137-
138-
if (listen(this->socketDescriptor, static_cast<int>(connectionBacklogSize)) == -1)
139-
{
140-
this->close();
141-
throw SocketException("Error Listening on port: " + std::to_string(this->port) + ": " + std::string(std::strerror(errno)));
142-
}*/
143-
144-
#elif __linux__
145-
sockaddr_rc localAddress = {0};
146-
this->socketDescriptor = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
147-
148-
if (isInvalidSocket(this->socketDescriptor))
149-
{
150-
throw SocketException("Error establishing BT server socket: " + std::string(std::strerror(errno)));
151-
}
152-
153-
localAddress.rc_family = AF_BLUETOOTH;
154-
localAddress.rc_bdaddr = ((bdaddr_t) {{0, 0, 0, 0, 0, 0}});
155-
localAddress.rc_channel = static_cast<uint8_t>(this->port);
156-
157-
if (bind(this->socketDescriptor, (sockaddr *)&localAddress, sizeof(localAddress)) == -1)
158-
{
159-
throw BindingException("Error binding BT connection, the port " + std::to_string(this->port) + " is already being used: " + std::string(std::strerror(errno)));
160-
}
161-
162-
if (listen(this->socketDescriptor, connectionBacklogSize) == -1)
163-
{
164-
this->close();
165-
throw SocketException("Error Listening on port " + std::to_string(this->port) + ": " + std::string(std::strerror(errno)));
166-
}
167-
#endif
168-
169-
// Make discoverable
170-
92+
this->constructWifiSocket(localHostname, connectionBacklogSize);
17193
}
17294

17395
void kt::ServerSocket::constructWifiSocket(const std::optional<std::string>& localHostname, const unsigned int& connectionBacklogSize)
@@ -248,32 +170,6 @@ namespace kt
248170
this->port = kt::getPortNumber(address.first.value());
249171
}
250172

251-
252-
void kt::ServerSocket::setDiscoverable()
253-
{
254-
throw kt::SocketException("ServerSocket::setDiscoverable() not implemented.");
255-
256-
#if __linux__
257-
hci_dev_req req;
258-
req.dev_id = 0;
259-
// req.dev_id = hci_get_route(nullptr);
260-
req.dev_opt = SCAN_PAGE | SCAN_INQUIRY;
261-
262-
if (ioctl(this->socketDescriptor, HCISETSCAN, (unsigned long)&req) < 0)
263-
{
264-
throw SocketException("Failed to make device discoverable.");
265-
}
266-
#endif
267-
}
268-
269-
/**
270-
* @return the *kt::SocketType* for this *kt::ServerSocket*.
271-
*/
272-
kt::SocketType kt::ServerSocket::getType() const
273-
{
274-
return this->type;
275-
}
276-
277173
/**
278174
* Used to get the port number that the ServerSocket is listening on.
279175
* @return An unsigned int of the port number that the ServerSocket is listening on.
@@ -334,41 +230,6 @@ namespace kt
334230
return kt::TCPSocket(temp, hostname.value(), portNum, this->getInternetProtocolVersion(), acceptedAddress);
335231
}
336232

337-
kt::BluetoothSocket kt::ServerSocket::acceptBluetoothConnection(const long& timeout)
338-
{
339-
if (timeout > 0)
340-
{
341-
int res = kt::pollSocket(this->socketDescriptor, timeout);
342-
if (res == -1)
343-
{
344-
throw kt::SocketException("Failed to poll as socket is no longer valid.");
345-
}
346-
else if (res == 0)
347-
{
348-
throw kt::TimeoutException("No applicable connections could be accepted during the time period specified " + std::to_string(timeout) + " microseconds.");
349-
}
350-
}
351-
352-
throw kt::SocketException("acceptBluetoothConnection() - Not yet implemented.");
353-
#ifdef __linux__
354-
// Remove bluetooth related code
355-
356-
// sockaddr_rc remoteDevice = { 0 };
357-
// socklen_t socketSize = sizeof(remoteDevice);
358-
// SOCKET temp = ::accept(this->socketDescriptor, (sockaddr *) &remoteDevice, &socketSize);
359-
// if (temp == -1)
360-
// {
361-
// throw SocketException("Failed to accept connection. Socket is in an invalid state.");
362-
// }
363-
364-
// if (this->type == kt::SocketType::Bluetooth)
365-
// {
366-
// char remoteAddress[1024] = {0};
367-
// ba2str(&remoteDevice.rc_bdaddr, remoteAddress);
368-
// }
369-
#endif
370-
}
371-
372233
/**
373234
* Closes the existing connection. If no connection is open, then it will do nothing.
374235
*

src/serversocket/ServerSocket.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
#include <optional>
44

55
#include "../address/SocketAddress.h"
6-
7-
#include "../socket/BluetoothSocket.h"
86
#include "../socket/TCPSocket.h"
9-
10-
#include "../enums/SocketType.h"
117
#include "../enums/InternetProtocolVersion.h"
128

139
#ifdef _WIN32
@@ -42,26 +38,21 @@ namespace kt
4238
{
4339
protected:
4440
unsigned short port = 0;
45-
kt::SocketType type = kt::SocketType::None;
4641
kt::InternetProtocolVersion protocolVersion = kt::InternetProtocolVersion::Any;
4742
kt::SocketAddress serverAddress = {};
4843
SOCKET socketDescriptor = getInvalidSocketValue();
4944

50-
void setDiscoverable();
5145
void constructSocket(const std::optional<std::string>&, const unsigned int&);
52-
void constructBluetoothSocket(const unsigned int&);
5346
void constructWifiSocket(const std::optional<std::string>& localHostname, const unsigned int&);
5447
void initialisePortNumber();
5548

5649
public:
57-
ServerSocket(const kt::SocketType, const std::optional<std::string>& = std::nullopt, const unsigned short& = 0, const unsigned int& = 20, const kt::InternetProtocolVersion = kt::InternetProtocolVersion::Any);
50+
ServerSocket(const std::optional<std::string>& = std::nullopt, const unsigned short& = 0, const unsigned int& = 20, const kt::InternetProtocolVersion = kt::InternetProtocolVersion::Any);
5851
ServerSocket(const kt::ServerSocket&);
5952
kt::ServerSocket& operator=(const kt::ServerSocket&);
6053

6154
kt::TCPSocket acceptTCPConnection(const long& = 0) const;
62-
kt::BluetoothSocket acceptBluetoothConnection(const long& = 0);
6355

64-
kt::SocketType getType() const;
6556
kt::InternetProtocolVersion getInternetProtocolVersion() const;
6657
unsigned short getPort() const;
6758
SOCKET getSocket() const;

0 commit comments

Comments
 (0)