feat(cube: create_nicsensor) add parse board feature

This commit is contained in:
leimingsheng 2025-03-28 10:40:05 +08:00
parent 7997c17932
commit c5e1b16cfc

@ -2,6 +2,8 @@ from datetime import datetime
import os
import json
cube_version="0.1"
def get_content_before_substring(a, b):
index = a.find(b)
if index != -1:
@ -33,13 +35,43 @@ dbg_print("info", f"hw_topo_path={hw_topo_path}")
dbg_print("info", f"output_path={output_path}")
def init_output_file():
header_content = f"""#!/bin/sh
# This Script is Genreated by nicsensor cube project
SCRIPT_VERSION="Generate by Cube {cube_version}"
"""
param_context = """
nic_slot=$1
sensor_type=$2
chip_slave=$3
option_data=$4
option_data2=$5
INFO="Info"
WARNING="Warning"
ERROR="Error"
i2c_bus=12
pca9641_slave=0x41
pca9548_channel=0x04
pca9548_slave=0x72
log="/tmp/nicsensor_debug.log"
fru_file_name=$option_data2
fru_write_size=0
fru_write_data=""
nic_type=""
slot_number=""
res_tmp468=""
"""
dbg_print("info", "====== Init output file ======")
# if file exist , remove it
# create output file
try:
with open(output_path, 'w', encoding='utf-8') as file:
content = "#!/bin/sh"
file.write(content)
file.write(header_content)
file.write(param_context)
except Exception as e:
print(f"写入文件时出现错误: {e}")
@ -109,8 +141,232 @@ def process_hw_topo():
else:
dbg_print("info", f"{channel} is null")
def load_adc128_feature(data):
dbg_print("info", ">>> Start load adc128 feature ...")
ch0_name = data['ch0_name']
ch1_name = data['ch1_name']
ch2_name = data['ch2_name']
ch3_name = data['ch3_name']
ch4_name = data['ch4_name']
ch5_name = data['ch5_name']
ch6_name = data['ch6_name']
ch7_name = data['ch7_name']
ch0_factor = data['ch0_factor']
ch1_factor = data['ch1_factor']
ch2_factor = data['ch2_factor']
ch3_factor = data['ch3_factor']
ch4_factor = data['ch4_factor']
ch5_factor = data['ch5_factor']
ch6_factor = data['ch6_factor']
ch7_factor = data['ch7_factor']
adc128_channel_name_context = f"""
ADC128_Channel0_name="{ch0_name}"
ADC128_Channel1_name="{ch1_name}"
ADC128_Channel2_name="{ch2_name}"
ADC128_Channel3_name="{ch3_name}"
ADC128_Channel4_name="{ch4_name}"
ADC128_Channel5_name="{ch5_name}"
ADC128_Channel6_name="{ch6_name}"
ADC128_Channel7_name="{ch7_name}"
"""
adc128_channel_factor_context = f"""
votage_division_factor_0="{ch0_factor}"
votage_division_factor_1="{ch1_factor}"
votage_division_factor_2="{ch2_factor}"
votage_division_factor_3="{ch3_factor}"
votage_division_factor_4="{ch4_factor}"
votage_division_factor_5="{ch5_factor}"
votage_division_factor_6="{ch6_factor}"
votage_division_factor_7="{ch7_factor}"
"""
adc128_REG_info_context = """
REG_adc128_config="0x00"
REG_adc128_advance="0x0b"
REG_adc128_status="0x0c"
REG_adc128_ch0="0x20"
REG_adc128_ch1="0x21"
REG_adc128_ch2="0x22"
REG_adc128_ch3="0x23"
REG_adc128_ch4="0x24"
REG_adc128_ch5="0x25"
REG_adc128_ch6="0x26"
REG_adc128_ch7="0x27"
"""
try:
with open (output_path, 'a+', encoding='utf-8') as file:
file.write(adc128_channel_name_context)
file.write(adc128_channel_factor_context)
file.write(adc128_REG_info_context)
dbg_print("info", "Success to load adc128 feature!")
except Exception as e:
dbg_print("Error", f"An error ouccuer when load adc128 feature")
def load_ina3221_feature(data):
dbg_print("info", ">>> Start load ina3221 feature ...")
ch0_name = data['ch0_name']
ch1_name = data['ch1_name']
ch2_name = data['ch2_name']
ch0_shunt = data['ch0_shunt']
ch1_shunt = data['ch1_shunt']
ch2_shunt = data['ch2_shunt']
ina3221_channel_name_context = f"""
INA3221_Channel0_name="{ch0_name}"
INA3221_Channel1_name="{ch1_name}"
INA3221_Channel2_name="{ch2_name}"
"""
ina3221_channel_shunt_context = f"""
shunt_resistor_0="{ch0_shunt}"
shunt_resistor_1="{ch1_shunt}"
shunt_resistor_2="{ch2_shunt}"
"""
ina3221_REG_info_context = """
REG_ina3221_ch1="0x01"
REG_ina3221_ch2="0x03"
REG_ina3221_ch3="0x05"
REG_ina3221_bus1="0x02"
REG_ina3221_bus2="0x04"
REG_ina3221_bus3="0x06"
ina3221_ch0_volt="0"
ina3221_ch1_volt="0"
ina3221_ch2_volt="0"
ina3221_ch0_current="0"
ina3221_ch1_current="0"
ina3221_ch2_current="0"
INA3221_SHUNT_VOLT=0
INA3221_BUS_VOLT=1
INA3221_POWER=2
"""
try:
with open (output_path, 'a+', encoding='utf-8') as file:
file.write(ina3221_channel_name_context)
file.write(ina3221_channel_shunt_context)
file.write(ina3221_REG_info_context)
dbg_print("info", "Success to load ina3221 feature!")
except Exception as e:
dbg_print("Error", f"An error ouccuer when load ina3221 feature")
def load_emc1413_feature(data):
dbg_print("info", ">>> Start load emc1413 feature ...")
def load_tmp468_feature(data):
dbg_print("info", ">>> Start load tmp468 feature ...")
def load_tmp112_feature(data):
dbg_print("info", ">>> Start load tmp112 feature ...")
def load_fru_feature(data):
dbg_print("info", ">>> Start load fru feature ...")
def load_chip_feature(data):
dbg_print("info", ">>> Start load chip feature ...")
def process_nic_feature():
dbg_print("info", "====== Process Nic Board Feature ======")
try:
# 打开JSON文件
with open(board_feature_path, 'r') as file:
# 读取并解析JSON数据
nic_feature_data = json.load(file)
# 处理解析后的数据
except FileNotFoundError:
dbg_print("Error", "错误: 未找到JSON文件。")
except json.JSONDecodeError:
dbg_print("Error", "错误: JSON文件格式有误。")
except Exception as e:
dbg_print("Error", f"发生未知错误:{e}")
adc128_data = nic_feature_data['adc128']
ina3221_data = nic_feature_data['ina3221']
emc1413_data = nic_feature_data['emc1413']
tmp468_data = nic_feature_data['tmp468']
tmp112_data = nic_feature_data['tmp112']
fru_data = nic_feature_data['fru']
chip_data = nic_feature_data['chip']
if adc128_data['status'] == "enable":
load_adc128_feature(adc128_data)
if ina3221_data['status'] == "enable":
load_ina3221_feature(ina3221_data)
if emc1413_data['status'] == "enable":
load_emc1413_feature(emc1413_data)
if tmp468_data['status'] == "enable":
load_tmp468_feature(tmp468_data)
if tmp112_data['status'] == "enable":
load_tmp112_feature(tmp112_data)
if fru_data['status'] == "enable":
load_fru_feature(fru_data)
if chip_data['status'] == "enable":
load_chip_feature(chip_data)
def load_public_function():
dbg_print("info", "====== Start load public function======")
public_function_context="""
# print format message to console
# @Param1 message level [Info Warning Error]
# @Param2 message content
format_print(){
echo ">>> [$1] $2"
}
# print format message to log file
# @Param1 message level [Info Warning Error]
# @Param2 message content
format_log_print(){
echo "[$1] $2" >> $log
}
# switch pca9548 channels then do i2cdetect one times
# @Param1 i2c bus number
# @Param2 pca9548 slave address
# @Param3 pca9548 channel hex value
# @Param4 pca9548 channel
# @Param5 pcie slot number
do_i2c_detect(){
if [ $nic_type == "ocp" ];then
format_print $INFO "OCP slot $5 : bus$1 9548channel$4"
else
format_print $INFO "PCIe slot $5 : bus$1 9548channel$4"
fi
i2ctransfer -y $1 w1@$2 $3
i2cdetect -y $1
}
# Reset pca9548 channel to 0x00
# @Param1 i2c bus number
# @Param2 pca9548 slave address
reset_pca9548(){
i2ctransfer -y $1 w1@$2 0x00
}
"""
try:
with open (output_path, 'a+', encoding='utf-8') as file:
file.write(public_function_context)
dbg_print("info", "Success to load public function!")
except Exception as e:
dbg_print("Error", f"An error ouccuer when load public function")
# =====================================================
# Start execute script
# =====================================================
init_output_file()
process_nic_feature()
load_public_function()
process_hw_topo()