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 
19 NET_BUF_POOL_DEFINE(at_pool, 1, 140, 0, NULL);
20 
21 static const char example_data[] = "\r\n+ABCD:999\r\n";
22 
at_handle(struct at_client * hf_at)23 int at_handle(struct at_client *hf_at)
24 {
25 	uint32_t val;
26 
27 	zassert_equal(at_get_number(hf_at, &val), 0, "Error getting value");
28 
29 	zassert_equal(val, 999, "Invalid value parsed");
30 
31 	return 0;
32 }
33 
at_resp(struct at_client * hf_at,struct net_buf * buf)34 int at_resp(struct at_client *hf_at, struct net_buf *buf)
35 {
36 	int err;
37 
38 	err = at_parse_cmd_input(hf_at, buf, "ABCD", at_handle,
39 				 AT_CMD_TYPE_NORMAL);
40 	zassert_equal(err, 0, "Error parsing CMD input");
41 
42 	return 0;
43 }
44 
45 ZTEST_SUITE(at_tests, NULL, NULL, NULL, NULL, NULL);
46 
ZTEST(at_tests,test_at)47 ZTEST(at_tests, test_at)
48 {
49 	struct net_buf *buf;
50 	int len;
51 
52 	buf = net_buf_alloc(&at_pool, K_FOREVER);
53 	zassert_not_null(buf, "Failed to get buffer");
54 
55 	at_register(&at, at_resp, NULL);
56 	len = strlen(example_data);
57 
58 	zassert_true(net_buf_tailroom(buf) >= len,
59 		    "Allocated buffer is too small");
60 	net_buf_add_mem(buf, example_data, len);
61 
62 	zassert_equal(at_parse_input(&at, buf), 0, "Parsing failed");
63 }
64