KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_server.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <fmt/format.h>
22#include <wx/app.h>
23#include <wx/datetime.h>
24#include <wx/event.h>
25#include <wx/stdpaths.h>
26
27#include <advanced_config.h>
28#include <api/api_handler.h>
29#include <api/api_utils.h> // traceApi
30#include <api/api_server.h>
31#include <kiid.h>
32#include <kinng.h>
33#include <paths.h>
34#include <pgm_base.h>
36#include <string_utils.h>
37
38#include <api/common/envelope.pb.h>
39
40#ifdef __UNIX__
41#include <sys/file.h>
42#endif
43
44using kiapi::common::ApiRequest, kiapi::common::ApiResponse, kiapi::common::ApiStatusCode;
45
46
47wxString KICAD_API_SERVER::s_logFileName = "api.log";
48
49
50wxDEFINE_EVENT( API_REQUEST_EVENT, wxCommandEvent );
51
52
54 wxEvtHandler(),
55 m_token( KIID().AsStdString() ),
56 m_readyToReply( false )
57{
58 if( !aAutoStart )
59 return;
60
61 if( !Pgm().GetCommonSettings()->m_Api.enable_server )
62 {
63 wxLogTrace( traceApi, "Server: disabled by user preferences." );
64 return;
65 }
66
67 Start();
68}
69
70
75
76
78{
79 if( Running() )
80 return;
81
82 wxFileName socket;
83
84 if( m_socketPathOverride.IsEmpty() )
85 {
86#ifdef __WXMAC__
87 socket.AssignDir( wxS( "/tmp" ) );
88#else
89 socket.AssignDir( wxStandardPaths::Get().GetTempDir() );
90#endif
91 socket.AppendDir( wxS( "kicad" ) );
92 socket.SetFullName( wxS( "api.sock" ) );
93 }
94 else
95 {
96 socket.Assign( m_socketPathOverride );
97
98 if( !socket.IsAbsolute() )
99 socket.MakeAbsolute();
100 }
101
102 if( !PATHS::EnsurePathExists( socket.GetPath() ) )
103 {
104 wxLogTrace( traceApi, wxString::Format( "Server: socket path %s could not be created",
105 socket.GetPath() ) );
106 return;
107 }
108
109#ifndef __WINDOWS__
110 // We use non-abstract sockets because macOS and some other non-Linux platforms don't support
111 // abstract sockets, which means there might be an old socket to unlink. In order to try to
112 // recover this, we lock a file (which will be unlocked on process exit) and if we get the lock,
113 // we know the old socket is orphaned and can be removed.
114 wxFileName lockFilePath( socket.GetPath(), wxS( "api.lock" ) );
115
116 int lockFile = open( lockFilePath.GetFullPath().c_str(), O_RDONLY | O_CREAT, 0600 );
117
118 if( lockFile >= 0 && flock( lockFile, LOCK_EX | LOCK_NB ) == 0 )
119 {
120 if( socket.Exists() )
121 {
122 wxLogTrace( traceApi, wxString::Format( "Server: cleaning up stale socket path %s",
123 socket.GetFullPath() ) );
124 wxRemoveFile( socket.GetFullPath() );
125 }
126 }
127#endif
128
129 if( socket.Exists() )
130 {
131 socket.SetFullName( wxString::Format( wxS( "api-%lu.sock" ), ::wxGetProcessId() ) );
132
133 if( socket.Exists() )
134 {
135 wxLogTrace( traceApi, wxString::Format( "Server: PID socket path %s already exists!",
136 socket.GetFullPath() ) );
137 return;
138 }
139 }
140
141 m_server = std::make_unique<KINNG_REQUEST_SERVER>(
142 fmt::format( "ipc://{}", socket.GetFullPath().ToStdString() ) );
143 m_server->SetCallback( [&]( std::string* aRequest ) { onApiRequest( aRequest ); } );
144
145 if( !m_server->Start() )
146 {
147 wxLogTrace( traceApi, "Server: failed to start KINNG listener thread" );
148 m_server.reset( nullptr );
149 return;
150 }
151
152 m_logFilePath.AssignDir( PATHS::GetLogsPath() );
153 m_logFilePath.SetName( s_logFileName );
154
155 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
156 {
158 log( fmt::format( "--- KiCad API server started at {} ---\n", SocketPath() ) );
159 }
160
161 wxLogTrace( traceApi, wxString::Format( "Server: listening at %s", SocketPath() ) );
162 Bind( API_REQUEST_EVENT, &KICAD_API_SERVER::handleApiEvent, this );
163}
164
165
167{
168 if( !Running() )
169 return;
170
171 wxLogTrace( traceApi, "Stopping server" );
172 Unbind( API_REQUEST_EVENT, &KICAD_API_SERVER::handleApiEvent, this );
173
174 m_server->Stop();
175 m_server.reset( nullptr );
176}
177
178
180{
181 return m_server && m_server->Running();
182}
183
184
186{
187 wxCHECK( aHandler, /* void */ );
188 m_handlers.insert( aHandler );
189}
190
191
193{
194 m_handlers.erase( aHandler );
195}
196
197
199{
200 return m_server ? m_server->SocketPath() : "";
201}
202
203
204void KICAD_API_SERVER::onApiRequest( std::string* aRequest )
205{
206 if( !m_readyToReply )
207 {
208 ApiResponse notHandled;
209 notHandled.mutable_status()->set_status( ApiStatusCode::AS_NOT_READY );
210 notHandled.mutable_status()->set_error_message( "KiCad is not ready to reply" );
211 m_server->Reply( notHandled.SerializeAsString() );
212 log( "Got incoming request but was not yet ready to reply." );
213 return;
214 }
215
216 wxCommandEvent* evt = new wxCommandEvent( API_REQUEST_EVENT );
217
218 // We don't actually need write access to this string, but client data is non-const
219 evt->SetClientData( static_cast<void*>( aRequest ) );
220
221 // Takes ownership and frees the wxCommandEvent
222 QueueEvent( evt );
223}
224
225
226void KICAD_API_SERVER::handleApiEvent( wxCommandEvent& aEvent )
227{
228 std::string& requestString = *static_cast<std::string*>( aEvent.GetClientData() );
229 handleApiRequestString( requestString );
230}
231
232
233void KICAD_API_SERVER::handleApiRequestString( std::string& aRequestString )
234{
235 ApiRequest request;
236
237 if( !request.ParseFromString( aRequestString ) )
238 {
239 ApiResponse error;
240 error.mutable_header()->set_kicad_token( m_token );
241 error.mutable_status()->set_status( ApiStatusCode::AS_BAD_REQUEST );
242 error.mutable_status()->set_error_message( "request could not be parsed" );
243 m_server->Reply( error.SerializeAsString() );
244
245 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
246 log( "Response (ERROR): " + error.Utf8DebugString() );
247
248 return;
249 }
250
251 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
252 log( "Request: " + request.Utf8DebugString() );
253
254 if( !request.header().kicad_token().empty() &&
255 request.header().kicad_token().compare( m_token ) != 0 )
256 {
257 ApiResponse error;
258 error.mutable_header()->set_kicad_token( m_token );
259 error.mutable_status()->set_status( ApiStatusCode::AS_TOKEN_MISMATCH );
260 error.mutable_status()->set_error_message(
261 "the provided kicad_token did not match this KiCad instance's token" );
262 m_server->Reply( error.SerializeAsString() );
263
264 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
265 log( "Response (ERROR): " + error.Utf8DebugString() );
266
267 return;
268 }
269
271
272 for( API_HANDLER* handler : m_handlers )
273 {
274 result = handler->Handle( request );
275
276 if( result.has_value() )
277 break;
278 else if( result.error().status() != ApiStatusCode::AS_UNHANDLED )
279 break;
280 }
281
282 // Note: at the point we call Reply(), we no longer own requestString.
283
284 if( result.has_value() )
285 {
286 result->mutable_header()->set_kicad_token( m_token );
287 m_server->Reply( result->SerializeAsString() );
288
289 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
290 log( "Response: " + result->Utf8DebugString() );
291 }
292 else
293 {
294 ApiResponse error;
295 error.mutable_status()->CopyFrom( result.error() );
296 error.mutable_header()->set_kicad_token( m_token );
297
298 if( result.error().status() == ApiStatusCode::AS_UNHANDLED )
299 {
300 std::string type = "<unparseable Any>";
301 google::protobuf::Any::ParseAnyTypeUrl( request.message().type_url(), &type );
302 std::string msg = fmt::format( "no handler available for request of type {}", type );
303 error.mutable_status()->set_error_message( msg );
304 }
305
306 m_server->Reply( error.SerializeAsString() );
307
308 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
309 log( "Response (ERROR): " + error.Utf8DebugString() );
310 }
311}
312
313
314void KICAD_API_SERVER::log( const std::string& aOutput )
315{
316 FILE* fp = wxFopen( m_logFilePath.GetFullPath(), wxT( "a" ) );
317
318 if( !fp )
319 return;
320
321 wxString out;
322 wxDateTime now = wxDateTime::Now();
323
324 fprintf( fp, "%s", TO_UTF8( out.Format( wxS( "%s: %s" ),
325 now.FormatISOCombined(), aOutput ) ) );
326 fclose( fp );
327}
tl::expected< ApiResponse, ApiResponseStatus > API_RESULT
Definition api_handler.h:42
wxDEFINE_EVENT(API_REQUEST_EVENT, wxCommandEvent)
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
std::string m_token
Definition api_server.h:104
bool Running() const
void handleApiEvent(wxCommandEvent &aEvent)
Event handler that receives the event on the main thread sent by onApiRequest.
wxFileName m_logFilePath
Definition api_server.h:112
static wxString s_logFileName
Definition api_server.h:110
void RegisterHandler(API_HANDLER *aHandler)
Adds a new request handler to the server.
void onApiRequest(std::string *aRequest)
Callback that executes on the server thread and generates an event that will be handled by the wxWidg...
wxString m_socketPathOverride
Definition api_server.h:108
std::set< API_HANDLER * > m_handlers
Definition api_server.h:102
std::string SocketPath() const
void handleApiRequestString(std::string &aRequestString)
void log(const std::string &aOutput)
KICAD_API_SERVER(bool aAutoStart=true)
void DeregisterHandler(API_HANDLER *aHandler)
std::unique_ptr< KINNG_REQUEST_SERVER > m_server
Definition api_server.h:100
Definition kiid.h:48
static wxString GetLogsPath()
Gets a path to use for user-visible log files.
Definition paths.cpp:507
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
Definition paths.cpp:518
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
Definition api_utils.cpp:27
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
wxString result
Test unit parsing edge cases and error handling.