#pragma once // 下列 ifdef 块是创建使从 DLL 导出更简单的 // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 LOGICDEVICE_EXPORTS // 符号编译的。在使用此 DLL 的 // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将 // LOGICDEVICE_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的 // 符号视为是被导出的。 #ifndef LOGICDEVICE_EXPORTS #ifdef _WIN64 #ifdef _DEBUG #pragma comment(lib, "LogicDeviceX64D.lib") #else #pragma comment(lib, "LogicDeviceX64.lib") #endif #else #ifdef _DEBUG #pragma comment(lib, "LogicDeviceD.lib") #else #pragma comment(lib, "LogicDevice.lib") #endif #endif #endif #ifdef LOGICDEVICE_EXPORTS #define LOGICDEVICE_API __declspec(dllexport) #else #define LOGICDEVICE_API __declspec(dllimport) #endif #include #include "Definitions.h" #include "ResDataObject.h" #include "logger.h" #include "DPC.h" #include "CcosLock.h" #include #include "ccosfilehandle.h" #include "CcosThread.h" #include "MsgQueue.h" #include #pragma warning(disable:4251) //STL 模板引起的warning //#ifndef GUID //typedef struct _GUID { // unsigned int Data1; // unsigned int Data2; // unsigned int Data3; // char Data4[8]; //} GUID; //#endif typedef enum _SyslogLevel { Syslog_Debug = 0, Syslog_Information = 1, Syslog_Warning = 2, Syslog_Recoverable = 3, Syslog_Error = 4, Syslog_Fatal = 5, Syslog_Telegram = 6, Syslog_XReset = 7, Syslog_MAX }SYSLOGLEVEL; #define SYSLOG_DEBUG(Code,format,...) SystemLog(Syslog_Debug,Code,format,__VA_ARGS__) #define SYSLOG_INFO(Code,format,...) SystemLog(Syslog_Information,Code,format,__VA_ARGS__) #define SYSLOG_WARNING(Code,format,...) SystemLog(Syslog_Warning,Code,format,__VA_ARGS__) #define SYSLOG_ERROR(Code,format,...) SystemLog(Syslog_Error,Code,format,__VA_ARGS__) #define SYSLOG_FATAL(Code,format,...) SystemLog(Syslog_Fatal,Code,format,__VA_ARGS__) class LogicDevice; //class ccos_mqtt_connection; using ccos_mqtt_callback = std::function; //typedef int MQTT_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* message); using mqtt_topic_list = std::list; using ccos_mqtt_connection = std::tuple; //四元组,mqtt客户端/给mqtt的标准回调函数/消息钩子//客户端名称 const int MQTT_CLT_ID = 0; const int MQTT_TIPIC_LIST_ID = 1; const int MSG_HOOK_ID = 2; const int CLINET_ID_ID = 3; const int USER_MSG_CAKBCK_ID = 4; const int CONNECTED_HANDLE_ID = 5; const int CONN_SEND_LOCK_ID = 6; using ccos_mqtt_msg_filter_func = std::function; using ccos_mqtt_msg_filter = std::tuple; const int FILTER_FUNC_ID = 0; const int FILTER_HANDLE_ID = 1; const int FILTER_RES_OBJ_ID = 2; /// /// 逻辑设备命令收发接口 /// class LOGICDEVICE_API LogicDeviceSysIF { protected: LogicDevice *m_pLogicDev; public: LogicDeviceSysIF(void); virtual ~LogicDeviceSysIF(void); //init void SetLogicDevice(LogicDevice *p); LogicDevice* GetLogicDevice(); //Command In and Out //notify from lower layer /// /// notify from lower layer,设备上行命令,发送端为逻辑设备 /// /// /// virtual RET_STATUS HW_ACTION CmdFromLogicDev(ResDataObject PARAM_IN *pCmd); //notify to lower layer /// /// notify to lower layer,设备命令下行,接收端为逻辑设备 /// /// /// virtual RET_STATUS SYSTEM_CALL CmdToLogicDev(ResDataObject PARAM_IN *pCmd); }; using ccos_topic_filter = std::function; class LOGICDEVICE_API LogicDevice : public CcosLock, public Thread_Base { //friend class LogicDeviceSysIF; LogicDeviceSysIF *m_pSysLogic; protected: char szPad2[128]; ccos_mqtt_connection* m_pMqttConntion; char szPad[128]; string m_strMqttUser; string m_strMqttPassword; string m_strServer; string m_strServerPort; bool m_bMqttUseSSL; ccos_topic_filter m_topicFilter; string m_strClientID; //MQTT 的客户端连接的ID,一般用DEVICE的逻辑ID来表示 VendorID_ProductID_SerialID_LogicID; std::vector m_strSubTopics; //要订阅的主题列表 bool m_bIsTreeRoot; string m_strEBusRoot; std::vector m_subDevices; //按照原有Ebus组织形式的树 string m_strCCOSRoot; std::vector m_subCcosDevices; //按照新的树形结构的树 LogicDevice* m_pParent; //父节点 string m_strDevicePath; //相对CCOS root 根节点的 当前设备的名称,用于构建主题URI,必须以/开头 ResDataObject m_Actions; MsgQueue* m_pPacketReceivedQue; Logger *m_pLogger; char *m_pDevInstance; ResDataObject *m_pResErrorList; HANDLE m_EvtNotify;//此事件的目的是当本地有事件发生,而这事件需要要往上传递,需要驱动线程主动下来GET. HANDLE m_SemphRequest; //请求到了 DriverDPC *m_pDrvDPC;//设备对象的HOST RET_STATUS SystemLog(SYSLOGLEVEL Level, const char *pCode, const char* fmt, ...); RET_STATUS IoSystemLog(int Level, const char* pCode, const char* pContext,size_t ContextSize, const char* pAppId = ""); bool CheckFeatureLicense(const char *pszFeatureId); virtual RET_STATUS HW_ACTION AddErrorMessageBase(const char* DevInstance, const char* Code, int &Level, const char* ResInfo, const char* Description, int nMessageType = 0, const char* pAppId = ""); void SubscribeActions(); void PacketArrived(ResDataObject* pRequest); public: LogicDevice(void); virtual ~LogicDevice(void); virtual bool OnStartThread() override; virtual bool OnEndThread() override; virtual bool Exec(void) override; RET_STATUS AddEbusChildren(LogicDevice* pChild, const char* szDevPath); RET_STATUS AddCcosChildren(LogicDevice* pChild, const char* szCcosDevPath); LogicDevice* GetEbusChild(const char* szEbusDevPath); LogicDevice* GetCcosChild(const char* szCcosDevPath); string& GetRootPath() { return m_strEBusRoot; } string& GetCcosRootPath() { return m_strCCOSRoot; } void SetTreeRoot(bool bIsTreeRoot) { m_bIsTreeRoot = bIsTreeRoot; } bool IsTreeRoot() { return m_bIsTreeRoot; } void SetParent(LogicDevice* pParent) { m_pParent = pParent; } void SetEbusRoot(const char* pszEbusRoot) { m_strEBusRoot = pszEbusRoot; } bool HasAction(const char* pszAction) { return m_Actions.GetFirstOf(pszAction) >= 0; } bool GetActions(ResDataObject& resAction); void NotifyParent(ResDataObject* NotifyData, const char* pszTopic, ccos_mqtt_connection* hConnection=nullptr); //1. init part void SYSTEM_CALL SetSysLogicDevice(LogicDeviceSysIF PARAM_IN *pLogic); void SYSTEM_CALL SetLogHandle(Logger PARAM_IN *pLogger); //2. common part Logger *GetLogHandle(); void SetDrvDPC(DriverDPC *pDPC); DriverDPC *GetDrvDPC(); void SetClientRootID(const char* pszEBusRoot, const char* pszCCOSRoot); //设置 设备URI根 路径 ,包括EBUS和CCOS两个 virtual void OnSetClientID(); void SetTopicFilter(ccos_topic_filter filter); virtual void SubscribeSelf(); //所有子类必须使用 SubscribeTopic 订阅自身topic及资源topic,如果不知道需要DPC代订阅 void SubScribeTopic(const char* pszTopic, bool bSubscribe = true); void HelpSubscribeTopic(const char* pszTopic); //代注册接口,注册在主MQTT连接上 ccos_mqtt_connection* CreateConnection(const char* pszClientID, ccos_mqtt_callback onmsg); HANDLE GetEvtHandle(); void NotifyDrvThread(); virtual RET_STATUS SYSTEM_CALL EvtProcedure();//m_EvtNotify事件发生后,由上层驱动线程进行调入. virtual int SYSTEM_CALL GetDevice_Thread_Priority();//return THREAD_PRIORITY_NONE //get device type,returns device type GUID virtual bool SYSTEM_CALL GetDeviceType(GUID &DevType) = 0; virtual void SYSTEM_CALL CompleteInit(); virtual void SYSTEM_CALL CompleteUnInit(); //get device resource /// /// 获取设备资源 get device resource /// /// /// virtual RET_STATUS SYSTEM_CALL GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource); //3. command //normal sync routine,Request to device and response from device /// /// 设备的命令响应接口 normal sync routine,Request to device and response from device /// /// /// /// virtual RET_STATUS SYSTEM_CALL Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse) = 0; //upper layer ------------------------------> lower layer //app -> service -> framework -> logicdevice -> hw //notify to lower layer /// /// 发送给设备的命令 只是通知吗?notify to lower layer /// /// /// virtual RET_STATUS SYSTEM_CALL CmdToLogicDev(ResDataObject PARAM_IN *pCmd) = 0; //处理来自客户的请求 virtual RET_STATUS ProcessSubscribeMsg(ResDataObject* pCmd); virtual RET_STATUS ProcessSubscribeRequest(ResDataObject* pCmd); virtual RET_STATUS ProcessRequest(ResDataObject* pCmd, PACKET_CMD cmd); virtual RET_STATUS ProcessResponse(ResDataObject* pCmd, PACKET_CMD cmd); //需要子类自己实现自己的订阅处理 virtual RET_STATUS ProcessNotify(ResDataObject* pCmd, PACKET_CMD cmd); //notify from lower layer /// /// 设备侧 来的 命令?只可能是通知吗?notify from lower layer /// /// /// RET_STATUS HW_ACTION CmdFromLogicDev(ResDataObject PARAM_IN *pCmd); virtual RET_STATUS HW_ACTION AddErrorMessageUnicode(const char* DevInstance, const char* Code, int &Level, const wchar_t* ResInfo, const wchar_t* Description, int nMessageType = 0); virtual RET_STATUS HW_ACTION AddErrorMessage(const char* DevInstance, const char* Code, int &Level, const char* ResInfo, const char* Description, int nMessageType = 0, const char* pAppId = ""); virtual RET_STATUS HW_ACTION AddErrorMessage(const char* Code, int &Level, const char* ResInfo, int nMessageType = 0, const char* pAppId = ""); virtual RET_STATUS HW_ACTION DelErrorMessage(const char* DevInstance, const char* Code, int &Level, const char* ResInfo, const char* Description, int nMessageType = 0); virtual RET_STATUS HW_ACTION DelErrorMessage(const char* Code, int &Level, const char* ResInfo, int nMessageType = 0); static ccos_mqtt_connection* NewConnection(const char* pszServer, const char* pszServerPort, const char* pszUserName, const char* pszPassword, const char* pszClientID, ccos_mqtt_callback onmsg); }; //创建额外连接,需要提供回调函数 LOGICDEVICE_API ccos_mqtt_connection* NewConnection(const char* pszClientID, ccos_mqtt_callback onmsg, DWORD dwOpenTimeout = 10000, bool async = false); LOGICDEVICE_API void CloseConnection(ccos_mqtt_connection* hConnection); //主动订阅主题 LOGICDEVICE_API int SubscribeTopic(ccos_mqtt_connection* hConnection, const char* pszTopic, bool isShare = false); //主题订阅取消 LOGICDEVICE_API int UnSubscribe(ccos_mqtt_connection* hConnection, const char* pszTopic); //往指定主题发送CCOS协议包整包,使用临时创建连接,仅发送,不接收 LOGICDEVICE_API int PublishMsg(ResDataObject* pCmd, const char* pszTopic, const char* pszSenderName); //往指定主题发送CCOS协议包整包,使用已创建的连接发送 LOGICDEVICE_API int PublishAction(ResDataObject* pAction, const char* pszTopic, ccos_mqtt_connection* hConnection = nullptr); //往指定主题发送Action包,携带参数,并指定答复的Topic,异步模式处理Resp,需要在对应连接的回调中处理 LOGICDEVICE_API int PublishAction(const char* pAction, ResDataObject* pContext, const char* pszTopic, const char* pszRespTopic, ccos_mqtt_connection* hConnection = nullptr); //使用现有连接 往指定主题发送Action包,携带参数,并指定答复的Topic,同步等待resp,超时没收到应答返回失败,复用链接时须小心,该函数会 //LOGICDEVICE_API int ActionAndRespWithConnection(ccos_mqtt_connection* hConnection, const char* pAction, ResDataObject& req, ResDataObject* pContext, const char* pszTopic, const char* pszRespTopic, ResDataObject& resObj, // DWORD dwWaitTime); //使用现有连接 往指定主题发送Action包,携带参数,复用连接订阅的默认Topic,通过提供的消息钩子 等待resp,超时没收到应答返回失败, LOGICDEVICE_API int ActionAndRespWithConnDefalt(ccos_mqtt_connection* hConnection, const char* pAction, ResDataObject& req, ResDataObject* pContext, const char* pszTopic, ResDataObject& resObj, DWORD dwWaitTime); //往指定主题发送Action包,携带参数,并指定答复的Topic,同步等待resp,临时创建连接,并等待应答,超时没收到应答返回失败 LOGICDEVICE_API int ActionAndResp(const char* pAction, ResDataObject* pContext, const char* pszTopic, const char* pszRespTopic, ResDataObject& resObj, DWORD dwWaitTime, const char* pszSenderName);