at_cli.c 9.1 KB
Newer Older
Lawlieta's avatar
Lawlieta 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
Lawlieta's avatar
Lawlieta 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
Lawlieta's avatar
Lawlieta 已提交
5 6 7 8 9 10 11 12 13
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-04-02     armink       first version
 */

#include <at.h>
#include <stdio.h>
#include <string.h>
14
#include <stdint.h>
Lawlieta's avatar
Lawlieta 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <rtthread.h>
#include <rtdevice.h>
#include <rthw.h>

#ifdef AT_USING_CLI

#define AT_CLI_FIFO_SIZE                      256

static struct rt_semaphore console_rx_notice;
static struct rt_ringbuffer *console_rx_fifo = RT_NULL;
static rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size) = RT_NULL;

#ifdef AT_USING_CLIENT
static struct rt_semaphore client_rx_notice;
static struct rt_ringbuffer *client_rx_fifo = RT_NULL;
30
#endif
Lawlieta's avatar
Lawlieta 已提交
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

static char console_getchar(void)
{
    char ch;

    rt_sem_take(&console_rx_notice, RT_WAITING_FOREVER);
    rt_ringbuffer_getchar(console_rx_fifo, (rt_uint8_t *)&ch);

    return ch;
}

static rt_err_t console_getchar_rx_ind(rt_device_t dev, rt_size_t size)
{
    uint8_t ch;
    rt_size_t i;

    for (i = 0; i < size; i++)
    {
        /* read a char */
        if (rt_device_read(dev, 0, &ch, 1))
        {
            rt_ringbuffer_put_force(console_rx_fifo, &ch, 1);
            rt_sem_release(&console_rx_notice);
        }
    }

    return RT_EOK;
}

void at_cli_init(void)
{
    rt_base_t int_lvl;
    rt_device_t console;

65
    rt_sem_init(&console_rx_notice, "cli_c", 0, RT_IPC_FLAG_FIFO);
Lawlieta's avatar
Lawlieta 已提交
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

    /* create RX FIFO */
    console_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
    /* created must success */
    RT_ASSERT(console_rx_fifo);

    int_lvl = rt_hw_interrupt_disable();
    console = rt_console_get_device();
    if (console)
    {
        /* backup RX indicate */
        odev_rx_ind = console->rx_indicate;
        rt_device_set_rx_indicate(console, console_getchar_rx_ind);
    }

    rt_hw_interrupt_enable(int_lvl);
}

void at_cli_deinit(void)
{
    rt_base_t int_lvl;
    rt_device_t console;

    int_lvl = rt_hw_interrupt_disable();
    console = rt_console_get_device();
    if (console && odev_rx_ind)
    {
        /* restore RX indicate */
        rt_device_set_rx_indicate(console, odev_rx_ind);
    }
    rt_hw_interrupt_enable(int_lvl);
97 98 99

    rt_sem_detach(&console_rx_notice);
    rt_ringbuffer_destroy(console_rx_fifo);
Lawlieta's avatar
Lawlieta 已提交
100 101 102
}

#ifdef AT_USING_SERVER
103 104 105 106 107 108
static rt_err_t at_server_console_getchar(struct at_server *server, char *ch, rt_int32_t timeout)
{
    *ch = console_getchar();
    return RT_EOK;
}

Lawlieta's avatar
Lawlieta 已提交
109 110 111 112 113 114 115
static void server_cli_parser(void)
{
    extern at_server_t at_get_server(void);

    at_server_t server = at_get_server();
    rt_base_t int_lvl;
    static rt_device_t device_bak;
116
    static rt_err_t (*getchar_bak)(struct at_server *server, char *ch, rt_int32_t timeout);
Lawlieta's avatar
Lawlieta 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130
    static char endmark_back[AT_END_MARK_LEN];

    /* backup server device and getchar function */
    {
        int_lvl = rt_hw_interrupt_disable();

        device_bak = server->device;
        getchar_bak = server->get_char;

        memset(endmark_back, 0x00, AT_END_MARK_LEN);
        memcpy(endmark_back, server->end_mark, strlen(server->end_mark));

        /* setup server device as console device */
        server->device = rt_console_get_device();
131
        server->get_char = at_server_console_getchar;
Lawlieta's avatar
Lawlieta 已提交
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

        memset(server->end_mark, 0x00, AT_END_MARK_LEN);
        server->end_mark[0] = '\r';

        rt_hw_interrupt_enable(int_lvl);
    }

    if (server)
    {
        rt_kprintf("======== Welcome to using RT-Thread AT command server cli ========\n");
        rt_kprintf("Input your at command for test server. Press 'ESC' to exit.\n");
        server->parser_entry(server);
    }
    else
    {
        rt_kprintf("AT client not initialized\n");
    }

    /* restore server device and getchar function */
    {
        int_lvl = rt_hw_interrupt_disable();

        server->device = device_bak;
        server->get_char = getchar_bak;

        memset(server->end_mark, 0x00, AT_END_MARK_LEN);
        memcpy(server->end_mark, endmark_back, strlen(endmark_back));

        rt_hw_interrupt_enable(int_lvl);
    }
}
#endif /* AT_USING_SERVER */

#ifdef AT_USING_CLIENT
static char client_getchar(void)
{
    char ch;

    rt_sem_take(&client_rx_notice, RT_WAITING_FOREVER);
    rt_ringbuffer_getchar(client_rx_fifo, (rt_uint8_t *)&ch);

    return ch;
}

static void at_client_entry(void *param)
{
    char ch;

    while(1)
    {
        ch = client_getchar();
        rt_kprintf("%c", ch);
    }
}

static rt_err_t client_getchar_rx_ind(rt_device_t dev, rt_size_t size)
{
    uint8_t ch;
    rt_size_t i;

    for (i = 0; i < size; i++)
    {
        /* read a char */
        if (rt_device_read(dev, 0, &ch, 1))
        {
            rt_ringbuffer_put_force(client_rx_fifo, &ch, 1);
            rt_sem_release(&client_rx_notice);
        }
    }

    return RT_EOK;
}
204
static void client_cli_parser(at_client_t  client)
Lawlieta's avatar
Lawlieta 已提交
205 206 207 208 209 210 211 212 213 214 215
{
#define ESC_KEY                 0x1B
#define BACKSPACE_KEY           0x08
#define DELECT_KEY              0x7F

    char ch;
    char cur_line[FINSH_CMD_SIZE] = { 0 };
    rt_size_t cur_line_len = 0;
    static rt_err_t (*client_odev_rx_ind)(rt_device_t dev, rt_size_t size) = RT_NULL;
    rt_base_t int_lvl;
    rt_thread_t at_client;
216
    at_status_t client_odev_status;
Lawlieta's avatar
Lawlieta 已提交
217 218 219

    if (client)
    {
220 221 222 223 224 225
        /* backup client status */
        {
            client_odev_status = client->status;
            client->status = AT_STATUS_CLI;
        }

Lawlieta's avatar
Lawlieta 已提交
226 227 228 229 230 231 232 233
        /* backup client device RX indicate */
        {
            int_lvl = rt_hw_interrupt_disable();
            client_odev_rx_ind = client->device->rx_indicate;
            rt_device_set_rx_indicate(client->device, client_getchar_rx_ind);
            rt_hw_interrupt_enable(int_lvl);
        }

234
        rt_sem_init(&client_rx_notice, "cli_r", 0, RT_IPC_FLAG_FIFO);
Lawlieta's avatar
Lawlieta 已提交
235 236
        client_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);

237
        at_client = rt_thread_create("at_cli", at_client_entry, RT_NULL, 512, 8, 8);
Lawlieta's avatar
Lawlieta 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        if (client_rx_fifo && at_client)
        {
            rt_kprintf("======== Welcome to using RT-Thread AT command client cli ========\n");
            rt_kprintf("Cli will forward your command to server port(%s). Press 'ESC' to exit.\n", client->device->parent.name);
            rt_thread_startup(at_client);
            /* process user input */
            while (ESC_KEY != (ch = console_getchar()))
            {
                if (ch == BACKSPACE_KEY || ch == DELECT_KEY)
                {
                    if (cur_line_len)
                    {
                        cur_line[--cur_line_len] = 0;
                        rt_kprintf("\b \b");
                    }
                    continue;
                }
                else if (ch == '\r' || ch == '\n')
                {
                    /* execute a AT request */
                    if (cur_line_len)
                    {
                        rt_kprintf("\n");
261
                        at_obj_exec_cmd(client, RT_NULL, "%.*s", cur_line_len, cur_line);
Lawlieta's avatar
Lawlieta 已提交
262 263 264 265 266
                    }
                    cur_line_len = 0;
                }
                else
                {
267 268 269 270
                    if(cur_line_len >= FINSH_CMD_SIZE)
                    {
                        continue;
                    }
Lawlieta's avatar
Lawlieta 已提交
271 272 273 274 275
                    rt_kprintf("%c", ch);
                    cur_line[cur_line_len++] = ch;
                }
            }

276 277 278
            /* restore client status */
            client->status = client_odev_status;

Lawlieta's avatar
Lawlieta 已提交
279 280 281 282 283 284
            /* restore client device RX indicate */
            {
                int_lvl = rt_hw_interrupt_disable();
                rt_device_set_rx_indicate(client->device, client_odev_rx_ind);
                rt_hw_interrupt_enable(int_lvl);
            }
285 286 287 288

            rt_thread_delete(at_client);
            rt_sem_detach(&client_rx_notice);
            rt_ringbuffer_destroy(client_rx_fifo);
Lawlieta's avatar
Lawlieta 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
        }
        else
        {
            rt_kprintf("No mem for AT cli client\n");
        }
    }
    else
    {
        rt_kprintf("AT client not initialized\n");
    }
}
#endif /* AT_USING_CLIENT */

static void at(int argc, char **argv)
{
304 305

    if (argc != 2 && argc != 3)
Lawlieta's avatar
Lawlieta 已提交
306
    {
307
        rt_kprintf("Please input '<server|client [dev_name]>' \n");
Lawlieta's avatar
Lawlieta 已提交
308 309 310 311 312 313 314 315 316 317 318
        return;
    }

    at_cli_init();

    if (!strcmp(argv[1], "server"))
    {
#ifdef AT_USING_SERVER
        server_cli_parser();
#else
        rt_kprintf("Not support AT server, please check your configure!\n");
319
#endif /* AT_USING_SERVER */
Lawlieta's avatar
Lawlieta 已提交
320 321 322 323
    }
    else if (!strcmp(argv[1], "client"))
    {
#ifdef AT_USING_CLIENT
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
        at_client_t client = RT_NULL;

        if (argc == 2)
        {
            client_cli_parser(at_client_get_first());
        }
        else if (argc == 3)
        {
            client = at_client_get(argv[2]);
            if (client == RT_NULL)
            {
                rt_kprintf("input AT client device name(%s) error.\n", argv[2]);
            }
            else
            {
                client_cli_parser(client);
            }
        }
Lawlieta's avatar
Lawlieta 已提交
342 343
#else
        rt_kprintf("Not support AT client, please check your configure!\n");
344
#endif /* AT_USING_CLIENT */
Lawlieta's avatar
Lawlieta 已提交
345 346 347
    }
    else
    {
348
        rt_kprintf("Please input '<server|client [dev_name]>' \n");
Lawlieta's avatar
Lawlieta 已提交
349 350 351 352
    }

    at_cli_deinit();
}
353
MSH_CMD_EXPORT(at, RT-Thread AT component cli: at <server|client [dev_name]>);
Lawlieta's avatar
Lawlieta 已提交
354 355

#endif /* AT_USING_CLI */