BLE Current Time Service

BLE Current Time Service (CTS) implementation.

supported only "Current Time" Characteristics.

https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx

I don't have a device corresponding to the CTS, I cannot check it. x)

Revision:
5:2d2370b78f0e
Parent:
4:ad8739f7e30a
--- a/CurrentTimeService.h	Sat Oct 17 17:58:31 2015 +0000
+++ b/CurrentTimeService.h	Sat Oct 24 08:53:24 2015 +0000
@@ -5,13 +5,13 @@
  */
 #ifndef __BLE_CURRENT_TIME_SERVICE_H__
 #define __BLE_CURRENT_TIME_SERVICE_H__
-
+ 
 #include "ble/BLE.h"
 #include <time.h>
-
+ 
 //extern Serial  pc;
-
-
+ 
+ 
 enum BLE_DayofWeek {
     notknown = 0,
     Monday = 1,
@@ -22,7 +22,7 @@
     Saturday,
     Sunday
 };
-
+ 
 typedef struct {
     uint16_t year;
     uint8_t  month;
@@ -31,26 +31,26 @@
     uint8_t  minutes;
     uint8_t  seconds;
 } BLE_DateTime;
-
+ 
 typedef struct : BLE_DateTime {
     BLE_DayofWeek   dayOfWeek;
 } BLE_DayDateTime;
-
+ 
 typedef struct BLE_ExactTime256 : BLE_DayDateTime {
     uint8_t fractions256;
 } BLE_ExactTime256;
-
+ 
 typedef struct BLE_CurrentTime : BLE_ExactTime256 {
     uint8_t adjustReason;
 } BLE_CurrentTime;
-
+ 
 #define     BLE_CURRENT_TIME_CHAR_VALUE_SIZE      10
-
+ 
 /**
  *
  */
 class CurrentTimeService {
-
+ 
 protected:
     Ticker  ticker; 
  
@@ -64,11 +64,11 @@
         tmpEpochTime++;
         dataTimeBufferByEpochTime(&tmpEpochTime);
     }
-
+ 
     void dataTimeBufferByEpochTime(time_t *epochTime)
     {
         struct tm *tmPtr = localtime(epochTime);
-
+ 
         *(uint16_t *)&valueBytes[0] = tmPtr->tm_year + 1900;
         valueBytes[2] = tmPtr->tm_mon + 1;
         valueBytes[3] = tmPtr->tm_mday;
@@ -78,7 +78,7 @@
         valueBytes[7] = (BLE_DayofWeek)((tmPtr->tm_wday == 0) ? 7 : tmPtr->tm_wday);
         valueBytes[8] = 0x00;
         valueBytes[9] = 0x00;
-
+ 
         ble.gattServer().write(currentTimeCharacteristic.getValueHandle(), valueBytes, BLE_CURRENT_TIME_CHAR_VALUE_SIZE);
     }
     
@@ -98,7 +98,7 @@
     
         return epochTime;
     }
-
+ 
 public:
     //------------------------------------------------------------------------------------
     /**
@@ -118,11 +118,11 @@
  
         GattCharacteristic *charsTable[] = {&currentTimeCharacteristic};
         GattService  currentTimeService(GattService::UUID_CURRENT_TIME_SERVICE, charsTable, sizeof(charsTable) / sizeof(GattCharacteristic *)   );
-
+ 
         ble.addService(currentTimeService);
         ble.onDataWritten(this, &CurrentTimeService::onDataWritten);
     }
-
+ 
     /**
      */
     void writeDateTime(BLE_DateTime &dateTime)
@@ -133,12 +133,17 @@
         valueBytes[4] = dateTime.hours;
         valueBytes[5] = dateTime.minutes;
         valueBytes[6] = dateTime.seconds;
-
+ 
         // not support
         valueBytes[7] = 0x00;   // day of week
         valueBytes[8] = 0x00;   // Fractions256
         valueBytes[9] = 0x00;   // Adjust Reason
     }
+ 
+    void writeEpochTime(time_t et)
+    {
+        dataTimeBufferByEpochTime(&et);
+    }
 
     /**
      */
@@ -151,43 +156,27 @@
         dateTime.minutes  = valueBytes[5];
         dateTime.seconds  = valueBytes[6];
     }
-
  
-    // for BLE GATT callback
-    // "WRITE" is optionaly
+    time_t readEpochTime()
+    {
+        return epochTimeByDateTimeBuffer();
+    }
+ 
+ 
+    // for BLE GATT callback (optional)
     virtual void onDataWritten(const GattWriteCallbackParams *params)
     {
         if (params->handle == currentTimeCharacteristic.getValueHandle()) {
             memcpy((void *)&valueBytes, params->data, params->len);
         }
     }
-
+ 
 protected:
     BLE &ble;
     uint8_t   valueBytes[BLE_CURRENT_TIME_CHAR_VALUE_SIZE];    
     GattCharacteristic  currentTimeCharacteristic;
 
-
-
-/*
-  // for debug infos.
-    void printExactTime256Buffer()
-    {
-        BLE_CurrentTime currentTime;
-        currentTime.year      = *((uint16_t *)&valueBytes[0]);
-        currentTime.month     = valueBytes[2];
-        currentTime.day       = valueBytes[3];
-        currentTime.hours     = valueBytes[4];
-        currentTime.minutes   = valueBytes[5];
-        currentTime.seconds   = valueBytes[6];
-        currentTime.dayOfWeek = (BLE_DayofWeek)valueBytes[7];
-
-        pc.printf("%04d-%02d-%02d %02d:%02d:%02d [%02d]\n", 
-            currentTime.year, currentTime.month, currentTime.day,
-            currentTime.hours, currentTime.minutes, currentTime.seconds,
-            currentTime.dayOfWeek    );
-    }
-*/
 };
-
+ 
 #endif /* #ifndef __BLE_CURRENT_TIME_SERVICE_H__*/
+ 
\ No newline at end of file