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
15 #include "pw_system/init.h"
16
17 #include "pw_log/log.h"
18 #include "pw_rpc/echo_service_nanopb.h"
19 #include "pw_system/rpc_server.h"
20 #include "pw_system/target_hooks.h"
21 #include "pw_system/work_queue.h"
22 #include "pw_system_private/log.h"
23 #include "pw_thread/detached_thread.h"
24
25 namespace pw::system {
26 namespace {
27 rpc::EchoService echo_service;
28
InitImpl()29 void InitImpl() {
30 PW_LOG_INFO("System init");
31
32 // Setup logging.
33 const Status status = GetLogThread().OpenUnrequestedLogStream(
34 kDefaultRpcChannelId, GetRpcServer(), GetLogService());
35 if (!status.ok()) {
36 PW_LOG_ERROR("Error opening unrequested log streams %d",
37 static_cast<int>(status.code()));
38 }
39
40 PW_LOG_INFO("Registering RPC services");
41 GetRpcServer().RegisterService(echo_service);
42 GetRpcServer().RegisterService(GetLogService());
43
44 PW_LOG_INFO("Starting threads");
45 // Start threads.
46 thread::DetachedThread(system::LogThreadOptions(), GetLogThread());
47 thread::DetachedThread(system::RpcThreadOptions(), GetRpcDispatchThread());
48
49 GetWorkQueue().CheckPushWork(UserAppInit);
50 }
51
52 } // namespace
53
Init()54 void Init() {
55 thread::DetachedThread(system::WorkQueueThreadOptions(), GetWorkQueue());
56 GetWorkQueue().CheckPushWork(InitImpl);
57 }
58
59 } // namespace pw::system
60