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:15 UTC

[08/49] incubator-mynewt-core git commit: directory re-org

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libs/tinycbor/src/cbortojson.c
----------------------------------------------------------------------
diff --git a/libs/tinycbor/src/cbortojson.c b/libs/tinycbor/src/cbortojson.c
deleted file mode 100644
index 953f2aa..0000000
--- a/libs/tinycbor/src/cbortojson.c
+++ /dev/null
@@ -1,686 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 Intel Corporation
-**
-** Permission is hereby granted, free of charge, to any person obtaining a copy
-** of this software and associated documentation files (the "Software"), to deal
-** in the Software without restriction, including without limitation the rights
-** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-** copies of the Software, and to permit persons to whom the Software is
-** furnished to do so, subject to the following conditions:
-**
-** The above copyright notice and this permission notice shall be included in
-** all copies or substantial portions of the Software.
-**
-** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-** THE SOFTWARE.
-**
-****************************************************************************/
-
-#define _BSD_SOURCE 1
-#define _DEFAULT_SOURCE 1
-#define _GNU_SOURCE 1
-#define _POSIX_C_SOURCE 200809L
-#ifndef __STDC_LIMIT_MACROS
-#  define __STDC_LIMIT_MACROS 1
-#endif
-
-#include "cbor.h"
-#include "cborjson.h"
-#include "compilersupport_p.h"
-#include "math_support_p.h"
-
-#include <float.h>
-#include <inttypes.h>
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-
-/**
- * \defgroup CborToJson Converting CBOR to JSON
- * \brief Group of functions used to convert CBOR to JSON.
- *
- * This group contains two functions that are can be used to convert one
- * CborValue object to an equivalent JSON representation. This module attempts
- * to follow the recommendations from RFC 7049 section 4.1 "Converting from
- * CBOR to JSON", though it has a few differences. They are noted below.
- *
- * These functions produce a "minified" JSON output, with no spacing,
- * indentation or line breaks. If those are necessary, they need to be applied
- * in a post-processing phase.
- *
- * Note that JSON cannot support all CBOR types with fidelity, so the
- * conversion is usually lossy. For that reason, TinyCBOR supports adding a set
- * of metadata JSON values that can be used by a JSON-to-CBOR converter to
- * restore the original data types.
- *
- * The TinyCBOR library does not provide a way to convert from JSON
- * representation back to encoded form. However, it provides a tool called
- * \c json2cbor which can be used for that purpose. That tool supports the
- * metadata format that these functions may produce.
- *
- * Either of the functions in this section will attempt to convert exactly one
- * CborValue object to JSON. Those functions may return any error documented
- * for the functions for CborParsing. In addition, if the C standard library
- * stream functions return with error, the text conversion will return with
- * error CborErrorIO.
- *
- * These functions also perform UTF-8 validation in CBOR text strings. If they
- * encounter a sequence of bytes that not permitted in UTF-8, they will return
- * CborErrorInvalidUtf8TextString. That includes encoding of surrogate points
- * in UTF-8.
- *
- * \warning The metadata produced by these functions is not guaranteed to
- * remain stable. A future update of TinyCBOR may produce different output for
- * the same input and parsers may be unable to handle them.
- *
- * \sa CborParsing, CborPretty, cbor_parser_init()
- */
-
-/**
- * \addtogroup CborToJson
- * @{
- * <h2 class="groupheader">Conversion limitations</h2>
- *
- * When converting from CBOR to JSON, there may be information loss. This
- * section lists the possible scenarios.
- *
- * \par Number precision:
- * ALL JSON numbers, due to its JavaScript heritage, are IEEE 754
- * double-precision floating point. This means JSON is not capable of
- * representing integers numbers outside the range [-(2<sup>53</sup>)+1,
- * 2<sup>53</sup>-1] and is not capable of representing NaN or infinite. If the
- * CBOR data contains a number outside the valid range, the conversion will
- * lose precision. If the input was NaN or infinite, the result of the
- * conversion will be "null". In addition, the distinction between half-,
- * single- and double-precision is lost.
- *
- * \par
- * If enabled, the original value and original type are stored in the metadata.
- *
- * \par Non-native types:
- * CBOR's type system is richer than JSON's, which means some data values
- * cannot be represented when converted to JSON. The conversion silently turns
- * them into strings: CBOR simple types become "simple(nn)" where \c nn is the
- * simple type's value, with the exception of CBOR undefined, which becomes
- * "undefined", while CBOR byte strings are converted to an Base16, Base64, or
- * Base64url encoding
- *
- * \par
- * If enabled, the original type is stored in the metadata.
- *
- * \par Presence of tags:
- * JSON has no support for tagged values, so by default tags are dropped when
- * converting to JSON. However, if the CborConvertObeyByteStringTags option is
- * active (default), then certain known tags are honored and are used to format
- * the conversion of the tagged byte string to JSON.
- *
- * \par
- * If the CborConvertTagsToObjects option is active, then the tag and the
- * tagged value are converted to to a JSON object. Otherwise, if enabled, the
- * last (innermost) tag is stored in the metadata.
- *
- * \par Non-string keys in maps:
- * JSON requires all Object keys to be strings, while CBOR does not. By
- * default, if a non-string key is found, the conversion fails with error
- * CborErrorJsonObjectKeyNotString. If the CborConvertStringifyMapKeys option
- * is active, then the conversion attempts to create a string representation
- * using CborPretty. Note that the \c json2cbor tool is not able to parse this
- * back to the original form.
- *
- * \par Duplicate keys in maps:
- * Neither JSON nor CBOR allow duplicated keys, but current TinyCBOR does not
- * validate that this is the case. If there are duplicated keys in the input,
- * they will be repeated in the output, which may JSON tools may flag as
- * invalid. In addition to that, if the CborConvertStringifyMapKeys option is
- * active, it is possible that a non-string key in a CBOR map will be converted
- * to a string form that is identical to another key.
- *
- * \par
- * When metadata support is active, the conversion will add extra key-value
- * pairs to the JSON output so it can store the metadata. It is possible that
- * the keys for the metadata clash with existing keys in the JSON map.
- */
-
-extern FILE *open_memstream(char **bufptr, size_t *sizeptr);
-
-enum ConversionStatusFlags {
-    TypeWasNotNative            = 0x100,    /* anything but strings, boolean, null, arrays and maps */
-    TypeWasTagged               = 0x200,
-    NumberPrecisionWasLost      = 0x400,
-    NumberWasNaN                = 0x800,
-    NumberWasInfinite           = 0x1000,
-    NumberWasNegative           = 0x2000,   /* always used with NumberWasInifite or NumberWasTooBig */
-
-    FinalTypeMask               = 0xff
-};
-
-typedef struct ConversionStatus {
-    CborTag lastTag;
-    uint64_t originalNumber;
-    int flags;
-} ConversionStatus;
-
-static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType type, ConversionStatus *status);
-
-static CborError dump_bytestring_base16(char **result, CborValue *it)
-{
-    static const char characters[] = "0123456789abcdef";
-    size_t i;
-    size_t n = 0;
-    uint8_t *buffer;
-    CborError err = cbor_value_calculate_string_length(it, &n);
-    if (err)
-        return err;
-
-    /* a Base16 (hex) output is twice as big as our buffer */
-    buffer = (uint8_t *)malloc(n * 2 + 1);
-    *result = (char *)buffer;
-
-    /* let cbor_value_copy_byte_string know we have an extra byte for the terminating NUL */
-    ++n;
-    err = cbor_value_copy_byte_string(it, buffer + n - 1, &n, it);
-    assert(err == CborNoError);
-
-    for (i = 0; i < n; ++i) {
-        uint8_t byte = buffer[n + i];
-        buffer[2*i]     = characters[byte >> 4];
-        buffer[2*i + 1] = characters[byte & 0xf];
-    }
-    return CborNoError;
-}
-
-static CborError generic_dump_base64(char **result, CborValue *it, const char alphabet[65])
-{
-    size_t n = 0, i;
-    uint8_t *buffer, *out, *in;
-    CborError err = cbor_value_calculate_string_length(it, &n);
-    if (err)
-        return err;
-
-    /* a Base64 output (untruncated) has 4 bytes for every 3 in the input */
-    size_t len = (n + 5) / 3 * 4;
-    out = buffer = (uint8_t *)malloc(len + 1);
-    *result = (char *)buffer;
-
-    /* we read our byte string at the tail end of the buffer
-     * so we can do an in-place conversion while iterating forwards */
-    in = buffer + len - n;
-
-    /* let cbor_value_copy_byte_string know we have an extra byte for the terminating NUL */
-    ++n;
-    err = cbor_value_copy_byte_string(it, in, &n, it);
-    assert(err == CborNoError);
-
-    uint_least32_t val = 0;
-    for (i = 0; n - i >= 3; i += 3) {
-        /* read 3 bytes x 8 bits = 24 bits */
-        if (false) {
-#ifdef __GNUC__
-        } else if (i) {
-            __builtin_memcpy(&val, in + i - 1, sizeof(val));
-            val = cbor_ntohl(val);
-#endif
-        } else {
-            val = (in[i] << 16) | (in[i + 1] << 8) | in[i + 2];
-        }
-
-        /* write 4 chars x 6 bits = 24 bits */
-        *out++ = alphabet[(val >> 18) & 0x3f];
-        *out++ = alphabet[(val >> 12) & 0x3f];
-        *out++ = alphabet[(val >> 6) & 0x3f];
-        *out++ = alphabet[val & 0x3f];
-    }
-
-    /* maybe 1 or 2 bytes left */
-    if (n - i) {
-        /* we can read in[i + 1] even if it's past the end of the string because
-         * we know (by construction) that it's a NUL byte */
-#ifdef __GNUC__
-        uint16_t val16;
-        __builtin_memcpy(&val16, in + i, sizeof(val16));
-        val = cbor_ntohs(val16);
-#else
-        val = (in[i] << 8) | in[i + 1];
-#endif
-        val <<= 8;
-
-        /* the 65th character in the alphabet is our filler: either '=' or '\0' */
-        out[4] = '\0';
-        out[3] = alphabet[64];
-        if (n - i == 2) {
-            /* write the third char in 3 chars x 6 bits = 18 bits */
-            out[2] = alphabet[(val >> 6) & 0x3f];
-        } else {
-            out[2] = alphabet[64];  /* filler */
-        }
-        out[1] = alphabet[(val >> 12) & 0x3f];
-        out[0] = alphabet[(val >> 18) & 0x3f];
-    } else {
-        out[0] = '\0';
-    }
-
-    return CborNoError;
-}
-
-static CborError dump_bytestring_base64(char **result, CborValue *it)
-{
-    static const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
-                                   "ghijklmn" "opqrstuv" "wxyz0123" "456789+/" "=";
-    return generic_dump_base64(result, it, alphabet);
-}
-
-static CborError dump_bytestring_base64url(char **result, CborValue *it)
-{
-    static const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
-                                   "ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
-    return generic_dump_base64(result, it, alphabet);
-}
-
-static CborError add_value_metadata(FILE *out, CborType type, const ConversionStatus *status)
-{
-    int flags = status->flags;
-    if (flags & TypeWasTagged) {
-        /* extract the tagged type, which may be JSON native */
-        type = flags & FinalTypeMask;
-        flags &= ~(FinalTypeMask | TypeWasTagged);
-
-        if (fprintf(out, "\"tag\":\"%" PRIu64 "\"%s", status->lastTag,
-                    flags & ~TypeWasTagged ? "," : "") < 0)
-            return CborErrorIO;
-    }
-
-    if (!flags)
-        return CborNoError;
-
-    /* print at least the type */
-    if (fprintf(out, "\"t\":%d", type) < 0)
-        return CborErrorIO;
-
-    if (flags & NumberWasNaN)
-        if (fprintf(out, ",\"v\":\"nan\"") < 0)
-            return CborErrorIO;
-    if (flags & NumberWasInfinite)
-        if (fprintf(out, ",\"v\":\"%sinf\"", flags & NumberWasNegative ? "-" : "") < 0)
-            return CborErrorIO;
-    if (flags & NumberPrecisionWasLost)
-        if (fprintf(out, ",\"v\":\"%c%" PRIx64 "\"", flags & NumberWasNegative ? '-' : '+',
-                    status->originalNumber) < 0)
-            return CborErrorIO;
-    if (type == CborSimpleType)
-        if (fprintf(out, ",\"v\":%d", (int)status->originalNumber) < 0)
-            return CborErrorIO;
-    return CborNoError;
-}
-
-static CborError find_tagged_type(CborValue *it, CborTag *tag, CborType *type)
-{
-    CborError err = CborNoError;
-    *type = cbor_value_get_type(it);
-    while (*type == CborTagType) {
-        cbor_value_get_tag(it, tag);    /* can't fail */
-        err = cbor_value_advance_fixed(it);
-        if (err)
-            return err;
-
-        *type = cbor_value_get_type(it);
-    }
-    return err;
-}
-
-static CborError tagged_value_to_json(FILE *out, CborValue *it, int flags, ConversionStatus *status)
-{
-    CborTag tag;
-    CborError err;
-
-    if (flags & CborConvertTagsToObjects) {
-        cbor_value_get_tag(it, &tag);       /* can't fail */
-        err = cbor_value_advance_fixed(it);
-        if (err)
-            return err;
-
-        if (fprintf(out, "{\"tag%" PRIu64 "\":", tag) < 0)
-            return CborErrorIO;
-
-        CborType type = cbor_value_get_type(it);
-        err = value_to_json(out, it, flags, type, status);
-        if (err)
-            return err;
-        if (flags & CborConvertAddMetadata && status->flags) {
-            if (fprintf(out, ",\"tag%" PRIu64 "$cbor\":{", tag) < 0 ||
-                    add_value_metadata(out, type, status) != CborNoError ||
-                    fputc('}', out) < 0)
-                return CborErrorIO;
-        }
-        if (fputc('}', out) < 0)
-            return CborErrorIO;
-        status->flags = TypeWasNotNative | CborTagType;
-        return CborNoError;
-    }
-
-    CborType type;
-    err = find_tagged_type(it, &status->lastTag, &type);
-    if (err)
-        return err;
-    tag = status->lastTag;
-
-    /* special handling of byte strings? */
-    if (type == CborByteStringType && (flags & CborConvertByteStringsToBase64Url) == 0 &&
-            (tag == CborNegativeBignumTag || tag == CborExpectedBase16Tag || tag == CborExpectedBase64Tag)) {
-        char *str;
-        char *pre = "";
-
-        if (tag == CborNegativeBignumTag) {
-            pre = "~";
-            err = dump_bytestring_base64url(&str, it);
-        } else if (tag == CborExpectedBase64Tag) {
-            err = dump_bytestring_base64(&str, it);
-        } else { /* tag == CborExpectedBase16Tag */
-            err = dump_bytestring_base16(&str, it);
-        }
-        if (err)
-            return err;
-        err = fprintf(out, "\"%s%s\"", pre, str) < 0 ? CborErrorIO : CborNoError;
-        free(str);
-        status->flags = TypeWasNotNative | TypeWasTagged | CborByteStringType;
-        return err;
-    }
-
-    /* no special handling */
-    err = value_to_json(out, it, flags, type, status);
-    status->flags |= TypeWasTagged | type;
-    return err;
-}
-
-static CborError stringify_map_key(char **key, CborValue *it, int flags, CborType type)
-{
-    (void)flags;    /* unused */
-    (void)type;     /* unused */
-#ifdef WITHOUT_OPEN_MEMSTREAM
-    (void)key;      /* unused */
-    (void)it;       /* unused */
-    return CborErrorJsonNotImplemented;
-#else
-    size_t size;
-
-    FILE *memstream = open_memstream(key, &size);
-    if (memstream == NULL)
-        return CborErrorOutOfMemory;        /* could also be EMFILE, but it's unlikely */
-    CborError err = cbor_value_to_pretty_advance(memstream, it);
-
-    if (unlikely(fclose(memstream) < 0 || *key == NULL))
-        return CborErrorInternalError;
-    return err;
-#endif
-}
-
-static CborError array_to_json(FILE *out, CborValue *it, int flags, ConversionStatus *status)
-{
-    const char *comma = "";
-    while (!cbor_value_at_end(it)) {
-        if (fprintf(out, "%s", comma) < 0)
-            return CborErrorIO;
-        comma = ",";
-
-        CborError err = value_to_json(out, it, flags, cbor_value_get_type(it), status);
-        if (err)
-            return err;
-    }
-    return CborNoError;
-}
-
-static CborError map_to_json(FILE *out, CborValue *it, int flags, ConversionStatus *status)
-{
-    const char *comma = "";
-    CborError err;
-    while (!cbor_value_at_end(it)) {
-        char *key;
-        if (fprintf(out, "%s", comma) < 0)
-            return CborErrorIO;
-        comma = ",";
-
-        CborType keyType = cbor_value_get_type(it);
-        if (likely(keyType == CborTextStringType)) {
-            size_t n = 0;
-            err = cbor_value_dup_text_string(it, &key, &n, it);
-        } else if (flags & CborConvertStringifyMapKeys) {
-            err = stringify_map_key(&key, it, flags, keyType);
-        } else {
-            return CborErrorJsonObjectKeyNotString;
-        }
-        if (err)
-            return err;
-
-        /* first, print the key */
-        if (fprintf(out, "\"%s\":", key) < 0)
-            return CborErrorIO;
-
-        /* then, print the value */
-        CborType valueType = cbor_value_get_type(it);
-        err = value_to_json(out, it, flags, valueType, status);
-
-        /* finally, print any metadata we may have */
-        if (flags & CborConvertAddMetadata) {
-            if (!err && keyType != CborTextStringType) {
-                if (fprintf(out, ",\"%s$keycbordump\":true", key) < 0)
-                    err = CborErrorIO;
-            }
-            if (!err && status->flags) {
-                if (fprintf(out, ",\"%s$cbor\":{", key) < 0 ||
-                        add_value_metadata(out, valueType, status) != CborNoError ||
-                        fputc('}', out) < 0)
-                    err = CborErrorIO;
-            }
-        }
-
-        free(key);
-        if (err)
-            return err;
-    }
-    return CborNoError;
-}
-
-static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType type, ConversionStatus *status)
-{
-    CborError err;
-    status->flags = 0;
-
-    switch (type) {
-    case CborArrayType:
-    case CborMapType: {
-        /* recursive type */
-        CborValue recursed;
-        err = cbor_value_enter_container(it, &recursed);
-        if (err) {
-            it->ptr = recursed.ptr;
-            return err;       /* parse error */
-        }
-        if (fputc(type == CborArrayType ? '[' : '{', out) < 0)
-            return CborErrorIO;
-
-        err = (type == CborArrayType) ?
-                  array_to_json(out, &recursed, flags, status) :
-                  map_to_json(out, &recursed, flags, status);
-        if (err) {
-            it->ptr = recursed.ptr;
-            return err;       /* parse error */
-        }
-
-        if (fputc(type == CborArrayType ? ']' : '}', out) < 0)
-            return CborErrorIO;
-        err = cbor_value_leave_container(it, &recursed);
-        if (err)
-            return err;       /* parse error */
-
-        status->flags = 0;    /* reset, there are never conversion errors for us */
-        return CborNoError;
-    }
-
-    case CborIntegerType: {
-        double num;     /* JS numbers are IEEE double precision */
-        uint64_t val;
-        cbor_value_get_raw_integer(it, &val);    /* can't fail */
-        num = (double)val;
-
-        if (cbor_value_is_negative_integer(it)) {
-            num = -num - 1;                     /* convert to negative */
-            if ((uint64_t)(-num - 1) != val) {
-                status->flags = NumberPrecisionWasLost | NumberWasNegative;
-                status->originalNumber = val;
-            }
-        } else {
-            if ((uint64_t)num != val) {
-                status->flags = NumberPrecisionWasLost;
-                status->originalNumber = val;
-            }
-        }
-        if (fprintf(out, "%.0f", num) < 0)  /* this number has no fraction, so no decimal points please */
-            return CborErrorIO;
-        break;
-    }
-
-    case CborByteStringType:
-    case CborTextStringType: {
-        char *str;
-        if (type == CborByteStringType) {
-            err = dump_bytestring_base64url(&str, it);
-            status->flags = TypeWasNotNative;
-        } else {
-            size_t n = 0;
-            err = cbor_value_dup_text_string(it, &str, &n, it);
-        }
-        if (err)
-            return err;
-        err = (fprintf(out, "\"%s\"", str) < 0) ? CborErrorIO : CborNoError;
-        free(str);
-        return err;
-    }
-
-    case CborTagType:
-        return tagged_value_to_json(out, it, flags, status);
-
-    case CborSimpleType: {
-        uint8_t simple_type;
-        cbor_value_get_simple_type(it, &simple_type);  /* can't fail */
-        status->flags = TypeWasNotNative;
-        status->originalNumber = simple_type;
-        if (fprintf(out, "\"simple(%" PRIu8 ")\"", simple_type) < 0)
-            return CborErrorIO;
-        break;
-    }
-
-    case CborNullType:
-        if (fprintf(out, "null") < 0)
-            return CborErrorIO;
-        break;
-
-    case CborUndefinedType:
-        status->flags = TypeWasNotNative;
-        if (fprintf(out, "\"undefined\"") < 0)
-            return CborErrorIO;
-        break;
-
-    case CborBooleanType: {
-        bool val;
-        cbor_value_get_boolean(it, &val);       /* can't fail */
-        if (fprintf(out, val ? "true" : "false") < 0)
-            return CborErrorIO;
-        break;
-    }
-
-    case CborDoubleType: {
-        double val;
-        if (false) {
-            float f;
-    case CborFloatType:
-            status->flags = TypeWasNotNative;
-            cbor_value_get_float(it, &f);
-            val = f;
-        } else if (false) {
-            uint16_t f16;
-    case CborHalfFloatType:
-            status->flags = TypeWasNotNative;
-            cbor_value_get_half_float(it, &f16);
-            val = decode_half(f16);
-        } else {
-            cbor_value_get_double(it, &val);
-        }
-
-        int r = fpclassify(val);
-        if (r == FP_NAN || r == FP_INFINITE) {
-            if (fprintf(out, "null") < 0)
-                return CborErrorIO;
-            status->flags |= r == FP_NAN ? NumberWasNaN :
-                                           NumberWasInfinite | (val < 0 ? NumberWasNegative : 0);
-        } else {
-            uint64_t ival = (uint64_t)fabs(val);
-            if ((double)ival == fabs(val)) {
-                /* print as integer so we get the full precision */
-                r = fprintf(out, "%s%" PRIu64, val < 0 ? "-" : "", ival);
-                status->flags |= TypeWasNotNative;   /* mark this integer number as a double */
-            } else {
-                /* this number is definitely not a 64-bit integer */
-                r = fprintf(out, "%." DBL_DECIMAL_DIG_STR "g", val);
-            }
-            if (r < 0)
-                return CborErrorIO;
-        }
-        break;
-    }
-
-    case CborInvalidType:
-        return CborErrorUnknownType;
-    }
-
-    return cbor_value_advance_fixed(it);
-}
-
-/**
- * \enum CborToJsonFlags
- * The CborToJsonFlags enum contains flags that control the conversion of CBOR to JSON.
- *
- * \value CborConvertAddMetadata        Adds metadata to facilitate restoration of the original CBOR data.
- * \value CborConvertTagsToObjects      Converts CBOR tags to JSON objects
- * \value CborConvertIgnoreTags         (default) Ignore CBOR tags, except for byte strings
- * \value CborConvertObeyByteStringTags (default) Honor formatting of CBOR byte strings if so tagged
- * \value CborConvertByteStringsToBase64Url Force the conversion of all CBOR byte strings to Base64url encoding, despite any tags
- * \value CborConvertRequireMapStringKeys (default) Require CBOR map keys to be strings, failing the conversion if they are not
- * \value CborConvertStringifyMapKeys   Convert non-string keys in CBOR maps to a string form
- * \value CborConvertDefaultFlags       Default conversion flags.
- */
-
-/**
- * \fn CborError cbor_value_to_json(FILE *out, const CborValue *value, int flags)
- *
- * Converts the current CBOR type pointed by \a value to JSON and writes that
- * to the \a out stream. If an error occurs, this function returns an error
- * code similar to CborParsing. The \a flags parameter indicates one of the
- * flags from CborToJsonFlags that control the conversion.
- *
- * \sa cbor_value_to_json_advance(), cbor_value_to_pretty()
- */
-
-/**
- * Converts the current CBOR type pointed by \a value to JSON and writes that
- * to the \a out stream. If an error occurs, this function returns an error
- * code similar to CborParsing. The \a flags parameter indicates one of the
- * flags from CborToJsonFlags that control the conversion.
- *
- * If no error ocurred, this function advances \a value to the next element.
- *
- * \sa cbor_value_to_json(), cbor_value_to_pretty_advance()
- */
-CborError cbor_value_to_json_advance(FILE *out, CborValue *value, int flags)
-{
-    ConversionStatus status;
-    return value_to_json(out, value, flags, cbor_value_get_type(value), &status);
-}
-
-/** @} */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libs/tinycbor/src/open_memstream.c
----------------------------------------------------------------------
diff --git a/libs/tinycbor/src/open_memstream.c b/libs/tinycbor/src/open_memstream.c
deleted file mode 100644
index eaa53e5..0000000
--- a/libs/tinycbor/src/open_memstream.c
+++ /dev/null
@@ -1,117 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2015 Intel Corporation
-**
-** Permission is hereby granted, free of charge, to any person obtaining a copy
-** of this software and associated documentation files (the "Software"), to deal
-** in the Software without restriction, including without limitation the rights
-** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-** copies of the Software, and to permit persons to whom the Software is
-** furnished to do so, subject to the following conditions:
-**
-** The above copyright notice and this permission notice shall be included in
-** all copies or substantial portions of the Software.
-**
-** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-** THE SOFTWARE.
-**
-****************************************************************************/
-
-#define _BSD_SOURCE 1
-#define _DEFAULT_SOURCE 1
-#define _GNU_SOURCE 1
-
-#ifndef WITHOUT_OPEN_MEMSTREAM
-
-#include <sys/types.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#if defined(__unix__) || defined(__APPLE__)
-#  include <unistd.h>
-#endif
-#ifdef __APPLE__
-typedef int RetType;
-typedef int LenType;
-#elif __GLIBC__
-typedef ssize_t RetType;
-typedef size_t LenType;
-#else
-#  error "Cannot implement open_memstream!"
-#endif
-
-#include "compilersupport_p.h"
-
-struct Buffer
-{
-    char **ptr;
-    size_t *len;
-    size_t alloc;
-};
-
-static RetType write_to_buffer(void *cookie, const char *data, LenType len)
-{
-    struct Buffer *b = (struct Buffer *)cookie;
-    char *ptr = *b->ptr;
-    size_t newsize;
-
-    errno = EFBIG;
-    if (unlikely(add_check_overflow(*b->len, len, &newsize)))
-        return -1;
-
-    if (newsize > b->alloc) {
-        // make room
-        size_t newalloc = newsize + newsize / 2 + 1;    // give 50% more room
-        ptr = realloc(ptr, newalloc);
-        if (ptr == NULL)
-            return -1;
-        b->alloc = newalloc;
-        *b->ptr = ptr;
-    }
-
-    memcpy(ptr + *b->len, data, len);
-    *b->len = newsize;
-    return len;
-}
-
-static int close_buffer(void *cookie)
-{
-    struct Buffer *b = (struct Buffer *)cookie;
-    if (*b->ptr)
-        (*b->ptr)[*b->len] = '\0';
-    free(b);
-    return 0;
-}
-
-FILE *open_memstream(char **bufptr, size_t *lenptr)
-{
-    struct Buffer *b = (struct Buffer *)malloc(sizeof(struct Buffer));
-    if (b == NULL)
-        return NULL;
-    b->alloc = 0;
-    b->len = lenptr;
-    b->ptr = bufptr;
-    *bufptr = NULL;
-    *lenptr = 0;
-
-#ifdef __APPLE__
-    return funopen(b, NULL, write_to_buffer, NULL, close_buffer);
-#elif __GLIBC__
-    static const cookie_io_functions_t vtable = {
-        NULL,
-        write_to_buffer,
-        NULL,
-        close_buffer
-    };
-    return fopencookie(b, "w", vtable);
-#endif
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libs/util/include/util/base64.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/base64.h b/libs/util/include/util/base64.h
deleted file mode 100644
index 8e0c045..0000000
--- a/libs/util/include/util/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/6a7432f4/libs/util/include/util/crc16.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/crc16.h b/libs/util/include/util/crc16.h
deleted file mode 100644
index b9b965f..0000000
--- a/libs/util/include/util/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/6a7432f4/libs/util/include/util/crc8.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/crc8.h b/libs/util/include/util/crc8.h
deleted file mode 100644
index 336acca..0000000
--- a/libs/util/include/util/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/6a7432f4/libs/util/include/util/hex.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/hex.h b/libs/util/include/util/hex.h
deleted file mode 100644
index 1e9d8c8..0000000
--- a/libs/util/include/util/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/6a7432f4/libs/util/include/util/tpq.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/tpq.h b/libs/util/include/util/tpq.h
deleted file mode 100644
index 9e2b8a5..0000000
--- a/libs/util/include/util/tpq.h
+++ /dev/null
@@ -1,67 +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_TPQ_H__ 
-#define __UTIL_TPQ_H__
-
-#include <os/queue.h>
-#include <os/os_eventq.h>
-
-/* A task packet queue element */
-struct tpq_elem {
-    STAILQ_ENTRY(tpq_elem) tpq_next;
-};
-
-/* The task packet queue object */
-struct tpq
-{
-    STAILQ_HEAD(, tpq_elem) tpq_head;
-    struct os_event tpq_ev;
-};
-
-/**
- * Put an element on a task packet queue and post an event to an event queue. 
- * 
- * @param evq   Pointer to event queue
- * @param tpq   Pointer to task packet queue
- * @param elem  Pointer to element to enqueue
- */
-void tpq_put(struct os_eventq *evq, struct tpq *tpq, struct tpq_elem *elem);
-
-/**
- * Retrieve an element from a task packet queue. This removes the element at 
- * the head of the queue. 
- * 
- * @param head 
- * 
- * @return struct tpq_elem* 
- */
-struct tpq_elem *tpq_get(struct tpq *tpq);
-
-/**
- * Initialize a task packet queue 
- * 
- * @param tpq Pointer to task packet queue
- * @param ev_type Type of event.
- * @param ev_arg Argument of event
- * 
- * @return int 
- */
-void tpq_init(struct tpq *tpq, uint8_t ev_type, void *ev_arg);
-
-#endif /* __UTIL_TPQ_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libs/util/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/util/pkg.yml b/libs/util/pkg.yml
index b922830..9957805 100644
--- a/libs/util/pkg.yml
+++ b/libs/util/pkg.yml
@@ -27,4 +27,4 @@ pkg.keywords:
 
 pkg.deps:
     - hw/hal
-    - libs/os
+    - kernel/os

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libs/util/src/base64.c
----------------------------------------------------------------------
diff --git a/libs/util/src/base64.c b/libs/util/src/base64.c
deleted file mode 100644
index 685135e..0000000
--- a/libs/util/src/base64.c
+++ /dev/null
@@ -1,181 +0,0 @@
-/* 
- * This file is based on roken from the FreeBSD source.  It has been modified
- * to not use malloc() and instead expect static buffers, and tabs have been 
- * replaced with spaces.  Also, instead of strlen() on the resulting string, 
- * pointer arithmitic is done, as p represents the end of the buffer.
- */
-
-/* 
- * Copyright (c) 1995-2001 Kungliga Tekniska H�gskolan
- * (Royal Institute of Technology, Stockholm, Sweden).
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. 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.
- *
- * 3. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE OR 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 <stdlib.h>
-#include <string.h>
-
-#include <stdio.h>
-
-#include <util/base64.h>
-
-static const char base64_chars[] = 
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-static int 
-pos(char c)
-{
-    const char *p;
-    for (p = base64_chars; *p; p++)
-        if (*p == c)
-            return p - base64_chars;
-    return -1;
-}
-
-int 
-base64_encode(const void *data, int size, char *s, uint8_t should_pad)
-{
-    char *p;
-    int i;
-    int c;
-    const unsigned char *q;
-    char *last;
-    int diff;
-
-    p = s;
-
-    q = (const unsigned char *) data;
-    last = NULL;
-    i = 0;
-    while (i < size) {
-        c = q[i++];
-        c *= 256;
-        if (i < size)
-            c += q[i];
-        i++;
-        c *= 256;
-        if (i < size)
-            c += q[i];
-        i++;
-        p[0] = base64_chars[(c & 0x00fc0000) >> 18];
-        p[1] = base64_chars[(c & 0x0003f000) >> 12];
-        p[2] = base64_chars[(c & 0x00000fc0) >> 6];
-        p[3] = base64_chars[(c & 0x0000003f) >> 0];
-        last = p;
-        p += 4;
-    }
-
-    if (last) {
-        diff = i - size;
-        if (diff > 0) {
-            if (should_pad) {
-                memset(last + (4 - diff), '=', diff);
-            } else {
-                p = last + (4 - diff);
-            }
-        }
-    } 
-
-    *p = 0;
-
-    return (p - s);
-}
-
-int 
-base64_pad(char *buf, int len)
-{
-    int remainder;
-
-    remainder = len % 4;
-    if (remainder == 0) {
-        return (0);
-    }
-
-    memset(buf, '=', 4 - remainder);
-
-    return (4 - remainder);
-}
-
-#define DECODE_ERROR -1
-
-static unsigned int
-token_decode(const char *token)
-{
-    int i;
-    unsigned int val = 0;
-    int marker = 0;
-    if (strlen(token) < 4)
-        return DECODE_ERROR;
-    for (i = 0; i < 4; i++) {
-        val *= 64;
-        if (token[i] == '=')
-            marker++;
-        else if (marker > 0)
-            return DECODE_ERROR;
-        else
-            val += pos(token[i]);
-    }
-    if (marker > 2)
-        return DECODE_ERROR;
-    return (marker << 24) | val;
-}
-
-int 
-base64_decode(const char *str, void *data)
-{
-    const char *p;
-    unsigned char *q;
-
-    q = data;
-    for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
-        unsigned int val = token_decode(p);
-        unsigned int marker = (val >> 24) & 0xff;
-        if (val == DECODE_ERROR)
-            return -1;
-        *q++ = (val >> 16) & 0xff;
-        if (marker < 2)
-            *q++ = (val >> 8) & 0xff;
-        if (marker < 1)
-            *q++ = val & 0xff;
-    }
-    return q - (unsigned char *) data;
-}
-
-
-int
-base64_decode_len(const char *str)
-{
-    int len;
-
-    len = strlen(str);
-    while (len && str[len - 1] == '=') {
-        len--;
-    }
-    return len * 3 / 4;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libs/util/src/crc16.c
----------------------------------------------------------------------
diff --git a/libs/util/src/crc16.c b/libs/util/src/crc16.c
deleted file mode 100644
index 625d690..0000000
--- a/libs/util/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 "util/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/6a7432f4/libs/util/src/crc8.c
----------------------------------------------------------------------
diff --git a/libs/util/src/crc8.c b/libs/util/src/crc8.c
deleted file mode 100644
index 389e43a..0000000
--- a/libs/util/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 "util/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/6a7432f4/libs/util/src/hex.c
----------------------------------------------------------------------
diff --git a/libs/util/src/hex.c b/libs/util/src/hex.c
deleted file mode 100644
index 153f974..0000000
--- a/libs/util/src/hex.c
+++ /dev/null
@@ -1,101 +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 <inttypes.h>
-#include <ctype.h>
-#include <stddef.h>
-
-#include "util/hex.h"
-
-static const char hex_bytes[] = "0123456789abcdef";
-
-/*
- * Turn byte array into a printable array. I.e. "\x01" -> "01"
- *
- * @param src_v		Data to convert
- * @param src_len	Number of bytes of input
- * @param dst		String where to place the results
- * @param dst_len	Size of the target string
- *
- * @return		Pointer to 'dst' if successful; NULL on failure
- */
-char *
-hex_format(void *src_v, int src_len, char *dst, int dst_len)
-{
-    int i;
-    uint8_t *src = (uint8_t *)src_v;
-    char *tgt = dst;
-
-    if (dst_len <= src_len * 2) {
-        return NULL;
-    }
-    for (i = 0; i < src_len; i++) {
-        tgt[0] = hex_bytes[(src[i] >> 4) & 0xf];
-        tgt[1] = hex_bytes[src[i] & 0xf];
-        tgt += 2;
-        dst_len -= 2;
-    }
-    *tgt = '\0';
-    return dst;
-}
-
-/*
- * Turn string of hex decimals into a byte array. I.e. "01" -> "\x01
- *
- * @param src		String to convert
- * @param src_len	Number of bytes in input string
- * @param dst_v		Memory location to place the result
- * @param dst_len	Amount of space for the result
- *
- * @return		-1 on failure; number of bytes of input
- */
-int
-hex_parse(char *src, int src_len, void *dst_v, int dst_len)
-{
-    int i;
-    uint8_t *dst = (uint8_t *)dst_v;
-    char c;
-
-    if (src_len & 0x1) {
-        return -1;
-    }
-    if (dst_len * 2 < src_len) {
-        return -1;
-    }
-    for (i = 0; i < src_len; i++, src++) {
-        c = *src;
-        if (isdigit(c)) {
-            c -= '0';
-        } else if (c >= 'a' && c <= 'f') {
-            c -= ('a' - 10);
-        } else if (c >= 'A' && c <= 'F') {
-            c -= ('A' - 10);
-        } else {
-            return -1;
-        }
-        if (i & 1) {
-            *dst |= c;
-            dst++;
-            dst_len--;
-        } else {
-            *dst = c << 4;
-        }
-    }
-    return src_len >> 1;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libs/util/src/tpq.c
----------------------------------------------------------------------
diff --git a/libs/util/src/tpq.c b/libs/util/src/tpq.c
deleted file mode 100644
index 71c35b6..0000000
--- a/libs/util/src/tpq.c
+++ /dev/null
@@ -1,88 +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 "os/os.h"
-#include "util/tpq.h"
-
-/**
- * Put an element on a task packet queue and post an event to an event queue. 
- * 
- * @param evq   Pointer to event queue
- * @param tpq   Pointer to task packet queue
- * @param elem  Pointer to element to enqueue
- */
-void
-tpq_put(struct os_eventq *evq, struct tpq *tpq, struct tpq_elem *elem)
-{
-    os_sr_t sr;
-
-    OS_ENTER_CRITICAL(sr);
-    STAILQ_INSERT_TAIL(&tpq->tpq_head, elem, tpq_next);
-    OS_EXIT_CRITICAL(sr);
-    os_eventq_put(evq, &tpq->tpq_ev);
-}
-
-/**
- * Retrieve an element from a task packet queue. This removes the element at 
- * the head of the queue. 
- * 
- * @param head 
- * 
- * @return struct tpq_elem* 
- */
-struct tpq_elem *
-tpq_get(struct tpq *tpq)
-{
-    os_sr_t sr;
-    struct tpq_elem *elem;
-
-    OS_ENTER_CRITICAL(sr);
-    elem = STAILQ_FIRST(&tpq->tpq_head);
-    if (elem) {
-        STAILQ_REMOVE_HEAD(&tpq->tpq_head, tpq_next);
-    }
-    OS_EXIT_CRITICAL(sr);
-
-    return elem;
-}
-
-/**
- * Initialize a task packet queue 
- * 
- * @param tpq Pointer to task packet queue
- * @param ev_type Type of event.
- * @param ev_arg Argument of event
- * 
- * @return int 
- */
-void
-tpq_init(struct tpq *tpq, uint8_t ev_type, void *ev_arg)
-{
-    struct os_event *ev;
-
-    /* Initialize the task packet queue */
-    STAILQ_INIT(&tpq->tpq_head);
-
-    /* Initial task packet queue event */
-    ev = &tpq->tpq_ev;
-    ev->ev_arg = ev_arg;
-    ev->ev_type = ev_type;
-    ev->ev_queued = 0;
-    STAILQ_NEXT(ev, ev_next) = NULL;
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/net/ip/inet_def_service/pkg.yml
----------------------------------------------------------------------
diff --git a/net/ip/inet_def_service/pkg.yml b/net/ip/inet_def_service/pkg.yml
index 061d0cb..2780694 100644
--- a/net/ip/inet_def_service/pkg.yml
+++ b/net/ip/inet_def_service/pkg.yml
@@ -27,8 +27,8 @@ pkg.keywords:
     - test
 
 pkg.deps:
-    - sys/mn_socket
-    - libs/os
+    - net/ip/mn_socket
+    - kernel/os
     - libs/util
 
 pkg.reqs:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/net/ip/mn_socket/include/mn_socket/arch/sim/native_sock.h
----------------------------------------------------------------------
diff --git a/net/ip/mn_socket/include/mn_socket/arch/sim/native_sock.h b/net/ip/mn_socket/include/mn_socket/arch/sim/native_sock.h
new file mode 100644
index 0000000..66e8016
--- /dev/null
+++ b/net/ip/mn_socket/include/mn_socket/arch/sim/native_sock.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 __NATIVE_SOCK_H_
+#define __NATIVE_SOCK_H_
+
+int native_sock_init(void);
+
+#endif /* __NATIVE_SOCK_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/net/ip/mn_socket/include/mn_socket/mn_socket.h
----------------------------------------------------------------------
diff --git a/net/ip/mn_socket/include/mn_socket/mn_socket.h b/net/ip/mn_socket/include/mn_socket/mn_socket.h
new file mode 100644
index 0000000..47c54fb
--- /dev/null
+++ b/net/ip/mn_socket/include/mn_socket/mn_socket.h
@@ -0,0 +1,218 @@
+/**
+ * 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 __SYS_MN_SOCKET_H_
+#define __SYS_MN_SOCKET_H_
+
+#include <inttypes.h>
+
+/*
+ * Address/protocol family.
+ */
+#define MN_AF_INET              4
+#define MN_PF_INET              MN_AF_INET
+#define MN_AF_INET6             6
+#define MN_PF_INET6             MN_AF_INET6
+
+/*
+ * Socket types
+ */
+#define MN_SOCK_STREAM          1
+#define MN_SOCK_DGRAM           2
+
+/*
+ * Error codes from mn_socket interface.
+ */
+#define MN_EAFNOSUPPORT         1
+#define MN_EPROTONOSUPPORT      2
+#define MN_ENOBUFS              3
+#define MN_EINVAL               4
+#define MN_ENOTCONN             5
+#define MN_ECONNABORTED         6
+#define MN_EDESTADDRREQ         7
+#define MN_EADDRINUSE           8
+#define MN_ETIMEDOUT            9
+#define MN_EAGAIN               10
+#define MN_EUNKNOWN             11
+#define MN_EADDRNOTAVAIL        12
+
+/*
+ * Multicast macros
+ */
+#define MN_IN_MULTICAST(a)                                              \
+    ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
+
+#define MN_IN6_IS_ADDR_MULTICAST(a)                                     \
+    ((a)->s_addr[0] == 0xff)
+
+struct mn_socket;
+struct mn_socket_ops;
+struct mn_sock_cb;
+struct os_mbuf;
+
+struct mn_socket {
+    const union mn_socket_cb *ms_cbs;          /* filled in by user */
+    void *ms_cb_arg;                           /* filled in by user */
+    const struct mn_socket_ops *ms_ops;        /* filled in by mn_socket */
+};
+
+/*
+ * Callbacks. Socket callbacks are for sockets which exchange
+ * data. Listen callback is for TCP listen sockets.
+ */
+union mn_socket_cb {
+    struct {
+        void (*readable)(void *cb_arg, int err);
+        void (*writable)(void *cb_arg, int err);
+    } socket;
+    struct {
+        int (*newconn)(void *cb_arg, struct mn_socket *new);
+    } listen;
+};
+
+struct mn_sockaddr {
+    uint8_t msa_len;
+    uint8_t msa_family;
+    char    msa_data[2];
+};
+
+struct mn_in_addr {
+    uint32_t s_addr;
+};
+
+struct mn_sockaddr_in {
+    uint8_t msin_len;
+    uint8_t msin_family;
+    uint16_t msin_port;
+    struct mn_in_addr msin_addr;
+};
+
+struct mn_in6_addr {
+    uint8_t s_addr[16];
+};
+
+struct mn_sockaddr_in6 {
+    uint8_t msin6_len;
+    uint8_t msin6_family;
+    uint16_t msin6_port;
+    uint32_t msin6_flowinfo;
+    struct mn_in6_addr msin6_addr;
+    uint32_t msin6_scope_id;
+};
+
+extern const uint32_t nm_in6addr_any[4];
+
+/*
+ * Structure for multicast join/leave
+ */
+struct mn_mreq {
+    uint8_t mm_idx;			/* interface index */
+    uint8_t mm_family;			/* address family */
+    union {
+        struct mn_in_addr v4;
+        struct mn_in6_addr v6;
+    } mm_addr;
+};
+
+#define MN_SO_LEVEL                     0xfe
+
+#define MN_MCAST_JOIN_GROUP             1
+#define MN_MCAST_LEAVE_GROUP            2
+#define MN_MCAST_IF                     3
+
+/*
+ * Socket calls.
+ *
+ * mn_connect() for TCP is asynchronous. Once connection has been established,
+ * socket callback (*writable) will be called.
+ *
+ * mn_sendto() is asynchronous as well. If it fails due to buffer shortage,
+ * socket provider should call (*writable) when more data can be sent.
+ *
+ * mn_recvfrom() returns immediatelly if no data is available. If data arrives,
+ * the callback (*readable) will be called. Once that happens, owner of the
+ * socket should keep calling mn_recvfrom() until it has drained all the
+ * data from the socket.
+ *
+ * If remote end closes the socket, socket callback (*readable) will be
+ * called.
+ */
+int mn_socket(struct mn_socket **, uint8_t domain, uint8_t type, uint8_t proto);
+int mn_bind(struct mn_socket *, struct mn_sockaddr *);
+int mn_connect(struct mn_socket *, struct mn_sockaddr *);
+int mn_listen(struct mn_socket *, uint8_t qlen);
+
+int mn_recvfrom(struct mn_socket *, struct os_mbuf **,
+  struct mn_sockaddr *from);
+int mn_sendto(struct mn_socket *, struct os_mbuf *, struct mn_sockaddr *to);
+
+int mn_getsockopt(struct mn_socket *, uint8_t level, uint8_t optname,
+  void *optval);
+int mn_setsockopt(struct mn_socket *, uint8_t level, uint8_t optname,
+  void *optval);
+
+int mn_getsockname(struct mn_socket *, struct mn_sockaddr *);
+int mn_getpeername(struct mn_socket *, struct mn_sockaddr *);
+
+int mn_close(struct mn_socket *);
+
+#define mn_socket_set_cbs(sock, cb_arg, cbs)                            \
+    do {                                                                \
+        (sock)->ms_cbs = (cbs);                                         \
+        (sock)->ms_cb_arg = (cb_arg);                                   \
+    } while (0)
+
+/*
+ * Address conversion
+ */
+int mn_inet_pton(int af, const char *src, void *dst);
+const char *mn_inet_ntop(int af, const void *src, void *dst, int len);
+
+/*
+ * Info about interfaces.
+ */
+#define MN_ITF_NAME_MAX    8
+
+/*
+ * Interface flags
+ */
+#define MN_ITF_F_UP        1
+#define MN_ITF_F_MULTICAST 2
+
+struct mn_itf {
+    char mif_name[MN_ITF_NAME_MAX];
+    uint8_t mif_idx;
+    uint8_t mif_flags;
+};
+
+struct mn_itf_addr {
+    uint8_t mifa_family;
+    uint8_t mifa_plen;
+    union {
+        struct mn_in_addr v4;
+        struct mn_in6_addr v6;
+    } mifa_addr;
+};
+
+/*
+ * Iterate through interfaces, and their addresses
+ */
+int mn_itf_getnext(struct mn_itf *);
+int mn_itf_addr_getnext(struct mn_itf *, struct mn_itf_addr *);
+
+#endif /* __SYS_MN_SOCKET_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/net/ip/mn_socket/include/mn_socket/mn_socket_ops.h
----------------------------------------------------------------------
diff --git a/net/ip/mn_socket/include/mn_socket/mn_socket_ops.h b/net/ip/mn_socket/include/mn_socket/mn_socket_ops.h
new file mode 100644
index 0000000..39f11c1
--- /dev/null
+++ b/net/ip/mn_socket/include/mn_socket/mn_socket_ops.h
@@ -0,0 +1,85 @@
+/**
+ * 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 __SYS_MN_SOCKET_OPS_H_
+#define __SYS_MN_SOCKET_OPS_H_
+
+#include <inttypes.h>
+
+/*
+ * Interface for socket providers.
+ * - mso_create() creates a socket, memory allocation has to be done by
+ *   the socket provider.
+ * - mso_close() closes the socket, memory should be freed. User should not
+ *   be using the socket pointer once it has been closed.
+ */
+struct mn_socket_ops {
+    int (*mso_create)(struct mn_socket **, uint8_t domain, uint8_t type,
+      uint8_t protocol);
+    int (*mso_close)(struct mn_socket *);
+
+    int (*mso_bind)(struct mn_socket *, struct mn_sockaddr *);
+    int (*mso_connect)(struct mn_socket *, struct mn_sockaddr *);
+    int (*mso_listen)(struct mn_socket *, uint8_t qlen);
+
+    int (*mso_sendto)(struct mn_socket *, struct os_mbuf *,
+      struct mn_sockaddr *to);
+    int (*mso_recvfrom)(struct mn_socket *, struct os_mbuf **,
+      struct mn_sockaddr *from);
+
+    int (*mso_getsockopt)(struct mn_socket *, uint8_t level, uint8_t name,
+      void *val);
+    int (*mso_setsockopt)(struct mn_socket *, uint8_t level, uint8_t name,
+      void *val);
+
+    int (*mso_getsockname)(struct mn_socket *, struct mn_sockaddr *);
+    int (*mso_getpeername)(struct mn_socket *, struct mn_sockaddr *);
+
+    int (*mso_itf_getnext)(struct mn_itf *);
+    int (*mso_itf_addr_getnext)(struct mn_itf *, struct mn_itf_addr *);
+};
+
+int mn_socket_ops_reg(const struct mn_socket_ops *ops);
+
+static inline void
+mn_socket_writable(struct mn_socket *s, int error)
+{
+    if (s->ms_cbs && s->ms_cbs->socket.writable) {
+        s->ms_cbs->socket.writable(s->ms_cb_arg, error);
+    }
+}
+
+static inline void
+mn_socket_readable(struct mn_socket *s, int error)
+{
+    if (s->ms_cbs && s->ms_cbs->socket.readable) {
+        s->ms_cbs->socket.readable(s->ms_cb_arg, error);
+    }
+}
+
+static inline int
+mn_socket_newconn(struct mn_socket *s, struct mn_socket *new)
+{
+    if (s->ms_cbs && s->ms_cbs->listen.newconn) {
+        return s->ms_cbs->listen.newconn(s->ms_cb_arg, new);
+    } else {
+        return -1;
+    }
+}
+
+#endif /* __SYS_MN_SOCKET_OPS_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/net/ip/mn_socket/pkg.yml
----------------------------------------------------------------------
diff --git a/net/ip/mn_socket/pkg.yml b/net/ip/mn_socket/pkg.yml
new file mode 100644
index 0000000..ca4ee99
--- /dev/null
+++ b/net/ip/mn_socket/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: net/ip/mn_socket
+pkg.description: Socket interface for Mynewt.
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+    - socket
+    - IP
+
+pkg.deps:
+    - kernel/os
+    - libs/util

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/net/ip/mn_socket/src/arch/sim/native_itf.c
----------------------------------------------------------------------
diff --git a/net/ip/mn_socket/src/arch/sim/native_itf.c b/net/ip/mn_socket/src/arch/sim/native_itf.c
new file mode 100644
index 0000000..78607e7
--- /dev/null
+++ b/net/ip/mn_socket/src/arch/sim/native_itf.c
@@ -0,0 +1,212 @@
+/**
+ * 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 <ifaddrs.h>
+#include <net/if.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include "mn_socket/mn_socket.h"
+#include "native_sock_priv.h"
+
+static uint8_t
+itf_flags(int if_flags)
+{
+    uint8_t flags;
+
+    flags = 0;
+
+    if ((if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) {
+        flags |= MN_ITF_F_UP;
+    }
+    if (if_flags & IFF_MULTICAST) {
+        flags |= MN_ITF_F_MULTICAST;
+    }
+    return flags;
+}
+
+int
+native_sock_itf_getnext(struct mn_itf *mi)
+{
+    int prev_idx, cur_idx;
+    struct ifaddrs *ifap;
+    struct ifaddrs *ifa;
+    int rc;
+
+    if (mi->mif_name[0] == '\0') {
+        prev_idx = 0;
+    } else {
+        prev_idx = mi->mif_idx;
+    }
+    mi->mif_idx = UCHAR_MAX;
+    rc = getifaddrs(&ifap);
+    if (rc < 0) {
+        rc = native_sock_err_to_mn_err(errno);
+        return rc;
+    }
+
+    rc = MN_ENOBUFS;
+    for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+        cur_idx = if_nametoindex(ifa->ifa_name);
+        if (cur_idx <= prev_idx || cur_idx >= mi->mif_idx) {
+            continue;
+        }
+        strncpy(mi->mif_name, ifa->ifa_name, sizeof(mi->mif_name));
+        mi->mif_idx = cur_idx;
+        mi->mif_flags = itf_flags(ifa->ifa_flags);
+        rc = 0;
+    }
+    freeifaddrs(ifap);
+    return rc;
+}
+
+static int
+addrcmp(uint8_t fam1, void *addr1, uint8_t fam2, void *addr2, int alen)
+{
+    if (fam1 != fam2) {
+        return fam1 - fam2;
+    }
+    return memcmp(addr1, addr2, alen);
+}
+
+static int
+plen(void *addr, int alen)
+{
+    int i;
+    int j;
+    uint8_t b;
+
+    for (i = 0; i < alen; i++) {
+        b = ((uint8_t *)addr)[i];
+        if (b == 0xff) {
+            continue;
+        }
+        for (j = 0; j < 7; j++) {
+            if ((b & (0x80 >> j)) == 0) {
+                return i * 8 + j;
+            }
+        }
+    }
+    return alen * 8;
+}
+
+int
+native_sock_itf_addr(int idx, uint32_t *addr)
+{
+    struct ifaddrs *ifap;
+    struct ifaddrs *ifa;
+    struct sockaddr_in *sin;
+    int rc;
+
+    rc = getifaddrs(&ifap);
+    if (rc < 0) {
+        rc = native_sock_err_to_mn_err(errno);
+        return rc;
+    }
+
+    rc = MN_EADDRNOTAVAIL;
+    for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+        if (if_nametoindex(ifa->ifa_name) != idx) {
+            continue;
+        }
+        if (ifa->ifa_addr->sa_family == AF_INET) {
+            sin = (struct sockaddr_in *)ifa->ifa_addr;
+            *addr = sin->sin_addr.s_addr;
+            rc = 0;
+            break;
+        }
+    }
+    freeifaddrs(ifap);
+    return rc;
+}
+
+int
+native_sock_itf_addr_getnext(struct mn_itf *mi, struct mn_itf_addr *mia)
+{
+    struct ifaddrs *ifap;
+    struct ifaddrs *ifa;
+    struct sockaddr_in *sin;
+    struct sockaddr_in6 *sin6;
+    int rc;
+    uint8_t prev_family;
+    uint8_t prev_addr[16];
+
+    rc = getifaddrs(&ifap);
+    if (rc < 0) {
+        rc = native_sock_err_to_mn_err(errno);
+        return rc;
+    }
+
+    prev_family = mia->mifa_family;
+    memcpy(prev_addr, &mia->mifa_addr, sizeof(mia->mifa_addr));
+    mia->mifa_family = UCHAR_MAX;
+    memset(&mia->mifa_addr, 0xff, sizeof(mia->mifa_addr));
+
+    rc = MN_ENOBUFS;
+
+    for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+        if (if_nametoindex(ifa->ifa_name) != mi->mif_idx) {
+            continue;
+        }
+        if (ifa->ifa_addr->sa_family == AF_INET) {
+            sin = (struct sockaddr_in *)ifa->ifa_addr;
+            if (addrcmp(MN_AF_INET, &sin->sin_addr,
+                prev_family, prev_addr,
+                sizeof(struct in_addr)) <= 0) {
+                continue;
+            }
+            if (addrcmp(MN_AF_INET, &sin->sin_addr,
+                mia->mifa_family, &mia->mifa_addr,
+                sizeof(struct in_addr)) >= 0) {
+                continue;
+            }
+            mia->mifa_family = MN_AF_INET;
+            memcpy(&mia->mifa_addr, &sin->sin_addr, sizeof(struct in_addr));
+
+            sin = (struct sockaddr_in *)ifa->ifa_netmask;
+            mia->mifa_plen = plen(&sin->sin_addr, sizeof(struct in_addr));
+        } else if (ifa->ifa_addr->sa_family == AF_INET6) {
+            sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
+            if (addrcmp(MN_AF_INET6, &sin6->sin6_addr,
+                prev_family, prev_addr,
+                sizeof(struct in6_addr)) <= 0) {
+                continue;
+            }
+            if (addrcmp(MN_AF_INET6, &sin6->sin6_addr,
+                mia->mifa_family, &mia->mifa_addr,
+                sizeof(struct in6_addr)) >= 0) {
+                continue;
+            }
+            mia->mifa_family = MN_AF_INET6;
+            memcpy(&mia->mifa_addr, &sin6->sin6_addr, sizeof(struct in6_addr));
+
+            sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
+            mia->mifa_plen = plen(&sin6->sin6_addr, sizeof(struct in6_addr));
+        } else {
+            continue;
+        }
+        rc = 0;
+    }
+    freeifaddrs(ifap);
+    return rc;
+}
+
+