Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Passive.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <stdlib.h>
4 #include <Passive.h>
5 
6 static unsigned char * mockData = 0;
7 static int MOCK_SIZE = 0;
8 
10 {
11  static std::ifstream file;
12 
13  #ifdef MOCK_DATA
14  std::cout << "Mocaaaando" << std::endl;
15  static int mockIndex = 0;
16 
17  // First time
18  if(MOCK_SIZE == 0)
19  {
20  file.open("files/passive_mock.txt", std::ios::in | std::ios::binary | std::ios::ate);
21  if(file.is_open())
22  {
23  std::streampos size = file.tellg();
24  MOCK_SIZE = size;
25  // TODO: free this memory later
26  mockData = new unsigned char[MOCK_SIZE];
27  file.seekg(0, std::ios::beg);
28  file.read(mockData, MOCK_SIZE);
29  file.close();
30  std::cout << "Passive mockData loaded: " << size << " bytes long" << std::endl;
31  }
32  else
33  {
34  std::cout << "Failed to load passive mockData" << std::endl;
35  mockData = unsigned new char[1];
36  *mockData = '0';
37  }
38  file.close();
39  }
40 
41  // Just get next mockData line and we're good
42  if(!mockData)
43  {
44  std::cout << "No mockData" << std::endl;
45  return -1;
46  }
47 
48  if(mockIndex >= MOCK_SIZE)
49  mockIndex = 0;
50 
51  for(int i = 0; i < BUFFER_SIZE && mockIndex < MOCK_SIZE; i++, mockIndex++)
52  buffer[i] = mockData[mockIndex];
53  mockIndex++; // Skip line break
54 
55  #else
56  file.open(PASSIVE_FILENAME, std::ios::in | std::ios::binary | std::ios::ate);
57  if(file.is_open())
58  {
59  file.seekg(0, std::ios::beg);
60  // TODO: check how the input is gonna be written by msp430
61  file.read((char *)buffer, BUFFER_SIZE);
62  file.close();
63  std::cout << "Passive mockData loaded: " << BUFFER_SIZE << " bytes long" << std::endl;
64  }
65  else
66  std::cout << "Failed to load passive mockData" << std::endl;
67  file.close();
68  #endif
69  return 0;
70 }
static unsigned char * mockData
Definition: Passive.cpp:6
#define PASSIVE_FILENAME
Definition: Passive.h:6
static int flush()
Definition: Passive.cpp:9
static unsigned char buffer[BUFFER_SIZE]
Definition: Device.h:29
#define BUFFER_SIZE
Definition: Device.h:6
static int MOCK_SIZE
Definition: Passive.cpp:7