aboutsummaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-02-21 23:58:39 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-02-21 23:58:39 +0100
commit5926b05afa21eaac36c185e7fc458710efa30b02 (patch)
tree1d835b53fcb3dbc44b07084155a37874ce8325dc /apps
parenta76e09808905d280282a81958cb4c68f71f18ca4 (diff)
downloadble-toys-5926b05afa21eaac36c185e7fc458710efa30b02.tar.gz
ble-toys-5926b05afa21eaac36c185e7fc458710efa30b02.tar.bz2
ble-toys-5926b05afa21eaac36c185e7fc458710efa30b02.tar.xz
ble-toys-5926b05afa21eaac36c185e7fc458710efa30b02.zip
o Support for reading and writing characteristics.
Diffstat (limited to 'apps')
-rw-r--r--apps/ble-inspect-device.cpp1
-rw-r--r--apps/sm-get-value.cpp47
-rw-r--r--apps/soil-moisture.h6
3 files changed, 51 insertions, 3 deletions
diff --git a/apps/ble-inspect-device.cpp b/apps/ble-inspect-device.cpp
index e50f6ae..b9abe11 100644
--- a/apps/ble-inspect-device.cpp
+++ b/apps/ble-inspect-device.cpp
@@ -3,6 +3,7 @@
#include <vector>
#include <boost/uuid/uuid_io.hpp>
#include "Bluetooth.h"
+#include "log.h"
using namespace std;
using namespace trygvis::bluetooth;
diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp
index 4d84c45..30ef49a 100644
--- a/apps/sm-get-value.cpp
+++ b/apps/sm-get-value.cpp
@@ -2,6 +2,7 @@
#include <boost/uuid/uuid_io.hpp>
#include <boost/optional.hpp>
#include "Bluetooth.h"
+#include "soil-moisture.h"
#include "log.h"
using namespace std;
@@ -30,6 +31,8 @@ uuid_t trygvis_io_base_uuid = {
uuid_t soil_moisture_service = makeUuid(trygvis_io_base_uuid, 0x00, 0x10);
uuid_t soil_moisture_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x11);
+ByteBuffer &operator<<(ByteBuffer &bytes, const sm_req &req);
+
int main(int argc, char *argv[]) {
if (argc != 2) {
cerr << "usage: " << argv[0] << " [mac]" << endl;
@@ -57,14 +60,22 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
- auto c = (*service)->findCharacteristic(soil_moisture_characteristic);
+ auto c = service->findCharacteristic(soil_moisture_characteristic);
if (!c) {
cout << "The device is missing the soil moisture characteristic" << endl;
return EXIT_FAILURE;
}
-// auto bytes = gatt.readValue(c);
+ sm_req req = {
+ .code = SM_CMD_GET_SENSOR_COUNT,
+ };
+
+ auto requestBytes = ByteBuffer::alloc(sizeof(struct sm_req));
+ requestBytes << req;
+ gatt.writeValue(*c, requestBytes);
+
+ auto responseBytes = gatt.readValue(*c);
gatt.disconnect();
return EXIT_SUCCESS;
@@ -76,3 +87,35 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
}
+
+ByteBuffer &operator<<(ByteBuffer &bytes, const sm_req &req) {
+ bytes.write8(req.code);
+ switch (req.code) {
+ case SM_CMD_GET_SENSOR_COUNT:
+ return bytes;
+ case SM_CMD_GET_VALUE:
+ return bytes.
+ write8(req.get_value.sensor);
+ case SM_CMD_SET_WARNING_VALUE:
+ return bytes.
+ write8(req.set_warning_value.sensor).
+ write16le(req.set_warning_value.warning_value);
+ case SM_CMD_GET_WARNING_VALUE:
+ return bytes.
+ write8(req.set_warning_value.sensor);
+ case SM_CMD_SET_SENSOR_NAME:
+ return bytes.
+ write8(req.set_sensor_name.sensor).
+ write8(req.set_sensor_name.length).
+ write(req.set_sensor_name.name, req.set_sensor_name.length);
+ case SM_CMD_GET_SENSOR_NAME:
+ return bytes.
+ write8(req.get_sensor_name.sensor);
+ case SM_CMD_SET_UPDATE_INTERVAL:
+ return bytes.
+ write8(req.set_update_interval.sensor).
+ write8(req.set_update_interval.interval_in_seconds);
+ default:
+ throw runtime_error("Unknown code: " + to_string(req.code));
+ }
+}
diff --git a/apps/soil-moisture.h b/apps/soil-moisture.h
index 4f19de1..c960f79 100644
--- a/apps/soil-moisture.h
+++ b/apps/soil-moisture.h
@@ -71,8 +71,13 @@ struct sm_set_update_interval_req {
struct sm_set_update_interval_res {
} __attribute__((packed));
+#define SM_REQ_HEADER_SIZE 1
+
struct sm_req {
+ // header
uint8_t code;
+
+ // body
union {
struct sm_get_sensor_count_req get_sensor_count;
struct sm_get_value_req get_value;
@@ -84,7 +89,6 @@ struct sm_req {
} __attribute__((packed));
} __attribute__((packed));
-// len + code
#define SM_RES_HEADER_SIZE 1
struct sm_res {