~aleteoryx/muditaos

ref: 214451ccf152a8262d9925b84bc97cd14e7c5c6e muditaos/module-services/service-lwip/ServiceLwIP.cpp -rw-r--r-- 3.3 KiB
214451cc — Jakub Pyszczak [EGD-7083] Fixed earspeaker bt HSP 4 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
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "service-lwip/ServiceLwIP.hpp"

#include <MessageType.hpp>
#include <Service/Message.hpp>
#include <Service/Service.hpp>
#include <log.hpp>

extern "C"
{
#include "dhcp-server/dhserver.h" // for dhserv_init, dhcp_config_t, dhcp_entry_t
#include "lwip/apps/httpd.h"      // for httpd_init
#include "lwip/tcpip.h"           // for tcpip_init
};

#include <bits/exception.h>
#include <cstddef>
#include <memory>
#include <string>
#include <utility>

#define NUM_DHCP_ENTRY 3

extern "C"
{
    static dhcp_entry_t entries[NUM_DHCP_ENTRY] = {
        /* mac    ip address        subnet mask        lease time */
        {{0}, {192, 168, 7, 2}, {255, 255, 255, 0}, 24 * 60 * 60},
        {{0}, {192, 168, 7, 3}, {255, 255, 255, 0}, 24 * 60 * 60},
        {{0}, {192, 168, 7, 4}, {255, 255, 255, 0}, 24 * 60 * 60}};

    static dhcp_config_t dhcp_config = {
        {192, 168, 7, 1},
        67,             /* server address, port */
        {0, 0, 0, 0},   /* dns server */
        NULL,           /* dns suffix */
        NUM_DHCP_ENTRY, /* num entry */
        entries         /* entries */
    };
};

sys::ReturnCodes message_lwip(sys::Service *app, LwIP_message::Request req)
{
    std::shared_ptr<LwIP_message> msg = std::make_shared<LwIP_message>(req);
    auto ret                          = app->bus.sendUnicastSync(msg, "ServiceLwIP", 5000);
    if (ret.first != sys::ReturnCodes::Success) {
        LOG_ERROR("err: %s", c_str(ret.first));
    }
    return ret.first;
}

ServiceLwIP::ServiceLwIP() : sys::Service(service::name::lwip, "", StackDepth)
{
    LOG_INFO("[ServiceLwIP] Initializing");
    tcpip_init(nullptr, nullptr);
    // lwip_init();
    dhserv_init(&dhcp_config);
    httpd_init();
}

sys::ReturnCodes ServiceLwIP::InitHandler()
{
    LOG_ERROR("LwIP experimental!");
    return sys::ReturnCodes::Success;
}

sys::ReturnCodes ServiceLwIP::DeinitHandler()
{
    LOG_ERROR("TODO");
    return sys::ReturnCodes::Success;
}

void ServiceLwIP::ProcessCloseReason(sys::CloseReason closeReason)
{
    sendCloseReadyMessage(this);
}

sys::MessagePointer ServiceLwIP::DataReceivedHandler(sys::DataMessage *msg, sys::ResponseMessage *resp)
{
    LOG_ERROR("TRY START LWIP");
    try {
        switch (static_cast<MessageType>(msg->messageType)) {
        case MessageType::LwIP_request: {
            LwIP_message *lmsg = dynamic_cast<LwIP_message *>(msg);
            switch (lmsg->req) {
            case LwIP_message::Request::Start:
                LOG_ERROR("START LWIP");
                tcpip_init(nullptr, nullptr);
                // lwip_init();
                dhserv_init(&dhcp_config);
                httpd_init();
                LOG_INFO("LwIP started!");
                break;
            default:
                LOG_ERROR("Not implemented: %d", lmsg->req);
            }
        } break;
        default:
            LOG_ERROR("Not handled messageType: %d", static_cast<int>(msg->messageType));
        }
    }
    catch (std::exception &ex) {
        LOG_ERROR("Exception on ServiceLwIP!: %s", ex.what());
    }
    return std::make_shared<sys::ResponseMessage>();
}

sys::ReturnCodes ServiceLwIP::SwitchPowerModeHandler(const sys::ServicePowerMode mode)
{
    LOG_ERROR("TODO");
    return sys::ReturnCodes::Success;
}