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 (C) 2023 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
40using kiapi::common::ApiRequest, kiapi::common::ApiResponse, kiapi::common::ApiStatusCode;
41
42
43wxString KICAD_API_SERVER::s_logFileName = "api.log";
44
45
46wxDEFINE_EVENT( API_REQUEST_EVENT, wxCommandEvent );
47
48
50 wxEvtHandler(),
51 m_token( KIID().AsStdString() ),
52 m_readyToReply( false )
53{
54 if( !Pgm().GetCommonSettings()->m_Api.enable_server )
55 {
56 wxLogTrace( traceApi, "Server: disabled by user preferences." );
57 return;
58 }
59
60 Start();
61}
62
63
65{
66}
67
68
70{
71 if( Running() )
72 return;
73
74 wxFileName socket;
75#ifdef __WXMAC__
76 socket.AssignDir( wxS( "/tmp" ) );
77#else
78 socket.AssignDir( wxStandardPaths::Get().GetTempDir() );
79#endif
80 socket.AppendDir( wxS( "kicad" ) );
81 socket.SetFullName( wxS( "api.sock" ) );
82
83 if( !PATHS::EnsurePathExists( socket.GetPath() ) )
84 {
85 wxLogTrace( traceApi, wxString::Format( "Server: socket path %s could not be created",
86 socket.GetPath() ) );
87 return;
88 }
89
90 if( socket.FileExists() )
91 {
92 socket.SetFullName( wxString::Format( wxS( "api-%ul.sock" ), ::wxGetProcessId() ) );
93
94 if( socket.FileExists() )
95 {
96 wxLogTrace( traceApi, wxString::Format( "Server: PID socket path %s already exists!",
97 socket.GetFullPath() ) );
98 return;
99 }
100 }
101
102 m_server = std::make_unique<KINNG_REQUEST_SERVER>(
103 fmt::format( "ipc://{}", socket.GetFullPath().ToStdString() ) );
104 m_server->SetCallback( [&]( std::string* aRequest ) { onApiRequest( aRequest ); } );
105
106 m_logFilePath.AssignDir( PATHS::GetLogsPath() );
107 m_logFilePath.SetName( s_logFileName );
108
110 {
112 log( fmt::format( "--- KiCad API server started at {} ---\n", SocketPath() ) );
113 }
114
115 wxLogTrace( traceApi, wxString::Format( "Server: listening at %s", SocketPath() ) );
116
117 Bind( API_REQUEST_EVENT, &KICAD_API_SERVER::handleApiEvent, this );
118}
119
120
122{
123 if( !Running() )
124 return;
125
126 wxLogTrace( traceApi, "Stopping server" );
127 Unbind( API_REQUEST_EVENT, &KICAD_API_SERVER::handleApiEvent, this );
128
129 m_server->Stop();
130 m_server.reset( nullptr );
131}
132
133
135{
136 return m_server && m_server->Running();
137}
138
139
141{
142 wxCHECK( aHandler, /* void */ );
143 m_handlers.insert( aHandler );
144}
145
146
148{
149 m_handlers.erase( aHandler );
150}
151
152
154{
155 return m_server ? m_server->SocketPath() : "";
156}
157
158
159void KICAD_API_SERVER::onApiRequest( std::string* aRequest )
160{
161 if( !m_readyToReply )
162 {
163 ApiResponse notHandled;
164 notHandled.mutable_status()->set_status( ApiStatusCode::AS_NOT_READY );
165 notHandled.mutable_status()->set_error_message( "KiCad is not ready to reply" );
166 m_server->Reply( notHandled.SerializeAsString() );
167 log( "Got incoming request but was not yet ready to reply." );
168 return;
169 }
170
171 wxCommandEvent* evt = new wxCommandEvent( API_REQUEST_EVENT );
172
173 // We don't actually need write access to this string, but client data is non-const
174 evt->SetClientData( static_cast<void*>( aRequest ) );
175
176 // Takes ownership and frees the wxCommandEvent
177 QueueEvent( evt );
178}
179
180
181void KICAD_API_SERVER::handleApiEvent( wxCommandEvent& aEvent )
182{
183 std::string& requestString = *static_cast<std::string*>( aEvent.GetClientData() );
184 ApiRequest request;
185
186 if( !request.ParseFromString( requestString ) )
187 {
188 ApiResponse error;
189 error.mutable_header()->set_kicad_token( m_token );
190 error.mutable_status()->set_status( ApiStatusCode::AS_BAD_REQUEST );
191 error.mutable_status()->set_error_message( "request could not be parsed" );
192 m_server->Reply( error.SerializeAsString() );
193
195 log( "Response (ERROR): " + error.Utf8DebugString() );
196 }
197
199 log( "Request: " + request.Utf8DebugString() );
200
201 if( !request.header().kicad_token().empty() &&
202 request.header().kicad_token().compare( m_token ) != 0 )
203 {
204 ApiResponse error;
205 error.mutable_header()->set_kicad_token( m_token );
206 error.mutable_status()->set_status( ApiStatusCode::AS_TOKEN_MISMATCH );
207 error.mutable_status()->set_error_message(
208 "the provided kicad_token did not match this KiCad instance's token" );
209 m_server->Reply( error.SerializeAsString() );
210
212 log( "Response (ERROR): " + error.Utf8DebugString() );
213 }
214
215 API_RESULT result;
216
217 for( API_HANDLER* handler : m_handlers )
218 {
219 result = handler->Handle( request );
220
221 if( result.has_value() )
222 break;
223 else if( result.error().status() != ApiStatusCode::AS_UNHANDLED )
224 break;
225 }
226
227 // Note: at the point we call Reply(), we no longer own requestString.
228
229 if( result.has_value() )
230 {
231 result->mutable_header()->set_kicad_token( m_token );
232 m_server->Reply( result->SerializeAsString() );
233
235 log( "Response: " + result->Utf8DebugString() );
236 }
237 else
238 {
239 ApiResponse error;
240 error.mutable_status()->CopyFrom( result.error() );
241 error.mutable_header()->set_kicad_token( m_token );
242
243 if( result.error().status() == ApiStatusCode::AS_UNHANDLED )
244 {
245 std::string type = "<unparseable Any>";
246 google::protobuf::Any::ParseAnyTypeUrl( request.message().type_url(), &type );
247 std::string msg = fmt::format( "no handler available for request of type {}", type );
248 error.mutable_status()->set_error_message( msg );
249 }
250
251 m_server->Reply( error.SerializeAsString() );
252
253 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
254 log( "Response (ERROR): " + error.Utf8DebugString() );
255 }
256}
257
258
259void KICAD_API_SERVER::log( const std::string& aOutput )
260{
261 FILE* fp = wxFopen( m_logFilePath.GetFullPath(), wxT( "a" ) );
262
263 if( !fp )
264 return;
265
266 wxString out;
267 wxDateTime now = wxDateTime::Now();
268
269 fprintf( fp, "%s", TO_UTF8( out.Format( wxS( "%s: %s" ),
270 now.FormatISOCombined(), aOutput ) ) );
271 fclose( fp );
272}
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:97
bool Running() const
Definition: api_server.cpp:134
void handleApiEvent(wxCommandEvent &aEvent)
Event handler that receives the event on the main thread sent by onApiRequest.
Definition: api_server.cpp:181
wxFileName m_logFilePath
Definition: api_server.h:103
static wxString s_logFileName
Definition: api_server.h:101
void RegisterHandler(API_HANDLER *aHandler)
Adds a new request handler to the server.
Definition: api_server.cpp:140
void onApiRequest(std::string *aRequest)
Callback that executes on the server thread and generates an event that will be handled by the wxWidg...
Definition: api_server.cpp:159
std::set< API_HANDLER * > m_handlers
Definition: api_server.h:95
std::string SocketPath() const
Definition: api_server.cpp:153
void log(const std::string &aOutput)
Definition: api_server.cpp:259
void DeregisterHandler(API_HANDLER *aHandler)
Definition: api_server.cpp:147
std::unique_ptr< KINNG_REQUEST_SERVER > m_server
Definition: api_server.h:93
Definition: kiid.h:49
static wxString GetLogsPath()
Gets a path to use for user-visible log files.
Definition: paths.cpp:412
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:423
bool m_EnableAPILogging
Log IPC API requests and responses.
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
Definition: api_utils.cpp:26
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398