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: 23838 $
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 #ifndef NN_NDM_STUB
52         result = nn::srv::GetServiceHandle(&Interface::s_Session, PORT_NAME_USER);
53         NN_UTIL_RETURN_IF_FAILED(result);
54 #else
55         NN_LOG_WARN("stub mode.");
56 #endif
57     }
58     ++s_InitializedCount;
59     return ResultSuccess();
60 }
61 
Finalize()62 Result Finalize()
63 {
64     nn::os::CriticalSection::ScopedLock locker(s_cs);
65     Result result;
66 
67     if (s_InitializedCount == 0)
68     {
69         NN_LOG_WARN("ndm library is not initialized.");
70         return ResultNotInitialized();
71     }
72     else if (s_InitializedCount == 1)
73     {
74 #ifndef NN_NDM_STUB
75         result = nn::svc::CloseHandle(Interface::s_Session);
76         NN_UTIL_RETURN_IF_FAILED(result);
77 #endif
78 
79         CTR::detail::Interface::s_Session = Handle();
80     }
81     --s_InitializedCount;
82     return ResultSuccess();
83 }
84 
IsInitialized(void)85 bool IsInitialized(void)
86 {
87     return s_InitializedCount > 0;
88 }
89 
EnterExclusiveState(ExclusiveMode mode)90 Result EnterExclusiveState(ExclusiveMode mode)
91 {
92 #ifndef NN_NDM_STUB
93     return Interface::EnterExclusiveState(static_cast<s32>(mode));
94 #else
95 NN_UNUSED_VAR(mode);
96     return ResultSuccess();
97 #endif
98 }
99 
QueryExclusiveMode(ExclusiveMode & mode)100 Result QueryExclusiveMode(ExclusiveMode& mode)
101 {
102 #ifndef NN_NDM_STUB
103     s32 modeNumber;
104     Result result = Interface::QueryExclusiveMode(&modeNumber);
105     NN_UTIL_RETURN_IF_FAILED(result);
106     mode = static_cast<ExclusiveMode>(modeNumber);
107 #else
108     mode = ndm::EM_NONE;
109 #endif
110     return ResultSuccess();
111 }
112 
LeaveExclusiveState(void)113 Result LeaveExclusiveState(void)
114 {
115 #ifndef NN_NDM_STUB
116     return Interface::LeaveExclusiveState();
117 #else
118     return ResultSuccess();
119 #endif
120 }
121 
LockState(void)122 Result LockState(void)
123 {
124 #ifndef NN_NDM_STUB
125     return Interface::LockState();
126 #else
127     return ResultSuccess();
128 #endif
129 }
130 
UnlockState(void)131 Result UnlockState(void)
132 {
133 #ifndef NN_NDM_STUB
134     return Interface::UnlockState();
135 #else
136     return ResultSuccess();
137 #endif
138 }
139 
SuspendDaemons(bit32 mask)140 Result SuspendDaemons(bit32 mask)
141 {
142 #ifndef NN_NDM_STUB
143     return Interface::SuspendDaemons(mask);
144 #else
145     NN_UNUSED_VAR(mask);
146     return ResultSuccess();
147 #endif
148 }
149 
ResumeDaemons(bit32 mask)150 Result ResumeDaemons(bit32 mask)
151 {
152 #ifndef NN_NDM_STUB
153     return Interface::ResumeDaemons(mask);
154 #else
155     NN_UNUSED_VAR(mask);
156     return ResultSuccess();
157 #endif
158 }
159 
Suspend(DaemonName name)160 Result Suspend(DaemonName name)
161 {
162     if (name < 0 || name >= NUM_OF_DAEMONS)
163     {
164         return ResultInvalidEnumValue();
165     }
166     return SuspendDaemons(1 << name);
167 }
168 
Resume(DaemonName name)169 Result Resume(DaemonName name)
170 {
171     if (name < 0 || name >= NUM_OF_DAEMONS)
172     {
173         return ResultInvalidEnumValue();
174     }
175     return ResumeDaemons(1 << name);
176 }
177 
SuspendScheduler(bool bAsync)178 Result SuspendScheduler(bool bAsync)
179 {
180 #ifndef NN_NDM_STUB
181     return Interface::SuspendScheduler(bAsync);
182 #else
183     NN_UNUSED_VAR(bAsync);
184     return ResultSuccess();
185 #endif
186 }
187 
ResumeScheduler(void)188 Result ResumeScheduler(void)
189 {
190 #ifndef NN_NDM_STUB
191     return Interface::ResumeScheduler();
192 #else
193     return ResultSuccess();
194 #endif
195 }
196 
197 } // end of namespace ndm
198 } // end of namespace nn
199