From 0575f19f178ae78118369b8e0304d7500fcbf9c9 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sun, 11 Oct 2020 23:36:34 +0200
Subject: [PATCH] feat(attc): Parse date/time from central and set RTC /
 timezone

---
 epicardium/ble/meson.build          |   1 +
 epicardium/ble/profiles/tipc_main.c | 203 ++++++++++++++++++++++++++++
 lib/sdk/Libraries/BTLE/meson.build  |   2 +-
 3 files changed, 205 insertions(+), 1 deletion(-)
 create mode 100644 epicardium/ble/profiles/tipc_main.c

diff --git a/epicardium/ble/meson.build b/epicardium/ble/meson.build
index 84b6dbe40..6cf974836 100644
--- a/epicardium/ble/meson.build
+++ b/epicardium/ble/meson.build
@@ -3,6 +3,7 @@ ble_sources = files(
   'stack.c',
   'ble_main.c',
   'ble_attc.c',
+  'profiles/tipc_main.c',
   'svc_dis.c',
   'svc_core.c',
   'bondings.c',
diff --git a/epicardium/ble/profiles/tipc_main.c b/epicardium/ble/profiles/tipc_main.c
new file mode 100644
index 000000000..e01e1c2a1
--- /dev/null
+++ b/epicardium/ble/profiles/tipc_main.c
@@ -0,0 +1,203 @@
+/*************************************************************************************************/
+/*!
+ *  \file
+ *
+ *  \brief  Time profile client.
+ *
+ *  Copyright (c) 2011-2018 Arm Ltd. All Rights Reserved.
+ *  ARM Ltd. confidential and proprietary.
+ *
+ *  IMPORTANT.  Your use of this file is governed by a Software License Agreement
+ *  ("Agreement") that must be accepted in order to download or otherwise receive a
+ *  copy of this file.  You may not use or copy this file for any purpose other than
+ *  as described in the Agreement.  If you do not agree to all of the terms of the
+ *  Agreement do not use this file and delete all copies in your possession or control;
+ *  if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
+ *  to any use, copying or further distribution of this software.
+ */
+/*************************************************************************************************/
+/* card10:
+ * copied from lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/profiles/tipc/tipc_main.c
+ */
+/* clang-format off */
+/* clang-formet turned off for easier diffing against orginal file */
+
+#include "wsf_types.h"
+#include "wsf_assert.h"
+#include "wsf_trace.h"
+#include "util/bstream.h"
+#include "app_api.h"
+#include "tipc/tipc_api.h"
+
+#include "epicardium.h"
+
+#include <time.h>
+#include <stdio.h>
+
+/**************************************************************************************************
+  Local Variables
+**************************************************************************************************/
+
+static time_t s_time;
+
+/*!
+ *  Current Time service
+ */
+
+/* Characteristics for discovery */
+
+/*! Current time */
+static const attcDiscChar_t tipcCtsCt =
+{
+  attCtChUuid,
+  ATTC_SET_REQUIRED
+};
+
+/*! Current time client characteristic configuration descriptor */
+static const attcDiscChar_t tipcCtsCtCcc =
+{
+  attCliChCfgUuid,
+  ATTC_SET_REQUIRED | ATTC_SET_DESCRIPTOR
+};
+
+/*! Local time information */
+static const attcDiscChar_t tipcCtsLti =
+{
+  attLtiChUuid,
+  0
+};
+
+/*! Reference time information */
+static const attcDiscChar_t tipcCtsRti =
+{
+  attRtiChUuid,
+  0
+};
+
+/*! List of characteristics to be discovered; order matches handle index enumeration  */
+static const attcDiscChar_t *tipcCtsDiscCharList[] =
+{
+  &tipcCtsCt,                   /* Current time */
+  &tipcCtsCtCcc,                /* Current time client characteristic configuration descriptor */
+  &tipcCtsLti,                  /* Local time information */
+  &tipcCtsRti                   /* Reference time information */
+};
+
+/* sanity check:  make sure handle list length matches characteristic list length */
+WSF_CT_ASSERT(TIPC_CTS_HDL_LIST_LEN == ((sizeof(tipcCtsDiscCharList) / sizeof(attcDiscChar_t *))));
+
+/*************************************************************************************************/
+/*!
+ *  \brief  Perform service and characteristic discovery for Current Time service.  Parameter
+ *          pHdlList must point to an array of length TIPC_CTS_HDL_LIST_LEN.  If discovery is
+ *          successful the handles of discovered characteristics and descriptors will be set
+ *          in pHdlList.
+ *
+ *  \param  connId    Connection identifier.
+ *  \param  pHdlList  Characteristic handle list.
+ *
+ *  \return None.
+ */
+/*************************************************************************************************/
+void TipcCtsDiscover(dmConnId_t connId, uint16_t *pHdlList)
+{
+  AppDiscFindService(connId, ATT_16_UUID_LEN, (uint8_t *) attCtsSvcUuid,
+                     TIPC_CTS_HDL_LIST_LEN, (attcDiscChar_t **) tipcCtsDiscCharList, pHdlList);
+}
+
+/*************************************************************************************************/
+/*!
+ *  \brief  Process a value received in an ATT read response, notification, or indication
+ *          message.  Parameter pHdlList must point to an array of length TIPC_CTS_HDL_LIST_LEN.
+ *          If the attribute handle of the message matches a handle in the handle list the value
+ *          is processed, otherwise it is ignored.
+ *
+ *  \param  pHdlList  Characteristic handle list.
+ *  \param  pMsg      ATT callback message.
+ *
+ *  \return ATT_SUCCESS if handle is found, ATT_ERR_NOT_FOUND otherwise.
+ */
+/*************************************************************************************************/
+uint8_t TipcCtsValueUpdate(uint16_t *pHdlList, attEvt_t *pMsg)
+{
+  uint8_t   status = ATT_SUCCESS;
+  uint8_t   *p;
+  uint16_t  year;
+  uint8_t   month, day, hour, min, sec, dayOfWeek, adjustReason;
+  uint8_t   sec256 = 0;
+  int8_t    timeZone;
+  uint8_t   dstOffset, source, accuracy;
+
+  /* Suppress unused variable compile warning */
+  (void)month; (void)day; (void)hour; (void)min; (void)sec; (void)dayOfWeek; (void)adjustReason;
+  (void)year; (void)sec256; (void)dstOffset; (void)accuracy; (void)timeZone; (void)source;
+
+  /* current time */
+  if (pMsg->handle == pHdlList[TIPC_CTS_CT_HDL_IDX])
+  {
+    /* parse value */
+    p = pMsg->pValue;
+    BSTREAM_TO_UINT16(year, p);
+    BSTREAM_TO_UINT8(month, p);
+    BSTREAM_TO_UINT8(day, p);
+    BSTREAM_TO_UINT8(hour, p);
+    BSTREAM_TO_UINT8(min, p);
+    BSTREAM_TO_UINT8(sec, p);
+    BSTREAM_TO_UINT8(dayOfWeek, p);
+    BSTREAM_TO_UINT8(sec256, p);
+    BSTREAM_TO_UINT8(adjustReason, p);
+
+    struct tm t;
+    t.tm_year = year - 1900;
+    t.tm_mon = month - 1;
+    t.tm_mday = day;
+    t.tm_hour = hour;
+    t.tm_min = min;
+    t.tm_sec = sec;
+    t.tm_isdst = -1; /* unknown */
+    s_time = mktime(&t);
+
+    APP_TRACE_INFO3("Date: %d/%d/%d", month, day, year);
+    APP_TRACE_INFO3("Time: %02d:%02d:%02d", hour, min, sec);
+    APP_TRACE_INFO3("dayOfWeek:%d sec256:%d adjustReason:%d",  dayOfWeek, sec256, adjustReason);
+  }
+  /* local time information */
+  else if (pMsg->handle == pHdlList[TIPC_CTS_LTI_HDL_IDX])
+  {
+    /* parse value */
+    p = pMsg->pValue;
+    BSTREAM_TO_UINT8(timeZone, p);
+    BSTREAM_TO_UINT8(dstOffset, p);
+
+    APP_TRACE_INFO2("timeZone:%d dstOffset:%d", timeZone, dstOffset);
+
+    if(s_time) {
+      char buf[32];
+      snprintf(buf, sizeof(buf), "%+05d", (timeZone +dstOffset) / 4 * 100);
+      epic_config_set_string("timezone", buf);
+
+      epic_rtc_set_milliseconds(s_time * 1000 + (1000 * sec256) / 256 - (timeZone + dstOffset) * 15 * 60 * 1000);
+    }
+  }
+  /* reference time information */
+  else if (pMsg->handle == pHdlList[TIPC_CTS_RTI_HDL_IDX])
+  {
+    /* parse value */
+    p = pMsg->pValue;
+    BSTREAM_TO_UINT8(source, p);
+    BSTREAM_TO_UINT8(accuracy, p);
+    BSTREAM_TO_UINT8(day, p);
+    BSTREAM_TO_UINT8(hour, p);
+
+    APP_TRACE_INFO2("Ref. time source:%d accuracy:%d", source, accuracy);
+    APP_TRACE_INFO2("Last update days:%d hours:%d", day, hour);
+  }
+  /* handle not found in list */
+  else
+  {
+    status = ATT_ERR_NOT_FOUND;
+  }
+
+  return status;
+}
+/* clang-format on */
diff --git a/lib/sdk/Libraries/BTLE/meson.build b/lib/sdk/Libraries/BTLE/meson.build
index c6da154e5..ccd9d2e86 100644
--- a/lib/sdk/Libraries/BTLE/meson.build
+++ b/lib/sdk/Libraries/BTLE/meson.build
@@ -98,7 +98,7 @@ sources = files(
 'stack/ble-profiles/sources/profiles/plxps/plxps_db.c',
 'stack/ble-profiles/sources/profiles/plxps/plxps_main.c',
 'stack/ble-profiles/sources/profiles/dis/dis_main.c',
-'stack/ble-profiles/sources/profiles/tipc/tipc_main.c',
+#'stack/ble-profiles/sources/profiles/tipc/tipc_main.c',
 'stack/ble-profiles/sources/profiles/bas/bas_main.c',
 'stack/ble-profiles/sources/profiles/udsc/udsc_main.c',
 'stack/ble-profiles/sources/profiles/scpps/scpps_main.c',
-- 
GitLab