1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <libminradio/sim/Filesystem.h>
18
19 #include <libminradio/sim/IccConstants.h>
20 #include <libminradio/sim/IccUtils.h>
21
22 #include <format>
23
24 namespace android::hardware::radio::minimal::sim {
25
26 using namespace ::android::hardware::radio::minimal::sim::constants;
27 using FileView = Filesystem::FileView;
28
29 namespace paths {
30
31 // 3GPP TS 51.011 10.7
32 const Filesystem::Path mf{MF_SIM_VAL, ""};
33 const Filesystem::Path fplmn{EF_FPLMN, MF_SIM + DF_ADF};
34 const Filesystem::Path iccid{EF_ICCID, MF_SIM};
35 const Filesystem::Path msisdn{EF_MSISDN, MF_SIM + DF_ADF};
36 const Filesystem::Path pl{EF_PL, MF_SIM};
37 const Filesystem::Path arr{EF_ARR, MF_SIM};
38 const Filesystem::Path ad{EF_AD, MF_SIM + DF_ADF};
39
40 } // namespace paths
41
Filesystem()42 Filesystem::Filesystem() {
43 write(paths::mf, ""); // Directories are not implemented.
44 write(paths::arr, "");
45 }
46
write(const Path & path,FileView contents)47 void Filesystem::write(const Path& path, FileView contents) {
48 std::unique_lock lck(mFilesGuard);
49 mFiles[path].assign(contents.begin(), contents.end()); // C++23: assign_range
50 mUpdates.insert(path.fileId);
51 }
52
write(const Path & path,std::string_view contents)53 void Filesystem::write(const Path& path, std::string_view contents) {
54 std::unique_lock lck(mFilesGuard);
55 mFiles[path].assign(contents.begin(), contents.end()); // C++23: assign_range
56 mUpdates.insert(path.fileId);
57 }
58
write(const Path & path,std::vector<uint8_t> && contents)59 void Filesystem::write(const Path& path, std::vector<uint8_t>&& contents) {
60 write(path, FileView(contents));
61 }
62
read(const Path & path) const63 std::optional<FileView> Filesystem::read(const Path& path) const {
64 std::unique_lock lck(mFilesGuard);
65 auto it = mFiles.find(path);
66 if (it == mFiles.end()) return std::nullopt;
67
68 return FileView(it->second);
69 }
70
writeBch(const Path & path,std::string_view contents)71 void Filesystem::writeBch(const Path& path, std::string_view contents) {
72 write(path, hexStringToBch(contents));
73 }
74
readBch(const Path & path) const75 std::optional<std::string> Filesystem::readBch(const Path& path) const {
76 auto contents = read(path);
77 if (!contents.has_value()) return std::nullopt;
78 return bchToHexString(*contents);
79 }
80
find(uint16_t fileId)81 std::optional<Filesystem::Path> Filesystem::find(uint16_t fileId) {
82 std::unique_lock lck(mFilesGuard);
83 for (auto& [path, content] : mFiles) {
84 if (path.fileId == fileId) return path;
85 }
86 return std::nullopt;
87 }
88
fetchAndClearUpdates()89 std::set<int32_t> Filesystem::fetchAndClearUpdates() {
90 std::unique_lock lck(mFilesGuard);
91 std::set<int32_t> result;
92 std::swap(result, mUpdates);
93 return result;
94 }
95
toString() const96 std::string Filesystem::Path::toString() const {
97 return std::format("{:s}/{:X}", pathId, fileId);
98 }
99
100 } // namespace android::hardware::radio::minimal::sim
101