• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_THREAD_SNAPSHOT_SERVICE
30 #include "pw_system/thread_snapshot_service.h"
31 #endif  // PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
32 
33 namespace pw::system {
34 namespace {
35 metric::MetricService metric_service(metric::global_metrics,
36                                      metric::global_groups);
37 
38 rpc::EchoService echo_service;
39 
InitImpl()40 void InitImpl() {
41   PW_LOG_INFO("System init");
42 
43   // Setup logging.
44   const Status status = GetLogThread().OpenUnrequestedLogStream(
45       kDefaultRpcChannelId, GetRpcServer(), GetLogService());
46   if (!status.ok()) {
47     PW_LOG_ERROR("Error opening unrequested log streams %d",
48                  static_cast<int>(status.code()));
49   }
50 
51   PW_LOG_INFO("Registering RPC services");
52   GetRpcServer().RegisterService(echo_service);
53   GetRpcServer().RegisterService(GetLogService());
54   GetRpcServer().RegisterService(metric_service);
55 #if PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
56   RegisterThreadSnapshotService(GetRpcServer());
57 #endif  // PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
58 
59   PW_LOG_INFO("Starting threads");
60   // Start threads.
61   thread::DetachedThread(system::LogThreadOptions(), GetLogThread());
62   thread::DetachedThread(system::RpcThreadOptions(), GetRpcDispatchThread());
63 
64   GetWorkQueue().CheckPushWork(UserAppInit);
65 }
66 
67 }  // namespace
68 
Init()69 void Init() {
70   thread::DetachedThread(system::WorkQueueThreadOptions(), GetWorkQueue());
71   GetWorkQueue().CheckPushWork(InitImpl);
72 }
73 
74 }  // namespace pw::system
75