nfp_interface.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/logging/log.h"
  4. #include "core/core.h"
  5. #include "core/hid/hid_types.h"
  6. #include "core/hle/kernel/k_event.h"
  7. #include "core/hle/service/ipc_helpers.h"
  8. #include "core/hle/service/nfc/common/device.h"
  9. #include "core/hle/service/nfc/common/device_manager.h"
  10. #include "core/hle/service/nfc/nfc_types.h"
  11. #include "core/hle/service/nfp/nfp_interface.h"
  12. #include "core/hle/service/nfp/nfp_result.h"
  13. #include "core/hle/service/nfp/nfp_types.h"
  14. namespace Service::NFP {
  15. Interface::Interface(Core::System& system_, const char* name)
  16. : NfcInterface{system_, name, NFC::BackendType::Nfp} {}
  17. Interface::~Interface() = default;
  18. void Interface::InitializeSystem(HLERequestContext& ctx) {
  19. Initialize(ctx);
  20. }
  21. void Interface::InitializeDebug(HLERequestContext& ctx) {
  22. Initialize(ctx);
  23. }
  24. void Interface::FinalizeSystem(HLERequestContext& ctx) {
  25. Finalize(ctx);
  26. }
  27. void Interface::FinalizeDebug(HLERequestContext& ctx) {
  28. Finalize(ctx);
  29. }
  30. void Interface::Mount(HLERequestContext& ctx) {
  31. IPC::RequestParser rp{ctx};
  32. const auto device_handle{rp.Pop<u64>()};
  33. const auto model_type{rp.PopEnum<ModelType>()};
  34. const auto mount_target{rp.PopEnum<MountTarget>()};
  35. LOG_INFO(Service_NFP, "called, device_handle={}, model_type={}, mount_target={}", device_handle,
  36. model_type, mount_target);
  37. auto result = GetManager()->Mount(device_handle, model_type, mount_target);
  38. result = TranslateResultToServiceError(result);
  39. IPC::ResponseBuilder rb{ctx, 2};
  40. rb.Push(result);
  41. }
  42. void Interface::Unmount(HLERequestContext& ctx) {
  43. IPC::RequestParser rp{ctx};
  44. const auto device_handle{rp.Pop<u64>()};
  45. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  46. auto result = GetManager()->Unmount(device_handle);
  47. result = TranslateResultToServiceError(result);
  48. IPC::ResponseBuilder rb{ctx, 2};
  49. rb.Push(result);
  50. }
  51. void Interface::OpenApplicationArea(HLERequestContext& ctx) {
  52. IPC::RequestParser rp{ctx};
  53. const auto device_handle{rp.Pop<u64>()};
  54. const auto access_id{rp.Pop<u32>()};
  55. LOG_INFO(Service_NFP, "called, device_handle={}, access_id={}", device_handle, access_id);
  56. auto result = GetManager()->OpenApplicationArea(device_handle, access_id);
  57. result = TranslateResultToServiceError(result);
  58. IPC::ResponseBuilder rb{ctx, 2};
  59. rb.Push(result);
  60. }
  61. void Interface::GetApplicationArea(HLERequestContext& ctx) {
  62. IPC::RequestParser rp{ctx};
  63. const auto device_handle{rp.Pop<u64>()};
  64. const auto data_size = ctx.GetWriteBufferSize();
  65. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  66. std::vector<u8> data(data_size);
  67. auto result = GetManager()->GetApplicationArea(device_handle, data);
  68. result = TranslateResultToServiceError(result);
  69. if (result.IsError()) {
  70. IPC::ResponseBuilder rb{ctx, 2};
  71. rb.Push(result);
  72. return;
  73. }
  74. ctx.WriteBuffer(data);
  75. IPC::ResponseBuilder rb{ctx, 3};
  76. rb.Push(result);
  77. rb.Push(static_cast<u32>(data_size));
  78. }
  79. void Interface::SetApplicationArea(HLERequestContext& ctx) {
  80. IPC::RequestParser rp{ctx};
  81. const auto device_handle{rp.Pop<u64>()};
  82. const auto data{ctx.ReadBuffer()};
  83. LOG_INFO(Service_NFP, "called, device_handle={}, data_size={}", device_handle, data.size());
  84. auto result = GetManager()->SetApplicationArea(device_handle, data);
  85. result = TranslateResultToServiceError(result);
  86. IPC::ResponseBuilder rb{ctx, 2};
  87. rb.Push(result);
  88. }
  89. void Interface::Flush(HLERequestContext& ctx) {
  90. IPC::RequestParser rp{ctx};
  91. const auto device_handle{rp.Pop<u64>()};
  92. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  93. auto result = GetManager()->Flush(device_handle);
  94. result = TranslateResultToServiceError(result);
  95. IPC::ResponseBuilder rb{ctx, 2};
  96. rb.Push(result);
  97. }
  98. void Interface::Restore(HLERequestContext& ctx) {
  99. IPC::RequestParser rp{ctx};
  100. const auto device_handle{rp.Pop<u64>()};
  101. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  102. auto result = GetManager()->Restore(device_handle);
  103. result = TranslateResultToServiceError(result);
  104. IPC::ResponseBuilder rb{ctx, 2};
  105. rb.Push(result);
  106. }
  107. void Interface::CreateApplicationArea(HLERequestContext& ctx) {
  108. IPC::RequestParser rp{ctx};
  109. const auto device_handle{rp.Pop<u64>()};
  110. const auto access_id{rp.Pop<u32>()};
  111. const auto data{ctx.ReadBuffer()};
  112. LOG_INFO(Service_NFP, "called, device_handle={}, data_size={}, access_id={}", device_handle,
  113. access_id, data.size());
  114. auto result = GetManager()->CreateApplicationArea(device_handle, access_id, data);
  115. result = TranslateResultToServiceError(result);
  116. IPC::ResponseBuilder rb{ctx, 2};
  117. rb.Push(result);
  118. }
  119. void Interface::GetRegisterInfo(HLERequestContext& ctx) {
  120. IPC::RequestParser rp{ctx};
  121. const auto device_handle{rp.Pop<u64>()};
  122. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  123. RegisterInfo register_info{};
  124. auto result = GetManager()->GetRegisterInfo(device_handle, register_info);
  125. result = TranslateResultToServiceError(result);
  126. if (result.IsSuccess()) {
  127. ctx.WriteBuffer(register_info);
  128. }
  129. IPC::ResponseBuilder rb{ctx, 2};
  130. rb.Push(result);
  131. }
  132. void Interface::GetCommonInfo(HLERequestContext& ctx) {
  133. IPC::RequestParser rp{ctx};
  134. const auto device_handle{rp.Pop<u64>()};
  135. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  136. CommonInfo common_info{};
  137. auto result = GetManager()->GetCommonInfo(device_handle, common_info);
  138. result = TranslateResultToServiceError(result);
  139. if (result.IsSuccess()) {
  140. ctx.WriteBuffer(common_info);
  141. }
  142. IPC::ResponseBuilder rb{ctx, 2};
  143. rb.Push(result);
  144. }
  145. void Interface::GetModelInfo(HLERequestContext& ctx) {
  146. IPC::RequestParser rp{ctx};
  147. const auto device_handle{rp.Pop<u64>()};
  148. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  149. ModelInfo model_info{};
  150. auto result = GetManager()->GetModelInfo(device_handle, model_info);
  151. result = TranslateResultToServiceError(result);
  152. if (result.IsSuccess()) {
  153. ctx.WriteBuffer(model_info);
  154. }
  155. IPC::ResponseBuilder rb{ctx, 2};
  156. rb.Push(result);
  157. }
  158. void Interface::GetApplicationAreaSize(HLERequestContext& ctx) {
  159. IPC::RequestParser rp{ctx};
  160. const auto device_handle{rp.Pop<u64>()};
  161. LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle);
  162. IPC::ResponseBuilder rb{ctx, 3};
  163. rb.Push(ResultSuccess);
  164. rb.Push(GetManager()->GetApplicationAreaSize());
  165. }
  166. void Interface::RecreateApplicationArea(HLERequestContext& ctx) {
  167. IPC::RequestParser rp{ctx};
  168. const auto device_handle{rp.Pop<u64>()};
  169. const auto access_id{rp.Pop<u32>()};
  170. const auto data{ctx.ReadBuffer()};
  171. LOG_INFO(Service_NFP, "called, device_handle={}, data_size={}, access_id={}", device_handle,
  172. access_id, data.size());
  173. auto result = GetManager()->RecreateApplicationArea(device_handle, access_id, data);
  174. result = TranslateResultToServiceError(result);
  175. IPC::ResponseBuilder rb{ctx, 2};
  176. rb.Push(result);
  177. }
  178. void Interface::Format(HLERequestContext& ctx) {
  179. IPC::RequestParser rp{ctx};
  180. const auto device_handle{rp.Pop<u64>()};
  181. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  182. auto result = GetManager()->Format(device_handle);
  183. result = TranslateResultToServiceError(result);
  184. IPC::ResponseBuilder rb{ctx, 2};
  185. rb.Push(result);
  186. }
  187. void Interface::GetAdminInfo(HLERequestContext& ctx) {
  188. IPC::RequestParser rp{ctx};
  189. const auto device_handle{rp.Pop<u64>()};
  190. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  191. AdminInfo admin_info{};
  192. auto result = GetManager()->GetAdminInfo(device_handle, admin_info);
  193. result = TranslateResultToServiceError(result);
  194. if (result.IsSuccess()) {
  195. ctx.WriteBuffer(admin_info);
  196. }
  197. IPC::ResponseBuilder rb{ctx, 2};
  198. rb.Push(result);
  199. }
  200. void Interface::GetRegisterInfoPrivate(HLERequestContext& ctx) {
  201. IPC::RequestParser rp{ctx};
  202. const auto device_handle{rp.Pop<u64>()};
  203. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  204. RegisterInfoPrivate register_info{};
  205. auto result = GetManager()->GetRegisterInfoPrivate(device_handle, register_info);
  206. result = TranslateResultToServiceError(result);
  207. if (result.IsSuccess()) {
  208. ctx.WriteBuffer(register_info);
  209. }
  210. IPC::ResponseBuilder rb{ctx, 2};
  211. rb.Push(result);
  212. }
  213. void Interface::SetRegisterInfoPrivate(HLERequestContext& ctx) {
  214. IPC::RequestParser rp{ctx};
  215. const auto device_handle{rp.Pop<u64>()};
  216. const auto register_info_buffer{ctx.ReadBuffer()};
  217. LOG_INFO(Service_NFP, "called, device_handle={}, buffer_size={}", device_handle,
  218. register_info_buffer.size());
  219. RegisterInfoPrivate register_info{};
  220. memcpy(&register_info, register_info_buffer.data(), sizeof(RegisterInfoPrivate));
  221. auto result = GetManager()->SetRegisterInfoPrivate(device_handle, register_info);
  222. result = TranslateResultToServiceError(result);
  223. IPC::ResponseBuilder rb{ctx, 2};
  224. rb.Push(result);
  225. }
  226. void Interface::DeleteRegisterInfo(HLERequestContext& ctx) {
  227. IPC::RequestParser rp{ctx};
  228. const auto device_handle{rp.Pop<u64>()};
  229. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  230. auto result = GetManager()->DeleteRegisterInfo(device_handle);
  231. result = TranslateResultToServiceError(result);
  232. IPC::ResponseBuilder rb{ctx, 2};
  233. rb.Push(result);
  234. }
  235. void Interface::DeleteApplicationArea(HLERequestContext& ctx) {
  236. IPC::RequestParser rp{ctx};
  237. const auto device_handle{rp.Pop<u64>()};
  238. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  239. auto result = GetManager()->DeleteApplicationArea(device_handle);
  240. result = TranslateResultToServiceError(result);
  241. IPC::ResponseBuilder rb{ctx, 2};
  242. rb.Push(result);
  243. }
  244. void Interface::ExistsApplicationArea(HLERequestContext& ctx) {
  245. IPC::RequestParser rp{ctx};
  246. const auto device_handle{rp.Pop<u64>()};
  247. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  248. bool has_application_area = false;
  249. auto result = GetManager()->ExistsApplicationArea(device_handle, has_application_area);
  250. result = TranslateResultToServiceError(result);
  251. if (result.IsError()) {
  252. IPC::ResponseBuilder rb{ctx, 2};
  253. rb.Push(result);
  254. return;
  255. }
  256. IPC::ResponseBuilder rb{ctx, 3};
  257. rb.Push(result);
  258. rb.Push(has_application_area);
  259. }
  260. void Interface::GetAll(HLERequestContext& ctx) {
  261. IPC::RequestParser rp{ctx};
  262. const auto device_handle{rp.Pop<u64>()};
  263. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  264. NfpData nfp_data{};
  265. auto result = GetManager()->GetAll(device_handle, nfp_data);
  266. result = TranslateResultToServiceError(result);
  267. if (result.IsSuccess()) {
  268. ctx.WriteBuffer(nfp_data);
  269. }
  270. IPC::ResponseBuilder rb{ctx, 2};
  271. rb.Push(result);
  272. }
  273. void Interface::SetAll(HLERequestContext& ctx) {
  274. IPC::RequestParser rp{ctx};
  275. const auto device_handle{rp.Pop<u64>()};
  276. const auto nfp_data_buffer{ctx.ReadBuffer()};
  277. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  278. NfpData nfp_data{};
  279. memcpy(&nfp_data, nfp_data_buffer.data(), sizeof(NfpData));
  280. auto result = GetManager()->SetAll(device_handle, nfp_data);
  281. result = TranslateResultToServiceError(result);
  282. IPC::ResponseBuilder rb{ctx, 2};
  283. rb.Push(result);
  284. }
  285. void Interface::FlushDebug(HLERequestContext& ctx) {
  286. IPC::RequestParser rp{ctx};
  287. const auto device_handle{rp.Pop<u64>()};
  288. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  289. auto result = GetManager()->FlushDebug(device_handle);
  290. result = TranslateResultToServiceError(result);
  291. IPC::ResponseBuilder rb{ctx, 2};
  292. rb.Push(result);
  293. }
  294. void Interface::BreakTag(HLERequestContext& ctx) {
  295. IPC::RequestParser rp{ctx};
  296. const auto device_handle{rp.Pop<u64>()};
  297. const auto break_type{rp.PopEnum<BreakType>()};
  298. LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, break_type={}", device_handle,
  299. break_type);
  300. auto result = GetManager()->BreakTag(device_handle, break_type);
  301. result = TranslateResultToServiceError(result);
  302. IPC::ResponseBuilder rb{ctx, 2};
  303. rb.Push(result);
  304. }
  305. void Interface::ReadBackupData(HLERequestContext& ctx) {
  306. IPC::RequestParser rp{ctx};
  307. const auto device_handle{rp.Pop<u64>()};
  308. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  309. std::vector<u8> backup_data{};
  310. auto result = GetManager()->ReadBackupData(device_handle, backup_data);
  311. result = TranslateResultToServiceError(result);
  312. if (result.IsSuccess()) {
  313. ctx.WriteBuffer(backup_data);
  314. }
  315. IPC::ResponseBuilder rb{ctx, 2};
  316. rb.Push(result);
  317. }
  318. void Interface::WriteBackupData(HLERequestContext& ctx) {
  319. IPC::RequestParser rp{ctx};
  320. const auto device_handle{rp.Pop<u64>()};
  321. const auto backup_data_buffer{ctx.ReadBuffer()};
  322. LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
  323. auto result = GetManager()->WriteBackupData(device_handle, backup_data_buffer);
  324. result = TranslateResultToServiceError(result);
  325. IPC::ResponseBuilder rb{ctx, 2};
  326. rb.Push(result);
  327. }
  328. void Interface::WriteNtf(HLERequestContext& ctx) {
  329. IPC::RequestParser rp{ctx};
  330. const auto device_handle{rp.Pop<u64>()};
  331. const auto write_type{rp.PopEnum<WriteType>()};
  332. const auto ntf_data_buffer{ctx.ReadBuffer()};
  333. LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}", device_handle);
  334. auto result = GetManager()->WriteNtf(device_handle, write_type, ntf_data_buffer);
  335. result = TranslateResultToServiceError(result);
  336. IPC::ResponseBuilder rb{ctx, 2};
  337. rb.Push(result);
  338. }
  339. } // namespace Service::NFP