nfp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/event.h"
  9. #include "core/hle/lock.h"
  10. #include "core/hle/service/hid/hid.h"
  11. #include "core/hle/service/nfp/nfp.h"
  12. #include "core/hle/service/nfp/nfp_user.h"
  13. namespace Service::NFP {
  14. namespace ErrCodes {
  15. constexpr ResultCode ERR_TAG_FAILED(ErrorModule::NFP,
  16. -1); // TODO(ogniK): Find the actual error code
  17. }
  18. Module::Interface::Interface(std::shared_ptr<Module> module, const char* name)
  19. : ServiceFramework(name), module(std::move(module)) {
  20. auto& kernel = Core::System::GetInstance().Kernel();
  21. nfc_tag_load =
  22. Kernel::Event::Create(kernel, Kernel::ResetType::OneShot, "IUser:NFCTagDetected");
  23. }
  24. Module::Interface::~Interface() = default;
  25. class IUser final : public ServiceFramework<IUser> {
  26. public:
  27. IUser(Module::Interface& nfp_interface)
  28. : ServiceFramework("NFP::IUser"), nfp_interface(nfp_interface) {
  29. static const FunctionInfo functions[] = {
  30. {0, &IUser::Initialize, "Initialize"},
  31. {1, &IUser::Finalize, "Finalize"},
  32. {2, &IUser::ListDevices, "ListDevices"},
  33. {3, &IUser::StartDetection, "StartDetection"},
  34. {4, &IUser::StopDetection, "StopDetection"},
  35. {5, &IUser::Mount, "Mount"},
  36. {6, &IUser::Unmount, "Unmount"},
  37. {7, &IUser::OpenApplicationArea, "OpenApplicationArea"},
  38. {8, &IUser::GetApplicationArea, "GetApplicationArea"},
  39. {9, nullptr, "SetApplicationArea"},
  40. {10, nullptr, "Flush"},
  41. {11, nullptr, "Restore"},
  42. {12, nullptr, "CreateApplicationArea"},
  43. {13, &IUser::GetTagInfo, "GetTagInfo"},
  44. {14, &IUser::GetRegisterInfo, "GetRegisterInfo"},
  45. {15, &IUser::GetCommonInfo, "GetCommonInfo"},
  46. {16, &IUser::GetModelInfo, "GetModelInfo"},
  47. {17, &IUser::AttachActivateEvent, "AttachActivateEvent"},
  48. {18, &IUser::AttachDeactivateEvent, "AttachDeactivateEvent"},
  49. {19, &IUser::GetState, "GetState"},
  50. {20, &IUser::GetDeviceState, "GetDeviceState"},
  51. {21, &IUser::GetNpadId, "GetNpadId"},
  52. {22, &IUser::GetApplicationAreaSize, "GetApplicationAreaSize"},
  53. {23, &IUser::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
  54. {24, nullptr, "RecreateApplicationArea"},
  55. };
  56. RegisterHandlers(functions);
  57. auto& kernel = Core::System::GetInstance().Kernel();
  58. deactivate_event =
  59. Kernel::Event::Create(kernel, Kernel::ResetType::OneShot, "IUser:DeactivateEvent");
  60. availability_change_event = Kernel::Event::Create(kernel, Kernel::ResetType::OneShot,
  61. "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. INSERT_PADDING_BYTES(0x15);
  69. u32_le protocol;
  70. u32_le tag_type;
  71. INSERT_PADDING_BYTES(0x2c);
  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, sizeof(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);
  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->Signal();
  149. device_state = DeviceState::Initialized;
  150. break;
  151. case DeviceState::SearchingForTag:
  152. case DeviceState::TagRemoved:
  153. device_state = DeviceState::Initialized;
  154. break;
  155. }
  156. IPC::ResponseBuilder rb{ctx, 2};
  157. rb.Push(RESULT_SUCCESS);
  158. }
  159. void GetDeviceState(Kernel::HLERequestContext& ctx) {
  160. LOG_DEBUG(Service_NFP, "called");
  161. auto nfc_event = nfp_interface.GetNFCEvent();
  162. if (!nfc_event->ShouldWait(Kernel::GetCurrentThread()) && !has_attached_handle) {
  163. device_state = DeviceState::TagFound;
  164. nfc_event->Clear();
  165. }
  166. IPC::ResponseBuilder rb{ctx, 3};
  167. rb.Push(RESULT_SUCCESS);
  168. rb.Push<u32>(static_cast<u32>(device_state));
  169. }
  170. void StartDetection(Kernel::HLERequestContext& ctx) {
  171. LOG_DEBUG(Service_NFP, "called");
  172. if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) {
  173. device_state = DeviceState::SearchingForTag;
  174. }
  175. IPC::ResponseBuilder rb{ctx, 2};
  176. rb.Push(RESULT_SUCCESS);
  177. }
  178. void GetTagInfo(Kernel::HLERequestContext& ctx) {
  179. LOG_DEBUG(Service_NFP, "called");
  180. IPC::ResponseBuilder rb{ctx, 2};
  181. auto amiibo = nfp_interface.GetAmiiboBuffer();
  182. TagInfo tag_info{};
  183. tag_info.uuid = amiibo.uuid;
  184. tag_info.uuid_length = static_cast<u8>(tag_info.uuid.size());
  185. tag_info.protocol = 1; // TODO(ogniK): Figure out actual values
  186. tag_info.tag_type = 2;
  187. ctx.WriteBuffer(&tag_info, sizeof(TagInfo));
  188. rb.Push(RESULT_SUCCESS);
  189. }
  190. void Mount(Kernel::HLERequestContext& ctx) {
  191. LOG_DEBUG(Service_NFP, "called");
  192. device_state = DeviceState::TagNearby;
  193. IPC::ResponseBuilder rb{ctx, 2};
  194. rb.Push(RESULT_SUCCESS);
  195. }
  196. void GetModelInfo(Kernel::HLERequestContext& ctx) {
  197. LOG_DEBUG(Service_NFP, "called");
  198. IPC::ResponseBuilder rb{ctx, 2};
  199. auto amiibo = nfp_interface.GetAmiiboBuffer();
  200. ctx.WriteBuffer(&amiibo.model_info, sizeof(amiibo.model_info));
  201. rb.Push(RESULT_SUCCESS);
  202. }
  203. void Unmount(Kernel::HLERequestContext& ctx) {
  204. LOG_DEBUG(Service_NFP, "called");
  205. device_state = DeviceState::TagFound;
  206. IPC::ResponseBuilder rb{ctx, 2};
  207. rb.Push(RESULT_SUCCESS);
  208. }
  209. void Finalize(Kernel::HLERequestContext& ctx) {
  210. LOG_DEBUG(Service_NFP, "called");
  211. device_state = DeviceState::Finalized;
  212. IPC::ResponseBuilder rb{ctx, 2};
  213. rb.Push(RESULT_SUCCESS);
  214. }
  215. void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) {
  216. LOG_WARNING(Service_NFP, "(STUBBED) called");
  217. IPC::ResponseBuilder rb{ctx, 2, 1};
  218. rb.Push(RESULT_SUCCESS);
  219. rb.PushCopyObjects(availability_change_event);
  220. }
  221. void GetRegisterInfo(Kernel::HLERequestContext& ctx) {
  222. LOG_WARNING(Service_NFP, "(STUBBED) called");
  223. // TODO(ogniK): Pull Mii and owner data from amiibo
  224. IPC::ResponseBuilder rb{ctx, 2};
  225. rb.Push(RESULT_SUCCESS);
  226. }
  227. void GetCommonInfo(Kernel::HLERequestContext& ctx) {
  228. LOG_WARNING(Service_NFP, "(STUBBED) called");
  229. // TODO(ogniK): Pull common information from amiibo
  230. CommonInfo common_info{};
  231. common_info.application_area_size = 0;
  232. ctx.WriteBuffer(&common_info, sizeof(CommonInfo));
  233. IPC::ResponseBuilder rb{ctx, 2};
  234. rb.Push(RESULT_SUCCESS);
  235. }
  236. void OpenApplicationArea(Kernel::HLERequestContext& ctx) {
  237. LOG_DEBUG(Service_NFP, "called");
  238. // We don't need to worry about this since we can just open the file
  239. IPC::ResponseBuilder rb{ctx, 2};
  240. rb.Push(RESULT_SUCCESS);
  241. }
  242. void GetApplicationAreaSize(Kernel::HLERequestContext& ctx) {
  243. LOG_WARNING(Service_NFP, "(STUBBED) called");
  244. // We don't need to worry about this since we can just open the file
  245. IPC::ResponseBuilder rb{ctx, 3};
  246. rb.Push(RESULT_SUCCESS);
  247. rb.PushRaw<u32>(0); // This is from the GetCommonInfo stub
  248. }
  249. void GetApplicationArea(Kernel::HLERequestContext& ctx) {
  250. LOG_WARNING(Service_NFP, "(STUBBED) called");
  251. // TODO(ogniK): Pull application area from amiibo
  252. IPC::ResponseBuilder rb{ctx, 3};
  253. rb.Push(RESULT_SUCCESS);
  254. rb.PushRaw<u32>(0); // This is from the GetCommonInfo stub
  255. }
  256. bool has_attached_handle{};
  257. const u64 device_handle{Common::MakeMagic('Y', 'U', 'Z', 'U')};
  258. const u32 npad_id{0}; // Player 1 controller
  259. State state{State::NonInitialized};
  260. DeviceState device_state{DeviceState::Initialized};
  261. Kernel::SharedPtr<Kernel::Event> deactivate_event;
  262. Kernel::SharedPtr<Kernel::Event> availability_change_event;
  263. const Module::Interface& nfp_interface;
  264. };
  265. void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) {
  266. LOG_DEBUG(Service_NFP, "called");
  267. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  268. rb.Push(RESULT_SUCCESS);
  269. rb.PushIpcInterface<IUser>(*this);
  270. }
  271. bool Module::Interface::LoadAmiibo(const std::vector<u8>& buffer) {
  272. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  273. if (buffer.size() < sizeof(AmiiboFile)) {
  274. return false;
  275. }
  276. std::memcpy(&amiibo, buffer.data(), sizeof(amiibo));
  277. nfc_tag_load->Signal();
  278. return true;
  279. }
  280. const Kernel::SharedPtr<Kernel::Event>& Module::Interface::GetNFCEvent() const {
  281. return nfc_tag_load;
  282. }
  283. const Module::Interface::AmiiboFile& Module::Interface::GetAmiiboBuffer() const {
  284. return amiibo;
  285. }
  286. void InstallInterfaces(SM::ServiceManager& service_manager) {
  287. auto module = std::make_shared<Module>();
  288. std::make_shared<NFP_User>(module)->InstallAsService(service_manager);
  289. }
  290. } // namespace Service::NFP