nfp.cpp 12 KB

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