xref: /aosp_15_r20/external/pigweed/pw_system/init.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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/device_service.h"
24 #include "pw_system/log.h"
25 #include "pw_system/rpc_server.h"
26 #include "pw_system/target_hooks.h"
27 #include "pw_system/work_queue.h"
28 #include "pw_thread/detached_thread.h"
29 
30 #if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
31 #include "pw_system/file_service.h"
32 #include "pw_system/transfer_service.h"
33 #endif  // PW_SYSTEM_ENABLE_TRANSFER_SERVICE
34 
35 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
36 #include "pw_system/trace_service.h"
37 #include "pw_trace/trace.h"
38 #endif  // PW_SYSTEM_ENABLE_TRACE_SERVICE
39 
40 #include "pw_system/file_manager.h"
41 
42 #if PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
43 #include "pw_system/thread_snapshot_service.h"
44 #endif  // PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
45 
46 #if PW_SYSTEM_ENABLE_CRASH_HANDLER
47 #include "pw_system/crash_handler.h"
48 #include "pw_system/crash_snapshot.h"
49 #endif  // PW_SYSTEM_ENABLE_CRASH_HANDLER
50 
51 namespace pw::system {
52 namespace {
53 metric::MetricService metric_service(metric::global_metrics,
54                                      metric::global_groups);
55 
56 rpc::EchoService echo_service;
57 
InitImpl()58 void InitImpl() {
59 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
60   // tracing is off by default, requring a user to enable it through
61   // the trace service
62   PW_TRACE_SET_ENABLED(false);
63 #endif
64 
65   PW_LOG_INFO("System init");
66 
67   // Setup logging.
68   const Status status = GetLogThread().OpenUnrequestedLogStream(
69       kLoggingRpcChannelId, GetRpcServer(), GetLogService());
70   if (!status.ok()) {
71     PW_LOG_ERROR("Error opening unrequested log streams %d",
72                  static_cast<int>(status.code()));
73   }
74 
75   PW_LOG_INFO("Registering RPC services");
76   GetRpcServer().RegisterService(echo_service);
77   GetRpcServer().RegisterService(GetLogService());
78   GetRpcServer().RegisterService(metric_service);
79   RegisterDeviceService(GetRpcServer());
80 
81 #if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
82   RegisterTransferService(GetRpcServer());
83   RegisterFileService(GetRpcServer());
84 #endif  // PW_SYSTEM_ENABLE_TRANSFER_SERVICE
85 
86 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
87   RegisterTraceService(GetRpcServer(), FileManager::kTraceTransferHandlerId);
88 #endif  // PW_SYSTEM_ENABLE_TRACE_SERVICE
89 
90 #if PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
91   RegisterThreadSnapshotService(GetRpcServer());
92 #endif  // PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
93 
94   PW_LOG_INFO("Starting threads");
95   // Start threads.
96   thread::DetachedThread(system::LogThreadOptions(), GetLogThread());
97   thread::DetachedThread(system::RpcThreadOptions(), GetRpcDispatchThread());
98 
99 #if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
100   thread::DetachedThread(system::TransferThreadOptions(), GetTransferThread());
101   InitTransferService();
102 #endif  // PW_SYSTEM_ENABLE_TRANSFER_SERVICE
103 
104   GetWorkQueue().CheckPushWork(UserAppInit);
105 }
106 
107 }  // namespace
108 
Init()109 void Init() {
110 #if PW_SYSTEM_ENABLE_CRASH_HANDLER
111   RegisterCrashHandler();
112 
113   if (HasCrashSnapshot()) {
114     PW_LOG_ERROR("==========================");
115     PW_LOG_ERROR("======CRASH DETECTED======");
116     PW_LOG_ERROR("==========================");
117     PW_LOG_ERROR("Crash snapshots available.");
118     PW_LOG_ERROR(
119         "Run `device.get_crash_snapshots()` to download and clear the "
120         "snapshots.");
121   } else {
122     PW_LOG_DEBUG("No crash snapshot");
123   }
124 #endif  // PW_SYSTEM_ENABLE_CRASH_HANDLER
125 
126   thread::DetachedThread(system::WorkQueueThreadOptions(), GetWorkQueue());
127   GetWorkQueue().CheckPushWork(InitImpl);
128 }
129 
130 }  // namespace pw::system
131