LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/topo/topo_addr_info/src - topo.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 41.9 % 74 31
Test Date: 2026-08-18 17:47:01 Functions: 66.7 % 3 2

            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              : 
      11              : #include "topo.h"
      12              : #include <stdio.h>
      13              : #include <stdlib.h>
      14              : #include <sys/stat.h>
      15              : #include <string.h>
      16              : #include <regex.h>
      17              : #include "securec.h"
      18              : #include "hal.h"
      19              : 
      20              : #define MAX_TOPO_FILE_SIZE (40960)
      21              : 
      22              : /**
      23              :  * @brief 用正则表达式解析JSON文件中的"topo_file_path"字段值(无goto,纯C实现)
      24              :  * @param json_file_path 输入的JSON文件路径
      25              :  * @return 成功返回动态分配的字符串(需调用者手动free),失败返回NULL
      26              :  */
      27            7 : int GetTopoFilePathFromFile(const char* filePath, char* topoFilePath, size_t bufSize)
      28              : {
      29              :     // 初始化所有资源为NULL/初始值,便于后续释放判断
      30              :     regex_t regex;
      31              :     regmatch_t match[2]; // match[0]整体匹配,match[1]捕获路径值
      32              : 
      33              :     // 1. 打开JSON文件
      34            7 :     FILE* fp = fopen(filePath, "rb");
      35            7 :     if (fp == NULL) {
      36            7 :         return -1;
      37              :     }
      38              : 
      39              :     struct stat st;
      40            0 :     fstat(fileno(fp), &st);
      41            0 :     size_t fileSize = st.st_size;
      42              :     // 避免消耗过大内存
      43            0 :     if (fileSize > MAX_TOPO_FILE_SIZE) {
      44            0 :         fclose(fp);
      45            0 :         return -1;
      46              :     }
      47              :     // 3. 分配内存并读取文件全部内容
      48            0 :     char* fileBuf = (char*)malloc(fileSize + 1);
      49            0 :     if (fileBuf == NULL) {
      50            0 :         fclose(fp);
      51            0 :         return -1;
      52              :     }
      53            0 :     memset_s(fileBuf, fileSize + 1, 0, fileSize + 1);
      54            0 :     size_t readBytes = fread(fileBuf, 1, fileSize, fp);
      55            0 :     if (readBytes != fileSize) {
      56            0 :         free(fileBuf); // 释放已分配的内存
      57            0 :         fclose(fp);    // 释放文件
      58            0 :         return -1;
      59              :     }
      60            0 :     fclose(fp); // 文件内容已读取,提前释放文件资源
      61              : 
      62              :     // 4. 编译正则表达式
      63            0 :     const char* pattern = "\"topo_file_path\"\\s*:\\s*\"([^\"]*)\"";
      64            0 :     if (regcomp(&regex, pattern, REG_EXTENDED) != 0) {
      65            0 :         free(fileBuf);
      66            0 :         return -1;
      67              :     }
      68              : 
      69              :     // 5. 执行正则匹配
      70            0 :     if (regexec(&regex, fileBuf, 2, match, 0) != 0) {
      71            0 :         regfree(&regex); // 释放正则编译资源
      72            0 :         free(fileBuf);
      73            0 :         return -1;
      74              :     }
      75              : 
      76              :     // 6. 提取匹配到的路径值
      77            0 :     int start = match[1].rm_so;
      78            0 :     int len = match[1].rm_eo - match[1].rm_so;
      79            0 :     int ret = strncpy_s(topoFilePath, bufSize, fileBuf + start, len);
      80            0 :     topoFilePath[len] = '\0'; // 确保字符串以'\0'结尾
      81              :     // 7. 释放临时资源(仅保留result作为返回值)
      82            0 :     regfree(&regex);
      83            0 :     free(fileBuf);
      84            0 :     return ret;
      85              : }
      86              : 
      87              : typedef struct closPorts {
      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           33 :         case MAIN_BOARD_ID_CARD_NOMESH:
     126           33 :             ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_1.json");
     127           33 :             break;
     128           33 :         case MAIN_BOARD_ID_CARD_2PMESH:
     129           33 :             ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_2.json");
     130           33 :             break;
     131           33 :         case MAIN_BOARD_ID_CARD_4PMESH:
     132           33 :             ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_350_3.json");
     133           33 :             break;
     134           65 :         case MAIN_BOARD_ID_POD:
     135              :         case MAIN_BOARD_ID_POD_2D:
     136           65 :             ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_950_1.json");
     137           65 :             break;
     138          132 :         case MAIN_BOARD_ID_SERVER_TYPE1:
     139              :         case MAIN_BOARD_ID_SERVER_8PMESH:
     140              :         case MAIN_BOARD_ID_SERVER_8PMESH_UBOE:
     141              :         case MAIN_BOARD_ID_SERVER_8PMESH_NOSP:
     142              :         case MAIN_BOARD_ID_SERVER_8PMESH_NOSP_UBOE:
     143          132 :             if (spod_type == TOPO_TYPE_SERVER_16FM) {
     144            1 :                 ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_2.json");
     145              :             } else {
     146          131 :                 ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_1.json");
     147              :             }
     148          132 :             break;
     149           34 :         case MAIN_BOARD_ID_SERVER_UBX:
     150           34 :             ret = sprintf_s(buf_size, buf_len, "%s/%s", driver_install_path, "driver/topo/950/atlas_850_3.json");
     151           34 :             break;
     152            0 :         default:
     153            0 :             break;
     154              :     }
     155          330 :     if (ret < 0) {
     156            0 :         return -1;
     157              :     }
     158          330 :     return 0;
     159              : }
        

Generated by: LCOV version 2.0-1