aboutsummaryrefslogtreecommitdiff
path: root/apps/sm-get-value.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/sm-get-value.cpp')
-rw-r--r--apps/sm-get-value.cpp47
1 files changed, 45 insertions, 2 deletions
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));
+ }
+}