~aleteoryx/muditaos

ref: 205fedb9a691850bf0bc32ac85b5635ef0205183 muditaos/module-bsp/board/linux/usb_cdc/usb_cdc.cpp -rw-r--r-- 6.8 KiB
205fedb9 — Mateusz Piesta [BH-1595] USB reset procedure 2 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "bsp/usb/usb.hpp"
#include <termios.h>
#include <fcntl.h>
#include <fstream>
#include <string>
#include <sys/inotify.h>
#include <array>
#include <limits.h>

#ifndef DEUBG_USB
#undef LOG_PRINTF
#undef LOG_TRACE
#undef LOG_DEBUG
#undef LOG_INFO
#undef LOG_WARN
#undef LOG_ERROR
#undef LOG_FATAL
#undef LOG_CUSTOM

#define LOG_PRINTF(...)
#define LOG_TRACE(...)
#define LOG_DEBUG(...)
#define LOG_INFO(...)
#define LOG_WARN(...)
#define LOG_ERROR(...)
#define LOG_FATAL(...)
#define LOG_CUSTOM(loggerLevel, ...)
#endif
namespace
{
    xTaskHandle taskHandleReceive;
}
namespace bsp
{
    int fd;
    int fdNofity;
    xQueueHandle USBReceiveQueue;
    xQueueHandle USBIrqQueue;
    constexpr auto ptsFileName = "/tmp/purephone_pts_name";
    char *pts_name             = NULL;

#if USBCDC_ECHO_ENABLED
    bool usbCdcEchoEnabled = false;

    constexpr std::string_view usbCDCEchoOnCmd("UsbCdcEcho=ON");
    constexpr std::string_view usbCDCEchoOffCmd("UsbCdcEcho=OFF");

    constexpr auto usbCDCEchoOnCmdLength  = usbCDCEchoOnCmd.length();
    constexpr auto usbCDCEchoOffCmdLength = usbCDCEchoOffCmd.length();
#endif

    void usbDeviceTask(void *ptr)
    {
        usbCDCReceive(ptr);
    }

    void checkUsbStatus()
    {
        char eventsBuff[((sizeof(inotify_event) + NAME_MAX + 1))];
        int len = read(fdNofity, eventsBuff, ((sizeof(inotify_event) + NAME_MAX + 1)));
        if (len > 0) {
            const inotify_event *event = (inotify_event *)&eventsBuff[0];
            if (event->mask & IN_OPEN) {
                USBDeviceStatus notification = USBDeviceStatus::Configured;
                xQueueSend(USBIrqQueue, &notification, 0);
            }
            if (event->mask & IN_CLOSE_WRITE) {
                USBDeviceStatus notification = USBDeviceStatus::Disconnected;
                xQueueSend(USBIrqQueue, &notification, 0);
            }
        }
    }

    int usbCDCReceive(void *)
    {
        LOG_INFO("[ServiceDesktop:BSP_Driver] Start reading on fd:%d", fd);
        char inputData[SERIAL_BUFFER_LEN];

        while (1) {

            checkUsbStatus();

            if (uxQueueSpacesAvailable(USBReceiveQueue) != 0) {
                memset(inputData, 0, SERIAL_BUFFER_LEN);

                ssize_t length = read(fd, &inputData[0], SERIAL_BUFFER_LEN);
                if (length > 0) {
                    LOG_DEBUG("[ServiceDesktop:BSP_Driver] Received: %d signs", static_cast<int>(length));
#if USBCDC_ECHO_ENABLED
                    bool usbCdcEchoEnabledPrev = usbCdcEchoEnabled;

                    auto usbEchoCmd = std::string_view{inputData, static_cast<size_t>(length)};

                    if ((length == usbCDCEchoOnCmdLength) && (usbCDCEchoOnCmd == usbEchoCmd)) {
                        usbCdcEchoEnabled = true;
                    }
                    else if ((length == usbCDCEchoOffCmdLength) && (usbCDCEchoOffCmd == usbEchoCmd)) {
                        usbCdcEchoEnabled = false;
                    }

                    if (usbCdcEchoEnabled || usbCdcEchoEnabledPrev) {
                        usbCDCSendRaw(inputData, length);
                        LOG_DEBUG(
                            "[ServiceDesktop:BSP_Driver] Echoed: %d signs: [%s]", static_cast<int>(length), inputData);
                        continue;
                    }
#endif
                    std::string *receiveMsg = new std::string(inputData, inputData + length);
                    xQueueSend(USBReceiveQueue, &receiveMsg, portMAX_DELAY);
                }
                else {
                    // yielding task because nothing in a buffer
                    vTaskDelay(10);
                }
            }
            else {
                LOG_DEBUG("[ServiceDesktop:BSP_Driver] USB receive Queue is full, yielding task");
                vTaskDelay(1000);
            }
        }
    }

    int usbCDCSend(std::string *sendMsg)
    {
        return usbCDCSendRaw(sendMsg->c_str(), sendMsg->length());
    }

    int usbCDCSendRaw(const char *dataPtr, size_t dataLen)
    {
        ssize_t t = write(fd, dataPtr, dataLen);
        if (t >= 0) {
            LOG_DEBUG("[ServiceDesktop:BSP_Driver] Sent: %d", static_cast<int>(t));
            return 0;
        }
        else {
            LOG_ERROR("[ServiceDesktop:BSP_Driver] Writing to PTY failed with code: %d", errno);
            return -1;
        }
    }

    void writePtsToFile(const char *pts_name)
    {
        std::ofstream ptsNameFile;
        ptsNameFile.open(ptsFileName, std::ios::out | std::ios::trunc);
        ptsNameFile << pts_name;
    }

    void usbDeinit()
    {
        LOG_INFO("usbDeinit removing file %s", ptsFileName);
        std::remove(ptsFileName);
        if (taskHandleReceive)
            vTaskDelete(taskHandleReceive);
    }

    int usbInit(const bsp::usbInitParams &initParams)
    {

        fd = 0;
        fd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
        if (fd == -1) {
            LOG_ERROR("bsp::usbInit Failed to open /dev/ptmx, can't allocate new pseudo terminal");
            return -1;
        }

        grantpt(fd);
        unlockpt(fd);

        pts_name = ptsname(fd);
        if (pts_name == nullptr) {
            LOG_ERROR("bsp::usbInit ptsname returned NULL, no pseudo terminal allocated");
            return -1;
        }

        fdNofity = inotify_init1(O_NONBLOCK);
        inotify_add_watch(fdNofity, pts_name, IN_OPEN | IN_CLOSE_WRITE);

        writePtsToFile(pts_name);
        LOG_INFO("bsp::usbInit linux ptsname: %s", pts_name);
        struct termios newtio;
        memset(&newtio, 0, sizeof(newtio));
        struct termios oldtio;
        tcgetattr(fd, &oldtio);

        newtio             = oldtio;
        newtio.c_cflag     = SERIAL_BAUDRATE | CS8 | CLOCAL | CREAD;
        newtio.c_iflag     = 0;
        newtio.c_oflag     = 0;
        newtio.c_lflag     = 0;
        newtio.c_cc[VMIN]  = 1;
        newtio.c_cc[VTIME] = 0;
        tcflush(fd, TCIFLUSH);

        cfsetispeed(&newtio, SERIAL_BAUDRATE);
        cfsetospeed(&newtio, SERIAL_BAUDRATE);
        tcsetattr(fd, TCSANOW, &newtio);

        USBReceiveQueue = initParams.queueHandle;
        USBIrqQueue     = initParams.irqQueueHandle;

        BaseType_t task_error = xTaskCreate(&bsp::usbDeviceTask,
                                            "USBLinuxReceive",
                                            SERIAL_BUFFER_LEN * 8,
                                            nullptr,
                                            tskIDLE_PRIORITY,
                                            &taskHandleReceive);

        if (task_error != pdPASS) {
            LOG_ERROR("bsp::usbInit Failed to start freertos USB_Linux_Receive");
            return -1;
        }

        return 0;
    }

    void usbHandleDataReceived()
    {}
} // namespace bsp