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 <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::CreateEventPair(kernel, Kernel::ResetType::OneShot,
  24. "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::CreateEventPair(
  61. kernel, Kernel::ResetType::OneShot, "IUser:DeactivateEvent");
  62. availability_change_event = Kernel::WritableEvent::CreateEventPair(
  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. rb.PushCopyObjects(deactivate_event.readable);
  144. }
  145. void StopDetection(Kernel::HLERequestContext& ctx) {
  146. LOG_DEBUG(Service_NFP, "called");
  147. switch (device_state) {
  148. case DeviceState::TagFound:
  149. case DeviceState::TagNearby:
  150. deactivate_event.writable->Signal();
  151. device_state = DeviceState::Initialized;
  152. break;
  153. case DeviceState::SearchingForTag:
  154. case DeviceState::TagRemoved:
  155. device_state = DeviceState::Initialized;
  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(Kernel::GetCurrentThread()) && !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. auto amiibo = nfp_interface.GetAmiiboBuffer();
  184. TagInfo tag_info{};
  185. tag_info.uuid = amiibo.uuid;
  186. tag_info.uuid_length = static_cast<u8>(tag_info.uuid.size());
  187. tag_info.protocol = 1; // TODO(ogniK): Figure out actual values
  188. tag_info.tag_type = 2;
  189. ctx.WriteBuffer(&tag_info, sizeof(TagInfo));
  190. rb.Push(RESULT_SUCCESS);
  191. }
  192. void Mount(Kernel::HLERequestContext& ctx) {
  193. LOG_DEBUG(Service_NFP, "called");
  194. device_state = DeviceState::TagNearby;
  195. IPC::ResponseBuilder rb{ctx, 2};
  196. rb.Push(RESULT_SUCCESS);
  197. }
  198. void GetModelInfo(Kernel::HLERequestContext& ctx) {
  199. LOG_DEBUG(Service_NFP, "called");
  200. IPC::ResponseBuilder rb{ctx, 2};
  201. auto amiibo = nfp_interface.GetAmiiboBuffer();
  202. ctx.WriteBuffer(&amiibo.model_info, sizeof(amiibo.model_info));
  203. rb.Push(RESULT_SUCCESS);
  204. }
  205. void Unmount(Kernel::HLERequestContext& ctx) {
  206. LOG_DEBUG(Service_NFP, "called");
  207. device_state = DeviceState::TagFound;
  208. IPC::ResponseBuilder rb{ctx, 2};
  209. rb.Push(RESULT_SUCCESS);
  210. }
  211. void Finalize(Kernel::HLERequestContext& ctx) {
  212. LOG_DEBUG(Service_NFP, "called");
  213. device_state = DeviceState::Finalized;
  214. IPC::ResponseBuilder rb{ctx, 2};
  215. rb.Push(RESULT_SUCCESS);
  216. }
  217. void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) {
  218. LOG_WARNING(Service_NFP, "(STUBBED) called");
  219. IPC::ResponseBuilder rb{ctx, 2, 1};
  220. rb.Push(RESULT_SUCCESS);
  221. rb.PushCopyObjects(availability_change_event.readable);
  222. }
  223. void GetRegisterInfo(Kernel::HLERequestContext& ctx) {
  224. LOG_WARNING(Service_NFP, "(STUBBED) called");
  225. // TODO(ogniK): Pull Mii and owner data from amiibo
  226. IPC::ResponseBuilder rb{ctx, 2};
  227. rb.Push(RESULT_SUCCESS);
  228. }
  229. void GetCommonInfo(Kernel::HLERequestContext& ctx) {
  230. LOG_WARNING(Service_NFP, "(STUBBED) called");
  231. // TODO(ogniK): Pull common information from amiibo
  232. CommonInfo common_info{};
  233. common_info.application_area_size = 0;
  234. ctx.WriteBuffer(&common_info, sizeof(CommonInfo));
  235. IPC::ResponseBuilder rb{ctx, 2};
  236. rb.Push(RESULT_SUCCESS);
  237. }
  238. void OpenApplicationArea(Kernel::HLERequestContext& ctx) {
  239. LOG_DEBUG(Service_NFP, "called");
  240. // We don't need to worry about this since we can just open the file
  241. IPC::ResponseBuilder rb{ctx, 2};
  242. rb.Push(RESULT_SUCCESS);
  243. }
  244. void GetApplicationAreaSize(Kernel::HLERequestContext& ctx) {
  245. LOG_WARNING(Service_NFP, "(STUBBED) called");
  246. // We don't need to worry about this since we can just open the file
  247. IPC::ResponseBuilder rb{ctx, 3};
  248. rb.Push(RESULT_SUCCESS);
  249. rb.PushRaw<u32>(0); // This is from the GetCommonInfo stub
  250. }
  251. void GetApplicationArea(Kernel::HLERequestContext& ctx) {
  252. LOG_WARNING(Service_NFP, "(STUBBED) called");
  253. // TODO(ogniK): Pull application area from amiibo
  254. IPC::ResponseBuilder rb{ctx, 3};
  255. rb.Push(RESULT_SUCCESS);
  256. rb.PushRaw<u32>(0); // This is from the GetCommonInfo stub
  257. }
  258. bool has_attached_handle{};
  259. const u64 device_handle{0}; // Npad device 1
  260. const u32 npad_id{0}; // Player 1 controller
  261. State state{State::NonInitialized};
  262. DeviceState device_state{DeviceState::Initialized};
  263. Kernel::EventPair deactivate_event;
  264. Kernel::EventPair availability_change_event;
  265. const Module::Interface& nfp_interface;
  266. };
  267. void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) {
  268. LOG_DEBUG(Service_NFP, "called");
  269. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  270. rb.Push(RESULT_SUCCESS);
  271. rb.PushIpcInterface<IUser>(*this);
  272. }
  273. bool Module::Interface::LoadAmiibo(const std::vector<u8>& buffer) {
  274. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  275. if (buffer.size() < sizeof(AmiiboFile)) {
  276. return false;
  277. }
  278. std::memcpy(&amiibo, buffer.data(), sizeof(amiibo));
  279. nfc_tag_load.writable->Signal();
  280. return true;
  281. }
  282. const Kernel::SharedPtr<Kernel::ReadableEvent>& Module::Interface::GetNFCEvent() const {
  283. return nfc_tag_load.readable;
  284. }
  285. const Module::Interface::AmiiboFile& Module::Interface::GetAmiiboBuffer() const {
  286. return amiibo;
  287. }
  288. void InstallInterfaces(SM::ServiceManager& service_manager) {
  289. auto module = std::make_shared<Module>();
  290. std::make_shared<NFP_User>(module)->InstallAsService(service_manager);
  291. }
  292. } // namespace Service::NFP