123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774 |
- #include <iostream>
- #include <stdio.h>
- //#include "scf.h"
- #include "CDI.h"
- #include "LocalConfig.h"
- #include "BusUnitClient.h"
- #include "LogicClient.h"
- #include "common_api.h"
- #include "PacketAnalizer.h"
- #include "AutoDmp.h"
- #define CRTDBG_MAP_ALLOC
- #include <stdlib.h>
- #include <chrono>
- #include <unistd.h>
- #include <string.h>
- #include "DeviceManager.h"
- const int maxDeviceNum = 30;
- int totalDeviceNum = 0;
- LogicDevice* g_AllDevice[maxDeviceNum];
- using namespace std;
- AutoDmp atdmp;
- LDM_API const char* add_driver(char* driverName)
- {
- if (AddDriverConfig(driverName) == false)
- {
- printf("DriverConfig File not Right.%s \nexit in 3sec\n", driverName);
- sleep(3);
- return 0;
- }
- return GetMajorID().c_str();
- }
- const char* pszAllDevice = "";
- /// <summary>
- /// //返回以;分割的可以Open的设备URI列表
- /// </summary>
- /// <param name="rpcPort">本驱动实际监听的RPC端口号</param>
- /// <param name="httpPort">本驱动实际监听的HTTP端口号</param>
- /// <returns>以;分割的可以Open的设备URI列表字符串</returns>
- LDM_API const char* get_all_device(int rpcPort, int httpPort)
- {
- if (totalDeviceNum <= 0)
- {
- if (GetCommandDispathIF()->InitAs(CCOS_PROC_MASTER, (UINT64)getpid()))
- {
- //启动成功,返回设备树
- return (pszAllDevice = GetCommandDispathIF()->GetAllDevice((void**)(&g_AllDevice[0]), totalDeviceNum));
-
- }
- else
- {
- cout << "Call from GRPC " << "get_all_device init failed." << endl;
- return "";
- }
- }
- else
- {
- return (pszAllDevice = GetCommandDispathIF()->GetAllDevice((void**)(&g_AllDevice[0]), totalDeviceNum));
- }
- return "";
- }
- void* CreateCgoStruct(std::vector<string>& argList)
- {
- int nLen = sizeof(void*) * argList.size();
- for (int x = 0; x < argList.size(); x++)
- {
- nLen += argList[x].length();
- nLen += 1;
- }
- void* ret = malloc(nLen);
-
- char* pString = ((char*)ret) + argList.size() * sizeof(void*);
- for (int x = 0; x < argList.size(); x++)
- {
- string msg = argList[x];
- ((char**)ret)[x] = pString;
- memcpy(pString, msg.c_str(), msg.length() + 1);
- pString += (msg.length() + 1);
- }
- return ret;
- }
- /// <summary>
- /// 打开设备
- /// </summary>
- /// <param name="devIdx"></param>
- /// <param name="devUri"></param>
- /// <param name="devGroup"></param>
- /// <param name="reply"></param>
- /// <returns></returns>
- LDM_API int open_device(int devIdx, char* devUri, char* devGroup, void** reply) {
- cout << "GRPC call: open_device - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- cout << "open_device - devIdx exceeds total device count (" << totalDeviceNum << "), handling enumeration" << endl;
- if (devUri != nullptr && devUri[0] == 0) {
- cout << "open_device - Enumerating all devices" << endl;
- string msg = "Enum All Device ok.";
- string context = pszAllDevice;
- std::vector<string> allDev;
- SplitDeviceList(pszAllDevice, allDev);
- cout << "open_device - Parsed device count: " << allDev.size() << endl;
- ResDataObject rescontext, resList;
- rescontext.add("Driver", resList);
- rescontext.add("Device", resList);
- rescontext.add("Host", resList);
- for (int x = 0; x < allDev.size(); x++) {
- if (allDev[x].substr(0, 11) == "CCOS/DRIVER") {
- rescontext["Driver"].update(allDev[x].c_str(), "");
- cout << "open_device - Enumerated driver device: " << allDev[x] << endl;
- }
- else if (allDev[x].substr(0, 11) == "CCOS/DEVICE") {
- rescontext["Device"].update(allDev[x].c_str(), "");
- cout << "open_device - Enumerated physical device: " << allDev[x] << endl;
- }
- else if (allDev[x].substr(0, 9) == "CCOS/HOST") {
- rescontext["Host"].update(allDev[x].c_str(), "");
- cout << "open_device - Enumerated host device: " << allDev[x] << endl;
- }
- }
- context = rescontext.encode();
- std::vector<string> params;
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "open_device - Device enumeration completed, returning result" << endl;
- return RET_SUCCEED;
- }
- *reply = nullptr;
- cout << "open_device - Invalid devUri, cannot enumerate devices, returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "open_device - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "open_device - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "open_device - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- cout << "open_device - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
- devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
- pDev = g_AllDevice[nScanStart];
- cout << "open_device - Matched device at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "open_device - Preparing to open device, calling DevOpen" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevOpen(devUri, devGroup, resResource);
- std::vector<string> params;
- string msg = "Open ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "open_device - Device opened successfully, return status=" << ret << endl;
- return RET_SUCCEED;
- }
- cout << "open_device - No matching device found, returning 0" << endl;
- return 0;
- }
- /// <summary>
- /// Close device
- /// </summary>
- LDM_API int close_device(int devIdx, char* devUri, char* devGroup, void** reply) {
- cout << "GRPC call: close_device - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- *reply = nullptr;
- cout << "close_device - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "close_device - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "close_device - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "close_device - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- cout << "close_device - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
- devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
- pDev = g_AllDevice[nScanStart];
- cout << "close_device - Matched device at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "close_device - Preparing to close device, calling DevClose" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevClose(devUri, devGroup, resResource);
- std::vector<string> params;
- string msg = "Close ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "close_device - Device closed successfully, return status=" << ret << endl;
- return RET_SUCCEED;
- }
- cout << "close_device - No matching device found, returning 0" << endl;
- return 0;
- }
- /// <summary>
- /// Get device property
- /// </summary>
- LDM_API int device_get(int devIdx, char* devUri, char* devProperty, void** reply) {
- cout << "GRPC call: device_get - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr")
- << ", property=" << (devProperty ? devProperty : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- *reply = nullptr;
- cout << "device_get - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "device_get - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "device_get - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "device_get - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- cout << "device_get - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
- devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
- pDev = g_AllDevice[nScanStart];
- cout << "device_get - Matched device at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "device_get - Preparing to get property, calling DevGet (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevGet(devUri, devProperty, resResource);
- std::vector<string> params;
- string msg = "Get ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(devProperty);
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "device_get - Property retrieved successfully, return status=" << ret << endl;
- return ret;
- }
- cout << "device_get - No matching device found, returning 0" << endl;
- return 0;
- }
- /// <summary>
- /// Set device property
- /// </summary>
- LDM_API int device_set(int devIdx, char* devUri, char* devProperty, char* devValueSet, void** reply) {
- cout << "GRPC call: device_set - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr")
- << ", property=" << (devProperty ? devProperty : "nullptr")
- << ", value=" << (devValueSet ? devValueSet : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- *reply = nullptr;
- cout << "device_set - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "device_set - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "device_set - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "device_set - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- cout << "device_set - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
- devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
- pDev = g_AllDevice[nScanStart];
- cout << "device_set - Matched device at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "device_set - Preparing to set property, calling DevSet (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevSet(devUri, devProperty, devValueSet, resResource);
- std::vector<string> params;
- string msg = "Set ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(devProperty);
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "device_set - Property set successfully, return status=" << ret << endl;
- return ret;
- }
- cout << "device_set - No matching device found, returning 0" << endl;
- return 0;
- }
- /// <summary>
- /// Update device property
- /// </summary>
- LDM_API int device_update(int devIdx, char* devUri, char* devProperty, char* devValueUpdate, void** reply) {
- cout << "GRPC call: device_update - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr")
- << ", property=" << (devProperty ? devProperty : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- *reply = nullptr;
- cout << "device_update - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "device_update - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "device_update - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "device_update - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- cout << "device_update - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
- devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
- pDev = g_AllDevice[nScanStart];
- cout << "device_update - Matched device at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "device_update - Preparing to update property, calling DevUpdate (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevUpdate(devUri, devProperty, devValueUpdate, resResource);
- std::vector<string> params;
- string msg = "Update ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(devProperty);
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "device_update - Property updated successfully, return status=" << ret << endl;
- return ret;
- }
- cout << "device_update - No matching device found, returning 0" << endl;
- return 0;
- }
- /// <summary>
- /// Add device property
- /// </summary>
- LDM_API int device_add(int devIdx, char* devUri, char* devProperty, char* devValueAdd, void** reply) {
- cout << "GRPC call: device_add - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr")
- << ", property=" << (devProperty ? devProperty : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- *reply = nullptr;
- cout << "device_add - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "device_add - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "device_add - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "device_add - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- cout << "device_add - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
- devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
- pDev = g_AllDevice[nScanStart];
- cout << "device_add - Matched device at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "device_add - Preparing to add property, calling DevAdd (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevAdd(devUri, devProperty, devValueAdd, resResource);
- std::vector<string> params;
- string msg = "Add ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(devProperty);
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "device_add - Property added successfully, return status=" << ret << endl;
- return ret;
- }
- cout << "device_add - No matching device found, returning 0" << endl;
- return 0;
- }
- /// <summary>
- /// Delete device property
- /// </summary>
- LDM_API int device_del(int devIdx, char* devUri, char* devProperty, char* devValueDel, void** reply) {
- cout << "GRPC call: device_del - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr")
- << ", property=" << (devProperty ? devProperty : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- *reply = nullptr;
- cout << "device_del - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "device_del - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "device_del - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "device_del - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- cout << "device_del - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
- devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
- pDev = g_AllDevice[nScanStart];
- cout << "device_del - Matched device at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "device_del - Preparing to delete property, calling DevDel (property=" << (devProperty ? devProperty : "nullptr") << ")" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevDel(devUri, devProperty, devValueDel, resResource);
- std::vector<string> params;
- string msg = "Del ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(devProperty);
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "device_del - Property deleted successfully, return status=" << ret << endl;
- return ret;
- }
- cout << "device_del - No matching device found, returning 0" << endl;
- return 0;
- }
- /// <summary>
- /// Execute device action
- /// </summary>
- LDM_API int device_action(int devIdx, char* devUri, char* devFunction, char* devReqParams, void** reply) {
- cout << "GRPC call: device_action - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr")
- << ", function=" << (devFunction ? devFunction : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- *reply = nullptr;
- cout << "device_action - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "device_action - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "device_action - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "device_action - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- string absPath, rootPath;
- cout << "device_action - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- absPath = g_AllDevice[nScanStart]->GetCcosAbstractPath();
- rootPath = g_AllDevice[nScanStart]->GetCcosRootPath();
- if (devUriString == absPath || devUriString == rootPath) {
- pDev = g_AllDevice[nScanStart];
- cout << "device_action - Exact match found at index " << nScanStart << endl;
- break;
- }
- if (absPath.find(devUriString) == 0 || rootPath.find(devUriString) == 0) {
- pDev = g_AllDevice[nScanStart];
- cout << "device_action - Prefix match found at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "device_action - Preparing to execute action, calling DevAction (function=" << (devFunction ? devFunction : "nullptr") << ")" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevAction(devUri, devFunction, devReqParams, resResource);
- std::vector<string> params;
- string msg = "Action ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(devFunction);
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "device_action - Action executed successfully, return status=" << ret << endl;
- return ret;
- }
- cout << "device_action - No matching device found, returning 0" << endl;
- return 0;
- }
- /// <summary>
- /// Send message to device
- /// </summary>
- LDM_API int device_message(int devIdx, char* devUri, char* devTopic, char* devMessageValue, void** reply) {
- cout << "GRPC call: device_message - Entering function, devIdx=" << devIdx
- << ", devUri=" << (devUri ? devUri : "nullptr")
- << ", topic=" << (devTopic ? devTopic : "nullptr") << endl;
- if (devIdx >= totalDeviceNum) {
- *reply = nullptr;
- cout << "device_message - devIdx exceeds total device count (" << totalDeviceNum << "), returning 0" << endl;
- return 0;
- }
- LogicDevice* pDev = nullptr;
- int nScanStart, nScanEnd;
- if (devIdx >= 0) {
- cout << "device_message - Searching device with specified devIdx=" << devIdx << endl;
- pDev = g_AllDevice[devIdx];
- if (pDev == nullptr) {
- *reply = nullptr;
- cout << "device_message - Device for devIdx=" << devIdx << " is null, returning 0" << endl;
- return 0;
- }
- nScanStart = nScanEnd = devIdx;
- }
- else {
- nScanStart = 0;
- nScanEnd = totalDeviceNum - 1;
- cout << "device_message - devIdx is negative, scanning all devices (" << nScanStart << " to " << nScanEnd << ")" << endl;
- }
- string devUriString = devUri ? devUri : "";
- cout << "device_message - Starting device matching, target devUri=" << devUriString << endl;
- for (; nScanStart <= nScanEnd; nScanStart++) {
- if (devUriString == g_AllDevice[nScanStart]->GetCcosAbstractPath() ||
- devUriString == g_AllDevice[nScanStart]->GetCcosRootPath()) {
- pDev = g_AllDevice[nScanStart];
- cout << "device_message - Matched device at index " << nScanStart << endl;
- break;
- }
- }
- if (pDev != nullptr) {
- cout << "device_message - Preparing to send message, calling DevMessage (topic=" << (devTopic ? devTopic : "nullptr") << ")" << endl;
- ResDataObject resResource;
- RET_STATUS ret = pDev->DevMessage(devUri, devTopic, devMessageValue, resResource);
- std::vector<string> params;
- string msg = "Set ok.";
- string context = resResource.size() > 0 ? resResource.encode() : (const char*)resResource;
- params.push_back(devTopic);
- params.push_back(msg);
- params.push_back(context);
- *reply = CreateCgoStruct(params);
- cout << "device_message - Message sent successfully, return status=" << ret << endl;
- return ret;
- }
- cout << "device_message - No matching device found, returning 0" << endl;
- return 0;
- }
- //LDM_API int device_get() {
- // cout << "Call from GRPC " << "device_get" << endl;
- //
- // return 0;
- //}
- //
- //LDM_API int device_update() {
- // cout << "Call from GRPC " << "device_update" << endl;
- // return 0;
- //}
- //
- //LDM_API int device_add() {
- // cout << "Call from GRPC " << "device_add" << endl;
- // return 0;
- //}
- //
- //LDM_API int device_del() {
- // cout << "Call from GRPC " << "device_del" << endl;
- // return 0;
- //}
- //
- //LDM_API int device_action() {
- // cout << "Call from GRPC " << "device_action" << endl;
- // return 0;
- //}
- //
- //LDM_API int device_message() {
- // cout << "Call from GRPC " << "device_message" << endl;
- // return 0;
- //}
- /// <summary>
- /// 释放结构体内的每一个char*
- /// </summary>
- /// <param name="pPoirnt"></param>
- /// <param name="nItemCount"></param>
- /// <returns></returns>
- LDM_API int free_struct(void* pPoirnt, int nItemCount)
- {
- if(pPoirnt != nullptr)
- free(pPoirnt);
- return RET_SUCCEED;
- }
|