From 7f0bee2ff8d6eca1f10a29d5c9e5e7663cb34143 Mon Sep 17 00:00:00 2001
From: Ac_K <Acoustik666@gmail.com>
Date: Wed, 28 Feb 2018 04:31:52 +0100
Subject: [PATCH] Stubs implementations (#45)

Services Bsd, Nifm & SSL stubs implementations
Objects IGeneralService, IRequest stubs implementations.
Fake-Fix GetAvailableLanguageCodes stub too ^^!
---
 Ryujinx.Core/OsHle/Services/Bsd/ServiceBsd.cs | 60 +++++++++++++++++++
 .../OsHle/Services/Nifm/IGeneralService.cs    | 34 +++++++++++
 Ryujinx.Core/OsHle/Services/Nifm/IRequest.cs  | 49 +++++++++++++++
 .../OsHle/Services/Nifm/ServiceNifm.cs        | 29 +++++++++
 Ryujinx.Core/OsHle/Services/ServiceFactory.cs |  6 ++
 Ryujinx.Core/OsHle/Services/Set/ServiceSet.cs | 11 ++--
 Ryujinx.Core/OsHle/Services/Ssl/ServiceSsl.cs | 20 +++++++
 7 files changed, 203 insertions(+), 6 deletions(-)
 create mode 100644 Ryujinx.Core/OsHle/Services/Bsd/ServiceBsd.cs
 create mode 100644 Ryujinx.Core/OsHle/Services/Nifm/IGeneralService.cs
 create mode 100644 Ryujinx.Core/OsHle/Services/Nifm/IRequest.cs
 create mode 100644 Ryujinx.Core/OsHle/Services/Nifm/ServiceNifm.cs
 create mode 100644 Ryujinx.Core/OsHle/Services/Ssl/ServiceSsl.cs

diff --git a/Ryujinx.Core/OsHle/Services/Bsd/ServiceBsd.cs b/Ryujinx.Core/OsHle/Services/Bsd/ServiceBsd.cs
new file mode 100644
index 0000000000..fa47d94456
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Services/Bsd/ServiceBsd.cs
@@ -0,0 +1,60 @@
+using Ryujinx.Core.OsHle.Ipc;
+using System.Collections.Generic;
+
+namespace Ryujinx.Core.OsHle.IpcServices.Bsd
+{
+    class ServiceBsd : IIpcService
+    {
+        private Dictionary<int, ServiceProcessRequest> m_Commands;
+
+        public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
+
+        public ServiceBsd()
+        {
+            m_Commands = new Dictionary<int, ServiceProcessRequest>()
+            {
+                { 0, Initialize      },
+                { 1, StartMonitoring }
+            };
+        }
+
+        //Initialize(u32, u32, u32, u32, u32, u32, u32, u32, u64 pid, u64 transferMemorySize, pid, KObject) -> u32 bsd_errno
+        public long Initialize(ServiceCtx Context)
+        {
+            /*
+            typedef struct  {
+                u32 version;                // Observed 1 on 2.0 LibAppletWeb, 2 on 3.0.
+
+                u32 tcp_tx_buf_size;        // Size of the TCP transfer (send) buffer (initial or fixed).
+                u32 tcp_rx_buf_size;        // Size of the TCP recieve buffer (initial or fixed).
+                u32 tcp_tx_buf_max_size;    // Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its initial value.
+                u32 tcp_rx_buf_max_size;    // Maximum size of the TCP receive buffer. If it is 0, the size of the buffer is fixed to its initial value.
+
+                u32 udp_tx_buf_size;        // Size of the UDP transfer (send) buffer (typically 0x2400 bytes).
+                u32 udp_rx_buf_size;        // Size of the UDP receive buffer (typically 0xA500 bytes).
+
+                u32 sb_efficiency;          // Number of buffers for each socket (standard values range from 1 to 8).
+            } BsdBufferConfig;
+            */
+
+            long Pid = Context.RequestData.ReadInt64();
+            long TransferMemorySize = Context.RequestData.ReadInt64();
+
+            // Two other args are unknown!
+
+            Context.ResponseData.Write(0);
+
+            //Todo: Stub
+
+            return 0;
+        }
+
+        //StartMonitoring(u64, pid)
+        public long StartMonitoring(ServiceCtx Context)
+        {
+            //Todo: Stub
+
+            return 0;
+        }
+    }
+}
\ No newline at end of file
diff --git a/Ryujinx.Core/OsHle/Services/Nifm/IGeneralService.cs b/Ryujinx.Core/OsHle/Services/Nifm/IGeneralService.cs
new file mode 100644
index 0000000000..c31ee36b20
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Services/Nifm/IGeneralService.cs
@@ -0,0 +1,34 @@
+using Ryujinx.Core.OsHle.Ipc;
+using System.Collections.Generic;
+
+using static Ryujinx.Core.OsHle.IpcServices.ObjHelper;
+
+namespace Ryujinx.Core.OsHle.IpcServices.Nifm
+{
+    class IGeneralService : IIpcService
+    {
+        private Dictionary<int, ServiceProcessRequest> m_Commands;
+
+        public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
+
+        public IGeneralService()
+        {
+            m_Commands = new Dictionary<int, ServiceProcessRequest>()
+            {
+                { 4, CreateRequest }
+            };
+        }
+
+        //CreateRequest(i32)
+        public long CreateRequest(ServiceCtx Context)
+        {
+            int Unknown = Context.RequestData.ReadInt32();
+
+            MakeObject(Context, new IRequest());
+
+            //Todo: Stub
+
+            return 0;
+        }
+    }
+}
\ No newline at end of file
diff --git a/Ryujinx.Core/OsHle/Services/Nifm/IRequest.cs b/Ryujinx.Core/OsHle/Services/Nifm/IRequest.cs
new file mode 100644
index 0000000000..6110e5fbe2
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Services/Nifm/IRequest.cs
@@ -0,0 +1,49 @@
+using Ryujinx.Core.OsHle.Ipc;
+using System.Collections.Generic;
+
+namespace Ryujinx.Core.OsHle.IpcServices.Nifm
+{
+    class IRequest : IIpcService
+    {
+        private Dictionary<int, ServiceProcessRequest> m_Commands;
+
+        public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
+
+        public IRequest()
+        {
+            m_Commands = new Dictionary<int, ServiceProcessRequest>()
+            {
+                { 0, GetRequestState               },
+                { 1, GetResult                     },
+                { 2, GetSystemEventReadableHandles }
+            };
+        }
+
+        // -> i32
+        public long GetRequestState(ServiceCtx Context)
+        {
+            Context.ResponseData.Write(0);
+
+            //Todo: Stub
+
+            return 0;
+        }
+
+        public long GetResult(ServiceCtx Context)
+        {
+            //Todo: Stub
+
+            return 0;
+        }
+
+        //GetSystemEventReadableHandles() -> (KObject, KObject)
+        public long GetSystemEventReadableHandles(ServiceCtx Context)
+        {
+            Context.Response.HandleDesc = IpcHandleDesc.MakeMove(0xbadcafe);
+
+            //Todo: Stub
+
+            return 0;
+        }
+    }
+}
\ No newline at end of file
diff --git a/Ryujinx.Core/OsHle/Services/Nifm/ServiceNifm.cs b/Ryujinx.Core/OsHle/Services/Nifm/ServiceNifm.cs
new file mode 100644
index 0000000000..7e183389b2
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Services/Nifm/ServiceNifm.cs
@@ -0,0 +1,29 @@
+using Ryujinx.Core.OsHle.Ipc;
+using System.Collections.Generic;
+
+using static Ryujinx.Core.OsHle.IpcServices.ObjHelper;
+
+namespace Ryujinx.Core.OsHle.IpcServices.Nifm
+{
+    class ServiceNifm : IIpcService
+    {
+        private Dictionary<int, ServiceProcessRequest> m_Commands;
+
+        public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
+
+        public ServiceNifm()
+        {
+            m_Commands = new Dictionary<int, ServiceProcessRequest>()
+            {
+                { 4, CreateGeneralServiceOld }
+            };
+        }
+
+        public long CreateGeneralServiceOld(ServiceCtx Context)
+        {
+            MakeObject(Context, new IGeneralService());
+
+            return 0;
+        }
+    }
+}
\ No newline at end of file
diff --git a/Ryujinx.Core/OsHle/Services/ServiceFactory.cs b/Ryujinx.Core/OsHle/Services/ServiceFactory.cs
index 54fa38ab78..aae624534e 100644
--- a/Ryujinx.Core/OsHle/Services/ServiceFactory.cs
+++ b/Ryujinx.Core/OsHle/Services/ServiceFactory.cs
@@ -2,16 +2,19 @@ using Ryujinx.Core.OsHle.IpcServices.Acc;
 using Ryujinx.Core.OsHle.IpcServices.Am;
 using Ryujinx.Core.OsHle.IpcServices.Apm;
 using Ryujinx.Core.OsHle.IpcServices.Aud;
+using Ryujinx.Core.OsHle.IpcServices.Bsd;
 using Ryujinx.Core.OsHle.IpcServices.Friend;
 using Ryujinx.Core.OsHle.IpcServices.FspSrv;
 using Ryujinx.Core.OsHle.IpcServices.Hid;
 using Ryujinx.Core.OsHle.IpcServices.Lm;
+using Ryujinx.Core.OsHle.IpcServices.Nifm;
 using Ryujinx.Core.OsHle.IpcServices.Ns;
 using Ryujinx.Core.OsHle.IpcServices.NvServices;
 using Ryujinx.Core.OsHle.IpcServices.Pctl;
 using Ryujinx.Core.OsHle.IpcServices.Pl;
 using Ryujinx.Core.OsHle.IpcServices.Set;
 using Ryujinx.Core.OsHle.IpcServices.Sm;
+using Ryujinx.Core.OsHle.IpcServices.Ssl;
 using Ryujinx.Core.OsHle.IpcServices.Time;
 using Ryujinx.Core.OsHle.IpcServices.Vi;
 using System;
@@ -31,16 +34,19 @@ namespace Ryujinx.Core.OsHle.IpcServices
                 case "appletOE": return new ServiceAppletOE();
                 case "audout:u": return new ServiceAudOut();
                 case "audren:u": return new ServiceAudRen();
+                case "bsd:u":    return new ServiceBsd();
                 case "friend:a": return new ServiceFriend();
                 case "fsp-srv":  return new ServiceFspSrv();
                 case "hid":      return new ServiceHid();
                 case "lm":       return new ServiceLm();
+                case "nifm:u":   return new ServiceNifm();
                 case "nvdrv":    return new ServiceNvDrv();
                 case "nvdrv:a":  return new ServiceNvDrv();
                 case "pctl:a":   return new ServicePctl();
                 case "pl:u":     return new ServicePl();
                 case "set":      return new ServiceSet();
                 case "sm:":      return new ServiceSm();
+                case "ssl":      return new ServiceSsl();
                 case "time:s":   return new ServiceTime();
                 case "time:u":   return new ServiceTime();
                 case "vi:m":     return new ServiceVi();
diff --git a/Ryujinx.Core/OsHle/Services/Set/ServiceSet.cs b/Ryujinx.Core/OsHle/Services/Set/ServiceSet.cs
index 05e409b0d1..c60e1712aa 100644
--- a/Ryujinx.Core/OsHle/Services/Set/ServiceSet.cs
+++ b/Ryujinx.Core/OsHle/Services/Set/ServiceSet.cs
@@ -1,5 +1,6 @@
 using ChocolArm64.Memory;
 using Ryujinx.Core.OsHle.Ipc;
+using System;
 using System.Collections.Generic;
 
 namespace Ryujinx.Core.OsHle.IpcServices.Set
@@ -30,12 +31,10 @@ namespace Ryujinx.Core.OsHle.IpcServices.Set
                 short Size     = Context.Request.RecvListBuff[0].Size;
 
                 //This should return an array of ints with values matching the LanguageCode enum.
-                byte[] Data = new byte[Size];
-
-                Data[0] = 0;
-                Data[1] = 1;
-
-                AMemoryHelper.WriteBytes(Context.Memory, Position, Data);
+                foreach (long value in new long[] { 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L })
+                {
+                    AMemoryHelper.WriteBytes(Context.Memory, Position += 8, BitConverter.GetBytes(value));
+                }
             }
 
             Context.ResponseData.Write(LangCodesCount);
diff --git a/Ryujinx.Core/OsHle/Services/Ssl/ServiceSsl.cs b/Ryujinx.Core/OsHle/Services/Ssl/ServiceSsl.cs
new file mode 100644
index 0000000000..23934b1405
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Services/Ssl/ServiceSsl.cs
@@ -0,0 +1,20 @@
+using Ryujinx.Core.OsHle.Ipc;
+using System.Collections.Generic;
+
+namespace Ryujinx.Core.OsHle.IpcServices.Ssl
+{
+    class ServiceSsl : IIpcService
+    {
+        private Dictionary<int, ServiceProcessRequest> m_Commands;
+
+        public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
+
+        public ServiceSsl()
+        {
+            m_Commands = new Dictionary<int, ServiceProcessRequest>()
+            {
+                //{ 0, Function }
+            };
+        }
+    }
+}
\ No newline at end of file