aboutsummaryrefslogtreecommitdiff
path: root/src/HciSocket.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/HciSocket.h')
-rw-r--r--src/HciSocket.h90
1 files changed, 90 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+
+// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//
+// >>
+// >>> 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 <unistd.h>
+#include <stdint.h>
+#include <vector>
+
+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<uint8_t> &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<uint8_t> 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;
+};