From 50150099b268b6081e4d0d65e0ba7d900eb88b06 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sat, 9 May 2020 22:50:02 +0200
Subject: [PATCH] feat(ess): Add humidity characteristic

---
 epicardium/ble/ble_main.c                     |  1 +
 epicardium/ble/cccd.h                         |  1 +
 epicardium/ble/ess.c                          | 58 ++++++++++++++++++-
 epicardium/ble/ess.h                          |  5 ++
 .../BTLE/stack/ble-host/include/att_uuid.h    |  2 +
 .../ble-host/sources/stack/att/att_uuid.c     |  1 +
 6 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/epicardium/ble/ble_main.c b/epicardium/ble/ble_main.c
index 4557a322a..98db125d3 100644
--- a/epicardium/ble/ble_main.c
+++ b/epicardium/ble/ble_main.c
@@ -230,6 +230,7 @@ static const attsCccSet_t bleCccSet[BLE_NUM_CCC_IDX] =
   {GATT_SC_CH_CCC_HDL,    ATT_CLIENT_CFG_INDICATE,  DM_SEC_LEVEL_NONE},   /* BLE_GATT_SC_CCC_IDX */
   {BATT_LVL_CH_CCC_HDL,   ATT_CLIENT_CFG_NOTIFY,    DM_SEC_LEVEL_NONE},   /* BLE_BATT_LVL_CCC_IDX */
   {ESS_TEMP_CH_CCC_HDL,   ATT_CLIENT_CFG_NOTIFY,    DM_SEC_LEVEL_NONE},   /* BLE_ESS_TEMP_CCC_IDX */
+  {ESS_HUMI_CH_CCC_HDL,   ATT_CLIENT_CFG_NOTIFY,    DM_SEC_LEVEL_NONE},   /* BLE_ESS_HUMI_CCC_IDX */
 };
 
 /**************************************************************************************************
diff --git a/epicardium/ble/cccd.h b/epicardium/ble/cccd.h
index 9e2907290..4ec4e5702 100644
--- a/epicardium/ble/cccd.h
+++ b/epicardium/ble/cccd.h
@@ -5,6 +5,7 @@ enum
   BLE_GATT_SC_CCC_IDX,                    /*! GATT service, service changed characteristic */
   BLE_BATT_LVL_CCC_IDX,                   /*! Battery service, battery level characteristic */
   BLE_ESS_TEMP_CCC_IDX,                   /*! Environmental sensing service, temperature characteristic */
+  BLE_ESS_HUMI_CCC_IDX,                   /*! Environmental sensing service, humidity characteristic */
   BLE_NUM_CCC_IDX
 };
 
diff --git a/epicardium/ble/ess.c b/epicardium/ble/ess.c
index 03fbdca3e..b5dab229f 100644
--- a/epicardium/ble/ess.c
+++ b/epicardium/ble/ess.c
@@ -27,15 +27,29 @@ static const uint8_t UUID_char_temperature[] = {
 	UINT16_TO_BYTES(ATT_UUID_TEMPERATURE)
 };
 
+/* BLE UUID for humidity */
+static const uint8_t UUID_char_humidity[] = {
+	ATT_PROP_READ | ATT_PROP_NOTIFY,
+	UINT16_TO_BYTES(ESS_HUMIDITY_VAL_HDL),
+	UINT16_TO_BYTES(ATT_UUID_HUMIDITY)
+};
+
 static const uint16_t UUID_char_len = sizeof(UUID_char_temperature);
 
 static uint8_t initTemperatureValue[] = { UINT16_TO_BYTES(0) };
 static uint16_t initTemperatureLen = sizeof(initTemperatureValue);
 
-/* Battery level client characteristic configuration */
+static uint8_t initHumidityValue[] = { UINT16_TO_BYTES(0) };
+static uint16_t initHumidityLen = sizeof(initHumidityValue);
+
+/* Temperature client characteristic configuration */
 static uint8_t essValTempChCcc[] = {UINT16_TO_BYTES(0x0000)};
 static const uint16_t essLenTempChCcc = sizeof(essValTempChCcc);
 
+/* Humidity client characteristic configuration */
+static uint8_t essValHumidityChCcc[] = {UINT16_TO_BYTES(0x0000)};
+static const uint16_t essLenHumidityChCcc = sizeof(essValHumidityChCcc);
+
 /* clang-format on */
 
 /*
@@ -79,6 +93,35 @@ static const attsAttr_t ESSSvcAttrList[] = {
 			(ATTS_PERMIT_READ |
 			 ATTS_PERMIT_WRITE) // How about security?
 	},
+
+	// Humidity
+	{
+		.pUuid       = attChUuid,
+		.pValue      = (uint8_t *)UUID_char_humidity,
+		.pLen        = (uint16_t *)&UUID_char_len,
+		.maxLen      = sizeof(UUID_char_humidity),
+		.permissions = ATTS_PERMIT_READ,
+	},
+	{
+		.pUuid       = attHumidityChUuid,
+		.pValue      = initHumidityValue,
+		.pLen        = &initHumidityLen,
+		.maxLen      = sizeof(initHumidityValue),
+		.permissions = ATTS_PERMIT_READ | ATTS_PERMIT_READ_ENC |
+			       ATTS_PERMIT_READ_AUTH,
+	},
+	/* Characteristic CCC descriptor */
+	{
+		.pUuid    = attCliChCfgUuid,
+		.pValue   = essValHumidityChCcc,
+		.pLen     = (uint16_t *)&essLenHumidityChCcc,
+		.maxLen   = sizeof(essValHumidityChCcc),
+		.settings = ATTS_SET_CCC,
+		.permissions =
+			(ATTS_PERMIT_READ |
+			 ATTS_PERMIT_WRITE) // How about security?
+	},
+
 };
 
 // validating that the service really has all charateristics
@@ -129,6 +172,11 @@ void bleESS_update(void)
 	uint16_t i16 = 0;
 	AttsSetAttr(ESS_TEMPERATURE_VAL_HDL, sizeof(i16), (uint8_t *)&i16);
 
+	uint16_t humidity = 0;
+	AttsSetAttr(
+		ESS_HUMIDITY_VAL_HDL, sizeof(humidity), (uint8_t *)&humidity
+	);
+
 	dmConnId_t connId = AppConnIsOpen();
 	if (connId != DM_CONN_ID_NONE) {
 		if (AttsCccEnabled(connId, BLE_ESS_TEMP_CCC_IDX)) {
@@ -139,6 +187,14 @@ void bleESS_update(void)
 				(uint8_t *)&i16
 			);
 		}
+		if (AttsCccEnabled(connId, BLE_ESS_HUMI_CCC_IDX)) {
+			AttsHandleValueNtf(
+				connId,
+				ESS_HUMIDITY_VAL_HDL,
+				sizeof(humidity),
+				(uint8_t *)&humidity
+			);
+		}
 	}
 }
 
diff --git a/epicardium/ble/ess.h b/epicardium/ble/ess.h
index d52439891..3bd5cf974 100644
--- a/epicardium/ble/ess.h
+++ b/epicardium/ble/ess.h
@@ -12,6 +12,11 @@ enum {
 	ESS_TEMPERATURE_CH_HDL,
 	ESS_TEMPERATURE_VAL_HDL,
 	ESS_TEMP_CH_CCC_HDL,                  /*!< Temperature CCCD */
+	/*!< \brief humidity characteristic */
+	ESS_HUMIDITY_CH_HDL,
+	ESS_HUMIDITY_VAL_HDL,
+	ESS_HUMI_CH_CCC_HDL,                  /*!< Humidity CCCD */
+
 	/*!< \brief Maximum handle. */
 	ESS_MAX_HDL
 };
diff --git a/lib/sdk/Libraries/BTLE/stack/ble-host/include/att_uuid.h b/lib/sdk/Libraries/BTLE/stack/ble-host/include/att_uuid.h
index ee9bfd0b8..ee28d104a 100644
--- a/lib/sdk/Libraries/BTLE/stack/ble-host/include/att_uuid.h
+++ b/lib/sdk/Libraries/BTLE/stack/ble-host/include/att_uuid.h
@@ -215,6 +215,7 @@ extern "C" {
 #define ATT_UUID_CTE_INTERVAL               0x7F84    /*!< \brief Constant Tone Extension interval */
 #define ATT_UUID_CTE_PHY                    0x7F85    /*!< \brief Constant Tone Extension PHY */
 #define ATT_UUID_TEMPERATURE                0x2A6E
+#define ATT_UUID_HUMIDITY                   0x2A6F
 /**@}*/
 
 /** \name GATT Unit UUIDs
@@ -534,6 +535,7 @@ extern const uint8_t attCteTxDurChUuid[ATT_16_UUID_LEN]; /*!< \brief Constant To
 extern const uint8_t attCteIntChUuid[ATT_16_UUID_LEN];   /*!< \brief Constant Tone Extension interval */
 extern const uint8_t attCtePhyChUuid[ATT_16_UUID_LEN];   /*!< \brief Constant Tone Extension PHY */
 extern const uint8_t attTemperatureChUuid[ATT_16_UUID_LEN];
+extern const uint8_t attHumidityChUuid[ATT_16_UUID_LEN];
 /**@}*/
 
 /*! \} */    /* STACK_ATT_API */
diff --git a/lib/sdk/Libraries/BTLE/stack/ble-host/sources/stack/att/att_uuid.c b/lib/sdk/Libraries/BTLE/stack/ble-host/sources/stack/att/att_uuid.c
index 5df838302..5efcfc750 100644
--- a/lib/sdk/Libraries/BTLE/stack/ble-host/sources/stack/att/att_uuid.c
+++ b/lib/sdk/Libraries/BTLE/stack/ble-host/sources/stack/att/att_uuid.c
@@ -187,3 +187,4 @@ const uint8_t attCteTxDurChUuid[ATT_16_UUID_LEN] = {UINT16_TO_BYTES(ATT_UUID_CTE
 const uint8_t attCteIntChUuid[ATT_16_UUID_LEN] =   {UINT16_TO_BYTES(ATT_UUID_CTE_INTERVAL)};
 const uint8_t attCtePhyChUuid[ATT_16_UUID_LEN] =   {UINT16_TO_BYTES(ATT_UUID_CTE_PHY)};
 const uint8_t attTemperatureChUuid[ATT_16_UUID_LEN] = {UINT16_TO_BYTES(ATT_UUID_TEMPERATURE)};
+const uint8_t attHumidityChUuid[ATT_16_UUID_LEN] = {UINT16_TO_BYTES(ATT_UUID_HUMIDITY)};
-- 
GitLab