~aleteoryx/muditaos

ref: 61f4ab3ea791753a6f030dd770d4b3498bffe772 muditaos/module-services/service-desktop/parser/ParserUtils.hpp -rw-r--r-- 6.4 KiB
61f4ab3e — Piotr Tański [EGD-5625] Optimize the binary size of the SML state machine 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
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include <stdint.h>         // for uint8_t
#include <log/log.hpp>      // for LOG_ERROR
#include <bits/exception.h> // for exception
#include <stddef.h>         // for size_t
#include <string>           // for string, allocator, basic_string, stol
#include <vector>

namespace parserFSM
{

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

    inline constexpr auto lastEndpoint = static_cast<int>(EndpointType::developerMode);
    // 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 http
    {
        /*! Enum class for the HTTP status codes.
         */
        enum class Code
        {
            OK                  = 200,
            Accepted            = 202,
            BadRequest          = 400,
            NotAcceptable       = 406,
            InternalServerError = 500
        };

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

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

    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 backupRequest    = "backupRequest";
        inline constexpr auto backupReady      = "backupReady";
        inline constexpr auto backupUpload     = "backupUpload";
        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";

        namespace filesystem
        {
            inline constexpr auto command = "command";
            namespace commands
            {
                inline constexpr auto upload   = "upload";
                inline constexpr auto rm       = "rm";
                inline constexpr auto download = "download";
            } // 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 id           = "id";
            inline constexpr auto count        = "count";
            inline constexpr auto offset       = "offset";
            inline constexpr auto phoneNumber  = "phoneNumber";
            inline constexpr auto messageBody  = "messageBody";
            inline constexpr auto isUnread     = "unread";
            inline constexpr auto contactID    = "contactID";
            inline constexpr auto date         = "date";
            inline constexpr auto dateSent     = "dateSent";
            inline constexpr auto type         = "type";
            inline constexpr auto threadID     = "threadID";
            inline constexpr auto msgTemplate  = "template";
            inline constexpr auto templateText = "text";
            namespace thread
            {
                inline constexpr auto msgCount       = "msgCount";
                inline constexpr auto snippet        = "snippet";
                inline constexpr auto unreadMsgCount = "unreadMsgCount";

            } // namespace thread

        } // namespace messages

    } // namespace json

} // namespace parserFSM