~aleteoryx/muditaos

muditaos/module-cellular/test/unittest_ATStream.cpp -rw-r--r-- 3.8 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <string>
#include <catch2/catch.hpp>
#include "modem/ATStream.hpp"
#include "Result.hpp"

TEST_CASE("Channel Test- AT return parser")
{
    SECTION("Parse AT - OK")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("OK", errcode);
        REQUIRE(code == at::Result::Code::OK);
    }

    SECTION("Parse AT - ERROR")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("ERROR", errcode);
        REQUIRE(code == at::Result::Code::ERROR);
    }

    SECTION("Parse AT - +CME ERROR: Valid")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("+CME ERROR: 33", errcode);
        REQUIRE(code == at::Result::Code::CME_ERROR);
        REQUIRE(errcode == 33);
    }

    SECTION("Parse AT - +CMS ERROR: Valid")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("+CMS ERROR: 33", errcode);
        REQUIRE(code == at::Result::Code::CMS_ERROR);
        REQUIRE(errcode == 33);
    }

    SECTION("Parse AT - +CMS ERROR: Invalid")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("+CMS ERROR: ssss", errcode);
        REQUIRE(code == at::Result::Code::PARSING_ERROR);
    }

    SECTION("Parse AT - +CMS ERROR: Invalid empty")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("+CMS ERROR:", errcode);
        REQUIRE(code == at::Result::Code::PARSING_ERROR);
    }

    SECTION("Parse AT - +CME ERROR: Valid")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("+CME ERROR: 33", errcode);
        REQUIRE(code == at::Result::Code::CME_ERROR);
        REQUIRE(errcode == 33);
    }

    SECTION("Parse AT - +CME ERROR: Invalid")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("+CME ERROR: ssss", errcode);
        REQUIRE(code == at::Result::Code::PARSING_ERROR);
    }

    SECTION("Parse AT - +CME ERROR: Invalid empty")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("+CME ERROR:", errcode);
        REQUIRE(code == at::Result::Code::PARSING_ERROR);
    }

    SECTION("Parse AT - Wrong data")
    {
        at::ATStream stream;
        uint32_t errcode = 0;
        auto code        = stream.parseState("+XME ERROR:", errcode);
        REQUIRE(code == at::Result::Code::NONE);
    }

    SECTION("Parse AT, OK on begin, two tokens")
    {
        std::string rawResponse(
            "\r\nOK\r\n\r\n+QPING: 0,\"172.217.20.206\",32,30,255\r\n\r\n+QPING: 0,\"172.217.20.206\",32,31,255\r\n");

        at::ATStream stream(3);
        stream.write(rawResponse);
        auto result = stream.getResult();

        REQUIRE(result.code == at::Result::Code::OK);
        REQUIRE(result.response.size() == 3);
        REQUIRE(result.response[0] == "OK");
        REQUIRE(result.response[1] == "+QPING: 0,\"172.217.20.206\",32,30,255");
        REQUIRE(result.response[2] == "+QPING: 0,\"172.217.20.206\",32,31,255");
    }

    SECTION("Parse AT, OK at end, two tokens")
    {
        std::string rawResponse("\r\n+QDAI: 1,0,0,3,0,1,1,1\r\n\r\nOK\r\n");

        at::ATStream stream(3);
        stream.write(rawResponse);
        auto result = stream.getResult();

        REQUIRE(result.code == at::Result::Code::OK);
        REQUIRE(result.response.size() == 2);
        REQUIRE(result.response[0] == "+QDAI: 1,0,0,3,0,1,1,1");
        REQUIRE(result.response[1] == "OK");
    }
}