~aleteoryx/muditaos

ref: ceb7d6a3757c5d7e7064f69062d79209cf72daed muditaos/module-services/service-desktop/parser/ParserUtils.hpp -rw-r--r-- 4.6 KiB
ceb7d6a3 — Roman Kubiak [EGD-3506]: unittest fixes 5 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#include <stdint.h>
#include <string>
#include <vector>
#include <log/log.hpp>

namespace parserFSM
{

    // Endpoint type definition
    enum class EndpointType
    {
        invalid = 0,
        deviceInfo,
        update,
        backup,
        restore,
        factory,
        contacts,
        messages,
        calllog
    };

    constexpr int lastEndpoint = static_cast<int>(EndpointType::calllog);
    // Message defs and utils
    namespace message
    {
        constexpr size_t size_length = 9;
        constexpr size_t size_header = size_length + 1;

        constexpr char endpointChar = '#';
        constexpr char rawDataChar  = '$';

        inline void removeHeader(std::string &msg)
        {
            msg.erase(msg.begin(), msg.begin() + size_header);
        }

        inline unsigned long calcPayloadLength(const std::string header)
        {
            try {
                return std::stol(header.substr(1, std::string::npos));
            }
            catch (const std::exception &e) {
                LOG_ERROR("[PARSER FSM] Cant calculate payload length: %s", e.what());
                return 0;
            }
        }

        inline std::string getHeader(const std::string &msg)
        {
            return msg.substr(0, size_header);
        }

        inline void eraseFront(std::string &msg, size_t pos)
        {
            msg.erase(msg.begin(), msg.begin() + pos);
        }
        inline std::string extractPayload(std::string &msg, size_t payloadLength)
        {
            if (msg.size() > payloadLength)
                return msg.substr(0, payloadLength);
            else
                return msg;
        }
    } // namespace message

    namespace http
    {
        /*! Enum class for the HTTP status codes.
         */
        enum class Code
        {
            OK                  = 200,
            BadRequest          = 400,
            InternalServerError = 500
        };

        /*! Enum class for the HTTP methods.
         */
        enum class Method
        {
            get = 1,
            post,
            put,
            del
        };

        bool isMethodValid(uint8_t);
    }; // namespace http

    namespace json
    {
        const inline std::string batteryLevel   = "batteryLevel";
        const inline std::string batteryState   = "batteryState";
        const inline std::string selectedSim    = "selectedSim";
        const inline std::string trayState      = "trayState";
        const inline std::string signalStrength = "signalStrength";
        const inline std::string fsTotal        = "fsTotal";
        const inline std::string fsFreePercent  = "fsFreePercent";
        const inline std::string fsFree         = "fsFree";
        const inline std::string gitRevision    = "gitRevision";
        const inline std::string gitBranch      = "gitBranch";
        const inline std::string gitTag         = "gitTag";
        const inline std::string currentRTCTime = "currentRTCTime";
        const inline std::string updateReady    = "updateReady";
        const inline std::string updateFileList = "updateFileList";
        const inline std::string backupRequest  = "backupRequest";
        const inline std::string backupReady    = "backupReady";
        const inline std::string backupUpload   = "backupUpload";
        const inline std::string restoreRequest = "restoreRequest";
        const inline std::string factoryRequest = "factoryRequest";

        namespace messages
        {
            const inline std::string id           = "id";
            const inline std::string count        = "count";
            const inline std::string offset       = "offset";
            const inline std::string phoneNumber  = "phoneNumber";
            const inline std::string messageBody  = "messageBody";
            const inline std::string isUnread     = "unread";
            const inline std::string contactID    = "contactID";
            const inline std::string date         = "date";
            const inline std::string dateSent     = "dateSent";
            const inline std::string type         = "type";
            const inline std::string threadID     = "threadID";
            const inline std::string msgTemplate  = "template";
            const inline std::string templateText = "text";
            namespace thread
            {
                const inline std::string msgCount       = "msgCount";
                const inline std::string snippet        = "snippet";
                const inline std::string unreadMsgCount = "unreadMsgCount";

            } // namespace thread

        } // namespace messages

    } // namespace json

} // namespace parserFSM