nfp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <atomic>
  6. #include "common/logging/log.h"
  7. #include "core/core.h"
  8. #include "core/hle/ipc_helpers.h"
  9. #include "core/hle/kernel/kernel.h"
  10. #include "core/hle/kernel/readable_event.h"
  11. #include "core/hle/kernel/thread.h"
  12. #include "core/hle/kernel/writable_event.h"
  13. #include "core/hle/lock.h"
  14. #include "core/hle/service/nfp/nfp.h"
  15. #include "core/hle/service/nfp/nfp_user.h"
  16. namespace Service::NFP {
  17. namespace ErrCodes {
  18. constexpr ResultCode ERR_NO_APPLICATION_AREA(ErrorModule::NFP, 152);
  19. } // namespace ErrCodes
  20. Module::Interface::Interface(std::shared_ptr<Module> module, Core::System& system, const char* name)
  21. : ServiceFramework(name), module(std::move(module)), system(system) {
  22. auto& kernel = system.Kernel();
  23. nfc_tag_load = Kernel::WritableEvent::CreateEventPair(kernel, "IUser:NFCTagDetected");
  24. }
  25. Module::Interface::~Interface() = default;
  26. class IUser final : public ServiceFramework<IUser> {
  27. public:
  28. IUser(Module::Interface& nfp_interface, Core::System& system)
  29. : ServiceFramework("NFP::IUser"), nfp_interface(nfp_interface) {
  30. static const FunctionInfo functions[] = {
  31. {0, &IUser::Initialize, "Initialize"},
  32. {1, &IUser::Finalize, "Finalize"},
  33. {2, &IUser::ListDevices, "ListDevices"},
  34. {3, &IUser::StartDetection, "StartDetection"},
  35. {4, &IUser::StopDetection, "StopDetection"},
  36. {5, &IUser::Mount, "Mount"},
  37. {6, &IUser::Unmount, "Unmount"},
  38. {7, &IUser::OpenApplicationArea, "OpenApplicationArea"},
  39. {8, &IUser::GetApplicationArea, "GetApplicationArea"},
  40. {9, nullptr, "SetApplicationArea"},
  41. {10, nullptr, "Flush"},
  42. {11, nullptr, "Restore"},
  43. {12, nullptr, "CreateApplicationArea"},
  44. {13, &IUser::GetTagInfo, "GetTagInfo"},
  45. {14, &IUser::GetRegisterInfo, "GetRegisterInfo"},
  46. {15, &IUser::GetCommonInfo, "GetCommonInfo"},
  47. {16, &IUser::GetModelInfo, "GetModelInfo"},
  48. {17, &IUser::AttachActivateEvent, "AttachActivateEvent"},
  49. {18, &IUser::AttachDeactivateEvent, "AttachDeactivateEvent"},
  50. {19, &IUser::GetState, "GetState"},
  51. {20, &IUser::GetDeviceState, "GetDeviceState"},
  52. {21, &IUser::GetNpadId, "GetNpadId"},
  53. {22, &IUser::GetApplicationAreaSize, "GetApplicationAreaSize"},
  54. {23, &IUser::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
  55. {24, nullptr, "RecreateApplicationArea"},
  56. };
  57. RegisterHandlers(functions);
  58. auto& kernel = system.Kernel();
  59. deactivate_event = Kernel::WritableEvent::CreateEventPair(kernel, "IUser:DeactivateEvent");
  60. availability_change_event =
  61. Kernel::WritableEvent::CreateEventPair(kernel, "IUser:AvailabilityChangeEvent");
  62. }
  63. private:
  64. struct TagInfo {
  65. std::array<u8, 10> uuid;
  66. u8 uuid_length; // TODO(ogniK): Figure out if this is actual the uuid length or does it
  67. // mean something else
  68. std::array<u8, 0x15> padding_1;
  69. u32_le protocol;
  70. u32_le tag_type;
  71. std::array<u8, 0x2c> padding_2;
  72. };
  73. static_assert(sizeof(TagInfo) == 0x54, "TagInfo is an invalid size");
  74. enum class State : u32 {
  75. NonInitialized = 0,
  76. Initialized = 1,
  77. };
  78. enum class DeviceState : u32 {
  79. Initialized = 0,
  80. SearchingForTag = 1,
  81. TagFound = 2,
  82. TagRemoved = 3,
  83. TagNearby = 4,
  84. Unknown5 = 5,
  85. Finalized = 6
  86. };
  87. struct CommonInfo {
  88. u16_be last_write_year;
  89. u8 last_write_month;
  90. u8 last_write_day;
  91. u16_be write_counter;
  92. u16_be version;
  93. u32_be application_area_size;
  94. INSERT_PADDING_BYTES(0x34);
  95. };
  96. static_assert(sizeof(CommonInfo) == 0x40, "CommonInfo is an invalid size");
  97. void Initialize(Kernel::HLERequestContext& ctx) {
  98. LOG_DEBUG(Service_NFC, "called");
  99. IPC::ResponseBuilder rb{ctx, 2, 0};
  100. rb.Push(RESULT_SUCCESS);
  101. state = State::Initialized;
  102. }
  103. void GetState(Kernel::HLERequestContext& ctx) {
  104. LOG_DEBUG(Service_NFC, "called");
  105. IPC::ResponseBuilder rb{ctx, 3, 0};
  106. rb.Push(RESULT_SUCCESS);
  107. rb.PushRaw<u32>(static_cast<u32>(state));
  108. }
  109. void ListDevices(Kernel::HLERequestContext& ctx) {
  110. IPC::RequestParser rp{ctx};
  111. const u32 array_size = rp.Pop<u32>();
  112. LOG_DEBUG(Service_NFP, "called, array_size={}", array_size);
  113. ctx.WriteBuffer(device_handle);
  114. IPC::ResponseBuilder rb{ctx, 3};
  115. rb.Push(RESULT_SUCCESS);
  116. rb.Push<u32>(1);
  117. }
  118. void GetNpadId(Kernel::HLERequestContext& ctx) {
  119. IPC::RequestParser rp{ctx};
  120. const u64 dev_handle = rp.Pop<u64>();
  121. LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle);
  122. IPC::ResponseBuilder rb{ctx, 3};
  123. rb.Push(RESULT_SUCCESS);
  124. rb.Push<u32>(npad_id);
  125. }
  126. void AttachActivateEvent(Kernel::HLERequestContext& ctx) {
  127. IPC::RequestParser rp{ctx};
  128. const u64 dev_handle = rp.Pop<u64>();
  129. LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle);
  130. IPC::ResponseBuilder rb{ctx, 2, 1};
  131. rb.Push(RESULT_SUCCESS);
  132. rb.PushCopyObjects(nfp_interface.GetNFCEvent());
  133. has_attached_handle = true;
  134. }
  135. void AttachDeactivateEvent(Kernel::HLERequestContext& ctx) {
  136. IPC::RequestParser rp{ctx};
  137. const u64 dev_handle = rp.Pop<u64>();
  138. LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle);
  139. IPC::ResponseBuilder rb{ctx, 2, 1};
  140. rb.Push(RESULT_SUCCESS);
  141. rb.PushCopyObjects(deactivate_event.readable);
  142. }
  143. void StopDetection(Kernel::HLERequestContext& ctx) {
  144. LOG_DEBUG(Service_NFP, "called");
  145. switch (device_state) {
  146. case DeviceState::TagFound:
  147. case DeviceState::TagNearby:
  148. deactivate_event.writable->Signal();
  149. device_state = DeviceState::Initialized;
  150. break;
  151. case DeviceState::SearchingForTag:
  152. case DeviceState::TagRemoved:
  153. device_state = DeviceState::Initialized;
  154. break;
  155. default:
  156. break;
  157. }
  158. IPC::ResponseBuilder rb{ctx, 2};
  159. rb.Push(RESULT_SUCCESS);
  160. }
  161. void GetDeviceState(Kernel::HLERequestContext& ctx) {
  162. LOG_DEBUG(Service_NFP, "called");
  163. auto nfc_event = nfp_interface.GetNFCEvent();
  164. if (!nfc_event->ShouldWait(&ctx.GetThread()) && !has_attached_handle) {
  165. device_state = DeviceState::TagFound;
  166. nfc_event->Clear();
  167. }
  168. IPC::ResponseBuilder rb{ctx, 3};
  169. rb.Push(RESULT_SUCCESS);
  170. rb.Push<u32>(static_cast<u32>(device_state));
  171. }
  172. void StartDetection(Kernel::HLERequestContext& ctx) {
  173. LOG_DEBUG(Service_NFP, "called");
  174. if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) {
  175. device_state = DeviceState::SearchingForTag;
  176. }
  177. IPC::ResponseBuilder rb{ctx, 2};
  178. rb.Push(RESULT_SUCCESS);
  179. }
  180. void GetTagInfo(Kernel::HLERequestContext& ctx) {
  181. LOG_DEBUG(Service_NFP, "called");
  182. IPC::ResponseBuilder rb{ctx, 2};
  183. const auto& amiibo = nfp_interface.GetAmiiboBuffer();
  184. const TagInfo tag_info{
  185. .uuid = amiibo.uuid,
  186. .uuid_length = static_cast<u8>(tag_info.uuid.size()),
  187. .padding_1 = {},
  188. .protocol = 1, // TODO(ogniK): Figure out actual values
  189. .tag_type = 2,
  190. .padding_2 = {},
  191. };
  192. ctx.WriteBuffer(tag_info);
  193. rb.Push(RESULT_SUCCESS);
  194. }
  195. void Mount(Kernel::HLERequestContext& ctx) {
  196. LOG_DEBUG(Service_NFP, "called");
  197. device_state = DeviceState::TagNearby;
  198. IPC::ResponseBuilder rb{ctx, 2};
  199. rb.Push(RESULT_SUCCESS);
  200. }
  201. void GetModelInfo(Kernel::HLERequestContext& ctx) {
  202. LOG_DEBUG(Service_NFP, "called");
  203. IPC::ResponseBuilder rb{ctx, 2};
  204. const auto& amiibo = nfp_interface.GetAmiiboBuffer();
  205. ctx.WriteBuffer(amiibo.model_info);
  206. rb.Push(RESULT_SUCCESS);
  207. }
  208. void Unmount(Kernel::HLERequestContext& ctx) {
  209. LOG_DEBUG(Service_NFP, "called");
  210. device_state = DeviceState::TagFound;
  211. IPC::ResponseBuilder rb{ctx, 2};
  212. rb.Push(RESULT_SUCCESS);
  213. }
  214. void Finalize(Kernel::HLERequestContext& ctx) {
  215. LOG_DEBUG(Service_NFP, "called");
  216. device_state = DeviceState::Finalized;
  217. IPC::ResponseBuilder rb{ctx, 2};
  218. rb.Push(RESULT_SUCCESS);
  219. }
  220. void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) {
  221. LOG_WARNING(Service_NFP, "(STUBBED) called");
  222. IPC::ResponseBuilder rb{ctx, 2, 1};
  223. rb.Push(RESULT_SUCCESS);
  224. rb.PushCopyObjects(availability_change_event.readable);
  225. }
  226. void GetRegisterInfo(Kernel::HLERequestContext& ctx) {
  227. LOG_WARNING(Service_NFP, "(STUBBED) called");
  228. // TODO(ogniK): Pull Mii and owner data from amiibo
  229. IPC::ResponseBuilder rb{ctx, 2};
  230. rb.Push(RESULT_SUCCESS);
  231. }
  232. void GetCommonInfo(Kernel::HLERequestContext& ctx) {
  233. LOG_WARNING(Service_NFP, "(STUBBED) called");
  234. // TODO(ogniK): Pull common information from amiibo
  235. CommonInfo common_info{};
  236. common_info.application_area_size = 0;
  237. ctx.WriteBuffer(common_info);
  238. IPC::ResponseBuilder rb{ctx, 2};
  239. rb.Push(RESULT_SUCCESS);
  240. }
  241. void OpenApplicationArea(Kernel::HLERequestContext& ctx) {
  242. LOG_WARNING(Service_NFP, "(STUBBED) called");
  243. IPC::ResponseBuilder rb{ctx, 2};
  244. rb.Push(ErrCodes::ERR_NO_APPLICATION_AREA);
  245. }
  246. void GetApplicationAreaSize(Kernel::HLERequestContext& ctx) {
  247. LOG_WARNING(Service_NFP, "(STUBBED) called");
  248. // We don't need to worry about this since we can just open the file
  249. IPC::ResponseBuilder rb{ctx, 3};
  250. rb.Push(RESULT_SUCCESS);
  251. rb.PushRaw<u32>(0); // This is from the GetCommonInfo stub
  252. }
  253. void GetApplicationArea(Kernel::HLERequestContext& ctx) {
  254. LOG_WARNING(Service_NFP, "(STUBBED) called");
  255. // TODO(ogniK): Pull application area from amiibo
  256. IPC::ResponseBuilder rb{ctx, 3};
  257. rb.Push(RESULT_SUCCESS);
  258. rb.PushRaw<u32>(0); // This is from the GetCommonInfo stub
  259. }
  260. bool has_attached_handle{};
  261. const u64 device_handle{0}; // Npad device 1
  262. const u32 npad_id{0}; // Player 1 controller
  263. State state{State::NonInitialized};
  264. DeviceState device_state{DeviceState::Initialized};
  265. Kernel::EventPair deactivate_event;
  266. Kernel::EventPair availability_change_event;
  267. const Module::Interface& nfp_interface;
  268. };
  269. void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) {
  270. LOG_DEBUG(Service_NFP, "called");
  271. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  272. rb.Push(RESULT_SUCCESS);
  273. rb.PushIpcInterface<IUser>(*this, system);
  274. }
  275. bool Module::Interface::LoadAmiibo(const std::vector<u8>& buffer) {
  276. std::lock_guard lock{HLE::g_hle_lock};
  277. if (buffer.size() < sizeof(AmiiboFile)) {
  278. return false;
  279. }
  280. std::memcpy(&amiibo, buffer.data(), sizeof(amiibo));
  281. nfc_tag_load.writable->Signal();
  282. return true;
  283. }
  284. const std::shared_ptr<Kernel::ReadableEvent>& Module::Interface::GetNFCEvent() const {
  285. return nfc_tag_load.readable;
  286. }
  287. const Module::Interface::AmiiboFile& Module::Interface::GetAmiiboBuffer() const {
  288. return amiibo;
  289. }
  290. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) {
  291. auto module = std::make_shared<Module>();
  292. std::make_shared<NFP_User>(module, system)->InstallAsService(service_manager);
  293. }
  294. } // namespace Service::NFP