1*c2c66affSColin Finck /*
2*c2c66affSColin Finck * PROJECT: ReactOS Drivers
3*c2c66affSColin Finck * LICENSE: BSD - See COPYING.ARM in the top level directory
4*c2c66affSColin Finck * FILE: drivers/sac/driver/channel.c
5*c2c66affSColin Finck * PURPOSE: Driver for the Server Administration Console (SAC) for EMS
6*c2c66affSColin Finck * PROGRAMMERS: ReactOS Portable Systems Group
7*c2c66affSColin Finck */
8*c2c66affSColin Finck
9*c2c66affSColin Finck /* INCLUDES *******************************************************************/
10*c2c66affSColin Finck
11*c2c66affSColin Finck #include "sacdrv.h"
12*c2c66affSColin Finck
13*c2c66affSColin Finck /* FUNCTIONS ******************************************************************/
14*c2c66affSColin Finck
15*c2c66affSColin Finck BOOLEAN
16*c2c66affSColin Finck NTAPI
ChannelIsValidType(IN SAC_CHANNEL_TYPE ChannelType)17*c2c66affSColin Finck ChannelIsValidType(IN SAC_CHANNEL_TYPE ChannelType)
18*c2c66affSColin Finck {
19*c2c66affSColin Finck /* Check if the type is valid */
20*c2c66affSColin Finck return ((ChannelType >= VtUtf8) && (ChannelType <= Raw));
21*c2c66affSColin Finck }
22*c2c66affSColin Finck
23*c2c66affSColin Finck BOOLEAN
24*c2c66affSColin Finck NTAPI
ChannelIsEqual(IN PSAC_CHANNEL Channel,IN PSAC_CHANNEL_ID ChannelId)25*c2c66affSColin Finck ChannelIsEqual(IN PSAC_CHANNEL Channel,
26*c2c66affSColin Finck IN PSAC_CHANNEL_ID ChannelId)
27*c2c66affSColin Finck {
28*c2c66affSColin Finck /* Check if the GUIDs match */
29*c2c66affSColin Finck return IsEqualGUIDAligned(&Channel->ChannelId.ChannelGuid,
30*c2c66affSColin Finck &ChannelId->ChannelGuid);
31*c2c66affSColin Finck }
32*c2c66affSColin Finck
33*c2c66affSColin Finck NTSTATUS
34*c2c66affSColin Finck NTAPI
ChannelDereferenceHandles(IN PSAC_CHANNEL Channel)35*c2c66affSColin Finck ChannelDereferenceHandles(IN PSAC_CHANNEL Channel)
36*c2c66affSColin Finck {
37*c2c66affSColin Finck CHECK_PARAMETER(Channel);
38*c2c66affSColin Finck
39*c2c66affSColin Finck /* Clear the data event */
40*c2c66affSColin Finck if (Channel->HasNewDataEvent)
41*c2c66affSColin Finck {
42*c2c66affSColin Finck ChannelUninitializeEvent(Channel,
43*c2c66affSColin Finck HasNewDataEvent,
44*c2c66affSColin Finck SAC_CHANNEL_FLAG_HAS_NEW_DATA_EVENT);
45*c2c66affSColin Finck }
46*c2c66affSColin Finck
47*c2c66affSColin Finck /* Clear the close event */
48*c2c66affSColin Finck if (Channel->CloseEvent)
49*c2c66affSColin Finck {
50*c2c66affSColin Finck ChannelUninitializeEvent(Channel,
51*c2c66affSColin Finck CloseEvent,
52*c2c66affSColin Finck SAC_CHANNEL_FLAG_CLOSE_EVENT);
53*c2c66affSColin Finck }
54*c2c66affSColin Finck
55*c2c66affSColin Finck /* Clear the lock event */
56*c2c66affSColin Finck if (Channel->LockEvent)
57*c2c66affSColin Finck {
58*c2c66affSColin Finck ChannelUninitializeEvent(Channel,
59*c2c66affSColin Finck LockEvent,
60*c2c66affSColin Finck SAC_CHANNEL_FLAG_LOCK_EVENT);
61*c2c66affSColin Finck }
62*c2c66affSColin Finck
63*c2c66affSColin Finck /* Clear the redraw event */
64*c2c66affSColin Finck if (Channel->RedrawEvent)
65*c2c66affSColin Finck {
66*c2c66affSColin Finck ChannelUninitializeEvent(Channel,
67*c2c66affSColin Finck RedrawEvent,
68*c2c66affSColin Finck SAC_CHANNEL_FLAG_REDRAW_EVENT);
69*c2c66affSColin Finck }
70*c2c66affSColin Finck
71*c2c66affSColin Finck /* All done */
72*c2c66affSColin Finck return STATUS_SUCCESS;
73*c2c66affSColin Finck }
74*c2c66affSColin Finck
75*c2c66affSColin Finck NTSTATUS
76*c2c66affSColin Finck NTAPI
ChannelDestroy(IN PSAC_CHANNEL Channel)77*c2c66affSColin Finck ChannelDestroy(IN PSAC_CHANNEL Channel)
78*c2c66affSColin Finck {
79*c2c66affSColin Finck CHECK_PARAMETER(Channel);
80*c2c66affSColin Finck
81*c2c66affSColin Finck /* Same thing as dereferencing all the handles */
82*c2c66affSColin Finck return ChannelDereferenceHandles(Channel);
83*c2c66affSColin Finck }
84*c2c66affSColin Finck
85*c2c66affSColin Finck NTSTATUS
86*c2c66affSColin Finck NTAPI
ChannelOWrite(IN PSAC_CHANNEL Channel,IN PCHAR Buffer,IN ULONG BufferSize)87*c2c66affSColin Finck ChannelOWrite(IN PSAC_CHANNEL Channel,
88*c2c66affSColin Finck IN PCHAR Buffer,
89*c2c66affSColin Finck IN ULONG BufferSize)
90*c2c66affSColin Finck {
91*c2c66affSColin Finck NTSTATUS Status;
92*c2c66affSColin Finck CHECK_PARAMETER3(BufferSize < SAC_OBUFFER_SIZE);
93*c2c66affSColin Finck
94*c2c66affSColin Finck /* While holding the output lock, write to the output buffer */
95*c2c66affSColin Finck ChannelLockOBuffer(Channel);
96*c2c66affSColin Finck Status = Channel->ChannelOutputWrite(Channel, Buffer, BufferSize);
97*c2c66affSColin Finck ChannelUnlockOBuffer(Channel);
98*c2c66affSColin Finck return Status;
99*c2c66affSColin Finck }
100*c2c66affSColin Finck
101*c2c66affSColin Finck NTSTATUS
102*c2c66affSColin Finck NTAPI
ChannelOFlush(IN PSAC_CHANNEL Channel)103*c2c66affSColin Finck ChannelOFlush(IN PSAC_CHANNEL Channel)
104*c2c66affSColin Finck {
105*c2c66affSColin Finck NTSTATUS Status;
106*c2c66affSColin Finck
107*c2c66affSColin Finck /* While holding the output lock, flush to the output buffer */
108*c2c66affSColin Finck ChannelLockOBuffer(Channel);
109*c2c66affSColin Finck Status = Channel->ChannelOutputFlush(Channel);
110*c2c66affSColin Finck ChannelUnlockOBuffer(Channel);
111*c2c66affSColin Finck return Status;
112*c2c66affSColin Finck }
113*c2c66affSColin Finck
114*c2c66affSColin Finck NTSTATUS
115*c2c66affSColin Finck NTAPI
ChannelIWrite(IN PSAC_CHANNEL Channel,IN PCHAR Buffer,IN ULONG BufferSize)116*c2c66affSColin Finck ChannelIWrite(IN PSAC_CHANNEL Channel,
117*c2c66affSColin Finck IN PCHAR Buffer,
118*c2c66affSColin Finck IN ULONG BufferSize)
119*c2c66affSColin Finck {
120*c2c66affSColin Finck NTSTATUS Status;
121*c2c66affSColin Finck
122*c2c66affSColin Finck /* Write into the input buffer while holding the lock */
123*c2c66affSColin Finck ChannelLockIBuffer(Channel);
124*c2c66affSColin Finck Status = Channel->ChannelInputWrite(Channel, Buffer, BufferSize);
125*c2c66affSColin Finck ChannelUnlockIBuffer(Channel);
126*c2c66affSColin Finck return Status;
127*c2c66affSColin Finck }
128*c2c66affSColin Finck
129*c2c66affSColin Finck NTSTATUS
130*c2c66affSColin Finck NTAPI
ChannelIRead(IN PSAC_CHANNEL Channel,IN PCHAR Buffer,IN ULONG BufferSize,IN OUT PULONG ResultBufferSize)131*c2c66affSColin Finck ChannelIRead(IN PSAC_CHANNEL Channel,
132*c2c66affSColin Finck IN PCHAR Buffer,
133*c2c66affSColin Finck IN ULONG BufferSize,
134*c2c66affSColin Finck IN OUT PULONG ResultBufferSize)
135*c2c66affSColin Finck {
136*c2c66affSColin Finck NTSTATUS Status;
137*c2c66affSColin Finck
138*c2c66affSColin Finck /* Read the input buffer while holding the lock */
139*c2c66affSColin Finck ChannelLockIBuffer(Channel);
140*c2c66affSColin Finck Status = Channel->ChannelInputRead(Channel,
141*c2c66affSColin Finck Buffer,
142*c2c66affSColin Finck BufferSize,
143*c2c66affSColin Finck ResultBufferSize);
144*c2c66affSColin Finck ChannelUnlockIBuffer(Channel);
145*c2c66affSColin Finck return Status;
146*c2c66affSColin Finck }
147*c2c66affSColin Finck
148*c2c66affSColin Finck WCHAR
149*c2c66affSColin Finck NTAPI
ChannelIReadLast(IN PSAC_CHANNEL Channel)150*c2c66affSColin Finck ChannelIReadLast(IN PSAC_CHANNEL Channel)
151*c2c66affSColin Finck {
152*c2c66affSColin Finck WCHAR LastChar;
153*c2c66affSColin Finck
154*c2c66affSColin Finck /* Read the last character while holding the lock */
155*c2c66affSColin Finck ChannelLockIBuffer(Channel);
156*c2c66affSColin Finck LastChar = Channel->ChannelInputReadLast(Channel);
157*c2c66affSColin Finck ChannelUnlockIBuffer(Channel);
158*c2c66affSColin Finck return LastChar;
159*c2c66affSColin Finck }
160*c2c66affSColin Finck
161*c2c66affSColin Finck ULONG
162*c2c66affSColin Finck NTAPI
ChannelIBufferLength(IN PSAC_CHANNEL Channel)163*c2c66affSColin Finck ChannelIBufferLength(IN PSAC_CHANNEL Channel)
164*c2c66affSColin Finck {
165*c2c66affSColin Finck ULONG Length;
166*c2c66affSColin Finck
167*c2c66affSColin Finck /* Get the input buffer length while holding the lock */
168*c2c66affSColin Finck ChannelLockOBuffer(Channel);
169*c2c66affSColin Finck Length = Channel->ChannelInputBufferLength(Channel);
170*c2c66affSColin Finck ChannelUnlockOBuffer(Channel);
171*c2c66affSColin Finck return Length;
172*c2c66affSColin Finck }
173*c2c66affSColin Finck
174*c2c66affSColin Finck NTSTATUS
175*c2c66affSColin Finck NTAPI
ChannelSetRedrawEvent(IN PSAC_CHANNEL Channel)176*c2c66affSColin Finck ChannelSetRedrawEvent(IN PSAC_CHANNEL Channel)
177*c2c66affSColin Finck {
178*c2c66affSColin Finck NTSTATUS Status;
179*c2c66affSColin Finck
180*c2c66affSColin Finck /* Set the event */
181*c2c66affSColin Finck ChannelSetEvent(Channel, RedrawEvent);
182*c2c66affSColin Finck return Status;
183*c2c66affSColin Finck }
184*c2c66affSColin Finck
185*c2c66affSColin Finck NTSTATUS
186*c2c66affSColin Finck NTAPI
ChannelSetLockEvent(IN PSAC_CHANNEL Channel)187*c2c66affSColin Finck ChannelSetLockEvent(IN PSAC_CHANNEL Channel)
188*c2c66affSColin Finck {
189*c2c66affSColin Finck NTSTATUS Status;
190*c2c66affSColin Finck
191*c2c66affSColin Finck /* Set the event */
192*c2c66affSColin Finck ChannelSetEvent(Channel, LockEvent);
193*c2c66affSColin Finck return Status;
194*c2c66affSColin Finck }
195*c2c66affSColin Finck
196*c2c66affSColin Finck NTSTATUS
197*c2c66affSColin Finck NTAPI
ChannelClearRedrawEvent(IN PSAC_CHANNEL Channel)198*c2c66affSColin Finck ChannelClearRedrawEvent(IN PSAC_CHANNEL Channel)
199*c2c66affSColin Finck {
200*c2c66affSColin Finck NTSTATUS Status;
201*c2c66affSColin Finck
202*c2c66affSColin Finck /* Clear the event */
203*c2c66affSColin Finck ChannelClearEvent(Channel, RedrawEvent);
204*c2c66affSColin Finck return Status;
205*c2c66affSColin Finck }
206*c2c66affSColin Finck
207*c2c66affSColin Finck NTSTATUS
208*c2c66affSColin Finck NTAPI
ChannelHasRedrawEvent(IN PSAC_CHANNEL Channel,OUT PBOOLEAN Present)209*c2c66affSColin Finck ChannelHasRedrawEvent(IN PSAC_CHANNEL Channel,
210*c2c66affSColin Finck OUT PBOOLEAN Present)
211*c2c66affSColin Finck {
212*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
213*c2c66affSColin Finck CHECK_PARAMETER2(Present);
214*c2c66affSColin Finck
215*c2c66affSColin Finck /* Return if the flag is set */
216*c2c66affSColin Finck *Present = Channel->Flags & SAC_CHANNEL_FLAG_REDRAW_EVENT;
217*c2c66affSColin Finck return STATUS_SUCCESS;
218*c2c66affSColin Finck }
219*c2c66affSColin Finck
220*c2c66affSColin Finck NTSTATUS
221*c2c66affSColin Finck NTAPI
ChannelGetStatus(IN PSAC_CHANNEL Channel,OUT PSAC_CHANNEL_STATUS ChannelStatus)222*c2c66affSColin Finck ChannelGetStatus(IN PSAC_CHANNEL Channel,
223*c2c66affSColin Finck OUT PSAC_CHANNEL_STATUS ChannelStatus)
224*c2c66affSColin Finck {
225*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
226*c2c66affSColin Finck
227*c2c66affSColin Finck /* Read the status while holding the attribute lock */
228*c2c66affSColin Finck ChannelLockAttributes(Channel);
229*c2c66affSColin Finck *ChannelStatus = Channel->ChannelStatus;
230*c2c66affSColin Finck ChannelUnlockAttributes(Channel);
231*c2c66affSColin Finck return STATUS_SUCCESS;
232*c2c66affSColin Finck }
233*c2c66affSColin Finck
234*c2c66affSColin Finck NTSTATUS
235*c2c66affSColin Finck NTAPI
ChannelSetStatus(IN PSAC_CHANNEL Channel,IN SAC_CHANNEL_STATUS ChannelStatus)236*c2c66affSColin Finck ChannelSetStatus(IN PSAC_CHANNEL Channel,
237*c2c66affSColin Finck IN SAC_CHANNEL_STATUS ChannelStatus)
238*c2c66affSColin Finck {
239*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
240*c2c66affSColin Finck
241*c2c66affSColin Finck /* Read the status while holding the attribute lock */
242*c2c66affSColin Finck ChannelLockAttributes(Channel);
243*c2c66affSColin Finck Channel->ChannelStatus = ChannelStatus;
244*c2c66affSColin Finck ChannelUnlockAttributes(Channel);
245*c2c66affSColin Finck return STATUS_SUCCESS;
246*c2c66affSColin Finck }
247*c2c66affSColin Finck
248*c2c66affSColin Finck BOOLEAN
249*c2c66affSColin Finck NTAPI
ChannelIsActive(IN PSAC_CHANNEL Channel)250*c2c66affSColin Finck ChannelIsActive(IN PSAC_CHANNEL Channel)
251*c2c66affSColin Finck {
252*c2c66affSColin Finck SAC_CHANNEL_STATUS ChannelStatus;
253*c2c66affSColin Finck BOOLEAN IsActive;
254*c2c66affSColin Finck
255*c2c66affSColin Finck /* Get the status */
256*c2c66affSColin Finck if (!NT_SUCCESS(ChannelGetStatus(Channel, &ChannelStatus)))
257*c2c66affSColin Finck {
258*c2c66affSColin Finck /* We couldn't even do that, assume it's inactive */
259*c2c66affSColin Finck IsActive = FALSE;
260*c2c66affSColin Finck }
261*c2c66affSColin Finck else
262*c2c66affSColin Finck {
263*c2c66affSColin Finck /* Check if the status shows activity */
264*c2c66affSColin Finck IsActive = (ChannelStatus == Active);
265*c2c66affSColin Finck }
266*c2c66affSColin Finck
267*c2c66affSColin Finck /* Return the state */
268*c2c66affSColin Finck return IsActive;
269*c2c66affSColin Finck }
270*c2c66affSColin Finck
271*c2c66affSColin Finck BOOLEAN
272*c2c66affSColin Finck NTAPI
ChannelIsClosed(IN PSAC_CHANNEL Channel)273*c2c66affSColin Finck ChannelIsClosed(IN PSAC_CHANNEL Channel)
274*c2c66affSColin Finck {
275*c2c66affSColin Finck SAC_CHANNEL_STATUS ChannelStatus;
276*c2c66affSColin Finck BOOLEAN IsClosed;
277*c2c66affSColin Finck
278*c2c66affSColin Finck /* Get the status */
279*c2c66affSColin Finck if (!NT_SUCCESS(ChannelGetStatus(Channel, &ChannelStatus)))
280*c2c66affSColin Finck {
281*c2c66affSColin Finck /* We couldn't even do that, assume it's inactive */
282*c2c66affSColin Finck IsClosed = FALSE;
283*c2c66affSColin Finck }
284*c2c66affSColin Finck else
285*c2c66affSColin Finck {
286*c2c66affSColin Finck /* Check if the status shows activity */
287*c2c66affSColin Finck IsClosed = ((ChannelStatus == Inactive) &&
288*c2c66affSColin Finck (Channel->ChannelHasNewOBufferData));
289*c2c66affSColin Finck }
290*c2c66affSColin Finck
291*c2c66affSColin Finck /* Return the state */
292*c2c66affSColin Finck return IsClosed;
293*c2c66affSColin Finck }
294*c2c66affSColin Finck
295*c2c66affSColin Finck NTSTATUS
296*c2c66affSColin Finck NTAPI
ChannelGetName(IN PSAC_CHANNEL Channel,OUT PWCHAR * Name)297*c2c66affSColin Finck ChannelGetName(IN PSAC_CHANNEL Channel,
298*c2c66affSColin Finck OUT PWCHAR *Name)
299*c2c66affSColin Finck {
300*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
301*c2c66affSColin Finck CHECK_PARAMETER2(Name);
302*c2c66affSColin Finck
303*c2c66affSColin Finck /* Allocate space to hold the name */
304*c2c66affSColin Finck *Name = SacAllocatePool(sizeof(Channel->NameBuffer), GLOBAL_BLOCK_TAG);
305*c2c66affSColin Finck CHECK_ALLOCATION(*Name);
306*c2c66affSColin Finck
307*c2c66affSColin Finck /* Lock the attributes while we copy the name */
308*c2c66affSColin Finck ChannelLockAttributes(Channel);
309*c2c66affSColin Finck
310*c2c66affSColin Finck /* Copy the name and null-terminate it */
311*c2c66affSColin Finck ASSERT(((wcslen(Channel->NameBuffer) + 1) * sizeof(WCHAR)) <= ((SAC_CHANNEL_NAME_SIZE + 1) * sizeof(WCHAR)));
312*c2c66affSColin Finck wcsncpy(*Name, Channel->NameBuffer, RTL_NUMBER_OF(Channel->NameBuffer)); // bug
313*c2c66affSColin Finck (*Name)[SAC_CHANNEL_NAME_SIZE] = UNICODE_NULL;
314*c2c66affSColin Finck
315*c2c66affSColin Finck /* Release the lock and return */
316*c2c66affSColin Finck ChannelUnlockAttributes(Channel);
317*c2c66affSColin Finck return STATUS_SUCCESS;
318*c2c66affSColin Finck }
319*c2c66affSColin Finck
320*c2c66affSColin Finck NTSTATUS
321*c2c66affSColin Finck NTAPI
ChannelSetName(IN PSAC_CHANNEL Channel,IN PWCHAR Name)322*c2c66affSColin Finck ChannelSetName(IN PSAC_CHANNEL Channel,
323*c2c66affSColin Finck IN PWCHAR Name)
324*c2c66affSColin Finck {
325*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
326*c2c66affSColin Finck CHECK_PARAMETER2(Name);
327*c2c66affSColin Finck
328*c2c66affSColin Finck /* Lock the attributes while we copy the name */
329*c2c66affSColin Finck ChannelLockAttributes(Channel);
330*c2c66affSColin Finck
331*c2c66affSColin Finck /* Copy the name and null-terminate it */
332*c2c66affSColin Finck ASSERT(((wcslen(Name) + 1) * sizeof(WCHAR)) <= ((SAC_CHANNEL_NAME_SIZE + 1) * sizeof(WCHAR)));
333*c2c66affSColin Finck wcsncpy(Channel->NameBuffer, Name, RTL_NUMBER_OF(Channel->NameBuffer)); // bug
334*c2c66affSColin Finck Channel->NameBuffer[SAC_CHANNEL_NAME_SIZE] = UNICODE_NULL;
335*c2c66affSColin Finck
336*c2c66affSColin Finck /* Release the lock and return */
337*c2c66affSColin Finck ChannelUnlockAttributes(Channel);
338*c2c66affSColin Finck return STATUS_SUCCESS;
339*c2c66affSColin Finck }
340*c2c66affSColin Finck
341*c2c66affSColin Finck NTSTATUS
342*c2c66affSColin Finck NTAPI
ChannelGetDescription(IN PSAC_CHANNEL Channel,IN PWCHAR * Description)343*c2c66affSColin Finck ChannelGetDescription(IN PSAC_CHANNEL Channel,
344*c2c66affSColin Finck IN PWCHAR* Description)
345*c2c66affSColin Finck {
346*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
347*c2c66affSColin Finck CHECK_PARAMETER2(Description);
348*c2c66affSColin Finck
349*c2c66affSColin Finck /* Allocate space to hold the description */
350*c2c66affSColin Finck *Description = SacAllocatePool(sizeof(Channel->DescriptionBuffer), GLOBAL_BLOCK_TAG);
351*c2c66affSColin Finck CHECK_ALLOCATION(*Description);
352*c2c66affSColin Finck
353*c2c66affSColin Finck /* Lock the attributes while we copy the name */
354*c2c66affSColin Finck ChannelLockAttributes(Channel);
355*c2c66affSColin Finck
356*c2c66affSColin Finck /* Copy the name and null-terminate it */
357*c2c66affSColin Finck ASSERT(((wcslen(Channel->DescriptionBuffer) + 1) * sizeof(WCHAR)) <= ((SAC_CHANNEL_DESCRIPTION_SIZE + 1) * sizeof(WCHAR)));
358*c2c66affSColin Finck wcsncpy(*Description, Channel->DescriptionBuffer, RTL_NUMBER_OF(Channel->DescriptionBuffer)); // bug
359*c2c66affSColin Finck (*Description)[SAC_CHANNEL_DESCRIPTION_SIZE] = UNICODE_NULL;
360*c2c66affSColin Finck
361*c2c66affSColin Finck /* Release the lock and return */
362*c2c66affSColin Finck ChannelUnlockAttributes(Channel);
363*c2c66affSColin Finck return STATUS_SUCCESS;
364*c2c66affSColin Finck }
365*c2c66affSColin Finck
366*c2c66affSColin Finck NTSTATUS
367*c2c66affSColin Finck NTAPI
ChannelSetDescription(IN PSAC_CHANNEL Channel,IN PWCHAR Description)368*c2c66affSColin Finck ChannelSetDescription(IN PSAC_CHANNEL Channel,
369*c2c66affSColin Finck IN PWCHAR Description)
370*c2c66affSColin Finck {
371*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
372*c2c66affSColin Finck CHECK_PARAMETER2(Description);
373*c2c66affSColin Finck
374*c2c66affSColin Finck /* Lock the attributes while we copy the name */
375*c2c66affSColin Finck ChannelLockAttributes(Channel);
376*c2c66affSColin Finck
377*c2c66affSColin Finck /* Copy the name and null-terminate it */
378*c2c66affSColin Finck ASSERT(((wcslen(Description) + 1) * sizeof(WCHAR)) <= ((SAC_CHANNEL_NAME_SIZE + 1) * sizeof(WCHAR)));
379*c2c66affSColin Finck wcsncpy(Channel->DescriptionBuffer, Description, RTL_NUMBER_OF(Channel->DescriptionBuffer)); // bug
380*c2c66affSColin Finck Channel->DescriptionBuffer[SAC_CHANNEL_DESCRIPTION_SIZE] = UNICODE_NULL;
381*c2c66affSColin Finck
382*c2c66affSColin Finck /* Release the lock and return */
383*c2c66affSColin Finck ChannelUnlockAttributes(Channel);
384*c2c66affSColin Finck return STATUS_SUCCESS;
385*c2c66affSColin Finck }
386*c2c66affSColin Finck
387*c2c66affSColin Finck NTSTATUS
388*c2c66affSColin Finck NTAPI
ChannelGetApplicationType(IN PSAC_CHANNEL Channel,OUT PGUID ApplicationType)389*c2c66affSColin Finck ChannelGetApplicationType(IN PSAC_CHANNEL Channel,
390*c2c66affSColin Finck OUT PGUID ApplicationType)
391*c2c66affSColin Finck {
392*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
393*c2c66affSColin Finck
394*c2c66affSColin Finck /* Read the application type GUID */
395*c2c66affSColin Finck ChannelLockAttributes(Channel);
396*c2c66affSColin Finck *ApplicationType = Channel->ApplicationType;
397*c2c66affSColin Finck ChannelUnlockAttributes(Channel);
398*c2c66affSColin Finck
399*c2c66affSColin Finck /* Always return success */
400*c2c66affSColin Finck return STATUS_SUCCESS;
401*c2c66affSColin Finck }
402*c2c66affSColin Finck
403*c2c66affSColin Finck NTSTATUS
404*c2c66affSColin Finck NTAPI
ChannelInitializeVTable(IN PSAC_CHANNEL Channel)405*c2c66affSColin Finck ChannelInitializeVTable(IN PSAC_CHANNEL Channel)
406*c2c66affSColin Finck {
407*c2c66affSColin Finck /* What kind of channel is this? */
408*c2c66affSColin Finck switch (Channel->ChannelType)
409*c2c66affSColin Finck {
410*c2c66affSColin Finck case VtUtf8:
411*c2c66affSColin Finck /* Setup the calls for a VT-UTF8 channel */
412*c2c66affSColin Finck Channel->ChannelCreate = VTUTF8ChannelCreate;
413*c2c66affSColin Finck Channel->ChannelDestroy = VTUTF8ChannelDestroy;
414*c2c66affSColin Finck Channel->ChannelOutputFlush = VTUTF8ChannelOFlush;
415*c2c66affSColin Finck Channel->ChannelOutputEcho = VTUTF8ChannelOEcho;
416*c2c66affSColin Finck Channel->ChannelOutputWrite = VTUTF8ChannelOWrite;
417*c2c66affSColin Finck Channel->ChannelOutputRead = VTUTF8ChannelORead;
418*c2c66affSColin Finck Channel->ChannelInputWrite = VTUTF8ChannelIWrite;
419*c2c66affSColin Finck Channel->ChannelInputRead = VTUTF8ChannelIRead;
420*c2c66affSColin Finck Channel->ChannelInputReadLast = VTUTF8ChannelIReadLast;
421*c2c66affSColin Finck Channel->ChannelInputBufferIsFull = VTUTF8ChannelIBufferIsFull;
422*c2c66affSColin Finck Channel->ChannelInputBufferLength = VTUTF8ChannelIBufferLength;
423*c2c66affSColin Finck break;
424*c2c66affSColin Finck
425*c2c66affSColin Finck case Cmd:
426*c2c66affSColin Finck /* FIXME: TODO */
427*c2c66affSColin Finck ASSERT(FALSE);
428*c2c66affSColin Finck return STATUS_NOT_IMPLEMENTED;
429*c2c66affSColin Finck
430*c2c66affSColin Finck case Raw:
431*c2c66affSColin Finck
432*c2c66affSColin Finck /* Setup the calls for a raw channel */
433*c2c66affSColin Finck Channel->ChannelCreate = RawChannelCreate;
434*c2c66affSColin Finck Channel->ChannelDestroy = RawChannelDestroy;
435*c2c66affSColin Finck Channel->ChannelOutputFlush = RawChannelOFlush;
436*c2c66affSColin Finck Channel->ChannelOutputEcho = RawChannelOEcho;
437*c2c66affSColin Finck Channel->ChannelOutputWrite = RawChannelOWrite;
438*c2c66affSColin Finck Channel->ChannelOutputRead = RawChannelORead;
439*c2c66affSColin Finck Channel->ChannelInputWrite = RawChannelIWrite;
440*c2c66affSColin Finck Channel->ChannelInputRead = RawChannelIRead;
441*c2c66affSColin Finck Channel->ChannelInputReadLast = RawChannelIReadLast;
442*c2c66affSColin Finck Channel->ChannelInputBufferIsFull = RawChannelIBufferIsFull;
443*c2c66affSColin Finck Channel->ChannelInputBufferLength = RawChannelIBufferLength;
444*c2c66affSColin Finck break;
445*c2c66affSColin Finck
446*c2c66affSColin Finck default:
447*c2c66affSColin Finck /* Unsupported channel type */
448*c2c66affSColin Finck return STATUS_INVALID_PARAMETER;
449*c2c66affSColin Finck }
450*c2c66affSColin Finck
451*c2c66affSColin Finck /* If we got here, the channel was supported */
452*c2c66affSColin Finck return STATUS_SUCCESS;
453*c2c66affSColin Finck }
454*c2c66affSColin Finck
455*c2c66affSColin Finck NTSTATUS
456*c2c66affSColin Finck NTAPI
ChannelCreate(IN PSAC_CHANNEL Channel,IN PSAC_CHANNEL_ATTRIBUTES Attributes,IN SAC_CHANNEL_ID ChannelId)457*c2c66affSColin Finck ChannelCreate(IN PSAC_CHANNEL Channel,
458*c2c66affSColin Finck IN PSAC_CHANNEL_ATTRIBUTES Attributes,
459*c2c66affSColin Finck IN SAC_CHANNEL_ID ChannelId)
460*c2c66affSColin Finck {
461*c2c66affSColin Finck NTSTATUS Status;
462*c2c66affSColin Finck CHECK_PARAMETER1(Channel);
463*c2c66affSColin Finck CHECK_PARAMETER2(Attributes);
464*c2c66affSColin Finck
465*c2c66affSColin Finck /* If a close event is being passed in, it must exist, and vice-versa */
466*c2c66affSColin Finck if (Attributes->Flag & SAC_CHANNEL_FLAG_CLOSE_EVENT)
467*c2c66affSColin Finck {
468*c2c66affSColin Finck CHECK_PARAMETER(Attributes->CloseEvent != NULL);
469*c2c66affSColin Finck }
470*c2c66affSColin Finck else
471*c2c66affSColin Finck {
472*c2c66affSColin Finck CHECK_PARAMETER(Attributes->CloseEvent == NULL);
473*c2c66affSColin Finck }
474*c2c66affSColin Finck
475*c2c66affSColin Finck /* If a new data event is being passed in, it must exist, and vice-versa */
476*c2c66affSColin Finck if (Attributes->Flag & SAC_CHANNEL_FLAG_HAS_NEW_DATA_EVENT)
477*c2c66affSColin Finck {
478*c2c66affSColin Finck CHECK_PARAMETER(Attributes->HasNewDataEvent != NULL);
479*c2c66affSColin Finck }
480*c2c66affSColin Finck else
481*c2c66affSColin Finck {
482*c2c66affSColin Finck CHECK_PARAMETER(Attributes->HasNewDataEvent == NULL);
483*c2c66affSColin Finck }
484*c2c66affSColin Finck
485*c2c66affSColin Finck /* If a lock event is being passed in, it must exist, and vice-versa */
486*c2c66affSColin Finck if (Attributes->Flag & SAC_CHANNEL_FLAG_LOCK_EVENT)
487*c2c66affSColin Finck {
488*c2c66affSColin Finck CHECK_PARAMETER(Attributes->LockEvent != NULL);
489*c2c66affSColin Finck }
490*c2c66affSColin Finck else
491*c2c66affSColin Finck {
492*c2c66affSColin Finck CHECK_PARAMETER(Attributes->LockEvent == NULL);
493*c2c66affSColin Finck }
494*c2c66affSColin Finck
495*c2c66affSColin Finck /* If a redraw event is being passed in, it must exist, and vice-versa */
496*c2c66affSColin Finck if (Attributes->Flag & SAC_CHANNEL_FLAG_REDRAW_EVENT)
497*c2c66affSColin Finck {
498*c2c66affSColin Finck CHECK_PARAMETER(Attributes->RedrawEvent != NULL);
499*c2c66affSColin Finck }
500*c2c66affSColin Finck else
501*c2c66affSColin Finck {
502*c2c66affSColin Finck CHECK_PARAMETER(Attributes->RedrawEvent == NULL);
503*c2c66affSColin Finck }
504*c2c66affSColin Finck
505*c2c66affSColin Finck /* Initialize the channel structure */
506*c2c66affSColin Finck RtlZeroMemory(Channel, sizeof(SAC_CHANNEL));
507*c2c66affSColin Finck Channel->ChannelId = ChannelId;
508*c2c66affSColin Finck Channel->ChannelType = Attributes->ChannelType;
509*c2c66affSColin Finck Channel->Flags = Attributes->Flag;
510*c2c66affSColin Finck if (Attributes->Flag & SAC_CHANNEL_FLAG_APPLICATION)
511*c2c66affSColin Finck {
512*c2c66affSColin Finck Channel->ApplicationType = Attributes->ChannelId;
513*c2c66affSColin Finck }
514*c2c66affSColin Finck
515*c2c66affSColin Finck /* Initialize all the locks and events */
516*c2c66affSColin Finck SacInitializeLock(&Channel->ChannelAttributeLock);
517*c2c66affSColin Finck SacInitializeLock(&Channel->ChannelOBufferLock);
518*c2c66affSColin Finck SacInitializeLock(&Channel->ChannelIBufferLock);
519*c2c66affSColin Finck ChannelInitializeEvent(Channel, Attributes, CloseEvent);
520*c2c66affSColin Finck ChannelInitializeEvent(Channel, Attributes, HasNewDataEvent);
521*c2c66affSColin Finck ChannelInitializeEvent(Channel, Attributes, LockEvent);
522*c2c66affSColin Finck ChannelInitializeEvent(Channel, Attributes, RedrawEvent);
523*c2c66affSColin Finck
524*c2c66affSColin Finck /* Set the name and description */
525*c2c66affSColin Finck ChannelSetName(Channel, Attributes->NameBuffer);
526*c2c66affSColin Finck ChannelSetDescription(Channel, Attributes->DescriptionBuffer);
527*c2c66affSColin Finck
528*c2c66affSColin Finck /* Initialize the function table for the type of channel this is */
529*c2c66affSColin Finck Status = ChannelInitializeVTable(Channel);
530*c2c66affSColin Finck if (!NT_SUCCESS(Status))
531*c2c66affSColin Finck {
532*c2c66affSColin Finck /* This is critical */
533*c2c66affSColin Finck SAC_DBG(SAC_DBG_INIT, "SAC Create Channel :: Failed to initialize vtable\n");
534*c2c66affSColin Finck goto FailChannel;
535*c2c66affSColin Finck }
536*c2c66affSColin Finck
537*c2c66affSColin Finck /* Now call the channel specific type constructor */
538*c2c66affSColin Finck Status = Channel->ChannelCreate(Channel);
539*c2c66affSColin Finck if (!NT_SUCCESS(Status))
540*c2c66affSColin Finck {
541*c2c66affSColin Finck /* This is critical */
542*c2c66affSColin Finck SAC_DBG(SAC_DBG_INIT, "SAC Create Channel :: Failed channel specific initialization\n");
543*c2c66affSColin Finck goto FailChannel;
544*c2c66affSColin Finck }
545*c2c66affSColin Finck
546*c2c66affSColin Finck /* Finally, mark the channel as active */
547*c2c66affSColin Finck ChannelSetStatus(Channel, Active);
548*c2c66affSColin Finck return STATUS_SUCCESS;
549*c2c66affSColin Finck
550*c2c66affSColin Finck FailChannel:
551*c2c66affSColin Finck /* Destroy the channel and return the failure code */
552*c2c66affSColin Finck Channel->ChannelDestroy(Channel);
553*c2c66affSColin Finck return Status;
554*c2c66affSColin Finck }
555*c2c66affSColin Finck
556*c2c66affSColin Finck NTSTATUS
557*c2c66affSColin Finck NTAPI
ChannelClose(IN PSAC_CHANNEL Channel)558*c2c66affSColin Finck ChannelClose(IN PSAC_CHANNEL Channel)
559*c2c66affSColin Finck {
560*c2c66affSColin Finck NTSTATUS Status;
561*c2c66affSColin Finck CHECK_PARAMETER(Channel);
562*c2c66affSColin Finck
563*c2c66affSColin Finck /* Set the channel inactive */
564*c2c66affSColin Finck ChannelSetStatus(Channel, Inactive);
565*c2c66affSColin Finck
566*c2c66affSColin Finck /* Set the close event */
567*c2c66affSColin Finck if (Channel->Flags & SAC_CHANNEL_FLAG_CLOSE_EVENT)
568*c2c66affSColin Finck {
569*c2c66affSColin Finck ChannelSetEvent(Channel, CloseEvent);
570*c2c66affSColin Finck }
571*c2c66affSColin Finck
572*c2c66affSColin Finck /* Close all the handles */
573*c2c66affSColin Finck Status = ChannelDereferenceHandles(Channel);
574*c2c66affSColin Finck return Status;
575*c2c66affSColin Finck }
576