This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-mcumgr.git

commit b3c25dbe5a21ee26df59ae8d09ee5280e5223aa4
Author: Christopher Collins <ccoll...@apache.org>
AuthorDate: Wed Feb 14 13:52:27 2018 -0800

    Remove calls to `*printf()`
    
    Prior to this commit, mcumgr used `sprintf()` and `snprintf()` to format
    text in certain responses.  These functions add quite a bit of code size
    for Zephyr builds (~10kB).
    
    This commit implements a few lightweight integer-to-string functions
    and eliminates the stdio calls.
---
 CMakeLists.txt                               |  1 +
 cmd/img_mgmt/src/img_mgmt_util.c             | 23 ++++++++---
 cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c |  7 ++--
 util/CMakeLists.txt                          |  8 ++++
 util/include/util/mcumgr_util.h              | 55 +++++++++++++++++++++++++
 util/src/mcumgr_util.c                       | 61 ++++++++++++++++++++++++++++
 6 files changed, 146 insertions(+), 9 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 21ac645..3736e03 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,6 +5,7 @@ add_subdirectory(cborattr)
 add_subdirectory(cmd)
 add_subdirectory(mgmt)
 add_subdirectory(smp)
+add_subdirectory(util)
 
 zephyr_library_link_libraries(MCUMGR)
 
diff --git a/cmd/img_mgmt/src/img_mgmt_util.c b/cmd/img_mgmt/src/img_mgmt_util.c
index 5889f61..a51ed88 100644
--- a/cmd/img_mgmt/src/img_mgmt_util.c
+++ b/cmd/img_mgmt/src/img_mgmt_util.c
@@ -22,18 +22,29 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "util/mcumgr_util.h"
 #include "img_mgmt/image.h"
 #include "img_mgmt/img_mgmt.h"
 
 int
 img_mgmt_ver_str(const struct image_version *ver, char *dst)
 {
+    int off;
+
+    off = 0;
+
+    off += ull_to_s(ver->iv_major, INT_MAX, dst + off);
+
+    dst[off++] = '.';
+    off += ull_to_s(ver->iv_minor, INT_MAX, dst + off);
+
+    dst[off++] = '.';
+    off += ull_to_s(ver->iv_revision, INT_MAX, dst + off);
+    
     if (ver->iv_build_num != 0) {
-        return sprintf(dst, "%u.%u.%u.%lu",
-          ver->iv_major, ver->iv_minor, ver->iv_revision,
-          (unsigned long)ver->iv_build_num);
-    } else {
-        return sprintf(dst, "%u.%u.%u",
-          ver->iv_major, ver->iv_minor, ver->iv_revision);
+        dst[off++] = '.';
+        off += ull_to_s(ver->iv_revision, INT_MAX, dst + off);
     }
+
+    return 0;
 }
diff --git a/cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c 
b/cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c
index ba52517..eae1afb 100644
--- a/cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c
+++ b/cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c
@@ -22,6 +22,7 @@
 #include <debug/object_tracing.h>
 #include <kernel_structs.h>
 #include <mgmt/mgmt.h>
+#include <util/mcumgr_util.h>
 #include <os_mgmt/os_mgmt.h>
 #include <os_mgmt/os_mgmt_impl.h>
 
@@ -62,13 +63,13 @@ os_mgmt_impl_task_info(int idx, struct os_mgmt_task_info 
*out_info)
     }
 
     *out_info = (struct os_mgmt_task_info){ 0 };
-
-    snprintf(out_info->oti_name, sizeof out_info->oti_name, "%d",
-             thread->base.prio);
+    ll_to_s(thread->base.prio, sizeof out_info->oti_name, out_info->oti_name);
     out_info->oti_prio = thread->base.prio;
     out_info->oti_taskid = idx;
     out_info->oti_state = thread->base.thread_state;
+#ifdef THREAD_STACK_INFO
     out_info->oti_stksize = thread->stack_info.size / 4;
+#endif
 
     return 0;
 }
diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt
new file mode 100644
index 0000000..2c20c66
--- /dev/null
+++ b/util/CMakeLists.txt
@@ -0,0 +1,8 @@
+target_include_directories(MCUMGR INTERFACE
+    include
+)
+
+zephyr_library_sources(
+    util/src/mcumgr_util.c
+)
+
diff --git a/util/include/util/mcumgr_util.h b/util/include/util/mcumgr_util.h
new file mode 100644
index 0000000..898b798
--- /dev/null
+++ b/util/include/util/mcumgr_util.h
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+
+#ifndef H_MCUMGR_UTIL_
+#define H_MCUMGR_UTIL_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Converts an unsigned long long to a null-terminated string.
+ *
+ * @param val                   The source number to convert.
+ * @param dst_max_len           The size, in bytes, of the destination buffer.
+ * @param dst                   The destination buffer.
+ *
+ * @return                      The length of the resulting string on success;
+ *                              -1 if the buffer is too small.
+ */
+int ull_to_s(unsigned long long val, int dst_max_len, char *dst);
+
+/**
+ * @brief Converts a long long to a null-terminated string.
+ *
+ * @param val                   The source number to convert.
+ * @param dst_max_len           The size, in bytes, of the destination buffer.
+ * @param dst                   The destination buffer.
+ *
+ * @return                      The length of the resulting string on success;
+ *                              -1 if the buffer is too small.
+ */
+int ll_to_s(long long val, int dst_max_len, char *dst);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/util/src/mcumgr_util.c b/util/src/mcumgr_util.c
new file mode 100644
index 0000000..42bf31e
--- /dev/null
+++ b/util/src/mcumgr_util.c
@@ -0,0 +1,61 @@
+#include <stdbool.h>
+#include "util/mcumgr_util.h"
+
+int
+ull_to_s(unsigned long long val, int dst_max_len, char *dst)
+{
+    unsigned long copy;
+    int digit;
+    int off;
+    int len;
+
+    /* First, calculate the length of the resulting string. */
+    copy = val;
+    for (len = 0; copy != 0; len++) {
+        copy /= 10;
+    }
+
+    /* A value of 0 still requires one character ("0"). */
+    if (len == 0) {
+        len = 1;
+    }
+
+    /* Ensure the buffer can accommodate the string and terminator. */
+    if (len >= dst_max_len - 1) {
+        return -1;
+    }
+
+    /* Encode the string from right to left. */
+    off = len;
+    dst[off--] = '\0';
+    do {
+        digit = val % 10;
+        dst[off--] = '0' + digit;
+
+        val /= 10;
+    } while (val > 0);
+
+    return len;
+}
+
+int
+ll_to_s(long long val, int dst_max_len, char *dst)
+{
+    unsigned long long ull;
+
+    if (val < 0) {
+        if (dst_max_len < 1) {
+            return -1;
+        }
+
+        dst[0] = '-';
+        dst_max_len--;
+        dst++;
+
+        ull = -val;
+    } else {
+        ull = val;
+    }
+
+    return ull_to_s(ull, dst_max_len, dst);
+}

-- 
To stop receiving notification emails like this one, please contact
ccoll...@apache.org.

Reply via email to