mkiiskila closed pull request #1350: helpers for exporting manufacturing time data via sys/confg URL: https://github.com/apache/mynewt-core/pull/1350
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/sys/config/conf_mmap/include/conf_mmap/conf_mmap.h b/sys/config/conf_mmap/include/conf_mmap/conf_mmap.h new file mode 100644 index 0000000000..89b536f5e5 --- /dev/null +++ b/sys/config/conf_mmap/include/conf_mmap/conf_mmap.h @@ -0,0 +1,90 @@ +/* + * 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 __CONF_MMAP_H +#define __CONF_MMAP_H + +#include <config/config.h> +#include <config/config_store.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Read-only config source from a memory mapped data region. + * + * Data is expected to be stored without keys. Config source registration + * provides key names to values when config source is registered. + * Like with other config values, data has to be a printable string. + * + * E.g. memory layout @ 0x10000 + * 0x10000: device_serial_number, 16 bytes, + * 0x10010: device_model, 6 bytes, + * 0x10006: device_hw_version, 8 bytes + * + * Config name to value mapping would be provided like this: + * static const struct conf_mmap_kv my_static_kvs[] = { + * [0] = { "id/serial", 0, 16 }, + * [1] = { "id/model", 16, 6 }, + * [2] = { "hw/ver", 22, 8 } + * }; + * + * Then declare the conf_mmap structure: + * static struct conf_mmap my_static_conf = { + * .cm_base = (uintptr_t)0x10000, + * .cm_kv_cnt = sizeof(my_static_kvs) / sizeof(my_static_kvs[0]), + * .cm_kv = my_static_kvs, + * }; + * + * and then register this, eg. before registering other config sources + * + * conf_mmap_src(&my_static_conf); + */ + +/** + * Config key value mapping declaration. + */ +struct conf_mmap_kv { + const char *cmk_key; /* key (string) */ + uint16_t cmk_off; /* offset of value from conf_mmap.cm_base */ + uint16_t cmk_maxlen; /* maximum length of value */ +}; + +struct conf_mmap { + struct conf_store cm_store; + uintptr_t cm_base; /* base address */ + int cm_kv_cnt; /* number of key/value array elements */ + const struct conf_mmap_kv *cm_kv; /* key/value array */ +}; + +/** + * Add memory mapped read-only data as a config source. + * + * @param cm Information about config data + * + * @return 0 on success, non-zero on failure. + */ +int conf_mmap_src(struct conf_mmap *cm); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/sys/config/conf_mmap/pkg.yml b/sys/config/conf_mmap/pkg.yml new file mode 100644 index 0000000000..5a2c4f510e --- /dev/null +++ b/sys/config/conf_mmap/pkg.yml @@ -0,0 +1,27 @@ +# +# 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: sys/config/conf_mmap +pkg.description: Memory mapped area for config values. +pkg.author: "Apache Mynewt <[email protected]>" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/sys/config" diff --git a/sys/config/conf_mmap/src/conf_mmap.c b/sys/config/conf_mmap/src/conf_mmap.c new file mode 100644 index 0000000000..5ab8a37c06 --- /dev/null +++ b/sys/config/conf_mmap/src/conf_mmap.c @@ -0,0 +1,67 @@ +/* + * 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 <config/config.h> +#include <config/config_store.h> + +#include "conf_mmap/conf_mmap.h" + +static int conf_mmap_load(struct conf_store *cs, conf_store_load_cb cb, + void *cb_arg); + +static struct conf_store_itf conf_mmap_itf = { + .csi_load = conf_mmap_load, +}; + +static int +conf_mmap_load(struct conf_store *cs, conf_store_load_cb cb, void *cb_arg) +{ + struct conf_mmap *cm = (struct conf_mmap *)cs; + const struct conf_mmap_kv *cmk; + char name[CONF_MAX_NAME_LEN + 1]; + char val[CONF_MAX_VAL_LEN + 1]; + int len; + int i; + + for (i = 0; i < cm->cm_kv_cnt; i++) { + cmk = &cm->cm_kv[i]; + + strncpy(name, cmk->cmk_key, sizeof(name) - 1); + name[sizeof(name) - 1] = '\0'; + len = cmk->cmk_maxlen; + if (len > CONF_MAX_VAL_LEN) { + len = CONF_MAX_VAL_LEN; + } + strncpy(val, (void *)(cm->cm_base + cmk->cmk_off), len); + cb(name, val, cb_arg); + } + return OS_OK; +} + +int +conf_mmap_src(struct conf_mmap *cm) +{ + /* XXX probably should check for magic number or something where + cm_base is */ + cm->cm_store.cs_itf = &conf_mmap_itf; + conf_src_register(&cm->cm_store); + + return OS_OK; +} diff --git a/sys/config/include/config/config.h b/sys/config/include/config/config.h index 063e111752..fc324c5ed3 100644 --- a/sys/config/include/config/config.h +++ b/sys/config/include/config/config.h @@ -330,15 +330,6 @@ char *conf_str_from_bytes(void *vp, int vp_len, char *buf, int buf_len); #define CONF_VALUE_SET(str, type, val) \ conf_value_from_str((str), (type), &(val), sizeof(val)) -/* - * Config storage - */ -struct conf_store_itf; -struct conf_store { - SLIST_ENTRY(conf_store) cs_next; - const struct conf_store_itf *cs_itf; -}; - #ifdef __cplusplus } #endif diff --git a/sys/config/include/config/config_fcb.h b/sys/config/include/config/config_fcb.h index a8074bd1a5..9c75fc9945 100644 --- a/sys/config/include/config/config_fcb.h +++ b/sys/config/include/config/config_fcb.h @@ -20,6 +20,7 @@ #define __SYS_CONFIG_FCB_H_ #include "config/config.h" +#include "config/config_store.h" #ifdef __cplusplus extern "C" { diff --git a/sys/config/include/config/config_file.h b/sys/config/include/config/config_file.h index ebf2977f78..b56c1f9df3 100644 --- a/sys/config/include/config/config_file.h +++ b/sys/config/include/config/config_file.h @@ -20,6 +20,7 @@ #define __SYS_CONFIG_FILE_H_ #include "config/config.h" +#include "config/config_store.h" #ifdef __cplusplus extern "C" { diff --git a/sys/config/include/config/config_store.h b/sys/config/include/config/config_store.h new file mode 100644 index 0000000000..0f53c5c37d --- /dev/null +++ b/sys/config/include/config/config_store.h @@ -0,0 +1,65 @@ +/* + * 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_CONFIG_STORE_H_ +#define __SYS_CONFIG_STORE_H_ + +/** + * @addtogroup SysConfig Configuration of Apache Mynewt System + * @{ + */ + +#include <os/queue.h> +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +struct conf_store; + +/* + * API for config storage. + */ +typedef void (*conf_store_load_cb)(char *name, char *val, void *cb_arg); +struct conf_store_itf { + int (*csi_load)(struct conf_store *cs, conf_store_load_cb cb, void *cb_arg); + int (*csi_save_start)(struct conf_store *cs); + int (*csi_save)(struct conf_store *cs, const char *name, const char *value); + int (*csi_save_end)(struct conf_store *cs); +}; + +struct conf_store { + SLIST_ENTRY(conf_store) cs_next; + const struct conf_store_itf *cs_itf; +}; + +void conf_src_register(struct conf_store *cs); +void conf_dst_register(struct conf_store *cs); + + +#ifdef __cplusplus +} +#endif + +/** + * @} SysConfig + */ + + +#endif /* __SYS_CONFIG_H_ */ diff --git a/sys/config/src/config_cli.c b/sys/config/src/config_cli.c index d24dd021a0..7c9755619e 100644 --- a/sys/config/src/config_cli.c +++ b/sys/config/src/config_cli.c @@ -21,6 +21,7 @@ #include "os/mynewt.h" #include "config/config.h" +#include "config/config_store.h" #include "config_priv.h" #if MYNEWT_VAL(CONFIG_CLI) diff --git a/sys/config/src/config_fcb.c b/sys/config/src/config_fcb.c index e34971b685..0612eac714 100644 --- a/sys/config/src/config_fcb.c +++ b/sys/config/src/config_fcb.c @@ -25,6 +25,7 @@ #include <string.h> #include "config/config.h" +#include "config/config_store.h" #include "config/config_fcb.h" #include "config/config_generic_kv.h" #include "config_priv.h" @@ -32,7 +33,7 @@ #define CONF_FCB_VERS 1 struct conf_fcb_load_cb_arg { - load_cb cb; + conf_store_load_cb cb; void *cb_arg; }; @@ -42,9 +43,10 @@ struct conf_kv_load_cb_arg { size_t len; }; -static int conf_fcb_load(struct conf_store *, load_cb cb, void *cb_arg); +static int conf_fcb_load(struct conf_store *, conf_store_load_cb cb, + void *cb_arg); static int conf_fcb_save(struct conf_store *, const char *name, - const char *value); + const char *value); static struct conf_store_itf conf_fcb_itf = { .csi_load = conf_fcb_load, @@ -128,7 +130,7 @@ conf_fcb_load_cb(struct fcb_entry *loc, void *arg) } static int -conf_fcb_load(struct conf_store *cs, load_cb cb, void *cb_arg) +conf_fcb_load(struct conf_store *cs, conf_store_load_cb cb, void *cb_arg) { struct conf_fcb *cf = (struct conf_fcb *)cs; struct conf_fcb_load_cb_arg arg; diff --git a/sys/config/src/config_file.c b/sys/config/src/config_file.c index 814a77f986..c722a760ad 100644 --- a/sys/config/src/config_file.c +++ b/sys/config/src/config_file.c @@ -30,7 +30,8 @@ #include "config/config_file.h" #include "config_priv.h" -static int conf_file_load(struct conf_store *, load_cb cb, void *cb_arg); +static int conf_file_load(struct conf_store *, conf_store_load_cb cb, + void *cb_arg); static int conf_file_save(struct conf_store *, const char *name, const char *value); @@ -104,7 +105,7 @@ conf_getnext_line(struct fs_file *file, char *buf, int blen, uint32_t *loc) * item found. */ static int -conf_file_load(struct conf_store *cs, load_cb cb, void *cb_arg) +conf_file_load(struct conf_store *cs, conf_store_load_cb cb, void *cb_arg) { struct conf_file *cf = (struct conf_file *)cs; struct fs_file *file; diff --git a/sys/config/src/config_priv.h b/sys/config/src/config_priv.h index f299d59205..8b98f5512b 100644 --- a/sys/config/src/config_priv.h +++ b/sys/config/src/config_priv.h @@ -40,20 +40,6 @@ int conf_line_make2(char *dst, int dlen, const char *name, const char *value); struct conf_handler *conf_parse_and_lookup(char *name, int *name_argc, char *name_argv[]); -/* - * API for config storage. - */ -typedef void (*load_cb)(char *name, char *val, void *cb_arg); -struct conf_store_itf { - int (*csi_load)(struct conf_store *cs, load_cb cb, void *cb_arg); - int (*csi_save_start)(struct conf_store *cs); - int (*csi_save)(struct conf_store *cs, const char *name, const char *value); - int (*csi_save_end)(struct conf_store *cs); -}; - -void conf_src_register(struct conf_store *cs); -void conf_dst_register(struct conf_store *cs); - SLIST_HEAD(conf_store_head, conf_store); extern struct conf_store_head conf_load_srcs; SLIST_HEAD(conf_handler_head, conf_handler); diff --git a/sys/config/src/config_store.c b/sys/config/src/config_store.c index fa21da1bbc..cbb882557d 100644 --- a/sys/config/src/config_store.c +++ b/sys/config/src/config_store.c @@ -22,6 +22,7 @@ #include "os/mynewt.h" #include "config/config.h" +#include "config/config_store.h" #include "config_priv.h" struct conf_dup_check_arg { diff --git a/sys/id/include/id/id.h b/sys/id/include/id/id.h index 8db3055dde..d730e93a80 100644 --- a/sys/id/include/id/id.h +++ b/sys/id/include/id/id.h @@ -24,11 +24,34 @@ extern "C" { #endif +#if MYNEWT_VAL(ID_SERIAL_PRESENT) /* - * Maximum configurable serial number. + * Maximum expected serial number string length. */ -#define ID_SERIAL_MAX_LEN 64 +#define ID_SERIAL_MAX_LEN MYNEWT_VAL(ID_SERIAL_MAX_LEN) extern char id_serial[]; +#endif + +#if MYNEWT_VAL(ID_MANUFACTURER_LOCAL) +/* + * Maximum expected manufacturer string length. + */ +#define ID_MANUFACTURER_MAX_LEN 64 +extern char id_manufacturer[]; +#else +extern const char id_manufacturer[]; +#endif + +#if MYNEWT_VAL(ID_MODEL_LOCAL) +/* + * Maximum expected model name string length. + */ +#define ID_MODEL_MAX_LEN 64 +extern char id_model[]; +#elif MYNEWT_VAL(ID_MODEL_PRESENT) +extern const char id_model[]; +#endif + extern char id_mfghash[]; extern const char *id_bsp_str; extern const char *id_app_str; diff --git a/sys/id/src/id.c b/sys/id/src/id.c index e738b390f6..c43c67dd8f 100644 --- a/sys/id/src/id.c +++ b/sys/id/src/id.c @@ -46,7 +46,15 @@ const char *id_bsp_str = ""; const char *id_app_str = ""; #endif +#if MYNEWT_VAL(ID_SERIAL_PRESENT) char id_serial[ID_SERIAL_MAX_LEN]; +#endif +#if MYNEWT_VAL(ID_MANUFACTURER_LOCAL) +char id_manufacturer[ID_MANUFACTURER_MAX_LEN]; +#endif +#if MYNEWT_VAL(ID_MODEL_LOCAL) +char id_model[ID_MODEL_MAX_LEN]; +#endif /** Base64-encoded null-terminated manufacturing hash. */ char id_mfghash[BASE64_ENCODE_SIZE(MFG_HASH_SZ) + 1]; @@ -74,8 +82,18 @@ id_conf_get(int argc, char **argv, char *val, int val_len_max) return (char *)id_bsp_str; } else if (!strcmp(argv[0], "app")) { return (char *)id_app_str; +#if MYNEWT_VAL(ID_SERIAL_PRESENT) } else if (!strcmp(argv[0], "serial")) { - return id_serial; + return (char *)id_serial; +#endif +#if MYNEWT_VAL(ID_MANUFACTURER_PRESENT) + } else if (!strcmp(argv[0], "mfger")) { + return (char *)id_manufacturer; +#endif +#if MYNEWT_VAL(ID_MODEL_PRESENT) + } else if (!strcmp(argv[0], "model")) { + return (char *)id_model; +#endif } else if (!strcmp(argv[0], "mfghash")) { return id_mfghash; } @@ -87,9 +105,21 @@ static int id_conf_set(int argc, char **argv, char *val) { if (argc == 1) { +#if MYNEWT_VAL(ID_SERIAL_PRESENT) if (!strcmp(argv[0], "serial")) { return CONF_VALUE_SET(val, CONF_STRING, id_serial); } +#endif +#if MYNEWT_VAL(ID_MANUFACTURER_LOCAL) + if (!strcmp(argv[0], "mfger")) { + return CONF_VALUE_SET(val, CONF_STRING, id_manufacturer); + } +#endif +#if MYNEWT_VAL(ID_MODEL_LOCAL) + if (!strcmp(argv[0], "model")) { + return CONF_VALUE_SET(val, CONF_STRING, id_model); + } +#endif } return OS_ENOENT; } @@ -112,8 +142,27 @@ id_conf_export(void (*export_func)(char *name, char *val), export_func("id/app", (char *)id_app_str); export_func("id/mfghash", (char *)id_mfghash); } +#if MYNEWT_VAL(ID_SERIAL_PRESENT) export_func("id/serial", id_serial); - +#endif /* ID_SERIAL_PRESENT */ +#if MYNEWT_VAL(ID_MANUFACTURER_PRESENT) +#if MYNEWT_VAL(ID_MANUFACTURER_LOCAL) + export_func("id/mfger", id_manufacturer); +#else + if (tgt == CONF_EXPORT_SHOW) { + export_func("id/mfger", (char *)id_manufacturer); + } +#endif /* ID_MANUFACTURER_LOCAL */ +#endif /* ID_MANUFACTURER_PRESENT */ +#if MYNEWT_VAL(ID_MODEL_PRESENT) +#if MYNEWT_VAL(ID_MODEL_LOCAL) + export_func("id/model", id_model); +#else + if (tgt == CONF_EXPORT_SHOW) { + export_func("id/model", (char *)id_model); + } +#endif /* ID_MODEL_LOCAL */ +#endif /* ID_MODEL_PRESENT */ return 0; } diff --git a/sys/id/syscfg.yml b/sys/id/syscfg.yml new file mode 100644 index 0000000000..d81acea4bc --- /dev/null +++ b/sys/id/syscfg.yml @@ -0,0 +1,49 @@ +# 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. +# + +# Package: sys/id + +syscfg.defs: + ID_SERIAL_PRESENT: + description: 'Device serial number exported as sys/id/serial.' + value: 1 + ID_SERIAL_MAX_LEN: + description: Maximum length of id/serial value + value: 64 + + ID_MANUFACTURER_PRESENT: + description: 'Device manufacturer string exported as sys/id/mfger.' + value: 0 + ID_MANUFACTURER_LOCAL: + description: > + If set, device manufacturer stored in sys/config. Otherwise + must be exported by some other package. + value: 0 + restrictions: + - 'ID_MANUFACTURER_PRESENT' + + ID_MODEL_PRESENT: + description: 'Device model name string exported as sys/id/model.' + value: 0 + ID_MODEL_LOCAL: + description: > + If set, device model name stored in sys/config. Otherwise + must be exported by some other package. + value: 0 + restrictions: + - 'ID_MODEL_PRESENT' ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
