LogicDevice.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #pragma once
  2. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  3. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 LOGICDEVICE_EXPORTS
  4. // 符号编译的。在使用此 DLL 的
  5. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  6. // LOGICDEVICE_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  7. // 符号视为是被导出的。
  8. #ifndef LOGICDEVICE_EXPORTS
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "LogicDeviceX64D.lib")
  12. #else
  13. #pragma comment(lib, "LogicDeviceX64.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "LogicDeviceD.lib")
  18. #else
  19. #pragma comment(lib, "LogicDevice.lib")
  20. #endif
  21. #endif
  22. #endif
  23. #ifdef LOGICDEVICE_EXPORTS
  24. #define LOGICDEVICE_API __declspec(dllexport)
  25. #else
  26. #define LOGICDEVICE_API __declspec(dllimport)
  27. #endif
  28. #include <Rpc.h>
  29. #include "Definitions.h"
  30. #include "ResDataObject.h"
  31. #include "logger.h"
  32. #include "DPC.h"
  33. #include "CcosLock.h"
  34. #include <functional>
  35. #include "ccosfilehandle.h"
  36. #include "CcosThread.h"
  37. #include "MsgQueue.h"
  38. #include <list>
  39. #pragma warning(disable:4251) //STL 模板引起的warning
  40. //#ifndef GUID
  41. //typedef struct _GUID {
  42. // unsigned int Data1;
  43. // unsigned int Data2;
  44. // unsigned int Data3;
  45. // char Data4[8];
  46. //} GUID;
  47. //#endif
  48. typedef enum _SyslogLevel {
  49. Syslog_Debug = 0,
  50. Syslog_Information = 1,
  51. Syslog_Warning = 2,
  52. Syslog_Recoverable = 3,
  53. Syslog_Error = 4,
  54. Syslog_Fatal = 5,
  55. Syslog_Telegram = 6,
  56. Syslog_XReset = 7,
  57. Syslog_MAX
  58. }SYSLOGLEVEL;
  59. #define SYSLOG_DEBUG(Code,format,...) SystemLog(Syslog_Debug,Code,format,__VA_ARGS__)
  60. #define SYSLOG_INFO(Code,format,...) SystemLog(Syslog_Information,Code,format,__VA_ARGS__)
  61. #define SYSLOG_WARNING(Code,format,...) SystemLog(Syslog_Warning,Code,format,__VA_ARGS__)
  62. #define SYSLOG_ERROR(Code,format,...) SystemLog(Syslog_Error,Code,format,__VA_ARGS__)
  63. #define SYSLOG_FATAL(Code,format,...) SystemLog(Syslog_Fatal,Code,format,__VA_ARGS__)
  64. class LogicDevice;
  65. //class ccos_mqtt_connection;
  66. using ccos_mqtt_callback = std::function<void(ResDataObject*, const char*, void*)>;
  67. //typedef int MQTT_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* message);
  68. using mqtt_topic_list = std::list<string>;
  69. using ccos_mqtt_connection = std::tuple<void*, mqtt_topic_list*, void*, const char*, ccos_mqtt_callback,HANDLE, CcosLock*>; //四元组,mqtt客户端/给mqtt的标准回调函数/消息钩子//客户端名称
  70. const int MQTT_CLT_ID = 0;
  71. const int MQTT_TIPIC_LIST_ID = 1;
  72. const int MSG_HOOK_ID = 2;
  73. const int CLINET_ID_ID = 3;
  74. const int USER_MSG_CAKBCK_ID = 4;
  75. const int CONNECTED_HANDLE_ID = 5;
  76. const int CONN_SEND_LOCK_ID = 6;
  77. using ccos_mqtt_msg_filter_func = std::function<bool(ResDataObject*)>;
  78. using ccos_mqtt_msg_filter = std::tuple<ccos_mqtt_msg_filter_func, HANDLE, ResDataObject*>;
  79. const int FILTER_FUNC_ID = 0;
  80. const int FILTER_HANDLE_ID = 1;
  81. const int FILTER_RES_OBJ_ID = 2;
  82. /// <summary>
  83. /// 逻辑设备命令收发接口
  84. /// </summary>
  85. class LOGICDEVICE_API LogicDeviceSysIF
  86. {
  87. protected:
  88. LogicDevice *m_pLogicDev;
  89. public:
  90. LogicDeviceSysIF(void);
  91. virtual ~LogicDeviceSysIF(void);
  92. //init
  93. void SetLogicDevice(LogicDevice *p);
  94. LogicDevice* GetLogicDevice();
  95. //Command In and Out
  96. //notify from lower layer
  97. /// <summary>
  98. /// notify from lower layer,设备上行命令,发送端为逻辑设备
  99. /// </summary>
  100. /// <param name="pCmd"></param>
  101. /// <returns></returns>
  102. virtual RET_STATUS HW_ACTION CmdFromLogicDev(ResDataObject PARAM_IN *pCmd);
  103. //notify to lower layer
  104. /// <summary>
  105. /// notify to lower layer,设备命令下行,接收端为逻辑设备
  106. /// </summary>
  107. /// <param name="pCmd"></param>
  108. /// <returns></returns>
  109. virtual RET_STATUS SYSTEM_CALL CmdToLogicDev(ResDataObject PARAM_IN *pCmd);
  110. };
  111. using ccos_topic_filter = std::function<void(ResDataObject* pTopics, ResDataObject*pReq)>;
  112. class LOGICDEVICE_API LogicDevice : public CcosLock, public Thread_Base
  113. {
  114. //friend class LogicDeviceSysIF;
  115. LogicDeviceSysIF *m_pSysLogic;
  116. protected:
  117. char szPad2[128];
  118. ccos_mqtt_connection* m_pMqttConntion;
  119. char szPad[128];
  120. string m_strMqttUser;
  121. string m_strMqttPassword;
  122. string m_strServer;
  123. string m_strServerPort;
  124. bool m_bMqttUseSSL;
  125. ccos_topic_filter m_topicFilter;
  126. string m_strClientID; //MQTT 的客户端连接的ID,一般用DEVICE的逻辑ID来表示 VendorID_ProductID_SerialID_LogicID;
  127. std::vector<string> m_strSubTopics; //要订阅的主题列表
  128. bool m_bIsTreeRoot;
  129. string m_strEBusRoot;
  130. std::vector<LogicDevice*> m_subDevices; //按照原有Ebus组织形式的树
  131. string m_strCCOSRoot;
  132. std::vector<LogicDevice*> m_subCcosDevices; //按照新的树形结构的树
  133. LogicDevice* m_pParent; //父节点
  134. string m_strDevicePath; //相对CCOS root 根节点的 当前设备的名称,用于构建主题URI,必须以/开头
  135. ResDataObject m_Actions;
  136. MsgQueue<ResDataObject>* m_pPacketReceivedQue;
  137. Logger *m_pLogger;
  138. char *m_pDevInstance;
  139. ResDataObject *m_pResErrorList;
  140. HANDLE m_EvtNotify;//此事件的目的是当本地有事件发生,而这事件需要要往上传递,需要驱动线程主动下来GET.
  141. HANDLE m_SemphRequest; //请求到了
  142. DriverDPC *m_pDrvDPC;//设备对象的HOST
  143. RET_STATUS SystemLog(SYSLOGLEVEL Level, const char *pCode, const char* fmt, ...);
  144. RET_STATUS IoSystemLog(int Level, const char* pCode, const char* pContext,size_t ContextSize, const char* pAppId = "");
  145. bool CheckFeatureLicense(const char *pszFeatureId);
  146. 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 = "");
  147. void SubscribeActions();
  148. void PacketArrived(ResDataObject* pRequest);
  149. public:
  150. LogicDevice(void);
  151. virtual ~LogicDevice(void);
  152. virtual bool OnStartThread() override;
  153. virtual bool OnEndThread() override;
  154. virtual bool Exec(void) override;
  155. RET_STATUS AddEbusChildren(LogicDevice* pChild, const char* szDevPath);
  156. RET_STATUS AddCcosChildren(LogicDevice* pChild, const char* szCcosDevPath);
  157. LogicDevice* GetEbusChild(const char* szEbusDevPath);
  158. LogicDevice* GetCcosChild(const char* szCcosDevPath);
  159. string& GetRootPath() { return m_strEBusRoot; }
  160. string& GetCcosRootPath() { return m_strCCOSRoot; }
  161. void SetTreeRoot(bool bIsTreeRoot) { m_bIsTreeRoot = bIsTreeRoot; }
  162. bool IsTreeRoot() { return m_bIsTreeRoot; }
  163. void SetParent(LogicDevice* pParent) { m_pParent = pParent; }
  164. void SetEbusRoot(const char* pszEbusRoot) { m_strEBusRoot = pszEbusRoot; }
  165. bool HasAction(const char* pszAction) { return m_Actions.GetFirstOf(pszAction) >= 0; }
  166. bool GetActions(ResDataObject& resAction);
  167. void NotifyParent(ResDataObject* NotifyData, const char* pszTopic, ccos_mqtt_connection* hConnection=nullptr);
  168. //1. init part
  169. void SYSTEM_CALL SetSysLogicDevice(LogicDeviceSysIF PARAM_IN *pLogic);
  170. void SYSTEM_CALL SetLogHandle(Logger PARAM_IN *pLogger);
  171. //2. common part
  172. Logger *GetLogHandle();
  173. void SetDrvDPC(DriverDPC *pDPC);
  174. DriverDPC *GetDrvDPC();
  175. void SetClientRootID(const char* pszEBusRoot, const char* pszCCOSRoot); //设置 设备URI根 路径 ,包括EBUS和CCOS两个
  176. virtual void OnSetClientID();
  177. void SetTopicFilter(ccos_topic_filter filter);
  178. virtual void SubscribeSelf(); //所有子类必须使用 SubscribeTopic 订阅自身topic及资源topic,如果不知道需要DPC代订阅
  179. void SubScribeTopic(const char* pszTopic, bool bSubscribe = true);
  180. void HelpSubscribeTopic(const char* pszTopic); //代注册接口,注册在主MQTT连接上
  181. ccos_mqtt_connection* CreateConnection(const char* pszClientID, ccos_mqtt_callback onmsg);
  182. HANDLE GetEvtHandle();
  183. void NotifyDrvThread();
  184. virtual RET_STATUS SYSTEM_CALL EvtProcedure();//m_EvtNotify事件发生后,由上层驱动线程进行调入.
  185. virtual int SYSTEM_CALL GetDevice_Thread_Priority();//return THREAD_PRIORITY_NONE
  186. //get device type,returns device type GUID
  187. virtual bool SYSTEM_CALL GetDeviceType(GUID &DevType) = 0;
  188. virtual void SYSTEM_CALL CompleteInit();
  189. virtual void SYSTEM_CALL CompleteUnInit();
  190. //get device resource
  191. /// <summary>
  192. /// 获取设备资源 get device resource
  193. /// </summary>
  194. /// <param name="pDeviceResource"></param>
  195. /// <returns></returns>
  196. virtual RET_STATUS SYSTEM_CALL GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource);
  197. //3. command
  198. //normal sync routine,Request to device and response from device
  199. /// <summary>
  200. /// 设备的命令响应接口 normal sync routine,Request to device and response from device
  201. /// </summary>
  202. /// <param name="pRequest"></param>
  203. /// <param name="pResponse"></param>
  204. /// <returns></returns>
  205. virtual RET_STATUS SYSTEM_CALL Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse) = 0;
  206. //upper layer ------------------------------> lower layer
  207. //app -> service -> framework -> logicdevice -> hw
  208. //notify to lower layer
  209. /// <summary>
  210. /// 发送给设备的命令 只是通知吗?notify to lower layer
  211. /// </summary>
  212. /// <param name="pCmd"></param>
  213. /// <returns></returns>
  214. virtual RET_STATUS SYSTEM_CALL CmdToLogicDev(ResDataObject PARAM_IN *pCmd) = 0;
  215. //处理来自客户的请求
  216. virtual RET_STATUS ProcessSubscribeMsg(ResDataObject* pCmd);
  217. virtual RET_STATUS ProcessSubscribeRequest(ResDataObject* pCmd);
  218. virtual RET_STATUS ProcessRequest(ResDataObject* pCmd, PACKET_CMD cmd);
  219. virtual RET_STATUS ProcessResponse(ResDataObject* pCmd, PACKET_CMD cmd);
  220. //需要子类自己实现自己的订阅处理
  221. virtual RET_STATUS ProcessNotify(ResDataObject* pCmd, PACKET_CMD cmd);
  222. //notify from lower layer
  223. /// <summary>
  224. /// 设备侧 来的 命令?只可能是通知吗?notify from lower layer
  225. /// </summary>
  226. /// <param name="pCmd"></param>
  227. /// <returns></returns>
  228. RET_STATUS HW_ACTION CmdFromLogicDev(ResDataObject PARAM_IN *pCmd);
  229. 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);
  230. 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 = "");
  231. virtual RET_STATUS HW_ACTION AddErrorMessage(const char* Code, int &Level, const char* ResInfo, int nMessageType = 0, const char* pAppId = "");
  232. virtual RET_STATUS HW_ACTION DelErrorMessage(const char* DevInstance, const char* Code, int &Level, const char* ResInfo, const char* Description, int nMessageType = 0);
  233. virtual RET_STATUS HW_ACTION DelErrorMessage(const char* Code, int &Level, const char* ResInfo, int nMessageType = 0);
  234. static ccos_mqtt_connection* NewConnection(const char* pszServer, const char* pszServerPort, const char* pszUserName, const char* pszPassword, const char* pszClientID, ccos_mqtt_callback onmsg);
  235. };
  236. //创建额外连接,需要提供回调函数
  237. LOGICDEVICE_API ccos_mqtt_connection* NewConnection(const char* pszClientID, ccos_mqtt_callback onmsg, DWORD dwOpenTimeout = 10000, bool async = false);
  238. LOGICDEVICE_API void CloseConnection(ccos_mqtt_connection* hConnection);
  239. //主动订阅主题
  240. LOGICDEVICE_API int SubscribeTopic(ccos_mqtt_connection* hConnection, const char* pszTopic, bool isShare = false);
  241. //主题订阅取消
  242. LOGICDEVICE_API int UnSubscribe(ccos_mqtt_connection* hConnection, const char* pszTopic);
  243. //往指定主题发送CCOS协议包整包,使用临时创建连接,仅发送,不接收
  244. LOGICDEVICE_API int PublishMsg(ResDataObject* pCmd, const char* pszTopic, const char* pszSenderName);
  245. //往指定主题发送CCOS协议包整包,使用已创建的连接发送
  246. LOGICDEVICE_API int PublishAction(ResDataObject* pAction, const char* pszTopic, ccos_mqtt_connection* hConnection = nullptr);
  247. //往指定主题发送Action包,携带参数,并指定答复的Topic,异步模式处理Resp,需要在对应连接的回调中处理
  248. LOGICDEVICE_API int PublishAction(const char* pAction, ResDataObject* pContext, const char* pszTopic, const char* pszRespTopic, ccos_mqtt_connection* hConnection = nullptr);
  249. //使用现有连接 往指定主题发送Action包,携带参数,并指定答复的Topic,同步等待resp,超时没收到应答返回失败,复用链接时须小心,该函数会
  250. //LOGICDEVICE_API int ActionAndRespWithConnection(ccos_mqtt_connection* hConnection, const char* pAction, ResDataObject& req, ResDataObject* pContext, const char* pszTopic, const char* pszRespTopic, ResDataObject& resObj,
  251. // DWORD dwWaitTime);
  252. //使用现有连接 往指定主题发送Action包,携带参数,复用连接订阅的默认Topic,通过提供的消息钩子 等待resp,超时没收到应答返回失败,
  253. LOGICDEVICE_API int ActionAndRespWithConnDefalt(ccos_mqtt_connection* hConnection, const char* pAction, ResDataObject& req, ResDataObject* pContext, const char* pszTopic, ResDataObject& resObj, DWORD dwWaitTime);
  254. //往指定主题发送Action包,携带参数,并指定答复的Topic,同步等待resp,临时创建连接,并等待应答,超时没收到应答返回失败
  255. LOGICDEVICE_API int ActionAndResp(const char* pAction, ResDataObject* pContext, const char* pszTopic, const char* pszRespTopic, ResDataObject& resObj,
  256. DWORD dwWaitTime, const char* pszSenderName);