~aleteoryx/muditaos

ref: b7e1a04a125e28761d71d12124a1474cd8b2b4f1 muditaos/module-services/service-desktop/parser/MessageHandler.cpp -rw-r--r-- 1.8 KiB
b7e1a04a — Hubert Chrzaniuk [EGD-4301] Bluetooth Audio profile support (#984) 5 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "MessageHandler.hpp"

#include <bits/exception.h> // for exception
#include <inttypes.h>       // for PRIu32
#include <memory>           // for operator!=, unique_ptr

#include "Context.hpp"         // for Context
#include "EndpointFactory.hpp" // for EndpointFactory
#include "log/log.hpp"         // for LOG_ERROR, LOG_DEBUG
#include "Endpoint.hpp"        // for Endpoint
#include "FreeRTOS.h"          // for xQueueHandle

namespace sys
{
    class Service;
} // namespace sys

using namespace parserFSM;

xQueueHandle MessageHandler::sendQueue;

MessageHandler::MessageHandler(std::string &message, sys::Service *OwnerService) : OwnerServicePtr(OwnerService)
{
    try {
        messageJson = json11::Json::parse(message, JsonErrorMsg);
    }
    catch (const std::exception &e) {
        LOG_ERROR("Cannot create MessageHandler! err:%s", e.what());
    }
}

void MessageHandler::processMessage()
{
    Context context(messageJson);

    LOG_DEBUG("[MsgHandler]\nmethod: %d\nendpoint: %d\nuuid: %" PRIu32 "\nbody: %s\n",
              static_cast<int>(context.getMethod()),
              static_cast<int>(context.getEndpoint()),
              context.getUuid(),
              context.getBody().dump().c_str());

    auto handler = EndpointFactory::create(context, OwnerServicePtr);

    if (handler != nullptr) {
        handler->handle(context);
    }
    else {
        LOG_ERROR("No way to handle!");
    }
}

void MessageHandler::putToSendQueue(const std::string msg)
{
#if defined(TARGET_RT1051)
    if (uxQueueSpacesAvailable(sendQueue) != 0) {
        auto *responseString = new std::string(msg);
        xQueueSend(sendQueue, &responseString, portMAX_DELAY);
    }
#endif
}