C++Instrument Catalog MFC 1.2
Metrolab C++InstrumentCatalog for MFC
Loading...
Searching...
No Matches
MFCManager.cpp
Go to the documentation of this file.
1
5
6// Standard includes
7#include <random>
8#include <chrono>
9#include <algorithm>
10// Personal includes
11#include "MFCManager.h"
12#include "Helpers.h"
13#include "OSDefines.h"
14#include "Exception.h"
15#if defined(MFC_NORMALIZATION_ENABLED)
16#include "MFCProLicInterface.h"
17#endif
18
19#define LOG(__msg__, __level__,__location__) l_Log(CInstrumentCatalog::sLogMessage(__level__, "MFCManager", __msg__, __location__))
20
21#define NO_SEARCH_SPR 0 // No search is being performed
22#define SEARCH_ENDED_SPR 100 // Search progress that is notified when search has ended
23
25
26
27//--------------------------------------------------------------------------//
28// MFC MANAGER
29//--------------------------------------------------------------------------//
30
31//--------------------------------------------------------------------------//
32// Reception of catalog events
33void CMFCManager::l_CatalogNotificationHandler(CAsyncNotification * pANot)
34{
35 CObserverNotifier_untyped & rNotifier = pANot->Notifier();
36 CObserver_untyped & rVar = pANot->Observer();
37
38 if (rVar.Is(m_Cat.oInstrumentCurrent))
39 {
40 // Current instrument requested to change
41 if (rNotifier.Is(m_ADNotifier))
42 {
44 l_NewInstrumentCurrent(rCurrentInstr.ReadCurrent());
45 }
46 }
47 else if (rVar.Is(m_Cat.oMFC3045SerialPortSettings))
48 {
49 // New MFC3045 Serial Port settings
50 if (rNotifier.Is(m_ADNotifier))
51 {
53 l_NewMFC3045SerialSettings(rMFC3045SerialSett.ReadCurrent());
54 }
55 }
56 else if (rVar.Is(m_Cat.oInstrumentState))
57 {
58 // Current instrument state requested to change
59 if (rNotifier.Is(m_ADNotifier))
60 {
62 l_NewInstrumentState(rInstrState.ReadCurrent());
63 }
64 }
65 else if (rVar.Is(m_Cat.oRemoteBoxBusyLedState))
66 {
67 // Current remote box busy led state requested to change
68 if (rNotifier.Is(m_ADNotifier))
69 {
71 l_NewRBBusyLedState(rRBBusyLedState.ReadCurrent());
72 }
73 }
74 else if (rVar.Is(m_Cat.oNormalizationRequest))
75 {
76 // New Normalization request received
77 if (rNotifier.Is(m_ADNotifier))
78 {
80 l_NewNormalizationRequest(rNormalizationRequest.ReadCurrent());
81 }
82 }
83 else if (rVar.Is(m_Cat.oLicenseInfoRequest))
84 {
85 // New license information request received
86 // rVar value is unused
87 if (rNotifier.Is(m_ADNotifier))
88 {
89 l_GetDongleLicenseInfo();
90 }
91 }
92 else if (rVar.Is(m_Cat.oLicenseUpdateRequest))
93 {
94 // New license update request received
95 if (rNotifier.Is(m_ADNotifier))
96 {
98 l_DongleLicenseUpdateRequest(rLicenseUpdateRequest.ReadCurrent());
99 }
100 }
101 delete pANot;
102}
103
104void CMFCManager::l_ControllerNotificationHandler(CAsyncNotification * pASNot)
105{
106 CObserver_untyped & rVar = pASNot->Observer();
107
108 const CAsyncSnapshotNotification * l_pASNot = static_cast<CAsyncSnapshotNotification *>(pASNot);
109
110 if (rVar.Is(m_ContCat.oState))
111 {
112 const CMFCManagerCatalog::eControllerState * l_pVal = static_cast<const CMFCManagerCatalog::eControllerState *>(l_pASNot->DataCopy());
113 SlotControllerState(*l_pVal);
114 }
115 else if (rVar.Is(m_ContCat.oInstrumentList))
116 {
117 const CParsedResourceList * l_pVal = static_cast<const CParsedResourceList *>(l_pASNot->DataCopy());
118 SlotInstrumentList(*l_pVal);
119 }
120 else if (rVar.Is(m_ContCat.oPAInfo))
121 {
122 const CInstrumentCatalog::sProbeArrayInformation * l_pVal = static_cast<const CInstrumentCatalog::sProbeArrayInformation *>(l_pASNot->DataCopy());
123 SlotPAInfo(*l_pVal);
124 }
125 else if (rVar.Is(m_ContCat.oInstrumentConfiguration))
126 {
127 const CInstrumentCatalog::sInstrumentConfiguration * l_pVal = static_cast<const CInstrumentCatalog::sInstrumentConfiguration *>(l_pASNot->DataCopy());
128 SlotInstrConfig(*l_pVal);
129 }
130 else if (rVar.Is(m_ContCat.oSearchProgress))
131 {
132 const U8 * l_pVal = static_cast<const U8 *>(l_pASNot->DataCopy());
133 SlotSearchProgress(*l_pVal);
134 }
135 else if (rVar.Is(m_ContCat.oSearchResult))
136 {
137 const CInstrumentCatalog::sSearchResult * l_pVal = static_cast<const CInstrumentCatalog::sSearchResult *>(l_pASNot->DataCopy());
138 SlotSearchResult(*l_pVal);
139 }
140 else if (rVar.Is(m_ContCat.oMeasResults))
141 {
142 const CInstrumentCatalog::sMeasurementResults * l_pVal = static_cast<const CInstrumentCatalog::sMeasurementResults *>(l_pASNot->DataCopy());
143 SlotMeasResult(*l_pVal);
144 }
145 else if (rVar.Is(m_ContCat.oError))
146 {
147 const CMFCDevice::sMFCDeviceError * l_pVal = static_cast<const CMFCDevice::sMFCDeviceError *>(l_pASNot->DataCopy());
148 SlotControllerError(*l_pVal);
149 }
150 else if (rVar.Is(m_ContCat.oLogMessage))
151 {
152 const CInstrumentCatalog::sLogMessage * l_pVal = static_cast<const CInstrumentCatalog::sLogMessage *>(l_pASNot->DataCopy());
154 }
155 else if (rVar.Is(m_ContCat.oRemoteBoxStart))
156 {
157 SlotRBStart();
158 }
159 else if (rVar.Is(m_ContCat.oUSBRemoteBoxStart))
160 {
161 SlotRBStart();
162 }
163
164 delete pASNot;
165}
166
167void CMFCManager::l_ClearInstrumentInfo()
168{
170
171 // Fill empty probe array and instrument information and return it to catalog
172 LOG("Instrument has been disconnected", CInstrumentCatalog::eLogLevel::kInfo, MTL__LOCATION__);
173 m_InstrInfo = CInstrumentCatalog::sInstrumentInformation();
174 m_Cat.cProbeArrayInformation.rRequestControl(CInstrumentCatalog::sProbeArrayInformation());
175 m_Cat.cInstrumentInformation.rRequestControl(CInstrumentCatalog::sInstrumentInformation());
176 m_Cat.cInstrumentConfiguration.rRequestControl(CInstrumentCatalog::sInstrumentConfiguration());
177}
178
179void CMFCManager::l_NewInstrumentCurrent(const CInstrumentCatalog::sInstrumentConnection & rNewInstr)
180{
183
184 // Abort any current operation...
185 m_MFCController.Terminate();
186
187 try
188 {
189 if(nullptr != m_pMFCDevice)
190 l_UpdateRBBusyLedState(CInstrumentCatalog::eRemoteBoxBusyLedState::kRemoteBusyLedOff); //try to turn off remote led on previous connected instrument
191 // If we are already connected to an instrument, disconnect from it
192 delete m_pMFCDevice;
193 m_pMFCDevice = nullptr;
194
195 // If the given connection is empty, we do not connect to any instrument
196 if (rNewInstr.isEmpty())
197 {
198 l_ClearInstrumentInfo();
199 // Set controller state to scan instrument
200 m_MFCController.ScanInstruments(&m_RsrcManager);
201 }
202 else
203 {
204 // In any case of new instrument connection, send an empty Probe Array info
205 m_Cat.cProbeArrayInformation.rRequestControl(CInstrumentCatalog::sProbeArrayInformation());
206 m_InstrInfo = CInstrumentCatalog::sInstrumentInformation(); //clear current MFCManager m_InstrInfo
207 // Look for the requested Serial Port resource
208 auto l_InstrIt = m_RsrcListMap.find(rNewInstr.UniqueID);
209 if (l_InstrIt == m_RsrcListMap.end())
210 throw CMFCManagerException("Cannot find requested resource among available list", MTL__LOCATION__);
212 {
213 // Instrument is MFC3045 (MFC3045 main unit + MFC3048 probe array)
214 m_pMFCDevice = new CMFC3045Device(m_MFCController, m_RsrcManager, l_InstrIt->second.ParsedResource.Name, m_MFC3045SerialSett);
215 }
216 else
217 {
218 // Instrument is MFC2x46 (PT2026 main unit + FCA7046 + MFC9x46 probe array)
219 m_pMFCDevice = new CMFC2x46Device(m_MFCController, m_RsrcManager, l_InstrIt->second.ParsedResource.Name, m_CustomPAConfScriptDirectory);
220 }
221 // Try to connect to instrument and get its information
222 CMFCDevice::sMFCDeviceError l_RetError;
223 l_RetError = m_pMFCDevice->Connect(m_InstrInfo);
224 if(l_RetError.HasError())
225 {
226 throw CMFCManagerException(l_RetError.Description, l_RetError.Location);
227 }
228 m_InstrInfo.InstrumentConnection = rNewInstr;
229
230 // Fill instrument information and return it to catalog
231 m_Cat.cInstrumentInformation.rRequestControl(m_InstrInfo);
232
233 // Configure remote box busy led mode state on current instrument
234 if(!l_UpdateRBBusyLedState(m_RBBusyLedState))
235 throw CMFCManagerException("Fail to set remote led mode state", MTL__LOCATION__);
236
237 // Set controller state to Scan probe array
238 m_MFCController.ScanPA(m_pMFCDevice);
239 }
240 }
241 catch (CMFCManagerException & rE)
242 {
244
245 // Fill empty instrument information and return it to catalog
246 l_ClearInstrumentInfo();
247
248 // Delete connected instrument if exists
249 if(nullptr != m_pMFCDevice)
250 {
251 delete m_pMFCDevice;
252 m_pMFCDevice = nullptr;
253 }
254
255 // Set controller state to scan instrument
256 m_MFCController.ScanInstruments(&m_RsrcManager);
257 }
258}
259
260// This method will set rs232 setting to use with MFC3045. If a MFC3045 is already connected, it allows to change instrument serial setting.
261void CMFCManager::l_NewMFC3045SerialSettings(const CInstrumentCatalog::sMFC3045SerialPortSettings & rNewSerialSett)
262{
265
266 CInstrumentCatalog::sInstrumentConnection l_CurrentInstr;
267 m_Cat.oInstrumentCurrent.rReadCurrent(l_CurrentInstr);
268 try
269 {
270 // If current connected instrument is MFC3045 and serial settings is different, change it.
271 if ((nullptr != m_pMFCDevice) && (l_CurrentInstr.Interface == CInstrumentCatalog::eConnectionInterface::kSerial) && (rNewSerialSett != m_MFC3045SerialSett))
272 {
273 // Abort any current operation...
274 m_MFCController.Terminate();
275 // Change instrument serial setting
276 LOG("Serial settings change in progress. Operation on current instrument may take a few seconds.", CInstrumentCatalog::eLogLevel::kInfo, MTL__LOCATION__);
277 SlotPAInfo(CInstrumentCatalog::sProbeArrayInformation());//Force to send an Empty PA info
278 CMFCDevice::sMFCDeviceError l_RetError;
279 l_RetError = m_pMFCDevice->ChangeSerialParmsOnConnectedInstr(rNewSerialSett);
280 if(l_RetError.HasError())
281 {
282 l_SendError(l_RetError);
284 {
285 throw CMFCManagerException("Serial settings change failed. Instrument serial settings might be corrupted. Set default instrument settings by pressing remote Start button untill end of led blinking while resetting the instrument.", MTL__LOCATION__);
286 }
287 else //warning
288 throw CMFCManagerException("Serial settings change failed.", MTL__LOCATION__);
289 }
290 // Reconnect to instrument with new serial port settings
291 m_MFC3045SerialSett = rNewSerialSett;
292 LOG("Serial settings change completed. Reconnection to instrument in progress.", CInstrumentCatalog::eLogLevel::kInfo, MTL__LOCATION__);
293 delete m_pMFCDevice; //Delete current m_pMFCDevice. CMFC3045Device.ChangeSerialParmsOnConnectedInstr() has done a reset of MFC3045 after serial reconfiguration.
294 m_pMFCDevice = nullptr;
295 l_NewInstrumentCurrent(l_CurrentInstr);
296 }
297 }
298 catch (CMFCManagerException & rE)
299 {
301 //Disconnect m_pMFCDevice, send empty PA & instrument information and restart MFCController thread with state kScanningInstruments
302 if ((nullptr != m_pMFCDevice) && (l_CurrentInstr.Interface == CInstrumentCatalog::eConnectionInterface::kSerial))
303 l_NewInstrumentCurrent(CInstrumentCatalog::sInstrumentConnection());
304 }
305 m_MFC3045SerialSett = rNewSerialSett; // In any case we store new settings for next serial instrument connection
306}
307
308void CMFCManager::l_NewInstrumentState(const CInstrumentCatalog::sInstrumentState & rNewInstrState)
309{
312
313 // Abort any current operation...
314 m_MFCController.Terminate();
315
316 // Prepare for new operation...
317 m_InstrState = rNewInstrState;
318
319 // Clear Search Progress
320 l_UpdateSPR(NO_SEARCH_SPR);
321
323 {
324 // Set controller state
325 if (nullptr != m_pMFCDevice)
326 m_MFCController.ScanPA(m_pMFCDevice);
327 else
328 m_MFCController.ScanInstruments(&m_RsrcManager);
329 }
330 else
331 {
332 // Start new operation
333 m_MFCController.SetInstrumentState(m_pMFCDevice, m_InstrState);
334 }
335}
336
337void CMFCManager::l_NewNormalizationRequest(const CInstrumentCatalog::sNormalization & rNewNormalizationRequest)
338{
341
342#if defined(MFC_NORMALIZATION_ENABLED) //only available in MFCTool Pro
343 // Abort any current operation...
344 m_MFCController.Terminate();
345
346 try
347 {
348 CMFCDevice::sMFCDeviceError l_RetError;
349 // last license dongle info was expired refresh it
350 if(m_LicInfo.LicenseIsExpired)
351 l_GetDongleLicenseInfo();
352
353 if (nullptr == m_pMFCDevice)
354 throw CMFCManagerException("No device connected", MTL__LOCATION__);
355
356 // Try to proceed to Normalization request with connected instrument
357 CInstrumentCatalog::sNormalization l_ReturnedNormalization(rNewNormalizationRequest);
358
359 l_RetError = m_pMFCDevice->NewNormalizationRequest(rNewNormalizationRequest, l_ReturnedNormalization);
360 if(l_RetError.HasError())
361 {
362 throw l_RetError;
363 }
364 // If kReadRAM or kReadROM, return the normalization table read with Succeeded acknowledge
365 // If kWriteRAM or kWriteROM, return the written normalization table with Succeeded acknowledge
366 m_Cat.cNormalizationReturned.rRequestControl(l_ReturnedNormalization);
367
368 if ((rNewNormalizationRequest.Access == CInstrumentCatalog::eNormalizationAccess::kWriteROM))
369 {
370 m_MFCController.UpdateNormalizationDate(); //update normalization date
371 }
372 }
373 catch (CMFCDevice::sMFCDeviceError & rE)
374 {
375 l_SendError(rE);
376 CInstrumentCatalog::sNormalization l_ReturnNormFailed;
377 l_ReturnNormFailed.Access = rNewNormalizationRequest.Access;
378 l_ReturnNormFailed.Succeeded = false;
379 m_Cat.cNormalizationReturned.rRequestControl(l_ReturnNormFailed); //return empty normalization failed for acknowledge
380 l_GetDongleLicenseInfo();
381 }
382 catch (CMFCManagerException & rE)
383 {
385 }
386 // Set controller state
387 if (nullptr != m_pMFCDevice)
388 m_MFCController.ScanPA(m_pMFCDevice);
389 else
390 m_MFCController.ScanInstruments(&m_RsrcManager);
391#endif
392}
393
394//Get MFCToolPro license information from connected USB dongle.
395void CMFCManager::l_GetDongleLicenseInfo()
396{
398
399#if defined(MFC_NORMALIZATION_ENABLED) //only available in MFCTool Pro
400 //Get license information from USB dongle. No need to abort any current operation from instrument in controller
401 try
402 {
403 CMFCDevice::sMFCDeviceError l_Error;
404 CInstrumentCatalog::sLicenseInformation l_LicenseInfo;
405 l_Error = CMFCProLicInterface::DongleGetLicenseInfo(l_LicenseInfo);
406 if(l_Error.HasError())
407 throw l_Error;
408
409 //return license information to instrument catalog
410 m_LicInfo = l_LicenseInfo;
411 m_Cat.cLicenseInfoReturned.RequestControl(l_LicenseInfo);
412 }
413 catch (CMFCDevice::sMFCDeviceError & rE)
414 {
415 m_LicInfo = CInstrumentCatalog::sLicenseInformation();
416 m_Cat.cLicenseInfoReturned.RequestControl(m_LicInfo); //return empty license information
417 l_SendError(rE);
418 }
419#endif
420}
421
422//Do an update of the MFCTolPro dongle license.
423void CMFCManager::l_DongleLicenseUpdateRequest(const CInstrumentCatalog::sLicenseUpdate & rLicenseUpdateRequest)
424{
426
427#if defined(MFC_NORMALIZATION_ENABLED) //only available in MFCTool Pro
428 //Update license in Metrolab USB dongle. No need to abort any current operation from instrument in controller
429 CInstrumentCatalog::sLicenseUpdate l_LicenseUpdate = rLicenseUpdateRequest;
430 try
431 {
432 CMFCDevice::sMFCDeviceError l_Error;
433 l_Error = CMFCProLicInterface::DongleUpdateLicense(l_LicenseUpdate);
434 if(l_Error.HasError())
435 throw l_Error;
436
437 //if license succeeded, call dongle information to refresh it:
438 if(l_LicenseUpdate.UpdateSucceeded)
439 l_GetDongleLicenseInfo();
440 }
441 catch (CMFCDevice::sMFCDeviceError & rE)
442 {
443 l_SendError(rE);
444 }
445 //return license update status to instrument catalog
446 m_Cat.cLicenseUpdateReturned.RequestControl(l_LicenseUpdate);
447#endif
448}
449
450// Set the new m_RBBusyLedState to be used for next connection or if already connected, changes remote led mode on instrument
451void CMFCManager::l_NewRBBusyLedState(const CInstrumentCatalog::eRemoteBoxBusyLedState & rNewRBBusyLedState)
452{
455
456 // Abort any current operation...
457 m_MFCController.Terminate();
458 try
459 {
460 if(nullptr != m_pMFCDevice)
461 {
462 if(!l_UpdateRBBusyLedState(rNewRBBusyLedState))
463 throw CMFCManagerException("Fail to change remote led mode state", MTL__LOCATION__);
464 }
465 m_RBBusyLedState = rNewRBBusyLedState; // Update m_RBBusyLedState in any case (connected or not)
466 }
467 catch (CMFCManagerException & rE)
468 {
470 }
471 // Set controller state
472 if (nullptr != m_pMFCDevice)
473 m_MFCController.ScanPA(m_pMFCDevice);
474 else
475 m_MFCController.ScanInstruments(&m_RsrcManager);
476}
477
478//--------------------------------------------------------------------------//
479// private method
480// Update the m_RBBusyLedState (remote box led state) on the current instrument
481bool CMFCManager::l_UpdateRBBusyLedState(const CInstrumentCatalog::eRemoteBoxBusyLedState & rNewRBBusyLedState)
482{
484 try
485 {
486 if (nullptr == m_pMFCDevice)
487 throw CMFCManagerException("No device connected", MTL__LOCATION__);
488
489 CMFCDevice::sMFCDeviceError l_RetError;
490 // Change remote box busy led state mode on connected instrument
491 switch (m_InstrInfo.Type)
492 {
494 {
495 l_RetError = m_pMFCDevice->SetRBBusyLedState(rNewRBBusyLedState);
496 if(l_RetError.HasError())
497 l_SendError(CInstrumentCatalog::eErrorLevel::kWarning, l_RetError.Description, MTL__LOCATION__); //set always as warning
498 break;
499 }
501 {
502 l_RetError = m_pMFCDevice->SetRBBusyLedState(rNewRBBusyLedState);
503 if(l_RetError.HasError())
504 {
505 throw l_RetError;
506 }
507 break;
508 }
509 default:
510 // send warning:
511 l_SendError(CInstrumentCatalog::eErrorLevel::kWarning, "Set remote led state not supported. Unknown Instrument type", MTL__LOCATION__);
512 break;
513 }
514 }
515 catch (CMFCManagerException & rE)
516 {
518 return false;
519 }
520 catch (CMFCDevice::sMFCDeviceError & rE)
521 {
522 l_SendError(rE);
523 return false;
524 }
525 return true;
526}
527
528// Notifications to the catalog
529void CMFCManager::l_UpdateInstrumentStatus()
530{
532
533 bool l_Busy = false;
534
535 // Compute State
536 switch (m_MFCControllerState)
537 {
539 // Nothing to say but controller stopped a job
540 break;
542 l_UpdateSPR(NO_SEARCH_SPR);
544 break;
546 l_UpdateSPR(NO_SEARCH_SPR);
547 if (0 == m_Cat.cProbeArrayInformation.ReadCurrent().NbProbes())
549 else
551 break;
554 l_Busy = true;
555 break;
558 l_Busy = true;
559 break;
560 }
561
562 // Update instrument State & SearchProgress
563 m_Cat.cInstrumentStatus.rRequestControl(m_InstrStatus);
564
565 // Update USB Remote box busy led state
566 m_ContCat.cUSBRemoteBusyState.RequestControl(l_Busy);
567}
568
569void CMFCManager::l_SendError(const CMFCDevice::sMFCDeviceError & rMFCDeviceError)
570{
571 switch(rMFCDeviceError.Level)
572 {
574 m_Cat.cError.rRequestControl(CInstrumentCatalog::sError(CInstrumentCatalog::eErrorLevel::kFatal, CInstrumentCatalog::FATAL_ERROR_CODE, rMFCDeviceError.Description, rMFCDeviceError.Location));
575 break;
577 m_Cat.cError.rRequestControl(CInstrumentCatalog::sError(CInstrumentCatalog::eErrorLevel::kError, CInstrumentCatalog::ERROR_CODE, rMFCDeviceError.Description, rMFCDeviceError.Location));
578 break;
580 m_Cat.cError.rRequestControl(CInstrumentCatalog::sError(CInstrumentCatalog::eErrorLevel::kWarning, CInstrumentCatalog::WARNING_ERROR_CODE, rMFCDeviceError.Description, rMFCDeviceError.Location));
581 break;
583 // No error message to send further to instrumentCatalog.
584 break;
585 }
586}
587
588void CMFCManager::l_SendError(CInstrumentCatalog::eErrorLevel Level, std::string Descr, std::string Source)
589{
591
592 switch(Level)
593 {
595 m_Cat.cError.rRequestControl(CInstrumentCatalog::sError(Level, CInstrumentCatalog::FATAL_ERROR_CODE, Descr, Source));
596 break;
598 m_Cat.cError.rRequestControl(CInstrumentCatalog::sError(Level, CInstrumentCatalog::ERROR_CODE, Descr, Source));
599 break;
601 m_Cat.cError.rRequestControl(CInstrumentCatalog::sError(Level, CInstrumentCatalog::WARNING_ERROR_CODE, Descr, Source));
602 break;
603 }
604}
605
606void CMFCManager::l_SendError(CInstrumentCatalog::sError Error)
607{
609 m_Cat.cError.rRequestControl(Error);
610}
611
612void CMFCManager::l_Log(const CInstrumentCatalog::sLogMessage & rMessage)
613{
614 m_Cat.cLogMessage.rRequestControl(rMessage);
615}
616
617void CMFCManager::l_UpdateSPR(uint8_t SearchProgress)
618{
619 m_InstrStatus.SearchProgress = SearchProgress;
620}
621
622void CMFCManager::l_ThreadTask(CThreadedMailBox::eEventType EvtType, sMFCManagerMailBoxMsg * pMsg)
623{
624 switch (EvtType)
625 {
626 case eEventType::kExit:
627 break;
628 case eEventType::kUserMsg:
629 if (nullptr != pMsg)
630 {
631 if (nullptr != pMsg->pCatNot)
632 l_CatalogNotificationHandler(pMsg->pCatNot);
633
634 if (nullptr != pMsg->pContrNot)
635 l_ControllerNotificationHandler(pMsg->pContrNot);
636 }
637 break;
638 }
639}
640
645
650
651
652
653//--------------------------------------------------------------------------//
654// Reception of external events
655//--------------------------------------------------------------------------//
657{
658 m_MFCController.ScanInstruments(&m_RsrcManager);
659}
660
662{
664
666 {
667 switch (m_MFCControllerState)
668 {
670 break;
672 break;
674 break;
676 m_MFCController.ScanPA(m_pMFCDevice);
677 break;
679 m_MFCController.ScanPA(m_pMFCDevice);
680 break;
681 default:
682 break;
683 }
684 }
685 m_MFCControllerState = State;
686
687 // Compute new status
688 l_UpdateInstrumentStatus();
689}
690
692{
694
695 //Parse resource list and create user-friendly names when possible
696 // USB0::0x1BFA::0xC7EA::PT2026_00012::INSTR => PT2026 00012
697 // ASRLx::INSTR => INSTR - COMx (x correspond to port COM number) , we can't have any instrument information yet
698 m_RsrcListMap = l_PrettyResourceNameList(NewResourceList);
699
700 // Convert list type
701 m_PrettyInstrList.clear();
702 for (auto & l_it : m_RsrcListMap)
703 {
705 l_InsCon.Label = l_it.second.PrettyName; // "Pretty name"
706 l_InsCon.UniqueID = l_it.first; // VISA resource name is a unique ID
707 switch (l_it.second.ParsedResource.Info.Type())
708 {
711 break;
714 break;
717 break;
718 default:
720 break;
721 }
722 m_PrettyInstrList.push_back(l_InsCon);
723 }
724 m_Cat.cInstrumentList.rRequestControl(m_PrettyInstrList);
725}
726
727void CMFCManager::SlotPAInfo(CInstrumentCatalog::sProbeArrayInformation PAInfo) // New PA Information received from CMFCController
728{
730
731 if(nullptr !=m_pMFCDevice)
732 {
733 //Update remote Busy led state if MFC3048 PA has been disconnected and reconnected. Otherwise Busy led doesn't work anymore correctly.
735 m_PADisconnected = true;
736 else if (m_PADisconnected)
737 {
738 m_PADisconnected = false;
740 m_pMFCDevice->SetRBBusyLedState(m_RBBusyLedState); // reload busy led state after a disconnection and reconnection of MFC3048 Probe-Array
741 }
742 m_Cat.cProbeArrayInformation.rRequestControl(PAInfo);
743 }
744 else // clear PA info if no main unit anymore
745 m_Cat.cProbeArrayInformation.rRequestControl(CInstrumentCatalog::sProbeArrayInformation());
746 // Compute new status
747 l_UpdateInstrumentStatus();
748}
749
751{
753
754 m_Cat.cInstrumentConfiguration.rRequestControl(InstrConfig);
755}
756
758{
760
761 m_Cat.cProbeArrayAngle.rRequestControl(Angle);
762}
763
765{
767
768 m_Cat.cRemoteBoxTriggerEvent.RequestControl(CInstrumentCatalog::eRemoteBoxTrigger::kRemoteBoxTrigger);
769}
770
772{
774
775 l_UpdateSPR(Progress);
776 l_UpdateInstrumentStatus();
777}
778
780{
782
783 l_UpdateSPR(SEARCH_ENDED_SPR); // Set SPR to 100% when we have a result
784 l_UpdateInstrumentStatus();
785 m_Cat.cSearchResult.rRequestControl(SearRes);
786}
787
789{
791
792 l_UpdateSPR(SEARCH_ENDED_SPR); // Set SPR to 100% when we have a result
793 l_UpdateInstrumentStatus();
794 m_Cat.cMeasurementResults.rRequestControl(MeasRes);
795}
796
798{
800
801 // Whatever the error, broadcast it to instrument catalog
802 l_SendError(error);
803
804 switch (m_MFCControllerState)
805 {
807 break;
810 {
811 m_MFCController.Terminate();
812 l_SendError(CInstrumentCatalog::eErrorLevel::kFatal, "Fatal error received from MFCController", MTL__LOCATION__);
813 }
814 break;
816 switch (error.Level)
817 {
819 m_MFCController.Terminate();
820 l_ClearInstrumentInfo();
821 l_SendError(CInstrumentCatalog::eErrorLevel::kFatal, "Fatal error received from MFCController", MTL__LOCATION__);
822 break;
824 //Disconnect m_pMFCDevice, send empty PA & instrument information and restart MFCController thread with state kScanningInstruments
825 l_NewInstrumentCurrent(CInstrumentCatalog::sInstrumentConnection());
826 break;
828 // Error has already been reported. Nothing to do.
829 break;
830 default:
831 break;
832 }
833 break;
836 m_MFCController.ScanPA(m_pMFCDevice);
837 break;
838 default:
839 break;
840 }
841}
842
847
854{
856
857 m_Cat.cRemoteBoxTriggerEvent.RequestControl(CInstrumentCatalog::eRemoteBoxTrigger::kRemoteBoxTrigger);
858}
859
860
861
862//--------------------------------------------------------------------------//
863// Class management
864CMFCManager::CMFCManager(CInstrumentCatalog & rCatalog, const std::string & rCustomPAConfScriptDir)
865 :
866 m_Cat(rCatalog),
867 m_ContCat(m_ContCatInstance),
868 m_ADNotifier(&CMFCManager::PostCatalogNotification, this),
869 m_ASControllerNotifier(&CMFCManager::PostControllerNotification, this),
870 m_pMFCDevice(nullptr),
871 m_MFCController(m_ContCatInstance, rCustomPAConfScriptDir),
872 m_RBController(m_RsrcManager, m_ContCatInstance),
873 m_CustomPAConfScriptDirectory(rCustomPAConfScriptDir)
874{
876
877 // Initialize variables
880}
885
887{
889
890 // Initialize controls (synchronize catalog with instrument manager)
891 //m_Cat.cInstrumentList.rRequestControl(m_PrettyInstrList);
892 m_Cat.cInstrumentInformation.rRequestControl(CInstrumentCatalog::sInstrumentInformation());
893 m_Cat.cProbeArrayInformation.rRequestControl(CInstrumentCatalog::sProbeArrayInformation());
894 m_Cat.cProbeArrayAngle.rRequestControl(CInstrumentCatalog::sProbeArrayAngle());
895 m_Cat.cInstrumentStatus.rRequestControl(m_InstrStatus);
896 // SearchResults is ephemeral
897 // MeasurementResults is ephemeral
898 // Error is ephemeral
899#if defined(MFC_NORMALIZATION_ENABLED)
900 // NormalizationReturned is ephemeral
901 m_Cat.cLicenseInfoReturned.rRequestControl(m_LicInfo);
902#endif
903
904}
905
907{
909 // Read variables (synchronize catalog with instrument manager)
910 m_Cat.oInstrumentCurrent.rReadCurrent(m_Instr);
911 m_Cat.oMFC3045SerialPortSettings.rReadCurrent(m_MFC3045SerialSett);
912 m_Cat.oInstrumentState.rReadCurrent(m_InstrState);
913 m_Cat.oRemoteBoxBusyLedState.rReadCurrent(m_RBBusyLedState);
914 // Instrument calibration date is ephemeral
915 // Normalization request is ephemeral
916 // LicenseInfoRequest is ephemeral
917}
918
920{
922 // Subscribe to InstrumentCatalog observers
923 m_Cat.oInstrumentCurrent.Subscribe(m_ADNotifier);
924 m_Cat.oMFC3045SerialPortSettings.Subscribe(m_ADNotifier);
925 m_Cat.oInstrumentState.Subscribe(m_ADNotifier);
926 m_Cat.oNormalizationRequest.Subscribe(m_ADNotifier);
927 m_Cat.oLicenseInfoRequest.Subscribe(m_ADNotifier);
928 m_Cat.oLicenseUpdateRequest.Subscribe(m_ADNotifier);
929 m_Cat.oRemoteBoxBusyLedState.Subscribe(m_ADNotifier);
930
931 // Subscribe to MFCController observers
932 m_ContCat.oState.Subscribe(m_ASControllerNotifier);
933 m_ContCat.oInstrumentList.Subscribe(m_ASControllerNotifier);
934 m_ContCat.oPAInfo.Subscribe(m_ASControllerNotifier);
935 m_ContCat.oInstrumentConfiguration.Subscribe(m_ASControllerNotifier);
936 m_ContCat.oSearchProgress.Subscribe(m_ASControllerNotifier);
937 m_ContCat.oSearchResult.Subscribe(m_ASControllerNotifier);
938 m_ContCat.oMeasResults.Subscribe(m_ASControllerNotifier);
939 m_ContCat.oError.Subscribe(m_ASControllerNotifier);
940 m_ContCat.oLogMessage.Subscribe(m_ASControllerNotifier);
941 m_ContCat.oRemoteBoxStart.Subscribe(m_ASControllerNotifier);
942
943 // Subscribe to USBRemoteBoxController observer
944 m_ContCat.oUSBRemoteBoxStart.Subscribe(m_ASControllerNotifier);
945}
946
948{
950
951 // Initialize Resource Manager
952 m_RsrcManager.Initialize();
953
954 // Start controller thread
955 m_MFCController.Operate();
956
957 // Start USB remote box controller thread
958 m_RBController.Operate();
959
960 // Start Catalog notification thread
962
963 // Run first controller operation
964 m_MFCController.ScanInstruments(&m_RsrcManager);
965}
966
968{
969 // Stop Catalog notification thread
971 // Stop USB Remote Box controller
972 m_RBController.Terminate();
973 // Stop MFC controller
974 m_MFCController.Terminate();
975 // Close MFC instrument (=> PT2026 or MFC3045) connection
976 delete m_pMFCDevice;
977 m_pMFCDevice = nullptr;
978}
979
980CMFCManager::tResourceMap CMFCManager::l_PrettyResourceNameList(const CParsedResourceList & rRsrcList)
981{
982 // Expected resource formats
983 // "USB0::0x1BFA::0x07EA::0000015::INSTR",
984 // "TCPIP0::pt2026_00015::inst0::INSTR",
985 // "TCPIP0::192.168.110.239::inst0::INSTR",
986 // "ASRL12::INSTR"
987
988 CMFCManager::tResourceMap l_ReturnedList;
989
990 std::regex l_reg("([^:]+)+");
991 std::sregex_token_iterator l_end;
992 for (auto & l_RsrcIt : rRsrcList)
993 {
994 std::string l_Alias(l_RsrcIt.Info.Alias());
995 // Tokenize resource name
996 std::sregex_token_iterator l_TokIt(l_RsrcIt.Name.begin(), l_RsrcIt.Name.end(), l_reg);
997 std::vector<std::string> l_Tokens(l_TokIt, l_end);
998 // Analyze content
999 if (l_Tokens.size() >= 2 && l_RsrcIt.Info.Class() == "INSTR" && l_Tokens.back() == "INSTR")
1000 {
1001 // Parse USB resource
1002 if (l_Tokens[0].find("USB") == 0 && l_Tokens.size() == 5 && l_Tokens[1] == "0x1BFA" && l_Tokens[2] == "0x07EA")
1003 {
1004 // "PT2026 nnnnn USB"
1005 std::string l_InstrName = "PT2026 " + l_Tokens[3];
1006 l_ReturnedList.insert(std::pair<tUniqueID, sPrettyResource>(l_RsrcIt.Name, { l_InstrName, l_RsrcIt }));
1007 }
1008 // Parse TCPIP resource
1009 else if (0 == l_Tokens[0].find("TCPIP") && 4 == l_Tokens.size())
1010 {
1011 if(std::string::npos != l_Tokens[1].find("PT2026") || std::string::npos != l_Tokens[1].find("pt2026"))
1012 {
1013 // "pt2026_nnnnn" or "pt2026-nnnnn"
1014 std::string l_InstrName = l_Tokens[1];
1015 l_ReturnedList.insert(std::pair<tUniqueID, sPrettyResource>(l_RsrcIt.Name, { l_InstrName, l_RsrcIt }));
1016 }
1017 else if(std::string::npos != l_Alias.find("PT2026") ||std::string::npos != l_Alias.find("pt2026"))
1018 {
1019 //Use Alias
1020 l_ReturnedList.insert(std::pair<tUniqueID, sPrettyResource>(l_RsrcIt.Name, { l_Alias, l_RsrcIt }));
1021 }
1022 //otherwise we ignore IP address to avoid having all instruments on network listed in the resource list
1023 }
1024 // Parse ASRL resource
1025 else if (l_Tokens[0].find("ASRL") == 0)
1026 {
1027 // "MFC3045 COMn"
1028 U8 l_COMnumber = static_cast<U8>(l_RsrcIt.Info.Number()); //static_cast<U8>(std::stoul(l_Tokens[0].substr(4)));
1029 std::string l_InstrName = "INSTR - COM" + std::to_string(l_COMnumber);
1030 l_ReturnedList.insert(std::pair<tUniqueID, sPrettyResource>(l_RsrcIt.Name, { l_InstrName, l_RsrcIt }));
1031 }
1032 }
1033 }
1034
1035 return l_ReturnedList;
1036}
#define LOG(__msg__, __level__, __location__)
#define NO_SEARCH_SPR
#define SEARCH_ENDED_SPR
CException< CMFCManager > CMFCManagerException
Exception handling utilities.
Collection of utility macros for error messages.
#define MTL__LOCATION__
Definition Helpers.h:24
Platform Dependent Definitions.
static constexpr I32 FATAL_ERROR_CODE
static constexpr I32 ERROR_CODE
static std::string l_Dump(const tInstrumentList &rInsList)
static constexpr I32 WARNING_ERROR_CODE
CObserver< CInstrumentCatalog::sLicenseInformation > oLicenseInfoRequest
CObserver< CInstrumentCatalog::sLicenseUpdate > oLicenseUpdateRequest
CObserver< CInstrumentCatalog::sInstrumentConnection > oInstrumentCurrent
CObserver< CInstrumentCatalog::eRemoteBoxBusyLedState > oRemoteBoxBusyLedState
CObserver< CInstrumentCatalog::sNormalization > oNormalizationRequest
CObserver< CInstrumentCatalog::sInstrumentState > oInstrumentState
CObserver< CInstrumentCatalog::sMFC3045SerialPortSettings > oMFC3045SerialPortSettings
enum MTL::CMFCDevice::sMFCDeviceError::eLevel Level
CAsyncNotification * pCatNot
Definition MFCManager.h:32
CAsyncNotification * pContrNot
Definition MFCManager.h:33
void SlotPAInfo(CInstrumentCatalog::sProbeArrayInformation PAInfo)
void PostCatalogNotification(CAsyncNotification *pANot)
void SlotControllerState(CMFCManagerCatalog::eControllerState State)
void SlotControllerError(CMFCDevice::sMFCDeviceError error)
void SlotSearchResult(CInstrumentCatalog::sSearchResult SearRes)
void SlotMeasResult(CInstrumentCatalog::sMeasurementResults MeasRes)
void SlotInstrumentList(CParsedResourceList List)
CMFCManager(CInstrumentCatalog &rCatalog, const std::string &rCustomPAConfScriptDir)
void SlotControllerThreadIsReady()
void SlotProbeArrayAngle(CInstrumentCatalog::sProbeArrayAngle Angle)
void SlotInstrConfig(CInstrumentCatalog::sInstrumentConfiguration InstrConfig)
void PostControllerNotification(CAsyncNotification *pASNot)
void SlotRemoteBoxTriggerEvent()
void SlotControllerLogMessage(CInstrumentCatalog::sLogMessage Message)
void SlotSearchProgress(U8 Progress)
virtual ~CMFCManager()
void l_SendMsg(const UserMsgT &rMsg)
Exception to be thrown.
Definition Exception.h:17
const char * message() const noexcept
Return the message.
Definition Exception.h:40
const char * context() const noexcept
Return the context.
Definition Exception.h:46