You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2016/09/29 01:34:09 UTC
[02/49] incubator-mynewt-core git commit: move crc and cbmem to util
move crc and cbmem to util
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/54fbe73d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/54fbe73d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/54fbe73d
Branch: refs/heads/develop
Commit: 54fbe73d8949e4066e3f11f77a916780cd44e5b9
Parents: 6a7432f
Author: Sterling Hughes <st...@apache.org>
Authored: Wed Sep 28 16:48:03 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Wed Sep 28 18:32:22 2016 -0700
----------------------------------------------------------------------
boot/boot_serial/pkg.yml | 2 +-
encoding/crc/include/crc/base64.h | 32 ---
encoding/crc/include/crc/crc16.h | 36 ---
encoding/crc/include/crc/crc8.h | 32 ---
encoding/crc/include/crc/hex.h | 25 ---
encoding/crc/pkg.yml | 28 ---
encoding/crc/src/crc16.c | 83 -------
encoding/crc/src/crc8.c | 74 -------
encoding/crc/test/pkg.yml | 30 ---
encoding/crc/test/src/encoding_test.c | 46 ----
encoding/crc/test/src/encoding_test_priv.h | 25 ---
encoding/crc/test/src/hex_test.c | 125 -----------
fs/fcb/pkg.yml | 2 +-
fs/nffs/pkg.yml | 2 +-
sys/shell/pkg.yml | 2 +-
util/cbmem/include/cbmem/cbmem.h | 66 ++++++
util/cbmem/pkg.yml | 30 +++
util/cbmem/src/cbmem.c | 277 ++++++++++++++++++++++++
util/cbmem/test/pkg.yml | 30 +++
util/cbmem/test/src/cbmem_test.c | 176 +++++++++++++++
util/cbmem/test/src/util_test.c | 53 +++++
util/cbmem/test/src/util_test_priv.h | 25 +++
util/crc/include/crc/crc16.h | 36 +++
util/crc/include/crc/crc8.h | 32 +++
util/crc/pkg.yml | 28 +++
util/crc/src/crc16.c | 83 +++++++
util/crc/src/crc8.c | 74 +++++++
util/crc/test/pkg.yml | 30 +++
util/crc/test/src/encoding_test.c | 46 ++++
util/crc/test/src/encoding_test_priv.h | 25 +++
util/crc/test/src/hex_test.c | 125 +++++++++++
31 files changed, 1140 insertions(+), 540 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/boot/boot_serial/pkg.yml
----------------------------------------------------------------------
diff --git a/boot/boot_serial/pkg.yml b/boot/boot_serial/pkg.yml
index 69773a1..b2a9308 100644
--- a/boot/boot_serial/pkg.yml
+++ b/boot/boot_serial/pkg.yml
@@ -30,7 +30,7 @@ pkg.deps:
- kernel/os
- boot/bootutil
- encoding/base64
- - encoding/crc
+ - util/crc
pkg.req_apis:
- console
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/include/crc/base64.h
----------------------------------------------------------------------
diff --git a/encoding/crc/include/crc/base64.h b/encoding/crc/include/crc/base64.h
deleted file mode 100644
index 8e0c045..0000000
--- a/encoding/crc/include/crc/base64.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/**
- * 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 __UTIL_BASE64_H
-#define __UTIL_BASE64_H
-
-#include <stdint.h>
-#include <string.h>
-
-int base64_encode(const void *, int, char *, uint8_t);
-int base64_decode(const char *, void *buf);
-int base64_pad(char *, int);
-int base64_decode_len(const char *str);
-
-#define BASE64_ENCODE_SIZE(__size) ((((__size) * 4) / 3) + 4)
-
-#endif /* __UTIL_BASE64_H__ */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/include/crc/crc16.h
----------------------------------------------------------------------
diff --git a/encoding/crc/include/crc/crc16.h b/encoding/crc/include/crc/crc16.h
deleted file mode 100644
index b9b965f..0000000
--- a/encoding/crc/include/crc/crc16.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright 2001-2010 Georges Menie (www.menie.org)
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the University of California, Berkeley nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef _CRC16_H_
-#define _CRC16_H_
-
-#include <inttypes.h>
-
-#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
-unsigned short crc16_ccitt(uint16_t initial_crc, const void *buf, int len);
-
-#endif /* _CRC16_H_ */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/include/crc/crc8.h
----------------------------------------------------------------------
diff --git a/encoding/crc/include/crc/crc8.h b/encoding/crc/include/crc/crc8.h
deleted file mode 100644
index 336acca..0000000
--- a/encoding/crc/include/crc/crc8.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/**
- * 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.
- */
-
-/*
- * CRC8-CCITT, with normal polynomial; 0x07.
- */
-
-#ifndef _UTIL_CRC8_H_
-#define _UTIL_CRC8_H_
-
-#include <inttypes.h>
-
-uint8_t crc8_init(void);
-uint8_t crc8_calc(uint8_t val, void *buf, int cnt);
-
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/include/crc/hex.h
----------------------------------------------------------------------
diff --git a/encoding/crc/include/crc/hex.h b/encoding/crc/include/crc/hex.h
deleted file mode 100644
index 1e9d8c8..0000000
--- a/encoding/crc/include/crc/hex.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * 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 _UTIL_HEX_H_
-#define _UTIL_HEX_H_
-
-char *hex_format(void *src_v, int src_len, char *dst, int dst_len);
-int hex_parse(char *src, int src_len, void *dst_v, int dst_len);
-
-#endif /* _UTIL_HEX_H_ */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/pkg.yml
----------------------------------------------------------------------
diff --git a/encoding/crc/pkg.yml b/encoding/crc/pkg.yml
deleted file mode 100644
index 49e531e..0000000
--- a/encoding/crc/pkg.yml
+++ /dev/null
@@ -1,28 +0,0 @@
-#
-# 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.
-#
-
-pkg.name: encoding/crc
-pkg.description: Library containing encoding functions
-pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
- - crc16
- - crc32
- - crc8
- - crc
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/src/crc16.c
----------------------------------------------------------------------
diff --git a/encoding/crc/src/crc16.c b/encoding/crc/src/crc16.c
deleted file mode 100644
index 6ece419..0000000
--- a/encoding/crc/src/crc16.c
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Copyright 2001-2010 Georges Menie (www.menie.org)
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the University of California, Berkeley nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <inttypes.h>
-#include "crc/crc16.h"
-
-/* CRC16 implementation acording to CCITT standards */
-
-static const uint16_t crc16tab[256]= {
- 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
- 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
- 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
- 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
- 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
- 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
- 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
- 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
- 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
- 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
- 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
- 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
- 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
- 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
- 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
- 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
- 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
- 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
- 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
- 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
- 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
- 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
- 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
- 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
- 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
- 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
- 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
- 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
- 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
- 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
- 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
- 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
-};
-
-uint16_t
-crc16_ccitt(uint16_t initial_crc, const void *buf, int len)
-{
- const uint8_t *ptr;
- uint16_t crc;
- int counter;
-
- crc = initial_crc;
- ptr = buf;
-
- for (counter = 0; counter < len; counter++) {
- crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *ptr++)&0x00FF];
- }
-
- return crc;
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/src/crc8.c
----------------------------------------------------------------------
diff --git a/encoding/crc/src/crc8.c b/encoding/crc/src/crc8.c
deleted file mode 100644
index 384acb2..0000000
--- a/encoding/crc/src/crc8.c
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * 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.
- */
-
-/*
- * Table computation:
- *
- * void
- * gen_small_table(uint8_t poly)
- * {
- * int i;
- * int j;
- * uint8_t curr;
- *
- * for (i = 0; i < 16; i++) {
- * curr = i;
- *
- * for (j = 0; j < 8; j++) {
- * if ((curr & 0x80) != 0) {
- * curr = (curr << 1) ^ poly;
- * } else {
- * curr <<= 1;
- * }
- * }
- *
- * small_table[i] = curr;
- *
- * printf("0x%x, ", small_table[i]);
- * }
- * printf("\n");
- *}
- */
-
-#include "crc/crc8.h"
-
-static uint8_t crc8_small_table[16] = {
- 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
- 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d
-};
-
-uint8_t
-crc8_init(void)
-{
- return 0xff;
-}
-
-uint8_t
-crc8_calc(uint8_t val, void *buf, int cnt)
-{
- int i;
- uint8_t *p = buf;
-
- for (i = 0; i < cnt; i++) {
- val ^= p[i];
- val = (val << 4) ^ crc8_small_table[val >> 4];
- val = (val << 4) ^ crc8_small_table[val >> 4];
- }
- return val;
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/test/pkg.yml
----------------------------------------------------------------------
diff --git a/encoding/crc/test/pkg.yml b/encoding/crc/test/pkg.yml
deleted file mode 100644
index dac3cb1..0000000
--- a/encoding/crc/test/pkg.yml
+++ /dev/null
@@ -1,30 +0,0 @@
-# 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.
-#
-pkg.name: crypto/encoding/test
-pkg.type: unittest
-pkg.description: "Crypto encoding unit tests."
-pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-
-pkg.deps:
- - libs/testutil
- - libs/util
-
-pkg.deps.SELFTEST:
- - libs/console/stub
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/test/src/encoding_test.c
----------------------------------------------------------------------
diff --git a/encoding/crc/test/src/encoding_test.c b/encoding/crc/test/src/encoding_test.c
deleted file mode 100644
index b21fee8..0000000
--- a/encoding/crc/test/src/encoding_test.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/**
- * 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 <stddef.h>
-#include "syscfg/syscfg.h"
-#include "testutil/testutil.h"
-#include "encoding_test_priv.h"
-
-
-int
-hex_fmt_test_all(void)
-{
- hex_fmt_test_suite();
- return tu_case_failed;
-}
-
-#if MYNEWT_VAL(SELFTEST)
-
-int
-main(int argc, char **argv)
-{
- tu_config.tc_print_results = 1;
- tu_init();
-
- hex_fmt_test_all();
- return tu_any_failed;
-}
-
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/test/src/encoding_test_priv.h
----------------------------------------------------------------------
diff --git a/encoding/crc/test/src/encoding_test_priv.h b/encoding/crc/test/src/encoding_test_priv.h
deleted file mode 100644
index fc2fcac..0000000
--- a/encoding/crc/test/src/encoding_test_priv.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * 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 __ENCODING_TEST_PRIV_
-#define __ENCODING_TEST_PRIV_
-
-int hex_fmt_test_suite(void);
-
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/encoding/crc/test/src/hex_test.c
----------------------------------------------------------------------
diff --git a/encoding/crc/test/src/hex_test.c b/encoding/crc/test/src/hex_test.c
deleted file mode 100644
index 8183425..0000000
--- a/encoding/crc/test/src/hex_test.c
+++ /dev/null
@@ -1,125 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-
-#include "testutil/testutil.h"
-#include "util/hex.h"
-
-TEST_CASE(hex2str)
-{
- int i;
- char *ret;
- char cmp_data[8];
-
- struct {
- char *in;
- int inlen;
- char *out;
- int outlen;
- } test_data[] = {
- [0] = {
- .in = "\x01",
- .inlen = 1,
- .out = "01",
- .outlen = 2,
- },
- [1] = {
- .in = "\xaf\xf2",
- .inlen = 2,
- .out = "aff2",
- .outlen = 4,
- }
- };
-
- for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
- ret = hex_format(test_data[i].in, test_data[i].inlen,
- cmp_data, sizeof(cmp_data));
- TEST_ASSERT(ret == cmp_data);
- TEST_ASSERT(strlen(cmp_data) == test_data[i].outlen);
- TEST_ASSERT(!strcmp(test_data[i].out, cmp_data));
- }
-
- /*
- * Test not enough space. Must have space for '\0' at the end.
- */
- ret = hex_format("\x01\x02", 2, cmp_data, 1);
- TEST_ASSERT(ret == NULL);
-
- ret = hex_format("\x01\x02", 2, cmp_data, 2);
- TEST_ASSERT(ret == NULL);
-}
-
-TEST_CASE(str2hex)
-{
- int i;
- char cmp_data[8];
- int rc;
-
- struct {
- char *in;
- int inlen;
- char *out;
- int outlen;
- } test_data[] = {
- [0] = {
- .in = "01",
- .inlen = 2,
- .out = "\x01",
- .outlen = 1,
- },
- [1] = {
- .in = "AfF2",
- .inlen = 4,
- .out = "\xaf\xf2",
- .outlen = 2,
- }
- };
-
- for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
- rc = hex_parse(test_data[i].in, test_data[i].inlen,
- cmp_data, sizeof(cmp_data));
- TEST_ASSERT(rc == test_data[i].outlen);
- TEST_ASSERT(!memcmp(test_data[i].out, cmp_data, rc));
- }
-
- /*
- * Test invalid input
- */
- rc = hex_parse("HJ", 2, cmp_data, sizeof(cmp_data));
- TEST_ASSERT(rc < 0);
-
- rc = hex_parse("a", 1, cmp_data, sizeof(cmp_data));
- TEST_ASSERT(rc < 0);
-
- rc = hex_parse("0102", 4, cmp_data, 1);
- TEST_ASSERT(rc < 0);
-
- /*
- * This should be valid.
- */
- rc = hex_parse("0102", 4, cmp_data, 2);
- TEST_ASSERT(rc == 2);
-}
-
-TEST_SUITE(hex_fmt_test_suite)
-{
- hex2str();
- str2hex();
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/fs/fcb/pkg.yml
----------------------------------------------------------------------
diff --git a/fs/fcb/pkg.yml b/fs/fcb/pkg.yml
index 9b90bb9..daa5557 100644
--- a/fs/fcb/pkg.yml
+++ b/fs/fcb/pkg.yml
@@ -26,4 +26,4 @@ pkg.keywords:
pkg.deps:
- kernel/os
- - encoding/crc
+ - util/crc
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/fs/nffs/pkg.yml
----------------------------------------------------------------------
diff --git a/fs/nffs/pkg.yml b/fs/nffs/pkg.yml
index 19d91be..b012149 100644
--- a/fs/nffs/pkg.yml
+++ b/fs/nffs/pkg.yml
@@ -28,7 +28,7 @@ pkg.keywords:
pkg.deps:
- fs/fs
- - encoding/crc
+ - util/crc
- hw/hal
- kernel/os
- test/testutil
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/sys/shell/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/shell/pkg.yml b/sys/shell/pkg.yml
index c4449e5..f704169 100644
--- a/sys/shell/pkg.yml
+++ b/sys/shell/pkg.yml
@@ -27,7 +27,7 @@ pkg.deps:
- kernel/os
- libs/util
- encoding/base64
- - encoding/crc
+ - util/crc
pkg.req_apis:
- console
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/cbmem/include/cbmem/cbmem.h
----------------------------------------------------------------------
diff --git a/util/cbmem/include/cbmem/cbmem.h b/util/cbmem/include/cbmem/cbmem.h
new file mode 100644
index 0000000..42decfb
--- /dev/null
+++ b/util/cbmem/include/cbmem/cbmem.h
@@ -0,0 +1,66 @@
+/**
+ * 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 __UTIL_CBMEM_H__
+#define __UTIL_CBMEM_H__
+
+#include <os/os.h>
+
+struct cbmem_entry_hdr {
+ uint16_t ceh_len;
+ uint16_t ceh_flags;
+} __attribute__((packed));
+
+struct cbmem {
+ struct os_mutex c_lock;
+
+ struct cbmem_entry_hdr *c_entry_start;
+ struct cbmem_entry_hdr *c_entry_end;
+ uint8_t *c_buf;
+ uint8_t *c_buf_end;
+ uint8_t *c_buf_cur_end;
+};
+
+struct cbmem_iter {
+ struct cbmem_entry_hdr *ci_start;
+ struct cbmem_entry_hdr *ci_cur;
+ struct cbmem_entry_hdr *ci_end;
+};
+
+#define CBMEM_ENTRY_SIZE(__p) (sizeof(struct cbmem_entry_hdr) \
+ + ((struct cbmem_entry_hdr *) (__p))->ceh_len)
+#define CBMEM_ENTRY_NEXT(__p) ((struct cbmem_entry_hdr *) \
+ ((uint8_t *) (__p) + CBMEM_ENTRY_SIZE(__p)))
+
+typedef int (*cbmem_walk_func_t)(struct cbmem *, struct cbmem_entry_hdr *,
+ void *arg);
+
+int cbmem_lock_acquire(struct cbmem *cbmem);
+int cbmem_lock_release(struct cbmem *cbmem);
+int cbmem_init(struct cbmem *cbmem, void *buf, uint32_t buf_len);
+int cbmem_append(struct cbmem *cbmem, void *data, uint16_t len);
+void cbmem_iter_start(struct cbmem *cbmem, struct cbmem_iter *iter);
+struct cbmem_entry_hdr *cbmem_iter_next(struct cbmem *cbmem,
+ struct cbmem_iter *iter);
+int cbmem_read(struct cbmem *cbmem, struct cbmem_entry_hdr *hdr, void *buf,
+ uint16_t off, uint16_t len);
+int cbmem_walk(struct cbmem *cbmem, cbmem_walk_func_t walk_func, void *arg);
+
+int cbmem_flush(struct cbmem *);
+
+#endif /* __UTIL_CBMEM_H__ */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/cbmem/pkg.yml
----------------------------------------------------------------------
diff --git a/util/cbmem/pkg.yml b/util/cbmem/pkg.yml
new file mode 100644
index 0000000..9957805
--- /dev/null
+++ b/util/cbmem/pkg.yml
@@ -0,0 +1,30 @@
+#
+# 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.
+#
+
+pkg.name: libs/util
+pkg.description: Library containing miscellaneous utilities.
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+ - base64
+ - circular buffer
+
+pkg.deps:
+ - hw/hal
+ - kernel/os
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/cbmem/src/cbmem.c
----------------------------------------------------------------------
diff --git a/util/cbmem/src/cbmem.c b/util/cbmem/src/cbmem.c
new file mode 100644
index 0000000..63e098e
--- /dev/null
+++ b/util/cbmem/src/cbmem.c
@@ -0,0 +1,277 @@
+/**
+ * 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/os.h>
+#include <string.h>
+
+#include "cbmem/cbmem.h"
+
+
+int
+cbmem_init(struct cbmem *cbmem, void *buf, uint32_t buf_len)
+{
+ os_mutex_init(&cbmem->c_lock);
+
+ memset(cbmem, 0, sizeof(*cbmem));
+ cbmem->c_buf = buf;
+ cbmem->c_buf_end = buf + buf_len;
+
+ return (0);
+}
+
+int
+cbmem_lock_acquire(struct cbmem *cbmem)
+{
+ int rc;
+
+ if (!os_started()) {
+ return (0);
+ }
+
+ rc = os_mutex_pend(&cbmem->c_lock, OS_WAIT_FOREVER);
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (0);
+err:
+ return (rc);
+}
+
+int
+cbmem_lock_release(struct cbmem *cbmem)
+{
+ int rc;
+
+ if (!os_started()) {
+ return (0);
+ }
+
+ rc = os_mutex_release(&cbmem->c_lock);
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (0);
+err:
+ return (rc);
+}
+
+
+int
+cbmem_append(struct cbmem *cbmem, void *data, uint16_t len)
+{
+ struct cbmem_entry_hdr *dst;
+ uint8_t *start;
+ uint8_t *end;
+ int rc;
+
+ rc = cbmem_lock_acquire(cbmem);
+ if (rc != 0) {
+ goto err;
+ }
+
+ if (cbmem->c_entry_end) {
+ dst = CBMEM_ENTRY_NEXT(cbmem->c_entry_end);
+ } else {
+ dst = (struct cbmem_entry_hdr *) cbmem->c_buf;
+ }
+ end = (uint8_t *) dst + len + sizeof(*dst);
+
+ /* If this item would take us past the end of this buffer, then adjust
+ * the item to the beginning of the buffer.
+ */
+ if (end > cbmem->c_buf_end) {
+ cbmem->c_buf_cur_end = (uint8_t *) dst;
+ dst = (struct cbmem_entry_hdr *) cbmem->c_buf;
+ end = (uint8_t *) dst + len + sizeof(*dst);
+ if ((uint8_t *) cbmem->c_entry_start >= cbmem->c_buf_cur_end) {
+ cbmem->c_entry_start = (struct cbmem_entry_hdr *) cbmem->c_buf;
+ }
+ }
+
+ /* If the destination is prior to the start, and would overrwrite the
+ * start of the buffer, move start forward until you don't overwrite it
+ * anymore.
+ */
+ start = (uint8_t *) cbmem->c_entry_start;
+ if (start && (uint8_t *) dst < start + CBMEM_ENTRY_SIZE(start) &&
+ end > start) {
+ while (start < end) {
+ start = (uint8_t *) CBMEM_ENTRY_NEXT(start);
+ if (start == cbmem->c_buf_cur_end) {
+ start = cbmem->c_buf;
+ break;
+ }
+ }
+ cbmem->c_entry_start = (struct cbmem_entry_hdr *) start;
+ }
+
+ /* Copy the entry into the log
+ */
+ dst->ceh_len = len;
+ memcpy((uint8_t *) dst + sizeof(*dst), data, len);
+
+ cbmem->c_entry_end = dst;
+ if (!cbmem->c_entry_start) {
+ cbmem->c_entry_start = dst;
+ }
+
+ rc = cbmem_lock_release(cbmem);
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (0);
+err:
+ return (-1);
+}
+
+void
+cbmem_iter_start(struct cbmem *cbmem, struct cbmem_iter *iter)
+{
+ iter->ci_start = cbmem->c_entry_start;
+ iter->ci_cur = cbmem->c_entry_start;
+ iter->ci_end = cbmem->c_entry_end;
+}
+
+struct cbmem_entry_hdr *
+cbmem_iter_next(struct cbmem *cbmem, struct cbmem_iter *iter)
+{
+ struct cbmem_entry_hdr *hdr;
+
+ if (iter->ci_start > iter->ci_end) {
+ hdr = iter->ci_cur;
+ iter->ci_cur = CBMEM_ENTRY_NEXT(iter->ci_cur);
+
+ if ((uint8_t *) iter->ci_cur >= cbmem->c_buf_cur_end) {
+ iter->ci_cur = (struct cbmem_entry_hdr *) cbmem->c_buf;
+ iter->ci_start = (struct cbmem_entry_hdr *) cbmem->c_buf;
+ }
+ } else {
+ hdr = iter->ci_cur;
+ if (!iter->ci_cur) {
+ goto err;
+ }
+
+ if (hdr == CBMEM_ENTRY_NEXT(iter->ci_end)) {
+ hdr = NULL;
+ } else {
+ iter->ci_cur = CBMEM_ENTRY_NEXT(iter->ci_cur);
+ }
+ }
+
+err:
+ return (hdr);
+}
+
+int
+cbmem_flush(struct cbmem *cbmem)
+{
+ int rc;
+
+ rc = cbmem_lock_acquire(cbmem);
+ if (rc != 0) {
+ goto err;
+ }
+
+ cbmem->c_entry_start = NULL;
+ cbmem->c_entry_end = NULL;
+ cbmem->c_buf_cur_end = NULL;
+
+ rc = cbmem_lock_release(cbmem);
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (0);
+err:
+ return (rc);
+}
+
+int
+cbmem_read(struct cbmem *cbmem, struct cbmem_entry_hdr *hdr, void *buf,
+ uint16_t off, uint16_t len)
+{
+ int rc;
+
+ rc = cbmem_lock_acquire(cbmem);
+ if (rc != 0) {
+ goto err;
+ }
+
+ /* Only read the maximum number of bytes, if we exceed that,
+ * truncate the read.
+ */
+ if (off + len > hdr->ceh_len) {
+ len = hdr->ceh_len - off;
+ }
+
+ if (off > hdr->ceh_len) {
+ rc = -1;
+ cbmem_lock_release(cbmem);
+ goto err;
+ }
+
+ memcpy(buf, (uint8_t *) hdr + sizeof(*hdr) + off, len);
+
+ rc = cbmem_lock_release(cbmem);
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (len);
+err:
+ return (-1);
+}
+
+int
+cbmem_walk(struct cbmem *cbmem, cbmem_walk_func_t walk_func, void *arg)
+{
+ struct cbmem_entry_hdr *hdr;
+ struct cbmem_iter iter;
+ int rc;
+
+ rc = cbmem_lock_acquire(cbmem);
+ if (rc != 0) {
+ goto err;
+ }
+
+ cbmem_iter_start(cbmem, &iter);
+ while (1) {
+ hdr = cbmem_iter_next(cbmem, &iter);
+ if (hdr == NULL) {
+ break;
+ }
+
+ rc = walk_func(cbmem, hdr, arg);
+ if (rc == 1) {
+ break;
+ }
+ }
+
+ rc = cbmem_lock_release(cbmem);
+ if (rc != 0) {
+ goto err;
+ }
+
+ return (0);
+err:
+ return (rc);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/cbmem/test/pkg.yml
----------------------------------------------------------------------
diff --git a/util/cbmem/test/pkg.yml b/util/cbmem/test/pkg.yml
new file mode 100644
index 0000000..b03db98
--- /dev/null
+++ b/util/cbmem/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: util/cbmem/test
+pkg.type: unittest
+pkg.description: "Util unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+ - test/testutil
+ - util/cbmem
+
+pkg.deps.SELFTEST:
+ - sys/console/stub
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/cbmem/test/src/cbmem_test.c
----------------------------------------------------------------------
diff --git a/util/cbmem/test/src/cbmem_test.c b/util/cbmem/test/src/cbmem_test.c
new file mode 100644
index 0000000..b486334
--- /dev/null
+++ b/util/cbmem/test/src/cbmem_test.c
@@ -0,0 +1,176 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+
+#include "testutil/testutil.h"
+#include "util/cbmem.h"
+
+#define CBMEM1_BUF_SIZE (64 * 1024)
+
+struct cbmem cbmem1;
+uint8_t cbmem1_buf[CBMEM1_BUF_SIZE];
+uint8_t cbmem1_entry[1024];
+
+/*
+ * Things to test.
+ *
+ * - Wrap of the circular buffer.
+ * - Reading through all entries.
+ */
+
+static void
+setup_cbmem1(void)
+{
+ int i;
+ int rc;
+
+ rc = cbmem_init(&cbmem1, cbmem1_buf, CBMEM1_BUF_SIZE);
+ TEST_ASSERT_FATAL(rc == 0, "cbmem_init() failed, non-zero RC = %d", rc);
+
+ memset(cbmem1_entry, 0xff, sizeof(cbmem1_entry));
+
+ /* Insert 65 1024 entries, and overflow buffer.
+ * This should overflow two entries, because the buffer is sized for 64
+ * entries, and then the headers themselves will eat into one of the entries,
+ * so there should be a total of 63 entries.
+ * Ensure no data corruption.
+ */
+ for (i = 0; i < 65; i++) {
+ cbmem1_entry[0] = i;
+ rc = cbmem_append(&cbmem1, cbmem1_entry, sizeof(cbmem1_entry));
+ TEST_ASSERT_FATAL(rc == 0, "Could not append entry %d, rc = %d", i, rc);
+ }
+}
+
+static int
+cbmem_test_case_1_walk(struct cbmem *cbmem, struct cbmem_entry_hdr *hdr,
+ void *arg)
+{
+ uint8_t expected;
+ uint8_t actual;
+ int rc;
+
+ expected = *(uint8_t *) arg;
+
+ rc = cbmem_read(cbmem, hdr, &actual, 0, sizeof(actual));
+ TEST_ASSERT_FATAL(rc == 1, "Couldn't read 1 byte from cbmem");
+ TEST_ASSERT_FATAL(actual == expected,
+ "Actual doesn't equal expected (%d = %d)", actual, expected);
+
+ *(uint8_t *) arg = ++expected;
+
+ return (0);
+}
+
+TEST_CASE(cbmem_test_case_1)
+{
+ int i;
+ int rc;
+
+ /* i starts at 2, for the 2 overwritten entries. */
+ i = 2;
+ rc = cbmem_walk(&cbmem1, cbmem_test_case_1_walk, &i);
+ TEST_ASSERT_FATAL(rc == 0, "Could not walk cbmem tree! rc = %d", rc);
+ TEST_ASSERT_FATAL(i == 65,
+ "Did not go through every element of walk, %d processed", i - 2);
+
+}
+
+TEST_CASE(cbmem_test_case_2)
+{
+ struct cbmem_entry_hdr *hdr;
+ struct cbmem_iter iter;
+ uint8_t i;
+ uint8_t val;
+ int rc;
+
+ i = 2;
+ cbmem_iter_start(&cbmem1, &iter);
+ while (1) {
+ hdr = cbmem_iter_next(&cbmem1, &iter);
+ if (hdr == NULL) {
+ break;
+ }
+
+ rc = cbmem_read(&cbmem1, hdr, &val, 0, sizeof(val));
+ TEST_ASSERT_FATAL(rc == 1, "Couldn't read 1 byte from cbmem");
+ TEST_ASSERT_FATAL(val == i, "Entry index does not match %d vs %d",
+ val, i);
+
+ i++;
+ }
+
+ /* i starts at 2, for the 2 overwritten entries */
+ TEST_ASSERT_FATAL(i == 65,
+ "Did not iterate through all 63 elements of CBMEM1, processed %d",
+ i - 2);
+}
+
+TEST_CASE(cbmem_test_case_3)
+{
+ struct cbmem_entry_hdr *hdr;
+ struct cbmem_iter iter;
+ uint16_t off;
+ uint16_t len;
+ uint8_t buf[128];
+ int i;
+ int rc;
+
+ i = 0;
+ cbmem_iter_start(&cbmem1, &iter);
+ while (1) {
+ hdr = cbmem_iter_next(&cbmem1, &iter);
+ if (hdr == NULL) {
+ break;
+ }
+
+ /* first ensure we can read the entire entry */
+ off = 0;
+ len = 0;
+ while (1) {
+ rc = cbmem_read(&cbmem1, hdr, buf, off, sizeof(buf));
+ TEST_ASSERT_FATAL(rc >= 0,
+ "Error reading from buffer rc=%d, off=%d,len=%d", rc, off,
+ sizeof(buf));
+ if (rc == 0) {
+ break;
+ }
+ off += rc;
+ len += rc;
+ }
+ TEST_ASSERT_FATAL(len == 1024,
+ "Couldn't read full entry, expected %d got %d", 1024, len);
+ i++;
+
+ /* go apesh*t, and read data out of bounds, see what we get. */
+ rc = cbmem_read(&cbmem1, hdr, buf, 2048, sizeof(buf));
+ TEST_ASSERT_FATAL(rc < 0,
+ "Reading invalid should return error, instead %d returned.",
+ rc);
+ }
+}
+
+TEST_SUITE(cbmem_test_suite)
+{
+ setup_cbmem1();
+ cbmem_test_case_1();
+ cbmem_test_case_2();
+ cbmem_test_case_3();
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/cbmem/test/src/util_test.c
----------------------------------------------------------------------
diff --git a/util/cbmem/test/src/util_test.c b/util/cbmem/test/src/util_test.c
new file mode 100644
index 0000000..4e21a9a
--- /dev/null
+++ b/util/cbmem/test/src/util_test.c
@@ -0,0 +1,53 @@
+/**
+ * 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 <stddef.h>
+#include "syscfg/syscfg.h"
+#include "testutil/testutil.h"
+#include "util_test_priv.h"
+
+int
+util_test_all(void)
+{
+ cbmem_test_suite();
+ return tu_case_failed;
+}
+
+int
+hex_fmt_test_all(void)
+{
+ hex_fmt_test_suite();
+ return tu_case_failed;
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+ tu_config.tc_print_results = 1;
+ tu_init();
+
+ util_test_all();
+ hex_fmt_test_all();
+ return tu_any_failed;
+}
+
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/cbmem/test/src/util_test_priv.h
----------------------------------------------------------------------
diff --git a/util/cbmem/test/src/util_test_priv.h b/util/cbmem/test/src/util_test_priv.h
new file mode 100644
index 0000000..93289b8
--- /dev/null
+++ b/util/cbmem/test/src/util_test_priv.h
@@ -0,0 +1,25 @@
+/**
+ * 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 __UTIL_TEST_PRIV_
+#define __UTIL_TEST_PRIV_
+
+int cbmem_test_suite(void);
+
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/include/crc/crc16.h
----------------------------------------------------------------------
diff --git a/util/crc/include/crc/crc16.h b/util/crc/include/crc/crc16.h
new file mode 100644
index 0000000..b9b965f
--- /dev/null
+++ b/util/crc/include/crc/crc16.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2001-2010 Georges Menie (www.menie.org)
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the University of California, Berkeley nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _CRC16_H_
+#define _CRC16_H_
+
+#include <inttypes.h>
+
+#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
+unsigned short crc16_ccitt(uint16_t initial_crc, const void *buf, int len);
+
+#endif /* _CRC16_H_ */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/include/crc/crc8.h
----------------------------------------------------------------------
diff --git a/util/crc/include/crc/crc8.h b/util/crc/include/crc/crc8.h
new file mode 100644
index 0000000..336acca
--- /dev/null
+++ b/util/crc/include/crc/crc8.h
@@ -0,0 +1,32 @@
+/**
+ * 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.
+ */
+
+/*
+ * CRC8-CCITT, with normal polynomial; 0x07.
+ */
+
+#ifndef _UTIL_CRC8_H_
+#define _UTIL_CRC8_H_
+
+#include <inttypes.h>
+
+uint8_t crc8_init(void);
+uint8_t crc8_calc(uint8_t val, void *buf, int cnt);
+
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/pkg.yml
----------------------------------------------------------------------
diff --git a/util/crc/pkg.yml b/util/crc/pkg.yml
new file mode 100644
index 0000000..d5fd15b
--- /dev/null
+++ b/util/crc/pkg.yml
@@ -0,0 +1,28 @@
+#
+# 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.
+#
+
+pkg.name: util/crc
+pkg.description: Library containing encoding functions
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+ - crc16
+ - crc32
+ - crc8
+ - crc
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/src/crc16.c
----------------------------------------------------------------------
diff --git a/util/crc/src/crc16.c b/util/crc/src/crc16.c
new file mode 100644
index 0000000..6ece419
--- /dev/null
+++ b/util/crc/src/crc16.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2001-2010 Georges Menie (www.menie.org)
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the University of California, Berkeley nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <inttypes.h>
+#include "crc/crc16.h"
+
+/* CRC16 implementation acording to CCITT standards */
+
+static const uint16_t crc16tab[256]= {
+ 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
+ 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
+ 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
+ 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
+ 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
+ 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
+ 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
+ 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
+ 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
+ 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
+ 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
+ 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
+ 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
+ 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
+ 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
+ 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
+ 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
+ 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
+ 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
+ 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
+ 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
+ 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
+ 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
+ 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
+ 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
+ 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
+ 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
+ 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
+ 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
+ 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
+ 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
+ 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
+};
+
+uint16_t
+crc16_ccitt(uint16_t initial_crc, const void *buf, int len)
+{
+ const uint8_t *ptr;
+ uint16_t crc;
+ int counter;
+
+ crc = initial_crc;
+ ptr = buf;
+
+ for (counter = 0; counter < len; counter++) {
+ crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *ptr++)&0x00FF];
+ }
+
+ return crc;
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/src/crc8.c
----------------------------------------------------------------------
diff --git a/util/crc/src/crc8.c b/util/crc/src/crc8.c
new file mode 100644
index 0000000..384acb2
--- /dev/null
+++ b/util/crc/src/crc8.c
@@ -0,0 +1,74 @@
+/**
+ * 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.
+ */
+
+/*
+ * Table computation:
+ *
+ * void
+ * gen_small_table(uint8_t poly)
+ * {
+ * int i;
+ * int j;
+ * uint8_t curr;
+ *
+ * for (i = 0; i < 16; i++) {
+ * curr = i;
+ *
+ * for (j = 0; j < 8; j++) {
+ * if ((curr & 0x80) != 0) {
+ * curr = (curr << 1) ^ poly;
+ * } else {
+ * curr <<= 1;
+ * }
+ * }
+ *
+ * small_table[i] = curr;
+ *
+ * printf("0x%x, ", small_table[i]);
+ * }
+ * printf("\n");
+ *}
+ */
+
+#include "crc/crc8.h"
+
+static uint8_t crc8_small_table[16] = {
+ 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
+ 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d
+};
+
+uint8_t
+crc8_init(void)
+{
+ return 0xff;
+}
+
+uint8_t
+crc8_calc(uint8_t val, void *buf, int cnt)
+{
+ int i;
+ uint8_t *p = buf;
+
+ for (i = 0; i < cnt; i++) {
+ val ^= p[i];
+ val = (val << 4) ^ crc8_small_table[val >> 4];
+ val = (val << 4) ^ crc8_small_table[val >> 4];
+ }
+ return val;
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/test/pkg.yml
----------------------------------------------------------------------
diff --git a/util/crc/test/pkg.yml b/util/crc/test/pkg.yml
new file mode 100644
index 0000000..dac3cb1
--- /dev/null
+++ b/util/crc/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: crypto/encoding/test
+pkg.type: unittest
+pkg.description: "Crypto encoding unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+ - libs/testutil
+ - libs/util
+
+pkg.deps.SELFTEST:
+ - libs/console/stub
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/test/src/encoding_test.c
----------------------------------------------------------------------
diff --git a/util/crc/test/src/encoding_test.c b/util/crc/test/src/encoding_test.c
new file mode 100644
index 0000000..b21fee8
--- /dev/null
+++ b/util/crc/test/src/encoding_test.c
@@ -0,0 +1,46 @@
+/**
+ * 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 <stddef.h>
+#include "syscfg/syscfg.h"
+#include "testutil/testutil.h"
+#include "encoding_test_priv.h"
+
+
+int
+hex_fmt_test_all(void)
+{
+ hex_fmt_test_suite();
+ return tu_case_failed;
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+ tu_config.tc_print_results = 1;
+ tu_init();
+
+ hex_fmt_test_all();
+ return tu_any_failed;
+}
+
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/test/src/encoding_test_priv.h
----------------------------------------------------------------------
diff --git a/util/crc/test/src/encoding_test_priv.h b/util/crc/test/src/encoding_test_priv.h
new file mode 100644
index 0000000..fc2fcac
--- /dev/null
+++ b/util/crc/test/src/encoding_test_priv.h
@@ -0,0 +1,25 @@
+/**
+ * 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 __ENCODING_TEST_PRIV_
+#define __ENCODING_TEST_PRIV_
+
+int hex_fmt_test_suite(void);
+
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/54fbe73d/util/crc/test/src/hex_test.c
----------------------------------------------------------------------
diff --git a/util/crc/test/src/hex_test.c b/util/crc/test/src/hex_test.c
new file mode 100644
index 0000000..8183425
--- /dev/null
+++ b/util/crc/test/src/hex_test.c
@@ -0,0 +1,125 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+
+#include "testutil/testutil.h"
+#include "util/hex.h"
+
+TEST_CASE(hex2str)
+{
+ int i;
+ char *ret;
+ char cmp_data[8];
+
+ struct {
+ char *in;
+ int inlen;
+ char *out;
+ int outlen;
+ } test_data[] = {
+ [0] = {
+ .in = "\x01",
+ .inlen = 1,
+ .out = "01",
+ .outlen = 2,
+ },
+ [1] = {
+ .in = "\xaf\xf2",
+ .inlen = 2,
+ .out = "aff2",
+ .outlen = 4,
+ }
+ };
+
+ for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
+ ret = hex_format(test_data[i].in, test_data[i].inlen,
+ cmp_data, sizeof(cmp_data));
+ TEST_ASSERT(ret == cmp_data);
+ TEST_ASSERT(strlen(cmp_data) == test_data[i].outlen);
+ TEST_ASSERT(!strcmp(test_data[i].out, cmp_data));
+ }
+
+ /*
+ * Test not enough space. Must have space for '\0' at the end.
+ */
+ ret = hex_format("\x01\x02", 2, cmp_data, 1);
+ TEST_ASSERT(ret == NULL);
+
+ ret = hex_format("\x01\x02", 2, cmp_data, 2);
+ TEST_ASSERT(ret == NULL);
+}
+
+TEST_CASE(str2hex)
+{
+ int i;
+ char cmp_data[8];
+ int rc;
+
+ struct {
+ char *in;
+ int inlen;
+ char *out;
+ int outlen;
+ } test_data[] = {
+ [0] = {
+ .in = "01",
+ .inlen = 2,
+ .out = "\x01",
+ .outlen = 1,
+ },
+ [1] = {
+ .in = "AfF2",
+ .inlen = 4,
+ .out = "\xaf\xf2",
+ .outlen = 2,
+ }
+ };
+
+ for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
+ rc = hex_parse(test_data[i].in, test_data[i].inlen,
+ cmp_data, sizeof(cmp_data));
+ TEST_ASSERT(rc == test_data[i].outlen);
+ TEST_ASSERT(!memcmp(test_data[i].out, cmp_data, rc));
+ }
+
+ /*
+ * Test invalid input
+ */
+ rc = hex_parse("HJ", 2, cmp_data, sizeof(cmp_data));
+ TEST_ASSERT(rc < 0);
+
+ rc = hex_parse("a", 1, cmp_data, sizeof(cmp_data));
+ TEST_ASSERT(rc < 0);
+
+ rc = hex_parse("0102", 4, cmp_data, 1);
+ TEST_ASSERT(rc < 0);
+
+ /*
+ * This should be valid.
+ */
+ rc = hex_parse("0102", 4, cmp_data, 2);
+ TEST_ASSERT(rc == 2);
+}
+
+TEST_SUITE(hex_fmt_test_suite)
+{
+ hex2str();
+ str2hex();
+}