summaryrefslogtreecommitdiff
path: root/components/lwp/terminal/tty_ptmx.c
blob: 5938bf01039023d0ecd3128eb64037329b1eb022 (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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-12-07     Shell        init ver.
 */

#define DBG_TAG "lwp.tty"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include "tty_config.h"
#include "tty_internal.h"
#include "bsd_porting.h"
#include "terminal.h"

#include <fcntl.h>

static struct dfs_file_ops ptm_fops;

static int ptm_fops_open(struct dfs_file *file)
{
    int rc;
    rt_uint32_t oflags = file->flags;
    rt_thread_t cur_thr = rt_thread_self();

    if (file->vnode && file->vnode->data)
    {
        /**
         * Filter out illegal flags
         */
        if ((oflags & ~(O_RDWR | O_NOCTTY | O_CLOEXEC | O_LARGEFILE)) == 0)
        {
            rc = pts_alloc(FFLAGS(oflags & O_ACCMODE), cur_thr, file);

            /* detached operation from devfs */
            if (rc == 0)
                file->vnode->fops = &ptm_fops;
        }
        else
        {
            rc = -EINVAL;
        }
    }
    else
    {
        rc = -EINVAL;
    }

    return rc;
}

static int ptm_fops_close(struct dfs_file *file)
{
    int rc;
    lwp_tty_t tp;
    rt_device_t device;

    if (file->data)
    {
        device = (rt_device_t)file->data;
        tp = rt_container_of(device, struct lwp_tty, parent);
        rc = bsd_ptsdev_methods.fo_close(tp, rt_thread_self());
    }
    else
    {
        rc = -EINVAL;
    }

    return rc;
}

static ssize_t ptm_fops_read(struct dfs_file *file, void *buf, size_t count,
                             off_t *pos)
{
    ssize_t rc = 0;
    int error;
    struct uio uio;
    struct iovec iov;
    rt_device_t device;
    struct lwp_tty *tp;
    int oflags = file->flags;

    if (file->data)
    {
        device = (rt_device_t)file->data;
        tp = rt_container_of(device, struct lwp_tty, parent);

        /* setup uio parameters */
        iov.iov_base = (void *)buf;
        iov.iov_len = count;
        uio.uio_offset = file->fpos;
        uio.uio_resid = count;
        uio.uio_iov = &iov;
        uio.uio_iovcnt = 1;
        uio.uio_rw = UIO_READ;

        rc = count;
        error =
            bsd_ptsdev_methods.fo_read(tp, &uio, 0, oflags, rt_thread_self());
        rc -= uio.uio_resid;
        if (error)
        {
            rc = error;
        }

        /* reset file context */
        file->fpos = uio.uio_offset;
    }

    return rc;
}

static ssize_t ptm_fops_write(struct dfs_file *file, const void *buf,
                              size_t count, off_t *pos)
{
    ssize_t rc = 0;
    int error;
    struct uio uio;
    struct iovec iov;
    rt_device_t device;
    struct lwp_tty *tp;
    int oflags = file->flags;

    if (file->data)
    {
        device = (rt_device_t)file->data;
        tp = rt_container_of(device, struct lwp_tty, parent);

        /* setup uio parameters */
        iov.iov_base = (void *)buf;
        iov.iov_len = count;
        uio.uio_offset = file->fpos;
        uio.uio_resid = count;
        uio.uio_iov = &iov;
        uio.uio_iovcnt = 1;
        uio.uio_rw = UIO_WRITE;

        rc = count;
        error =
            bsd_ptsdev_methods.fo_write(tp, &uio, 0, oflags, rt_thread_self());
        if (error)
        {
            rc = error;
        }
        else
        {
            rc -= uio.uio_resid;
        }

        /* reset file context */
        file->fpos = uio.uio_offset;
    }
    return rc;
}

static int ptm_fops_ioctl(struct dfs_file *file, int cmd, void *arg)
{
    int rc;
    lwp_tty_t tp;
    rt_device_t device;
    rt_ubase_t cmd_normal = (unsigned int)cmd;

    if (file->data)
    {
        device = (rt_device_t)file->data;
        tp = rt_container_of(device, struct lwp_tty, parent);

        switch (cmd_normal)
        {
            case TIOCSPTLCK:
            {
                int is_lock;
                if (lwp_get_from_user(&is_lock, arg, sizeof(int)) != sizeof(int))
                    return -EFAULT;
                pts_set_lock(tp, is_lock);
                rc = 0;
            }
            break;
            case TIOCGPTLCK:
            {
                int is_lock = pts_is_locked(tp);
                if (lwp_put_to_user(arg, &is_lock, sizeof(int)) != sizeof(int))
                    return -EFAULT;
                rc = 0;
            }
            break;
            case TIOCGPKT:
            {
                int pktmode = pts_get_pktmode(tp);
                if (lwp_put_to_user(arg, &pktmode, sizeof(int)) != sizeof(int))
                    return -EFAULT;
                rc = 0;
            }
            break;
            default:
                rc = bsd_ptsdev_methods.fo_ioctl(
                    tp, cmd_normal, arg, 0, FFLAGS(file->flags), rt_thread_self());
                break;
        }
    }
    else
    {
        rc = -EINVAL;
    }

    return rc;
}

static int ptm_fops_flush(struct dfs_file *file)
{
    return -EINVAL;
}

static off_t ptm_fops_lseek(struct dfs_file *file, off_t offset, int wherece)
{
    return -EINVAL;
}

static int ptm_fops_truncate(struct dfs_file *file, off_t offset)
{
    return -EINVAL;
}

static int ptm_fops_poll(struct dfs_file *file, struct rt_pollreq *req)
{
    int rc;
    rt_device_t device;
    struct lwp_tty *tp;

    if (file->data)
    {
        device = (rt_device_t)file->data;
        tp = rt_container_of(device, struct lwp_tty, parent);

        rc = bsd_ptsdev_methods.fo_poll(tp, req, 0, rt_thread_self());
    }
    else
    {
        rc = -1;
    }

    return rc;
}

static int ptm_fops_mmap(struct dfs_file *file, struct lwp_avl_struct *mmap)
{
    return -EINVAL;
}

static int ptm_fops_lock(struct dfs_file *file, struct file_lock *flock)
{
    return -EINVAL;
}

static int ptm_fops_flock(struct dfs_file *file, int operation, struct file_lock *flock)
{
    return -EINVAL;
}

static struct dfs_file_ops ptm_fops = {
    .open = ptm_fops_open,
    .close = ptm_fops_close,
    .ioctl = ptm_fops_ioctl,
    .read = ptm_fops_read,
    .write = ptm_fops_write,
    .flush = ptm_fops_flush,
    .lseek = ptm_fops_lseek,
    .truncate = ptm_fops_truncate,
    .poll = ptm_fops_poll,
    .mmap = ptm_fops_mmap,
    .lock = ptm_fops_lock,
    .flock = ptm_fops_flock,
};

rt_err_t lwp_ptmx_init(rt_device_t ptmx_device, const char *root_path)
{
    char *device_name;
    int root_len;
    const char *dev_rel_path;
    rt_err_t rc;

    root_len = strlen(root_path);
    dev_rel_path = "/ptmx";
    device_name = rt_malloc(root_len + sizeof("/ptmx"));

    if (device_name)
    {
        /* Register device */
        snprintf(device_name, root_len + sizeof("/ptmx"), "%s%s", root_path, dev_rel_path);
        rt_device_register(ptmx_device, device_name, 0);

        /* Setup fops */
        ptmx_device->fops = &ptm_fops;

        rt_free(device_name);

        rc = RT_EOK;
    }
    else
    {
        rc = -RT_ENOMEM;
    }

    return rc;
}

/* system level ptmx */
static struct rt_device sysptmx;

static struct dfs_file_ops sysptmx_file_ops;

static rt_err_t sysptmx_readlink(struct rt_device *dev, char *buf, int len)
{
    int rc = 0;

    /* TODO: support multi-root ? */
    snprintf(buf, len, "pts/ptmx");

    return rc;
}

static int _sys_ptmx_init(void)
{
    rt_err_t rc;
    rt_device_t sysptmx_rtdev = &sysptmx;

    /* setup system level device */
    sysptmx_rtdev->type = RT_Device_Class_Char;
    sysptmx_rtdev->ops = RT_NULL;
    rc = rt_device_register(sysptmx_rtdev, "ptmx", RT_DEVICE_FLAG_DYNAMIC);
    if (rc == RT_EOK)
    {
        sysptmx_rtdev->readlink = &sysptmx_readlink;
        sysptmx_rtdev->fops = &sysptmx_file_ops;
    }
    return rc;
}
INIT_DEVICE_EXPORT(_sys_ptmx_init);