xref: /aosp_15_r20/external/pigweed/pw_system/public/pw_system/file_manager.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 #pragma once
15 
16 #include <string_view>
17 
18 #include "pw_persistent_ram/flat_file_system_entry.h"
19 #include "pw_system/config.h"
20 #include "pw_system/transfer_handlers.h"
21 
22 namespace pw::system {
23 
24 class FileManager {
25  public:
26   // Each transfer handler ID corresponds 1:1 with a transfer handler and
27   // filesystem element pair.  The ID must be unique and increment from 0 to
28   // ensure no gaps in the FileManager handler & filesystem arrays.
29   // NOTE: the enumerators should never have values defined, to ensure they
30   // increment from zero and kNumFileSystemEntries is correct
31   enum TransferHandlerId : uint32_t {
32 #if PW_SYSTEM_ENABLE_CRASH_HANDLER
33     kCrashSnapshotTransferHandlerId,
34 #endif  // PW_SYSTEM_ENABLE_CRASH_HANDLER
35 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
36     kTraceTransferHandlerId,
37 #endif  // PW_SYSTEM_ENABLE_TRACE_SERVICE
38     kNumFileSystemEntries
39   };
40 
41 #if PW_SYSTEM_ENABLE_CRASH_HANDLER
42   static constexpr std::string_view kCrashSnapshotFilename{
43       "/snapshots/crash_0.snapshot"};
44 #endif  // PW_SYSTEM_ENABLE_CRASH_HANDLER
45 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
46   static constexpr std::string_view kTraceFilename{"/trace/0.bin"};
47 #endif  // PW_SYSTEM_ENABLE_TRACE_SERVICE
48 
49   FileManager();
50 
GetTransferHandlers()51   std::array<transfer::Handler*, kNumFileSystemEntries>& GetTransferHandlers() {
52     return transfer_handlers_;
53   }
54   std::array<file::FlatFileSystemService::Entry*, kNumFileSystemEntries>&
GetFileSystemEntries()55   GetFileSystemEntries() {
56     return file_system_entries_;
57   }
58 
59  private:
60 #if PW_SYSTEM_ENABLE_CRASH_HANDLER
61   CrashSnapshotBufferTransfer crash_snapshot_handler_;
62   persistent_ram::FlatFileSystemPersistentBufferEntry<
63       PW_SYSTEM_CRASH_SNAPSHOT_MEMORY_SIZE_BYTES>
64       crash_snapshot_filesystem_entry_;
65 #endif  // PW_SYSTEM_ENABLE_CRASH_HANDLER
66 
67 #if PW_SYSTEM_ENABLE_TRACE_SERVICE
68   TraceBufferTransfer trace_data_handler_;
69   persistent_ram::FlatFileSystemPersistentBufferEntry<
70       PW_TRACE_BUFFER_SIZE_BYTES>
71       trace_data_filesystem_entry_;
72 #endif  // PW_SYSTEM_ENABLE_TRACE_SERVICE
73 
74   std::array<transfer::Handler*, kNumFileSystemEntries> transfer_handlers_;
75   std::array<file::FlatFileSystemService::Entry*, kNumFileSystemEntries>
76       file_system_entries_;
77 };
78 
79 FileManager& GetFileManager();
80 
81 }  // namespace pw::system
82