aboutsummaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/CMakeLists.txt22
-rw-r--r--apps/ble-inspect-device.cpp72
-rw-r--r--apps/sm-get-value.cpp75
-rw-r--r--apps/soil-moisture.h118
4 files changed, 287 insertions, 0 deletions
diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt
new file mode 100644
index 0000000..a138336
--- /dev/null
+++ b/apps/CMakeLists.txt
@@ -0,0 +1,22 @@
+set(APPS sm-get-value ble-inspect-device)
+
+# Boost
+find_package(Boost COMPONENTS system log thread REQUIRED)
+
+# Bluez
+pkg_check_modules(BLUEZ bluez REQUIRED)
+
+# pthreads
+find_package(Threads REQUIRED)
+
+foreach(app ${APPS})
+ include_directories("${PROJECT_SOURCE_DIR}/ble")
+
+ add_executable(${app} ${app}.cpp)
+ add_dependencies(${app} ble)
+
+ target_link_libraries(${app} ble)
+ target_link_libraries(${app} ${Boost_LIBRARIES})
+ target_link_libraries(${app} ${BLUEZ_LIBRARIES})
+ target_link_libraries(${app} ${CMAKE_THREAD_LIBS_INIT})
+endforeach(app)
diff --git a/apps/ble-inspect-device.cpp b/apps/ble-inspect-device.cpp
new file mode 100644
index 0000000..361311c
--- /dev/null
+++ b/apps/ble-inspect-device.cpp
@@ -0,0 +1,72 @@
+#include <exception>
+#include <iostream>
+#include <vector>
+#include <boost/uuid/uuid_io.hpp>
+#include "Bluetooth.h"
+
+using namespace std;
+using namespace trygvis::bluetooth;
+
+Mac *targetMac;
+
+void scan_callback(BluetoothDevice &device) {
+ device.adapter().stopScan();
+
+ if (device.mac() != *targetMac) {
+ cout << "found device: " << device.mac().str() << ", but not the one we want " << targetMac->str() << endl;
+ return;
+ }
+
+ cout << "Connecting to device: " << device.mac().str() << endl;
+
+ device.connect();
+
+ device.discoverServices();
+
+ vector<BluetoothGattService *> services = device.getServices();
+ cout << "Device has " << services.size() << " services" << endl;
+
+ for (auto &s: services) {
+ const vector<BluetoothGattCharacteristic *> characteristics = s->getCharacteristics();
+
+ cout << "Service: UUID: " << s->getUuid() << ", has " << characteristics.size() << " characteristics" << endl;
+
+ for (auto &c: characteristics) {
+ cout << "Characteristic: UUID: " << c->getUuid() << ", properties: " << (int) c->getProperties() << endl;
+ }
+ }
+
+ device.disconnect();
+}
+
+int main(int argc, char *argv[]) {
+ if (argc != 2) {
+ cerr << "usage: " << argv[0] << " [mac]" << endl;
+ return EXIT_FAILURE;
+ }
+
+ int e;
+// try {
+ Mac mac = Mac::parseMac(argv[1]);
+ targetMac = &mac;
+
+ BluetoothAdapter &adapter = getAdapter(0);
+
+ BluetoothDevice &device = adapter.getDevice(mac);
+
+ scan_callback(device);
+
+// adapter->runScan(scan_callback);
+
+ e = EXIT_SUCCESS;
+// } catch (std::runtime_error ex) {
+// W << "std::runtime_error: " << ex.what();
+// e = EXIT_FAILURE;
+// } catch (std::exception ex) {
+// W << "std::exception: " << ex.what();
+// e = EXIT_FAILURE;
+// }
+
+ shutdown();
+ return e;
+}
diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp
new file mode 100644
index 0000000..232c166
--- /dev/null
+++ b/apps/sm-get-value.cpp
@@ -0,0 +1,75 @@
+#include <exception>
+#include <iostream>
+#include <vector>
+#include <boost/uuid/uuid_io.hpp>
+#include <boost/optional.hpp>
+#include "Bluetooth.h"
+#include "soil-moisture.h"
+
+using namespace std;
+using namespace trygvis::bluetooth;
+
+Mac *targetMac;
+
+void scan_callback(BluetoothDevice &device) {
+ device.adapter().stopScan();
+
+ if (device.mac() != *targetMac) {
+ cout << "found device: " << device.mac().str() << ", but not the one we want " << targetMac->str() << endl;
+ return;
+ }
+
+ cout << "Connecting to device: " << device.mac().str() << endl;
+
+ device.connect();
+
+ device.discoverServices();
+
+ vector<BluetoothGattService *> services = device.getServices();
+ cout << "Device has " << services.size() << " services" << endl;
+
+ for (auto &s: services) {
+ const vector<BluetoothGattCharacteristic *> characteristics = s->getCharacteristics();
+
+ cout << "Service: UUID: " << s->getUuid() << ", has " << characteristics.size() << " characteristics" << endl;
+
+ for (auto &c: characteristics) {
+ cout << "Characteristic: UUID: " << c->getUuid() << ", properties: " << (int) c->getProperties() << endl;
+ }
+ }
+
+ boost::uuids::uuid soil_moisture_service;
+ boost::optional<BluetoothGattService*> service = device.findService(soil_moisture_service);
+
+ device.disconnect();
+}
+
+int main(int argc, char *argv[]) {
+ if (argc != 2) {
+ cerr << "usage: " << argv[0] << " [mac]" << endl;
+ return EXIT_FAILURE;
+ }
+
+ int e;
+// try {
+ Mac mac = Mac::parseMac(argv[1]);
+ targetMac = &mac;
+
+ BluetoothAdapter &adapter = getAdapter(0);
+
+ BluetoothDevice &device = adapter.getDevice(mac);
+
+ scan_callback(device);
+
+ e = EXIT_SUCCESS;
+// } catch (std::runtime_error ex) {
+// W << "std::runtime_error: " << ex.what();
+// e = EXIT_FAILURE;
+// } catch (std::exception ex) {
+// W << "std::exception: " << ex.what();
+// e = EXIT_FAILURE;
+// }
+
+ shutdown();
+ return e;
+}
diff --git a/apps/soil-moisture.h b/apps/soil-moisture.h
new file mode 100644
index 0000000..4f19de1
--- /dev/null
+++ b/apps/soil-moisture.h
@@ -0,0 +1,118 @@
+#ifndef SOIL_MOISTURE_H
+#define SOIL_MOISTURE_H
+
+#define SENSOR_NAME_LEN 10
+
+enum sm_cmd_code {
+ SM_CMD_GET_SENSOR_COUNT = 1,
+ SM_CMD_GET_VALUE = 2,
+ SM_CMD_SET_WARNING_VALUE = 3,
+ SM_CMD_GET_WARNING_VALUE = 4,
+ SM_CMD_SET_SENSOR_NAME = 5,
+ SM_CMD_GET_SENSOR_NAME = 6,
+ SM_CMD_SET_UPDATE_INTERVAL = 7,
+ SM_CMD_FAIL = 255,
+};
+
+struct sm_get_sensor_count_req {
+} __attribute__((packed));
+
+struct sm_get_sensor_count_res {
+ uint8_t count;
+} __attribute__((packed));
+
+struct sm_get_value_req {
+ uint8_t sensor;
+} __attribute__((packed));
+
+struct sm_get_value_res {
+ uint16_t value;
+} __attribute__((packed));
+
+struct sm_set_warning_value_req {
+ uint8_t sensor;
+ uint16_t warning_value;
+} __attribute__((packed));
+
+struct sm_set_warning_value_res {
+} __attribute__((packed));
+
+struct sm_get_warning_value_req {
+ uint8_t sensor;
+} __attribute__((packed));
+
+struct sm_get_warning_value_res {
+ uint16_t warning_value;
+} __attribute__((packed));
+
+struct sm_set_sensor_name_req {
+ uint8_t sensor;
+ uint8_t length;
+ uint8_t name[SENSOR_NAME_LEN];
+} __attribute__((packed));
+
+struct sm_set_sensor_name_res {
+} __attribute__((packed));
+
+struct sm_get_sensor_name_req {
+ uint8_t sensor;
+} __attribute__((packed));
+
+struct sm_get_sensor_name_res {
+ uint8_t length;
+ uint8_t name[SENSOR_NAME_LEN];
+} __attribute__((packed));
+
+struct sm_set_update_interval_req {
+ uint8_t sensor;
+ uint8_t interval_in_seconds;
+} __attribute__((packed));
+
+struct sm_set_update_interval_res {
+} __attribute__((packed));
+
+struct sm_req {
+ uint8_t code;
+ union {
+ struct sm_get_sensor_count_req get_sensor_count;
+ struct sm_get_value_req get_value;
+ struct sm_set_warning_value_req set_warning_value;
+ struct sm_get_warning_value_req get_warning_value;
+ struct sm_set_sensor_name_req set_sensor_name;
+ struct sm_get_sensor_name_req get_sensor_name;
+ struct sm_set_update_interval_req set_update_interval;
+ } __attribute__((packed));
+} __attribute__((packed));
+
+// len + code
+#define SM_RES_HEADER_SIZE 1
+
+struct sm_res {
+ // header
+ uint8_t code;
+
+ // body
+ union {
+ struct sm_get_sensor_count_res get_sensor_count;
+ struct sm_get_value_res get_value;
+ struct sm_set_warning_value_res set_warning_value;
+ struct sm_get_warning_value_res get_warning_value;
+ struct sm_set_sensor_name_res set_sensor_name;
+ struct sm_get_sensor_name_res get_sensor_name;
+ struct sm_set_update_interval_res set_update_interval;
+ } __attribute__((packed));
+} __attribute__((packed));
+
+#ifndef SM_DEBUG
+#define SM_DEBUG 1
+#endif
+
+#if SM_DEBUG == 1
+
+void write_req(struct sm_req const &req);
+
+void write_res(struct sm_res const &res);
+
+#endif
+
+#endif