at_client.c 24.0 KB
Newer Older
Lawlieta's avatar
Lawlieta 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * File      : at_client.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-03-30     chenyong     first version
 * 2018-04-12     chenyong     add client implement
24
 * 2018-08-17     chenyong     multiple client support
Lawlieta's avatar
Lawlieta 已提交
25 26 27 28 29 30 31 32 33 34 35 36
 */

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

#define AT_RESP_END_OK                 "OK"
#define AT_RESP_END_ERROR              "ERROR"
#define AT_RESP_END_FAIL               "FAIL"
#define AT_END_CR_LF                   "\r\n"

37
static struct at_client at_client_table[AT_CLIENT_NUM_MAX] = { 0 };
Lawlieta's avatar
Lawlieta 已提交
38 39 40 41 42 43

extern rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args);
extern void at_print_raw_cmd(const char *type, const char *cmd, rt_size_t size);
extern const char *at_get_last_cmd(rt_size_t *cmd_size);

/**
44
 * Create response object.
Lawlieta's avatar
Lawlieta 已提交
45 46 47 48 49 50 51
 *
 * @param buf_size the maximum response buffer size
 * @param line_num the number of setting response lines
 *         = 0: the response data will auto return when received 'OK' or 'ERROR'
 *        != 0: the response data will return when received setting lines number data
 * @param timeout the maximum response time
 *
52
 * @return != RT_NULL: response object
Lawlieta's avatar
Lawlieta 已提交
53 54 55 56 57 58 59
 *          = RT_NULL: no memory
 */
at_response_t at_create_resp(rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout)
{
    at_response_t resp = RT_NULL;

    resp = (at_response_t) rt_calloc(1, sizeof(struct at_response));
60
    if (resp == RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
61
    {
62
        LOG_E("AT create response object failed! No memory for response object!");
Lawlieta's avatar
Lawlieta 已提交
63 64 65 66
        return RT_NULL;
    }

    resp->buf = (char *) rt_calloc(1, buf_size);
67
    if (resp->buf == RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
68
    {
69
        LOG_E("AT create response object failed! No memory for response buffer!");
Lawlieta's avatar
Lawlieta 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
        rt_free(resp);
        return RT_NULL;
    }

    resp->buf_size = buf_size;
    resp->line_num = line_num;
    resp->line_counts = 0;
    resp->timeout = timeout;

    return resp;
}

/**
83
 * Delete and free response object.
Lawlieta's avatar
Lawlieta 已提交
84
 *
85
 * @param resp response object
Lawlieta's avatar
Lawlieta 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
 */
void at_delete_resp(at_response_t resp)
{
    if (resp && resp->buf)
    {
        rt_free(resp->buf);
    }

    if (resp)
    {
        rt_free(resp);
        resp = RT_NULL;
    }
}

/**
102
 * Set response object information
Lawlieta's avatar
Lawlieta 已提交
103
 *
104
 * @param resp response object
Lawlieta's avatar
Lawlieta 已提交
105 106 107 108 109 110
 * @param buf_size the maximum response buffer size
 * @param line_num the number of setting response lines
 *         = 0: the response data will auto return when received 'OK' or 'ERROR'
 *        != 0: the response data will return when received setting lines number data
 * @param timeout the maximum response time
 *
111
 * @return  != RT_NULL: response object
Lawlieta's avatar
Lawlieta 已提交
112 113 114 115 116 117
 *           = RT_NULL: no memory
 */
at_response_t at_resp_set_info(at_response_t resp, rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout)
{
    RT_ASSERT(resp);

118
    if (resp->buf_size != buf_size)
Lawlieta's avatar
Lawlieta 已提交
119 120 121
    {
        resp->buf_size = buf_size;

122 123
        resp->buf = (char *) rt_realloc(resp->buf, buf_size);
        if (!resp->buf)
Lawlieta's avatar
Lawlieta 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        {
            LOG_D("No memory for realloc response buffer size(%d).", buf_size);
            return RT_NULL;
        }
    }

    resp->line_num = line_num;
    resp->timeout = timeout;

    return resp;
}

/**
 * Get one line AT response buffer by line number.
 *
139
 * @param resp response object
Lawlieta's avatar
Lawlieta 已提交
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
 * @param resp_line line number, start from '1'
 *
 * @return != RT_NULL: response line buffer
 *          = RT_NULL: input response line error
 */
const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line)
{
    char *resp_buf = resp->buf;
    char *resp_line_buf = RT_NULL;
    rt_size_t line_num = 1;

    RT_ASSERT(resp);

    if (resp_line > resp->line_counts || resp_line <= 0)
    {
        LOG_E("AT response get line failed! Input response line(%d) error!", resp_line);
        return RT_NULL;
    }

    for (line_num = 1; line_num <= resp->line_counts; line_num++)
    {
        if (resp_line == line_num)
        {
            resp_line_buf = resp_buf;

            return resp_line_buf;
        }

        resp_buf += strlen(resp_buf) + 1;
    }

    return RT_NULL;
}

/**
 * Get one line AT response buffer by keyword
 *
177
 * @param resp response object
Lawlieta's avatar
Lawlieta 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
 * @param keyword query keyword
 *
 * @return != RT_NULL: response line buffer
 *          = RT_NULL: no matching data
 */
const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword)
{
    char *resp_buf = resp->buf;
    char *resp_line_buf = RT_NULL;
    rt_size_t line_num = 1;

    RT_ASSERT(resp);
    RT_ASSERT(keyword);

    for (line_num = 1; line_num <= resp->line_counts; line_num++)
    {
194
        if (strstr(resp_buf, keyword))
Lawlieta's avatar
Lawlieta 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        {
            resp_line_buf = resp_buf;

            return resp_line_buf;
        }

        resp_buf += strlen(resp_buf) + 1;
    }

    return RT_NULL;
}

/**
 * Get and parse AT response buffer arguments by line number.
 *
210
 * @param resp response object
Lawlieta's avatar
Lawlieta 已提交
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
 * @param resp_line line number, start from '1'
 * @param resp_expr response buffer expression
 *
 * @return -1 : input response line number error or get line buffer error
 *          0 : parsed without match
 *         >0 : the number of arguments successfully parsed
 */
int at_resp_parse_line_args(at_response_t resp, rt_size_t resp_line, const char *resp_expr, ...)
{
    va_list args;
    int resp_args_num = 0;
    const char *resp_line_buf = RT_NULL;

    RT_ASSERT(resp);
    RT_ASSERT(resp_expr);

    if ((resp_line_buf = at_resp_get_line(resp, resp_line)) == RT_NULL)
    {
        return -1;
    }

    va_start(args, resp_expr);

    resp_args_num = vsscanf(resp_line_buf, resp_expr, args);

    va_end(args);

    return resp_args_num;
}

/**
 * Get and parse AT response buffer arguments by keyword.
 *
244
 * @param resp response object
Lawlieta's avatar
Lawlieta 已提交
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
 * @param keyword query keyword
 * @param resp_expr response buffer expression
 *
 * @return -1 : input keyword error or get line buffer error
 *          0 : parsed without match
 *         >0 : the number of arguments successfully parsed
 */
int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const char *resp_expr, ...)
{
    va_list args;
    int resp_args_num = 0;
    const char *resp_line_buf = RT_NULL;

    RT_ASSERT(resp);
    RT_ASSERT(resp_expr);

    if ((resp_line_buf = at_resp_get_line_by_kw(resp, keyword)) == RT_NULL)
    {
        return -1;
    }

    va_start(args, resp_expr);

    resp_args_num = vsscanf(resp_line_buf, resp_expr, args);

    va_end(args);

    return resp_args_num;
}

/**
 * Send commands to AT server and wait response.
 *
278 279
 * @param client current AT client object
 * @param resp AT response object, using RT_NULL when you don't care response
Lawlieta's avatar
Lawlieta 已提交
280 281 282 283 284 285
 * @param cmd_expr AT commands expression
 *
 * @return 0 : success
 *        -1 : response status error
 *        -2 : wait timeout
 */
286
int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...)
Lawlieta's avatar
Lawlieta 已提交
287 288 289 290 291 292 293 294
{
    va_list args;
    rt_size_t cmd_size = 0;
    rt_err_t result = RT_EOK;
    const char *cmd = RT_NULL;

    RT_ASSERT(cmd_expr);

295 296 297 298 299 300
    if (client == RT_NULL)
    {
        LOG_E("input AT Client object is NULL, please create or get AT Client object!");
        return -RT_ERROR;
    }

Lawlieta's avatar
Lawlieta 已提交
301 302 303 304 305 306 307 308 309
    rt_mutex_take(client->lock, RT_WAITING_FOREVER);

    client->resp_status = AT_RESP_OK;
    client->resp = resp;

    va_start(args, cmd_expr);
    at_vprintfln(client->device, cmd_expr, args);
    va_end(args);

310
    if (resp != RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
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
    {
        resp->line_counts = 0;
        if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK)
        {
            cmd = at_get_last_cmd(&cmd_size);
            LOG_E("execute command (%.*s) timeout (%d ticks)!", cmd_size, cmd, resp->timeout);
            client->resp_status = AT_RESP_TIMEOUT;
            result = -RT_ETIMEOUT;
            goto __exit;
        }
        if (client->resp_status != AT_RESP_OK)
        {
            cmd = at_get_last_cmd(&cmd_size);
            LOG_E("execute command (%.*s) failed!", cmd_size, cmd);
            result = -RT_ERROR;
            goto __exit;
        }
    }

__exit:
    client->resp = RT_NULL;

    rt_mutex_release(client->lock);

    return result;
}

Z
zylx 已提交
338 339 340
/**
 * Waiting for connection to external devices.
 *
341
 * @param client current AT client object
Z
zylx 已提交
342 343 344 345 346 347
 * @param timeout millisecond for timeout
 *
 * @return 0 : success
 *        -2 : timeout
 *        -5 : no memory
 */
348
int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout)
Z
zylx 已提交
349 350 351 352 353
{
    rt_err_t result = RT_EOK;
    at_response_t resp = RT_NULL;
    rt_tick_t start_time = 0;

354 355
    if (client == RT_NULL)
    {
356 357
        LOG_E("input AT Client object is NULL, please create or get AT Client object!");
        return -RT_ERROR;
358 359
    }

Z
zylx 已提交
360
    resp = at_create_resp(16, 0, rt_tick_from_millisecond(500));
361
    if (resp == RT_NULL)
Z
zylx 已提交
362
    {
363
        LOG_E("No memory for response object!");
Z
zylx 已提交
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
        return -RT_ENOMEM;
    }

    rt_mutex_take(client->lock, RT_WAITING_FOREVER);
    client->resp = resp;

    start_time = rt_tick_get();

    while (1)
    {
        /* Check whether it is timeout */
        if (rt_tick_get() - start_time > timeout)
        {
            LOG_E("wait connect timeout (%d millisecond)!", timeout);
            result = -RT_ETIMEOUT;
            break;
        }

        /* Check whether it is already connected */
        resp->line_counts = 0;
        rt_device_write(client->device, 0, "AT\r\n", 4);

        if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK)
            continue;
        else
            break;
    }

    at_delete_resp(resp);

    client->resp = RT_NULL;

    rt_mutex_release(client->lock);

    return result;
}

Lawlieta's avatar
Lawlieta 已提交
401 402 403
/**
 * Send data to AT server, send data don't have end sign(eg: \r\n).
 *
404
 * @param client current AT client object
Lawlieta's avatar
Lawlieta 已提交
405 406 407
 * @param buf   send data buffer
 * @param size  send fixed data size
 *
408 409
 * @return >0: send data size
 *         =0: send failed
Lawlieta's avatar
Lawlieta 已提交
410
 */
411
rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size)
Lawlieta's avatar
Lawlieta 已提交
412 413 414
{
    RT_ASSERT(buf);

415 416 417 418 419 420
    if (client == RT_NULL)
    {
        LOG_E("input AT Client object is NULL, please create or get AT Client object!");
        return 0;
    }

Lawlieta's avatar
Lawlieta 已提交
421 422 423 424 425 426 427
#ifdef AT_PRINT_RAW_CMD
    at_print_raw_cmd("send", buf, size);
#endif

    return rt_device_write(client->device, 0, buf, size);
}

428
static rt_err_t at_client_getchar(at_client_t client, char *ch, rt_int32_t timeout)
Lawlieta's avatar
Lawlieta 已提交
429
{
430
    rt_err_t result = RT_EOK;
Lawlieta's avatar
Lawlieta 已提交
431

432
    while (rt_device_read(client->device, 0, ch, 1) == 0)
433
    {
434
        rt_sem_control(client->rx_notice, RT_IPC_CMD_RESET, RT_NULL);
435 436 437 438 439 440

        result = rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout));
        if (result != RT_EOK)
        {
            return result;
        }
441
    }
Lawlieta's avatar
Lawlieta 已提交
442

443
    return RT_EOK;
Lawlieta's avatar
Lawlieta 已提交
444 445 446 447 448
}

/**
 * AT client receive fixed-length data.
 *
449
 * @param client current AT client object
Lawlieta's avatar
Lawlieta 已提交
450 451
 * @param buf   receive data buffer
 * @param size  receive fixed data size
452
 * @param timeout  receive data timeout (ms)
Lawlieta's avatar
Lawlieta 已提交
453 454 455
 *
 * @note this function can only be used in execution function of URC data
 *
456 457
 * @return >0: receive data size
 *         =0: receive failed
Lawlieta's avatar
Lawlieta 已提交
458
 */
459
rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_int32_t timeout)
Lawlieta's avatar
Lawlieta 已提交
460 461
{
    rt_size_t read_idx = 0;
462
    rt_err_t result = RT_EOK;
Lawlieta's avatar
Lawlieta 已提交
463 464 465 466
    char ch;

    RT_ASSERT(buf);

467 468 469 470 471 472
    if (client == RT_NULL)
    {
        LOG_E("input AT Client object is NULL, please create or get AT Client object!");
        return 0;
    }

Lawlieta's avatar
Lawlieta 已提交
473 474 475 476
    while (1)
    {
        if (read_idx < size)
        {
477 478 479 480 481 482
            result = at_client_getchar(client, &ch, timeout);
            if (result != RT_EOK)
            {
                LOG_E("AT Client receive failed, uart device get data error(%d)", result);
                return 0;
            }
Lawlieta's avatar
Lawlieta 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496

            buf[read_idx++] = ch;
        }
        else
        {
            break;
        }
    }

#ifdef AT_PRINT_RAW_CMD
    at_print_raw_cmd("urc_recv", buf, size);
#endif

    return read_idx;
497 498 499 500 501 502 503 504 505 506
}

/**
 *  AT client set end sign.
 *
 * @param client current AT client object
 * @param ch the end sign, can not be used when it is '\0'
 */
void at_obj_set_end_sign(at_client_t client, char ch)
{
507 508 509 510 511
    if (client == RT_NULL)
    {
        LOG_E("input AT Client object is NULL, please create or get AT Client object!");
        return;
    }
Lawlieta's avatar
Lawlieta 已提交
512

513
    client->end_sign = ch;
Lawlieta's avatar
Lawlieta 已提交
514 515 516
}

/**
517
 * set URC(Unsolicited Result Code) table
Lawlieta's avatar
Lawlieta 已提交
518
 *
519 520 521
 * @param client current AT client object
 * @param table URC table
 * @param size table size
Lawlieta's avatar
Lawlieta 已提交
522
 */
523
void at_obj_set_urc_table(at_client_t client, const struct at_urc *urc_table, rt_size_t table_sz)
Lawlieta's avatar
Lawlieta 已提交
524
{
525 526
    rt_size_t idx;

527 528 529 530 531 532
    if (client == RT_NULL)
    {
        LOG_E("input AT Client object is NULL, please create or get AT Client object!");
        return;
    }

533 534 535 536 537
    for (idx = 0; idx < table_sz; idx++)
    {
        RT_ASSERT(urc_table[idx].cmd_prefix);
        RT_ASSERT(urc_table[idx].cmd_suffix);
    }
Lawlieta's avatar
Lawlieta 已提交
538

539 540
    client->urc_table = urc_table;
    client->urc_table_size = table_sz;
Lawlieta's avatar
Lawlieta 已提交
541 542 543
}

/**
544
 * get AT client object by AT device name.
Lawlieta's avatar
Lawlieta 已提交
545
 *
546
 * @dev_name AT client device name
Lawlieta's avatar
Lawlieta 已提交
547
 *
548
 * @return AT client object
Lawlieta's avatar
Lawlieta 已提交
549
 */
550
at_client_t at_client_get(const char *dev_name)
Lawlieta's avatar
Lawlieta 已提交
551
{
552
    int idx = 0;
Lawlieta's avatar
Lawlieta 已提交
553

554 555 556 557 558 559 560 561 562 563 564
    RT_ASSERT(dev_name);

    for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
    {
        if (rt_strcmp(at_client_table[idx].device->parent.name, dev_name) == 0)
        {
            return &at_client_table[idx];
        }
    }

    return RT_NULL;
Lawlieta's avatar
Lawlieta 已提交
565 566
}

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
/**
 * get first AT client object in the table.
 *
 * @return AT client object
 */
at_client_t at_client_get_first(void)
{
    if (at_client_table[0].device == RT_NULL)
    {
        return RT_NULL;
    }

    return &at_client_table[0];
}

static const struct at_urc *get_urc_obj(at_client_t client)
Lawlieta's avatar
Lawlieta 已提交
583 584
{
    rt_size_t i, prefix_len, suffix_len;
585 586
    rt_size_t buf_sz;
    char *buffer = RT_NULL;
Lawlieta's avatar
Lawlieta 已提交
587 588 589 590 591 592

    if (client->urc_table == RT_NULL)
    {
        return RT_NULL;
    }

593 594 595
    buffer = client->recv_buffer;
    buf_sz = client->cur_recv_len;

Lawlieta's avatar
Lawlieta 已提交
596 597 598 599
    for (i = 0; i < client->urc_table_size; i++)
    {
        prefix_len = strlen(client->urc_table[i].cmd_prefix);
        suffix_len = strlen(client->urc_table[i].cmd_suffix);
600
        if (buf_sz < prefix_len + suffix_len)
Lawlieta's avatar
Lawlieta 已提交
601 602 603
        {
            continue;
        }
604 605
        if ((prefix_len ? !strncmp(buffer, client->urc_table[i].cmd_prefix, prefix_len) : 1)
                && (suffix_len ? !strncmp(buffer + buf_sz - suffix_len, client->urc_table[i].cmd_suffix, suffix_len) : 1))
Lawlieta's avatar
Lawlieta 已提交
606 607 608 609 610 611 612 613
        {
            return &client->urc_table[i];
        }
    }

    return RT_NULL;
}

614
static int at_recv_readline(at_client_t client)
Lawlieta's avatar
Lawlieta 已提交
615 616 617 618 619
{
    rt_size_t read_len = 0;
    char ch = 0, last_ch = 0;
    rt_bool_t is_full = RT_FALSE;

620
    memset(client->recv_buffer, 0x00, client->recv_bufsz);
Lawlieta's avatar
Lawlieta 已提交
621 622 623 624
    client->cur_recv_len = 0;

    while (1)
    {
625
        at_client_getchar(client, &ch, RT_WAITING_FOREVER);
Lawlieta's avatar
Lawlieta 已提交
626

627
        if (read_len < client->recv_bufsz)
Lawlieta's avatar
Lawlieta 已提交
628 629
        {
            client->recv_buffer[read_len++] = ch;
630
            client->cur_recv_len = read_len;
Lawlieta's avatar
Lawlieta 已提交
631 632 633 634 635 636 637
        }
        else
        {
            is_full = RT_TRUE;
        }

        /* is newline or URC data */
638 639
        if ((ch == '\n' && last_ch == '\r') || (client->end_sign != 0 && ch == client->end_sign)
                || get_urc_obj(client))
Lawlieta's avatar
Lawlieta 已提交
640 641 642
        {
            if (is_full)
            {
643 644
                LOG_E("read line failed. The line data length is out of buffer size(%d)!", client->recv_bufsz);
                memset(client->recv_buffer, 0x00, client->recv_bufsz);
Lawlieta's avatar
Lawlieta 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
                client->cur_recv_len = 0;
                return -RT_EFULL;
            }
            break;
        }
        last_ch = ch;
    }

#ifdef AT_PRINT_RAW_CMD
    at_print_raw_cmd("recvline", client->recv_buffer, read_len);
#endif

    return read_len;
}

static void client_parser(at_client_t client)
{
    int resp_buf_len = 0;
    const struct at_urc *urc;
    rt_size_t line_counts = 0;

    while(1)
    {
668
        if (at_recv_readline(client) > 0)
Lawlieta's avatar
Lawlieta 已提交
669
        {
670
            if ((urc = get_urc_obj(client)) != RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
            {
                /* current receive is request, try to execute related operations */
                if (urc->func != RT_NULL)
                {
                    urc->func(client->recv_buffer, client->cur_recv_len);
                }
            }
            else if (client->resp != RT_NULL)
            {
                /* current receive is response */
                client->recv_buffer[client->cur_recv_len - 1] = '\0';
                if (resp_buf_len + client->cur_recv_len < client->resp->buf_size)
                {
                    /* copy response lines, separated by '\0' */
                    memcpy(client->resp->buf + resp_buf_len, client->recv_buffer, client->cur_recv_len);
                    resp_buf_len += client->cur_recv_len;

                    line_counts++;
                }
                else
                {
                    client->resp_status = AT_RESP_BUFF_FULL;
                    LOG_E("Read response buffer failed. The Response buffer size is out of buffer size(%d)!", client->resp->buf_size);
                }
                /* check response result */
                if (memcmp(client->recv_buffer, AT_RESP_END_OK, strlen(AT_RESP_END_OK)) == 0
                        && client->resp->line_num == 0)
                {
                    /* get the end data by response result, return response state END_OK. */
                    client->resp_status = AT_RESP_OK;
                }
702
                else if (strstr(client->recv_buffer, AT_RESP_END_ERROR)
Lawlieta's avatar
Lawlieta 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
                        || (memcmp(client->recv_buffer, AT_RESP_END_FAIL, strlen(AT_RESP_END_FAIL)) == 0))
                {
                    client->resp_status = AT_RESP_ERROR;
                }
                else if (line_counts == client->resp->line_num && client->resp->line_num)
                {
                    /* get the end data by response line, return response state END_OK.*/
                    client->resp_status = AT_RESP_OK;
                }
                else
                {
                    continue;
                }
                client->resp->line_counts = line_counts;

                client->resp = RT_NULL;
                rt_sem_release(client->resp_notice);
                resp_buf_len = 0, line_counts = 0;
            }
            else
            {
//                log_d("unrecognized line: %.*s", client->cur_recv_len, client->recv_buffer);
            }
        }
    }
}

static rt_err_t at_client_rx_ind(rt_device_t dev, rt_size_t size)
{
732 733 734
    int idx = 0;

    for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
735
    {
736 737 738 739
        if (at_client_table[idx].device == dev && size > 0)
        {
            rt_sem_release(at_client_table[idx].rx_notice);
        }
740 741
    }

Lawlieta's avatar
Lawlieta 已提交
742 743 744
    return RT_EOK;
}

745 746
/* initialize the client object parameters */
static int at_client_para_init(at_client_t client)
Lawlieta's avatar
Lawlieta 已提交
747
{
748 749 750 751
#define AT_CLIENT_LOCK_NAME            "at_c"
#define AT_CLIENT_SEM_NAME             "at_cs"
#define AT_CLIENT_RESP_NAME            "at_cr"
#define AT_CLIENT_THREAD_NAME          "at_clnt"
Lawlieta's avatar
Lawlieta 已提交
752 753

    int result = RT_EOK;
754 755
    static int at_client_num = 0;
    char name[RT_NAME_MAX];
Lawlieta's avatar
Lawlieta 已提交
756

757 758 759 760 761
    client->status = AT_STATUS_UNINITIALIZED;

    client->cur_recv_len = 0;
    client->recv_buffer = (char *) rt_calloc(1, client->recv_bufsz);
    if (client->recv_buffer == RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
762
    {
H
HubretXie 已提交
763
        LOG_E("AT client initialize failed! No memory for receive buffer.");
764 765
        result = -RT_ENOMEM;
        goto __exit;
Lawlieta's avatar
Lawlieta 已提交
766 767
    }

768 769 770
    rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_LOCK_NAME, at_client_num);
    client->lock = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
    if (client->lock == RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
771
    {
772 773
        LOG_E("AT client initialize failed! at_client_recv_lock create failed!");
        result = -RT_ENOMEM;
Lawlieta's avatar
Lawlieta 已提交
774 775 776
        goto __exit;
    }

777 778 779
    rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_SEM_NAME, at_client_num);
    client->rx_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
    if (client->rx_notice == RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
780
    {
781
        LOG_E("AT client initialize failed! at_client_notice semaphore create failed!");
Lawlieta's avatar
Lawlieta 已提交
782 783 784 785
        result = -RT_ENOMEM;
        goto __exit;
    }

786 787 788
    rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_RESP_NAME, at_client_num);
    client->resp_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
    if (client->resp_notice == RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
789
    {
790
        LOG_E("AT client initialize failed! at_client_resp semaphore create failed!");
Lawlieta's avatar
Lawlieta 已提交
791 792 793 794
        result = -RT_ENOMEM;
        goto __exit;
    }

795 796 797 798 799 800 801 802 803 804 805
    client->urc_table = RT_NULL;
    client->urc_table_size = 0;

    rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_THREAD_NAME, at_client_num);
    client->parser = rt_thread_create(name,
                                     (void (*)(void *parameter))client_parser,
                                     client,
                                     1024 + 512,
                                     RT_THREAD_PRIORITY_MAX / 3 - 1,
                                     5);
    if (client->parser == RT_NULL)
Lawlieta's avatar
Lawlieta 已提交
806 807 808 809 810
    {
        result = -RT_ENOMEM;
        goto __exit;
    }

811 812
__exit:
    if (result != RT_EOK)
Lawlieta's avatar
Lawlieta 已提交
813
    {
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
        if (client->lock)
        {
            rt_mutex_delete(client->lock);
        }

        if (client->rx_notice)
        {
            rt_sem_delete(client->rx_notice);
        }

        if (client->resp_notice)
        {
            rt_sem_delete(client->resp_notice);
        }

        if (client->device)
        {
            rt_device_close(client->device);
        }

        if (client->recv_buffer)
        {
            rt_free(client->recv_buffer);
        }

        rt_memset(client, 0x00, sizeof(struct at_client));
    }
    else
    {
        at_client_num++;
    }

    return result;
}

/**
 * AT client initialize.
 *
 * @param dev_name AT client device name
 * @param recv_bufsz the maximum number of receive buffer length
 *
 * @return 0 : initialize success
 *        -1 : initialize failed
 *        -5 : no memory
 */
int at_client_init(const char *dev_name,  rt_size_t recv_bufsz)
{
    int idx = 0;
    int result = RT_EOK;
    rt_err_t open_result = RT_EOK;
    at_client_t client = RT_NULL;

    RT_ASSERT(dev_name);
    RT_ASSERT(recv_bufsz > 0);

    for (idx = 0; idx < AT_CLIENT_NUM_MAX && at_client_table[idx].device; idx++);

    if (idx >= AT_CLIENT_NUM_MAX)
    {
        LOG_E("AT client initialize filed! Check the maximum number(%d) of AT client.", AT_CLIENT_NUM_MAX);
        result = -RT_EFULL;
        goto __exit;
    }

    client = &at_client_table[idx];
    client->recv_bufsz = recv_bufsz;

    /* find and open command device */
    client->device = rt_device_find(dev_name);
    if (client->device)
    {
        RT_ASSERT(client->device->type == RT_Device_Class_Char);
Lawlieta's avatar
Lawlieta 已提交
886

887
        /* using DMA mode first */
888
        open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
889 890 891
        /* using interrupt mode when DMA mode not supported */
        if (open_result == -RT_EIO)
        {
892
            open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
893 894
        }
        RT_ASSERT(open_result == RT_EOK);
Lawlieta's avatar
Lawlieta 已提交
895

896
        rt_device_set_rx_indicate(client->device, at_client_rx_ind);
Lawlieta's avatar
Lawlieta 已提交
897 898 899
    }
    else
    {
900
        LOG_E("AT client initialize failed! Not find the device(%s).", dev_name);
Lawlieta's avatar
Lawlieta 已提交
901 902 903 904
        result = -RT_ERROR;
        goto __exit;
    }

905 906
    result = at_client_para_init(client);
    if (result != RT_EOK)
Lawlieta's avatar
Lawlieta 已提交
907 908 909 910 911
    {
        goto __exit;
    }

__exit:
912
    if (result == RT_EOK)
Lawlieta's avatar
Lawlieta 已提交
913
    {
914
        client->status = AT_STATUS_INITIALIZED;
Lawlieta's avatar
Lawlieta 已提交
915

916
        rt_thread_startup(client->parser);
Lawlieta's avatar
Lawlieta 已提交
917

918
        LOG_I("AT client(V%s) on device %s initialize success.", AT_SW_VERSION, dev_name);
Lawlieta's avatar
Lawlieta 已提交
919 920 921
    }
    else
    {
922
        LOG_E("AT client(V%s) on device %s initialize failed(%d).", AT_SW_VERSION, dev_name, result);
Lawlieta's avatar
Lawlieta 已提交
923 924 925 926
    }

    return result;
}