1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #define PW_LOG_MODULE_NAME "pw_system"
15
16 #include "pw_system/init.h"
17
18 #include "pw_log/log.h"
19 #include "pw_metric/global.h"
20 #include "pw_metric/metric_service_pwpb.h"
21 #include "pw_rpc/echo_service_pwpb.h"
22 #include "pw_system/config.h"
23 #include "pw_system/rpc_server.h"
24 #include "pw_system/target_hooks.h"
25 #include "pw_system/work_queue.h"
26 #include "pw_system_private/log.h"
27 #include "pw_thread/detached_thread.h"
28
29 #if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
30 #include "pw_system/transfer_service.h"
31 #endif // PW_SYSTEM_ENABLE_TRANSFER_SERVICE
32
33 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
34 #include "pw_system/file_service.h"
35 #include "pw_system/trace_service.h"
36 #include "pw_trace/trace.h"
37 #endif // PW_SYSTEM_ENABLE_TRACE_SERVICE
38
39 #include "pw_system/file_manager.h"
40
41 #if PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
42 #include "pw_system/thread_snapshot_service.h"
43 #endif // PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
44
45 namespace pw::system {
46 namespace {
47 metric::MetricService metric_service(metric::global_metrics,
48 metric::global_groups);
49
50 rpc::EchoService echo_service;
51
InitImpl()52 void InitImpl() {
53 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
54 // tracing is off by default, requring a user to enable it through
55 // the trace service
56 PW_TRACE_SET_ENABLED(false);
57 #endif
58
59 PW_LOG_INFO("System init");
60
61 // Setup logging.
62 const Status status = GetLogThread().OpenUnrequestedLogStream(
63 kLoggingRpcChannelId, GetRpcServer(), GetLogService());
64 if (!status.ok()) {
65 PW_LOG_ERROR("Error opening unrequested log streams %d",
66 static_cast<int>(status.code()));
67 }
68
69 PW_LOG_INFO("Registering RPC services");
70 GetRpcServer().RegisterService(echo_service);
71 GetRpcServer().RegisterService(GetLogService());
72 GetRpcServer().RegisterService(metric_service);
73
74 #if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
75 RegisterTransferService(GetRpcServer());
76 #endif // PW_SYSTEM_ENABLE_TRANSFER_SERVICE
77
78 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
79 RegisterFileService(GetRpcServer());
80 RegisterTraceService(GetRpcServer(), FileManager::kTraceTransferHandlerId);
81 #endif // PW_SYSTEM_ENABLE_TRACE_SERVICE
82
83 #if PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
84 RegisterThreadSnapshotService(GetRpcServer());
85 #endif // PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
86
87 PW_LOG_INFO("Starting threads");
88 // Start threads.
89 thread::DetachedThread(system::LogThreadOptions(), GetLogThread());
90 thread::DetachedThread(system::RpcThreadOptions(), GetRpcDispatchThread());
91
92 #if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
93 thread::DetachedThread(system::TransferThreadOptions(), GetTransferThread());
94 InitTransferService();
95 #endif // PW_SYSTEM_ENABLE_TRANSFER_SERVICE
96
97 GetWorkQueue().CheckPushWork(UserAppInit);
98 }
99
100 } // namespace
101
Init()102 void Init() {
103 thread::DetachedThread(system::WorkQueueThreadOptions(), GetWorkQueue());
104 GetWorkQueue().CheckPushWork(InitImpl);
105 }
106
107 } // namespace pw::system
108