summaryrefslogtreecommitdiff
path: root/CDAL/AsyncCommunication.h
blob: 36ce5c779d4eb7eaaacb0dfea1ebaaa9ecdc1e51 (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
/*
 * AsyncCommunication.h
 *
 * Copyright (C) ST-Ericsson SA 2011
 * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
 *          Ola Borgelin <ola.borgelin@stericsson.com>,
 *          Karin Hedlund <karin.hedlund@stericsson.com>,
 *          Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
 * License terms: 3-clause BSD license
 *
 */

#pragma once

#include "CDAL.h"
#include "Event.h"
#include "Thread.h"
#include "CriticalSection.h"

struct commRequest {
    void* buf;
    int length;
    DataCallback_t callback;
    void* param;
    bool isShutdown;
    bool isActive;
};

class AsyncCommunication
{
public:
    AsyncCommunication(CommDevice* device);
    ~AsyncCommunication();

    int readNoWait(void* buf, int len, DataCallback_t fn, void* param);
    int writeNoWait(void* buf, int len, DataCallback_t fn, void* param);

    bool isReadActive() {
        return readRequest_.isActive;
    }
    void waitReadCanceled();
private:
    CommDevice* device_;
    bool isReadInvoked_;
    bool isWriteInvoked_;

    commRequest readRequest_;
    CriticalSection readCs_;

    commRequest writeRequest_;
    CriticalSection writeCs_;

    Thread* readerThread_;
    static void* threadReadProc(void*);
    Event readRequestEvent_;
    Event readCanceledEvent_;

    Thread* writerThread_;
    static void* threadWriteProc(void*);
    Event writeRequestEvent_;
};