implemented client cts

This commit is contained in:
hathach committed 2017-07-18 23:32:13 +07:00
1 parent 054ab9d96e
commit 5c7b883558
3 files changed
+124 -5

No files matched your search

@@ -18,6 +18,11 @@
*
* Note: Currently only iOS act as CTS server, Android does not, The easiest
* way to test this sketch is using an iOS device.
*
* Current Time Service Info
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.current_time.xml
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml
*/
#include <bluefruit.h>
@@ -41,6 +46,7 @@ void setup()
// Configure CTS client
bleCTime.begin();
bleCTime.setAdjustCallback(cts_adjust_callback);
// Set up and start advertising
startAdv();
@@ -81,7 +87,12 @@ void loop()
// If service is not yet discovered
if ( !bleCTime.discovered() ) return;
// Your code here
// Get Time from iOS one every 1 second
bleCTime.getCurrentTime();
printTime();
delay(1000);
}
void connect_callback(uint16_t conn_handle)
@@ -93,20 +104,48 @@ void connect_callback(uint16_t conn_handle)
{
Serial.println("Discovered");
// ANCS requires pairing to work, it makes sense to request security here as well
// iOS requires pairing to work, it makes sense to request security here as well
Serial.print("Attempting to PAIR with the iOS device, please press PAIR on your phone ... ");
if ( Bluefruit.requestPairing() )
{
Serial.println("Done");
// Serial.println("Enabling notifications");
// Serial.println();
// bleancs.enableNotification();
Serial.println("Enabling Time Adjust Notify");
bleCTime.enableAdjust();
Serial.print("Get Current Time chars value");
bleCTime.getCurrentTime();
Serial.print("Get Local Time Info chars value");
bleCTime.getLocalTimeInfo();
Serial.println();
}
Serial.println();
}
}
void cts_adjust_callback(uint8_t reason)
{
const char * reason_str[] = { "Manual", "External Reference", "Change of Time Zone", "Change of DST" };
Serial.println("iOS Device time changed due to ");
Serial.println( reason_str[reason] );
}
void printTime(void)
{
const char * day_of_week_str[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
Serial.printf("%04d-%02d-%02d ", bleCTime.Time.year, bleCTime.Time.month, bleCTime.Time.day);
Serial.printf("%02d:%02d:%02d ", bleCTime.Time.hour, bleCTime.Time.minute, bleCTime.Time.second);
Serial.println(day_of_week_str[bleCTime.Time.weekday]);
Serial.printf("UTC %d min, ", bleCTime.LocalInfo.timezone*15);
Serial.printf("Daytime Saving +%d min", bleCTime.LocalInfo.dst_offset*15);
Serial.println();
}
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) reason;
@@ -36,18 +36,28 @@
#include "bluefruit.h"
void blects_central_notify_cb(BLEClientCharacteristic& chr, uint8_t* data, uint16_t len);
BLEClientCts::BLEClientCts(void)
: BLEClientService(UUID16_SVC_CURRENT_TIME), _cur_time(UUID16_CHR_CURRENT_TIME), _local_info(UUID16_CHR_LOCAL_TIME_INFORMATION)
{
varclr(&Time);
varclr(&LocalInfo);
_adjust_cb = NULL;
}
bool BLEClientCts::begin(void)
{
VERIFY_STATIC(sizeof(Time) == 10);
VERIFY_STATIC(sizeof(LocalInfo) == 2);
// Invoke base class begin()
BLEClientService::begin();
_cur_time.begin(this);
_cur_time.setNotifyCallback(blects_central_notify_cb);
_local_info.begin(this);
return true;
@@ -65,5 +75,39 @@ bool BLEClientCts::discover(uint16_t conn_handle)
// Current Time chars is mandatory
VERIFY( _cur_time.valueHandle() != BLE_GATT_HANDLE_INVALID, false);
_conn_hdl = conn_handle;
return true;
}
bool BLEClientCts::getCurrentTime(void)
{
return _cur_time.read(&Time, sizeof(Time)) > 0;
}
bool BLEClientCts::getLocalTimeInfo(void)
{
return _local_info.read(&LocalInfo, sizeof(LocalInfo)) > 0;
}
bool BLEClientCts::enableAdjust(void)
{
return _cur_time.enableNotify();
}
void BLEClientCts::setAdjustCallback(adjust_callback_t fp)
{
_adjust_cb = fp;
}
void BLEClientCts::_cur_time_notify_cb(uint8_t* data, uint16_t len)
{
memcpy(&Time, data, len);
// invoked callback if set
if ( _adjust_cb ) _adjust_cb( Time.adjust_reason );
}
void blects_central_notify_cb(BLEClientCharacteristic& chr, uint8_t* data, uint16_t len)
{
((BLEClientCts&) chr.parentService())._cur_time_notify_cb(data, len);
}
@@ -43,14 +43,50 @@
class BLEClientCts : public BLEClientService
{
public:
typedef void (*adjust_callback_t) (uint8_t reason);
BLEClientCts(void);
virtual bool begin(void);
virtual bool discover(uint16_t conn_handle);
bool getCurrentTime(void);
bool getLocalTimeInfo(void);
bool enableAdjust(void);
void setAdjustCallback(adjust_callback_t fp);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.current_time.xml
struct ATTR_PACKED {
uint16_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
uint8_t weekday;
uint8_t subsecond;
uint8_t adjust_reason;
} Time;
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml
struct ATTR_PACKED {
int8_t timezone;
uint8_t dst_offset;
}LocalInfo;
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
* Although declare as public, it is meant to be invoked by internal
* code. User should not call these directly
*------------------------------------------------------------------*/
void _cur_time_notify_cb(uint8_t* data, uint16_t len);
private:
BLEClientCharacteristic _cur_time;
BLEClientCharacteristic _local_info;
adjust_callback_t _adjust_cb;
};
#endif /* BLECLIENTCTS_H_ */