mkiiskila commented on a change in pull request #1514: sys/stats/full: 
Persistent stats
URL: https://github.com/apache/mynewt-core/pull/1514#discussion_r234250779
 
 

 ##########
 File path: sys/stats/full/src/stats_conf.c
 ##########
 @@ -0,0 +1,199 @@
+/*
+ * 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 "os/mynewt.h"
+
+#if MYNEWT_VAL(STATS_PERSIST)
+
+#include <assert.h>
+#include <stdio.h>
+
+#include "base64/base64.h"
+#include "config/config.h"
+#include "stats/stats.h"
+#include "stats_priv.h"
+
+static char *stats_conf_get(int argc, char **argv, char *buf, int max_len);
+static int stats_conf_set(int argc, char **argv, char *val);
+static int stats_conf_commit(void);
+static int stats_conf_export(void (*func)(char *name, char *val),
+                             enum conf_export_tgt tgt);
+
+static struct conf_handler stats_conf_handler = {
+    .ch_name = "stat",
+    .ch_get = stats_conf_get,
+    .ch_set = stats_conf_set,
+    .ch_commit = stats_conf_commit,
+    .ch_export = stats_conf_export
+};
+
+static int
+stats_conf_snprintf_name(const struct stats_hdr *hdr, size_t max_len,
+                         char *buf)
+{
+    return snprintf(buf, max_len, "stat/%s", hdr->s_name);
+}
+
+static void
+stats_conf_name(const struct stats_hdr *hdr, char *buf)
+{
+    stats_conf_snprintf_name(hdr, MYNEWT_VAL(STATS_PERSIST_MAX_NAME_SIZE),
+                             buf);
+}
+
+static void
+stats_conf_serialize(const struct stats_hdr *hdr, void *buf)
+{
+    size_t rawlen;
+    void *data;
+
+    rawlen = stats_size(hdr);
+    data = stats_data(hdr);
+
+    conf_str_from_bytes(data, rawlen, buf, MYNEWT_VAL(STATS_PERSIST_BUF_SIZE));
+}
+
+/** Converts in-RAM setting to a config-friendly string. */
+static char *
+stats_conf_get(int argc, char **argv, char *buf, int max_len)
+{
+    const struct stats_hdr *hdr;
+
+    if (argc == 1) {
+        hdr = stats_group_find(argv[0]);
+        if (hdr != NULL) {
+            stats_conf_serialize(hdr, buf);
+        }
+    }
+    return NULL;
+}
+
+/** Converts config string to binary in-RAM value. */
+static int
+stats_conf_set(int argc, char **argv, char *val)
+{
+    struct stats_hdr *hdr;
+    size_t size;
+    void *data;
+    int decode_len;
+
+    if (argc == 1) {
+        hdr = stats_group_find(argv[0]);
+        if (hdr != NULL) {
+            size = stats_size(hdr);
+            data = stats_data(hdr);
+
+            decode_len = base64_decode_len(val);
+            if (decode_len > size) {
+                DEBUG_PANIC();
+                return SYS_ENOMEM;
 
 Review comment:
   SYS_ENOMEM -> OS_ENOMEM

----------------------------------------------------------------
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