Skip to content

Commit

Permalink
[set_model]:export api for sim
Browse files Browse the repository at this point in the history
[Desc]:
export operation :
1. write info
2. get info
3. reset info
4. set default info
add CMake secripts for set_model

Signed-off-by: pengyinjie <[email protected]>
  • Loading branch information
XMPengYinjie committed Dec 26, 2024
1 parent 35aa750 commit 81b6441
Show file tree
Hide file tree
Showing 9 changed files with 597 additions and 204 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@
nuttx_add_subdirectory()

nuttx_generate_kconfig(MENUDESC "Security")

# ##############################################################################
# Include Directory
# ##############################################################################

set(EXPORT_INCDIR ${CMAKE_CURRENT_LIST_DIR}/include)

# ##############################################################################
# Global FLAG
# ##############################################################################

if(TARGET nuttx)
set_property(
TARGET nuttx
APPEND
PROPERTY NUTTX_INCLUDE_DIRECTORIES ${EXPORT_INCDIR})
endif(TARGET nuttx)
145 changes: 145 additions & 0 deletions include/set_model_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright (C) 2022-2024 Xiaomi Corporation
*
* Licensed 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 SET_MODEL_API_H_
#define SET_MODEL_API_H_

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Writes product model information and CRC to a PSM (Product Specific Model) file.
*
* This function prepares a file path to store the product model information and its CRC.
* It first checks if the directory specified by PSM_PATH exists, and creates it if not.
* Then, it opens (or creates) a file named 'ot_config.psm_model' within this directory
* for writing. The product model (PRODUCT_MODEL macro) is written to this file, followed
* by its CRC (computed using kvs_crc8). Additionally, it sets several system properties
* related to the product, including market name, model, Bluetooth name and PID, MiIO model,
* and system app ID.
*
* @return 0 on success, -1 if an error occurs during directory creation, file opening,
* writing to file, or setting system properties.
*
* @note PRODUCT_MODEL, PSM_PATH, and other PRODUCT_* macros must be defined prior to
* calling this function.
*/
int psm_info_write(void);

/**
* @brief Clears the content of the device information file.
*
* This function first checks the status of the directory where the device information file
* resides (typically /data/etc). If the directory status check fails, an error message is
* logged and the function returns with an error code.
*
* If the directory status check is successful, the function proceeds to open the device
* information file (specified by DEVICE_INFO_PATH) in write-only mode. If the file cannot
* be opened for writing, an error message is logged and the function returns with an error
* code.
*
* Once the file is successfully opened, the content of the file is cleared by truncating
* it to zero length. If truncation fails, an error message is logged, the file is closed,
* and the function returns with an error code.
*
* Upon successful truncation, the file is closed, and a success message is logged.
*
* @return 0 on success, -1 on failure.
*
* @note This function assumes that DEVICE_INFO_PATH is defined and points to a valid
* path to the device information file.
*/
int device_info_clear(void);

/**
* @brief Retrieves the value associated with a given key from the device information file.
*
* This function first checks the status of the directory where the device information
* file resides. If the check fails, an error is logged and the function returns with
* an error code.
*
* It then calls the is_key_exist() function to search for the given key in the device
* information file. If the key is not found, an error message is logged and the
* function returns with an error code.
*
* If the key is found, the function returns successfully without any further action
* since the value (if needed) is retrieved and copied by the is_key_exist() function.
*
* @param key The key for which to retrieve the value.
* @param value A pointer to a character array where the retrieved value will be stored.
* This parameter is ignored by this function but is used by is_key_exist().
* @return 0 on success, -1 on failure.
*
* @note This function assumes that DEVICE_INFO_PATH is defined and points to a valid
* path to the device information file.
*/
int device_info_get(const char* key, char* value);

/**
* @brief Sets the value for a given key in the device information file.
*
* This function first checks the status of the directory where the device information file
* is located (usually /data/etc). If the directory status check fails, an error is logged
* and the function returns with an error code.
*
* If the key already exists in the device information file, as determined by calling
* is_key_exist(), an informational message is logged indicating that the key already
* exists and the function returns successfully without making any changes.
*
* If the key does not exist, the function proceeds to open the device information file
* (specified by DEVICE_INFO_PATH) in append mode with read/write capabilities ("a+").
* If the file cannot be opened, an error is logged and the function returns with an error
* code.
*
* Using fprintf(), the function attempts to write the key and its corresponding value
* to the file in the format "key=\"value\"\n". If the write operation fails, an error
* is logged, the file is closed, and the function returns with an error code.
*
* Upon successful write, the file is closed and the function returns successfully.
*
* @param key The key for which to set the value.
* @param value The value to set for the given key.
* @return 0 on success, -1 on failure.
*
* @note This function assumes that DEVICE_INFO_PATH is defined and points to a valid
* path to the device information file. It also relies on the implementation of
* check_dir_status() and is_key_exist() functions to perform their respective
* tasks correctly.
*/
int device_info_set(const char* key, const char* value);

/**
* @brief Sets the default device information.
*
* This function iterates through a predefined list of device information key-value pairs
* and sets each pair using the device_info_set function. The device information includes
* serial number (sn), WiFi MAC address (mac_wifi), Bluetooth MAC address (mac_bt),
* MiIO device ID (miio_did), MiIO device key (miio_key), color ID (color_id), and color
* description (color_desc).
*
* @return 0 on success, -1 if any of the device information settings fail.
*
* @note The default values for each key are defined as macros (e.g., DEFAULT_SN_VALUE)
* and should be defined prior to calling this function.
*/
int default_device_info_set(void);

#ifdef __cplusplus
}
#endif

#endif
23 changes: 23 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ##############################################################################
# frameworks/security/tools/CMakeList.txt
#
# 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.
#
# ##############################################################################

nuttx_add_subdirectory()

nuttx_generate_kconfig(MENUDESC "Security tools")
49 changes: 49 additions & 0 deletions tools/set_model/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ##############################################################################
# frameworks/security/tools/set_model/CMakeLists.txt
#
# 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.
#
# ##############################################################################

# ##############################################################################
# Source files
# ##############################################################################

set(CSRCS)

if(CONFIG_SC_SET_MODEL_TOOL)
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/set_model.c)
endif(CONFIG_SC_SET_MODEL_TOOL)

if(CONFIG_SC_SET_MODEL_API)
list(APPEND CSRCS ${CMAKE_CURRENT_LIST_DIR}/set_model_api.c)
endif(CONFIG_SC_SET_MODEL_API)

# ##############################################################################
# Application add
# ##############################################################################

if(CONFIG_SC_SET_MODEL_TOOL)
nuttx_add_application(
NAME
${CONFIG_SC_SET_MODEL_PROGNAME}
SRCS
${CSRCS}
STACKSIZE
${CONFIG_SC_SET_MODEL_STACKSIZE}
PRIORITY
${CONFIG_SET_MODEL_PRIORITY})
endif(CONFIG_SC_SET_MODEL_TOOL)
46 changes: 42 additions & 4 deletions tools/set_model/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
#
############################################################################

config SC_SET_MODEL
bool "enable set_model security tool"
config SC_SET_MODEL_API
bool "enable set_model security api"
default n
---help---
This config enable the set_model security tool, which is
This config enable the set_model security api, which is
used to get the device infomation (SN, Mac etc..) from the
storage device/file system and set to kvdb.

if SC_SET_MODEL
if SC_SET_MODEL_API

config SC_SET_MODEL_TOOL
bool "use security set model tool"
default n
---help---
"use security set model tool"

config SC_SET_MODEL_MIIO_PSM_PATH
string "the data path"
Expand Down Expand Up @@ -54,6 +60,38 @@ config SC_SET_MODEL_PRODUCT_HARDWARE
string "hardware name"
default "X4P"

config SC_SET_MODEL_DEFAULT_SN_VALUE
string "sn value, its length shall be 15"
default "55118/F3Z800841"

config SC_SET_MODEL_DEFAULT_MAC_WIFI_VALUE
string "mac_wifi value, its length shall be 17"
default "42:43:44:45:46:47"

config SC_SET_MODEL_DEFAULT_MAC_BT_VALUE
string "mac_bt value, its length shall be 17"
default "42:43:44:45:46:47"

config SC_SET_MODEL_DEFAULT_MIIO_DID_VALUE
string "miio_did value, its length shall be 9"
default"000000001"

config SC_SET_MODEL_DEFAULT_MIIO_KEY_VALUE
string "miio_key value, its length shall be 16"
default "0000000000000001"

config SC_SET_MODEL_DEFAULT_COLOR_ID_VALUE
string "color_id value, its length shall be 1"
default "0"

config SC_SET_MODEL_DEFAULT_COLOR_DESC_VALUE
string "color_desc value, its length shall be 15"
default "000000000000000"

config SC_SET_MODEL_DEVICE_INFO_PATH
string "device info path"
default "/data/etc/device_info.txt"

config SC_SET_MODEL_PROGNAME
string "Program name"
default "set_model"
Expand Down
2 changes: 1 addition & 1 deletion tools/set_model/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
#
############################################################################

ifneq ($(CONFIG_SC_SET_MODEL),)
ifneq ($(CONFIG_SC_SET_MODEL_API),)
CONFIGURED_APPS += $(APPDIR)/frameworks/security/tools/set_model
endif
7 changes: 6 additions & 1 deletion tools/set_model/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@

include $(APPDIR)/Make.defs

ifneq ($(CONFIG_SC_SET_MODEL_API),)
CSRCS += set_model_api.c
ifneq ($(CONFIG_SC_SET_MODEL_TOOL),)
PROGNAME = ${CONFIG_SC_SET_MODEL_PROGNAME}
PRIORITY = ${CONFIG_SC_SET_MODEL_PRIORITY}
STACKSIZE = ${CONFIG_SC_SET_MODEL_STACKSIZE}
MODULE = ${CONFIG_SC_SET_MODEL}
MODULE = ${CONFIG_SC_SET_MODEL_TOOL}
MAINSRC = set_model.c
endif
endif

include $(APPDIR)/Application.mk
Loading

0 comments on commit 81b6441

Please sign in to comment.