This is an automated email from the ASF dual-hosted git repository. ccollins pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 15d991048f905d7c4595cdd29bdd3189f923b675 Author: Christopher Collins <ccoll...@apache.org> AuthorDate: Fri May 10 10:04:19 2019 -0700 util/streamer: generic data streaming interface This package defines a generic data streaming interface. Supported operations are: * write flat buf * write printf-formatted text There are two built-in implementations: * console * mbuf chain --- util/streamer/include/streamer/streamer.h | 108 ++++++++++++++++++++++++++++++ util/streamer/pkg.yml | 28 ++++++++ util/streamer/src/streamer.c | 45 +++++++++++++ util/streamer/src/streamer_console.c | 50 ++++++++++++++ util/streamer/src/streamer_mbuf.c | 99 +++++++++++++++++++++++++++ util/streamer/syscfg.yml | 23 +++++++ 6 files changed, 353 insertions(+) diff --git a/util/streamer/include/streamer/streamer.h b/util/streamer/include/streamer/streamer.h new file mode 100644 index 0000000..87ea802 --- /dev/null +++ b/util/streamer/include/streamer/streamer.h @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_STREAMER_ +#define H_STREAMER_ + +#include "os/mynewt.h" + +struct streamer; + +typedef int streamer_write_fn(struct streamer *streamer, + const void *src, size_t len); + +typedef int streamer_vprintf_fn(struct streamer *streamer, + const char *fmt, va_list ap); + +struct streamer_cfg { + streamer_write_fn *write_cb; + streamer_vprintf_fn *vprintf_cb; +}; + +/** + * @brief Provides a generic data streaming interface. + */ +struct streamer { + const struct streamer_cfg *cfg; +}; + +/** + * @brief Streams data to an mbuf chain. + */ +struct streamer_mbuf { + struct streamer streamer; /* Must be first member. */ + struct os_mbuf *om; +}; + +/** + * @brief Writes the given flat buffer to a streamer. + * + * @param streamer The streamer to write to. + * @param src The flat buffer to write. + * @param len The number of bytes to write. + * + * @return 0 on success; SYS_E[...] on failure. + */ +int streamer_write(struct streamer *streamer, const void *src, size_t len); + +/** + * @brief Writes printf-formatted text to a streamer. + * + * A null-terminator does *not* get written. + * + * @param streamer The streamer to write to. + * @param src The flat buffer to write. + * @param len The number of bytes to write. + * + * @return Number of bytes written on success; + * SYS_E[...] on failure. + */ +int streamer_vprintf(struct streamer *streamer, const char *fmt, va_list ap); + +/** + * @brief Writes printf-formatted text to a streamer. + */ +int streamer_printf(struct streamer *streamer, const char *fmt, ...); + +/** + * @brief Acquires the singleton console streamer. + */ +struct streamer *streamer_console_get(void); + +/** + * Constructs an mbuf streamer. + * + * @param sm The mbuf streamer object to populate. + * @param om The mbuf chain to write to. This may already + * contain data. + * + * @return 0 on success; SYS_E[...] on failure. + */ +int streamer_mbuf_new(struct streamer_mbuf *sm, struct os_mbuf *om); + +/** + * Constructs an mbuf streamer that uses mbufs acquired from msys. + * + * @param sm The mbuf streamer object to populate. + * + * @return 0 on success; SYS_E[...] on failure. + */ +int streamer_msys_new(struct streamer_mbuf *sm); + +#endif diff --git a/util/streamer/pkg.yml b/util/streamer/pkg.yml new file mode 100644 index 0000000..77cf34a --- /dev/null +++ b/util/streamer/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/streamer +pkg.description: "Generic streaming utility" +pkg.author: "Apache Mynewt <d...@mynewt.apache.org>" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/kernel/os" + +pkg.req_apis: + - console diff --git a/util/streamer/src/streamer.c b/util/streamer/src/streamer.c new file mode 100644 index 0000000..a33072b --- /dev/null +++ b/util/streamer/src/streamer.c @@ -0,0 +1,45 @@ +/* + * 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 "streamer/streamer.h" + +int +streamer_write(struct streamer *streamer, const void *src, size_t len) +{ + return streamer->cfg->write_cb(streamer, src, len); +} + +int +streamer_vprintf(struct streamer *streamer, const char *fmt, va_list ap) +{ + return streamer->cfg->vprintf_cb(streamer, fmt, ap); +} + +int +streamer_printf(struct streamer *streamer, const char *fmt, ...) +{ + va_list ap; + int rc; + + va_start(ap, fmt); + rc = streamer_vprintf(streamer, fmt, ap); + va_end(ap); + + return rc; +} diff --git a/util/streamer/src/streamer_console.c b/util/streamer/src/streamer_console.c new file mode 100644 index 0000000..5a3b24a --- /dev/null +++ b/util/streamer/src/streamer_console.c @@ -0,0 +1,50 @@ +/* + * 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 "console/console.h" +#include "streamer/streamer.h" + +static int +streamer_console_write(struct streamer *streamer, const void *src, size_t len) +{ + console_write(src, len); + return 0; +} + +static int +streamer_console_vprintf(struct streamer *streamer, + const char *fmt, va_list ap) +{ + return console_vprintf(fmt, ap); +} + +static const struct streamer_cfg streamer_cfg_console = { + .write_cb = streamer_console_write, + .vprintf_cb = streamer_console_vprintf, +}; + +static struct streamer streamer_console = { + .cfg = &streamer_cfg_console, +}; + +struct streamer * +streamer_console_get(void) +{ + return &streamer_console; +} diff --git a/util/streamer/src/streamer_mbuf.c b/util/streamer/src/streamer_mbuf.c new file mode 100644 index 0000000..800527e --- /dev/null +++ b/util/streamer/src/streamer_mbuf.c @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "os/mynewt.h" +#include "streamer/streamer.h" + +static int +streamer_mbuf_write(struct streamer *streamer, const void *src, size_t len) +{ + struct streamer_mbuf *sm; + int rc; + + if (len > UINT16_MAX) { + return SYS_EINVAL; + } + + sm = (struct streamer_mbuf *)streamer; + rc = os_mbuf_append(sm->om, src, len); + if (rc != 0) { + return os_error_to_sys(rc); + } + + return 0; +} + +static int +streamer_mbuf_vprintf(struct streamer *streamer, const char *fmt, va_list ap) +{ + char buf[MYNEWT_VAL(STREAMER_MBUF_PRINTF_MAX)]; + int num_chars; + int rc; + + num_chars = vsnprintf(buf, sizeof buf, fmt, ap); + if (num_chars > sizeof buf - 1) { + num_chars = sizeof buf - 1; + } + + rc = streamer_mbuf_write(streamer, buf, num_chars); + if (rc != 0) { + return rc; + } + + return num_chars; +} + +static const struct streamer_cfg streamer_cfg_mbuf = { + .write_cb = streamer_mbuf_write, + .vprintf_cb = streamer_mbuf_vprintf, +}; + +int +streamer_mbuf_new(struct streamer_mbuf *sm, struct os_mbuf *om) +{ + if (sm == NULL || om == NULL) { + return SYS_EINVAL; + } + + *sm = (struct streamer_mbuf) { + .streamer.cfg = &streamer_cfg_mbuf, + .om = om, + }; + + return 0; +} + +int +streamer_msys_new(struct streamer_mbuf *sm) +{ + struct os_mbuf *om; + int rc; + + om = os_msys_get_pkthdr(0, 0); + if (om == NULL) { + return SYS_ENOMEM; + } + + rc = streamer_mbuf_new(sm, om); + if (rc != 0) { + return rc; + } + + return 0; +} diff --git a/util/streamer/syscfg.yml b/util/streamer/syscfg.yml new file mode 100644 index 0000000..36e0edb --- /dev/null +++ b/util/streamer/syscfg.yml @@ -0,0 +1,23 @@ +# 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. + +syscfg.defs: + STREAMER_MBUF_PRINTF_MAX: + description: > + Maximum number of characters that can be streamed to an mbuf in a + single printf call. + value: 128