#include #include //#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 #include #include #include #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 = ""; /// /// //返回以;分割的可以Open的设备URI列表 /// /// 本驱动实际监听的RPC端口号 /// 本驱动实际监听的HTTP端口号 /// 以;分割的可以Open的设备URI列表字符串 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& 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; } /// /// 打开设备 /// /// /// /// /// /// 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 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 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 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; } /// /// Close device /// 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 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; } /// /// Get device property /// 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 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; } /// /// Set device property /// 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 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; } /// /// Update device property /// 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 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; } /// /// Add device property /// 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 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; } /// /// Delete device property /// 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 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; } /// /// Execute device action /// 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 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; } /// /// Send message to device /// 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 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; //} /// /// 释放结构体内的每一个char* /// /// /// /// LDM_API int free_struct(void* pPoirnt, int nItemCount) { if(pPoirnt != nullptr) free(pPoirnt); return RET_SUCCEED; }