summaryrefslogtreecommitdiff
path: root/network/modem/fakemodem/readwrit.c
blob: af6da93732f5c2cb255a66cdd3cdad4545e82378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

    ioctl.c

Abstract:

    This is a simple form of function driver for Fm device. The driver
    doesn't handle any PnP and Power events because the framework provides
    default behaviour for those events. This driver has enough support to
    allow an user application (toast/notify.exe) to open the device
    interface registered by the driver and send read, write or ioctl requests.

Environment:

    Kernel mode

--*/

#include "fakemodem.h"


VOID
FmEvtIoRead(
    IN WDFQUEUE     Queue,
    IN WDFREQUEST   Request,
    IN size_t         Length
    )
/*++
Routine Description:

    This event is called when the framework receives IRP_MJ_READ
    requests from the system.
    This routines defers read for later processing if there is no data to read.

Arguments:

    Queue - Handle to the framework queue object that is associated
            with the I/O request.
    Request - Handle to a framework request object.

    Length - Length of the IO operation
                 The default property of the queue is to not dispatch
                 zero lenght read & write requests to the driver and
                 complete is with status success. So we will never get
                 a zero length request.

Return Value:

   VOID

--*/
{
    PFM_DEVICE_DATA    fmDeviceData = FmDeviceDataGet(WdfIoQueueGetDevice(Queue));
    ULONG              information;
    NTSTATUS           status;
    PUCHAR             systemBuffer;
    size_t             bufLen;

    status = WdfRequestRetrieveOutputBuffer(Request, Length, &systemBuffer, &bufLen);
    if (!NT_SUCCESS(status)) {
        WdfRequestComplete(Request, status);
        return;
    }

    if (fmDeviceData->BytesInReadBuffer > 0) {

        ProcessReadBuffer(fmDeviceData,
                          systemBuffer,
                          (ULONG) Length,
                          &information);

        WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, information);

        return;

    } else {

        //
        // No data to read. Queue the request for later processing.
        //

        status = WdfRequestForwardToIoQueue(Request, fmDeviceData->FmReadQueue);
        if (!NT_SUCCESS(status)) {
            WdfRequestCompleteWithInformation(Request, status, 0);
            return;
        }
    }
}

VOID
FmEvtIoWrite(
    IN WDFQUEUE     Queue,
    IN WDFREQUEST   Request,
    IN size_t        Length
    )
/*++
Routine Description:

    This event is called when the framework receives IRP_MJ_WRITE
    requests from the system.
    This routine will also drain the read queue if there are pending reads.

Arguments:

    Queue - Handle to the framework queue object that is associated
            with the I/O request.
    Request - Handle to a framework request object.

    Length - Length of the IO operation
                 The default property of the queue is to not dispatch
                 zero lenght read & write requests to the driver and
                 complete is with status success. So we will never get
                 a zero length request.

Return Value:

   VOID

--*/
{
    PFM_DEVICE_DATA    fmDeviceData = FmDeviceDataGet(WdfIoQueueGetDevice(Queue));
    PUCHAR             systemBuffer;
    NTSTATUS           status;
    size_t             length;
    WDFREQUEST         readRequest;

    status = WdfRequestRetrieveInputBuffer(Request, Length, &systemBuffer, &length);
    if (!NT_SUCCESS(status)) {
        WdfRequestComplete(Request, status);
        return;
    }

    ProcessWriteBytes( fmDeviceData, systemBuffer, (ULONG) Length);

    //
    // Process read requests and complete them here.
    //

    while (fmDeviceData->BytesInReadBuffer > 0) {
        ULONG bytesToMove = 0;

        status = WdfIoQueueRetrieveNextRequest(fmDeviceData->FmReadQueue, &readRequest);
        if (!NT_SUCCESS(status)) {
            break;
        }

        status = WdfRequestRetrieveOutputBuffer(readRequest, 0, &systemBuffer, &length);
        if (NT_SUCCESS(status)) {
            ProcessReadBuffer(fmDeviceData,
                              systemBuffer,
                              (ULONG) length,
                              &bytesToMove);
        }
        WdfRequestCompleteWithInformation(readRequest, status, bytesToMove);
    }

    ProcessConnectionStateChange( fmDeviceData);

    WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, Length);
}

VOID
ProcessWriteBytes(
    PFM_DEVICE_DATA   FmDeviceData,
    PUCHAR            Characters,
    ULONG             Length
    )
/*++
Routine Description:

    This function is called when the framework receives IRP_MJ_WRITE
    requests from the system. The write event handler(FmEvtIoWrite) calls ProcessWriteBytes.
    It parses the Characters passed in and looks for the  for sequences "AT" -ok  ,
    "ATA" --CONNECT, ATD<number> -- CONNECT and sets the state of the device appropriately.
    These bytes are placed in the read Buffer to be processed later since this device
    works in a loopback fashion.


Arguments:

    FmDeviceData - Handle to the framework queue object that is associated
            with the I/O request.
    Characters - Pointer to the write IRP's system buffer.

    Length - Length of the IO operation
                 The default property of the queue is to not dispatch
                 zero lenght read & write requests to the driver and
                 complete is with status success. So we will never get
                 a zero length request.

Return Value:

   VOID

--*/

{

    UCHAR               currentCharacter;

    while (Length != 0) {

        currentCharacter=*Characters++;
        Length--;

        if(currentCharacter == '\0')
        {
            continue;
        }

        PutCharInReadBuffer( FmDeviceData, currentCharacter);

        switch (FmDeviceData->CommandMatchState) {

            case COMMAND_MATCH_STATE_IDLE:

                if ((currentCharacter == 'a') || (currentCharacter == 'A')) {
                    //  got an A
                    FmDeviceData->CommandMatchState=COMMAND_MATCH_STATE_GOT_A;

                    FmDeviceData->ConnectCommand=FALSE;

                    FmDeviceData->IgnoreNextChar=FALSE;

                }

            break;

            case COMMAND_MATCH_STATE_GOT_A:

                if ((currentCharacter == 't') || (currentCharacter == 'T')) {
                    //  got an T
                    FmDeviceData->CommandMatchState=COMMAND_MATCH_STATE_GOT_T;

                } else {

                    if (currentCharacter == '\r') {

                        FmDeviceData->CommandMatchState=COMMAND_MATCH_STATE_IDLE;
                    }
                }

            break;

            case COMMAND_MATCH_STATE_GOT_T:

                if (!FmDeviceData->IgnoreNextChar) {
                    //  the last char was not a special char
                    //  check for CONNECT command
                    if ((currentCharacter == 'A') || (currentCharacter == 'a')) {

                        FmDeviceData->ConnectCommand=TRUE;
                    }

                    if ((currentCharacter == 'D') || (currentCharacter == 'd')) {

                        FmDeviceData->ConnectCommand=TRUE;
                    }
                }

                FmDeviceData->IgnoreNextChar=TRUE;

                if (currentCharacter == '\r') {
                    //
                    //  got a CR, send a response to the command
                    //
                    FmDeviceData->CommandMatchState=COMMAND_MATCH_STATE_IDLE;

                    if (FmDeviceData->ConnectCommand) {
                        //
                        //  place <cr><lf>CONNECT<cr><lf>  in the buffer
                        //
                        PutCharInReadBuffer(FmDeviceData,'\r');
                        PutCharInReadBuffer(FmDeviceData,'\n');

                        PutCharInReadBuffer(FmDeviceData,'C');
                        PutCharInReadBuffer(FmDeviceData,'O');
                        PutCharInReadBuffer(FmDeviceData,'N');
                        PutCharInReadBuffer(FmDeviceData,'N');
                        PutCharInReadBuffer(FmDeviceData,'E');
                        PutCharInReadBuffer(FmDeviceData,'C');
                        PutCharInReadBuffer(FmDeviceData,'T');

                        PutCharInReadBuffer(FmDeviceData,'\r');
                        PutCharInReadBuffer(FmDeviceData,'\n');

                        //
                        //  connected now raise CD
                        //
                        FmDeviceData->CurrentlyConnected=TRUE;

                        FmDeviceData->ConnectionStateChanged=TRUE;

                    } else {

                        //  place <cr><lf>OK<cr><lf>  in the buffer

                        PutCharInReadBuffer(FmDeviceData,'\r');
                        PutCharInReadBuffer(FmDeviceData,'\n');
                        PutCharInReadBuffer(FmDeviceData,'O');
                        PutCharInReadBuffer(FmDeviceData,'K');
                        PutCharInReadBuffer(FmDeviceData,'\r');
                        PutCharInReadBuffer(FmDeviceData,'\n');
                    }
                }


            break;

            default:

            break;

        }
    }

    return;

}

VOID
PutCharInReadBuffer(
    PFM_DEVICE_DATA   FmDeviceData,
    UCHAR             Character
    )
/*++
Routine Description:

    This routine puts the charcter into the circular read buffer checking for overflows while doing it.

Arguments:

    FmDeviceData - Handle to the framework queue object that is associated
            with the I/O request.
    Characters - Handle to a framework request object.


Return Value:

   VOID

--*/

{

    if (FmDeviceData->BytesInReadBuffer < READ_BUFFER_SIZE) {

        //  room in buffer
        FmDeviceData->ReadBuffer[FmDeviceData->ReadBufferEnd]=Character;
        FmDeviceData->ReadBufferEnd++;
        FmDeviceData->ReadBufferEnd %= READ_BUFFER_SIZE;
        FmDeviceData->BytesInReadBuffer++;

    }

    return;

}

VOID
ProcessReadBuffer(
    IN  PFM_DEVICE_DATA FmDeviceData,
    IN  PUCHAR  SystemBuffer,
    IN  ULONG   Length,
    OUT PULONG  BytesToMove
    )
/*++
Routine Description:

     This event is called when the framework receives IRP_MJ_READ
    requests from the system. It is called by the read event handler.
    It copies data from the IRp's system buffer to the device read buffer.
    if the size of data from the Irp's system buffer is greater than the size of the
    read buffer the number of bytes remaining is passed back in  BytesToMove.


Arguments:

    FmDeviceData - Handle to the framework queue object that is associated
            with the I/O request.
    SystemBuffer -   The buffer passed in the IRP which contains the read data.

    Length -  Length of data in the systembuffer
    BytesToMove -  Remaining bytes not copied into the read buffer.
Return Value:

   VOID

--*/
{
    ULONG              firstHalf;
    ULONG              secondHalf;
    ULONG              bytesToMove;
    NTSTATUS           status;
    ULONG              safeBoundsCheck;

    //
    //  there is an IRP and there are characters waiting
    //


    bytesToMove = (Length < FmDeviceData->BytesInReadBuffer) ? Length
                                    : FmDeviceData->BytesInReadBuffer;

    status =   RtlULongAdd (FmDeviceData->ReadBufferBegin,
                            bytesToMove,
                            &safeBoundsCheck);

    if (!NT_SUCCESS(status)) {
        return;
    }

    if (safeBoundsCheck > READ_BUFFER_SIZE) {

        //
        //  the buffer is wrapped around, have move in two pieces
        //

        firstHalf = READ_BUFFER_SIZE - FmDeviceData->ReadBufferBegin;

        secondHalf= bytesToMove - firstHalf;

        RtlCopyMemory(
            SystemBuffer,
            &FmDeviceData->ReadBuffer[FmDeviceData->ReadBufferBegin],
            firstHalf);

        RtlCopyMemory(
            (SystemBuffer + firstHalf),
            &FmDeviceData->ReadBuffer[0],
            secondHalf);

    } else {

        //
        //  can do it all at once
        //

        RtlCopyMemory(
            SystemBuffer,
            &FmDeviceData->ReadBuffer[FmDeviceData->ReadBufferBegin],
            bytesToMove);
    }

    //
    //  fix up queue pointers
    //
    FmDeviceData->BytesInReadBuffer -= bytesToMove;

    FmDeviceData->ReadBufferBegin += bytesToMove;

    FmDeviceData->ReadBufferBegin %= READ_BUFFER_SIZE;

    *BytesToMove = bytesToMove;
}