DriverManager.cpp 28 KB

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