LocalConfig.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. // LocalConfig.cpp
  2. // 添加必要的头文件
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <arpa/inet.h>
  10. #include <ifaddrs.h>
  11. #include <netinet/in.h>
  12. #include <net/if.h>
  13. #include <sys/ioctl.h>
  14. #include <time.h>
  15. #include <errno.h>
  16. #include <algorithm>
  17. #include <cctype>
  18. #include <cstring>
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cstdarg>
  22. #include <map>
  23. #include <sys/types.h>
  24. #include <netpacket/packet.h> // 包含 sockaddr_ll 定义
  25. #include "common_api.h"
  26. #include "LocalConfig.h"
  27. #include "Crc64.h"
  28. #include <iostream>
  29. // 全局变量定义
  30. // ================================================================
  31. CCOS_PROC_TYPE g_ConfigMode = CCOS_PROC_MASTER; // 默认进程角色为主进程
  32. vector<string> g_DriverConfigFilepath; // 驱动配置文件路径列表
  33. string g_LocalBusId = ""; // 本地总线ID
  34. string g_MajorID = ""; // 主ID
  35. string g_VendorID = ""; // 供应商ID
  36. string g_ProductID = ""; // 产品ID
  37. string g_SerialID = ""; // 序列号ID
  38. string g_DeviceType = ""; // 设备类型
  39. pthread_mutex_t m_Section = PTHREAD_MUTEX_INITIALIZER; // 线程互斥锁
  40. ResDataObject g_resUid2TypeMap; // GUID到类型的映射
  41. /**
  42. * 递归创建目录
  43. * @param path 目录路径
  44. * @return 成功返回true,失败返回false
  45. */
  46. bool CreateDirectoryLinux(const char* path) {
  47. char tmp[256];
  48. char* p = NULL;
  49. size_t len;
  50. // 复制路径并确保不以斜杠结尾
  51. snprintf(tmp, sizeof(tmp), "%s", path);
  52. len = strlen(tmp);
  53. if (len > 0 && tmp[len - 1] == '/') {
  54. tmp[len - 1] = '\0';
  55. }
  56. // 逐级创建目录
  57. for (p = tmp + 1; *p; p++) {
  58. if (*p == '/') {
  59. *p = '\0'; // 临时截断路径
  60. // 创建中间目录(忽略已存在的目录)
  61. if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
  62. return false;
  63. }
  64. *p = '/'; // 恢复路径
  65. }
  66. }
  67. // 创建最终目录
  68. return mkdir(tmp, 0755) == 0 || errno == EEXIST;
  69. }
  70. /**
  71. * 检查文件或目录是否存在
  72. * @param path 路径
  73. * @return 存在返回true,否则返回false
  74. */
  75. bool FileExists(const char* path) {
  76. struct stat info;
  77. return stat(path, &info) == 0;
  78. }
  79. /**
  80. * 从完整路径中提取文件名
  81. * @param fullPath 完整路径
  82. * @return 文件名
  83. */
  84. string GetFileName(const string& fullPath) {
  85. size_t pos = fullPath.find_last_of("/");
  86. return (pos != string::npos) ? fullPath.substr(pos + 1) : fullPath;
  87. }
  88. /**
  89. * 从完整路径中提取目录路径
  90. * @param fullPath 完整路径
  91. * @return 目录路径
  92. */
  93. string GetFileDirectory(const string& fullPath) {
  94. size_t pos = fullPath.find_last_of("/");
  95. return (pos != string::npos) ? fullPath.substr(0, pos) : "";
  96. }
  97. /**
  98. * 获取当前时间戳(毫秒)
  99. * @return 毫秒时间戳
  100. */
  101. DWORD GetTickCount() {
  102. struct timespec ts;
  103. // 使用单调时钟获取时间
  104. clock_gettime(CLOCK_MONOTONIC, &ts);
  105. return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
  106. }
  107. /**
  108. * 获取所有网络接口的MAC地址
  109. * @param vMacAddress 输出MAC地址列表
  110. * @return 找到的MAC地址数量
  111. */
  112. LOCALCONFIG_API DWORD GetMacAddress(vector<string>& vMacAddress) {
  113. struct ifaddrs* ifaddr, * ifa;
  114. vMacAddress.clear();
  115. if (getifaddrs(&ifaddr) == -1) {
  116. return 0;
  117. }
  118. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  119. if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_PACKET)
  120. continue;
  121. // 使用正确的类型转换
  122. struct sockaddr_ll* s = (struct sockaddr_ll*)ifa->ifa_addr;
  123. // 检查 MAC 地址长度
  124. if (s->sll_halen < 6) continue;
  125. // 格式化 MAC 地址
  126. char mac[32];
  127. snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
  128. s->sll_addr[0], s->sll_addr[1], s->sll_addr[2],
  129. s->sll_addr[3], s->sll_addr[4], s->sll_addr[5]);
  130. vMacAddress.push_back(mac);
  131. }
  132. freeifaddrs(ifaddr);
  133. return vMacAddress.size();
  134. }
  135. // 主要功能函数实现
  136. // ================================================================
  137. /**
  138. * 添加驱动配置文件路径并解析EBus ID
  139. */
  140. LOCALCONFIG_API bool AddDriverConfig(const char* pszDriverConfig) {
  141. // 构造完整路径: 配置路径 + 文件名
  142. string FullPath = GetDriverConfigPath().encode();
  143. std::cout << "[DEBUG] Construct the complete path: configuration path : " << FullPath << std::endl;
  144. FullPath += pszDriverConfig;
  145. std::cout << "[DEBUG] Construct the complete path: configuration path + file name: " << FullPath << std::endl;
  146. // 添加到全局列表
  147. g_DriverConfigFilepath.push_back(FullPath);
  148. // 解析驱动文件获取EBus ID
  149. ResDataObject BusId, DriverPath;
  150. bool ret = GetDriverEbusId(FullPath.c_str(), BusId, DriverPath);
  151. if (ret) {
  152. g_LocalBusId = (const char*)BusId;
  153. }
  154. return ret;
  155. }
  156. /**
  157. * 设置配置模式并加载GUID映射
  158. */
  159. LOCALCONFIG_API void SetConfigMode(CCOS_PROC_TYPE Master) {
  160. g_ConfigMode = Master;
  161. string mapConf = GetProcessDirectory() + "/LocalConfig.xml";
  162. ResDataObject config;
  163. // 加载并解析配置文件
  164. if (config.loadFile(mapConf.c_str())) {
  165. // 查找GUID到类型的映射
  166. int Idx = config["CONFIGURATION"].GetFirstOf("GUID2Type");
  167. if (Idx >= 0) {
  168. g_resUid2TypeMap = config["CONFIGURATION"][Idx];
  169. }
  170. }
  171. }
  172. /**
  173. * 获取本地IP地址
  174. */
  175. LOCALCONFIG_API ResDataObject& getLocalIpAddress() {
  176. static ResDataObject mn_LocalIpAddress;
  177. struct ifaddrs* ifaddr, * ifa;
  178. // 获取网络接口列表
  179. if (getifaddrs(&ifaddr) != 0) {
  180. return mn_LocalIpAddress;
  181. }
  182. // 遍历网络接口
  183. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  184. // 只处理IPv4接口
  185. if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
  186. continue;
  187. // 获取IP地址
  188. void* tmpAddrPtr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr;
  189. char addressBuffer[INET_ADDRSTRLEN];
  190. inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
  191. // 跳过回环地址
  192. if (strcmp(addressBuffer, "127.0.0.1") != 0) {
  193. mn_LocalIpAddress = addressBuffer;
  194. break;
  195. }
  196. }
  197. // 释放资源
  198. freeifaddrs(ifaddr);
  199. return mn_LocalIpAddress;
  200. }
  201. /**
  202. * 获取当前可执行文件名
  203. */
  204. LOCALCONFIG_API ResDataObject& GetModuleTitle() {
  205. static ResDataObject mn_ModuleTitle;
  206. char path[1024] = { 0 };
  207. // 通过 /proc/self/exe 获取可执行文件路径
  208. ssize_t count = readlink("/proc/self/exe", path, sizeof(path) - 1);
  209. if (count > 0) {
  210. path[count] = '\0';
  211. char* last_slash = strrchr(path, '/');
  212. if (last_slash) {
  213. // 提取文件名
  214. mn_ModuleTitle = last_slash + 1;
  215. }
  216. }
  217. return mn_ModuleTitle;
  218. }
  219. /**
  220. * 获取主机名(大写形式)作为机器ID
  221. */
  222. LOCALCONFIG_API ResDataObject& getLocalMachineId() {
  223. static ResDataObject mn_LocalMachineId;
  224. char hostname[256] = { 0 };
  225. // 获取主机名
  226. if (gethostname(hostname, sizeof(hostname) - 1) == 0) {
  227. string filenameBig = hostname;
  228. // 转换为大写
  229. transform(filenameBig.begin(), filenameBig.end(), filenameBig.begin(), ::toupper);
  230. mn_LocalMachineId = filenameBig.c_str();
  231. }
  232. return mn_LocalMachineId;
  233. }
  234. /**
  235. * 创建工作路径
  236. */
  237. LOCALCONFIG_API ResDataObject MakeWorkPath(ResDataObject& probeInfo, ResDataObject& Connection,
  238. ResDataObject& ProcPath, bool ForWork) {
  239. ResDataObject ResRet;
  240. ResDataObject major, product, serial;
  241. // 获取必要字段
  242. if (!TryGetValue(probeInfo, "MajorID", major) ||
  243. !TryGetValue(probeInfo, "ProductID", product)) {
  244. return ResRet; // 返回空对象
  245. }
  246. // 处理序列号
  247. if (!TryGetValue(probeInfo, "SerialID", serial) || strlen((const char*)serial) == 0) {
  248. probeInfo.add("SerialID", GetTickCount());
  249. serial = (const char*)probeInfo["SerialID"];
  250. }
  251. // 确定目录结构
  252. vector<string> folders;
  253. folders.push_back(ForWork ? "WorkPath" : "Logs");
  254. folders.push_back((const char*)major);
  255. folders.push_back((const char*)product);
  256. folders.push_back((const char*)serial);
  257. // 构建完整路径
  258. string workpath = string((const char*)ProcPath) + "/";
  259. for (size_t i = 0; i < folders.size(); i++) {
  260. string dirPath = workpath + folders[i];
  261. // 创建目录(如果不存在)
  262. if (!FileExists(dirPath.c_str()) && !CreateDirectoryLinux(dirPath.c_str())) {
  263. return ResRet; // 创建失败返回空对象
  264. }
  265. workpath += folders[i] + "/";
  266. }
  267. // 返回构建的路径
  268. ResRet = workpath.c_str();
  269. return ResRet;
  270. }
  271. /**
  272. * 创建设备路径
  273. */
  274. LOCALCONFIG_API ResDataObject MakeDevicePath(ResDataObject& probeInfo, ResDataObject& Connection,
  275. ResDataObject& SubPath) {
  276. ResDataObject resRet;
  277. ResDataObject major, minor, vendor, product, serial;
  278. // 获取所有必要字段
  279. if (!TryGetValue(probeInfo, "MajorID", major) ||
  280. !TryGetValue(probeInfo, "MinorID", minor) ||
  281. !TryGetValue(probeInfo, "VendorID", vendor) ||
  282. !TryGetValue(probeInfo, "ProductID", product)) {
  283. return resRet;
  284. }
  285. // 处理序列号
  286. if (!TryGetValue(probeInfo, "SerialID", serial) || strlen((const char*)serial) == 0) {
  287. probeInfo.add("SerialID", GetTickCount());
  288. serial = (const char*)probeInfo["SerialID"];
  289. }
  290. // 构建设备路径
  291. string devicepath = "/" + string((const char*)getLocalEbusId()) + "/" +
  292. string((const char*)major) + "/" +
  293. string((const char*)minor) + "/" +
  294. string((const char*)vendor) + "/" +
  295. string((const char*)product) + "/" +
  296. string((const char*)serial) + "/" +
  297. string((const char*)SubPath);
  298. resRet = devicepath.c_str();
  299. return resRet;
  300. }
  301. /**
  302. * 创建CCOS路径
  303. */
  304. LOCALCONFIG_API ResDataObject MakeCcosPath(ResDataObject& probeInfo, ResDataObject& Connection,
  305. ResDataObject& SubPath, bool isDriver) {
  306. ResDataObject resRet;
  307. ResDataObject major, vendor, product, serial;
  308. // 获取必要字段
  309. if (!TryGetValue(probeInfo, "MajorID", major) ||
  310. !TryGetValue(probeInfo, "VendorID", vendor) ||
  311. !TryGetValue(probeInfo, "ProductID", product)) {
  312. return resRet;
  313. }
  314. // 处理序列号
  315. if (!TryGetValue(probeInfo, "SerialID", serial) || strlen((const char*)serial) == 0) {
  316. probeInfo.add("SerialID", GetTickCount());
  317. serial = (const char*)probeInfo["SerialID"];
  318. }
  319. // 构建CCOS路径
  320. string devicepath = isDriver ? "CCOS/DRIVER/" : "CCOS/DEVICE/";
  321. devicepath += string((const char*)major) + "/" +
  322. string((const char*)vendor) + "/" +
  323. string((const char*)product) + "/" +
  324. string((const char*)serial);
  325. resRet = devicepath.c_str();
  326. return resRet;
  327. }
  328. /**
  329. * 获取驱动配置路径
  330. */
  331. LOCALCONFIG_API ResDataObject& GetDriverConfigPath() {
  332. static ResDataObject resRet;
  333. std::string ret = GetProcessDirectory() + "/DriverConfig/";
  334. std::cout << "[DEBUG] GetProcessDirectory() + / DriverConfig / : " << ret << std::endl;
  335. resRet = ret.c_str();
  336. //resRet = "/home/cxdz/code/diosproc/DriverConfig/";
  337. std::cout << "[DEBUG] resRet : " << resRet.encode() << std::endl;
  338. return resRet;
  339. }
  340. /**
  341. * 获取驱动EBus ID
  342. */
  343. LOCALCONFIG_API bool GetDriverEbusId(const char* pConfigFilepath, ResDataObject& BusId,
  344. ResDataObject& DriverPath, bool ForceUpdate) {
  345. ResDataObject Context;
  346. // 加载配置文件
  347. if (!Context.loadFile(pConfigFilepath)) {
  348. return false;
  349. }
  350. // 提取配置中的各个ID
  351. string absContext = ""; // 用于生成唯一ID的上下文字符串
  352. char HighFive[6] = { 0 }; // 存储ID的首字符
  353. // 提取并验证各个ID
  354. string MajorID = (const char*)Context["CONFIGURATION"]["MajorID"];
  355. string MinorID = (const char*)Context["CONFIGURATION"]["MinorID"];
  356. string VendorID = (const char*)Context["CONFIGURATION"]["VendorID"];
  357. string ProductID = (const char*)Context["CONFIGURATION"]["ProductID"];
  358. string SerialID = (const char*)Context["CONFIGURATION"]["SerialID"];
  359. string GUID = (const char*)Context["CONFIGURATION"]["GUID"];
  360. std::cout << "[DEBUG] Configuration IDs:" << std::endl;
  361. std::cout << "[DEBUG] MajorID: " << MajorID << std::endl;
  362. std::cout << "[DEBUG] MinorID: " << MinorID << std::endl;
  363. std::cout << "[DEBUG] VendorID: " << VendorID << std::endl;
  364. std::cout << "[DEBUG] ProductID: " << ProductID << std::endl;
  365. std::cout << "[DEBUG] SerialID: " << SerialID << std::endl;
  366. std::cout << "[DEBUG] GUID: " << GUID << std::endl;
  367. if (MajorID.empty() || MinorID.empty() || VendorID.empty() ||
  368. ProductID.empty() || SerialID.empty() || GUID.empty()) {
  369. return false;
  370. }
  371. // 更新全局ID缓存
  372. g_MajorID = MajorID;
  373. g_VendorID = VendorID;
  374. g_ProductID = ProductID;
  375. g_SerialID = SerialID;
  376. // 构建唯一性上下文
  377. absContext = "/" + MajorID + "/" + MinorID + "/" + VendorID + "/" + ProductID + "/" + SerialID + "/" + GUID;
  378. HighFive[0] = MajorID[0];
  379. HighFive[1] = MinorID[0];
  380. HighFive[2] = VendorID[0];
  381. HighFive[3] = ProductID[0];
  382. HighFive[4] = SerialID[0];
  383. // 尝试获取配置中的BusId
  384. string OnlyBusId = "";
  385. int BusIdsIndex = Context["CONFIGURATION"].GetFirstOf("BusId");
  386. if (BusIdsIndex >= 0) {
  387. OnlyBusId = (const char*)Context["CONFIGURATION"][BusIdsIndex];
  388. }
  389. // 如未配置BusId,则生成一个
  390. if (OnlyBusId.empty()) {
  391. // 使用CRC32生成唯一ID
  392. int Crc32res = GetCrc32(absContext.c_str(), absContext.size());
  393. OnlyBusId = "D" + string(HighFive) + FormatstdString("%08X", Crc32res);
  394. }
  395. // 构建设备路径
  396. string TempPath = "/" + OnlyBusId + absContext;
  397. BusId = OnlyBusId.c_str();
  398. DriverPath = TempPath.c_str();
  399. return true;
  400. }
  401. /**
  402. * 生成唯一EBus ID
  403. */
  404. LOCALCONFIG_API bool MakeUniqEbusId(ResDataObject& res) {
  405. static DWORD l_PrevTime = 0; // 上次生成ID的时间戳
  406. ResDataObject mId = getLocalMachineId(); // 获取机器ID
  407. vector<string> maclist; // MAC地址列表
  408. // 获取MAC地址
  409. if (GetMacAddress(maclist) == 0) {
  410. return false;
  411. }
  412. // 加锁确保线程安全
  413. pthread_mutex_lock(&m_Section);
  414. // 获取唯一时间戳(避免重复)
  415. DWORD CurTime = GetTickCount();
  416. while (CurTime == l_PrevTime) {
  417. usleep(1000); // 休眠1毫秒
  418. CurTime = GetTickCount();
  419. }
  420. l_PrevTime = CurTime;
  421. // 释放锁
  422. pthread_mutex_unlock(&m_Section);
  423. // 构建唯一字符串
  424. string crcstr = (const char*)mId;
  425. for (size_t i = 0; i < maclist.size(); i++) {
  426. crcstr += maclist[i];
  427. }
  428. crcstr += FormatstdString("%08X", l_PrevTime); // 时间戳
  429. crcstr += FormatstdString("%08X", getpid()); // 进程ID
  430. crcstr += FormatstdString("%08X", (unsigned long)pthread_self()); // 线程ID
  431. // 计算CRC32作为唯一ID
  432. int crc = GetCrc32(crcstr.c_str(), crcstr.size());
  433. res = FormatstdString("%08X", crc).c_str();
  434. return true;
  435. }
  436. /**
  437. * 获取设备类型字符串
  438. */
  439. LOCALCONFIG_API string& GetMajorID() {
  440. // 从本地配置获取缺失的ID
  441. if (g_MajorID.empty()) {
  442. ResDataObject val = tryGetLocalOptions("MajorID");
  443. g_MajorID = (const char*)val;
  444. }
  445. if (g_VendorID.empty()) {
  446. ResDataObject val = tryGetLocalOptions("VendorID");
  447. g_VendorID = (const char*)val;
  448. }
  449. if (g_ProductID.empty()) {
  450. ResDataObject val = tryGetLocalOptions("ProductID");
  451. g_ProductID = (const char*)val;
  452. }
  453. if (g_SerialID.empty()) {
  454. ResDataObject val = tryGetLocalOptions("SerialID");
  455. g_SerialID = (const char*)val;
  456. }
  457. // 构建设备类型字符串
  458. g_DeviceType = g_MajorID + "_" + g_VendorID + "_" + g_ProductID + "_" + g_SerialID;
  459. return g_DeviceType;
  460. }
  461. /**
  462. * 获取设备ID
  463. */
  464. LOCALCONFIG_API string GetDeviceID() {
  465. // 确保所有ID已加载
  466. if (g_MajorID.empty() || g_VendorID.empty() || g_ProductID.empty() || g_SerialID.empty()) {
  467. GetMajorID(); // 加载ID
  468. }
  469. // 构建设备ID字符串
  470. if (!g_MajorID.empty()) {
  471. return g_MajorID + "/" + g_VendorID + "/" + g_ProductID + "/" + g_SerialID;
  472. }
  473. return "Channel/Channel"; // 默认值
  474. }
  475. // ================================================================
  476. LOCALCONFIG_API const char* GetDriverConfigFilepath(size_t idx) {
  477. return (idx < g_DriverConfigFilepath.size()) ? g_DriverConfigFilepath[idx].c_str() : nullptr;
  478. }
  479. LOCALCONFIG_API CCOS_PROC_TYPE GetConfigMode() {
  480. return g_ConfigMode;
  481. }
  482. LOCALCONFIG_API ResDataObject tryGetLocalOptions(const char* pKey) {
  483. string dir = GetProcessDirectory() + "/LocalConfig.xml";
  484. ResDataObject config, result;
  485. if (config.loadFile(dir.c_str())) {
  486. int Idx = config["CONFIGURATION"].GetFirstOf(pKey);
  487. if (Idx >= 0) {
  488. result = config["CONFIGURATION"][Idx];
  489. }
  490. }
  491. return result;
  492. }
  493. LOCALCONFIG_API ResDataObject& getLocalEbusId() {
  494. static ResDataObject mn_LocalEbusId;
  495. if (g_ConfigMode == CCOS_PROC_MASTER) {
  496. mn_LocalEbusId = g_LocalBusId.c_str();
  497. }
  498. else if (g_ConfigMode == CCOS_PROC_CHANNEL) {
  499. mn_LocalEbusId = "ccosChannel";
  500. }
  501. return mn_LocalEbusId;
  502. }
  503. LOCALCONFIG_API ResDataObject& getChannelEbusId() {
  504. static ResDataObject mn_LocalEbusId;
  505. mn_LocalEbusId = "ccosChannel";
  506. return mn_LocalEbusId;
  507. }
  508. LOCALCONFIG_API ResDataObject& getChannelRootpath() {
  509. static ResDataObject mn_ChannelId;
  510. mn_ChannelId = "/ccosChannel";
  511. return mn_ChannelId;
  512. }
  513. LOCALCONFIG_API ResDataObject& getLocalEbusRouterIp() {
  514. static ResDataObject mn_EbusRouterIp;
  515. mn_EbusRouterIp = tryGetLocalOptions("RouterIp");
  516. return mn_EbusRouterIp;
  517. }
  518. LOCALCONFIG_API ResDataObject& getLocalEbusPort() {
  519. static ResDataObject mn_EbusPort;
  520. mn_EbusPort = tryGetLocalOptions("Port");
  521. return mn_EbusPort;
  522. }
  523. static ResDataObject g_LogRootPath;
  524. LOCALCONFIG_API void setLogRootpath(const char* pszRootPath) {
  525. g_LogRootPath = pszRootPath;
  526. }
  527. LOCALCONFIG_API ResDataObject& getLogRootpath() {
  528. return g_LogRootPath;
  529. }
  530. LOCALCONFIG_API ResDataObject& getRootpath() {
  531. static ResDataObject mn_Rootpath;
  532. string rootpath = "/" + string((const char*)getLocalEbusId());
  533. mn_Rootpath = rootpath.c_str();
  534. return mn_Rootpath;
  535. }
  536. LOCALCONFIG_API bool getEbusLogFlag() {
  537. ResDataObject val = tryGetLocalOptions("EnableEbusLog");
  538. return (bool)val;
  539. }
  540. LOCALCONFIG_API bool getP2pFlag() {
  541. ResDataObject val = tryGetLocalOptions("P2pEnable");
  542. return (bool)val;
  543. }
  544. LOCALCONFIG_API ResDataObject getP2pServerIp() {
  545. return tryGetLocalOptions("P2pServer");
  546. }
  547. LOCALCONFIG_API ResDataObject getP2pRole() {
  548. return tryGetLocalOptions("P2pClient");
  549. }
  550. LOCALCONFIG_API ResDataObject& GetFullDriverConfigPath() {
  551. static ResDataObject resRet;
  552. string ret = GetProcessDirectory() + "/FullConfig/";
  553. resRet = ret.c_str();
  554. return resRet;
  555. }
  556. LOCALCONFIG_API DWORD GetFullDriverConfigPathEx(ResDataObject& PathList) {
  557. DWORD ret = 1;
  558. PathList.clear();
  559. // 根目录
  560. string dir = GetProcessDirectory();
  561. dir += "/FullConfig/";
  562. PathList.add(dir.c_str(), "");
  563. // 子版本目录
  564. string subdir = GetProcessDirectory();
  565. subdir += "/SubVersions/";
  566. map<string, vector<string>> templist;
  567. if (FindSubVersionDirs(subdir, templist)) {
  568. ret += (DWORD)templist.size();
  569. map<string, vector<string>>::iterator iter = templist.begin();
  570. while (iter != templist.end()) {
  571. for (DWORD i = 0; i < iter->second.size(); i++) {
  572. string filename = iter->second[i];
  573. // 在Linux下保持原始大小写
  574. // 构建路径
  575. string dirPath = filename + "/FullConfig/";
  576. PathList.add(dirPath.c_str(), iter->first.c_str());
  577. }
  578. ++iter;
  579. }
  580. }
  581. return ret;
  582. }
  583. LOCALCONFIG_API ResDataObject& GetFullDriverConfigPathX32() {
  584. static ResDataObject resRet;
  585. string ret = GetProcessDirectory() + "/win32/FullConfig/";
  586. resRet = ret.c_str();
  587. return resRet;
  588. }
  589. LOCALCONFIG_API ResDataObject& GetDriverConfigPathX32() {
  590. static ResDataObject resRet;
  591. string ret = GetProcessDirectory() + "/win32/DriverConfig/";
  592. resRet = ret.c_str();
  593. return resRet;
  594. }
  595. LOCALCONFIG_API DWORD GetDriverConfigPathEx(ResDataObject& PathList) {
  596. DWORD ret = 1;
  597. PathList.clear();
  598. // 根目录
  599. string dir = GetProcessDirectory();
  600. dir += "/DriverConfig/";
  601. PathList.add(dir.c_str(), "");
  602. // 子版本目录
  603. string subdir = GetProcessDirectory();
  604. subdir += "/SubVersions/";
  605. map<string, vector<string>> templist;
  606. if (FindSubVersionDirs(subdir, templist)) {
  607. ret += (DWORD)templist.size();
  608. map<string, vector<string>>::iterator iter = templist.begin();
  609. while (iter != templist.end()) {
  610. for (DWORD i = 0; i < iter->second.size(); i++) {
  611. string filename = iter->second[i];
  612. // 在Linux下保持原始大小写
  613. // 构建路径
  614. string dirPath = filename + "/DriverConfig/";
  615. PathList.add(dirPath.c_str(), iter->first.c_str());
  616. }
  617. ++iter;
  618. }
  619. }
  620. return ret;
  621. }
  622. LOCALCONFIG_API ResDataObject& GetLogConfigPath() {
  623. static ResDataObject resRet;
  624. string ret = GetProcessDirectory() + "/logconfig.xml";
  625. resRet = ret.c_str();
  626. return resRet;
  627. }
  628. LOCALCONFIG_API bool GetDriverProcessPath(const char* pConfigFilepath, ResDataObject& DriverProcessPath) {
  629. ResDataObject Context;
  630. if (!Context.loadFile(pConfigFilepath)) {
  631. return false;
  632. }
  633. string WorkPath = GetFileDirectory(GetFileDirectory(string(pConfigFilepath)));
  634. WorkPath += "/CcosProc";
  635. string NodeName = (const char*)Context["CONFIGURATION"]["ProcType"];
  636. if (NodeName == "MFC") {
  637. WorkPath += "MFC";
  638. }
  639. NodeName = (const char*)Context["CONFIGURATION"]["Arch"];
  640. if (NodeName == "64") {
  641. WorkPath += "X64";
  642. }
  643. WorkPath += ".exe";
  644. DriverProcessPath = WorkPath.c_str();
  645. return true;
  646. }
  647. LOCALCONFIG_API string GetModuleConfigrateFilePath(ResDataObject& resHardware) {
  648. string MajorID = resHardware["MajorID"].encode();
  649. string VendorID = resHardware["VendorID"].encode();
  650. string ProductID = resHardware["ProductID"].encode();
  651. string SerialID = resHardware["SerialID"].encode();
  652. string strPath = GetProcessDirectory();
  653. strPath += "/DriverDefine/Config/";
  654. strPath += MajorID + "_" + VendorID + "_" + ProductID + "_" + SerialID + ".json";
  655. return strPath;
  656. }
  657. LOCALCONFIG_API string GetModuleConfigrateFilePath(string devDioPath) {
  658. vector<string> parts;
  659. char buffer[1024];
  660. strcpy(buffer, devDioPath.c_str());
  661. char* token = strtok(buffer, "/");
  662. while (token) {
  663. parts.push_back(token);
  664. token = strtok(NULL, "/");
  665. }
  666. if (parts.size() >= 6) {
  667. string MajorID = parts[1];
  668. string VendorID = parts[3];
  669. string ProductID = parts[4];
  670. string SerialID = parts[5];
  671. string strPath = GetProcessDirectory();
  672. strPath += "/DriverDefine/Config/";
  673. strPath += MajorID + "_" + VendorID + "_" + ProductID + "_" + SerialID + ".json";
  674. return strPath;
  675. }
  676. return "";
  677. }
  678. LOCALCONFIG_API string GetCcosModuleConfigrateFilePath(string devDioPath, bool useTemplate) {
  679. vector<string> parts;
  680. char buffer[1024];
  681. strcpy(buffer, devDioPath.c_str());
  682. char* token = strtok(buffer, "/");
  683. while (token) {
  684. parts.push_back(token);
  685. token = strtok(NULL, "/");
  686. }
  687. if (parts.size() >= 6) {
  688. string MajorID = parts[2];
  689. string VendorID = parts[3];
  690. string ProductID = parts[4];
  691. string SerialID = parts[5];
  692. string strPath = GetProcessDirectory();
  693. strPath += useTemplate ? "/DriverDefine/" : "/DriverDefine/Config/";
  694. strPath += MajorID + "_" + VendorID + "_" + ProductID;
  695. strPath += useTemplate ? "" : "_" + SerialID;
  696. strPath += ".json";
  697. return strPath;
  698. }
  699. return "";
  700. }
  701. LOCALCONFIG_API string GetModuleDevicePath(ResDataObject& resHardware) {
  702. string MajorID = resHardware["MajorID"].encode();
  703. string VendorID = resHardware["VendorID"].encode();
  704. string ProductID = resHardware["ProductID"].encode();
  705. string SerialID = resHardware["SerialID"].encode();
  706. return "CCOS/DEVICE/" + MajorID + "/" + VendorID + "/" + ProductID + "/" + SerialID;
  707. }
  708. LOCALCONFIG_API BOOL ClearAllModuleConfigrate() {
  709. string dirPath = GetProcessDirectory() + "/DriverDefine/Config/";
  710. DIR* dir = opendir(dirPath.c_str());
  711. if (!dir) return FALSE;
  712. struct dirent* ent;
  713. while ((ent = readdir(dir))) {
  714. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  715. continue;
  716. string fullPath = dirPath + ent->d_name;
  717. if (fullPath.size() > 5 && fullPath.substr(fullPath.size() - 5) == ".json") {
  718. remove(fullPath.c_str());
  719. }
  720. }
  721. closedir(dir);
  722. return TRUE;
  723. }
  724. LOCALCONFIG_API bool GetClientUniqEbusId(ResDataObject& res) {
  725. static string clientBusId;
  726. if (clientBusId.empty()) {
  727. ResDataObject obj;
  728. if (MakeUniqEbusId(obj)) {
  729. clientBusId = "Client" + string((const char*)obj);
  730. if (clientBusId.size() > 16) {
  731. clientBusId = clientBusId.substr(0, 16);
  732. }
  733. }
  734. }
  735. res = clientBusId.c_str();
  736. return !clientBusId.empty();
  737. }
  738. LOCALCONFIG_API bool GetFullDriverConfigFiles(ResDataObject& filelist) {
  739. return GetSpecificDriverConfigFiles(filelist, true);
  740. }
  741. LOCALCONFIG_API bool GetDriverConfigFiles(ResDataObject& filelist) {
  742. return GetSpecificDriverConfigFiles(filelist, false);
  743. }
  744. LOCALCONFIG_API bool GetSpecificDriverConfigFiles(ResDataObject& filelist, bool FullDriverList) {
  745. filelist.clear();
  746. ResDataObject PathList;
  747. DWORD pathCount = FullDriverList ?
  748. GetFullDriverConfigPathEx(PathList) :
  749. GetDriverConfigPathEx(PathList);
  750. for (DWORD i = 0; i < pathCount; i++) {
  751. string configPath = (const char*)PathList.GetKey(i);
  752. vector<string> files;
  753. if (FindSubFiles(configPath, files)) {
  754. for (const auto& file : files) {
  755. // 检查是否为XML文件
  756. string filenameLow = file;
  757. transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(), ::tolower);
  758. if (filenameLow.size() > 4 && filenameLow.substr(filenameLow.size() - 4) == ".xml") {
  759. filelist.add(file.c_str(), PathList[i]);
  760. }
  761. }
  762. }
  763. }
  764. return filelist.size() > 0;
  765. }
  766. LOCALCONFIG_API bool GetDriverProcInfo(const char* pConfigFilepath, ResDataObject& DriverProcInfo) {
  767. ResDataObject procPath;
  768. if (!GetDriverProcessPath(pConfigFilepath, procPath)) {
  769. return false;
  770. }
  771. string WorkPath = GetFileDirectory(string(pConfigFilepath));
  772. WorkPath = GetFileDirectory(WorkPath);
  773. WorkPath += "/";
  774. DriverProcInfo.add("WorkPath", WorkPath.c_str());
  775. DriverProcInfo.add("DriverPath", procPath);
  776. DriverProcInfo.add("DriverConfigPath", pConfigFilepath);
  777. // 读取配置中的Console设置
  778. ResDataObject Context;
  779. if (Context.loadFile(pConfigFilepath)) {
  780. string Console = (const char*)Context["CONFIGURATION"]["Console"];
  781. DriverProcInfo.add("ShowWindow", (Console != "OFF"));
  782. string AutoLoad = (const char*)Context["CONFIGURATION"]["AutoLoad"];
  783. DriverProcInfo.add("AutoLoad", (AutoLoad != "OFF"));
  784. }
  785. else {
  786. DriverProcInfo.add("ShowWindow", true);
  787. DriverProcInfo.add("AutoLoad", true);
  788. }
  789. return true;
  790. }
  791. LOCALCONFIG_API bool GetLogPatternResource(ResDataObject& Pattern) {
  792. Pattern.clear();
  793. ResDataObject configFile;
  794. if (!configFile.loadFile(GetLogConfigPath())) {
  795. return false;
  796. }
  797. ResDataObject config = configFile["CONFIGURATION"];
  798. ResDataObject val;
  799. if (TryGetValue(config, "LEVEL", val)) {
  800. Pattern.add("LEVEL", (int)val);
  801. }
  802. else {
  803. Pattern.add("LEVEL", 3);
  804. }
  805. if (TryGetValue(config, "MAXFILESIZE", val)) {
  806. Pattern.add("MAXFILESIZE", ((int)val) * 1024 * 1024);
  807. }
  808. else {
  809. Pattern.add("MAXFILESIZE", 10 * 1024 * 1024);
  810. }
  811. if (TryGetValue(config, "MAXBACKUPCOUNT", val)) {
  812. Pattern.add("MAXBACKUPCOUNT", ((int)val));
  813. }
  814. else {
  815. Pattern.add("MAXBACKUPCOUNT", 1);
  816. }
  817. if (TryGetValue(config, "MAXTIMEPERIOD", val)) {
  818. Pattern.add("MAXTIMEPERIOD", ((int)val));
  819. }
  820. else {
  821. Pattern.add("MAXTIMEPERIOD", 0);
  822. }
  823. if (TryGetValue(config, "FILENAME", val)) {
  824. Pattern.add("FILENAME", ((int)val));
  825. }
  826. else {
  827. Pattern.add("FILENAME", 0);
  828. }
  829. if (TryGetValue(config, "FUNCTIONNAME", val)) {
  830. Pattern.add("FUNCTIONNAME", ((int)val));
  831. }
  832. else {
  833. Pattern.add("FUNCTIONNAME", 0);
  834. }
  835. if (TryGetValue(config, "LINE", val)) {
  836. Pattern.add("LINE", ((int)val));
  837. }
  838. else {
  839. Pattern.add("LINE", 0);
  840. }
  841. if (TryGetValue(config, "DATE", val)) {
  842. Pattern.add("DATE", ((int)val));
  843. }
  844. else {
  845. Pattern.add("DATE", 0);
  846. }
  847. if (TryGetValue(config, "PROC", val)) {
  848. Pattern.add("PROC", ((int)val));
  849. }
  850. else {
  851. Pattern.add("PROC", 0);
  852. }
  853. if (TryGetValue(config, "THREAD", val)) {
  854. Pattern.add("THREAD", ((int)val));
  855. }
  856. else {
  857. Pattern.add("THREAD", 0);
  858. }
  859. if (TryGetValue(config, "CACHE", val)) {
  860. Pattern.add("CACHE", ((int)val));
  861. }
  862. else {
  863. Pattern.add("CACHE", 1);
  864. }
  865. return true;
  866. }
  867. LOCALCONFIG_API bool GetShareMemSettings(DWORD& BigBlockSize, DWORD& BigBlockCount,
  868. DWORD& SmallBlockSize, DWORD& SmallBlockCount) {
  869. try {
  870. ResDataObject val;
  871. val = tryGetLocalOptions("BigBlockSize");
  872. BigBlockSize = (DWORD)val;
  873. val = tryGetLocalOptions("BigBlockCount");
  874. BigBlockCount = (DWORD)val;
  875. val = tryGetLocalOptions("SmallBlockSize");
  876. SmallBlockSize = (DWORD)val;
  877. val = tryGetLocalOptions("SmallBlockCount");
  878. SmallBlockCount = (DWORD)val;
  879. return true;
  880. }
  881. catch (...) {
  882. BigBlockSize = 1048576; // 1MB
  883. BigBlockCount = 10;
  884. SmallBlockSize = 4096; // 4KB
  885. SmallBlockCount = 100;
  886. return false;
  887. }
  888. }
  889. LOCALCONFIG_API bool GetShareMemLogLevel(int& LogLevel) {
  890. try {
  891. ResDataObject val = tryGetLocalOptions("LogLevel");
  892. LogLevel = (int)val;
  893. return true;
  894. }
  895. catch (...) {
  896. LogLevel = 3;
  897. return false;
  898. }
  899. }