1 // Copyright 2023 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/file_manager.h"
16
17 #if PW_SYSTEM_ENABLE_CRASH_HANDLER
18 #include "pw_system/crash_snapshot.h"
19 #endif // PW_SYSTEM_ENABLE_CRASH_HANDLER
20
21 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
22 #include "pw_system/trace_service.h"
23 #endif // PW_SYSTEM_ENABLE_TRACE_SERVICE
24
25 namespace pw::system {
26
27 namespace {
28 FileManager file_manager;
29 }
30
GetFileManager()31 FileManager& GetFileManager() { return file_manager; }
32
FileManager()33 FileManager::FileManager()
34 :
35 #if PW_SYSTEM_ENABLE_CRASH_HANDLER
36 crash_snapshot_handler_(kCrashSnapshotTransferHandlerId,
37 GetCrashSnapshotBuffer()),
38 crash_snapshot_filesystem_entry_(
39 kCrashSnapshotFilename,
40 kCrashSnapshotTransferHandlerId,
41 file::FlatFileSystemService::Entry::FilePermissions::READ,
42 GetCrashSnapshotBuffer()),
43 #endif // PW_SYSTEM_ENABLE_CRASH_HANDLER
44 // TODO: b/354777918 - this will fail if both services disabled. Need to come
45 // up with a more scalable pattern for registering files.
46 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
47 trace_data_handler_(kTraceTransferHandlerId, GetTraceData()),
48 trace_data_filesystem_entry_(
49 kTraceFilename,
50 kTraceTransferHandlerId,
51 file::FlatFileSystemService::Entry::FilePermissions::READ,
52 GetTraceData())
53 #endif // PW_SYSTEM_ENABLE_TRACE_SERVICE
54 {
55 // Every handler & filesystem element must be added to the collections, using
56 // the associated handler ID as the index.
57 #if PW_SYSTEM_ENABLE_CRASH_HANDLER
58 transfer_handlers_[kCrashSnapshotTransferHandlerId] =
59 &crash_snapshot_handler_;
60 file_system_entries_[kCrashSnapshotTransferHandlerId] =
61 &crash_snapshot_filesystem_entry_;
62 #endif // PW_SYSTEM_ENABLE_CRASH_HANDLER
63 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
64 transfer_handlers_[kTraceTransferHandlerId] = &trace_data_handler_;
65 file_system_entries_[kTraceTransferHandlerId] = &trace_data_filesystem_entry_;
66 #endif // PW_SYSTEM_ENABLE_TRACE_SERVICE
67 }
68
69 } // namespace pw::system
70