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/thread.h"
  11. #include "core/hle/kernel/writable_event.h"
  12. #include "core/hle/lock.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. constexpr ResultCode ERR_NO_APPLICATION_AREA(ErrorModule::NFP, 152);
  20. } // namespace ErrCodes
  21. Module::Interface::Interface(std::shared_ptr<Module> module, const char* name)
  22. : ServiceFramework(name), module(std::move(module)) {
  23. auto& kernel = Core::System::GetInstance().Kernel();
  24. nfc_tag_load = Kernel::WritableEvent::CreateEventPair(kernel, Kernel::ResetType::OneShot,
  25. "IUser:NFCTagDetected");
  26. }
  27. Module::Interface::~Interface() = default;
  28. class IUser final : public ServiceFramework<IUser> {
  29. public:
  30. IUser(Module::Interface& nfp_interface)
  31. : ServiceFramework("NFP::IUser"), nfp_interface(nfp_interface) {
  32. static const FunctionInfo functions[] = {
  33. {0, &IUser::Initialize, "Initialize"},
  34. {1, &IUser::Finalize, "Finalize"},
  35. {2, &IUser::ListDevices, "ListDevices"},
  36. {3, &IUser::StartDetection, "StartDetection"},
  37. {4, &IUser::StopDetection, "StopDetection"},
  38. {5, &IUser::Mount, "Mount"},
  39. {6, &IUser::Unmount, "Unmount"},
  40. {7, &IUser::OpenApplicationArea, "OpenApplicationArea"},
  41. {8, &IUser::GetApplicationArea, "GetApplicationArea"},
  42. {9, nullptr, "SetApplicationArea"},
  43. {10, nullptr, "Flush"},
  44. {11, nullptr, "Restore"},
  45. {12, nullptr, "CreateApplicationArea"},
  46. {13, &IUser::GetTagInfo, "GetTagInfo"},
  47. {14, &IUser::GetRegisterInfo, "GetRegisterInfo"},
  48. {15, &IUser::GetCommonInfo, "GetCommonInfo"},
  49. {16, &IUser::GetModelInfo, "GetModelInfo"},
  50. {17, &IUser::AttachActivateEvent, "AttachActivateEvent"},
  51. {18, &IUser::AttachDeactivateEvent, "AttachDeactivateEvent"},
  52. {19, &IUser::GetState, "GetState"},
  53. {20, &IUser::GetDeviceState, "GetDeviceState"},
  54. {21, &IUser::GetNpadId, "GetNpadId"},
  55. {22, &IUser::GetApplicationAreaSize, "GetApplicationAreaSize"},
  56. {23, &IUser::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
  57. {24, nullptr, "RecreateApplicationArea"},
  58. };
  59. RegisterHandlers(functions);
  60. auto& kernel = Core::System::GetInstance().Kernel();
  61. deactivate_event = Kernel::WritableEvent::CreateEventPair(
  62. kernel, Kernel::ResetType::OneShot, "IUser:DeactivateEvent");
  63. availability_change_event = Kernel::WritableEvent::CreateEventPair(
  64. kernel, Kernel::ResetType::OneShot, "IUser:AvailabilityChangeEvent");
  65. }
  66. private:
  67. struct TagInfo {
  68. std::array<u8, 10> uuid;
  69. u8 uuid_length; // TODO(ogniK): Figure out if this is actual the uuid length or does it
  70. // mean something else
  71. INSERT_PADDING_BYTES(0x15);
  72. u32_le protocol;
  73. u32_le tag_type;
  74. INSERT_PADDING_BYTES(0x2c);
  75. };
  76. static_assert(sizeof(TagInfo) == 0x54, "TagInfo is an invalid size");
  77. enum class State : u32 {
  78. NonInitialized = 0,
  79. Initialized = 1,
  80. };
  81. enum class DeviceState : u32 {
  82. Initialized = 0,
  83. SearchingForTag = 1,
  84. TagFound = 2,
  85. TagRemoved = 3,
  86. TagNearby = 4,
  87. Unknown5 = 5,
  88. Finalized = 6
  89. };
  90. struct CommonInfo {
  91. u16_be last_write_year;
  92. u8 last_write_month;
  93. u8 last_write_day;
  94. u16_be write_counter;
  95. u16_be version;
  96. u32_be application_area_size;
  97. INSERT_PADDING_BYTES(0x34);
  98. };
  99. static_assert(sizeof(CommonInfo) == 0x40, "CommonInfo is an invalid size");
  100. void Initialize(Kernel::HLERequestContext& ctx) {
  101. LOG_DEBUG(Service_NFC, "called");
  102. IPC::ResponseBuilder rb{ctx, 2, 0};
  103. rb.Push(RESULT_SUCCESS);
  104. state = State::Initialized;
  105. }
  106. void GetState(Kernel::HLERequestContext& ctx) {
  107. LOG_DEBUG(Service_NFC, "called");
  108. IPC::ResponseBuilder rb{ctx, 3, 0};
  109. rb.Push(RESULT_SUCCESS);
  110. rb.PushRaw<u32>(static_cast<u32>(state));
  111. }
  112. void ListDevices(Kernel::HLERequestContext& ctx) {
  113. IPC::RequestParser rp{ctx};
  114. const u32 array_size = rp.Pop<u32>();
  115. LOG_DEBUG(Service_NFP, "called, array_size={}", array_size);
  116. ctx.WriteBuffer(&device_handle, sizeof(device_handle));
  117. IPC::ResponseBuilder rb{ctx, 3};
  118. rb.Push(RESULT_SUCCESS);
  119. rb.Push<u32>(1);
  120. }
  121. void GetNpadId(Kernel::HLERequestContext& ctx) {
  122. IPC::RequestParser rp{ctx};
  123. const u64 dev_handle = rp.Pop<u64>();
  124. LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle);
  125. IPC::ResponseBuilder rb{ctx, 3};
  126. rb.Push(RESULT_SUCCESS);
  127. rb.Push<u32>(npad_id);
  128. }
  129. void AttachActivateEvent(Kernel::HLERequestContext& ctx) {
  130. IPC::RequestParser rp{ctx};
  131. const u64 dev_handle = rp.Pop<u64>();
  132. LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle);
  133. IPC::ResponseBuilder rb{ctx, 2, 1};
  134. rb.Push(RESULT_SUCCESS);
  135. rb.PushCopyObjects(nfp_interface.GetNFCEvent());
  136. has_attached_handle = true;
  137. }
  138. void AttachDeactivateEvent(Kernel::HLERequestContext& ctx) {
  139. IPC::RequestParser rp{ctx};
  140. const u64 dev_handle = rp.Pop<u64>();
  141. LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle);
  142. IPC::ResponseBuilder rb{ctx, 2, 1};
  143. rb.Push(RESULT_SUCCESS);
  144. rb.PushCopyObjects(deactivate_event.readable);
  145. }
  146. void StopDetection(Kernel::HLERequestContext& ctx) {
  147. LOG_DEBUG(Service_NFP, "called");
  148. switch (device_state) {
  149. case DeviceState::TagFound:
  150. case DeviceState::TagNearby:
  151. deactivate_event.writable->Signal();
  152. device_state = DeviceState::Initialized;
  153. break;
  154. case DeviceState::SearchingForTag:
  155. case DeviceState::TagRemoved:
  156. device_state = DeviceState::Initialized;
  157. break;
  158. }
  159. IPC::ResponseBuilder rb{ctx, 2};
  160. rb.Push(RESULT_SUCCESS);
  161. }
  162. void GetDeviceState(Kernel::HLERequestContext& ctx) {
  163. LOG_DEBUG(Service_NFP, "called");
  164. auto nfc_event = nfp_interface.GetNFCEvent();
  165. if (!nfc_event->ShouldWait(Kernel::GetCurrentThread()) && !has_attached_handle) {
  166. device_state = DeviceState::TagFound;
  167. nfc_event->Clear();
  168. }
  169. IPC::ResponseBuilder rb{ctx, 3};
  170. rb.Push(RESULT_SUCCESS);
  171. rb.Push<u32>(static_cast<u32>(device_state));
  172. }
  173. void StartDetection(Kernel::HLERequestContext& ctx) {
  174. LOG_DEBUG(Service_NFP, "called");
  175. if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) {
  176. device_state = DeviceState::SearchingForTag;
  177. }
  178. IPC::ResponseBuilder rb{ctx, 2};
  179. rb.Push(RESULT_SUCCESS);
  180. }
  181. void GetTagInfo(Kernel::HLERequestContext& ctx) {
  182. LOG_DEBUG(Service_NFP, "called");
  183. IPC::ResponseBuilder rb{ctx, 2};
  184. auto amiibo = nfp_interface.GetAmiiboBuffer();
  185. TagInfo tag_info{};
  186. tag_info.uuid = amiibo.uuid;
  187. tag_info.uuid_length = static_cast<u8>(tag_info.uuid.size());
  188. tag_info.protocol = 1; // TODO(ogniK): Figure out actual values
  189. tag_info.tag_type = 2;
  190. ctx.WriteBuffer(&tag_info, sizeof(TagInfo));
  191. rb.Push(RESULT_SUCCESS);
  192. }
  193. void Mount(Kernel::HLERequestContext& ctx) {
  194. LOG_DEBUG(Service_NFP, "called");
  195. device_state = DeviceState::TagNearby;
  196. IPC::ResponseBuilder rb{ctx, 2};
  197. rb.Push(RESULT_SUCCESS);
  198. }
  199. void GetModelInfo(Kernel::HLERequestContext& ctx) {
  200. LOG_DEBUG(Service_NFP, "called");
  201. IPC::ResponseBuilder rb{ctx, 2};
  202. auto amiibo = nfp_interface.GetAmiiboBuffer();
  203. ctx.WriteBuffer(&amiibo.model_info, sizeof(amiibo.model_info));
  204. rb.Push(RESULT_SUCCESS);
  205. }
  206. void Unmount(Kernel::HLERequestContext& ctx) {
  207. LOG_DEBUG(Service_NFP, "called");
  208. device_state = DeviceState::TagFound;
  209. IPC::ResponseBuilder rb{ctx, 2};
  210. rb.Push(RESULT_SUCCESS);
  211. }
  212. void Finalize(Kernel::HLERequestContext& ctx) {
  213. LOG_DEBUG(Service_NFP, "called");
  214. device_state = DeviceState::Finalized;
  215. IPC::ResponseBuilder rb{ctx, 2};
  216. rb.Push(RESULT_SUCCESS);
  217. }
  218. void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) {
  219. LOG_WARNING(Service_NFP, "(STUBBED) called");
  220. IPC::ResponseBuilder rb{ctx, 2, 1};
  221. rb.Push(RESULT_SUCCESS);
  222. rb.PushCopyObjects(availability_change_event.readable);
  223. }
  224. void GetRegisterInfo(Kernel::HLERequestContext& ctx) {
  225. LOG_WARNING(Service_NFP, "(STUBBED) called");
  226. // TODO(ogniK): Pull Mii and owner data from amiibo
  227. IPC::ResponseBuilder rb{ctx, 2};
  228. rb.Push(RESULT_SUCCESS);
  229. }
  230. void GetCommonInfo(Kernel::HLERequestContext& ctx) {
  231. LOG_WARNING(Service_NFP, "(STUBBED) called");
  232. // TODO(ogniK): Pull common information from amiibo
  233. CommonInfo common_info{};
  234. common_info.application_area_size = 0;
  235. ctx.WriteBuffer(&common_info, sizeof(CommonInfo));
  236. IPC::ResponseBuilder rb{ctx, 2};
  237. rb.Push(RESULT_SUCCESS);
  238. }
  239. void OpenApplicationArea(Kernel::HLERequestContext& ctx) {
  240. LOG_WARNING(Service_NFP, "(STUBBED) called");
  241. IPC::ResponseBuilder rb{ctx, 2};
  242. rb.Push(ErrCodes::ERR_NO_APPLICATION_AREA);
  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