Merge aicom-network-branch (without NDIS changes for now)
[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
25 /* FUNCTIONS *****************************************************************/
26 static DWORD WINAPI RpcThreadRoutine(LPVOID lpParameter)
27 {
28 RPC_STATUS Status;
29
30 Status = RpcServerUseProtseqEpW(L"ncalrpc", 20, L"wlansvc", NULL);
31 if (Status != RPC_S_OK)
32 {
33 DPRINT("RpcServerUseProtseqEpW() failed (Status %lx)\n", Status);
34 return 0;
35 }
36
37 Status = RpcServerRegisterIf(wlansvc_interface_v1_0_s_ifspec, NULL, NULL);
38 if (Status != RPC_S_OK)
39 {
40 DPRINT("RpcServerRegisterIf() failed (Status %lx)\n", Status);
41 return 0;
42 }
43
44 Status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, 0);
45 if (Status != RPC_S_OK)
46 {
47 DPRINT("RpcServerListen() failed (Status %lx)\n", Status);
48 }
49
50 DPRINT("RpcServerListen finished\n");
51 return 0;
52 }
53
54 static void UpdateServiceStatus(HANDLE hServiceStatus, DWORD NewStatus, DWORD Increment)
55 {
56 if (Increment > 0)
57 SvcStatus.dwCheckPoint += Increment;
58 else
59 SvcStatus.dwCheckPoint = 0;
60
61 SvcStatus.dwCurrentState = NewStatus;
62 SetServiceStatus(hServiceStatus, &SvcStatus);
63 }
64
65 static DWORD WINAPI
66 ServiceControlHandler(DWORD dwControl,
67 DWORD dwEventType,
68 LPVOID lpEventData,
69 LPVOID lpContext)
70 {
71 switch (dwControl)
72 {
73 case SERVICE_CONTROL_SHUTDOWN:
74 case SERVICE_CONTROL_STOP:
75 UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOP_PENDING, 1);
76 RpcMgmtStopServerListening(NULL);
77 break;
78 case SERVICE_CONTROL_INTERROGATE:
79 return NO_ERROR;
80 default:
81 return ERROR_CALL_NOT_IMPLEMENTED;
82 }
83 return NO_ERROR;
84 }
85
86 static VOID CALLBACK
87 ServiceMain(DWORD argc, LPWSTR *argv)
88 {
89 HANDLE hThread;
90
91 UNREFERENCED_PARAMETER(argc);
92 UNREFERENCED_PARAMETER(argv);
93
94 DPRINT("ServiceMain() called\n");
95
96 SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
97 SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
98 SvcStatus.dwCheckPoint = 0;
99 SvcStatus.dwWin32ExitCode = 0;
100 SvcStatus.dwServiceSpecificExitCode = 0;
101 SvcStatus.dwWaitHint = 4000;
102
103 ServiceStatusHandle = RegisterServiceCtrlHandlerExW(SERVICE_NAME,
104 ServiceControlHandler,
105 NULL);
106
107 UpdateServiceStatus(ServiceStatusHandle, SERVICE_RUNNING, 0);
108
109 hThread = CreateThread(NULL,
110 0,
111 (LPTHREAD_START_ROUTINE)
112 RpcThreadRoutine,
113 NULL,
114 0,
115 NULL);
116
117 if (!hThread)
118 DPRINT("Can't create RpcThread\n");
119 else
120 {
121 WaitForSingleObject(hThread, INFINITE);
122 CloseHandle(hThread);
123 }
124
125 UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOPPED, 0);
126
127 DPRINT("ServiceMain() done\n");
128 }
129
130 int
131 wmain(int argc, WCHAR *argv[])
132 {
133 SERVICE_TABLE_ENTRYW ServiceTable[2] =
134 {
135 {SERVICE_NAME, ServiceMain},
136 {NULL, NULL}
137 };
138
139 UNREFERENCED_PARAMETER(argc);
140 UNREFERENCED_PARAMETER(argv);
141
142 DPRINT("wlansvc: main() started\n");
143
144 StartServiceCtrlDispatcherW(ServiceTable);
145
146 DPRINT("wlansvc: main() done\n");
147
148 ExitThread(0);
149
150 return 0;
151 }
152
153 /* EOF */