Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

CCFUtils.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // =STS=> CCFUtils.cpp[1691].aa28 open SMID:29
  2. //////////////////////////////////////////////////////////////////////
  3. //
  4. // (c) Copyright 2003-2008 Cyberkinetics, Inc.
  5. // (c) Copyright 2008-2013 Blackrock Microsystems
  6. //
  7. // $Workfile: CCFUtils.cpp $
  8. // $Archive: /Cerebus/Human/WindowsApps/cbhwlib/CCFUtils.cpp $
  9. // $Revision: 2 $
  10. // $Date: 4/26/05 2:56p $
  11. // $Author: Kkorver $
  12. //
  13. // $NoKeywords: $
  14. //
  15. //////////////////////////////////////////////////////////////////////
  16. #include "StdAfx.h"
  17. #include "CCFUtils.h"
  18. #include "CCFUtilsBinary.h"
  19. #include "CCFUtilsXml.h"
  20. #include "CCFUtilsConcurrent.h"
  21. #include "cbhwlib.h"
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char THIS_FILE[]=__FILE__;
  25. #endif
  26. using namespace ccf;
  27. // Author & Date: Ehsan Azar 11 April 2012
  28. // Purpose: CCF base constructor
  29. CCFUtils::CCFUtils(bool bSend, bool bThreaded, cbCCF * pCCF, cbCCFCallback pCallbackFn, uint32_t nInstance) :
  30. m_pImpl(NULL),
  31. m_bSend(bSend),
  32. m_pCallbackFn(pCallbackFn),
  33. m_bThreaded(bThreaded),
  34. m_pCCF(pCCF),
  35. m_nInternalVersion(0),
  36. m_nInternalOriginalVersion(0),
  37. m_nInstance(nInstance),
  38. m_szFileName(NULL),
  39. m_bAutoSort(FALSE),
  40. m_bBinaryOriginal(FALSE)
  41. {
  42. // Initial copy
  43. if (pCCF)
  44. {
  45. m_pImpl = new ccfXml();
  46. m_nInternalVersion = m_pImpl->GetInternalVersion();
  47. // We rely on the fact that last version uses cbCCF
  48. dynamic_cast<ccfXml *>(m_pImpl)->m_data = *pCCF;
  49. m_pImpl->m_nInstance = m_nInstance;
  50. }
  51. }
  52. // Author & Date: Ehsan Azar 11 April 2012
  53. // Purpose: CCF base destructor
  54. CCFUtils::~CCFUtils()
  55. {
  56. if (m_pImpl)
  57. {
  58. delete m_pImpl;
  59. m_pImpl = NULL;
  60. }
  61. m_nInternalVersion = 0;
  62. m_nInternalOriginalVersion = 0;
  63. }
  64. // Author & Date: Ehsan Azar 12 April 2012
  65. // Purpose: Convert from older version
  66. CCFUtils * CCFUtils::Convert(CCFUtils * pOldConfig)
  67. {
  68. // Nothing to convert
  69. return NULL;
  70. }
  71. // Author & Date: Ehsan Azar 10 April 2012
  72. // Purpose: Send the latest configuration to NSP
  73. ccfResult CCFUtils::SendCCF()
  74. {
  75. if (m_pImpl == NULL)
  76. return CCFRESULT_ERR_NULL;
  77. m_nInternalVersion = m_pImpl->GetInternalVersion();
  78. cbCCF & data = dynamic_cast<ccfXml *>(m_pImpl)->m_data;
  79. ccfResult res = CCFRESULT_SUCCESS;
  80. if (m_pCallbackFn)
  81. m_pCallbackFn(m_nInstance, res, m_szFileName, CCFSTATE_SEND, 0);
  82. // Custom digital filters
  83. for (int i = 0; i < cbNUM_DIGITAL_FILTERS; ++i)
  84. {
  85. if (data.filtinfo[i].filt)
  86. {
  87. data.filtinfo[i].type = cbPKTTYPE_FILTSET;
  88. cbSendPacket(&data.filtinfo[i], m_nInstance);
  89. }
  90. }
  91. // Chaninfo
  92. for (int i = 0; i < cbMAXCHANS; ++i)
  93. {
  94. if (data.isChan[i].chan)
  95. {
  96. data.isChan[i].type = cbPKTTYPE_CHANSET;
  97. cbSendPacket(&data.isChan[i], m_nInstance);
  98. }
  99. }
  100. // Sorting
  101. {
  102. if (data.isSS_Statistics.type)
  103. {
  104. data.isSS_Statistics.type = cbPKTTYPE_SS_STATISTICSSET;
  105. cbSendPacket(&data.isSS_Statistics, m_nInstance);
  106. }
  107. for (int i = 0; i < cbNUM_ANALOG_CHANS; ++i)
  108. {
  109. if (data.isSS_NoiseBoundary[i].chan)
  110. {
  111. data.isSS_NoiseBoundary[i].type = cbPKTTYPE_SS_NOISE_BOUNDARYSET;
  112. cbSendPacket(&data.isSS_NoiseBoundary[i], m_nInstance);
  113. }
  114. }
  115. if (data.isSS_Detect.type)
  116. {
  117. data.isSS_Detect.type = cbPKTTYPE_SS_DETECTSET;
  118. cbSendPacket(&data.isSS_Detect, m_nInstance);
  119. }
  120. if (data.isSS_ArtifactReject.type)
  121. {
  122. data.isSS_ArtifactReject.type = cbPKTTYPE_SS_ARTIF_REJECTSET;
  123. cbSendPacket(&data.isSS_ArtifactReject, m_nInstance);
  124. }
  125. }
  126. // Sysinfo
  127. if (data.isSysInfo.type)
  128. {
  129. data.isSysInfo.type = cbPKTTYPE_SYSSETSPKLEN;
  130. cbSendPacket(&data.isSysInfo, m_nInstance);
  131. }
  132. // LNC
  133. if (data.isLnc.type)
  134. {
  135. data.isLnc.type = cbPKTTYPE_LNCSET;
  136. cbSendPacket(&data.isLnc, m_nInstance);
  137. }
  138. // Anlog output waveforms
  139. for (int i = 0; i < AOUT_NUM_GAIN_CHANS; ++i)
  140. {
  141. for (int j = 0; j < cbMAX_AOUT_TRIGGER; ++j)
  142. {
  143. if (data.isWaveform[i][j].chan)
  144. {
  145. data.isWaveform[i][j].type = cbPKTTYPE_WAVEFORMSET;
  146. cbSendPacket(&data.isWaveform[i][j], m_nInstance);
  147. }
  148. }
  149. }
  150. // NTrode
  151. for (int i = 0; i < cbMAXNTRODES; ++i)
  152. {
  153. if (data.isNTrodeInfo[i].ntrode)
  154. {
  155. data.isNTrodeInfo[i].type = cbPKTTYPE_SETNTRODEINFO;
  156. cbSendPacket(&data.isNTrodeInfo[i], m_nInstance);
  157. }
  158. }
  159. // Adaptive filter
  160. if (data.isAdaptInfo.type)
  161. {
  162. data.isAdaptInfo.type = cbPKTTYPE_ADAPTFILTSET;
  163. cbSendPacket(&data.isAdaptInfo, m_nInstance);
  164. }
  165. // if any spike sorting packets were read and the protocol is before the combined firmware,
  166. // set all the channels to autosorting
  167. if ((m_nInternalVersion < 8) && !m_bAutoSort)
  168. {
  169. cbPKT_SS_STATISTICS isSSStatistics;
  170. isSSStatistics.set(300, cbAUTOALG_HOOPS, cbAUTOALG_MODE_APPLY,
  171. 9, 125,
  172. 0.80f, 0.94f,
  173. 0.50f, 0.50f, 0.016f,
  174. 250, 0);
  175. cbSendPacket(&isSSStatistics, m_nInstance);
  176. }
  177. if (data.isSS_Status.type)
  178. {
  179. data.isSS_Status.type = cbPKTTYPE_SS_STATUSSET;
  180. data.isSS_Status.cntlNumUnits.nMode = ADAPT_NEVER; // Prevent rebuilding spike sorting when loading ccf.
  181. cbSendPacket(&data.isSS_Status, m_nInstance);
  182. }
  183. if (m_pCallbackFn)
  184. m_pCallbackFn(m_nInstance, res, m_szFileName, CCFSTATE_SEND, 100);
  185. return res;
  186. }
  187. // Author & Date: Kirk Korver 22 Sep 2004
  188. // Purpose: Write out a CCF file. Overwrite any other file by this name.
  189. // Do NOT prompt for the file to write out.
  190. // Inputs:
  191. // szFileName - the name of the file to write
  192. ccfResult CCFUtils::WriteCCFNoPrompt(LPCSTR szFileName)
  193. {
  194. m_szFileName = szFileName;
  195. ccfResult res = CCFRESULT_SUCCESS;
  196. if (m_bThreaded)
  197. {
  198. // Perform a threaded write operation
  199. ccf::ConWriteCCF(szFileName, m_pCCF, m_pCallbackFn, m_nInstance);
  200. } else {
  201. // If not read, use live NSP
  202. if (!m_pImpl)
  203. {
  204. // This should create m_pImpl on success
  205. res = ReadCCF(); // Read from NSP
  206. if (res)
  207. return res;
  208. }
  209. if (!m_pImpl)
  210. return CCFRESULT_ERR_UNKNOWN;
  211. if (m_pCallbackFn)
  212. m_pCallbackFn(m_nInstance, res, szFileName, CCFSTATE_WRITE, 0);
  213. // Ask the actual implementation to write file
  214. res = m_pImpl->WriteCCFNoPrompt(szFileName);
  215. if (m_pCallbackFn)
  216. m_pCallbackFn(m_nInstance, res, szFileName, CCFSTATE_WRITE, 100);
  217. }
  218. return res;
  219. }
  220. // Author & Date: Ehsan Azar 10 June 2012
  221. // Purpose: Read a CCF version information alone.
  222. // Inputs:
  223. // szFileName - the name of the file to read (if NULL uses live config)
  224. ccfResult CCFUtils::ReadVersion(LPCSTR szFileName)
  225. {
  226. ccfResult res = CCFRESULT_SUCCESS;
  227. m_szFileName = szFileName;
  228. if (m_pImpl == NULL)
  229. {
  230. m_pImpl = new ccfXml();
  231. m_pImpl->m_nInstance = m_nInstance;
  232. }
  233. if (szFileName)
  234. {
  235. // read from file
  236. res = m_pImpl->ReadVersion(szFileName);
  237. m_nInternalVersion = m_pImpl->GetInternalVersion();
  238. m_nInternalOriginalVersion = m_pImpl->GetInternalOriginalVersion();
  239. m_bBinaryOriginal = m_pImpl->IsBinaryOriginal();
  240. }
  241. else
  242. {
  243. // read from NSP
  244. m_nInternalVersion = m_pImpl->GetInternalVersion();
  245. m_pImpl->m_nInternalOriginalVersion = m_nInternalOriginalVersion = m_nInternalVersion;
  246. m_bBinaryOriginal = FALSE;
  247. }
  248. return res;
  249. }
  250. // Author & Date: Ehsan Azar 10 April 2012
  251. // Purpose: Read in a CCF file.
  252. // Check for the correct version of the file and convert if necessary and possible
  253. // Inputs:
  254. // szFileName - the name of the file to read (if NULL uses live config)
  255. // bConvert - if convertion can happen (has no effect if reading from NSP)
  256. ccfResult CCFUtils::ReadCCF(LPCSTR szFileName, bool bConvert)
  257. {
  258. m_szFileName = szFileName;
  259. ccfResult res = CCFRESULT_SUCCESS;
  260. // First read version
  261. res = ReadVersion(szFileName);
  262. if (res)
  263. return res;
  264. if (szFileName == NULL)
  265. {
  266. uint32_t nIdx = cb_library_index[m_nInstance];
  267. if (!cb_library_initialized[nIdx] || cb_cfg_buffer_ptr[nIdx] == NULL || cb_cfg_buffer_ptr[nIdx]->sysinfo.chid == 0)
  268. res = CCFRESULT_ERR_OFFLINE;
  269. else
  270. ReadCCFOfNSP(); // Read from NSP
  271. return res;
  272. }
  273. CCFUtils * pConfig = NULL;
  274. if (m_bBinaryOriginal)
  275. {
  276. pConfig = new CCFUtilsBinary();
  277. }
  278. else
  279. {
  280. switch (m_pImpl->GetInternalOriginalVersion())
  281. {
  282. case 12:
  283. pConfig = new CCFUtilsXml_v1();
  284. break;
  285. default:
  286. // This means a newer unsupported XML
  287. break;
  288. }
  289. }
  290. if (pConfig == NULL)
  291. res = CCFRESULT_ERR_FORMAT;
  292. // If conversion is needed, check permission
  293. if (res == CCFRESULT_SUCCESS && !bConvert)
  294. {
  295. if (m_nInternalVersion != m_nInternalOriginalVersion || m_bBinaryOriginal)
  296. res = CCFRESULT_WARN_CONVERT;
  297. }
  298. if (res == CCFRESULT_SUCCESS)
  299. {
  300. if (m_bThreaded)
  301. {
  302. // Perform a threaded read (and optional send) operation
  303. ccf::ConReadCCF(szFileName, m_bSend, m_pCCF, m_pCallbackFn, m_nInstance);
  304. }
  305. else
  306. {
  307. if (m_pCallbackFn)
  308. m_pCallbackFn(m_nInstance, res, szFileName, CCFSTATE_READ, 0);
  309. // Ask the right version to read its own data
  310. res = pConfig->ReadCCF(szFileName, bConvert);
  311. if (m_pCallbackFn)
  312. m_pCallbackFn(m_nInstance, res, szFileName, CCFSTATE_READ, 100);
  313. // Start the conversion chain
  314. if (res == CCFRESULT_SUCCESS)
  315. {
  316. if (m_pCallbackFn)
  317. m_pCallbackFn(m_nInstance, res, szFileName, CCFSTATE_CONVERT, 0);
  318. m_pImpl->Convert(pConfig);
  319. if (m_pCallbackFn)
  320. m_pCallbackFn(m_nInstance, res, szFileName, CCFSTATE_CONVERT, 100);
  321. // Take a copy if needed
  322. if (m_pCCF)
  323. *m_pCCF = dynamic_cast<ccfXml *>(m_pImpl)->m_data;
  324. // Auto send if asked for
  325. if (m_bSend)
  326. res = SendCCF();
  327. }
  328. }
  329. }
  330. // Cleanup
  331. if (pConfig)
  332. delete pConfig;
  333. return res;
  334. }
  335. // Author & Date: Ehsan Azar 12 April 2012
  336. // Purpose: Read latest CCF from NSP
  337. void CCFUtils::ReadCCFOfNSP()
  338. {
  339. uint32_t nIdx = cb_library_index[m_nInstance];
  340. cbCCF & data = dynamic_cast<ccfXml *>(m_pImpl)->m_data;
  341. for (int i = 0; i < cbNUM_DIGITAL_FILTERS; ++i)
  342. data.filtinfo[i] = cb_cfg_buffer_ptr[nIdx]->filtinfo[0][cbFIRST_DIGITAL_FILTER + i - 1]; // First is 1 based, but index is 0 based
  343. for (int i = 0; i < cbMAXCHANS; ++i)
  344. data.isChan[i] = cb_cfg_buffer_ptr[nIdx]->chaninfo[i];
  345. data.isAdaptInfo = cb_cfg_buffer_ptr[nIdx]->adaptinfo;
  346. data.isSS_Detect = cb_cfg_buffer_ptr[nIdx]->isSortingOptions.pktDetect;
  347. data.isSS_ArtifactReject = cb_cfg_buffer_ptr[nIdx]->isSortingOptions.pktArtifReject;
  348. for (int i = 0; i < cbNUM_ANALOG_CHANS; ++i)
  349. data.isSS_NoiseBoundary[i] = cb_cfg_buffer_ptr[nIdx]->isSortingOptions.pktNoiseBoundary[i];
  350. data.isSS_Statistics = cb_cfg_buffer_ptr[nIdx]->isSortingOptions.pktStatistics;
  351. {
  352. data.isSS_Status = cb_cfg_buffer_ptr[nIdx]->isSortingOptions.pktStatus;
  353. data.isSS_Status.cntlNumUnits.fElapsedMinutes = 99;
  354. data.isSS_Status.cntlUnitStats.fElapsedMinutes = 99;
  355. }
  356. {
  357. data.isSysInfo = cb_cfg_buffer_ptr[nIdx]->sysinfo;
  358. // only set spike len and pre trigger len
  359. data.isSysInfo.type = cbPKTTYPE_SYSSETSPKLEN;
  360. }
  361. for (int i = 0; i < cbMAXNTRODES; ++i)
  362. data.isNTrodeInfo[i] = cb_cfg_buffer_ptr[nIdx]->isNTrodeInfo[i];
  363. data.isLnc = cb_cfg_buffer_ptr[nIdx]->isLnc;
  364. for (int i = 0; i < AOUT_NUM_GAIN_CHANS; ++i)
  365. {
  366. for (int j = 0; j < cbMAX_AOUT_TRIGGER; ++j)
  367. {
  368. data.isWaveform[i][j] = cb_cfg_buffer_ptr[nIdx]->isWaveform[i][j];
  369. // Unset triggered state, so that when loading it does not start generating waveform
  370. data.isWaveform[i][j].active = 0;
  371. }
  372. }
  373. }
  374. // Author & Date: Ehsan Azar 12 April 2012
  375. // Purpose: Get current internal version.
  376. // Outputs:
  377. // Returns version (0 is invalid)
  378. int CCFUtils::GetInternalVersion()
  379. {
  380. return m_nInternalVersion;
  381. }
  382. // Author & Date: Ehsan Azar 13 April 2012
  383. // Purpose: Get internal version of original data.
  384. // Outputs:
  385. // Returns version (0 is invalid)
  386. int CCFUtils::GetInternalOriginalVersion()
  387. {
  388. return m_nInternalOriginalVersion;
  389. }
  390. // Author & Date: Ehsan Azar 30 Nov 2012
  391. // Purpose: If original file is binary file
  392. BOOL CCFUtils::IsBinaryOriginal()
  393. {
  394. return m_bBinaryOriginal;
  395. }