forked from PiSCSI/piscsi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscsiloop_core.cpp
More file actions
175 lines (142 loc) · 4.81 KB
/
scsiloop_core.cpp
File metadata and controls
175 lines (142 loc) · 4.81 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI for Raspberry Pi
// Loopback tester utility
//
// Copyright (C) 2022 akuker
//
// [ Loopback tester utility ]
//
// For more information, see:
// https://github.com/PiSCSI/piscsi/wiki/Troubleshooting#Loopback_Testing
//
//---------------------------------------------------------------------------
#include "hal/log.h"
#include "shared/piscsi_version.h"
#include "shared/piscsi_util.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "scsiloop/scsiloop_core.h"
#include "scsiloop/scsiloop_cout.h"
#include "scsiloop/scsiloop_gpio.h"
#include "scsiloop/scsiloop_timer.h"
#include <iostream>
#include <signal.h>
#if defined CONNECT_TYPE_STANDARD
#include "hal/connection_type/connection_standard.h"
#elif defined CONNECT_TYPE_FULLSPEC
#include "hal/connection_type/connection_fullspec.h"
#elif defined CONNECT_TYPE_AIBOM
#include "hal/connection_type/connection_aibom.h"
#elif defined CONNECT_TYPE_GAMERNIUM
#include "hal/connection_type/connection_gamernium.h"
#else
#error Invalid connection type or none specified
#endif
using namespace std;
using namespace spdlog;
string current_log_level = "unknown"; // Some versions of spdlog do not support get_log_level()
void ScsiLoop::Banner(const vector<char *> &args) const
{
cout << piscsi_util::Banner("(SCSI Loopback Test)");
cout << "Connection type: " << CONNECT_DESC << '\n' << flush;
if ((args.size() > 1 && strcmp(args[1], "-h") == 0) || (args.size() > 1 && strcmp(args[1], "--help") == 0)) {
cout << "\nUsage: " << args[0] << " [-L log_level] ...\n\n";
exit(EXIT_SUCCESS);
}
}
bool ScsiLoop::SetLogLevel(const string &log_level)
{
if (log_level == "trace") {
set_level(level::trace);
} else if (log_level == "debug") {
set_level(level::debug);
} else if (log_level == "info") {
set_level(level::info);
} else if (log_level == "warn") {
set_level(level::warn);
} else if (log_level == "err") {
set_level(level::err);
} else if (log_level == "critical") {
set_level(level::critical);
} else if (log_level == "off") {
set_level(level::off);
} else {
return false;
}
current_log_level = log_level;
LOGINFO("Set log level to '%s'", current_log_level.c_str())
return true;
}
void ScsiLoop::TerminationHandler(int signum)
{
exit(signum);
}
bool ScsiLoop::ParseArgument(const vector<char *> &args)
{
string name;
string log_level;
const char *locale = setlocale(LC_MESSAGES, "");
if (locale == nullptr || !strcmp(locale, "C")) {
locale = "en";
}
opterr = 1;
int opt;
while ((opt = getopt(static_cast<int>(args.size()), args.data(), "-L:")) != -1) {
switch (opt) {
case 'L':
log_level = optarg;
continue;
default:
return false;
}
if (optopt) {
return false;
}
}
if (!log_level.empty()) {
SetLogLevel(log_level);
}
return true;
}
int ScsiLoop::run(const vector<char *> &args)
{
// added setvbuf to override stdout buffering, so logs are written immediately and not when the process exits.
setvbuf(stdout, nullptr, _IONBF, 0);
// Output the Banner
Banner(args);
// ParseArgument() requires the bus to have been initialized first, which requires the root user.
// The -v option should be available for any user, which requires special handling.
for (auto this_arg : args) {
if (!strcasecmp(this_arg, "-v")) {
cout << piscsi_get_version_string() << endl;
return 0;
}
}
// Create a thread-safe stdout logger to process the log messages
const auto logger = stdout_color_mt("scsiloop stdout logger");
set_level(level::info);
current_log_level = "info";
vector<string> error_list;
if (!ParseArgument(args)) {
return -1;
}
// Signal handler to detach all devices on a KILL or TERM signal
struct sigaction termination_handler;
termination_handler.sa_handler = TerminationHandler;
sigemptyset(&termination_handler.sa_mask);
termination_handler.sa_flags = 0;
sigaction(SIGINT, &termination_handler, nullptr);
sigaction(SIGTERM, &termination_handler, nullptr);
// This must be executed before the timer test, since this initializes the timer
ScsiLoop_GPIO gpio_test;
// int result = ScsiLoop_Timer::RunTimerTest(error_list);
int result = gpio_test.RunLoopbackTest(error_list);
if (result == 0) {
// Only test the dat inputs/outputs if the loopback test passed.
result += gpio_test.RunDataInputTest(error_list);
result += gpio_test.RunDataOutputTest(error_list);
}
ScsiLoop_Cout::PrintErrors(error_list);
gpio_test.Cleanup();
return 0;
}