- Update to r53061
[reactos.git] / base / services / wlansvc / wlansvc.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: services/wlansvc/wlansvc.c
5 * PURPOSE: WLAN Service
6 * PROGRAMMER: Christoph von Wittich
7 */
8
9 /* INCLUDES *****************************************************************/
10
11 #define WIN32_NO_STATUS
12 #include <windows.h>
13 #include "wlansvc_s.h"
14
15 //#define NDEBUG
16 #include <debug.h>
17
18 /* GLOBALS ******************************************************************/
19
20 #define SERVICE_NAME L"WLAN Service"
21
22 SERVICE_STATUS_HANDLE ServiceStatusHandle;
23 SERVICE_STATUS SvcStatus;
24 static WCHAR ServiceName[] = L"WlanSvc";
25
26 /* FUNCTIONS *****************************************************************/
27 static DWORD WINAPI RpcThreadRoutine(LPVOID lpParameter)
28 {
29 RPC_STATUS Status;
30
31 Status = RpcServerUseProtseqEpW(L"ncalrpc", 20, L"wlansvc", NULL);
32 if (Status != RPC_S_OK)
33 {
34 DPRINT("RpcServerUseProtseqEpW() failed (Status %lx)\n", Status);
35 return 0;
36 }
37
38 Status = RpcServerRegisterIf(wlansvc_interface_v1_0_s_ifspec, NULL, NULL);
39 if (Status != RPC_S_OK)
40 {
41 DPRINT("RpcServerRegisterIf() failed (Status %lx)\n", Status);
42 return 0;
43 }
44
45 Status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, 0);
46 if (Status != RPC_S_OK)
47 {
48 DPRINT("RpcServerListen() failed (Status %lx)\n", Status);
49 }
50
51 DPRINT("RpcServerListen finished\n");
52 return 0;
53 }
54
55 static void UpdateServiceStatus(HANDLE hServiceStatus, DWORD NewStatus, DWORD Increment)
56 {
57 if (Increment > 0)
58 SvcStatus.dwCheckPoint += Increment;
59 else
60 SvcStatus.dwCheckPoint = 0;
61
62 SvcStatus.dwCurrentState = NewStatus;
63 SetServiceStatus(hServiceStatus, &SvcStatus);
64 }
65
66 static DWORD WINAPI
67 ServiceControlHandler(DWORD dwControl,
68 DWORD dwEventType,
69 LPVOID lpEventData,
70 LPVOID lpContext)
71 {
72 switch (dwControl)
73 {
74 case SERVICE_CONTROL_SHUTDOWN:
75 case SERVICE_CONTROL_STOP:
76 UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOP_PENDING, 1);
77 RpcMgmtStopServerListening(NULL);
78 UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOPPED, 0);
79 break;
80 case SERVICE_CONTROL_INTERROGATE:
81 return NO_ERROR;
82 default:
83 return ERROR_CALL_NOT_IMPLEMENTED;
84 }
85 return NO_ERROR;
86 }
87
88 static VOID CALLBACK
89 ServiceMain(DWORD argc, LPWSTR *argv)
90 {
91 HANDLE hThread;
92
93 UNREFERENCED_PARAMETER(argc);
94 UNREFERENCED_PARAMETER(argv);
95
96 DPRINT("ServiceMain() called\n");
97
98 SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
99 SvcStatus.dwCurrentState = SERVICE_START_PENDING;
100 SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
101 SvcStatus.dwCheckPoint = 0;
102 SvcStatus.dwWin32ExitCode = NO_ERROR;
103 SvcStatus.dwServiceSpecificExitCode = 0;
104 SvcStatus.dwWaitHint = 4000;
105
106 ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
107 ServiceControlHandler,
108 NULL);
109
110 UpdateServiceStatus(ServiceStatusHandle, SERVICE_RUNNING, 0);
111
112 hThread = CreateThread(NULL,
113 0,
114 (LPTHREAD_START_ROUTINE)
115 RpcThreadRoutine,
116 NULL,
117 0,
118 NULL);
119
120 if (!hThread)
121 {
122 DPRINT("Can't create RpcThread\n");
123 UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOPPED, 0);
124 }
125 else
126 {
127 CloseHandle(hThread);
128 }
129
130 DPRINT("ServiceMain() done\n");
131 }
132
133 int
134 wmain(int argc, WCHAR *argv[])
135 {
136 SERVICE_TABLE_ENTRYW ServiceTable[2] =
137 {
138 {ServiceName, ServiceMain},
139 {NULL, NULL}
140 };
141
142 UNREFERENCED_PARAMETER(argc);
143 UNREFERENCED_PARAMETER(argv);
144
145 DPRINT("wlansvc: main() started\n");
146
147 StartServiceCtrlDispatcherW(ServiceTable);
148
149 DPRINT("wlansvc: main() done\n");
150
151 ExitThread(0);
152
153 return 0;
154 }
155
156 /* EOF */