5f0e1e6d95ee960135e292a7fe6a8173bcba6b6d
[reactos.git] / reactos / 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 break;
79 case SERVICE_CONTROL_INTERROGATE:
80 return NO_ERROR;
81 default:
82 return ERROR_CALL_NOT_IMPLEMENTED;
83 }
84 return NO_ERROR;
85 }
86
87 static VOID CALLBACK
88 ServiceMain(DWORD argc, LPWSTR *argv)
89 {
90 HANDLE hThread;
91
92 UNREFERENCED_PARAMETER(argc);
93 UNREFERENCED_PARAMETER(argv);
94
95 DPRINT("ServiceMain() called\n");
96
97 SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
98 SvcStatus.dwCurrentState = SERVICE_START_PENDING;
99 SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
100 SvcStatus.dwCheckPoint = 0;
101 SvcStatus.dwWin32ExitCode = NO_ERROR;
102 SvcStatus.dwServiceSpecificExitCode = 0;
103 SvcStatus.dwWaitHint = 4000;
104
105 ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
106 ServiceControlHandler,
107 NULL);
108
109 UpdateServiceStatus(ServiceStatusHandle, SERVICE_RUNNING, 0);
110
111 hThread = CreateThread(NULL,
112 0,
113 (LPTHREAD_START_ROUTINE)
114 RpcThreadRoutine,
115 NULL,
116 0,
117 NULL);
118
119 if (!hThread)
120 {
121 DPRINT("Can't create RpcThread\n");
122 UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOPPED, 0);
123 }
124 else
125 {
126 CloseHandle(hThread);
127 }
128
129 DPRINT("ServiceMain() done\n");
130 }
131
132 int
133 wmain(int argc, WCHAR *argv[])
134 {
135 SERVICE_TABLE_ENTRYW ServiceTable[2] =
136 {
137 {ServiceName, ServiceMain},
138 {NULL, NULL}
139 };
140
141 UNREFERENCED_PARAMETER(argc);
142 UNREFERENCED_PARAMETER(argv);
143
144 DPRINT("wlansvc: main() started\n");
145
146 StartServiceCtrlDispatcherW(ServiceTable);
147
148 DPRINT("wlansvc: main() done\n");
149
150 ExitThread(0);
151
152 return 0;
153 }
154
155 /* EOF */