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.
Dependencies: BLE_API nRF51822 mbed
Fork of KS7 by
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 #include "ble_date_time.h" 00012 #include "common.h" 00013 //extern Serial pc; 00014 00015 00016 enum BLE_DayofWeek { 00017 notknown = 0, 00018 Monday = 1, 00019 Tuesday, 00020 Wednesday, 00021 Thursday, 00022 Friday, 00023 Saturday, 00024 Sunday 00025 }; 00026 00027 typedef struct : ble_date_time_t/*BLE_DateTime*/ { 00028 BLE_DayofWeek dayOfWeek; 00029 } BLE_DayDateTime; 00030 00031 typedef struct BLE_ExactTime256 : BLE_DayDateTime { 00032 uint8_t fractions256; 00033 } BLE_ExactTime256; 00034 00035 typedef struct BLE_CurrentTime : BLE_ExactTime256 { 00036 uint8_t adjustReason; 00037 } BLE_CurrentTime; 00038 00039 #define BLE_CURRENT_TIME_CHAR_VALUE_SIZE 10 00040 00041 /** 00042 * 00043 */ 00044 class CurrentTimeService { 00045 00046 protected: 00047 Ticker ticker; 00048 00049 /** 00050 * ticker callback. 00051 * interval = 1sec 00052 */ 00053 void epochtimePeriodicCallback(void) 00054 { 00055 time_t tmpEpochTime = epochTimeByDateTimeBuffer(); 00056 tmpEpochTime++; 00057 dataTimeBufferByEpochTime(&tmpEpochTime); 00058 00059 counter++; 00060 tmCnt--; 00061 } 00062 00063 void dataTimeBufferByEpochTime(time_t *epochTime) 00064 { 00065 struct tm *tmPtr = localtime(epochTime); 00066 00067 *(uint16_t *)&valueBytes[0] = tmPtr->tm_year + 1900; 00068 valueBytes[2] = tmPtr->tm_mon + 1; 00069 valueBytes[3] = tmPtr->tm_mday; 00070 valueBytes[4] = tmPtr->tm_hour; 00071 valueBytes[5] = tmPtr->tm_min; 00072 valueBytes[6] = tmPtr->tm_sec; 00073 valueBytes[7] = (BLE_DayofWeek)((tmPtr->tm_wday == 0) ? 7 : tmPtr->tm_wday); 00074 valueBytes[8] = 0x00; 00075 valueBytes[9] = 0x00; 00076 //#ifdef UART_DEBUG 00077 // pc.printf("WRITE ==> %d/%d/%d %02d:%02d:%02d \r\n", tmPtr->tm_year, tmPtr->tm_mon, tmPtr->tm_mday, tmPtr->tm_hour, tmPtr->tm_min, tmPtr->tm_sec); 00078 //#endif 00079 00080 ble.gattServer().write(currentTimeCharacteristic.getValueHandle(), valueBytes, BLE_CURRENT_TIME_CHAR_VALUE_SIZE); 00081 } 00082 00083 time_t epochTimeByDateTimeBuffer() 00084 { 00085 struct tm timep; 00086 { 00087 timep.tm_year = *(uint16_t *)&valueBytes[0] - 1900; 00088 timep.tm_mon = valueBytes[2] - 1; 00089 timep.tm_mday = valueBytes[3]; 00090 timep.tm_hour = valueBytes[4]; 00091 timep.tm_min = valueBytes[5]; 00092 timep.tm_sec = valueBytes[6]; 00093 timep.tm_isdst = 0; 00094 } 00095 //#ifdef UART_DEBUG 00096 // pc.printf("READ ==> %d/%d/%d %02d:%02d:%02d \r\n", timep.tm_year, timep.tm_mon, timep.tm_mday, timep.tm_hour, timep.tm_min, timep.tm_sec); 00097 //#endif 00098 time_t epochTime = mktime(&timep); 00099 00100 return epochTime; 00101 } 00102 00103 public: 00104 //------------------------------------------------------------------------------------ 00105 /** 00106 * 00107 */ 00108 CurrentTimeService(BLE &_ble, ble_date_time_t &initialDateTime) : 00109 ble(_ble), 00110 currentTimeCharacteristic( GattCharacteristic::UUID_CURRENT_TIME_CHAR, 00111 valueBytes, BLE_CURRENT_TIME_CHAR_VALUE_SIZE, BLE_CURRENT_TIME_CHAR_VALUE_SIZE, 00112 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ 00113 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY 00114 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE 00115 ) 00116 { 00117 writeDateTime(initialDateTime); 00118 ticker.attach(this, &CurrentTimeService::epochtimePeriodicCallback, 1.0); 00119 00120 GattCharacteristic *charsTable[] = {¤tTimeCharacteristic}; 00121 GattService currentTimeService(GattService::UUID_CURRENT_TIME_SERVICE, charsTable, sizeof(charsTable) / sizeof(GattCharacteristic *) ); 00122 00123 ble.addService(currentTimeService); 00124 ble.onDataWritten(this, &CurrentTimeService::onDataWritten); 00125 00126 counter = 0; 00127 } 00128 00129 /** 00130 */ 00131 void writeDateTime(ble_date_time_t &dateTime) 00132 { 00133 *(uint16_t *)&valueBytes[0] = dateTime.year; 00134 valueBytes[2] = dateTime.month; 00135 valueBytes[3] = dateTime.day; 00136 valueBytes[4] = dateTime.hours; 00137 valueBytes[5] = dateTime.minutes; 00138 valueBytes[6] = dateTime.seconds; 00139 00140 // not support 00141 valueBytes[7] = 0x00; // day of week 00142 valueBytes[8] = 0x00; // Fractions256 00143 valueBytes[9] = 0x00; // Adjust Reason 00144 } 00145 00146 void writeEpochTime(time_t et) 00147 { 00148 dataTimeBufferByEpochTime(&et); 00149 } 00150 00151 /** 00152 */ 00153 void readDateTime(ble_date_time_t &dateTime) 00154 { 00155 dateTime.year = *(uint16_t *)&valueBytes[0]; 00156 dateTime.month = valueBytes[2]; 00157 dateTime.day = valueBytes[3]; 00158 dateTime.hours = valueBytes[4]; 00159 dateTime.minutes = valueBytes[5]; 00160 dateTime.seconds = valueBytes[6]; 00161 } 00162 00163 time_t readEpochTime() 00164 { 00165 return epochTimeByDateTimeBuffer(); 00166 } 00167 00168 00169 // for BLE GATT callback (optional) 00170 virtual void onDataWritten(const GattWriteCallbackParams *params) 00171 { 00172 if (params->handle == currentTimeCharacteristic.getValueHandle()) { 00173 memcpy((void *)&valueBytes, params->data, params->len); 00174 } 00175 } 00176 00177 int getCounter() const { return counter; } 00178 void setCounter(int val) { counter = val; } 00179 int getTm() const { return tmCnt; } 00180 void setTm(int val) { tmCnt = val; } 00181 00182 protected: 00183 BLE &ble; 00184 uint8_t valueBytes[BLE_CURRENT_TIME_CHAR_VALUE_SIZE]; 00185 GattCharacteristic currentTimeCharacteristic; 00186 private: 00187 int counter; /*timer counter*/ 00188 int tmCnt; /*for timeout*/ 00189 }; 00190 00191 #endif /* #ifndef __BLE_CURRENT_TIME_SERVICE_H__*/ 00192
Generated on Tue Jul 12 2022 21:16:17 by
1.7.2
