mod_ScannerSet.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #ifndef _MOD_HSPSCANNER_H_
  2. #define _MOD_HSPSCANNER_H_
  3. #pragma once
  4. #include "modVer.h"
  5. #include "SpTest.h"
  6. #include "ScannerSetFSM.h"
  7. #include "EventCode.h"
  8. #include "ScannerSet_UserErrorCode.h"
  9. #include "HSPScanner_client_g.h"
  10. #include "MaintainWatcher_client_g.h"
  11. using namespace HSPScanner;
  12. using namespace ScannerSet;
  13. class CScannerEntity;
  14. class ScannerServerSession: public ScannerService_ServerSessionBase
  15. {
  16. public:
  17. ScannerServerSession(CScannerEntity *pEntity):m_pEntity(pEntity){}
  18. virtual ~ScannerServerSession(){}
  19. virtual void Handle_StartPreview(SpReqAnsContext<ScannerService_StartPreview_Req,
  20. ScannerService_StartPreview_Ans>::Pointer ctx);
  21. virtual void Handle_CancelPreview(SpReqAnsContext<ScannerService_CancelPreview_Req,
  22. ScannerService_CancelPreview_Ans>::Pointer ctx);
  23. virtual void Handle_ScanImage(SpReqAnsContext<ScannerService_ScanImage_Req,
  24. ScannerService_ScanImage_Ans>::Pointer ctx);
  25. virtual void Handle_SetWindowPos(SpReqAnsContext<ScannerService_SetWindowPos_Req,
  26. ScannerService_SetWindowPos_Ans>::Pointer ctx);
  27. virtual void Handle_SetProperty(SpReqAnsContext<ScannerService_SetProperty_Req,
  28. ScannerService_SetProperty_Ans>::Pointer ctx);
  29. virtual void Handle_ShowProperty(SpReqAnsContext<ScannerService_ShowProperty_Req,
  30. ScannerService_ShowProperty_Ans>::Pointer ctx);
  31. virtual void Handle_GetDevStatus(SpReqAnsContext<ScannerService_GetDevStatus_Req,
  32. ScannerService_GetDevStatus_Ans>::Pointer ctx);
  33. virtual void Handle_CrossTermCall(SpReqAnsContext<ScannerService_CrossTermCall_Req,
  34. ScannerService_CrossTermCall_Ans>::Pointer ctx);
  35. virtual void Handle_CrossTermInvokeInfo(SpOnewayCallContext<ScannerService_CrossTermInvokeInfo_Info>::Pointer ctx);
  36. virtual void Handle_GetDevInfo(SpReqAnsContext<ScannerService_GetDevInfo_Req,
  37. ScannerService_GetDevInfo_Ans>::Pointer ctx);
  38. virtual void Handle_GetOnlineCameras(SpReqAnsContext<ScannerService_GetOnlineCameras_Req, ScannerService_GetOnlineCameras_Ans>::Pointer ctx);
  39. virtual void Handle_Exit(SpOnewayCallContext<ScannerService_Exit_Info>::Pointer ctx);
  40. private:
  41. CScannerEntity *m_pEntity;
  42. };
  43. class HSPScannerClient : public HSPScannerService_ClientBase
  44. {
  45. public:
  46. HSPScannerClient(CScannerEntity *pEntity);
  47. };
  48. class PortableScannerClient : public PortableScannerService_ClientBase
  49. {
  50. public:
  51. PortableScannerClient(CScannerEntity *pEntity);
  52. };
  53. class CScannerEntity : public CEntityBase, public ILogListener, public IEntityStateListener
  54. {
  55. public:
  56. CScannerEntity() {}
  57. virtual ~CScannerEntity()
  58. {
  59. if(GetFunction()->HasPrivilege()) {
  60. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  61. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  62. pFuncPrivilege->UnregistEntityStateEvent(m_fsm.GetChildEntityName());
  63. }
  64. }
  65. virtual const char *GetEntityName() const { return "ScannerSet"; }
  66. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  67. ON_ENTITYT_TEST()
  68. virtual CServerSessionBase* OnNewSession(const char* , const char * )
  69. {
  70. return new ScannerServerSession(this);
  71. }
  72. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
  73. CSmartPointer<ITransactionContext> pTransactionContext)
  74. {
  75. ErrorCodeEnum erroCode = m_fsm.Init(this);
  76. GetFunction()->SubscribeLog(m_uuidMaintainWatcher, this,
  77. Log_Event, Severity_Middle, Error_IgnoreAll, -2, "MaintainWatcher");
  78. #if defined(RVC_OS_WIN)
  79. GetFunction()->SubscribeLog(m_uuidPortableScanner, this, Log_Error, Severity_High, Error_IgnoreAll, -1, "PortableScanner");
  80. #endif //RVC_OS_WIN
  81. pTransactionContext->SendAnswer(erroCode);
  82. }
  83. void OnStarted()
  84. {
  85. if(GetFunction()->HasPrivilege()) {
  86. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  87. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  88. pFuncPrivilege->RegistEntityStateEvent(m_fsm.GetChildEntityName(), this);
  89. } else {
  90. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cur entity has no priviledge!");
  91. }
  92. m_fsm.AfterInit();
  93. }
  94. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  95. {
  96. LOG_FUNCTION();
  97. #ifdef TWINKLE_LOCAL_DEBUG
  98. const UINT result = UniTest(this);
  99. if(result != 0) {
  100. LogError(Severity_High, Error_Unexpect, result, CSimpleStringA::Format("line: %u", result));
  101. }
  102. #endif
  103. SpReqAnsContext<ScannerService_StartPreview_Req,
  104. ScannerService_StartPreview_Ans>::Pointer ctx =
  105. new SpReqAnsContext<ScannerService_StartPreview_Req,
  106. ScannerService_StartPreview_Ans>(pTransactionContext);
  107. Preview(ctx);
  108. pTransactionContext->SendAnswer(Error_Succeed);
  109. }
  110. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  111. {
  112. Exit(NULL);
  113. pTransactionContext->SendAnswer(Error_Succeed);
  114. }
  115. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,
  116. CSmartPointer<ITransactionContext> pTransactionContext)
  117. {
  118. ErrorCodeEnum erroCode = m_fsm.OnExit();
  119. pTransactionContext->SendAnswer(erroCode);
  120. }
  121. void Preview(SpReqAnsContext<ScannerService_StartPreview_Req,
  122. ScannerService_StartPreview_Ans>::Pointer ctx)
  123. {
  124. StartPreviewEvent* pEvent = new StartPreviewEvent();
  125. pEvent->m_ctx = ctx;
  126. m_fsm.PostEventFIFO(pEvent);
  127. }
  128. void CancelPreview(SpReqAnsContext<ScannerService_CancelPreview_Req,
  129. ScannerService_CancelPreview_Ans>::Pointer ctx)
  130. {
  131. StopPreviewEvent* pEvent = new StopPreviewEvent();
  132. pEvent->m_ctx = ctx;
  133. m_fsm.PostEventFIFO(pEvent);
  134. }
  135. void ScanImage(SpReqAnsContext<ScannerService_ScanImage_Req,
  136. ScannerService_ScanImage_Ans>::Pointer ctx)
  137. {
  138. ScanImageEvent* pEvent = new ScanImageEvent();
  139. pEvent->m_ctx = ctx;
  140. m_fsm.PostEventFIFO(pEvent);
  141. }
  142. void Exit(SpOnewayCallContext<ScannerService_Exit_Info>::Pointer ctx)
  143. {
  144. ErrorCodeEnum erroCode = m_fsm.OnDevExit();
  145. }
  146. void GetDevInfo(SpReqAnsContext<ScannerService_GetDevInfo_Req,
  147. ScannerService_GetDevInfo_Ans>::Pointer ctx)
  148. {
  149. m_fsm.GetDevInfo(ctx);
  150. return;
  151. }
  152. void GetOnlineCameras(SpReqAnsContext<ScannerService_GetOnlineCameras_Req,
  153. ScannerService_GetOnlineCameras_Ans>::Pointer ctx)
  154. {
  155. m_fsm.GetOnlineDevices(ctx);
  156. }
  157. void SetWinPos(SpReqAnsContext<ScannerService_SetWindowPos_Req,
  158. ScannerService_SetWindowPos_Ans>::Pointer ctx)
  159. {
  160. static ScannerService_SetWindowPos_Req lastReq = {-1, -1, -1, -1, -1};
  161. if((ctx->Req.type != lastReq.type
  162. || ctx->Req.pointX != lastReq.pointX
  163. || ctx->Req.pointY != lastReq.pointY
  164. || ctx->Req.nWidth != lastReq.nWidth
  165. /*|| ctx->Req.reserved4 != lastReq.reserved4*/)) {
  166. lastReq.type = ctx->Req.type;
  167. lastReq.pointX = ctx->Req.pointX;
  168. lastReq.pointY = ctx->Req.pointY;
  169. lastReq.nWidth = ctx->Req.nWidth;
  170. lastReq.reserved4 = ctx->Req.reserved4;
  171. PendingTask* pTask = new PendingTask(&m_fsm);
  172. pTask->SetContext(ctx);
  173. GetFunction()->PostThreadPoolTask(pTask);
  174. } else {
  175. LogWarn(Severity_Low, Error_Debug, LOG_WARN_DUPLICATE_SETWINPOS
  176. , CSimpleStringA::Format("same request with last-one: %d,%d,%d,%d,%d"
  177. , ctx->Req.type, ctx->Req.pointX, ctx->Req.pointY, ctx->Req.nWidth, ctx->Req.reserved4));
  178. ctx->Answer(Error_Succeed);
  179. }
  180. }
  181. void SetProperty(SpReqAnsContext<ScannerService_SetProperty_Req,
  182. ScannerService_SetProperty_Ans>::Pointer ctx)
  183. {
  184. WaitingFSMForIdle();
  185. SetPropertyEvent* pEvent = new SetPropertyEvent();
  186. pEvent->m_ctx = ctx;
  187. m_fsm.PostEventFIFO(pEvent);
  188. }
  189. void ShowProperty(SpReqAnsContext<ScannerService_ShowProperty_Req,
  190. ScannerService_ShowProperty_Ans>::Pointer ctx)
  191. {
  192. ShowPropertyEvent* pEvent = new ShowPropertyEvent();
  193. pEvent->m_ctx = ctx;
  194. m_fsm.PostEventFIFO(pEvent);
  195. }
  196. void GetDevStatus(SpReqAnsContext<ScannerService_GetDevStatus_Req,
  197. ScannerService_GetDevStatus_Ans>::Pointer ctx)
  198. {
  199. m_fsm.GetDevStatus(ctx);
  200. return;
  201. }
  202. void CrossTermCall(SpReqAnsContext<ScannerService_CrossTermCall_Req,
  203. ScannerService_CrossTermCall_Ans>::Pointer ctx)
  204. {
  205. CrossTermCallEvent* pEvent = new CrossTermCallEvent();
  206. pEvent->m_ctx = ctx;
  207. m_fsm.PostEventFIFO(pEvent);
  208. }
  209. void CrossTermInvokeInfo(SpOnewayCallContext<ScannerService_CrossTermInvokeInfo_Info>::Pointer ctx)
  210. {
  211. CrossTermInvokeEvent* pEvent = new CrossTermInvokeEvent();
  212. pEvent->m_ctx = ctx;
  213. m_fsm.PostEventFIFO(pEvent);
  214. }
  215. void OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
  216. {
  217. m_fsm.SelfTest(eTestType, pTransactionContext);
  218. }
  219. virtual bool IsService() const { return true; }
  220. virtual bool IsMultiThread() const { return true; };
  221. virtual void OnLog(
  222. const CAutoArray<CUUID> &SubIDs,
  223. const CUUID nLogID,
  224. const LogTypeEnum eLogType,
  225. const SeverityLevelEnum eLevel,
  226. const DWORD dwSysError,
  227. const DWORD dwUserCode,
  228. const DWORD dwEntityInstanceID,
  229. const WORD wEntityDevelID,
  230. const CAutoArray<DWORD> &Param,
  231. const char *pszEntityName,
  232. const char *pszModuleName,
  233. const char *pszMessage, const linkContext &pLinkInfo)
  234. {
  235. switch (dwUserCode)
  236. {
  237. case EVENT_USB_INSERT:
  238. m_fsm.OnDeviceInsert();
  239. break;
  240. case EVENT_USB_OUTPUT:
  241. m_fsm.OnDeviceRemoved();
  242. break;
  243. case EVENT_USB_CAMERA_NOT_ONLY:
  244. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("EVENT_USB_CAMERA_NOT_ONLY occurs !!!");
  245. break;
  246. case EVENT_USB_CAMERA_INPUT:
  247. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("EVENT_USB_CAMERA_INPUT occurs !!!");
  248. break;
  249. case LOG_ERROR_PORTABLESCANNER_IN_PREVIEW:
  250. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("LOG_ERROR_PORTABLESCANNER_IN_PREVIEW: %s", pszMessage);
  251. {
  252. m_fsm.PSEntityPostCameraError(pszMessage);
  253. }
  254. break;
  255. }
  256. }
  257. void OnEntityStateHook(const char *pszEntityName,const char *pszTriggerEntity,
  258. EntityStateEnum eState,EntityStateEnum eLastState)
  259. {
  260. m_fsm.OnChildEntityStateChange(eLastState, eState);
  261. }
  262. void OnUserStateHook(const char *pszEntityName,DWORD dwState,DWORD dwLastState)
  263. {
  264. }
  265. void OnCeateConnection(const char *pszCallerEntity,const char *pszServiceEntity)
  266. {
  267. }
  268. void OnCloseConnection(const char *pszCallerEntity,const char *pszServiceEntity)
  269. {
  270. }
  271. void WaitingFSMForIdle()
  272. {
  273. static const int waitTimes = 30;
  274. static const int waitEachTimeout = 300;
  275. int waitCurrTimes = 0;
  276. while(m_fsm.IsWorking() && (++waitCurrTimes <= waitTimes)) {
  277. Sleep(waitEachTimeout);
  278. }
  279. if(waitCurrTimes > waitTimes) {
  280. LogWarn(Severity_Low, Error_TimeOut, 0, "Waiting FSM to Idle timeout!");
  281. }
  282. }
  283. private:
  284. CScannerFSM m_fsm;
  285. CUUID m_uuidMaintainWatcher;
  286. CUUID m_uuidPortableScanner;
  287. };
  288. HSPScannerClient::HSPScannerClient(CScannerEntity *pEntity)
  289. :HSPScannerService_ClientBase(pEntity)
  290. {
  291. }
  292. #endif