~aleteoryx/muditaos

ref: 897c519e167dff6278a51bb56290c45cd14d4396 muditaos/products/BellHybrid/serial-number-parser/SerialNumberParser.cpp -rw-r--r-- 2.6 KiB
897c519e — Lefucjusz [MOS-1044] Fix duplicating phone number in messages app 2 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <serial-number-parser/SerialNumberParser.hpp>
#include <Utils.hpp>
#include <cstring>
#include <map>

/*
 * Old SN is in MMYYWWNNNNNNN form, where:
 * MM - model code, 04 for Harmony;
 * YY - production year;
 * WW - production week;
 * NNNNNNN - unique device number, defined per batch.
 *
 * ID string is a substring consisting of first 6 SN digits.
 *
 *
 * New SN is in MMRRCYYWWNNNN form, where:
 * MM - model code, 04 for Harmony;
 * RR - hardware revision, P1, P2, P3...;
 * C - case colour; G = gray, B = black;
 * YY - production year;
 * WW - production week;
 * NNNN - unique device number, defined per batch.
 *
 */

namespace serial_number_parser
{
    namespace
    {
        /* Old serial number constants */
        constexpr auto idStringOffset = 0;
        constexpr auto idStringLength = 6;

        /* ID string to version metadata map for already released batches */
        const std::map<std::string, VersionMetadata> idStringToVersionInfoMap{
            {"042148", VersionMetadata(grayColor, firstVersion)},
            {"042213", VersionMetadata(grayColor, firstVersion)},
            {"042242", VersionMetadata(blackColor, secondVersion)}};

        /* New serial number constants */
        constexpr auto colourCodeOffset = 4;
        const std::map<char, std::string> colourCodeToColourMap{{'G', grayColor}, {'B', blackColor}};
    } // namespace

    bool isOldSerialNumberFormat(const std::string &serialNumber)
    {
        return (serialNumber.find_first_not_of("0123456789") == std::string::npos);
    }

    std::optional<VersionMetadata> getDeviceVersionMetadata(const std::string &serialNumber)
    {
        if (isOldSerialNumberFormat(serialNumber)) {
            LOG_INFO("Device has old serial number format");
            const auto idString = serialNumber.substr(idStringOffset, idStringLength);
            const auto item     = idStringToVersionInfoMap.find(idString);
            if (item == idStringToVersionInfoMap.end()) {
                return std::nullopt;
            }
            return item->second;
        }
        else {
            LOG_INFO("Device has new serial number format");
            const auto colourCode = serialNumber[colourCodeOffset];
            const auto item       = colourCodeToColourMap.find(colourCode);
            if (item == colourCodeToColourMap.end()) {
                return std::nullopt;
            }

            return VersionMetadata(item->second, secondVersion);
        }
    }
} // namespace serial_number_parser