aboutsummaryrefslogtreecommitdiff
path: root/applications/message_handler_openamp/message_handler.hpp
blob: 94615696cd71777b994d99cd411eac095c1eeccc (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
/*
 * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
 * SPDX-License-Identifier: Apache-2.0
 *
 * 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
 *
 * 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.
 */

#pragma once

/*****************************************************************************
 * Includes
 *****************************************************************************/

#include <mailbox.hpp>
#include <rpmsg/ethosu_rpmsg.h>

#include "queue.hpp"
#include "remoteproc.hpp"

/*****************************************************************************
 * Messages
 *****************************************************************************/

struct Message {
    Message() {}

    Message(const uint32_t _src,
            const EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX,
            const uint64_t msgId                     = 0,
            const uint32_t _length                   = 0) :
        src(_src),
        length(_length) {
        rpmsg.header.magic  = ETHOSU_CORE_MSG_MAGIC;
        rpmsg.header.type   = _type;
        rpmsg.header.msg_id = msgId;
    }

    uint32_t src    = 0;
    uint32_t length = 0;
    EthosU::ethosu_core_rpmsg rpmsg;
};

/*****************************************************************************
 * MessageHandler
 *****************************************************************************/

class MessageHandler : public Rpmsg {
public:
    using InferenceQueue = Queue<Message *>;
    using ResponseQueue  = Queue<Message *>;

    MessageHandler(RProc &rproc, const char *const name);
    virtual ~MessageHandler();

    InferenceQueue &getInferenceQueue() {
        return inferenceQueue;
    }

    InferenceQueue &getResponseQueue() {
        return responseQueue;
    }

protected:
    // Handle incoming rpmsg
    int handleMessage(void *data, size_t len, uint32_t src) override;

    // Outgoing messages
    void sendError(const uint32_t src, const EthosU::ethosu_core_err_type type, const char *message);
    void sendPong(const uint32_t src, const uint64_t msgId);
    void sendVersionRsp(const uint32_t src, const uint64_t msgId);
    void sendCapabilitiesRsp(const uint32_t src, const uint64_t msgId);
    void sendNetworkInfoRsp(const uint32_t src, const uint64_t msgId, EthosU::ethosu_core_network_buffer &network);
    void forwardInferenceReq(const uint32_t src,
                             const uint64_t msgId,
                             const EthosU::ethosu_core_msg_inference_req &inference);
    void sendInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_core_status status);
    void sendCancelInferenceRsp(const uint32_t src, const uint64_t msgId, const EthosU::ethosu_core_status status);

    EthosU::ethosu_core_msg_capabilities_rsp getCapabilities() const;
    bool getNetwork(EthosU::ethosu_core_network_buffer &buffer);

    // Tasks returning response messages
    static void responseTask(void *param);

private:
    InferenceQueue inferenceQueue;
    ResponseQueue responseQueue;
    EthosU::ethosu_core_msg_capabilities_rsp capabilities;

    // FreeRTOS
    TaskHandle_t taskHandle;
};