aboutsummaryrefslogtreecommitdiff
path: root/apps/sm-get-value.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-02-22 16:16:48 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-02-22 16:16:48 +0100
commitd0fb1850606ce003887259675e271a9aade4fda0 (patch)
tree0bf3be59348c0fd64f17c9e9a727e85ffc7bd98b /apps/sm-get-value.cpp
parentba6324930118a62e05d3869f99543c4a7507f105 (diff)
downloadble-toys-d0fb1850606ce003887259675e271a9aade4fda0.tar.gz
ble-toys-d0fb1850606ce003887259675e271a9aade4fda0.tar.bz2
ble-toys-d0fb1850606ce003887259675e271a9aade4fda0.tar.xz
ble-toys-d0fb1850606ce003887259675e271a9aade4fda0.zip
o Starting a new class for all SoilMoisture GATT stuff.
Diffstat (limited to 'apps/sm-get-value.cpp')
-rw-r--r--apps/sm-get-value.cpp120
1 files changed, 5 insertions, 115 deletions
diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp
index e4ddc16..46177f2 100644
--- a/apps/sm-get-value.cpp
+++ b/apps/sm-get-value.cpp
@@ -2,100 +2,22 @@
#include <boost/uuid/uuid_io.hpp>
#include <boost/optional.hpp>
#include "ble/Bluetooth.h"
-#include "soil-moisture.h"
+#include "SoilMoisture.h"
using namespace std;
using namespace trygvis::bluetooth;
-
-#define BLUETOOTH_UUID_INITIALIZER \
- { \
- 0x00, 0x00, 0x00, 0x00, \
- 0x00, 0x00, \
- 0x10, 0x00, \
- 0x80, 0x00, \
- 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb \
- };
-
-auto trygvis_io_base_uuid = boost::uuids::uuid {
- 0x32, 0xd0, 0x00, 0x00,
- 0x03, 0x5d,
- 0x59, 0xc5,
- 0x70, 0xd3,
- 0xbc, 0x8e, 0x4a, 0x1f, 0xd8, 0x3f};
-
-auto soil_moisture_service = makeUuid(trygvis_io_base_uuid, 0x00, 0x10);
-auto soil_moisture_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x11);
-
-ByteBuffer &operator<<(ByteBuffer &bytes, const sm_req &req);
+using namespace trygvis::soil_moisture;
bool loop = true;
-ByteBuffer writeAndRead(BluetoothGatt &gatt, const BluetoothGattCharacteristic &c, const ByteBuffer &requestBytes) {
- uint8_t expectedCode = requestBytes.get8(0);
-
- gatt.writeValue(c, requestBytes);
-
- auto responseBytes = gatt.readValue(c);
-
- if (responseBytes.getSize() < 1) {
- throw runtime_error("Unexpected number of bytes read: " + to_string(requestBytes.getSize()));
- }
-
- uint8_t actualCode = responseBytes.read8();
- if (actualCode != expectedCode) {
- throw runtime_error("Unexpected response code: " + to_string(actualCode) + ", expected " + to_string(expectedCode));
- }
-
- return responseBytes;
-}
-
-uint8_t getSensorCount(BluetoothGatt &gatt, BluetoothGattCharacteristic &c) {
- sm_req req = {
- .code = SM_CMD_GET_SENSOR_COUNT,
- };
-
- auto requestBytes = ByteBuffer::alloc(sizeof(struct sm_req));
- requestBytes << req;
- requestBytes.setCursor(0);
-
- return writeAndRead(gatt, c, requestBytes).read8();
-}
-
-uint16_t getValue(BluetoothGatt &gatt, BluetoothGattCharacteristic &c, uint8_t sensor) {
- sm_req req = {
- .code = SM_CMD_GET_VALUE,
- };
- req.get_value = {
- .sensor = sensor,
- };
-
- auto requestBytes = ByteBuffer::alloc(sizeof(struct sm_req));
- requestBytes << req;
- requestBytes.setCursor(0);
-
- return writeAndRead(gatt, c, requestBytes).read16le();
-}
-
void withConnection(BluetoothGatt &gatt) {
- gatt.discoverServices();
+ SoilMoisture soilMoisture = SoilMoisture::create(gatt);
- auto service = gatt.findService(soil_moisture_service);
-
- if (!service) {
- throw runtime_error("The device is missing the soil moisture service");
- }
-
- auto c = service->findCharacteristic(soil_moisture_characteristic);
-
- if (!c) {
- throw runtime_error("The device is missing the soil moisture characteristic");
- }
-
- int sensorCount = getSensorCount(gatt, *c);
+ int sensorCount = soilMoisture.getSensorCount();
while (loop) {
for (uint8_t i = 0; i < sensorCount; i++) {
- uint16_t value = getValue(gatt, *c, i);
+ uint16_t value = soilMoisture.getValue(i);
cout << "sensor=" << to_string(i) << ", value=" << (int) value << endl;
}
@@ -141,35 +63,3 @@ 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));
- }
-}