~aleteoryx/muditaos

ref: 91d0fc873a77343947103a7ba0146da2616b0292 muditaos/module-bsp/board/linux/cellular/linux_cellular.cpp -rw-r--r-- 7.0 KiB
91d0fc87 — Maciej Janicki [EGD-5748] Revert remake cellular flow 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "linux_cellular.hpp"

#include <iostream>
#include <algorithm>

#include "log/log.hpp"
#include "mutex.hpp"
#include <common_data/EventStore.hpp>
#include <errno.h>
#include <fcntl.h>
#include <map>
#include <sys/stat.h>
#include <sys/types.h>
#include <ticks.hpp>
#include <time.h>
#include <unistd.h>

#define _LINUX_UART_DEBUG 0

namespace bsp
{

    std::map<uint32_t, speed_t> PortSpeeds_text = {{9600U, B9600},
                                                   {19200U, B19200},
                                                   {38400U, B38400},
                                                   {57600U, B57600},
                                                   {115200U, B115200},
                                                   {230400U, B230400},
                                                   {460800U, B460800}};

    LinuxCellular::LinuxCellular(const char *term, uint32_t portSpeed)
    {

        if (strcmp(term, "0") == 0) {
            fd = 0;
        }
        else {
            // open serial port
            fd = open(term, O_RDWR | O_NOCTTY | O_NONBLOCK);
            if (fd == -1) {
                LOG_FATAL("Failed to open serial port: %s (%d)", term, portSpeed);
                return;
            }

            struct termios t;
            memset(&t, 0, sizeof(t));
            tcgetattr(fd, &t);
            cfmakeraw(&t);
            t.c_cflag |= CLOCAL;
            // if(ctsrts == 1)
            //  t.c_cflag |= CRTSCTS; //enable the flow control on dev board
            // else
            t.c_cflag &= ~(CRTSCTS);                    // disable the flow control on dev board
            speed_t speed = PortSpeeds_text[portSpeed]; // B115200;
            cfsetispeed(&t, speed);
            cfsetospeed(&t, speed);
            tcsetattr(fd, TCSANOW, &t);
            int status = TIOCM_DTR | TIOCM_RTS;
            ioctl(fd, TIOCMBIS, &status);
        }

        epoll_fd = epoll_create1(0);

        if (epoll_fd == -1) {
            LOG_FATAL("Failed to create epoll file descriptor");
        }

        struct epoll_event event;

        event.events = EPOLLIN;

        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event)) {
            LOG_FATAL("Failed to add file descriptor to epoll: errno:%d", errno);
        }

        isInitialized = true;
    }

    LinuxCellular::~LinuxCellular()
    {

        if (fd >= 0) {
            close(fd);
        }

        if (epoll_fd >= 0) {
            close(epoll_fd);
        }
    }

    void LinuxCellular::PowerUp()
    {}

    void LinuxCellular::PowerDown()
    {}

    void LinuxCellular::Restart()
    {}

    ssize_t LinuxCellular::Read(void *buf, size_t nbytes)
    {

        cpp_freertos::LockGuard lock(serOutMutex);

        int ret;
        for (;;) {
            ret = read(fd, buf, nbytes);
            if (ret != -1 || errno != EINTR)
                break;
        }

#if _LINUX_UART_DEBUG
        if (ret > 0) {
            LOG_PRINTF("[RX] ");
            uint8_t *ptr = (uint8_t *)buf;
            for (size_t i = 0; i < ret; i++)
                LOG_PRINTF("%02X ", (uint8_t)*ptr++);
            LOG_PRINTF("\n");
        }
#endif
        return ret;
    }

    ssize_t LinuxCellular::Write(void *buf, size_t nbytes)
    {
        cpp_freertos::LockGuard lock(serOutMutex);
#if _LINUX_UART_DEBUG
        LOG_PRINTF("[TX] ");
        uint8_t *ptr = (uint8_t *)buf;
        for (size_t i = 0; i < nbytes; i++)
            LOG_PRINTF("%02X ", (uint8_t)*ptr++);
        LOG_PRINTF("\n");
#endif
        int ret;
        for (;;) {
            ret = write(fd, buf, nbytes);
            if (ret != -1 || errno != EINTR)
                break;
        }
        return ret;
    }

    void LinuxCellular::InformModemHostAsleep(void)
    {}

    void LinuxCellular::InformModemHostWakeup(void)
    {}

    void LinuxCellular::EnterSleep()
    {}

    void LinuxCellular::ExitSleep()
    {}

    uint32_t LinuxCellular::Wait(uint32_t timeout)
    {

        uint32_t currentTime   = cpp_freertos::Ticks::GetTicks();
        uint32_t timeoutNeeded = timeout == UINT32_MAX ? UINT32_MAX : currentTime + timeout;
        uint32_t timeElapsed   = currentTime;

        for (;;) {
            if (timeElapsed >= timeoutNeeded) {
                return 0; // timeout
            }

            auto event_count = epoll_wait(epoll_fd, events, MAX_EVENTS, timeoutNeeded - timeElapsed);
            timeElapsed      = cpp_freertos::Ticks::GetTicks();

            if (event_count == 0) {
                return 0; // timeout
            }
            else if ((event_count == -1) && (errno == EINTR)) {
                continue;
            }
            else {
                return 1;
            }
        }
    }

    void LinuxCellular::SetSpeed(uint32_t portSpeed)
    {
        struct termios t;
        memset(&t, 0, sizeof(t));
        tcgetattr(fd, &t);
        cfmakeraw(&t);
        t.c_cflag |= CLOCAL;
        // if(ctsrts == 1)
        //  t.c_cflag |= CRTSCTS; //enable the flow control on dev board
        // else
        t.c_cflag &= ~(CRTSCTS);                    // disable the flow control on dev board
        speed_t speed = PortSpeeds_text[portSpeed]; // B115200;
        cfsetispeed(&t, speed);
        cfsetospeed(&t, speed);
        tcsetattr(fd, TCSANOW, &t);
        int status = TIOCM_DTR | TIOCM_RTS;
        ioctl(fd, TIOCMBIS, &status);
    }

    void LinuxCellular::SelectAntenna(bsp::cellular::antenna antenna)
    {}

    bsp::cellular::antenna LinuxCellular::GetAntenna()
    {
        return bsp::cellular::antenna::lowBand;
    }

    namespace cellular
    {
        auto init(QueueHandle_t qHandle) -> int
        {
            return 0;
        }

        namespace USB
        {
            BootPinState getBootPin()
            {
                return BootPinState::NORMAL_BOOT;
            }

            PassthroughState getPassthrough()
            {
                return PassthroughState::DISABLED;
            }

            void setBootPin(BootPinState dfu){};
            void setPassthrough(PassthroughState pass){};
        } // namespace USB

        namespace status
        {

            bsp::cellular::status::value getStatus()
            {
                return bsp::cellular::status::value::ACTIVE;
            }

            BaseType_t statusIRQhandler()
            {
                return pdFALSE;
            }
        } // namespace status
        namespace sim
        {

            auto trayIRQ_handler() -> BaseType_t
            {
                return BaseType_t();
            }

            auto getTray() -> Store::GSM::Tray
            {
                return Store::GSM::Tray::IN;
            }

            void hotswap_trigger()
            {}

            void sim_sel()
            {}
        } // namespace sim
        namespace ringIndicator
        {
            auto riIRQ_handler() -> BaseType_t
            {
                return BaseType_t();
            }
        } // namespace ringIndicator
    }     // namespace cellular
} // namespace bsp