-
Notifications
You must be signed in to change notification settings - Fork 0
/
initiator-cli.cpp
143 lines (128 loc) · 3.79 KB
/
initiator-cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "initiator.hpp"
#include <iostream>
#include <fcntl.h> // File control definitions
#include <string.h> // strerror
#include <iomanip> // setw()
using namespace std;
using namespace hwitl;
template<typename T>
struct HexPrint {
HexPrint(T a) : a(a){};
T a;
};
template<typename T>
std::ostream& operator<<( std::ostream& o, const HexPrint<T>& a ){
const auto flags = o.flags();
o << hex;
o << "0x";
o << setfill('.');
o << setw( sizeof(T) * 2);
o << uppercase;
o << a.a;
o.setf(flags);
return o;
}
void readWriteAtZero(Initiator& remote) {
constexpr Address address = 0x00000000;
constexpr Payload payload = 0xFF;
cout << "[Initiator-cli] write " << HexPrint{address} << " value " << payload << endl;
auto stat = remote.write(address, payload);
if(stat != ResponseStatus::Ack::ok) {
cerr << "[Initiator-cli] Nak on write: " << static_cast<unsigned>(stat) << endl;
return;
}
cout << "[Initiator-cli] read " << HexPrint{address} << " ";
const auto ret = remote.read(address);
if(!ret) {
cerr << "[Initiator-cli] Could not read from remote" << endl;
return;
}
if(ret->getStatus().ack != ResponseStatus::Ack::ok) {
cerr << "[Initiator-cli] Nak on read: " << static_cast<unsigned>(ret->getStatus().ack) << endl;
return;
}
cout << "[Initiator-cli] value " << HexPrint{ret->getPayload()} << endl;
if(remote.getInterrupt())
cout << "[Initiator-cli] Interrupt was triggered" << endl;
}
void sweepAddressRoom(Initiator& remote) {
constexpr Address startAddr = 0x50000000;
constexpr Address endAddr = 0x50002FFF;
constexpr Payload payload = 0x8BADF00D;
constexpr Address printStep = 0x00001000;
for(Address address = startAddr; address <= endAddr; address += sizeof(Payload)) {
if(address % printStep == 0) {
cout << "[Initiator-cli] Scanning " << HexPrint{address} << " - " << HexPrint{(address + printStep)} << " ..." << endl;
}
auto ret = remote.read(address);
if(!ret) {
cerr << "[Initiator-cli] Could not read from remote" << endl;
return;
}
const auto status = ret->getStatus().ack;
if(status == ResponseStatus::Ack::not_mapped) {
// nothing
} else if(status == ResponseStatus::Ack::ok) {
cout << "[Initiator-cli] \t" << "Found Register at " << HexPrint{address} << " : " << HexPrint{ret->getPayload()} << endl;
if(remote.write(address, payload) == ResponseStatus::Ack::ok) {
cout << "[Initiator-cli] \t\t" << "Register Writable: " << HexPrint{payload} << endl;
}
} else {
cout << "[Initiator-cli] Other error at " << HexPrint{address} << ": [" << static_cast<int>(status) << "]" << endl;
}
}
}
void printUsage(const char* b) {
cout << "Usage: " << b << " path_to_file [--test num] [--baudrate num] [--help]" << endl;
}
int main(int argc, char* argv[]) {
int handle = -1;
// mandatory argument
if (argc > 1){
if(argv[1][0] == '-') {
// indicates some argument
printUsage(argv[0]);
return 0;
}
handle = open(argv[1], O_RDWR| O_NOCTTY);
} else {
printUsage(argv[0]);
return -1;
}
if (handle < 0) {
cerr << "[Initiator-cli] : " << argv[1] << " : " << strerror(errno) << endl;
return -1;
}
// optional arguments
if (argc > 2) {
for(int i = 2; i < argc; i++) {
if(strcmp(argv[i], "--baudrate") == 0) {
if(argc <= i+1) {
cerr << "Baudrate needs an argument!" << endl;
printUsage(argv[0]);
return -1;
} else {
if(!setBaudrate(handle, atoi(argv[i+1]))) {
return -2;
}
i++; // Argument consumed
}
} else if(strcmp(argv[i], "--help") == 0) {
printUsage(argv[0]);
return 0;
} else {
cerr << "Unknown argument " << argv[i] << endl;
}
// TODO: Different tests
}
}
setTTYRawmode(handle);
Initiator initiator(handle);
//while(handle) {
// different modes
sweepAddressRoom(initiator);
//}
close(handle);
cerr << "[Initiator-cli] end" << endl;
return 0;
}