DriverManager.cpp 27 KB

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