Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / channels / rdpdr / irp.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol client.
3  * File System Virtual Channel
4  *
5  * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  * Copyright 2010-2011 Vic Lee
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <freerdp/utils/memory.h>
26 #include <freerdp/utils/stream.h>
27 #include <freerdp/utils/svc_plugin.h>
28
29 #include "rdpdr_types.h"
30 #include "rdpdr_constants.h"
31 #include "devman.h"
32 #include "irp.h"
33
34 static void irp_free(IRP* irp)
35 {
36         DEBUG_SVC("DeviceId %d FileId %d CompletionId %d", irp->device->id, irp->FileId, irp->CompletionId);
37
38         stream_free(irp->input);
39         stream_free(irp->output);
40         xfree(irp);
41 }
42
43 static void irp_complete(IRP* irp)
44 {
45         int pos;
46
47         DEBUG_SVC("DeviceId %d FileId %d CompletionId %d", irp->device->id, irp->FileId, irp->CompletionId);
48
49         pos = stream_get_pos(irp->output);
50         stream_set_pos(irp->output, 12);
51         stream_write_uint32(irp->output, irp->IoStatus);
52         stream_set_pos(irp->output, pos);
53
54         svc_plugin_send(irp->devman->plugin, irp->output);
55         irp->output = NULL;
56
57         irp_free(irp);
58 }
59
60 IRP* irp_new(DEVMAN* devman, STREAM* data_in)
61 {
62         IRP* irp;
63         uint32 DeviceId;
64         DEVICE* device;
65
66         stream_read_uint32(data_in, DeviceId);
67         device = devman_get_device_by_id(devman, DeviceId);
68         if (device == NULL)
69         {
70                 DEBUG_WARN("unknown DeviceId %d", DeviceId);
71                 return NULL;
72         }
73
74         irp = xnew(IRP);
75         irp->device = device;
76         irp->devman = devman;
77         stream_read_uint32(data_in, irp->FileId);
78         stream_read_uint32(data_in, irp->CompletionId);
79         stream_read_uint32(data_in, irp->MajorFunction);
80         stream_read_uint32(data_in, irp->MinorFunction);
81         irp->input = data_in;
82
83         irp->output = stream_new(256);
84         stream_write_uint16(irp->output, RDPDR_CTYP_CORE);
85         stream_write_uint16(irp->output, PAKID_CORE_DEVICE_IOCOMPLETION);
86         stream_write_uint32(irp->output, DeviceId);
87         stream_write_uint32(irp->output, irp->CompletionId);
88         stream_seek_uint32(irp->output); /* IoStatus */
89
90         irp->Complete = irp_complete;
91         irp->Discard = irp_free;
92
93         DEBUG_SVC("DeviceId %d FileId %d CompletionId %d MajorFunction 0x%X MinorFunction 0x%x",
94                 irp->device->id, irp->FileId, irp->CompletionId, irp->MajorFunction, irp->MinorFunction);
95
96         return irp;
97 }