1/*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include "sysversion.h"
16
17#include <stdlib.h>
18#include <string.h>
19
20#include "beget_ext.h"
21#include "param_comm.h"
22#include "securec.h"
23
24/* *
25 * Major(M) version number.
26 */
27static int g_majorVersion = 2;
28
29/* *
30 * Senior(S) version number.
31 */
32static int g_seniorVersion = 2;
33
34/* *
35 * Feature(F) version number.
36 */
37static int g_featureVersion = 0;
38
39/* *
40 * Build(B) version number.
41 */
42static int g_buildVersion = 0;
43
44static void GetVersions(void)
45{
46    static int versionInited = 0;
47    if (versionInited) {
48        return;
49    }
50    const char *fullName = GetFullName_();
51    if (fullName == NULL) {
52        return;
53    }
54    const char *tmp = strstr(fullName, "-");
55    if (tmp == NULL) {
56        return;
57    }
58    tmp++; // skip "-"
59    int ret = sscanf_s(tmp, "%d.%d.%d.%d", &g_majorVersion, &g_seniorVersion, &g_featureVersion, &g_buildVersion);
60    BEGET_LOGV("fullName %s %d.%d.%d.%d ret %d",
61        fullName, g_majorVersion, g_seniorVersion, g_featureVersion, g_buildVersion, ret);
62    if (ret == 4) { // 4 parameters
63        versionInited = 1;
64    }
65}
66
67/* *
68 * Obtains the major (M) version number, which increases with any updates to the overall architecture.
69 * <p>The M version number monotonically increases from 1 to 99.
70 *
71 * @return Returns the M version number.
72 * @since 4
73 */
74int GetMajorVersion(void)
75{
76    GetVersions();
77    return g_majorVersion;
78}
79
80/* *
81 * Obtains the senior (S) version number, which increases with any updates to the partial
82 * architecture or major features.
83 * <p>The S version number monotonically increases from 0 to 99.
84 *
85 * @return Returns the S version number.
86 * @since 4
87 */
88int GetSeniorVersion(void)
89{
90    GetVersions();
91    return g_seniorVersion;
92}
93
94/* *
95 * Obtains the feature (F) version number, which increases with any planned new features.
96 * <p>The F version number monotonically increases from 0 or 1 to 99.
97 *
98 * @return Returns the F version number.
99 * @since 3
100 */
101int GetFeatureVersion(void)
102{
103    GetVersions();
104    return g_featureVersion;
105}
106
107/* *
108 * Obtains the build (B) version number, which increases with each new development build.
109 * <p>The B version number monotonically increases from 0 or 1 to 999.
110 *
111 * @return Returns the B version number.
112 * @since 3
113 */
114int GetBuildVersion(void)
115{
116    GetVersions();
117    return g_buildVersion;
118}