From 8883dca51468a7e4b4c7bdb1a3500d6e2fef4a77 Mon Sep 17 00:00:00 2001
From: mailwl <mailwl@gmail.com>
Date: Fri, 29 Sep 2017 15:09:10 +0300
Subject: [PATCH] Service/PTM: Stub GetStepHistory function

---
 src/core/hle/service/ptm/ptm.cpp      | 38 +++++++++++++++++++++------
 src/core/hle/service/ptm/ptm.h        | 12 +++++++++
 src/core/hle/service/ptm/ptm_gets.cpp |  2 +-
 src/core/hle/service/ptm/ptm_play.cpp |  2 +-
 src/core/hle/service/ptm/ptm_sysm.cpp |  2 +-
 src/core/hle/service/ptm/ptm_u.cpp    |  2 +-
 6 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/src/core/hle/service/ptm/ptm.cpp b/src/core/hle/service/ptm/ptm.cpp
index a0b959797..931623df8 100644
--- a/src/core/hle/service/ptm/ptm.cpp
+++ b/src/core/hle/service/ptm/ptm.cpp
@@ -30,7 +30,7 @@ static bool battery_is_charging;
 
 static bool pedometer_is_counting;
 
-void GetAdapterState(Service::Interface* self) {
+void GetAdapterState(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x5, 0, 0);
 
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
@@ -40,7 +40,7 @@ void GetAdapterState(Service::Interface* self) {
     LOG_WARNING(Service_PTM, "(STUBBED) called");
 }
 
-void GetShellState(Service::Interface* self) {
+void GetShellState(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x6, 0, 0);
 
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
@@ -48,7 +48,7 @@ void GetShellState(Service::Interface* self) {
     rb.Push(shell_open);
 }
 
-void GetBatteryLevel(Service::Interface* self) {
+void GetBatteryLevel(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x7, 0, 0);
 
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
@@ -58,7 +58,7 @@ void GetBatteryLevel(Service::Interface* self) {
     LOG_WARNING(Service_PTM, "(STUBBED) called");
 }
 
-void GetBatteryChargeState(Service::Interface* self) {
+void GetBatteryChargeState(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x8, 0, 0);
 
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
@@ -68,7 +68,7 @@ void GetBatteryChargeState(Service::Interface* self) {
     LOG_WARNING(Service_PTM, "(STUBBED) called");
 }
 
-void GetPedometerState(Service::Interface* self) {
+void GetPedometerState(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x9, 0, 0);
 
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
@@ -78,7 +78,29 @@ void GetPedometerState(Service::Interface* self) {
     LOG_WARNING(Service_PTM, "(STUBBED) called");
 }
 
-void GetTotalStepCount(Service::Interface* self) {
+void GetStepHistory(Interface* self) {
+    IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0xB, 3, 2);
+
+    u32 hours = rp.Pop<u32>();
+    u64 start_time = rp.Pop<u64>();
+    size_t steps_buff_size;
+    const VAddr steps_buff_addr = rp.PopMappedBuffer(&steps_buff_size);
+    ASSERT_MSG(sizeof(u16) * hours == steps_buff_size, "Buffer for steps count has incorrect size");
+
+    // Stub: set zero steps count for every hour
+    for (u32 i = 0; i < hours; ++i) {
+        const u16 steps_per_hour = 0;
+        Memory::Write16(steps_buff_addr + i * sizeof(u16), steps_per_hour);
+    }
+
+    IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
+    rb.Push(RESULT_SUCCESS);
+
+    LOG_WARNING(Service_PTM, "(STUBBED) called, from time(raw): 0x%llx, for %d hours", start_time,
+                hours);
+}
+
+void GetTotalStepCount(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0xC, 0, 0);
 
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
@@ -88,7 +110,7 @@ void GetTotalStepCount(Service::Interface* self) {
     LOG_WARNING(Service_PTM, "(STUBBED) called");
 }
 
-void GetSoftwareClosedFlag(Service::Interface* self) {
+void GetSoftwareClosedFlag(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x80F, 0, 0);
 
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
@@ -112,7 +134,7 @@ void CheckNew3DS(IPC::RequestBuilder& rb) {
     LOG_WARNING(Service_PTM, "(STUBBED) called isNew3DS = 0x%08x", static_cast<u32>(is_new_3ds));
 }
 
-void CheckNew3DS(Service::Interface* self) {
+void CheckNew3DS(Interface* self) {
     IPC::RequestBuilder rb(Kernel::GetCommandBuffer(), 0x40A, 0, 0); // 0x040A0000
     CheckNew3DS(rb);
 }
diff --git a/src/core/hle/service/ptm/ptm.h b/src/core/hle/service/ptm/ptm.h
index e17e59835..7a5cd3bdd 100644
--- a/src/core/hle/service/ptm/ptm.h
+++ b/src/core/hle/service/ptm/ptm.h
@@ -82,6 +82,18 @@ void GetBatteryChargeState(Interface* self);
  */
 void GetPedometerState(Interface* self);
 
+/**
+ * PTM::GetStepHistory service function
+ *  Inputs:
+ *      1 : Number of hours
+ *    2-3 : Start time
+ *      4 : Buffer mapping descriptor
+ *      5 : (short*) Buffer for step counts
+ *  Outputs:
+ *      1 : Result of function, 0 on success, otherwise error code
+ */
+void GetStepHistory(Interface* self);
+
 /**
  * PTM::GetTotalStepCount service function
  *  Outputs:
diff --git a/src/core/hle/service/ptm/ptm_gets.cpp b/src/core/hle/service/ptm/ptm_gets.cpp
index b23e508d6..eeaeeba33 100644
--- a/src/core/hle/service/ptm/ptm_gets.cpp
+++ b/src/core/hle/service/ptm/ptm_gets.cpp
@@ -20,7 +20,7 @@ const Interface::FunctionInfo FunctionTable[] = {
     {0x00080000, GetBatteryChargeState, "GetBatteryChargeState"},
     {0x00090000, nullptr, "GetPedometerState"},
     {0x000A0042, nullptr, "GetStepHistoryEntry"},
-    {0x000B00C2, nullptr, "GetStepHistory"},
+    {0x000B00C2, GetStepHistory, "GetStepHistory"},
     {0x000C0000, GetTotalStepCount, "GetTotalStepCount"},
     {0x000D0040, nullptr, "SetPedometerRecordingMode"},
     {0x000E0000, nullptr, "GetPedometerRecordingMode"},
diff --git a/src/core/hle/service/ptm/ptm_play.cpp b/src/core/hle/service/ptm/ptm_play.cpp
index bcb00e0d4..a04ef1899 100644
--- a/src/core/hle/service/ptm/ptm_play.cpp
+++ b/src/core/hle/service/ptm/ptm_play.cpp
@@ -20,7 +20,7 @@ const Interface::FunctionInfo FunctionTable[] = {
     {0x00080000, GetBatteryChargeState, "GetBatteryChargeState"},
     {0x00090000, nullptr, "GetPedometerState"},
     {0x000A0042, nullptr, "GetStepHistoryEntry"},
-    {0x000B00C2, nullptr, "GetStepHistory"},
+    {0x000B00C2, GetStepHistory, "GetStepHistory"},
     {0x000C0000, GetTotalStepCount, "GetTotalStepCount"},
     {0x000D0040, nullptr, "SetPedometerRecordingMode"},
     {0x000E0000, nullptr, "GetPedometerRecordingMode"},
diff --git a/src/core/hle/service/ptm/ptm_sysm.cpp b/src/core/hle/service/ptm/ptm_sysm.cpp
index f95dfdbb1..ee73aff1a 100644
--- a/src/core/hle/service/ptm/ptm_sysm.cpp
+++ b/src/core/hle/service/ptm/ptm_sysm.cpp
@@ -20,7 +20,7 @@ const Interface::FunctionInfo FunctionTable[] = {
     {0x00080000, GetBatteryChargeState, "GetBatteryChargeState"},
     {0x00090000, nullptr, "GetPedometerState"},
     {0x000A0042, nullptr, "GetStepHistoryEntry"},
-    {0x000B00C2, nullptr, "GetStepHistory"},
+    {0x000B00C2, GetStepHistory, "GetStepHistory"},
     {0x000C0000, GetTotalStepCount, "GetTotalStepCount"},
     {0x000D0040, nullptr, "SetPedometerRecordingMode"},
     {0x000E0000, nullptr, "GetPedometerRecordingMode"},
diff --git a/src/core/hle/service/ptm/ptm_u.cpp b/src/core/hle/service/ptm/ptm_u.cpp
index 696a58a36..b815c1bae 100644
--- a/src/core/hle/service/ptm/ptm_u.cpp
+++ b/src/core/hle/service/ptm/ptm_u.cpp
@@ -19,7 +19,7 @@ const Interface::FunctionInfo FunctionTable[] = {
     {0x00080000, GetBatteryChargeState, "GetBatteryChargeState"},
     {0x00090000, GetPedometerState, "GetPedometerState"},
     {0x000A0042, nullptr, "GetStepHistoryEntry"},
-    {0x000B00C2, nullptr, "GetStepHistory"},
+    {0x000B00C2, GetStepHistory, "GetStepHistory"},
     {0x000C0000, GetTotalStepCount, "GetTotalStepCount"},
     {0x000D0040, nullptr, "SetPedometerRecordingMode"},
     {0x000E0000, nullptr, "GetPedometerRecordingMode"},