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 7 : 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 7 : FILE* fp = fopen(filePath, "rb");
34 7 : if (fp == NULL) {
35 7 : 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 : unsigned int mainboardId;
88 : int dieId;
89 : int ports[32];
90 : int portCnt;
91 : } ClosPortMap;
92 :
93 : /**
94 : * 固定写死每种产品类型的端口
95 : */
96 : static ClosPortMap closPortMap[2] = {
97 : {MAIN_BOARD_ID_SERVER_TYPE1, 0, {4, 5, 6, 7}, 4},
98 : {MAIN_BOARD_ID_SERVER_TYPE1, 1, {5, 6}, 2},
99 : };
100 :
101 0 : int TopoGetClosPort(unsigned int mainboardId, int dieId, int* ports, int* portCnt)
102 : {
103 0 : int size = sizeof(closPortMap) / sizeof(ClosPortMap);
104 0 : for (int i = 0; i < size; ++i) {
105 0 : if (mainboardId == closPortMap[i].mainboardId && dieId == closPortMap[i].dieId) {
106 0 : memcpy_s(ports, (*portCnt), closPortMap[i].ports, closPortMap[i].portCnt);
107 0 : *portCnt = closPortMap[i].portCnt;
108 0 : return 0;
109 : }
110 : }
111 0 : return 0;
112 : }
113 :
114 : #define MAX_DRIVER_INSTALL_PATH (128)
115 :
116 330 : int TopoGetFilePath(unsigned mainboard_id, unsigned int spod_type, char* buf_size, size_t buf_len)
117 : {
118 330 : char driver_install_path[MAX_DRIVER_INSTALL_PATH] = {0};
119 330 : if (0 != hal_get_driver_install_path(driver_install_path, MAX_DRIVER_INSTALL_PATH)) {
120 0 : return -1;
121 : }
122 330 : int ret = -1;
123 330 : switch (mainboard_id) {
124 33 : case MAIN_BOARD_ID_CARD_NOMESH:
125 33 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_1.json");
126 33 : break;
127 33 : case MAIN_BOARD_ID_CARD_2PMESH:
128 33 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_2.json");
129 33 : break;
130 33 : case MAIN_BOARD_ID_CARD_4PMESH:
131 33 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_3.json");
132 33 : break;
133 65 : case MAIN_BOARD_ID_POD:
134 : case MAIN_BOARD_ID_POD_2D:
135 65 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_950_1.json");
136 65 : break;
137 132 : case MAIN_BOARD_ID_SERVER_TYPE1:
138 : case MAIN_BOARD_ID_SERVER_8PMESH:
139 : case MAIN_BOARD_ID_SERVER_8PMESH_UBOE:
140 : case MAIN_BOARD_ID_SERVER_8PMESH_NOSP:
141 : case MAIN_BOARD_ID_SERVER_8PMESH_NOSP_UBOE:
142 132 : if (spod_type == TOPO_TYPE_SERVER_16FM) {
143 1 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_2.json");
144 : } else {
145 131 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_1.json");
146 : }
147 132 : break;
148 34 : case MAIN_BOARD_ID_SERVER_UBX:
149 34 : ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_3.json");
150 34 : break;
151 0 : default:
152 0 : break;
153 : }
154 330 : if (ret < 0) {
155 0 : return -1;
156 : }
157 330 : return 0;
158 : }
|