~aleteoryx/muditaos

ref: de5b923e81ed56bc845b75b2833cb040fafc3409 muditaos/module-cellular/at/cmd/src/CSCA.cpp -rw-r--r-- 2.3 KiB
de5b923e — DariuszSabala [BH-369] Turned UTF8 into separate library 4 years 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <log.hpp>
#include <memory>
#include <string>
#include <type_traits>
#include <at/cmd/CSCA.hpp>

namespace at
{
    namespace cmd
    {
        CSCA::CSCA(at::cmd::Modifier mod) noexcept : Cmd("AT+CSCA", mod)
        {}

        CSCA::CSCA() noexcept : CSCA(at::cmd::Modifier::None)
        {}

        result::CSCA &CSCA::parse(Result &base_result)
        {
            auto *p = new result::CSCA(base_result);
            result  = std::unique_ptr<result::CSCA>(p);
            // use parent operator bool
            if (p->Result::operator bool()) {
                if (p->response.empty()) {
                    LOG_ERROR("Can't parse - empty response");
                    p->code = result::CSCA::Code::PARSING_ERROR;
                }
                else {
                    auto str = p->response[0];
                    str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
                    auto pos            = str.find(":");
                    auto end            = str.find(",");
                    p->smsCenterAddress = std::string(str, pos + 1, end - pos - 1);
                    p->smsTypeOfAddress = std::string(str, end + 1);
                    if (p->smsCenterAddress.empty() || p->smsTypeOfAddress.empty()) {
                        LOG_ERROR("Can't parse - bad value");
                        p->code = result::CSCA::Code::PARSING_ERROR;
                    }
                }
            }
            return *p;
        }

        void CSCA::set(const utils::PhoneNumber::View &smsCenterAddress, int smsTypeOfAddress)
        {
            body += "\"" + smsCenterAddress.getE164() + "\"," + std::to_string(smsTypeOfAddress);
        }
    } // namespace cmd

    namespace result
    {
        CSCA::CSCA(const Result &that) : Result(that)
        {}

        CSCA::operator bool() const
        {
            if (!Result::operator bool()) {
                return false;
            }
            if (smsCenterAddress.empty() || smsCenterAddress == "\"\"") {
                return false;
            }
            if (smsTypeOfAddress.empty() || smsTypeOfAddress == "\"\"") {
                return false;
            }
            return true;
        }
    } // namespace result
} // namespace at