| 
									
										
										
										
											2017-06-24 22:00:52 -07:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | #ifndef QT_BLE_H
 | 
					
						
							|  |  |  | #define QT_BLE_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-18 14:42:38 -07:00
										 |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | #include "core/libdivecomputer.h"
 | 
					
						
							| 
									
										
										
										
											2017-06-27 14:56:30 +02:00
										 |  |  | #include <QVector>
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | #include <QLowEnergyController>
 | 
					
						
							|  |  |  | #include <QEventLoop>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-03 19:24:39 +02:00
										 |  |  | #define HW_OSTC_BLE_DATA_RX	0
 | 
					
						
							|  |  |  | #define HW_OSTC_BLE_DATA_TX	1
 | 
					
						
							|  |  |  | #define HW_OSTC_BLE_CREDITS_RX	2
 | 
					
						
							|  |  |  | #define HW_OSTC_BLE_CREDITS_TX	3
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | class BLEObject : public QObject | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Q_OBJECT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2017-06-27 15:14:27 -07:00
										 |  |  | 	BLEObject(QLowEnergyController *c, dc_user_device_t *); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | 	~BLEObject(); | 
					
						
							|  |  |  | 	dc_status_t write(const void* data, size_t size, size_t *actual); | 
					
						
							|  |  |  | 	dc_status_t read(void* data, size_t size, size_t *actual); | 
					
						
							| 
									
										
										
										
											2017-06-27 14:56:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//TODO: need better mode of selecting the desired service than below
 | 
					
						
							|  |  |  | 	inline QLowEnergyService *preferredService() | 
					
						
							|  |  |  | 				{ return services.isEmpty() ? nullptr : services[0]; } | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | public slots: | 
					
						
							|  |  |  | 	void addService(const QBluetoothUuid &newService); | 
					
						
							|  |  |  | 	void serviceStateChanged(QLowEnergyService::ServiceState s); | 
					
						
							|  |  |  | 	void characteristcStateChanged(const QLowEnergyCharacteristic &c, const QByteArray &value); | 
					
						
							| 
									
										
										
										
											2017-07-03 21:21:02 +02:00
										 |  |  | 	void characteristicWritten(const QLowEnergyCharacteristic &c, const QByteArray &value); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | 	void writeCompleted(const QLowEnergyDescriptor &d, const QByteArray &value); | 
					
						
							| 
									
										
										
										
											2017-07-04 16:40:33 +02:00
										 |  |  | 	dc_status_t setupHwTerminalIo(QList<QLowEnergyCharacteristic>); | 
					
						
							| 
									
										
										
										
											2017-07-05 18:37:21 +02:00
										 |  |  | 	dc_status_t setHwCredit(unsigned int c); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2017-06-27 14:56:30 +02:00
										 |  |  | 	QVector<QLowEnergyService *> services; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QLowEnergyController *controller = nullptr; | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | 	QList<QByteArray> receivedPackets; | 
					
						
							| 
									
										
										
										
											2017-07-03 19:24:39 +02:00
										 |  |  | 	bool isCharacteristicWritten; | 
					
						
							| 
									
										
										
										
											2017-06-27 15:14:27 -07:00
										 |  |  | 	dc_user_device_t *device; | 
					
						
							| 
									
										
										
										
											2017-07-05 18:37:21 +02:00
										 |  |  | 	unsigned int hw_credit = 0; | 
					
						
							| 
									
										
										
										
											2017-07-03 19:24:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QList<QUuid> hwAllCharacteristics = { | 
					
						
							|  |  |  | 		"{00000001-0000-1000-8000-008025000000}", // HW_OSTC_BLE_DATA_RX
 | 
					
						
							|  |  |  | 		"{00000002-0000-1000-8000-008025000000}", // HW_OSTC_BLE_DATA_TX
 | 
					
						
							|  |  |  | 		"{00000003-0000-1000-8000-008025000000}", // HW_OSTC_BLE_CREDITS_RX
 | 
					
						
							|  |  |  | 		"{00000004-0000-1000-8000-008025000000}"  // HW_OSTC_BLE_CREDITS_TX
 | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2017-06-12 19:47:50 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *name); | 
					
						
							|  |  |  | dc_status_t qt_ble_read(dc_custom_io_t *io, void* data, size_t size, size_t *actual); | 
					
						
							|  |  |  | dc_status_t qt_ble_write(dc_custom_io_t *io, const void* data, size_t size, size_t *actual); | 
					
						
							|  |  |  | dc_status_t qt_ble_close(dc_custom_io_t *io); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |