aboutsummaryrefslogtreecommitdiff
path: root/pw_system/init.cc
blob: 96d5dad39ab82aeea9f233e0417c668087be248a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#define PW_LOG_MODULE_NAME "pw_system"

#include "pw_system/init.h"

#include "pw_log/log.h"
#include "pw_metric/global.h"
#include "pw_metric/metric_service_pwpb.h"
#include "pw_rpc/echo_service_pwpb.h"
#include "pw_system/config.h"
#include "pw_system/rpc_server.h"
#include "pw_system/target_hooks.h"
#include "pw_system/work_queue.h"
#include "pw_system_private/log.h"
#include "pw_thread/detached_thread.h"

#if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
#include "pw_system/transfer_service.h"
#endif  // PW_SYSTEM_ENABLE_TRANSFER_SERVICE

#if PW_SYSTEM_ENABLE_TRACE_SERVICE
#include "pw_system/file_service.h"
#include "pw_system/trace_service.h"
#include "pw_trace/trace.h"
#endif  // PW_SYSTEM_ENABLE_TRACE_SERVICE

#include "pw_system/file_manager.h"

#if PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
#include "pw_system/thread_snapshot_service.h"
#endif  // PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE

namespace pw::system {
namespace {
metric::MetricService metric_service(metric::global_metrics,
                                     metric::global_groups);

rpc::EchoService echo_service;

void InitImpl() {
#if PW_SYSTEM_ENABLE_TRACE_SERVICE
  // tracing is off by default, requring a user to enable it through
  // the trace service
  PW_TRACE_SET_ENABLED(false);
#endif

  PW_LOG_INFO("System init");

  // Setup logging.
  const Status status = GetLogThread().OpenUnrequestedLogStream(
      kLoggingRpcChannelId, GetRpcServer(), GetLogService());
  if (!status.ok()) {
    PW_LOG_ERROR("Error opening unrequested log streams %d",
                 static_cast<int>(status.code()));
  }

  PW_LOG_INFO("Registering RPC services");
  GetRpcServer().RegisterService(echo_service);
  GetRpcServer().RegisterService(GetLogService());
  GetRpcServer().RegisterService(metric_service);

#if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
  RegisterTransferService(GetRpcServer());
#endif  // PW_SYSTEM_ENABLE_TRANSFER_SERVICE

#if PW_SYSTEM_ENABLE_TRACE_SERVICE
  RegisterFileService(GetRpcServer());
  RegisterTraceService(GetRpcServer(), FileManager::kTraceTransferHandlerId);
#endif  // PW_SYSTEM_ENABLE_TRACE_SERVICE

#if PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE
  RegisterThreadSnapshotService(GetRpcServer());
#endif  // PW_SYSTEM_ENABLE_THREAD_SNAPSHOT_SERVICE

  PW_LOG_INFO("Starting threads");
  // Start threads.
  thread::DetachedThread(system::LogThreadOptions(), GetLogThread());
  thread::DetachedThread(system::RpcThreadOptions(), GetRpcDispatchThread());

#if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
  thread::DetachedThread(system::TransferThreadOptions(), GetTransferThread());
  InitTransferService();
#endif  // PW_SYSTEM_ENABLE_TRANSFER_SERVICE

  GetWorkQueue().CheckPushWork(UserAppInit);
}

}  // namespace

void Init() {
  thread::DetachedThread(system::WorkQueueThreadOptions(), GetWorkQueue());
  GetWorkQueue().CheckPushWork(InitImpl);
}

}  // namespace pw::system