123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074 |
- // LocalConfig.cpp
- // 添加必要的头文件
- #include <unistd.h>
- #include <pthread.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <ifaddrs.h>
- #include <netinet/in.h>
- #include <net/if.h>
- #include <sys/ioctl.h>
- #include <time.h>
- #include <errno.h>
- #include <algorithm>
- #include <cctype>
- #include <cstring>
- #include <cstdio>
- #include <cstdlib>
- #include <cstdarg>
- #include <map>
- #include <sys/types.h>
- #include <netpacket/packet.h> // 包含 sockaddr_ll 定义
- #include "common_api.h"
- #include "LocalConfig.h"
- #include "Crc64.h"
- #include <iostream>
- // 全局变量定义
- // ================================================================
- CCOS_PROC_TYPE g_ConfigMode = CCOS_PROC_MASTER; // 默认进程角色为主进程
- vector<string> g_DriverConfigFilepath; // 驱动配置文件路径列表
- string g_LocalBusId = ""; // 本地总线ID
- string g_MajorID = ""; // 主ID
- string g_VendorID = ""; // 供应商ID
- string g_ProductID = ""; // 产品ID
- string g_SerialID = ""; // 序列号ID
- string g_DeviceType = ""; // 设备类型
- pthread_mutex_t m_Section = PTHREAD_MUTEX_INITIALIZER; // 线程互斥锁
- ResDataObject g_resUid2TypeMap; // GUID到类型的映射
- /**
- * 递归创建目录
- * @param path 目录路径
- * @return 成功返回true,失败返回false
- */
- bool CreateDirectoryLinux(const char* path) {
- char tmp[256];
- char* p = NULL;
- size_t len;
- // 复制路径并确保不以斜杠结尾
- snprintf(tmp, sizeof(tmp), "%s", path);
- len = strlen(tmp);
- if (len > 0 && tmp[len - 1] == '/') {
- tmp[len - 1] = '\0';
- }
- // 逐级创建目录
- for (p = tmp + 1; *p; p++) {
- if (*p == '/') {
- *p = '\0'; // 临时截断路径
- // 创建中间目录(忽略已存在的目录)
- if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
- return false;
- }
- *p = '/'; // 恢复路径
- }
- }
- // 创建最终目录
- return mkdir(tmp, 0755) == 0 || errno == EEXIST;
- }
- /**
- * 检查文件或目录是否存在
- * @param path 路径
- * @return 存在返回true,否则返回false
- */
- bool FileExists(const char* path) {
- struct stat info;
- return stat(path, &info) == 0;
- }
- /**
- * 从完整路径中提取文件名
- * @param fullPath 完整路径
- * @return 文件名
- */
- string GetFileName(const string& fullPath) {
- size_t pos = fullPath.find_last_of("/");
- return (pos != string::npos) ? fullPath.substr(pos + 1) : fullPath;
- }
- /**
- * 从完整路径中提取目录路径
- * @param fullPath 完整路径
- * @return 目录路径
- */
- string GetFileDirectory(const string& fullPath) {
- size_t pos = fullPath.find_last_of("/");
- return (pos != string::npos) ? fullPath.substr(0, pos) : "";
- }
- /**
- * 获取当前时间戳(毫秒)
- * @return 毫秒时间戳
- */
- DWORD GetTickCount() {
- struct timespec ts;
- // 使用单调时钟获取时间
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
- }
- /**
- * 获取所有网络接口的MAC地址
- * @param vMacAddress 输出MAC地址列表
- * @return 找到的MAC地址数量
- */
- LOCALCONFIG_API DWORD GetMacAddress(vector<string>& vMacAddress) {
- struct ifaddrs* ifaddr, * ifa;
- vMacAddress.clear();
- if (getifaddrs(&ifaddr) == -1) {
- return 0;
- }
- for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
- if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_PACKET)
- continue;
- // 使用正确的类型转换
- struct sockaddr_ll* s = (struct sockaddr_ll*)ifa->ifa_addr;
- // 检查 MAC 地址长度
- if (s->sll_halen < 6) continue;
- // 格式化 MAC 地址
- char mac[32];
- snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
- s->sll_addr[0], s->sll_addr[1], s->sll_addr[2],
- s->sll_addr[3], s->sll_addr[4], s->sll_addr[5]);
- vMacAddress.push_back(mac);
- }
- freeifaddrs(ifaddr);
- return vMacAddress.size();
- }
- // 主要功能函数实现
- // ================================================================
- /**
- * 添加驱动配置文件路径并解析EBus ID
- */
- LOCALCONFIG_API bool AddDriverConfig(const char* pszDriverConfig) {
- // 构造完整路径: 配置路径 + 文件名
- string FullPath = GetDriverConfigPath().encode();
- std::cout << "[DEBUG] Construct the complete path: configuration path : " << FullPath << std::endl;
- FullPath += pszDriverConfig;
- std::cout << "[DEBUG] Construct the complete path: configuration path + file name: " << FullPath << std::endl;
- // 添加到全局列表
- g_DriverConfigFilepath.push_back(FullPath);
- // 解析驱动文件获取EBus ID
- ResDataObject BusId, DriverPath;
- bool ret = GetDriverEbusId(FullPath.c_str(), BusId, DriverPath);
- if (ret) {
- g_LocalBusId = (const char*)BusId;
- }
- return ret;
- }
- /**
- * 设置配置模式并加载GUID映射
- */
- LOCALCONFIG_API void SetConfigMode(CCOS_PROC_TYPE Master) {
- g_ConfigMode = Master;
- string mapConf = GetProcessDirectory() + "/LocalConfig.xml";
- ResDataObject config;
- // 加载并解析配置文件
- if (config.loadFile(mapConf.c_str())) {
- // 查找GUID到类型的映射
- int Idx = config["CONFIGURATION"].GetFirstOf("GUID2Type");
- if (Idx >= 0) {
- g_resUid2TypeMap = config["CONFIGURATION"][Idx];
- }
- }
- }
- /**
- * 获取本地IP地址
- */
- LOCALCONFIG_API ResDataObject& getLocalIpAddress() {
- static ResDataObject mn_LocalIpAddress;
- struct ifaddrs* ifaddr, * ifa;
- // 获取网络接口列表
- if (getifaddrs(&ifaddr) != 0) {
- return mn_LocalIpAddress;
- }
- // 遍历网络接口
- for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
- // 只处理IPv4接口
- if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
- continue;
- // 获取IP地址
- void* tmpAddrPtr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr;
- char addressBuffer[INET_ADDRSTRLEN];
- inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
- // 跳过回环地址
- if (strcmp(addressBuffer, "127.0.0.1") != 0) {
- mn_LocalIpAddress = addressBuffer;
- break;
- }
- }
- // 释放资源
- freeifaddrs(ifaddr);
- return mn_LocalIpAddress;
- }
- /**
- * 获取当前可执行文件名
- */
- LOCALCONFIG_API ResDataObject& GetModuleTitle() {
- static ResDataObject mn_ModuleTitle;
- char path[1024] = { 0 };
- // 通过 /proc/self/exe 获取可执行文件路径
- ssize_t count = readlink("/proc/self/exe", path, sizeof(path) - 1);
- if (count > 0) {
- path[count] = '\0';
- char* last_slash = strrchr(path, '/');
- if (last_slash) {
- // 提取文件名
- mn_ModuleTitle = last_slash + 1;
- }
- }
- return mn_ModuleTitle;
- }
- /**
- * 获取主机名(大写形式)作为机器ID
- */
- LOCALCONFIG_API ResDataObject& getLocalMachineId() {
- static ResDataObject mn_LocalMachineId;
- char hostname[256] = { 0 };
- // 获取主机名
- if (gethostname(hostname, sizeof(hostname) - 1) == 0) {
- string filenameBig = hostname;
- // 转换为大写
- transform(filenameBig.begin(), filenameBig.end(), filenameBig.begin(), ::toupper);
- mn_LocalMachineId = filenameBig.c_str();
- }
- return mn_LocalMachineId;
- }
- /**
- * 创建工作路径
- */
- LOCALCONFIG_API ResDataObject MakeWorkPath(ResDataObject& probeInfo, ResDataObject& Connection,
- ResDataObject& ProcPath, bool ForWork) {
- ResDataObject ResRet;
- ResDataObject major, product, serial;
- // 获取必要字段
- if (!TryGetValue(probeInfo, "MajorID", major) ||
- !TryGetValue(probeInfo, "ProductID", product)) {
- return ResRet; // 返回空对象
- }
- // 处理序列号
- if (!TryGetValue(probeInfo, "SerialID", serial) || strlen((const char*)serial) == 0) {
- probeInfo.add("SerialID", GetTickCount());
- serial = (const char*)probeInfo["SerialID"];
- }
- // 确定目录结构
- vector<string> folders;
- folders.push_back(ForWork ? "WorkPath" : "Logs");
- folders.push_back((const char*)major);
- folders.push_back((const char*)product);
- folders.push_back((const char*)serial);
- // 构建完整路径
- string workpath = string((const char*)ProcPath) + "/";
- for (size_t i = 0; i < folders.size(); i++) {
- string dirPath = workpath + folders[i];
- // 创建目录(如果不存在)
- if (!FileExists(dirPath.c_str()) && !CreateDirectoryLinux(dirPath.c_str())) {
- return ResRet; // 创建失败返回空对象
- }
- workpath += folders[i] + "/";
- }
- // 返回构建的路径
- ResRet = workpath.c_str();
- return ResRet;
- }
- /**
- * 创建设备路径
- */
- LOCALCONFIG_API ResDataObject MakeDevicePath(ResDataObject& probeInfo, ResDataObject& Connection,
- ResDataObject& SubPath) {
- ResDataObject resRet;
- ResDataObject major, minor, vendor, product, serial;
- // 获取所有必要字段
- if (!TryGetValue(probeInfo, "MajorID", major) ||
- !TryGetValue(probeInfo, "MinorID", minor) ||
- !TryGetValue(probeInfo, "VendorID", vendor) ||
- !TryGetValue(probeInfo, "ProductID", product)) {
- return resRet;
- }
- // 处理序列号
- if (!TryGetValue(probeInfo, "SerialID", serial) || strlen((const char*)serial) == 0) {
- probeInfo.add("SerialID", GetTickCount());
- serial = (const char*)probeInfo["SerialID"];
- }
- // 构建设备路径
- string devicepath = "/" + string((const char*)getLocalEbusId()) + "/" +
- string((const char*)major) + "/" +
- string((const char*)minor) + "/" +
- string((const char*)vendor) + "/" +
- string((const char*)product) + "/" +
- string((const char*)serial) + "/" +
- string((const char*)SubPath);
- resRet = devicepath.c_str();
- return resRet;
- }
- /**
- * 创建CCOS路径
- */
- LOCALCONFIG_API ResDataObject MakeCcosPath(ResDataObject& probeInfo, ResDataObject& Connection,
- ResDataObject& SubPath, bool isDriver) {
- ResDataObject resRet;
- ResDataObject major, vendor, product, serial;
- // 获取必要字段
- if (!TryGetValue(probeInfo, "MajorID", major) ||
- !TryGetValue(probeInfo, "VendorID", vendor) ||
- !TryGetValue(probeInfo, "ProductID", product)) {
- return resRet;
- }
- // 处理序列号
- if (!TryGetValue(probeInfo, "SerialID", serial) || strlen((const char*)serial) == 0) {
- probeInfo.add("SerialID", GetTickCount());
- serial = (const char*)probeInfo["SerialID"];
- }
- // 构建CCOS路径
- string devicepath = isDriver ? "CCOS/DRIVER/" : "CCOS/DEVICE/";
- devicepath += string((const char*)major) + "/" +
- string((const char*)vendor) + "/" +
- string((const char*)product) + "/" +
- string((const char*)serial);
- resRet = devicepath.c_str();
- return resRet;
- }
- /**
- * 获取驱动配置路径
- */
- LOCALCONFIG_API ResDataObject& GetDriverConfigPath() {
- static ResDataObject resRet;
- std::string ret = GetProcessDirectory() + "/DriverConfig/";
- std::cout << "[DEBUG] GetProcessDirectory() + / DriverConfig / : " << ret << std::endl;
- resRet = ret.c_str();
- //resRet = "/home/cxdz/code/diosproc/DriverConfig/";
- std::cout << "[DEBUG] resRet : " << resRet.encode() << std::endl;
- return resRet;
- }
- /**
- * 获取驱动EBus ID
- */
- LOCALCONFIG_API bool GetDriverEbusId(const char* pConfigFilepath, ResDataObject& BusId,
- ResDataObject& DriverPath, bool ForceUpdate) {
- ResDataObject Context;
- // 加载配置文件
- if (!Context.loadFile(pConfigFilepath)) {
- return false;
- }
- // 提取配置中的各个ID
- string absContext = ""; // 用于生成唯一ID的上下文字符串
- char HighFive[6] = { 0 }; // 存储ID的首字符
- // 提取并验证各个ID
- string MajorID = (const char*)Context["CONFIGURATION"]["MajorID"];
- string MinorID = (const char*)Context["CONFIGURATION"]["MinorID"];
- string VendorID = (const char*)Context["CONFIGURATION"]["VendorID"];
- string ProductID = (const char*)Context["CONFIGURATION"]["ProductID"];
- string SerialID = (const char*)Context["CONFIGURATION"]["SerialID"];
- string GUID = (const char*)Context["CONFIGURATION"]["GUID"];
- std::cout << "[DEBUG] Configuration IDs:" << std::endl;
- std::cout << "[DEBUG] MajorID: " << MajorID << std::endl;
- std::cout << "[DEBUG] MinorID: " << MinorID << std::endl;
- std::cout << "[DEBUG] VendorID: " << VendorID << std::endl;
- std::cout << "[DEBUG] ProductID: " << ProductID << std::endl;
- std::cout << "[DEBUG] SerialID: " << SerialID << std::endl;
- std::cout << "[DEBUG] GUID: " << GUID << std::endl;
- if (MajorID.empty() || MinorID.empty() || VendorID.empty() ||
- ProductID.empty() || SerialID.empty() || GUID.empty()) {
- return false;
- }
- // 更新全局ID缓存
- g_MajorID = MajorID;
- g_VendorID = VendorID;
- g_ProductID = ProductID;
- g_SerialID = SerialID;
- // 构建唯一性上下文
- absContext = "/" + MajorID + "/" + MinorID + "/" + VendorID + "/" + ProductID + "/" + SerialID + "/" + GUID;
- HighFive[0] = MajorID[0];
- HighFive[1] = MinorID[0];
- HighFive[2] = VendorID[0];
- HighFive[3] = ProductID[0];
- HighFive[4] = SerialID[0];
- // 尝试获取配置中的BusId
- string OnlyBusId = "";
- int BusIdsIndex = Context["CONFIGURATION"].GetFirstOf("BusId");
- if (BusIdsIndex >= 0) {
- OnlyBusId = (const char*)Context["CONFIGURATION"][BusIdsIndex];
- }
- // 如未配置BusId,则生成一个
- if (OnlyBusId.empty()) {
- // 使用CRC32生成唯一ID
- int Crc32res = GetCrc32(absContext.c_str(), absContext.size());
- OnlyBusId = "D" + string(HighFive) + FormatstdString("%08X", Crc32res);
- }
- // 构建设备路径
- string TempPath = "/" + OnlyBusId + absContext;
- BusId = OnlyBusId.c_str();
- DriverPath = TempPath.c_str();
- return true;
- }
- /**
- * 生成唯一EBus ID
- */
- LOCALCONFIG_API bool MakeUniqEbusId(ResDataObject& res) {
- static DWORD l_PrevTime = 0; // 上次生成ID的时间戳
- ResDataObject mId = getLocalMachineId(); // 获取机器ID
- vector<string> maclist; // MAC地址列表
- // 获取MAC地址
- if (GetMacAddress(maclist) == 0) {
- return false;
- }
- // 加锁确保线程安全
- pthread_mutex_lock(&m_Section);
- // 获取唯一时间戳(避免重复)
- DWORD CurTime = GetTickCount();
- while (CurTime == l_PrevTime) {
- usleep(1000); // 休眠1毫秒
- CurTime = GetTickCount();
- }
- l_PrevTime = CurTime;
- // 释放锁
- pthread_mutex_unlock(&m_Section);
- // 构建唯一字符串
- string crcstr = (const char*)mId;
- for (size_t i = 0; i < maclist.size(); i++) {
- crcstr += maclist[i];
- }
- crcstr += FormatstdString("%08X", l_PrevTime); // 时间戳
- crcstr += FormatstdString("%08X", getpid()); // 进程ID
- crcstr += FormatstdString("%08X", (unsigned long)pthread_self()); // 线程ID
- // 计算CRC32作为唯一ID
- int crc = GetCrc32(crcstr.c_str(), crcstr.size());
- res = FormatstdString("%08X", crc).c_str();
- return true;
- }
- /**
- * 获取设备类型字符串
- */
- LOCALCONFIG_API string& GetMajorID() {
- // 从本地配置获取缺失的ID
- if (g_MajorID.empty()) {
- ResDataObject val = tryGetLocalOptions("MajorID");
- g_MajorID = (const char*)val;
- }
- if (g_VendorID.empty()) {
- ResDataObject val = tryGetLocalOptions("VendorID");
- g_VendorID = (const char*)val;
- }
- if (g_ProductID.empty()) {
- ResDataObject val = tryGetLocalOptions("ProductID");
- g_ProductID = (const char*)val;
- }
- if (g_SerialID.empty()) {
- ResDataObject val = tryGetLocalOptions("SerialID");
- g_SerialID = (const char*)val;
- }
- // 构建设备类型字符串
- g_DeviceType = g_MajorID + "_" + g_VendorID + "_" + g_ProductID + "_" + g_SerialID;
- return g_DeviceType;
- }
- /**
- * 获取设备ID
- */
- LOCALCONFIG_API string GetDeviceID() {
- // 确保所有ID已加载
- if (g_MajorID.empty() || g_VendorID.empty() || g_ProductID.empty() || g_SerialID.empty()) {
- GetMajorID(); // 加载ID
- }
- // 构建设备ID字符串
- if (!g_MajorID.empty()) {
- return g_MajorID + "/" + g_VendorID + "/" + g_ProductID + "/" + g_SerialID;
- }
- return "Channel/Channel"; // 默认值
- }
- // ================================================================
- LOCALCONFIG_API const char* GetDriverConfigFilepath(size_t idx) {
- return (idx < g_DriverConfigFilepath.size()) ? g_DriverConfigFilepath[idx].c_str() : nullptr;
- }
- LOCALCONFIG_API CCOS_PROC_TYPE GetConfigMode() {
- return g_ConfigMode;
- }
- LOCALCONFIG_API ResDataObject tryGetLocalOptions(const char* pKey) {
- string dir = GetProcessDirectory() + "/LocalConfig.xml";
- ResDataObject config, result;
- if (config.loadFile(dir.c_str())) {
- int Idx = config["CONFIGURATION"].GetFirstOf(pKey);
- if (Idx >= 0) {
- result = config["CONFIGURATION"][Idx];
- }
- }
- return result;
- }
- LOCALCONFIG_API ResDataObject& getLocalEbusId() {
- static ResDataObject mn_LocalEbusId;
- if (g_ConfigMode == CCOS_PROC_MASTER) {
- mn_LocalEbusId = g_LocalBusId.c_str();
- }
- else if (g_ConfigMode == CCOS_PROC_CHANNEL) {
- mn_LocalEbusId = "ccosChannel";
- }
- return mn_LocalEbusId;
- }
- LOCALCONFIG_API ResDataObject& getChannelEbusId() {
- static ResDataObject mn_LocalEbusId;
- mn_LocalEbusId = "ccosChannel";
- return mn_LocalEbusId;
- }
- LOCALCONFIG_API ResDataObject& getChannelRootpath() {
- static ResDataObject mn_ChannelId;
- mn_ChannelId = "/ccosChannel";
- return mn_ChannelId;
- }
- LOCALCONFIG_API ResDataObject& getLocalEbusRouterIp() {
- static ResDataObject mn_EbusRouterIp;
- mn_EbusRouterIp = tryGetLocalOptions("RouterIp");
- return mn_EbusRouterIp;
- }
- LOCALCONFIG_API ResDataObject& getLocalEbusPort() {
- static ResDataObject mn_EbusPort;
- mn_EbusPort = tryGetLocalOptions("Port");
- return mn_EbusPort;
- }
- static ResDataObject g_LogRootPath;
- LOCALCONFIG_API void setLogRootpath(const char* pszRootPath) {
- g_LogRootPath = pszRootPath;
- }
- LOCALCONFIG_API ResDataObject& getLogRootpath() {
- return g_LogRootPath;
- }
- LOCALCONFIG_API ResDataObject& getRootpath() {
- static ResDataObject mn_Rootpath;
- string rootpath = "/" + string((const char*)getLocalEbusId());
- mn_Rootpath = rootpath.c_str();
- return mn_Rootpath;
- }
- LOCALCONFIG_API bool getEbusLogFlag() {
- ResDataObject val = tryGetLocalOptions("EnableEbusLog");
- return (bool)val;
- }
- LOCALCONFIG_API bool getP2pFlag() {
- ResDataObject val = tryGetLocalOptions("P2pEnable");
- return (bool)val;
- }
- LOCALCONFIG_API ResDataObject getP2pServerIp() {
- return tryGetLocalOptions("P2pServer");
- }
- LOCALCONFIG_API ResDataObject getP2pRole() {
- return tryGetLocalOptions("P2pClient");
- }
- LOCALCONFIG_API ResDataObject& GetFullDriverConfigPath() {
- static ResDataObject resRet;
- string ret = GetProcessDirectory() + "/FullConfig/";
- resRet = ret.c_str();
- return resRet;
- }
- LOCALCONFIG_API DWORD GetFullDriverConfigPathEx(ResDataObject& PathList) {
- DWORD ret = 1;
- PathList.clear();
- // 根目录
- string dir = GetProcessDirectory();
- dir += "/FullConfig/";
- PathList.add(dir.c_str(), "");
- // 子版本目录
- string subdir = GetProcessDirectory();
- subdir += "/SubVersions/";
- map<string, vector<string>> templist;
- if (FindSubVersionDirs(subdir, templist)) {
- ret += (DWORD)templist.size();
- map<string, vector<string>>::iterator iter = templist.begin();
- while (iter != templist.end()) {
- for (DWORD i = 0; i < iter->second.size(); i++) {
- string filename = iter->second[i];
- // 在Linux下保持原始大小写
- // 构建路径
- string dirPath = filename + "/FullConfig/";
- PathList.add(dirPath.c_str(), iter->first.c_str());
- }
- ++iter;
- }
- }
- return ret;
- }
- LOCALCONFIG_API ResDataObject& GetFullDriverConfigPathX32() {
- static ResDataObject resRet;
- string ret = GetProcessDirectory() + "/win32/FullConfig/";
- resRet = ret.c_str();
- return resRet;
- }
- LOCALCONFIG_API ResDataObject& GetDriverConfigPathX32() {
- static ResDataObject resRet;
- string ret = GetProcessDirectory() + "/win32/DriverConfig/";
- resRet = ret.c_str();
- return resRet;
- }
- LOCALCONFIG_API DWORD GetDriverConfigPathEx(ResDataObject& PathList) {
- DWORD ret = 1;
- PathList.clear();
- // 根目录
- string dir = GetProcessDirectory();
- dir += "/DriverConfig/";
- PathList.add(dir.c_str(), "");
- // 子版本目录
- string subdir = GetProcessDirectory();
- subdir += "/SubVersions/";
- map<string, vector<string>> templist;
- if (FindSubVersionDirs(subdir, templist)) {
- ret += (DWORD)templist.size();
- map<string, vector<string>>::iterator iter = templist.begin();
- while (iter != templist.end()) {
- for (DWORD i = 0; i < iter->second.size(); i++) {
- string filename = iter->second[i];
- // 在Linux下保持原始大小写
- // 构建路径
- string dirPath = filename + "/DriverConfig/";
- PathList.add(dirPath.c_str(), iter->first.c_str());
- }
- ++iter;
- }
- }
- return ret;
- }
- LOCALCONFIG_API ResDataObject& GetLogConfigPath() {
- static ResDataObject resRet;
- string ret = GetProcessDirectory() + "/logconfig.xml";
- resRet = ret.c_str();
- return resRet;
- }
- LOCALCONFIG_API bool GetDriverProcessPath(const char* pConfigFilepath, ResDataObject& DriverProcessPath) {
- ResDataObject Context;
- if (!Context.loadFile(pConfigFilepath)) {
- return false;
- }
- string WorkPath = GetFileDirectory(GetFileDirectory(string(pConfigFilepath)));
- WorkPath += "/CcosProc";
- string NodeName = (const char*)Context["CONFIGURATION"]["ProcType"];
- if (NodeName == "MFC") {
- WorkPath += "MFC";
- }
- NodeName = (const char*)Context["CONFIGURATION"]["Arch"];
- if (NodeName == "64") {
- WorkPath += "X64";
- }
- WorkPath += ".exe";
- DriverProcessPath = WorkPath.c_str();
- return true;
- }
- LOCALCONFIG_API string GetModuleConfigrateFilePath(ResDataObject& resHardware) {
- string MajorID = resHardware["MajorID"].encode();
- string VendorID = resHardware["VendorID"].encode();
- string ProductID = resHardware["ProductID"].encode();
- string SerialID = resHardware["SerialID"].encode();
- string strPath = GetProcessDirectory();
- strPath += "/DriverDefine/Config/";
- strPath += MajorID + "_" + VendorID + "_" + ProductID + "_" + SerialID + ".json";
- return strPath;
- }
- LOCALCONFIG_API string GetModuleConfigrateFilePath(string devDioPath) {
- vector<string> parts;
- char buffer[1024];
- strcpy(buffer, devDioPath.c_str());
- char* token = strtok(buffer, "/");
- while (token) {
- parts.push_back(token);
- token = strtok(NULL, "/");
- }
- if (parts.size() >= 6) {
- string MajorID = parts[1];
- string VendorID = parts[3];
- string ProductID = parts[4];
- string SerialID = parts[5];
- string strPath = GetProcessDirectory();
- strPath += "/DriverDefine/Config/";
- strPath += MajorID + "_" + VendorID + "_" + ProductID + "_" + SerialID + ".json";
- return strPath;
- }
- return "";
- }
- LOCALCONFIG_API string GetCcosModuleConfigrateFilePath(string devDioPath, bool useTemplate) {
- vector<string> parts;
- char buffer[1024];
- strcpy(buffer, devDioPath.c_str());
- char* token = strtok(buffer, "/");
- while (token) {
- parts.push_back(token);
- token = strtok(NULL, "/");
- }
- if (parts.size() >= 6) {
- string MajorID = parts[2];
- string VendorID = parts[3];
- string ProductID = parts[4];
- string SerialID = parts[5];
- string strPath = GetProcessDirectory();
- strPath += useTemplate ? "/DriverDefine/" : "/DriverDefine/Config/";
- strPath += MajorID + "_" + VendorID + "_" + ProductID;
- strPath += useTemplate ? "" : "_" + SerialID;
- strPath += ".json";
- return strPath;
- }
- return "";
- }
- LOCALCONFIG_API string GetModuleDevicePath(ResDataObject& resHardware) {
- string MajorID = resHardware["MajorID"].encode();
- string VendorID = resHardware["VendorID"].encode();
- string ProductID = resHardware["ProductID"].encode();
- string SerialID = resHardware["SerialID"].encode();
- return "CCOS/DEVICE/" + MajorID + "/" + VendorID + "/" + ProductID + "/" + SerialID;
- }
- LOCALCONFIG_API BOOL ClearAllModuleConfigrate() {
- string dirPath = GetProcessDirectory() + "/DriverDefine/Config/";
- DIR* dir = opendir(dirPath.c_str());
- if (!dir) return FALSE;
- struct dirent* ent;
- while ((ent = readdir(dir))) {
- if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
- continue;
- string fullPath = dirPath + ent->d_name;
- if (fullPath.size() > 5 && fullPath.substr(fullPath.size() - 5) == ".json") {
- remove(fullPath.c_str());
- }
- }
- closedir(dir);
- return TRUE;
- }
- LOCALCONFIG_API bool GetClientUniqEbusId(ResDataObject& res) {
- static string clientBusId;
- if (clientBusId.empty()) {
- ResDataObject obj;
- if (MakeUniqEbusId(obj)) {
- clientBusId = "Client" + string((const char*)obj);
- if (clientBusId.size() > 16) {
- clientBusId = clientBusId.substr(0, 16);
- }
- }
- }
- res = clientBusId.c_str();
- return !clientBusId.empty();
- }
- LOCALCONFIG_API bool GetFullDriverConfigFiles(ResDataObject& filelist) {
- return GetSpecificDriverConfigFiles(filelist, true);
- }
- LOCALCONFIG_API bool GetDriverConfigFiles(ResDataObject& filelist) {
- return GetSpecificDriverConfigFiles(filelist, false);
- }
- LOCALCONFIG_API bool GetSpecificDriverConfigFiles(ResDataObject& filelist, bool FullDriverList) {
- filelist.clear();
- ResDataObject PathList;
- DWORD pathCount = FullDriverList ?
- GetFullDriverConfigPathEx(PathList) :
- GetDriverConfigPathEx(PathList);
- for (DWORD i = 0; i < pathCount; i++) {
- string configPath = (const char*)PathList.GetKey(i);
- vector<string> files;
- if (FindSubFiles(configPath, files)) {
- for (const auto& file : files) {
- // 检查是否为XML文件
- string filenameLow = file;
- transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(), ::tolower);
- if (filenameLow.size() > 4 && filenameLow.substr(filenameLow.size() - 4) == ".xml") {
- filelist.add(file.c_str(), PathList[i]);
- }
- }
- }
- }
- return filelist.size() > 0;
- }
- LOCALCONFIG_API bool GetDriverProcInfo(const char* pConfigFilepath, ResDataObject& DriverProcInfo) {
- ResDataObject procPath;
- if (!GetDriverProcessPath(pConfigFilepath, procPath)) {
- return false;
- }
- string WorkPath = GetFileDirectory(string(pConfigFilepath));
- WorkPath = GetFileDirectory(WorkPath);
- WorkPath += "/";
- DriverProcInfo.add("WorkPath", WorkPath.c_str());
- DriverProcInfo.add("DriverPath", procPath);
- DriverProcInfo.add("DriverConfigPath", pConfigFilepath);
- // 读取配置中的Console设置
- ResDataObject Context;
- if (Context.loadFile(pConfigFilepath)) {
- string Console = (const char*)Context["CONFIGURATION"]["Console"];
- DriverProcInfo.add("ShowWindow", (Console != "OFF"));
- string AutoLoad = (const char*)Context["CONFIGURATION"]["AutoLoad"];
- DriverProcInfo.add("AutoLoad", (AutoLoad != "OFF"));
- }
- else {
- DriverProcInfo.add("ShowWindow", true);
- DriverProcInfo.add("AutoLoad", true);
- }
- return true;
- }
- LOCALCONFIG_API bool GetLogPatternResource(ResDataObject& Pattern) {
- Pattern.clear();
- ResDataObject configFile;
- if (!configFile.loadFile(GetLogConfigPath())) {
- return false;
- }
- ResDataObject config = configFile["CONFIGURATION"];
- ResDataObject val;
- if (TryGetValue(config, "LEVEL", val)) {
- Pattern.add("LEVEL", (int)val);
- }
- else {
- Pattern.add("LEVEL", 3);
- }
- if (TryGetValue(config, "MAXFILESIZE", val)) {
- Pattern.add("MAXFILESIZE", ((int)val) * 1024 * 1024);
- }
- else {
- Pattern.add("MAXFILESIZE", 10 * 1024 * 1024);
- }
- if (TryGetValue(config, "MAXBACKUPCOUNT", val)) {
- Pattern.add("MAXBACKUPCOUNT", ((int)val));
- }
- else {
- Pattern.add("MAXBACKUPCOUNT", 1);
- }
- if (TryGetValue(config, "MAXTIMEPERIOD", val)) {
- Pattern.add("MAXTIMEPERIOD", ((int)val));
- }
- else {
- Pattern.add("MAXTIMEPERIOD", 0);
- }
- if (TryGetValue(config, "FILENAME", val)) {
- Pattern.add("FILENAME", ((int)val));
- }
- else {
- Pattern.add("FILENAME", 0);
- }
- if (TryGetValue(config, "FUNCTIONNAME", val)) {
- Pattern.add("FUNCTIONNAME", ((int)val));
- }
- else {
- Pattern.add("FUNCTIONNAME", 0);
- }
- if (TryGetValue(config, "LINE", val)) {
- Pattern.add("LINE", ((int)val));
- }
- else {
- Pattern.add("LINE", 0);
- }
- if (TryGetValue(config, "DATE", val)) {
- Pattern.add("DATE", ((int)val));
- }
- else {
- Pattern.add("DATE", 0);
- }
- if (TryGetValue(config, "PROC", val)) {
- Pattern.add("PROC", ((int)val));
- }
- else {
- Pattern.add("PROC", 0);
- }
- if (TryGetValue(config, "THREAD", val)) {
- Pattern.add("THREAD", ((int)val));
- }
- else {
- Pattern.add("THREAD", 0);
- }
- if (TryGetValue(config, "CACHE", val)) {
- Pattern.add("CACHE", ((int)val));
- }
- else {
- Pattern.add("CACHE", 1);
- }
- return true;
- }
- LOCALCONFIG_API bool GetShareMemSettings(DWORD& BigBlockSize, DWORD& BigBlockCount,
- DWORD& SmallBlockSize, DWORD& SmallBlockCount) {
- try {
- ResDataObject val;
- val = tryGetLocalOptions("BigBlockSize");
- BigBlockSize = (DWORD)val;
- val = tryGetLocalOptions("BigBlockCount");
- BigBlockCount = (DWORD)val;
- val = tryGetLocalOptions("SmallBlockSize");
- SmallBlockSize = (DWORD)val;
- val = tryGetLocalOptions("SmallBlockCount");
- SmallBlockCount = (DWORD)val;
- return true;
- }
- catch (...) {
- BigBlockSize = 1048576; // 1MB
- BigBlockCount = 10;
- SmallBlockSize = 4096; // 4KB
- SmallBlockCount = 100;
- return false;
- }
- }
- LOCALCONFIG_API bool GetShareMemLogLevel(int& LogLevel) {
- try {
- ResDataObject val = tryGetLocalOptions("LogLevel");
- LogLevel = (int)val;
- return true;
- }
- catch (...) {
- LogLevel = 3;
- return false;
- }
- }
|