Maxim Integrated's IoT development kit.

Dependencies:   MAX30101 MAX30003 MAX113XX_Pixi MAX30205 max32630fthr USBDevice

Revision:
15:0d47d5879a21
Parent:
13:fba77a5d0fa0
--- a/main.cpp	Thu Jul 19 15:06:27 2018 +0300
+++ b/main.cpp	Thu Jul 19 16:50:40 2018 +0300
@@ -37,6 +37,7 @@
 #include "ble/BLE.h"
 #include "ble/Gap.h"
 #include "max32630fthr.h"
+#include "ble_gatt.h"
 #if defined(LIB_MAX30205)
 #	include "max30205_app.h"
 #endif
@@ -69,75 +70,116 @@
 /* Hardware serial port over DAPLink */
 Serial daplink(USBTX, USBRX, 115200);
 
-int aliveLedEventId;
+int alive_led_event_id;
 
 /******************************************************************************/
 const static char     DEVICE_NAME[] = MAXIM_PLATFORM_NAME;
-static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
+//static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
 
 /* Set Up custom Characteristics */
-UUID iotServiceUUID  ("00001520-1d66-11e8-b467-0ed5f89f718b");
+UUID iot_service_uuid("00001520-1d66-11e8-b467-0ed5f89f718b");
+
+static ble_desc_gatt_cpf_t cpf_float32 = {.format = BLE_DESC_GATT_CPF_FORMAT_FLOAT32};
+static ble_desc_gatt_cpf_t cpf_uint16 = {.format = BLE_DESC_GATT_CPF_FORMAT_UINT16};
+static ble_desc_gatt_cpf_t cpf_uint8 = {.format = BLE_DESC_GATT_CPF_FORMAT_UINT8};
+
+GattAttribute gatt_attr_cpf_format_float32(BLE_UUID_DESCRIPTOR_CHAR_PRESENTATION_FORMAT, (uint8_t *)&cpf_float32, sizeof(ble_desc_gatt_cpf_t));
+GattAttribute gatt_attr_cpf_format_uint16(BLE_UUID_DESCRIPTOR_CHAR_PRESENTATION_FORMAT, (uint8_t *)&cpf_uint16, sizeof(ble_desc_gatt_cpf_t));
+GattAttribute gatt_attr_cpf_format_uint8(BLE_UUID_DESCRIPTOR_CHAR_PRESENTATION_FORMAT, (uint8_t *)&cpf_uint8, sizeof(ble_desc_gatt_cpf_t));
 
-UUID uuidButtonPressedNotify("00001522-1d66-11e8-b467-0ed5f89f718b");
-static uint8_t buttonPressedCount = 0;
-GattCharacteristic gattCharButtonPressedNotify(uuidButtonPressedNotify, &buttonPressedCount, 1, 1,
-							  	  	  	    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+UUID gatt_char_uuid_pushbutton("00001522-1d66-11e8-b467-0ed5f89f718b");
+static uint8_t pushbutton_press_count = 0;
+GattAttribute pushbutton_user_desc_descriptor(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Push Button", sizeof("Push Button"));
+GattAttribute *pushbutton_descriptors[] = {&pushbutton_user_desc_descriptor, &gatt_attr_cpf_format_uint8};
+GattCharacteristic gatt_char_pushbutton(gatt_char_uuid_pushbutton, &pushbutton_press_count, 1, 1,
+										GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY,
+										pushbutton_descriptors,
+										sizeof(pushbutton_descriptors) / sizeof(GattAttribute*));
 
-UUID uuidRGBLED("00001523-1d66-11e8-b467-0ed5f89f718b");
-static uint8_t RGBLedInitValue[] = {LED_OFF, LED_OFF, LED_OFF};
-ReadWriteArrayGattCharacteristic<uint8_t, sizeof(RGBLedInitValue)> gattCharRGBLed(uuidRGBLED, RGBLedInitValue);
+UUID gatt_char_uuid_led("00001523-1d66-11e8-b467-0ed5f89f718b");
+static uint8_t led_init_value[] = {LED_OFF, LED_OFF, LED_OFF};
+GattAttribute led_user_desc_descriptor(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"LED", sizeof("LED"));
+GattAttribute *led_descriptors[] = {&led_user_desc_descriptor};
+ReadWriteArrayGattCharacteristic<uint8_t, sizeof(led_init_value)> gatt_char_led(gatt_char_uuid_led, led_init_value,
+																				GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NONE,
+																				led_descriptors,
+																				sizeof(led_descriptors) / sizeof(GattAttribute*));
 
 #if defined(LIB_MAX30003)
-UUID uuidBPM("00001524-1d66-11e8-b467-0ed5f89f718b");
-static float BPMInitValue;
-ReadOnlyGattCharacteristic<float> gattCharBPM(uuidBPM, &BPMInitValue,
-									GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+UUID gatt_char_uuid_bpm("00001524-1d66-11e8-b467-0ed5f89f718b");
+static float bpm_init_value;
+GattAttribute bpm_user_desc_descriptor(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"BPM", sizeof("BPM"));
+GattAttribute *bpm_descriptors[] = {&bpm_user_desc_descriptor, &gatt_attr_cpf_format_float32};
+ReadOnlyGattCharacteristic<float> gatt_char_bpm(gatt_char_uuid_bpm, &bpm_init_value,
+												GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY,
+												bpm_descriptors,
+												sizeof(bpm_descriptors) / sizeof(GattAttribute*));
 #endif
 
 #if defined(LIB_MAX30101)
-UUID uuidHeartRate("00001525-1d66-11e8-b467-0ed5f89f718b");
-static uint16_t HeartRateInitValue;
-ReadOnlyGattCharacteristic<uint16_t> gattCharHeartRate(uuidHeartRate, &HeartRateInitValue,
-									 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+UUID gatt_char_uuid_heartrate("00001525-1d66-11e8-b467-0ed5f89f718b");
+static uint16_t heartrate_init_value;
+GattAttribute heartrate_user_desc_descriptor(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Heart Rate", sizeof("Heart Rate"));
+GattAttribute *heartrate_descriptors[] = {&heartrate_user_desc_descriptor, &gatt_attr_cpf_format_uint16};
+ReadOnlyGattCharacteristic<uint16_t> gatt_char_heartrate(gatt_char_uuid_heartrate,
+														 &heartrate_init_value,
+														 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY,
+														 heartrate_descriptors,
+														 sizeof(heartrate_descriptors) / sizeof(GattAttribute*));
 
-UUID uuidSPO2("00001526-1d66-11e8-b467-0ed5f89f718b");
-static uint16_t SPO2InitValue;
-ReadOnlyGattCharacteristic<uint16_t> gattCharSPO2(uuidSPO2, &SPO2InitValue,
-									 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+UUID gatt_char_uuid_spo2("00001526-1d66-11e8-b467-0ed5f89f718b");
+static uint16_t spo2_init_value;
+GattAttribute spo2_user_desc_descriptor(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"SPO2", sizeof("SPO2"));
+GattAttribute *spo2_descriptors[] = {&spo2_user_desc_descriptor, &gatt_attr_cpf_format_uint16};
+ReadOnlyGattCharacteristic<uint16_t> gatt_char_spo2(gatt_char_uuid_spo2,
+													&spo2_init_value,
+													GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY,
+													spo2_descriptors,
+													sizeof(spo2_descriptors) / sizeof(GattAttribute*));
 #endif
 
 #if defined(LIB_MAX113XX_PIXI)
-UUID uuidADC("00001527-1d66-11e8-b467-0ed5f89f718b");
-static float ADCInitValue;
-ReadOnlyGattCharacteristic<float> gattCharADC(uuidADC, &ADCInitValue,
-									 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+UUID gatt_char_uuid_voltage("00001527-1d66-11e8-b467-0ed5f89f718b");
+static float voltage_init_value;
+GattAttribute voltage_user_desc_descriptor(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"ADC Voltage", sizeof("ADC Voltage"));
+GattAttribute *voltage_descriptors[] = {&voltage_user_desc_descriptor, &gatt_attr_cpf_format_float32};
+ReadOnlyGattCharacteristic<float> gatt_char_voltage(gatt_char_uuid_voltage,
+													&voltage_init_value,
+													GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY,
+													voltage_descriptors,
+													sizeof(voltage_descriptors) / sizeof(GattAttribute*));
 #endif
 
 #if defined(LIB_MAX30205)
-UUID uuidTemp("00001528-1d66-11e8-b467-0ed5f89f718b");
-static float TempInitValue;
-ReadOnlyGattCharacteristic<float> gattCharTemp(uuidTemp, &TempInitValue,
-								  GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+UUID gatt_char_uuid_temp("00001528-1d66-11e8-b467-0ed5f89f718b");
+static float temp_init_value;
+GattAttribute temp_user_desc_descriptor(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Temperature", sizeof("Temperature"));
+GattAttribute *temp_descriptors[] = {&temp_user_desc_descriptor, &gatt_attr_cpf_format_float32};
+ReadOnlyGattCharacteristic<float> gatt_char_temp(gatt_char_uuid_temp,
+												 &temp_init_value,
+												 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY,
+												 temp_descriptors,
+												 sizeof(temp_descriptors) / sizeof(GattAttribute*));
 #endif
 
 /* Set up custom service */
-GattCharacteristic *characteristics[] = {&gattCharRGBLed, &gattCharButtonPressedNotify,
+GattCharacteristic *characteristics[] = {&gatt_char_led, &gatt_char_pushbutton,
 #if defined(LIB_MAX30003)
-										 &gattCharBPM,
+										 &gatt_char_bpm,
 #endif
 #if defined(LIB_MAX30205)
-										 &gattCharTemp,
+										 &gatt_char_temp,
 #endif
 #if defined(LIB_MAX30101)
-										 &gattCharHeartRate,
-										 &gattCharSPO2,
+										 &gatt_char_heartrate,
+										 &gatt_char_spo2,
 #endif
 #if defined(LIB_MAX113XX_PIXI)
-										 &gattCharADC,
+										 &gatt_char_voltage,
 #endif
 };
 
-GattService iotService(iotServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+GattService iot_gatt_service(iot_service_uuid, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
 
 /******************************************************************************/
 
@@ -145,12 +187,12 @@
 
 void updateButtonState(uint8_t newState) {
 	printf("Button pressed...\r\n");
-	bleGattAttrWrite(gattCharButtonPressedNotify.getValueHandle(), (uint8_t *)&newState, sizeof(uint8_t));
+	bleGattAttrWrite(gatt_char_pushbutton.getValueHandle(), (uint8_t *)&newState, sizeof(uint8_t));
 }
 
 void buttonPressedCallback(void)
 {
-	eventQueue.call(Callback<void(uint8_t)>(&updateButtonState), ++buttonPressedCount);
+	eventQueue.call(Callback<void(uint8_t)>(&updateButtonState), ++pushbutton_press_count);
 }
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
@@ -183,7 +225,7 @@
  */
 void onDataWrittenCallback(const GattWriteCallbackParams *params)
 {
-	if ((params->handle == gattCharRGBLed.getValueHandle()) && (params->len >= 3)) {
+	if ((params->handle == gatt_char_led.getValueHandle()) && (params->len >= 3)) {
 		rLED = (params->data[0] != 0) ? LED_OFF : LED_ON;
 		gLED = (params->data[1] != 0) ? LED_OFF : LED_ON;
 		bLED = (params->data[2] != 0) ? LED_OFF : LED_ON;
@@ -211,11 +253,11 @@
 
 	ble.gattServer().onDataWritten(onDataWrittenCallback);
 
-	ble.gattServer().addService(iotService);
+	ble.gattServer().addService(iot_gatt_service);
 
 	/* setup advertising */
 	ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-	ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+	//ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
 	ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
 	ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
 	ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
@@ -234,7 +276,7 @@
 	osStatus status;
 	rLED = LED_OFF; gLED = LED_OFF; bLED = LED_OFF;
 
-	aliveLedEventId = eventQueue.call_every(1000, blinkCallback);
+	alive_led_event_id = eventQueue.call_every(1000, blinkCallback);
 
 	printf("Initializing BLE service...\r\n");
 
@@ -246,7 +288,7 @@
 	Thread thread_max30205_reader;
 	struct max30205_reader_task_args args_max30205 = {
 			i2c1,
-			gattCharTemp.getValueHandle(),
+			gatt_char_temp.getValueHandle(),
 			MAX30205_BLE_NOTIFY_PERIOD_SEC};
 	status = thread_max30205_reader.start(callback(max30205_reader_task, &args_max30205));
 	if (status != osOK) {
@@ -259,8 +301,8 @@
 	struct max30101_reader_task_args args_max30101 = {
 			&thread_max30101_reader,
 			i2c1, P3_2, P3_3,
-			gattCharHeartRate.getValueHandle(),
-			gattCharSPO2.getValueHandle(),
+			gatt_char_heartrate.getValueHandle(),
+			gatt_char_spo2.getValueHandle(),
 			MAX30101_BLE_NOTIFY_PERIOD_SEC};
 	status = thread_max30101_reader.start(callback(max30101_reader_task, &args_max30101));
 	if (status != osOK) {
@@ -273,7 +315,7 @@
 	struct max30003_reader_task_args args_max30003 = {
 			&thread_max30003_reader,
 			spim2, SPI2_SS,
-			gattCharBPM.getValueHandle(),
+			gatt_char_bpm.getValueHandle(),
 			MAX30003_BLE_NOTIFY_PERIOD_SEC};
 	status = thread_max30003_reader.start(callback(max30003_reader_task, &args_max30003));
 	if (status != osOK) {
@@ -285,7 +327,7 @@
 	Thread thread_max11301_reader;
 	struct max11301_reader_task_args args_max11301 = {
 			i2c1,
-			gattCharADC.getValueHandle(),
+			gatt_char_voltage.getValueHandle(),
 			MAX113XX_PIXI_BLE_NOTIFY_PERIOD_SEC};
 	status = thread_max11301_reader.start(callback(max11301_reader_task, &args_max11301));
 	if (status != osOK) {