From 0abba449b49e2021ea1d5f83723146f177bf6b1d Mon Sep 17 00:00:00 2001 From: Marko Kiiskila Date: Mon, 27 Jun 2016 15:47:22 -0700 Subject: sys/id; package which exports hw id, bsp name, app name and configurable serial number via config interface. --- sys/id/include/id/id.h | 33 ++++++++++++++ sys/id/pkg.yml | 38 ++++++++++++++++ sys/id/src/id.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 sys/id/include/id/id.h create mode 100644 sys/id/pkg.yml create mode 100644 sys/id/src/id.c (limited to 'sys') diff --git a/sys/id/include/id/id.h b/sys/id/include/id/id.h new file mode 100644 index 00000000..228d3247 --- /dev/null +++ b/sys/id/include/id/id.h @@ -0,0 +1,33 @@ +/** + * 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_ID_H__ +#define __SYS_ID_H__ + +/* + * Maximum configurable serial number. + */ +#define ID_SERIAL_MAX_LEN 64 + +/* + * Initialize manufacturing info storage/reporting. + */ +int id_init(void); + +#endif diff --git a/sys/id/pkg.yml b/sys/id/pkg.yml new file mode 100644 index 00000000..c76cf14a --- /dev/null +++ b/sys/id/pkg.yml @@ -0,0 +1,38 @@ +# +# 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/id +pkg.description: Library providing system ID services. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - serial + - manufacturing + - identifier + +pkg.deps: + - hw/hal + - libs/os + - libs/util + - sys/config +pkg.deps.SHELL: + - libs/shell +pkg.req_apis.SHELL: + - console +pkg.cflags.SHELL: -DSHELL_PRESENT diff --git a/sys/id/src/id.c b/sys/id/src/id.c new file mode 100644 index 00000000..3d2acc3a --- /dev/null +++ b/sys/id/src/id.c @@ -0,0 +1,115 @@ +/** + * 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 +#include +#include + +#include +#include +#include +#include + +#include "id/id.h" + +static char *id_conf_get(int argc, char **argv, char *val, int val_len_max); +static int id_conf_set(int argc, char **argv, char *val); +static int id_conf_export(void (*export_func)(char *name, char *val), + enum conf_export_tgt tgt); + +#define STAMP_STR(s) STAMP_STR1(s) +#define STAMP_STR1(str) #str + +#ifdef BSP_NAME +static const char *bsp_str = STAMP_STR(BSP_NAME); +static const char *app_str = STAMP_STR(APP_NAME); +#else +static const char *bsp_str = ""; +static const char *app_str = ""; +#endif + +static char serial[ID_SERIAL_MAX_LEN]; + +struct conf_handler id_conf = { + .ch_name = "id", + .ch_get = id_conf_get, + .ch_set = id_conf_set, + .ch_export = id_conf_export +}; + +static char * +id_conf_get(int argc, char **argv, char *val, int val_len_max) +{ + uint8_t src_buf[BSP_MAX_ID_LEN]; + int len; + + if (argc == 1) { + if (!strcmp(argv[0], "hwid")) { + len = bsp_hw_id(src_buf, sizeof(src_buf)); + if (len > 0) { + return conf_str_from_bytes(src_buf, len, val, val_len_max); + } + } else if (!strcmp(argv[0], "bsp")) { + return (char *)bsp_str; + } else if (!strcmp(argv[0], "app")) { + return (char *)app_str; + } else if (!strcmp(argv[0], "serial")) { + return serial; + } + } + return NULL; +} + +static int +id_conf_set(int argc, char **argv, char *val) +{ + if (argc == 1) { + if (!strcmp(argv[0], "serial")) { + return CONF_VALUE_SET(val, CONF_STRING, serial); + } + } + return OS_ENOENT; +} + +static int +id_conf_export(void (*export_func)(char *name, char *val), + enum conf_export_tgt tgt) +{ + uint8_t src_buf[BSP_MAX_ID_LEN]; + char str[sizeof(src_buf) * 2]; + int len; + + if (tgt == CONF_EXPORT_SHOW) { + len = bsp_hw_id(src_buf, sizeof(src_buf)); + if (len > 0) { + conf_str_from_bytes(src_buf, len, str, sizeof(str)); + } + export_func("id/hwid", str); + export_func("id/bsp", (char *)bsp_str); + export_func("id/app", (char *)app_str); + } + export_func("id/serial", serial); + + return 0; +} + +int +id_init(void) +{ + return conf_register(&id_conf); +} -- cgit v1.2.3