~aleteoryx/muditaos

ref: master muditaos/module-cellular/at/Cmd.hpp -rw-r--r-- 2.6 KiB
2cd0e472 — Lefucjusz [BH-000] Update Harmony 2.10.0 changelog 2 months 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include "Result.hpp"

#include <at/Constants.hpp>

#include <string>
#include <chrono>

namespace at
{
    namespace cmd
    {
        enum class Modifier
        {
            None,
            Get,
            Set
        };
    }

    /// at::Cmd structure with command, it's timeout and some runtime data
    /// { command, timeout, last : {sent, response, status } }
    struct Cmd
    {
      private:
        std::string cmd;                                     /// command head to run (AT, CLCC etc...)
        std::chrono::milliseconds timeout = default_timeout; /// timeout for this command

      protected:
        cmd::Modifier mod = cmd::Modifier::None; /// modifier responsible to define action we want to perform
        std::string body;                        /// part after command name
      public:
        Cmd() = delete;
        Cmd(std::string cmd, cmd::Modifier mod, std::chrono::milliseconds timeout = default_timeout) noexcept;
        Cmd(std::string cmd, std::chrono::milliseconds timeout = default_timeout) noexcept;
        Cmd(const Cmd &p) noexcept = default;
        Cmd(Cmd &&) noexcept       = default;
        auto operator=(const Cmd &) noexcept -> Cmd & = default;
        auto operator=(Cmd &&) noexcept -> Cmd & = default;
        virtual ~Cmd()                           = default;

        auto setModifier(cmd::Modifier mod) noexcept
        {
            this->mod = mod;
        }

        [[nodiscard]] auto getCmd() const
        {
            switch (mod) {
            case cmd::Modifier::Get:
                return cmd + "?";
            case cmd::Modifier::Set:
                return cmd + "=" + body;
            case cmd::Modifier::None:
                return cmd;
            }
            return cmd;
        }

        [[nodiscard]] auto getTimeout() const noexcept
        {
            return timeout;
        }

        /// not the prettiest, for now it's ok - for commands which modify strings to execute - return copy of command
        /// str
        [[nodiscard]] operator std::string() const
        {
            return getCmd();
        }

        auto operator+(const std::string &val) const -> Cmd
        {
            Cmd tmp = *this;
            tmp.cmd += val;
            return tmp;
        }

      protected:
        [[nodiscard]] static auto parseBase(const Result &base_result) -> Result;
        static void split(const std::string &str, Result &result);
    };
}; // namespace at