From 06f646aec4dbce64d28bae1be6111bd833f8e79e Mon Sep 17 00:00:00 2001 From: Paul Nettle Date: Fri, 25 Aug 2017 09:30:39 -0500 Subject: Initial version 1.0 --- src/HciSocket.h | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/HciSocket.h (limited to 'src/HciSocket.h') diff --git a/src/HciSocket.h b/src/HciSocket.h new file mode 100644 index 0000000..45f5538 --- /dev/null +++ b/src/HciSocket.h @@ -0,0 +1,90 @@ +// Copyright 2017 Paul Nettle. +// +// This file is part of Gobbledegook. +// +// Gobbledegook 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 3 of the License, or +// (at your option) any later version. +// +// Gobbledegook is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Gobbledegook. If not, see . + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// +// >> +// >>> INSIDE THIS FILE +// >> +// +// Low-level socket interface used to communicate with the Bluetooth Management API (see HciAdapter.cpp) +// +// >> +// >>> DISCUSSION +// >> +// +// See the discussion at the top of HciSocket.cpp +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +#pragma once + +#include +#include +#include + +class HciSocket +{ +public: + // Initializes an unconnected socket + HciSocket(); + + // Socket destructor + // + // This will automatically disconnect the socket if it is currently connected + ~HciSocket(); + + // Connects to an HCI socket using the Bluetooth Management API protocol + // + // Returns true on success, otherwise false + bool connect(); + + // Returns true if the socket is currently connected, otherwise false + bool isConnected() const; + + // Disconnects from the HCI socket + void disconnect(); + + // Reads data from the HCI socket + // + // Raw data is read until no more data is available. If no data is available when this method initially starts to read, it will + // retry for a maximum timeout defined by `kMaxRetryTimeMS`. + // + // Returns true if any data was read successfully, otherwise false is returned in the case of an error or a timeout. + bool read(std::vector &response) const; + + // Writes the array of bytes of a given count + // + // This method returns true if the bytes were written successfully, otherwise false + bool write(std::vector buffer) const; + + // Writes the array of bytes of a given count + // + // This method returns true if the bytes were written successfully, otherwise false + bool write(const uint8_t *pBuffer, size_t count) const; + +private: + + // Utilitarian function for logging errors for the given operation + void logErrno(const char *pOperation) const; + + int fdSocket; + + const size_t kResponseMaxSize = 64 * 1024; + const int kResponseChunkSize = 1024; + const int kRetryIntervalMS = 20; + const int kMaxRetryTimeMS = 5000; +}; -- cgit v1.2.3