summaryrefslogtreecommitdiff
path: root/examples/rt-link/rtlink_dev_example.c
blob: fb3629538ad17e91e06a5eba095fb2254a5f3909 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-06-19     Sherman    the first version
 */

#include <string.h>
#include <stdlib.h>

#define DBG_TAG    "example"
#define DBG_LVL    DBG_LOG
#include <rtdbg.h>
#include <rtlink_dev.h>

enum
{
    NONE_TEST = 0,
    SHORT_FRAME_TEST,
    LONG_FRAME_TEST
};
static rt_uint8_t speed_test_type = NONE_TEST;

static struct rt_link_device rtlink_dev = {0};
#define  RTLINK01       "rtlink01"
#define TEST_CONTEXT    "This message is sent by RT-Link"
rt_uint8_t test_buff[1024] = {0};

static rt_err_t rtlink_dev_rx_ind(rt_device_t dev, rt_size_t size)
{
    RT_ASSERT(dev != RT_NULL);
    LOG_I("rx_ind: dev name %s, rx size %d", dev->parent.name, size);
    return RT_EOK;
}

static rt_err_t rtlink_dev_tx_done(rt_device_t dev, void *buffer)
{
    RT_ASSERT(dev != RT_NULL);
    struct rt_link_device *rtlink_dev = (struct rt_link_device *)dev;
    LOG_I("tx_done: dev name %s, buffer 0x%p errno %d", dev->parent.name, buffer, rtlink_dev->service.err);
    rt_free(buffer);
    return RT_EOK;
}

#ifdef RT_USING_POSIX_DEVIO
#include <dfs_file.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <poll.h>
#include <sys/select.h>

#define RTLINK01_PATH "/dev/rtlink01"
int fd  = -1;

static void rtlink_fopen(int argc, char *argv[])
{
    fd = open(RTLINK01_PATH, O_RDWR | O_NONBLOCK);

    if (fd < 0)
    {
        LOG_E("open rt_link failed!");
    }
}
MSH_CMD_EXPORT(rtlink_fopen, rtlink posix interface example);

static void rtlink_fclose(int argc, char *argv[])
{
    LOG_D("colse %d", fd);
    close(fd);
    fd = -1;
}
MSH_CMD_EXPORT(rtlink_fclose, rtlink posix interface example);

static void rtlink_fread(int argc, char *argv[])
{
    int read_len;
    read_len = read(fd, test_buff, sizeof(test_buff));
    LOG_D("read len %d", read_len);
    LOG_HEX("read", 8, test_buff, 32);
}
MSH_CMD_EXPORT(rtlink_fread, rtlink posix interface example);

static void rtlink_fwrite(int argc, char *argv[])
{
    char *data = RT_NULL;
    rt_size_t length = 0;
    rt_uint16_t count = 0;
    rt_size_t ret = 0;

    if (argc == 1)
    {
        data = rt_malloc(sizeof(TEST_CONTEXT));
        if (data)
        {
            length = sizeof(TEST_CONTEXT) - 1;
            rt_memcpy(data, TEST_CONTEXT, sizeof(TEST_CONTEXT) - 1);
            ret = write(fd, data, length);
        }
        LOG_I("write data(0x%p) result: %d.", data, ret);
    }
    else if (argc >= 3)
    {
        if (strcmp(argv[1], "-l") == 0)
        {
            data = rt_malloc(atoi((const char *)argv[2]));
            if (data)
            {
                for (count = 0; count < atoi((const char *)argv[2]); count++)
                {
                    data[count] = (count % 93 + 33);
                }
                length = atoi((const char *)argv[2]);
                ret = write(fd, data, length);
            }
            LOG_I("write data(0x%p) result: %d.", data, ret);
        }
        else
        {
            LOG_E("Invalid parameter.");
        }
    }
}
MSH_CMD_EXPORT(rtlink_fwrite, rtlink posix interface example);

#define RTLINK02         "rtlink02"
#define RTLINK02_PATH    "/dev/rtlink02"
static struct rt_link_device rtlink_fd = {0};
rt_uint8_t fd_buff[1024] = {0};

static void listen_thread(void *param)
{
    int fd  = open(RTLINK02_PATH, O_RDWR | O_NONBLOCK);
    if (fd < 0)
    {
        LOG_E("open (%s) failed", RTLINK02);
        return;
    }
    while (1)
    {
        rt_uint8_t *write_buf = RT_NULL;
        int write_len = 0;
        fd_set readfds, writefds;
        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        FD_SET(fd, &readfds);
        FD_SET(fd, &writefds);

        int ret = select(fd + 1, &readfds, &writefds, RT_NULL, RT_NULL);
        LOG_D("select ret(%d), read (%d), write (%d)", ret, readfds, writefds);
        if (FD_ISSET(fd, &readfds))
        {
            LOG_I("POLLIN");
            int read_len = read(fd, fd_buff, sizeof(test_buff));
            LOG_D("read len %d", read_len);
            LOG_HEX("read", 8, test_buff, 32);
        }

        if (FD_ISSET(fd, &writefds))
        {
            LOG_I("POLLOUT");
            write_buf = rt_malloc(1024);
            if (write_buf)
            {
                write_len = write(fd, write_buf, 1024);
                LOG_D("write %d", write_len);
            }
        }

        rt_thread_delay(500);
    }
    LOG_I("fd (%s) listen thread exit", RTLINK02);
}

static void rtlink_fselect()
{
    /* step1: register rtlink to to the device framework */
    rt_link_dev_register(&rtlink_fd, RTLINK02,
                         RT_DEVICE_FLAG_RDWR |
                         RT_DEVICE_FLAG_REMOVABLE |
                         RT_DEVICE_FLAG_STANDALONE,
                         RT_NULL);

    /* step2: Initialize the rlink device as the default configuration,  */
    rt_device_t device = rt_device_find(RTLINK02);
    if (device == RT_NULL)
    {
        LOG_E("device not find!");
        return ;
    }
    rt_device_init(device);

    /* step3: config rtlink device rx/tx callback, channel, send timeout */
    rt_device_set_rx_indicate(device, rtlink_dev_rx_ind);
    rt_device_set_tx_complete(device, rtlink_dev_tx_done);
    struct rt_link_service service;
    service.service = RT_LINK_SERVICE_MNGT;
    service.timeout_tx = RT_WAITING_NO;
    rt_device_control(device, RT_DEVICE_CTRL_CONFIG, &service);

    rt_thread_t tid = rt_thread_create(RTLINK02, listen_thread, RT_NULL, 1024, 21, 20);
    if (tid)
    {
        rt_thread_startup(tid);
    }
}
MSH_CMD_EXPORT(rtlink_fselect, rtlink posix interface example);
#endif  /* RT_USING_POSIX_DEVIO */

static void rtlink_dread(void)
{
    rt_size_t read_len = 0;
    rt_device_t dev = rt_device_find(RTLINK01);
    if (dev == RT_NULL)
    {
        LOG_E("dev %s not find ", RTLINK01);
        return;
    }

    read_len = rt_device_read(dev, 0, test_buff, sizeof(test_buff));

    LOG_D("read len %d", read_len);
    LOG_HEX("read", 8, test_buff, 32);
}
MSH_CMD_EXPORT(rtlink_dread, rtlink device interface example);

void rt_link_speed_test(void *paremeter)
{
    int ret;
    rt_uint8_t *send_buf, *data;
    rt_size_t bufflen = 0;
    rt_size_t sentlen = 0;
    rt_size_t count = 0;
    rt_tick_t tick1, tick2;
    rt_size_t total = 0;
    rt_uint32_t integer, decimal;
    rt_device_t dev = rt_device_find(RTLINK01);
    if (dev == RT_NULL)
    {
        LOG_E("dev %s not find!", RTLINK01);
        return ;
    }

    if (speed_test_type == SHORT_FRAME_TEST)
    {
        bufflen = 988;
    }
    else
    {
        bufflen = 3036;
    }

    send_buf = rt_malloc(bufflen);
    if (send_buf != RT_NULL)
    {
        data = send_buf;
        for (count = 0; count < bufflen; count++)
        {
            *data++ = (count % 93 + 33);
        }
    }
    else
    {
        rt_kprintf("speed of send buffer malloc failed.");
        return;
    }

    tick1 = rt_tick_get();
    while (speed_test_type)
    {
        ret = rt_device_write(dev, 0, send_buf, bufflen);

        if (ret == RT_EOK)
        {
            sentlen += bufflen;
        }

        tick2 = rt_tick_get();
        if (tick2 - tick1 >= RT_TICK_PER_SECOND)
        {
            total = sentlen * RT_TICK_PER_SECOND / 125 / (tick2 - tick1);
            integer = total / 1000;
            decimal = total % 1000;
            LOG_I("%d.%03d0 Mbps!", integer, decimal);
            sentlen = 0;
            tick1 = tick2;
        }
    }
    rt_free(send_buf);
    LOG_W("speed test end, type %d", speed_test_type);
}

void create_thead_to_test_speed(rt_uint8_t mutil_num)
{
    rt_uint8_t i = 0;

    LOG_D("Speed test type [%02d], mutil [%02d]", speed_test_type, mutil_num);
    for (i = 0; i < mutil_num; i++)
    {
        rt_thread_t tid;
        char tid_name[RT_NAME_MAX + 1] = {0};

        rt_snprintf(tid_name, sizeof(tid_name), "lny_s%03d", i + 1);
        tid = rt_thread_create(tid_name, rt_link_speed_test, RT_NULL, 1024, 20, 10);
        rt_thread_startup(tid);
        LOG_I("Speed test thread[%s] startup", tid_name);
    }
}

static rt_err_t rtlink_dwrite(int argc, char *argv[])
{
    char *data = RT_NULL;
    rt_size_t length = 0;
    rt_uint16_t count = 0;
    rt_size_t ret = -RT_ERROR;

    rt_device_t dev = rt_device_find(RTLINK01);
    if (dev == RT_NULL)
    {
        LOG_E("device not find!");
        return ret;
    }

    if (argc == 1)
    {
        data = rt_malloc(sizeof(TEST_CONTEXT));
        length = sizeof(TEST_CONTEXT) - 1;
        rt_memcpy(data, TEST_CONTEXT, sizeof(TEST_CONTEXT) - 1);

        ret = rt_device_write(dev, 0, data, length);
        LOG_I("write data(0x%p) result: %d.", data, ret);
    }
    else if (argc >= 3)
    {
        if (strcmp(argv[1], "-l") == 0)
        {
            data = rt_malloc(atoi((const char *)argv[2]));
            for (count = 0; count < atoi((const char *)argv[2]); count++)
            {
                data[count] = (count % 93 + 33);
            }
            length = atoi((const char *)argv[2]);
            ret = rt_device_write(dev, 0, data, length);
            LOG_I("write data(0x%p) result: %d.", data, ret);
        }
        else
        {
            LOG_E("Invalid parameter.");
        }
    }
    return ret;
}
MSH_CMD_EXPORT(rtlink_dwrite, rtlink device interface example);

static void rtlink_dinit(void)
{
    /* step1: register rtlink to to the device framework */
    rt_link_dev_register(&rtlink_dev, RTLINK01,
                         RT_DEVICE_FLAG_RDWR |
                         RT_DEVICE_FLAG_REMOVABLE |
                         RT_DEVICE_FLAG_STANDALONE,
                         RT_NULL);

    /* step2: Initialize the rlink device as the default configuration,  */
    rt_device_t device = rt_device_find(RTLINK01);
    if (device == RT_NULL)
    {
        LOG_E("device not find!");
        return ;
    }
    rt_device_init(device);

    /* step3: config rtlink device rx/tx callback, channel, send timeout */
    rt_device_set_rx_indicate(device, rtlink_dev_rx_ind);
    rt_device_set_tx_complete(device, rtlink_dev_tx_done);
    struct rt_link_service service;
    service.service = RT_LINK_SERVICE_SOCKET;
    service.timeout_tx = RT_WAITING_FOREVER;
    service.flag = RT_LINK_FLAG_ACK | RT_LINK_FLAG_CRC;
    rt_device_control(device, RT_DEVICE_CTRL_CONFIG, &service);
}
MSH_CMD_EXPORT(rtlink_dinit, rtlink device interface example);

static void rtlink_dopen()
{
    /* step4: open rtlink device, attach the service channel */
    rt_device_t device = rt_device_find(RTLINK01);
    if (device == RT_NULL)
    {
        LOG_E("device not find!");
        return ;
    }
    rt_err_t ret = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
    LOG_I("dev open ret %d", ret);
}
MSH_CMD_EXPORT(rtlink_dopen, rtlink device interface example);