-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathInteractiveCommands.cpp
184 lines (150 loc) · 4.49 KB
/
InteractiveCommands.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
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
176
177
178
179
180
181
182
183
184
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "InteractiveCommands.h"
#include <platform/logging/LogV.h>
#include <system/SystemClock.h>
#include <editline.h>
#include <stdarg.h>
#include <stdio.h>
#include <string>
#include <vector>
constexpr char kInteractiveModePrompt[] = ">>> ";
constexpr char kInteractiveModeHistoryFileName[] = "chip_tool_history";
constexpr char kInteractiveModeStopCommand[] = "quit()";
namespace {
// File pointer for the log file
FILE * sLogFile = nullptr;
void OpenLogFile(const char * filePath)
{
sLogFile = fopen(filePath, "a");
if (sLogFile == nullptr)
{
perror("Failed to open log file");
}
}
void CloseLogFile()
{
if (sLogFile != nullptr)
{
fclose(sLogFile);
sLogFile = nullptr;
}
}
void ClearLine()
{
printf("\r\x1B[0J"); // Move cursor to the beginning of the line and clear from cursor to end of the screen
}
void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category, const char * msg, va_list args)
{
if (sLogFile == nullptr)
{
return;
}
uint64_t timeMs = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
uint64_t seconds = timeMs / 1000;
uint64_t milliseconds = timeMs % 1000;
flockfile(sLogFile);
fprintf(sLogFile, "[%llu.%06llu] CHIP:%s: ", static_cast<unsigned long long>(seconds),
static_cast<unsigned long long>(milliseconds), module);
vfprintf(sLogFile, msg, args);
fprintf(sLogFile, "\n");
fflush(sLogFile);
funlockfile(sLogFile);
}
} // namespace
char * InteractiveStartCommand::GetCommand(char * command)
{
if (command != nullptr)
{
free(command);
command = nullptr;
}
command = readline(kInteractiveModePrompt);
// Do not save empty lines
if (command != nullptr && *command)
{
add_history(command);
write_history(GetHistoryFilePath().c_str());
}
return command;
}
std::string InteractiveStartCommand::GetHistoryFilePath() const
{
std::string storageDir;
if (GetStorageDirectory().HasValue())
{
storageDir = GetStorageDirectory().Value();
}
else
{
// Match what GetFilename in ExamplePersistentStorage.cpp does.
const char * dir = getenv("TMPDIR");
if (dir == nullptr)
{
dir = "/tmp";
}
storageDir = dir;
}
return storageDir + "/" + kInteractiveModeHistoryFileName;
}
CHIP_ERROR InteractiveStartCommand::RunCommand()
{
read_history(GetHistoryFilePath().c_str());
if (mLogFilePath.HasValue())
{
OpenLogFile(mLogFilePath.Value());
// Redirect logs to the custom logging callback
chip::Logging::SetLogRedirectCallback(LoggingCallback);
}
char * command = nullptr;
int status;
while (true)
{
command = GetCommand(command);
if (command != nullptr && !ParseCommand(command, &status))
{
break;
}
}
if (command != nullptr)
{
free(command);
command = nullptr;
}
SetCommandExitStatus(CHIP_NO_ERROR);
CloseLogFile();
return CHIP_NO_ERROR;
}
bool InteractiveCommand::ParseCommand(char * command, int * status)
{
if (strcmp(command, kInteractiveModeStopCommand) == 0)
{
// If scheduling the cleanup fails, there is not much we can do.
// But if something went wrong while the application is leaving it could be because things have
// not been cleaned up properly, so it is still useful to log the failure.
LogErrorOnFailure(chip::DeviceLayer::PlatformMgr().ScheduleWork(ExecuteDeferredCleanups, 0));
return false;
}
ClearLine();
*status = mHandler->RunInteractive(command, GetStorageDirectory(), NeedsOperationalAdvertising());
return true;
}
bool InteractiveCommand::NeedsOperationalAdvertising()
{
return mAdvertiseOperational.ValueOr(true);
}