1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     ndm_UserControl.cpp
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 36718 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/srv.h>
17 #include <nn/util.h>
18 #include <nn/ndm/CTR/ndm_Types.h>
19 #include <nn/ndm/ndm_Result.h>
20 #include <nn/ndm/ndm_UserControl.h>
21 #include <nn/ndm/CTR/ndm_Interface.h>
22 #include <nn/os/os_CriticalSection.h>
23 #include <nn/dbg/dbg_PrintResult.h>
24 
25 #include "ndm_LibraryPrivate.h"
26 
27 NN_DBG_DECLARE_GET_RESULT_DESCRIPTION_STRING_IMPL_KEEPER(ndm)
28 
29 namespace {
30 
31     s32                     s_InitializedCount = 0;
32     nn::os::CriticalSection s_cs = nn::WithInitialize();
33 }
34 
35 namespace nn {
36 namespace ndm {
37     using namespace CTR;
38     using namespace CTR::detail;
39 
Initialize(void)40 Result Initialize(void)
41 {
42     nn::os::CriticalSection::ScopedLock locker(s_cs);
43     Result result;
44 
45     NN_DBG_USE_GET_RESULT_DESCRIPTION_STRING_IMPL_KEEPER(ndm);
46 
47     if (s_InitializedCount == 0)
48     {
49         nn::srv::Initialize();
50 
51         result = nn::srv::GetServiceHandle(&Interface::s_Session, PORT_NAME_USER);
52         NN_UTIL_RETURN_IF_FAILED(result);
53     }
54     ++s_InitializedCount;
55     return ResultSuccess();
56 }
57 
Finalize()58 Result Finalize()
59 {
60     nn::os::CriticalSection::ScopedLock locker(s_cs);
61     Result result;
62 
63     if (s_InitializedCount == 0)
64     {
65         NN_LOG_WARN("ndm library is not initialized.");
66         return ResultNotInitialized();
67     }
68     else if (s_InitializedCount == 1)
69     {
70         result = nn::svc::CloseHandle(Interface::s_Session);
71         NN_UTIL_RETURN_IF_FAILED(result);
72 
73         CTR::detail::Interface::s_Session = Handle();
74     }
75     --s_InitializedCount;
76     return ResultSuccess();
77 }
78 
IsInitialized(void)79 bool IsInitialized(void)
80 {
81     return s_InitializedCount > 0;
82 }
83 
EnterExclusiveState(ExclusiveMode mode)84 Result EnterExclusiveState(ExclusiveMode mode)
85 {
86     return Interface::EnterExclusiveState(static_cast<s32>(mode));
87 }
88 
QueryExclusiveMode(ExclusiveMode & mode)89 Result QueryExclusiveMode(ExclusiveMode& mode)
90 {
91     s32 modeNumber;
92     Result result = Interface::QueryExclusiveMode(&modeNumber);
93     NN_UTIL_RETURN_IF_FAILED(result);
94     mode = static_cast<ExclusiveMode>(modeNumber);
95     return ResultSuccess();
96 }
97 
LeaveExclusiveState(void)98 Result LeaveExclusiveState(void)
99 {
100     return Interface::LeaveExclusiveState();
101 }
102 
LockState(void)103 Result LockState(void)
104 {
105     return Interface::LockState();
106 }
107 
UnlockState(void)108 Result UnlockState(void)
109 {
110     return Interface::UnlockState();
111 }
112 
SuspendDaemons(bit32 mask)113 Result SuspendDaemons(bit32 mask)
114 {
115     return Interface::SuspendDaemons(mask);
116 }
117 
ResumeDaemons(bit32 mask)118 Result ResumeDaemons(bit32 mask)
119 {
120     return Interface::ResumeDaemons(mask);
121 }
122 
Suspend(DaemonName name)123 Result Suspend(DaemonName name)
124 {
125     if (name < 0 || name >= NUM_OF_DAEMONS)
126     {
127         return ResultInvalidEnumValue();
128     }
129     return SuspendDaemons(1 << name);
130 }
131 
Resume(DaemonName name)132 Result Resume(DaemonName name)
133 {
134     if (name < 0 || name >= NUM_OF_DAEMONS)
135     {
136         return ResultInvalidEnumValue();
137     }
138     return ResumeDaemons(1 << name);
139 }
140 
SuspendScheduler(bool bAsync)141 Result SuspendScheduler(bool bAsync)
142 {
143     return Interface::SuspendScheduler(bAsync);
144 }
145 
ResumeScheduler(void)146 Result ResumeScheduler(void)
147 {
148     return Interface::ResumeScheduler();
149 }
150 
OverrideDefaultDaemons(bit32 mask)151 Result OverrideDefaultDaemons( bit32 mask )
152 {
153     return Interface::OverrideDefaultDaemons(mask);
154 }
155 
ResetDefaultDaemons(void)156 Result ResetDefaultDaemons( void )
157 {
158     return Interface::ResetDefaultDaemons();
159 }
160 
161 } // end of namespace ndm
162 } // end of namespace nn
163