Skip to content

Commit

Permalink
Merge pull request #9 from Kilemonn/remove-bluetooth-support
Browse files Browse the repository at this point in the history
Remove bluetooth feature
  • Loading branch information
Kilemonn authored Dec 24, 2024
2 parents 4dcfee1 + 738227a commit fcaae74
Show file tree
Hide file tree
Showing 17 changed files with 21 additions and 607 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#Test File:
TestTCP
TestUDP
TestBluetooth

# Prerequisites
*.d

Expand Down
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ set(HEADERS
src/socket/Socket.h
src/socket/TCPSocket.h
src/socket/UDPSocket.h
src/socket/BluetoothSocket.h
src/address/SocketAddress.h
src/socketexceptions/BindingException.hpp
src/socketexceptions/SocketException.hpp
src/socketexceptions/TimeoutException.hpp
src/socketexceptions/SocketError.h

src/enums/SocketType.h
src/enums/InternetProtocolVersion.h
)

Expand All @@ -27,7 +25,6 @@ set(SOURCE
src/socket/Socket.cpp
src/socket/TCPSocket.cpp
src/socket/UDPSocket.cpp
src/socket/BluetoothSocket.cpp
src/socketexceptions/SocketError.cpp
src/address/SocketAddress.cpp
)
Expand Down
5 changes: 3 additions & 2 deletions Environment-Test-Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ FROM alpine:3.20.0 AS alpine

WORKDIR /alpine

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

COPY ./src ./src
COPY ./tests ./tests
Expand All @@ -22,7 +23,7 @@ FROM ubuntu:24.10 AS ubuntu

WORKDIR /ubuntu

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

COPY ./src ./src
COPY ./tests ./tests
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
# Cpp-SocketLibrary

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

## Getting Started

### Dependencies

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

The following **linux** dependencies are required:
- `libbluetooth-dev`
- `libglib2.0-dev`
- `bluez`

### Building the Library and Running the Tests - Linux

- Make sure `libglib2.0-dev` is installed

1. To build the library, firstly run cmake: `cmake . -B build-linux` in the root directory of the repository (`CppSocketLibrary/`).
2. Then move into the new `build-linux` folder: `cd build-linux`.
3. Then you can run `make` to build the library.
Expand All @@ -35,7 +32,7 @@ The following **linux** dependencies are required:
void tcpExample()
{
// Create a new Wifi ServerSocket
kt::ServerSocket server(kt::SocketType::Wifi, 56756, 20, kt::InternetProtocolVersion::IPV6);
kt::ServerSocket server(std::nullopt, 56756, 20, kt::InternetProtocolVersion::IPV6);

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

kt::UDPSocket client;
const std::string testString = "UDP test string";
Expand Down
11 changes: 0 additions & 11 deletions src/enums/SocketType.h

This file was deleted.

145 changes: 3 additions & 142 deletions src/serversocket/ServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "../socketexceptions/SocketException.hpp"
#include "../socketexceptions/BindingException.hpp"
#include "../socketexceptions/TimeoutException.hpp"
#include "../enums/SocketType.h"
#include "../socketexceptions/SocketError.h"
#include "../address/SocketAddress.h"

Expand Down Expand Up @@ -34,36 +33,27 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <unistd.h>

#endif

namespace kt
{
/**
* ServerSocket constructor. Creates a wifi/bluetooth ServerSocket and begins listening for connections.
* ServerSocket constructor. Creates a wifi ServerSocket and begins listening for connections.
*
* @param type - Determines whether this ServerSocket is a wifi or bluetooth ServerSocket.
* @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.
* @param connectionBacklogSize - You can enter a value here to specify the length of the server connection pool. The default value is 20.
*
* @throw SocketException - If the ServerSocket is unable to be instanciated or begin listening.
* @throw BindingException - If the ServerSocket is unable to bind to the specific port specified.
*/
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)
kt::ServerSocket::ServerSocket(const std::optional<std::string>& localHostname, const unsigned short& port, const unsigned int& connectionBacklogSize, const kt::InternetProtocolVersion protocolVersion)
{
this->socketDescriptor = getInvalidSocketValue();
this->port = port;
this->type = type;
this->protocolVersion = protocolVersion;

if (this->type == kt::SocketType::None)
{
throw SocketException("Failed to create ServerSocket with 'None' SocketType.");
}

this->constructSocket(localHostname, connectionBacklogSize);
}

Expand All @@ -75,7 +65,6 @@ namespace kt
kt::ServerSocket::ServerSocket(const kt::ServerSocket& socket)
{
this->port = socket.port;
this->type = socket.type;
this->protocolVersion = socket.protocolVersion;
this->socketDescriptor = socket.socketDescriptor;
this->serverAddress = socket.serverAddress;
Expand All @@ -91,7 +80,6 @@ namespace kt
kt::ServerSocket& kt::ServerSocket::operator=(const kt::ServerSocket& socket)
{
this->port = socket.port;
this->type = socket.type;
this->protocolVersion = socket.protocolVersion;
this->socketDescriptor = socket.socketDescriptor;
this->serverAddress = socket.serverAddress;
Expand All @@ -101,73 +89,7 @@ namespace kt

void kt::ServerSocket::constructSocket(const std::optional<std::string>& localHostname, const unsigned int& connectionBacklogSize)
{
if (this->type == kt::SocketType::Wifi)
{
this->constructWifiSocket(localHostname, connectionBacklogSize);
}
else if (this->type == kt::SocketType::Bluetooth)
{
this->constructBluetoothSocket(connectionBacklogSize);
this->setDiscoverable();
}
}

void kt::ServerSocket::constructBluetoothSocket(const unsigned int& connectionBacklogSize)
{
#ifdef _WIN32
throw kt::SocketException("ServerSocket::constructBluetoothSocket(unsigned int) is not supported on Windows.");

/*SOCKADDR_BTH bluetoothAddress;
this->socketDescriptor = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (this->socketDescriptor == 0)
{
throw SocketException("Error establishing BT server socket: " + std::string(std::strerror(errno)));
}
this->bluetoothAddress.addressFamily = AF_BTH;
this->bluetoothAddress.btAddr = 0;
this->bluetoothAddress.port = this->port;
if (bind(this->socketDescriptor, (sockaddr*)&this->bluetoothAddress, sizeof(SOCKADDR_BTH)) == -1)
{
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()));
}
if (listen(this->socketDescriptor, static_cast<int>(connectionBacklogSize)) == -1)
{
this->close();
throw SocketException("Error Listening on port: " + std::to_string(this->port) + ": " + std::string(std::strerror(errno)));
}*/

#elif __linux__
sockaddr_rc localAddress = {0};
this->socketDescriptor = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

if (isInvalidSocket(this->socketDescriptor))
{
throw SocketException("Error establishing BT server socket: " + std::string(std::strerror(errno)));
}

localAddress.rc_family = AF_BLUETOOTH;
localAddress.rc_bdaddr = ((bdaddr_t) {{0, 0, 0, 0, 0, 0}});
localAddress.rc_channel = static_cast<uint8_t>(this->port);

if (bind(this->socketDescriptor, (sockaddr *)&localAddress, sizeof(localAddress)) == -1)
{
throw BindingException("Error binding BT connection, the port " + std::to_string(this->port) + " is already being used: " + std::string(std::strerror(errno)));
}

if (listen(this->socketDescriptor, connectionBacklogSize) == -1)
{
this->close();
throw SocketException("Error Listening on port " + std::to_string(this->port) + ": " + std::string(std::strerror(errno)));
}
#endif

// Make discoverable

this->constructWifiSocket(localHostname, connectionBacklogSize);
}

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


void kt::ServerSocket::setDiscoverable()
{
throw kt::SocketException("ServerSocket::setDiscoverable() not implemented.");

#if __linux__
hci_dev_req req;
req.dev_id = 0;
// req.dev_id = hci_get_route(nullptr);
req.dev_opt = SCAN_PAGE | SCAN_INQUIRY;

if (ioctl(this->socketDescriptor, HCISETSCAN, (unsigned long)&req) < 0)
{
throw SocketException("Failed to make device discoverable.");
}
#endif
}

/**
* @return the *kt::SocketType* for this *kt::ServerSocket*.
*/
kt::SocketType kt::ServerSocket::getType() const
{
return this->type;
}

/**
* Used to get the port number that the ServerSocket is listening on.
* @return An unsigned int of the port number that the ServerSocket is listening on.
Expand Down Expand Up @@ -334,41 +230,6 @@ namespace kt
return kt::TCPSocket(temp, hostname.value(), portNum, this->getInternetProtocolVersion(), acceptedAddress);
}

kt::BluetoothSocket kt::ServerSocket::acceptBluetoothConnection(const long& timeout)
{
if (timeout > 0)
{
int res = kt::pollSocket(this->socketDescriptor, timeout);
if (res == -1)
{
throw kt::SocketException("Failed to poll as socket is no longer valid.");
}
else if (res == 0)
{
throw kt::TimeoutException("No applicable connections could be accepted during the time period specified " + std::to_string(timeout) + " microseconds.");
}
}

throw kt::SocketException("acceptBluetoothConnection() - Not yet implemented.");
#ifdef __linux__
// Remove bluetooth related code

// sockaddr_rc remoteDevice = { 0 };
// socklen_t socketSize = sizeof(remoteDevice);
// SOCKET temp = ::accept(this->socketDescriptor, (sockaddr *) &remoteDevice, &socketSize);
// if (temp == -1)
// {
// throw SocketException("Failed to accept connection. Socket is in an invalid state.");
// }

// if (this->type == kt::SocketType::Bluetooth)
// {
// char remoteAddress[1024] = {0};
// ba2str(&remoteDevice.rc_bdaddr, remoteAddress);
// }
#endif
}

/**
* Closes the existing connection. If no connection is open, then it will do nothing.
*
Expand Down
11 changes: 1 addition & 10 deletions src/serversocket/ServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
#include <optional>

#include "../address/SocketAddress.h"

#include "../socket/BluetoothSocket.h"
#include "../socket/TCPSocket.h"

#include "../enums/SocketType.h"
#include "../enums/InternetProtocolVersion.h"

#ifdef _WIN32
Expand Down Expand Up @@ -42,26 +38,21 @@ namespace kt
{
protected:
unsigned short port = 0;
kt::SocketType type = kt::SocketType::None;
kt::InternetProtocolVersion protocolVersion = kt::InternetProtocolVersion::Any;
kt::SocketAddress serverAddress = {};
SOCKET socketDescriptor = getInvalidSocketValue();

void setDiscoverable();
void constructSocket(const std::optional<std::string>&, const unsigned int&);
void constructBluetoothSocket(const unsigned int&);
void constructWifiSocket(const std::optional<std::string>& localHostname, const unsigned int&);
void initialisePortNumber();

public:
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);
ServerSocket(const std::optional<std::string>& = std::nullopt, const unsigned short& = 0, const unsigned int& = 20, const kt::InternetProtocolVersion = kt::InternetProtocolVersion::Any);
ServerSocket(const kt::ServerSocket&);
kt::ServerSocket& operator=(const kt::ServerSocket&);

kt::TCPSocket acceptTCPConnection(const long& = 0) const;
kt::BluetoothSocket acceptBluetoothConnection(const long& = 0);

kt::SocketType getType() const;
kt::InternetProtocolVersion getInternetProtocolVersion() const;
unsigned short getPort() const;
SOCKET getSocket() const;
Expand Down
Loading

0 comments on commit fcaae74

Please sign in to comment.