Line data Source code
1 : /**
2 : * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "topo.h"
11 : #include <stdio.h>
12 : #include <stdlib.h>
13 : #include <sys/stat.h>
14 : #include <string.h>
15 : #include <regex.h>
16 : #include "securec.h"
17 : #include "hal.h"
18 :
19 : #define MAX_TOPO_FILE_SIZE (40960)
20 :
21 : /**
22 : * @brief 用正则表达式解析JSON文件中的"topo_file_path"字段值(无goto,纯C实现)
23 : * @param json_file_path 输入的JSON文件路径
24 : * @return 成功返回动态分配的字符串(需调用者手动free),失败返回NULL
25 : */
26 0 : int GetTopoFilePathFromFile(const char* filePath, char* topoFilePath, size_t bufSize)
27 : {
28 : // 初始化所有资源为NULL/初始值,便于后续释放判断
29 : regex_t regex;
30 : regmatch_t match[2]; // match[0]整体匹配,match[1]捕获路径值
31 :
32 : // 1. 打开JSON文件
33 0 : FILE *fp = fopen(filePath, "rb");
34 0 : if (fp == NULL) {
35 0 : return -1;
36 : }
37 :
38 : struct stat st;
39 0 : fstat(fileno(fp), &st);
40 0 : size_t fileSize = st.st_size;
41 : // 避免消耗过大内存
42 0 : if (fileSize > MAX_TOPO_FILE_SIZE) {
43 0 : fclose(fp);
44 0 : return -1;
45 : }
46 : // 3. 分配内存并读取文件全部内容
47 0 : char* fileBuf = (char*)malloc(fileSize + 1);
48 0 : if (fileBuf == NULL) {
49 0 : fclose(fp);
50 0 : return -1;
51 : }
52 0 : memset_s(fileBuf, fileSize+1, 0, fileSize + 1);
53 0 : size_t readBytes = fread(fileBuf, 1, fileSize, fp);
54 0 : if (readBytes != fileSize) {
55 0 : free(fileBuf); // 释放已分配的内存
56 0 : fclose(fp); // 释放文件
57 0 : return -1;
58 : }
59 0 : fclose(fp); // 文件内容已读取,提前释放文件资源
60 :
61 : // 4. 编译正则表达式
62 0 : const char* pattern = "\"topo_file_path\"\\s*:\\s*\"([^\"]*)\"";
63 0 : if (regcomp(®ex, pattern, REG_EXTENDED) != 0) {
64 0 : free(fileBuf);
65 0 : return -1;
66 : }
67 :
68 : // 5. 执行正则匹配
69 0 : if (regexec(®ex, fileBuf, 2, match, 0) != 0) {
70 0 : regfree(®ex); // 释放正则编译资源
71 0 : free(fileBuf);
72 0 : return -1;
73 : }
74 :
75 : // 6. 提取匹配到的路径值
76 0 : int start = match[1].rm_so;
77 0 : int len = match[1].rm_eo - match[1].rm_so;
78 0 : int ret = strncpy_s(topoFilePath, bufSize, fileBuf + start, len);
79 0 : topoFilePath[len] = '\0'; // 确保字符串以'\0'结尾
80 : // 7. 释放临时资源(仅保留result作为返回值)
81 0 : regfree(®ex);
82 0 : free(fileBuf);
83 0 : return ret;
84 : }
85 :
86 : typedef struct closPorts
87 : {
88 : unsigned int mainboardId;
89 : int dieId;
90 : int ports[32];
91 : int portCnt;
92 : }ClosPortMap;
93 :
94 : /**
95 : * 固定写死每种产品类型的端口
96 : */
97 : static ClosPortMap closPortMap[2] = {
98 : {MAIN_BOARD_ID_SERVER_TYPE1, 0, {4,5,6,7}, 4},
99 : {MAIN_BOARD_ID_SERVER_TYPE1, 1, {5,6}, 2},
100 : };
101 :
102 0 : int TopoGetClosPort(unsigned int mainboardId, int dieId, int *ports, int *portCnt)
103 : {
104 0 : int size = sizeof(closPortMap) / sizeof(ClosPortMap);
105 0 : for (int i = 0; i < size; ++i) {
106 0 : if (mainboardId == closPortMap[i].mainboardId && dieId == closPortMap[i].dieId) {
107 0 : memcpy_s(ports, (*portCnt), closPortMap[i].ports, closPortMap[i].portCnt);
108 0 : *portCnt = closPortMap[i].portCnt;
109 0 : return 0;
110 : }
111 : }
112 0 : return 0;
113 : }
114 :
115 : #define MAX_DRIVER_INSTALL_PATH (128)
116 :
117 330 : int TopoGetFilePath(unsigned mainboard_id, unsigned int spod_type, char* buf_size, size_t buf_len)
118 : {
119 330 : char driver_install_path[MAX_DRIVER_INSTALL_PATH] = {0};
120 330 : if (0 != hal_get_driver_install_path(driver_install_path, MAX_DRIVER_INSTALL_PATH)) {
121 0 : return -1;
122 : }
123 330 : int ret = -1;
124 330 : switch(mainboard_id)
125 : {
126 33 : case MAIN_BOARD_ID_CARD_NOMESH:
127 33 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_1.json");
128 33 : break;
129 33 : case MAIN_BOARD_ID_CARD_2PMESH:
130 33 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_2.json");
131 33 : break;
132 33 : case MAIN_BOARD_ID_CARD_4PMESH:
133 33 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_3.json");
134 33 : break;
135 65 : case MAIN_BOARD_ID_POD:
136 : case MAIN_BOARD_ID_POD_2D:
137 65 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_950_1.json");
138 65 : break;
139 132 : case MAIN_BOARD_ID_SERVER_TYPE1:
140 : case MAIN_BOARD_ID_SERVER_8PMESH:
141 : case MAIN_BOARD_ID_SERVER_8PMESH_UBOE:
142 : case MAIN_BOARD_ID_SERVER_8PMESH_NOSP:
143 : case MAIN_BOARD_ID_SERVER_8PMESH_NOSP_UBOE:
144 132 : if (spod_type == TOPO_TYPE_SERVER_16FM) {
145 1 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_2.json");
146 : } else {
147 131 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_1.json");
148 : }
149 132 : break;
150 34 : case MAIN_BOARD_ID_SERVER_UBX:
151 34 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_3.json");
152 34 : break;
153 0 : default:
154 0 : break;
155 : }
156 330 : if (ret < 0) {
157 0 : return -1;
158 : }
159 330 : return 0;
160 : }
|