summaryrefslogtreecommitdiffstats
path: root/contexthubhal/nanohubhal.h
blob: 6a0432c8840782ddf5ccd44836a9a66a5f814799 (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
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _NANOHUB_HAL_H_
#define _NANOHUB_HAL_H_

#include <mutex>
#include <thread>
#include <list>

#include <hardware/context_hub.h>

#include <nanohub/nanohub.h>

//as per protocol
#define MAX_RX_PACKET               128
#define APP_FROM_HOST_EVENT_ID      0x000000F8
#define APP_FROM_HOST_CHRE_EVENT_ID 0x000000F9

namespace android {

namespace nanohub {

void dumpBuffer(const char *pfx, const hub_app_name_t &appId, uint32_t evtId, const void *data, size_t len, int status = 0);

struct nano_message_chre {
    HostMsgHdrChreV10 hdr;
    uint8_t data[MAX_RX_PACKET];
} __attribute__((packed));

struct nano_message {
    HostMsgHdr hdr;
    uint8_t data[MAX_RX_PACKET];
} __attribute__((packed));

class HubMessage : public hub_message_t {
    std::unique_ptr<uint8_t> data_;
public:
    HubMessage(const HubMessage &other) = delete;
    HubMessage &operator = (const HubMessage &other) = delete;

    HubMessage(const hub_app_name_t *name, uint32_t typ, const void *data, uint32_t len) {
        app_name = *name;
        message_type = typ;
        message_len = len;
        message = data;
        if (len > 0 && data != nullptr) {
            data_ = std::unique_ptr<uint8_t>(new uint8_t[len]);
            memcpy(data_.get(), data, len);
            message = data_.get();
        }
    }

    HubMessage(HubMessage &&other) {
        *this = (HubMessage &&)other;
    }

    HubMessage &operator = (HubMessage &&other) {
        *static_cast<hub_message_t *>(this) = static_cast<hub_message_t>(other);
        data_ = std::move(other.data_);
        other.message = nullptr;
        other.message_len = 0;
        return *this;
    }
};

class NanoHub {
    std::mutex mLock;
    bool mAppQuit;
    std::mutex mAppTxLock;
    std::condition_variable mAppTxCond;
    std::list<HubMessage> mAppTxQueue;
    std::thread mPollThread;
    std::thread mAppThread;
    context_hub_callback *mMsgCbkFunc;
    int mThreadClosingPipe[2];
    int mFd; // [0] is read end
    void * mMsgCbkData;

    NanoHub();
    ~NanoHub();

    void reset() {
        mThreadClosingPipe[0] = -1;
        mThreadClosingPipe[1] = -1;
        mFd = -1;
        mMsgCbkData = nullptr;
        mMsgCbkFunc = nullptr;
        mAppQuit = false;
    }

    void* runAppTx();
    void* runDeviceRx();

    int openHub();
    int closeHub();

    static NanoHub *hubInstance() {
        static NanoHub theHub;
        return &theHub;
    }

    int doSubscribeMessages(uint32_t hub_id, context_hub_callback *cbk, void *cookie);
    int doSendToNanohub(uint32_t hub_id, const hub_message_t *msg);
    int doSendToDevice(const hub_app_name_t name, const void *data, uint32_t len, uint32_t messageType = 0);
    void doSendToApp(HubMessage &&msg);

    static constexpr unsigned int FL_MESSAGE_TRACING = 1;

    unsigned int mFlags = 0;

public:

    // debugging interface

    static bool messageTracingEnabled() {
        return hubInstance()->mFlags & FL_MESSAGE_TRACING;
    }
    static unsigned int getDebugFlags() {
        return hubInstance()->mFlags;
    }
    static void setDebugFlags(unsigned int flags) {
        hubInstance()->mFlags = flags;
    }

    // messaging interface

    // define callback to invoke for APP messages
    static int subscribeMessages(uint32_t hub_id, context_hub_callback *cbk, void *cookie) {
        return hubInstance()->doSubscribeMessages(hub_id, cbk, cookie);
    }
    // all messages from APP go here
    static int sendToNanohub(uint32_t hub_id, const hub_message_t *msg) {
        return hubInstance()->doSendToNanohub(hub_id, msg);
    }
    // passes message to kernel driver directly
    static int sendToDevice(const hub_app_name_t *name, const void *data, uint32_t len) {
        return hubInstance()->doSendToDevice(*name, data, len);
    }
    // passes message to APP via callback
    static void sendToApp(HubMessage &&msg) {
        hubInstance()->doSendToApp((HubMessage &&)msg);
    }
};

}; // namespace nanohub

}; // namespace android

#endif