Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CurrentTimeService.h
00001 /* 00002 * BLE Current Time Service (subset) 00003 * 00004 * by ohneta/ Oct. 2015 00005 */ 00006 #ifndef __BLE_CURRENT_TIME_SERVICE_H__ 00007 #define __BLE_CURRENT_TIME_SERVICE_H__ 00008 00009 #include "ble/BLE.h" 00010 #include <time.h> 00011 00012 //extern Serial pc; 00013 00014 00015 enum BLE_DayofWeek { 00016 notknown = 0, 00017 Monday = 1, 00018 Tuesday, 00019 Wednesday, 00020 Thursday, 00021 Friday, 00022 Saturday, 00023 Sunday 00024 }; 00025 00026 typedef struct { 00027 uint16_t year; 00028 uint8_t month; 00029 uint8_t day; 00030 uint8_t hours; 00031 uint8_t minutes; 00032 uint8_t seconds; 00033 } BLE_DateTime; 00034 00035 typedef struct : BLE_DateTime { 00036 BLE_DayofWeek dayOfWeek; 00037 } BLE_DayDateTime; 00038 00039 typedef struct BLE_ExactTime256 : BLE_DayDateTime { 00040 uint8_t fractions256; 00041 } BLE_ExactTime256; 00042 00043 typedef struct BLE_CurrentTime : BLE_ExactTime256 { 00044 uint8_t adjustReason; 00045 } BLE_CurrentTime; 00046 00047 #define BLE_CURRENT_TIME_CHAR_VALUE_SIZE 10 00048 00049 /** 00050 * 00051 */ 00052 class CurrentTimeService { 00053 00054 protected: 00055 Ticker ticker; 00056 00057 /** 00058 * ticker callback. 00059 * interval = 1sec 00060 */ 00061 void epochtimePeriodicCallback(void) 00062 { 00063 time_t tmpEpochTime = epochTimeByDateTimeBuffer(); 00064 tmpEpochTime++; 00065 dataTimeBufferByEpochTime(&tmpEpochTime); 00066 } 00067 00068 void dataTimeBufferByEpochTime(time_t *epochTime) 00069 { 00070 struct tm *tmPtr = localtime(epochTime); 00071 00072 *(uint16_t *)&valueBytes[0] = tmPtr->tm_year + 1900; 00073 valueBytes[2] = tmPtr->tm_mon + 1; 00074 valueBytes[3] = tmPtr->tm_mday; 00075 valueBytes[4] = tmPtr->tm_hour; 00076 valueBytes[5] = tmPtr->tm_min; 00077 valueBytes[6] = tmPtr->tm_sec; 00078 valueBytes[7] = (BLE_DayofWeek)((tmPtr->tm_wday == 0) ? 7 : tmPtr->tm_wday); 00079 valueBytes[8] = 0x00; 00080 valueBytes[9] = 0x00; 00081 00082 ble.gattServer().write(currentTimeCharacteristic.getValueHandle(), valueBytes, BLE_CURRENT_TIME_CHAR_VALUE_SIZE); 00083 } 00084 00085 time_t epochTimeByDateTimeBuffer() 00086 { 00087 struct tm timep; 00088 { 00089 timep.tm_year = *(uint16_t *)&valueBytes[0] - 1900; 00090 timep.tm_mon = valueBytes[2] - 1; 00091 timep.tm_mday = valueBytes[3]; 00092 timep.tm_hour = valueBytes[4]; 00093 timep.tm_min = valueBytes[5]; 00094 timep.tm_sec = valueBytes[6]; 00095 timep.tm_isdst = 0; 00096 } 00097 time_t epochTime = mktime(&timep); 00098 00099 return epochTime; 00100 } 00101 00102 public: 00103 //------------------------------------------------------------------------------------ 00104 /** 00105 * 00106 */ 00107 CurrentTimeService(BLE &_ble, BLE_DateTime &initialDateTime) : 00108 ble(_ble), 00109 currentTimeCharacteristic( GattCharacteristic::UUID_CURRENT_TIME_CHAR, 00110 valueBytes, BLE_CURRENT_TIME_CHAR_VALUE_SIZE, BLE_CURRENT_TIME_CHAR_VALUE_SIZE, 00111 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ 00112 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY 00113 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE 00114 ) 00115 { 00116 writeDateTime(initialDateTime); 00117 ticker.attach(this, &CurrentTimeService::epochtimePeriodicCallback, 1.0); 00118 00119 GattCharacteristic *charsTable[] = {¤tTimeCharacteristic}; 00120 GattService currentTimeService(GattService::UUID_CURRENT_TIME_SERVICE, charsTable, sizeof(charsTable) / sizeof(GattCharacteristic *) ); 00121 00122 ble.addService(currentTimeService); 00123 ble.onDataWritten(this, &CurrentTimeService::onDataWritten); 00124 } 00125 00126 /** 00127 */ 00128 void writeDateTime(BLE_DateTime &dateTime) 00129 { 00130 *(uint16_t *)&valueBytes[0] = dateTime.year; 00131 valueBytes[2] = dateTime.month; 00132 valueBytes[3] = dateTime.day; 00133 valueBytes[4] = dateTime.hours; 00134 valueBytes[5] = dateTime.minutes; 00135 valueBytes[6] = dateTime.seconds; 00136 00137 // not support 00138 valueBytes[7] = 0x00; // day of week 00139 valueBytes[8] = 0x00; // Fractions256 00140 valueBytes[9] = 0x00; // Adjust Reason 00141 } 00142 00143 void writeEpochTime(time_t et) 00144 { 00145 dataTimeBufferByEpochTime(&et); 00146 } 00147 00148 /** 00149 */ 00150 void readDateTime(BLE_DateTime &dateTime) 00151 { 00152 dateTime.year = *(uint16_t *)&valueBytes[0]; 00153 dateTime.month = valueBytes[2]; 00154 dateTime.day = valueBytes[3]; 00155 dateTime.hours = valueBytes[4]; 00156 dateTime.minutes = valueBytes[5]; 00157 dateTime.seconds = valueBytes[6]; 00158 } 00159 00160 time_t readEpochTime() 00161 { 00162 return epochTimeByDateTimeBuffer(); 00163 } 00164 00165 00166 // for BLE GATT callback (optional) 00167 virtual void onDataWritten(const GattWriteCallbackParams *params) 00168 { 00169 if (params->handle == currentTimeCharacteristic.getValueHandle()) { 00170 memcpy((void *)&valueBytes, params->data, params->len); 00171 } 00172 } 00173 00174 protected: 00175 BLE &ble; 00176 uint8_t valueBytes[BLE_CURRENT_TIME_CHAR_VALUE_SIZE]; 00177 GattCharacteristic currentTimeCharacteristic; 00178 00179 }; 00180 00181 #endif /* #ifndef __BLE_CURRENT_TIME_SERVICE_H__*/ 00182
Generated on Sat Jul 23 2022 12:29:55 by
1.7.2