blob: 3f21eef81bbcc88b7e64cc9f1f28de1abc83dbb6 [file] [log] [blame]
Zane Shelley11b89942019-11-07 11:07:28 -06001#include "simulator.hpp"
2
Zane Shelley1be4c3c2020-04-17 15:55:07 -05003#include <fstream> // std::ifstream
4
5namespace libhei
6{
7
8//------------------------------------------------------------------------------
9
10// Paths are relative from the build/ directory
Zane Shelley8c093d82020-05-04 22:06:52 -050011const std::map<SimulatorData::SimChipType, const char*>
Zane Shelley1be4c3c2020-04-17 15:55:07 -050012 SimulatorData::cv_chipPath = {
Zane Shelleyaadf3bf2020-04-30 21:25:29 -050013 {SAMPLE, "../test/simulator/sample_data/sample.cdb"},
Zane Shelley1be4c3c2020-04-17 15:55:07 -050014};
Zane Shelley11b89942019-11-07 11:07:28 -060015
16//------------------------------------------------------------------------------
17
18void SimulatorData::addChip(const Chip& i_chip)
19{
20 // First check if this entry already exists.
Zane Shelley8c093d82020-05-04 22:06:52 -050021 auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
22 ASSERT_EQ(iv_chipList.end(), chip_itr);
Zane Shelley11b89942019-11-07 11:07:28 -060023
24 // Add the new entry.
25 iv_chipList.push_back(i_chip);
26
Zane Shelley8c093d82020-05-04 22:06:52 -050027 // Check if this chip type has been initialized.
28 ChipType_t chipType = i_chip.getType();
29 auto type_itr = std::find(iv_typeList.begin(), iv_typeList.end(), chipType);
30 if (iv_typeList.end() != type_itr)
31 {
32 return; // No need to continue.
33 }
34
35 // Add the new entry.
36 iv_typeList.push_back(chipType);
37
Zane Shelley1be4c3c2020-04-17 15:55:07 -050038 // Look for the file path
Zane Shelley8c093d82020-05-04 22:06:52 -050039 auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType));
Zane Shelley1be4c3c2020-04-17 15:55:07 -050040 ASSERT_NE(cv_chipPath.end(), itr2);
41 const char* path = itr2->second;
Zane Shelley11b89942019-11-07 11:07:28 -060042
Zane Shelley1be4c3c2020-04-17 15:55:07 -050043 // Open the Chip Data File
44 std::ifstream cdf{path, std::ifstream::binary};
45 ASSERT_TRUE(cdf.good());
46
47 // Get the length of file
48 cdf.seekg(0, cdf.end);
49 size_t sz_buffer = cdf.tellg();
50 cdf.seekg(0, cdf.beg);
51
52 // Allocate memory
53 char* buffer = new char[sz_buffer];
54
55 // Read data as a block
56 cdf.read(buffer, sz_buffer);
57
58 // Close the Chip Data File
59 cdf.close();
60
61 // Initilize the chip with this Chip Data File.
Zane Shelleydd109cc2020-04-30 21:41:41 -050062 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050063
64 // Clean up the buffer
65 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060066}
67
68//------------------------------------------------------------------------------
69
70void SimulatorData::endIteration()
71{
72 // Start by calling libhei::isolate().
73 IsolationData isoData{};
74 ReturnCode rc = isolate(iv_chipList, isoData);
75
76 // It is possible that even in a failure scenario the information in the
77 // returned IsolationData would be useful.
78 // TODO: Figure out where to put the data.
79
80 // Verify if isolation completed successfully.
81 ASSERT_TRUE(RC_SUCCESS == rc);
82
83 // Get the list of signatures found in isolation.
84 std::vector<Signature> givenSigList = isoData.getSignatureList();
85
86 // Verify the expected list and given list are the same.
87 ASSERT_EQ(iv_expSigList.size(), givenSigList.size());
88
89 std::sort(iv_expSigList.begin(), iv_expSigList.end());
90 std::sort(givenSigList.begin(), givenSigList.end());
91
92 ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(),
93 iv_expSigList.begin()));
94
95 // The iteration is complete so we can flush the data.
96 flushIterationData();
97}
Zane Shelley1be4c3c2020-04-17 15:55:07 -050098
99} // end namespace libhei