Carlos Chinchilla | ecf3d80 | 2021-12-08 12:35:54 -0800 | [diff] [blame] | 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" |
Armando Montanez | ce59a87 | 2022-03-16 11:14:28 -0700 | [diff] [blame] | 19 | #include "pw_system/rpc_server.h" |
Armando Montanez | 2c042f9 | 2021-12-15 14:55:36 -0800 | [diff] [blame] | 20 | #include "pw_system/target_hooks.h" |
| 21 | #include "pw_system/work_queue.h" |
Carlos Chinchilla | ecf3d80 | 2021-12-08 12:35:54 -0800 | [diff] [blame] | 22 | #include "pw_system_private/log.h" |
Carlos Chinchilla | ecf3d80 | 2021-12-08 12:35:54 -0800 | [diff] [blame] | 23 | #include "pw_thread/detached_thread.h" |
| 24 | |
| 25 | namespace pw::system { |
| 26 | namespace { |
| 27 | rpc::EchoService echo_service; |
Carlos Chinchilla | ecf3d80 | 2021-12-08 12:35:54 -0800 | [diff] [blame] | 28 | |
Armando Montanez | 2c042f9 | 2021-12-15 14:55:36 -0800 | [diff] [blame] | 29 | void InitImpl() { |
Carlos Chinchilla | ecf3d80 | 2021-12-08 12:35:54 -0800 | [diff] [blame] | 30 | PW_LOG_INFO("System init"); |
| 31 | |
| 32 | // Setup logging. |
| 33 | const Status status = GetLogThread().OpenUnrequestedLogStream( |
Armando Montanez | ce59a87 | 2022-03-16 11:14:28 -0700 | [diff] [blame] | 34 | kDefaultRpcChannelId, GetRpcServer(), GetLogService()); |
Carlos Chinchilla | ecf3d80 | 2021-12-08 12:35:54 -0800 | [diff] [blame] | 35 | if (!status.ok()) { |
Carlos Chinchilla | 4021c35 | 2021-12-15 16:08:33 -0800 | [diff] [blame] | 36 | PW_LOG_ERROR("Error opening unrequested log streams %d", |
| 37 | static_cast<int>(status.code())); |
Carlos Chinchilla | ecf3d80 | 2021-12-08 12:35:54 -0800 | [diff] [blame] | 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()); |
Armando Montanez | 2c042f9 | 2021-12-15 14:55:36 -0800 | [diff] [blame] | 48 | |
| 49 | GetWorkQueue().CheckPushWork(UserAppInit); |
| 50 | } |
| 51 | |
| 52 | } // namespace |
| 53 | |
| 54 | void Init() { |
| 55 | thread::DetachedThread(system::WorkQueueThreadOptions(), GetWorkQueue()); |
| 56 | GetWorkQueue().CheckPushWork(InitImpl); |
Carlos Chinchilla | ecf3d80 | 2021-12-08 12:35:54 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } // namespace pw::system |