DriverManager.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. #include <iostream>
  2. #include <stdio.h>
  3. //#include "scf.h"
  4. #include "CDI.h"
  5. #include "LocalConfig.h"
  6. #include "BusUnitClient.h"
  7. #include "LogicClient.h"
  8. #include "common_api.h"
  9. #include "PacketAnalizer.h"
  10. #include "AutoDmp.h"
  11. #define CRTDBG_MAP_ALLOC
  12. #include <stdlib.h>
  13. #include <chrono>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include "DeviceManager.h"
  17. const int maxDeviceNum = 30;
  18. int totalDeviceNum = 0;
  19. LogicDevice* g_AllDevice[maxDeviceNum];
  20. using namespace std;
  21. static std::mutex g_device_mutex;
  22. LDM_API const char* add_driver(char* driverName)
  23. {
  24. if (AddDriverConfig(driverName) == false)
  25. {
  26. printf("DriverConfig File not Right.%s \nexit in 3sec\n", driverName);
  27. sleep(3);
  28. return 0;
  29. }
  30. return GetMajorID().c_str();
  31. }
  32. const char* pszAllDevice = "";
  33. /// <summary>
  34. /// //返回以;分割的可以Open的设备URI列表
  35. /// </summary>
  36. /// <param name="rpcPort">本驱动实际监听的RPC端口号</param>
  37. /// <param name="httpPort">本驱动实际监听的HTTP端口号</param>
  38. /// <returns>以;分割的可以Open的设备URI列表字符串</returns>
  39. LDM_API const char* get_all_device(int rpcPort, int httpPort)
  40. {
  41. // 启用崩溃处理
  42. // 创建Linux专用配置
  43. static bool crashHandlerInitialized = false;
  44. if (!crashHandlerInitialized) {
  45. CrashConfig config;
  46. config.enableCoreDump = true;
  47. config.enableConsoleOutput = true;
  48. config.enableLogFile = true;
  49. config.enableBacktrace = true;
  50. config.enableDetailedInfo = true;
  51. config.crashDirectory = "/userdata/Devicelogs/crashes";
  52. config.maxCrashFiles = 10;
  53. config.maxBacktraceDepth = 128;
  54. config.asyncSafeOnly = false; // 允许详细日志记录
  55. // 使用配置启用崩溃处理
  56. ENABLE_CRASH_HANDLER_WITH_CONFIG(config);
  57. crashHandlerInitialized = true;
  58. }
  59. std::lock_guard<std::mutex> lock(g_device_mutex); // 添加互斥锁
  60. try {
  61. if (totalDeviceNum <= 0) {
  62. if (GetCommandDispathIF()->InitAs(CCOS_PROC_MASTER, (UINT64)getpid())) {
  63. pszAllDevice = GetCommandDispathIF()->GetAllDevice(
  64. (void**)(&g_AllDevice[0]), totalDeviceNum);
  65. }
  66. else {
  67. std::cerr << "Init failed in get_all_device" << std::endl;
  68. return "";
  69. }
  70. }
  71. else {
  72. pszAllDevice = GetCommandDispathIF()->GetAllDevice(
  73. (void**)(&g_AllDevice[0]), totalDeviceNum);
  74. }
  75. return pszAllDevice;
  76. }
  77. catch (const std::exception& e) {
  78. std::cerr << "C++ Exception in get_all_device: " << e.what() << std::endl;
  79. return "";
  80. }
  81. catch (...) {
  82. std::cerr << "Unknown C++ Exception in get_all_device" << std::endl;
  83. return "";
  84. }
  85. }
  86. void* CreateCgoStruct(std::vector<string>& argList)
  87. {
  88. int nLen = sizeof(void*) * argList.size();
  89. for (int x = 0; x < argList.size(); x++)
  90. {
  91. nLen += argList[x].length();
  92. nLen += 1;
  93. }
  94. void* ret = malloc(nLen);
  95. char* pString = ((char*)ret) + argList.size() * sizeof(void*);
  96. for (int x = 0; x < argList.size(); x++)
  97. {
  98. string msg = argList[x];
  99. ((char**)ret)[x] = pString;
  100. memcpy(pString, msg.c_str(), msg.length() + 1);
  101. pString += (msg.length() + 1);
  102. }
  103. return ret;
  104. }
  105. /// <summary>
  106. /// 打开设备
  107. /// </summary>
  108. /// <param name="devIdx"></param>
  109. /// <param name="devUri"></param>
  110. /// <param name="devGroup"></param>
  111. /// <param name="reply"></param>
  112. /// <returns></returns>
  113. LDM_API int open_device(int devIdx, char* devUri, char* devGroup, void** reply) {
  114. cout << "GRPC call: open_device - Entering function, devIdx=" << devIdx
  115. << ", devUri=" << (devUri ? devUri : "nullptr") << endl;
  116. if (devIdx >= totalDeviceNum) {
  117. cout << "open_device - devIdx exceeds total device count (" << totalDeviceNum << "), handling enumeration" << endl;
  118. if (devUri != nullptr && devUri[0] == 0) {
  119. cout << "open_device - Enumerating all devices" << endl;
  120. string msg = "Enum All Device ok.";
  121. string context = pszAllDevice;
  122. std::vector<string> allDev;
  123. SplitDeviceList(pszAllDevice, allDev);
  124. cout << "open_device - Parsed device count: " << allDev.size() << endl;
  125. ResDataObject rescontext, resList;
  126. rescontext.add("Driver", resList);
  127. rescontext.add("Device", resList);
  128. rescontext.add("Host", resList);
  129. for (int x = 0; x < allDev.size(); x++) {
  130. if (allDev[x].substr(0, 11) == "CCOS/DRIVER") {
  131. rescontext["Driver"].update(allDev[x].c_str(), "");
  132. cout << "open_device - Enumerated driver device: " << allDev[x] << endl;
  133. }
  134. else if (allDev[x].substr(0, 11) == "CCOS/DEVICE") {
  135. rescontext["Device"].update(allDev[x].c_str(), "");
  136. cout << "open_device - Enumerated physical device: " << allDev[x] << endl;
  137. }
  138. else if (allDev[x].substr(0, 9) == "CCOS/HOST") {
  139. rescontext["Host"].update(allDev[x].c_str(), "");
  140. cout << "open_device - Enumerated host device: " << allDev[x] << endl;
  141. }
  142. }
  143. context = rescontext.encode();
  144. std::vector<string> params;
  145. params.push_back(msg);
  146. params.push_back(context);
  147. *reply = CreateCgoStruct(params);
  148. cout << "open_device - Device enumeration completed, returning result" << endl;
  149. return RET_SUCCEED;
  150. }
  151. *reply = nullptr;
  152. cout << "open_device - Invalid devUri, cannot enumerate devices, returning 0" << endl;
  153. return 0;
  154. }
  155. LogicDevice* pDev = nullptr;
  156. int nScanStart, nScanEnd;
  157. if (devIdx >= 0) {
  158. cout << "open_device - Searching device with specified devIdx=" << devIdx << endl;
  159. pDev = g_AllDevice[devIdx];
  160. if (pDev == nullptr) {
  161. *reply = nullptr;
  162. cout << "open_device - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  163. return 0;
  164. }
  165. nScanStart = nScanEnd = devIdx;
  166. }
  167. else {
  168. nScanStart = 0;
  169. nScanEnd = totalDeviceNum - 1;
  170. cout << "open_device - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  171. }
  172. string devUriString = devUri ? devUri : "";
  173. cout << "open_device - Starting device matching, target devUri=" << devUriString << endl;
  174. for (; nScanStart <= nScanEnd; nScanStart++) {
  175. if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
  176. devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
  177. pDev = g_AllDevice[nScanStart];
  178. cout << "open_device - Matched device at index " << nScanStart << endl;
  179. break;
  180. }
  181. }
  182. if (pDev != nullptr) {
  183. cout << "open_device - Preparing to open device, calling DevOpen" << endl;
  184. ResDataObject resResource;
  185. RET_STATUS ret = pDev->DevOpen(devUri, devGroup, resResource);
  186. std::vector<string> params;
  187. string msg = "Open ok.";
  188. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  189. params.push_back(msg);
  190. params.push_back(context);
  191. *reply = CreateCgoStruct(params);
  192. cout << "open_device - Device opened successfully, return status=" << ret << endl;
  193. return RET_SUCCEED;
  194. }
  195. cout << "open_device - No matching device found, returning 0" << endl;
  196. return 0;
  197. }
  198. /// <summary>
  199. /// Close device
  200. /// </summary>
  201. LDM_API int close_device(int devIdx, char* devUri, char* devGroup, void** reply) {
  202. cout << "GRPC call: close_device - Entering function, devIdx=" << devIdx
  203. << ", devUri=" << (devUri ? devUri : "nullptr") << endl;
  204. if (devIdx >= totalDeviceNum) {
  205. *reply = nullptr;
  206. cout << "close_device - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
  207. return 0;
  208. }
  209. LogicDevice* pDev = nullptr;
  210. int nScanStart, nScanEnd;
  211. if (devIdx >= 0) {
  212. cout << "close_device - Searching device with specified devIdx=" << devIdx << endl;
  213. pDev = g_AllDevice[devIdx];
  214. if (pDev == nullptr) {
  215. *reply = nullptr;
  216. cout << "close_device - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  217. return 0;
  218. }
  219. nScanStart = nScanEnd = devIdx;
  220. }
  221. else {
  222. nScanStart = 0;
  223. nScanEnd = totalDeviceNum - 1;
  224. cout << "close_device - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  225. }
  226. string devUriString = devUri ? devUri : "";
  227. cout << "close_device - Starting device matching, target devUri=" << devUriString << endl;
  228. for (; nScanStart <= nScanEnd; nScanStart++) {
  229. if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
  230. devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
  231. pDev = g_AllDevice[nScanStart];
  232. cout << "close_device - Matched device at index " << nScanStart << endl;
  233. break;
  234. }
  235. }
  236. if (pDev != nullptr) {
  237. cout << "close_device - Preparing to close device, calling DevClose" << endl;
  238. ResDataObject resResource;
  239. RET_STATUS ret = pDev->DevClose(devUri, devGroup, resResource);
  240. std::vector<string> params;
  241. string msg = "Close ok.";
  242. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  243. params.push_back(msg);
  244. params.push_back(context);
  245. *reply = CreateCgoStruct(params);
  246. cout << "close_device - Device closed successfully, return status=" << ret << endl;
  247. return RET_SUCCEED;
  248. }
  249. cout << "close_device - No matching device found, returning 0" << endl;
  250. return 0;
  251. }
  252. /// <summary>
  253. /// Get device property
  254. /// </summary>
  255. LDM_API int device_get(int devIdx, char* devUri, char* devProperty, void** reply) {
  256. cout << "GRPC call: device_get - Entering function, devIdx=" << devIdx
  257. << ", devUri=" << (devUri ? devUri : "nullptr")
  258. << ", property=" << (devProperty ? devProperty : "nullptr") << endl;
  259. if (devIdx >= totalDeviceNum) {
  260. *reply = nullptr;
  261. cout << "device_get - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
  262. return 0;
  263. }
  264. LogicDevice* pDev = nullptr;
  265. int nScanStart, nScanEnd;
  266. if (devIdx >= 0) {
  267. cout << "device_get - Searching device with specified devIdx=" << devIdx << endl;
  268. pDev = g_AllDevice[devIdx];
  269. if (pDev == nullptr) {
  270. *reply = nullptr;
  271. cout << "device_get - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  272. return 0;
  273. }
  274. nScanStart = nScanEnd = devIdx;
  275. }
  276. else {
  277. nScanStart = 0;
  278. nScanEnd = totalDeviceNum - 1;
  279. cout << "device_get - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  280. }
  281. string devUriString = devUri ? devUri : "";
  282. cout << "device_get - Starting device matching, target devUri=" << devUriString << endl;
  283. for (; nScanStart <= nScanEnd; nScanStart++) {
  284. if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
  285. devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
  286. pDev = g_AllDevice[nScanStart];
  287. cout << "device_get - Matched device at index " << nScanStart << endl;
  288. break;
  289. }
  290. }
  291. if (pDev != nullptr) {
  292. cout << "device_get - Preparing to get property, calling DevGet (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
  293. ResDataObject resResource;
  294. RET_STATUS ret = pDev->DevGet(devUri, devProperty, resResource);
  295. std::vector<string> params;
  296. string msg = "Get ok.";
  297. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  298. params.push_back(devProperty);
  299. params.push_back(msg);
  300. params.push_back(context);
  301. *reply = CreateCgoStruct(params);
  302. cout << "device_get - Property retrieved successfully, return status=" << ret << endl;
  303. return ret;
  304. }
  305. cout << "device_get - No matching device found, returning 0" << endl;
  306. return 0;
  307. }
  308. /// <summary>
  309. /// Set device property
  310. /// </summary>
  311. LDM_API int device_set(int devIdx, char* devUri, char* devProperty, char* devValueSet, void** reply) {
  312. cout << "GRPC call: device_set - Entering function, devIdx=" << devIdx
  313. << ", devUri=" << (devUri ? devUri : "nullptr")
  314. << ", property=" << (devProperty ? devProperty : "nullptr")
  315. << ", value=" << (devValueSet ? devValueSet : "nullptr") << endl;
  316. if (devIdx >= totalDeviceNum) {
  317. *reply = nullptr;
  318. cout << "device_set - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
  319. return 0;
  320. }
  321. LogicDevice* pDev = nullptr;
  322. int nScanStart, nScanEnd;
  323. if (devIdx >= 0) {
  324. cout << "device_set - Searching device with specified devIdx=" << devIdx << endl;
  325. pDev = g_AllDevice[devIdx];
  326. if (pDev == nullptr) {
  327. *reply = nullptr;
  328. cout << "device_set - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  329. return 0;
  330. }
  331. nScanStart = nScanEnd = devIdx;
  332. }
  333. else {
  334. nScanStart = 0;
  335. nScanEnd = totalDeviceNum - 1;
  336. cout << "device_set - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  337. }
  338. string devUriString = devUri ? devUri : "";
  339. cout << "device_set - Starting device matching, target devUri=" << devUriString << endl;
  340. for (; nScanStart <= nScanEnd; nScanStart++) {
  341. if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
  342. devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
  343. pDev = g_AllDevice[nScanStart];
  344. cout << "device_set - Matched device at index " << nScanStart << endl;
  345. break;
  346. }
  347. }
  348. if (pDev != nullptr) {
  349. cout << "device_set - Preparing to set property, calling DevSet (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
  350. ResDataObject resResource;
  351. RET_STATUS ret = pDev->DevSet(devUri, devProperty, devValueSet, resResource);
  352. std::vector<string> params;
  353. string msg = "Set ok.";
  354. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  355. params.push_back(devProperty);
  356. params.push_back(msg);
  357. params.push_back(context);
  358. *reply = CreateCgoStruct(params);
  359. cout << "device_set - Property set successfully, return status=" << ret << endl;
  360. return ret;
  361. }
  362. cout << "device_set - No matching device found, returning 0" << endl;
  363. return 0;
  364. }
  365. /// <summary>
  366. /// Update device property
  367. /// </summary>
  368. LDM_API int device_update(int devIdx, char* devUri, char* devProperty, char* devValueUpdate, void** reply) {
  369. cout << "GRPC call: device_update - Entering function, devIdx=" << devIdx
  370. << ", devUri=" << (devUri ? devUri : "nullptr")
  371. << ", property=" << (devProperty ? devProperty : "nullptr") << endl;
  372. if (devIdx >= totalDeviceNum) {
  373. *reply = nullptr;
  374. cout << "device_update - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
  375. return 0;
  376. }
  377. LogicDevice* pDev = nullptr;
  378. int nScanStart, nScanEnd;
  379. if (devIdx >= 0) {
  380. cout << "device_update - Searching device with specified devIdx=" << devIdx << endl;
  381. pDev = g_AllDevice[devIdx];
  382. if (pDev == nullptr) {
  383. *reply = nullptr;
  384. cout << "device_update - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  385. return 0;
  386. }
  387. nScanStart = nScanEnd = devIdx;
  388. }
  389. else {
  390. nScanStart = 0;
  391. nScanEnd = totalDeviceNum - 1;
  392. cout << "device_update - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  393. }
  394. string devUriString = devUri ? devUri : "";
  395. cout << "device_update - Starting device matching, target devUri=" << devUriString << endl;
  396. for (; nScanStart <= nScanEnd; nScanStart++) {
  397. if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
  398. devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
  399. pDev = g_AllDevice[nScanStart];
  400. cout << "device_update - Matched device at index " << nScanStart << endl;
  401. break;
  402. }
  403. }
  404. if (pDev != nullptr) {
  405. cout << "device_update - Preparing to update property, calling DevUpdate (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
  406. ResDataObject resResource;
  407. RET_STATUS ret = pDev->DevUpdate(devUri, devProperty, devValueUpdate, resResource);
  408. std::vector<string> params;
  409. string msg = "Update ok.";
  410. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  411. params.push_back(devProperty);
  412. params.push_back(msg);
  413. params.push_back(context);
  414. *reply = CreateCgoStruct(params);
  415. cout << "device_update - Property updated successfully, return status=" << ret << endl;
  416. return ret;
  417. }
  418. cout << "device_update - No matching device found, returning 0" << endl;
  419. return 0;
  420. }
  421. /// <summary>
  422. /// Add device property
  423. /// </summary>
  424. LDM_API int device_add(int devIdx, char* devUri, char* devProperty, char* devValueAdd, void** reply) {
  425. cout << "GRPC call: device_add - Entering function, devIdx=" << devIdx
  426. << ", devUri=" << (devUri ? devUri : "nullptr")
  427. << ", property=" << (devProperty ? devProperty : "nullptr") << endl;
  428. if (devIdx >= totalDeviceNum) {
  429. *reply = nullptr;
  430. cout << "device_add - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
  431. return 0;
  432. }
  433. LogicDevice* pDev = nullptr;
  434. int nScanStart, nScanEnd;
  435. if (devIdx >= 0) {
  436. cout << "device_add - Searching device with specified devIdx=" << devIdx << endl;
  437. pDev = g_AllDevice[devIdx];
  438. if (pDev == nullptr) {
  439. *reply = nullptr;
  440. cout << "device_add - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  441. return 0;
  442. }
  443. nScanStart = nScanEnd = devIdx;
  444. }
  445. else {
  446. nScanStart = 0;
  447. nScanEnd = totalDeviceNum - 1;
  448. cout << "device_add - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  449. }
  450. string devUriString = devUri ? devUri : "";
  451. cout << "device_add - Starting device matching, target devUri=" << devUriString << endl;
  452. for (; nScanStart <= nScanEnd; nScanStart++) {
  453. if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
  454. devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
  455. pDev = g_AllDevice[nScanStart];
  456. cout << "device_add - Matched device at index " << nScanStart << endl;
  457. break;
  458. }
  459. }
  460. if (pDev != nullptr) {
  461. cout << "device_add - Preparing to add property, calling DevAdd (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
  462. ResDataObject resResource;
  463. RET_STATUS ret = pDev->DevAdd(devUri, devProperty, devValueAdd, resResource);
  464. std::vector<string> params;
  465. string msg = "Add ok.";
  466. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  467. params.push_back(devProperty);
  468. params.push_back(msg);
  469. params.push_back(context);
  470. *reply = CreateCgoStruct(params);
  471. cout << "device_add - Property added successfully, return status=" << ret << endl;
  472. return ret;
  473. }
  474. cout << "device_add - No matching device found, returning 0" << endl;
  475. return 0;
  476. }
  477. /// <summary>
  478. /// Delete device property
  479. /// </summary>
  480. LDM_API int device_del(int devIdx, char* devUri, char* devProperty, char* devValueDel, void** reply) {
  481. cout << "GRPC call: device_del - Entering function, devIdx=" << devIdx
  482. << ", devUri=" << (devUri ? devUri : "nullptr")
  483. << ", property=" << (devProperty ? devProperty : "nullptr") << endl;
  484. if (devIdx >= totalDeviceNum) {
  485. *reply = nullptr;
  486. cout << "device_del - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
  487. return 0;
  488. }
  489. LogicDevice* pDev = nullptr;
  490. int nScanStart, nScanEnd;
  491. if (devIdx >= 0) {
  492. cout << "device_del - Searching device with specified devIdx=" << devIdx << endl;
  493. pDev = g_AllDevice[devIdx];
  494. if (pDev == nullptr) {
  495. *reply = nullptr;
  496. cout << "device_del - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  497. return 0;
  498. }
  499. nScanStart = nScanEnd = devIdx;
  500. }
  501. else {
  502. nScanStart = 0;
  503. nScanEnd = totalDeviceNum - 1;
  504. cout << "device_del - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  505. }
  506. string devUriString = devUri ? devUri : "";
  507. cout << "device_del - Starting device matching, target devUri=" << devUriString << endl;
  508. for (; nScanStart <= nScanEnd; nScanStart++) {
  509. if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
  510. devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
  511. pDev = g_AllDevice[nScanStart];
  512. cout << "device_del - Matched device at index " << nScanStart << endl;
  513. break;
  514. }
  515. }
  516. if (pDev != nullptr) {
  517. cout << "device_del - Preparing to delete property, calling DevDel (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
  518. ResDataObject resResource;
  519. RET_STATUS ret = pDev->DevDel(devUri, devProperty, devValueDel, resResource);
  520. std::vector<string> params;
  521. string msg = "Del ok.";
  522. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  523. params.push_back(devProperty);
  524. params.push_back(msg);
  525. params.push_back(context);
  526. *reply = CreateCgoStruct(params);
  527. cout << "device_del - Property deleted successfully, return status=" << ret << endl;
  528. return ret;
  529. }
  530. cout << "device_del - No matching device found, returning 0" << endl;
  531. return 0;
  532. }
  533. /// <summary>
  534. /// Execute device action
  535. /// </summary>
  536. LDM_API int device_action(int devIdx, char* devUri, char* devFunction, char* devReqParams, void** reply) {
  537. cout << "GRPC call: device_action - Entering function, devIdx=" << devIdx
  538. << ", devUri=" << (devUri ? devUri : "nullptr")
  539. << ", function=" << (devFunction ? devFunction : "nullptr") << endl;
  540. if (devIdx >= totalDeviceNum) {
  541. *reply = nullptr;
  542. cout << "device_action - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
  543. return 0;
  544. }
  545. LogicDevice* pDev = nullptr;
  546. int nScanStart, nScanEnd;
  547. if (devIdx >= 0) {
  548. cout << "device_action - Searching device with specified devIdx=" << devIdx << endl;
  549. pDev = g_AllDevice[devIdx];
  550. if (pDev == nullptr) {
  551. *reply = nullptr;
  552. cout << "device_action - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  553. return 0;
  554. }
  555. nScanStart = nScanEnd = devIdx;
  556. }
  557. else {
  558. nScanStart = 0;
  559. nScanEnd = totalDeviceNum - 1;
  560. cout << "device_action - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  561. }
  562. string devUriString = devUri ? devUri : "";
  563. string absPath, rootPath;
  564. cout << "device_action - Starting device matching, target devUri=" << devUriString << endl;
  565. for (; nScanStart <= nScanEnd; nScanStart++) {
  566. absPath = g_AllDevice[nScanStart]->GetCcosAbstractPath();
  567. rootPath = g_AllDevice[nScanStart]->GetCcosRootPath();
  568. if (devUriString == absPath || devUriString == rootPath) {
  569. pDev = g_AllDevice[nScanStart];
  570. cout << "device_action - Exact match found at index " << nScanStart << endl;
  571. break;
  572. }
  573. if (absPath.find(devUriString) == 0 || rootPath.find(devUriString) == 0) {
  574. pDev = g_AllDevice[nScanStart];
  575. cout << "device_action - Prefix match found at index " << nScanStart << endl;
  576. break;
  577. }
  578. }
  579. if (pDev != nullptr) {
  580. cout << "device_action - Preparing to execute action, calling DevAction (function=" << (devFunction ? devFunction : "nullptr") << ")" << endl;
  581. ResDataObject resResource;
  582. RET_STATUS ret = pDev->DevAction(devUri, devFunction, devReqParams, resResource);
  583. std::vector<string> params;
  584. string msg = "Action ok.";
  585. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  586. params.push_back(devFunction);
  587. params.push_back(msg);
  588. params.push_back(context);
  589. *reply = CreateCgoStruct(params);
  590. cout << "device_action - Action executed successfully, return status=" << ret << endl;
  591. return ret;
  592. }
  593. cout << "device_action - No matching device found, returning 0" << endl;
  594. return 0;
  595. }
  596. /// <summary>
  597. /// Send message to device
  598. /// </summary>
  599. LDM_API int device_message(int devIdx, char* devUri, char* devTopic, char* devMessageValue, void** reply) {
  600. cout << "GRPC call: device_message - Entering function, devIdx=" << devIdx
  601. << ", devUri=" << (devUri ? devUri : "nullptr")
  602. << ", topic=" << (devTopic ? devTopic : "nullptr") << endl;
  603. if (devIdx >= totalDeviceNum) {
  604. *reply = nullptr;
  605. cout << "device_message - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
  606. return 0;
  607. }
  608. LogicDevice* pDev = nullptr;
  609. int nScanStart, nScanEnd;
  610. if (devIdx >= 0) {
  611. cout << "device_message - Searching device with specified devIdx=" << devIdx << endl;
  612. pDev = g_AllDevice[devIdx];
  613. if (pDev == nullptr) {
  614. *reply = nullptr;
  615. cout << "device_message - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
  616. return 0;
  617. }
  618. nScanStart = nScanEnd = devIdx;
  619. }
  620. else {
  621. nScanStart = 0;
  622. nScanEnd = totalDeviceNum - 1;
  623. cout << "device_message - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
  624. }
  625. string devUriString = devUri ? devUri : "";
  626. cout << "device_message - Starting device matching, target devUri=" << devUriString << endl;
  627. for (; nScanStart <= nScanEnd; nScanStart++) {
  628. if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
  629. devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
  630. pDev = g_AllDevice[nScanStart];
  631. cout << "device_message - Matched device at index " << nScanStart << endl;
  632. break;
  633. }
  634. }
  635. if (pDev != nullptr) {
  636. cout << "device_message - Preparing to send message, calling DevMessage (topic=" << (devTopic ? devTopic : "nullptr") << ")" << endl;
  637. ResDataObject resResource;
  638. RET_STATUS ret = pDev->DevMessage(devUri, devTopic, devMessageValue, resResource);
  639. std::vector<string> params;
  640. string msg = "Set ok.";
  641. string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
  642. params.push_back(devTopic);
  643. params.push_back(msg);
  644. params.push_back(context);
  645. *reply = CreateCgoStruct(params);
  646. cout << "device_message - Message sent successfully, return status=" << ret << endl;
  647. return ret;
  648. }
  649. cout << "device_message - No matching device found, returning 0" << endl;
  650. return 0;
  651. }
  652. //LDM_API int device_get() {
  653. // cout << "Call from GRPC " << "device_get" << endl;
  654. //
  655. // return 0;
  656. //}
  657. //
  658. //LDM_API int device_update() {
  659. // cout << "Call from GRPC " << "device_update" << endl;
  660. // return 0;
  661. //}
  662. //
  663. //LDM_API int device_add() {
  664. // cout << "Call from GRPC " << "device_add" << endl;
  665. // return 0;
  666. //}
  667. //
  668. //LDM_API int device_del() {
  669. // cout << "Call from GRPC " << "device_del" << endl;
  670. // return 0;
  671. //}
  672. //
  673. //LDM_API int device_action() {
  674. // cout << "Call from GRPC " << "device_action" << endl;
  675. // return 0;
  676. //}
  677. //
  678. //LDM_API int device_message() {
  679. // cout << "Call from GRPC " << "device_message" << endl;
  680. // return 0;
  681. //}
  682. /// <summary>
  683. /// 释放结构体内的每一个char*
  684. /// </summary>
  685. /// <param name="pPoirnt"></param>
  686. /// <param name="nItemCount"></param>
  687. /// <returns></returns>
  688. LDM_API int free_struct(void* pPoirnt, int nItemCount)
  689. {
  690. if(pPoirnt != nullptr)
  691. free(pPoirnt);
  692. return RET_SUCCEED;
  693. }