1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/types.h>
8 #include <stddef.h>
9 #include <string.h>
10 
11 #include <zephyr/net/buf.h>
12 
13 #include "subsys/bluetooth/host/classic/at.h"
14 
15 #include <zephyr/ztest.h>
16 
17 static struct at_client at;
18 static char buffer[140];
19 
20 NET_BUF_POOL_DEFINE(at_pool, 1, 140, 0, NULL);
21 
22 static const char example_data[] = "\r\n+ABCD:999\r\n";
23 
at_handle(struct at_client * hf_at)24 int at_handle(struct at_client *hf_at)
25 {
26 	uint32_t val;
27 
28 	zassert_equal(at_get_number(hf_at, &val), 0, "Error getting value");
29 
30 	zassert_equal(val, 999, "Invalid value parsed");
31 
32 	return 0;
33 }
34 
at_resp(struct at_client * hf_at,struct net_buf * buf)35 int at_resp(struct at_client *hf_at, struct net_buf *buf)
36 {
37 	int err;
38 
39 	err = at_parse_cmd_input(hf_at, buf, "ABCD", at_handle,
40 				 AT_CMD_TYPE_NORMAL);
41 	zassert_equal(err, 0, "Error parsing CMD input");
42 
43 	return 0;
44 }
45 
46 ZTEST_SUITE(at_tests, NULL, NULL, NULL, NULL, NULL);
47 
ZTEST(at_tests,test_at)48 ZTEST(at_tests, test_at)
49 {
50 	struct net_buf *buf;
51 	int len;
52 
53 	at.buf_max_len = 140U;
54 	at.buf = buffer;
55 
56 	buf = net_buf_alloc(&at_pool, K_FOREVER);
57 	zassert_not_null(buf, "Failed to get buffer");
58 
59 	at_register(&at, at_resp, NULL);
60 	len = strlen(example_data);
61 
62 	zassert_true(net_buf_tailroom(buf) >= len,
63 		    "Allocated buffer is too small");
64 	net_buf_add_mem(buf, example_data, len);
65 
66 	zassert_equal(at_parse_input(&at, buf), 0, "Parsing failed");
67 }
68