~aleteoryx/muditaos

ref: b64eb0622b6c5e59eeb2161223e793ba4164ceb9 muditaos/module-services/service-desktop/parser/ParserUtils.hpp -rw-r--r-- 7.8 KiB
b64eb062 — Pawel Olejniczak [EGD-5783] Exclude empty body from response message 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include <log/log.hpp>      // for LOG_ERROR
#include <bits/exception.h> // for exception
#include <cstddef>          // for size_t
#include <string>           // for string, allocator, basic_string, stol
#include <vector>
#include <parser/HttpEnums.hpp>

namespace parserFSM
{

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

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

        inline constexpr auto endpointChar = '#';
        inline constexpr auto 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 json
    {
        inline constexpr auto batteryLevel     = "batteryLevel";
        inline constexpr auto batteryState     = "batteryState";
        inline constexpr auto selectedSim      = "selectedSim";
        inline constexpr auto sim              = "sim";
        inline constexpr auto trayState        = "trayState";
        inline constexpr auto signalStrength   = "signalStrength";
        inline constexpr auto fsTotal          = "fsTotal";
        inline constexpr auto fsFreePercent    = "fsFreePercent";
        inline constexpr auto fsFree           = "fsFree";
        inline constexpr auto gitRevision      = "gitRevision";
        inline constexpr auto gitBranch        = "gitBranch";
        inline constexpr auto gitTag           = "gitTag";
        inline constexpr auto currentRTCTime   = "currentRTCTime";
        inline constexpr auto updateReady      = "updateReady";
        inline constexpr auto updateFileList   = "updateFileList";
        inline constexpr auto restoreRequest   = "restoreRequest";
        inline constexpr auto factoryRequest   = "factoryRequest";
        inline constexpr auto networkStatus    = "networkStatus";
        inline constexpr auto accessTechnology = "accessTechnology";
        inline constexpr auto fileName         = "fileName";
        inline constexpr auto fileSize         = "fileSize";
        inline constexpr auto update           = "update";
        inline constexpr auto updateInfo       = "updateInfo";
        inline constexpr auto updateError      = "updateError";
        inline constexpr auto errorCode        = "errorCode";
        inline constexpr auto statusCode       = "statusCode";
        inline constexpr auto updateHistory    = "updateHistory";
        inline constexpr auto versionString    = "string";
        inline constexpr auto fileExists       = "fileExists";
        inline constexpr auto version          = "version";
        inline constexpr auto task             = "task";
        inline constexpr auto state            = "state";
        inline constexpr auto success          = "success";
        inline constexpr auto request          = "request";
        inline constexpr auto finished         = "finished";
        inline constexpr auto pending          = "pending";
        inline constexpr auto location         = "location";
        namespace filesystem
        {
            inline constexpr auto command = "command";
            namespace commands
            {
                inline constexpr auto upload    = "upload";
                inline constexpr auto rm        = "rm";
                inline constexpr auto download  = "download";
                inline constexpr auto checkFile = "checkFile";
            } // namespace commands
        }     // namespace filesystem

        namespace updateprocess
        {
            inline constexpr auto command       = "command";
            inline constexpr auto updateAborted = "updateAborted";
            namespace commands
            {
                inline constexpr auto abort = "abort";
            } // namespace commands
        }     // namespace updateprocess

        namespace messages
        {
            inline constexpr auto count            = "count";
            inline constexpr auto category         = "category";
            inline constexpr auto categoryMessage  = "message";
            inline constexpr auto categoryThread   = "thread";
            inline constexpr auto categoryTemplate = "template";

            inline constexpr auto limit              = "limit";
            inline constexpr auto offset             = "offset";
            inline constexpr auto totalCount         = "totalCount";
            inline constexpr auto nextPage           = "nextPage";
            inline constexpr auto entries            = "entries";
            inline constexpr auto messageBody        = "messageBody";
            inline constexpr auto messageCount       = "messageCount";
            inline constexpr auto messageID          = "messageID";
            inline constexpr auto messageType        = "messageType";
            inline constexpr auto phoneNumber        = "phoneNumber";
            inline constexpr auto receivedAt         = "receivedAt";
            inline constexpr auto createdAt          = "createdAt";
            inline constexpr auto lastUsedAt         = "lastUsedAt";
            inline constexpr auto lastUpdatedAt      = "lastUpdatedAt";
            inline constexpr auto isUnread           = "isUnread";
            inline constexpr auto contactID          = "contactID";
            inline constexpr auto numberID           = "numberID";
            inline constexpr auto threadID           = "threadID";
            inline constexpr auto messageSnippet     = "messageSnippet";
            inline constexpr auto unreadMessageCount = "unreadMessageCount";
            inline constexpr auto messageTemplate    = "messageTemplate";
            inline constexpr auto templateBody       = "templateBody";
            inline constexpr auto templateID         = "templateID";
        } // namespace messages

        namespace usb
        {
            inline constexpr auto passcode = "passcode";
            inline constexpr auto id       = "uniqueId";
            inline constexpr auto config   = "config";
            inline constexpr auto status   = "usbSecurityStatus";
            inline constexpr auto on       = "on";
            inline constexpr auto off      = "off";
            inline constexpr auto locked   = "locked";
            inline constexpr auto unlocked = "unlocked";
            inline constexpr auto security = "usbSecurity";

        } // namespace usb

    } // namespace json

} // namespace parserFSM