andrzej-kaczmarek commented on a change in pull request #14: ble service: 
Current Time Service
URL: https://github.com/apache/mynewt-nimble/pull/14#discussion_r179071195
 
 

 ##########
 File path: nimble/host/services/cts/src/ble_svc_cts.c
 ##########
 @@ -0,0 +1,544 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <string.h>
+
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "host/ble_hs.h"
+#include "host/ble_gap.h"
+#include "services/cts/ble_svc_cts.h"
+#include "datetime/datetime.h"
+
+/* To use for days AND hours if update more than 255 days ago */
+#define BLE_SVC_CTS_UPDATED_SINCE_A_LONG_TIME_AGO 255
+
+#define BLE_SVC_CTS_ADJUST_REASON_MANUAL_TIME_UPDATE               1
+#define BLE_SVC_CTS_ADJUST_REASON_EXTERN_REFERENCE_TIME_UPDATE     2
+#define BLE_SVC_CTS_ADJUST_REASON_CHANGE_OF_TIMEZONE               4
+#define BLE_SVC_CTS_ADJUST_REASON_CHANGE_OF_DST                    8
+
+static bool     ble_svc_cts_reference_time_was_used  = false;
+static uint64_t ble_svc_cts_reference_time_last_used = 0;
+static uint8_t  ble_svc_cts_adjust_reason = 0;
+
+/* Used as buffer */
+static struct {
+    uint8_t time_source;
+    uint8_t time_accuracy;
+    uint8_t days;
+    uint8_t hours;
+} __attribute__((__packed__)) ble_svc_cts_reference_time_information = {
+    .time_source   = BLE_SVC_CTS_TIME_SOURCE_UNKNOWN,
+    .time_accuracy = BLE_SVC_CTS_TIME_ACCURACY_UNKNOWN,
+    .days          = BLE_SVC_CTS_UPDATED_SINCE_A_LONG_TIME_AGO,
+    .hours         = BLE_SVC_CTS_UPDATED_SINCE_A_LONG_TIME_AGO,
+};
+
+static struct {
+    uint16_t year;
+    uint8_t month;
+    uint8_t day;
+    uint8_t hours;
+    uint8_t minutes;
+    uint8_t secondes;
+    uint8_t day_of_week;
+    uint8_t fraction256;
+    uint8_t adjust_reason;
+} __attribute__((__packed__)) ble_svc_cts_current_time;
+
+static struct {
+    int8_t  time_zone;  /* -48 .. 56, -128 = unknown */
+    uint8_t dst_offset; 
+} __attribute__((__packed__)) ble_svc_cts_local_time_information;
+    
+/* Charachteristic value handles */
+#if MYNEWT_VAL(BLE_SVC_CTS_CURRENT_TIME_NOTIFY_ENABLE) > 0
+static uint16_t ble_svc_cts_current_time_handle;
+#endif
+
+/* Connection handle 
+ *
+ * TODO: In order to support multiple connections we would need to save
+ *       the handles for every connection, not just the most recent. Then
+ *       we would need to notify each connection when needed.
+ * */
+static uint16_t ble_svc_cts_conn_handle;
+
+/* Access function */
+static int
+ble_svc_cts_access(uint16_t conn_handle, uint16_t attr_handle,
+                   struct ble_gatt_access_ctxt *ctxt, void *arg);
+
+static const struct ble_gatt_svc_def ble_svc_cts_defs[] = {
+    {
+        /*** Current Time Service. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid = BLE_UUID16_DECLARE(BLE_SVC_CTS_UUID16),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+           /*** Current time characteristic */
+            .uuid = BLE_UUID16_DECLARE(BLE_SVC_CTS_CHR_UUID16_CURRENT_TIME),
+            .access_cb = ble_svc_cts_access,
+#if MYNEWT_VAL(BLE_SVC_CTS_CURRENT_TIME_NOTIFY_ENABLE) > 0
+           .val_handle = &ble_svc_cts_current_time_handle,
+#endif
+            .flags = BLE_GATT_CHR_F_READ |
+                    MYNEWT_VAL(BLE_SVC_CTS_CURRENT_TIME_READ_PERM) |
+#if MYNEWT_VAL(BLE_SVC_CTS_CURRENT_TIME_NOTIFY_ENABLE) > 0
+                    BLE_GATT_CHR_F_NOTIFY |
+#endif
+#if MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM) >= 0
+                     BLE_GATT_CHR_F_WRITE |
+                     MYNEWT_VAL(BLE_SVC_CTS_CURRENT_TIME_WRITE_PERM) |
+#endif
+                    0,
+           }, {
+#if MYNEWT_VAL(BLE_SVC_CTS_LOCAL_TIME_INFORMATION_READ_PERM) >= 0
+           /*** Local time information characteristic */
+            .uuid = 
BLE_UUID16_DECLARE(BLE_SVC_CTS_CHR_UUID16_LOCAL_TIME_INFORMATION),
+            .access_cb = ble_svc_cts_access,
+            .flags = BLE_GATT_CHR_F_READ |
+                    MYNEWT_VAL(BLE_SVC_CTS_LOCAL_TIME_INFORMATION_READ_PERM) |
+#if MYNEWT_VAL(BLE_SVC_CTS_LOCAL_TIME_INFORMATION_WRITE_PERM) >= 0
+                     BLE_GATT_CHR_F_WRITE |
+                     MYNEWT_VAL(BLE_SVC_CTS_LOCAL_TIME_INFORMATION_WRITE_PERM) 
|
+#endif
+                    0,
+           }, {
+#endif
+#if MYNEWT_VAL(BLE_SVC_CTS_REFERENCE_TIME_INFORMATION_READ_PERM) >= 0
+           /*** Reference time information characteristic */
+            .uuid = 
BLE_UUID16_DECLARE(BLE_SVC_CTS_CHR_UUID16_REFERENCE_TIME_INFORMATION),
+            .access_cb = ble_svc_cts_access,
+            .flags = BLE_GATT_CHR_F_READ |
+                    
MYNEWT_VAL(BLE_SVC_CTS_REFERENCE_TIME_INFORMATION_READ_PERM),
+           }, {
+#endif
+            0, /* No more characteristics in this service. */
+        } },
+    },
+
+    {
+        0, /* No more services. */
+    },
+};
+
+static inline int
+ble_svc_cts_current_time_read_access(struct ble_gatt_access_ctxt *ctxt)
+{
+    /* Get current clock time */
+    struct os_timeval  tv;
+    struct os_timezone tz;
+    struct clocktime   ct;
+    
+    if (os_gettimeofday(&tv, &tz) || timeval_to_clocktime(&tv, &tz, &ct)) {
+       return BLE_ATT_ERR_UNLIKELY;
+    }
+
+    /* Populate data */
+    ble_svc_cts_current_time.year          = htole16(ct.year);
+    ble_svc_cts_current_time.month         = ct.mon;
+    ble_svc_cts_current_time.day           = ct.day;
+    ble_svc_cts_current_time.hours         = ct.hour;
+    ble_svc_cts_current_time.minutes       = ct.min;
+    ble_svc_cts_current_time.secondes      = ct.sec;
+    ble_svc_cts_current_time.day_of_week   = (ct.dow == 0) ? 7 : ct.dow;
+    ble_svc_cts_current_time.fraction256   = (ct.usec * 256) / 1000000;
+    ble_svc_cts_current_time.adjust_reason = ble_svc_cts_adjust_reason;
+
+    /* Copy data */
+    if (os_mbuf_append(ctxt->om, &ble_svc_cts_current_time,
+                                sizeof(ble_svc_cts_current_time))) {
+       return BLE_ATT_ERR_INSUFFICIENT_RES;
+    }
+
+    return 0;
+}
+
+static inline int
+ble_svc_cts_current_time_write_access(struct ble_gatt_access_ctxt *ctxt)
+{
+    int rc = 0;
+
+    /* Retrieve data */
+    uint16_t om_len = OS_MBUF_PKTLEN(ctxt->om);
+    if (om_len != sizeof(ble_svc_cts_current_time)) {
+        return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+    }
+    if (ble_hs_mbuf_to_flat(ctxt->om,
+                           &ble_svc_cts_current_time, om_len, NULL)) {
+        return BLE_ATT_ERR_UNLIKELY;
+    }
+    
+    /* Convert to timeval */
+    struct clocktime   ct;
+    struct os_timezone tz;
+    struct os_timeval  tv;
 
 Review comment:
   all local variables should be defined at the beginning of function

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to