parameter.c 8.8 KB
Newer Older
M
Mupceet 已提交
1
/*
M
Mupceet 已提交
2
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
M
Mupceet 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "parameter.h"

#include <stdint.h>
#include <stdlib.h>

#include "param_comm.h"
#include "init_param.h"
M
Mupceet 已提交
23
#include "init_utils.h"
M
Mupceet 已提交
24 25
#include "sysparam_errno.h"
#include "securec.h"
L
laiguizhong 已提交
26
#include "beget_ext.h"
M
Mupceet 已提交
27 28 29

int WaitParameter(const char *key, const char *value, int timeout)
{
L
laiguizhong 已提交
30
    BEGET_CHECK(!(key == NULL || value == NULL), return EC_INVALID);
C
chengjinsong2 已提交
31 32
    int ret = SystemWaitParameter(key, value, timeout);
    return GetSystemError(ret);
M
Mupceet 已提交
33 34 35 36
}

uint32_t FindParameter(const char *key)
{
L
laiguizhong 已提交
37
    BEGET_CHECK(key != NULL, return (uint32_t)(-1));
M
Mupceet 已提交
38 39 40 41 42 43 44 45 46 47 48 49
    uint32_t handle = 0;
    int ret = SystemFindParameter(key, &handle);
    if (ret != 0) {
        return (uint32_t)(-1);
    }
    return handle;
}

uint32_t GetParameterCommitId(uint32_t handle)
{
    uint32_t commitId = 0;
    int ret = SystemGetParameterCommitId(handle, &commitId);
L
laiguizhong 已提交
50
    BEGET_CHECK(ret == 0, return (uint32_t)(-1));
M
Mupceet 已提交
51 52 53 54 55 56 57 58 59
    return commitId;
}

int GetParameterName(uint32_t handle, char *name, uint32_t len)
{
    if (name == NULL) {
        return EC_INVALID;
    }
    int ret = SystemGetParameterName(handle, name, len);
C
chengjinsong2 已提交
60 61 62 63
    if (ret == 0) {
        return strlen(name);
    }
    return GetSystemError(ret);
M
Mupceet 已提交
64 65 66 67 68 69 70 71 72
}

int GetParameterValue(uint32_t handle, char *value, uint32_t len)
{
    if (value == NULL) {
        return EC_INVALID;
    }
    uint32_t size = len;
    int ret = SystemGetParameterValue(handle, value, &size);
C
chengjinsong2 已提交
73 74 75 76
    if (ret == 0) {
        return strlen(value);
    }
    return GetSystemError(ret);
M
Mupceet 已提交
77 78 79 80 81 82 83
}

int GetParameter(const char *key, const char *def, char *value, uint32_t len)
{
    if ((key == NULL) || (value == NULL)) {
        return EC_INVALID;
    }
M
Mupceet 已提交
84
    int ret = GetParameter_(key, def, value, len);
M
Mupceet 已提交
85
    return (ret != 0) ? ret : strlen(value);
M
Mupceet 已提交
86 87 88 89 90 91 92 93
}

int SetParameter(const char *key, const char *value)
{
    if ((key == NULL) || (value == NULL)) {
        return EC_INVALID;
    }
    int ret = SystemSetParameter(key, value);
C
chengjinsong2 已提交
94
    return GetSystemError(ret);
M
Mupceet 已提交
95 96 97 98 99
}

const char *GetDeviceType(void)
{
    static const char *productType = NULL;
C
chengjinsong2 已提交
100
    const char *deviceType = GetProperty("const.product.devicetype", &productType);
101 102 103
    if (deviceType != NULL) {
        return deviceType;
    }
M
Mupceet 已提交
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
    return GetProperty("const.build.characteristics", &productType);
}

const char *GetProductModel(void)
{
    return GetProductModel_();
}

const char *GetManufacture(void)
{
    return GetManufacture_();
}

const char *GetBrand(void)
{
    static const char *productBrand = NULL;
    return GetProperty("const.product.brand", &productBrand);
}

const char *GetMarketName(void)
{
    static const char *marketName = NULL;
    return GetProperty("const.product.name", &marketName);
}

const char *GetProductSeries(void)
{
    static const char *productSeries = NULL;
    return GetProperty("const.build.product", &productSeries);
}

const char *GetSoftwareModel(void)
{
    static const char *softwareModel = NULL;
    return GetProperty("const.software.model", &softwareModel);
}

const char *GetHardwareModel(void)
{
    static const char *hardwareModel = NULL;
    return GetProperty("const.product.hardwareversion", &hardwareModel);
}

const char *GetHardwareProfile(void)
{
    static const char *hardwareProfile = NULL;
    return GetProperty("const.product.hardwareprofile", &hardwareProfile);
}

const char *GetAbiList(void)
{
    static const char *productAbiList = NULL;
    return GetProperty("const.product.cpu.abilist", &productAbiList);
}

const char *GetBootloaderVersion(void)
{
    static const char *productBootloader = NULL;
    return GetProperty("const.product.bootloader.version", &productBootloader);
}

int GetFirstApiVersion(void)
{
    static const char *firstApiVersion = NULL;
    GetProperty("const.product.firstapiversion", &firstApiVersion);
    if (firstApiVersion == NULL) {
        return 0;
    }
    return atoi(firstApiVersion);
}

const char *GetDisplayVersion(void)
{
    static const char *displayVersion = NULL;
    return GetProperty("const.product.software.version", &displayVersion);
}

const char *GetIncrementalVersion(void)
{
    static const char *incrementalVersion = NULL;
    return GetProperty("const.product.incremental.version", &incrementalVersion);
}

M
Mupceet 已提交
187
const char *GetOsReleaseType(void)
M
Mupceet 已提交
188 189 190 191 192
{
    static const char *osReleaseType = NULL;
    return GetProperty("const.ohos.releasetype", &osReleaseType);
}

M
Mupceet 已提交
193
static const char *GetSdkApiVersion_(void)
M
Mupceet 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
    static const char *sdkApiVersion = NULL;
    return GetProperty("const.ohos.apiversion", &sdkApiVersion);
}

const char *GetBuildType(void)
{
    static const char *buildType = NULL;
    return GetProperty("const.product.build.type", &buildType);
}

const char *GetBuildUser(void)
{
    static const char *buildUser = NULL;
    return GetProperty("const.product.build.user", &buildUser);
}

const char *GetBuildHost(void)
{
    static const char *buildHost = NULL;
    return GetProperty("const.product.build.host", &buildHost);
}

const char *GetBuildTime(void)
{
    static const char *buildTime = NULL;
    return GetProperty("const.product.build.date", &buildTime);
}

const char *GetSerial(void)
{
M
Mupceet 已提交
225
    return GetSerial_();
M
Mupceet 已提交
226 227 228 229 230 231 232 233 234 235 236
}

int GetDevUdid(char *udid, int size)
{
    return GetDevUdid_(udid, size);
}

static const char *BuildOSFullName(void)
{
    const char release[] = "Release";
    const char *releaseType = GetOsReleaseType();
L
laiguizhong 已提交
237 238 239 240 241
    const char *fullName = GetFullName_();
    if (fullName == NULL || releaseType == NULL) {
        return NULL;
    }
    if (strncmp(releaseType, release, sizeof(release) - 1) != 0) {
L
laiguizhong 已提交
242 243 244 245
        char *value = calloc(1, OS_FULL_NAME_LEN);
        if (value == NULL) {
            return NULL;
        }
L
laiguizhong 已提交
246
        int length = sprintf_s(value, OS_FULL_NAME_LEN, "%s(%s)", fullName, releaseType);
M
Mupceet 已提交
247
        if (length < 0) {
L
laiguizhong 已提交
248
            free(value);
L
laiguizhong 已提交
249
            return NULL;
M
Mupceet 已提交
250
        }
L
laiguizhong 已提交
251
        return value;
M
Mupceet 已提交
252
    }
L
laiguizhong 已提交
253
    return strdup(fullName);
M
Mupceet 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
}

const char *GetOSFullName(void)
{
    static const char *osFullName = NULL;
    if (osFullName != NULL) {
        return osFullName;
    }
    osFullName = BuildOSFullName();
    if (osFullName == NULL) {
        return EMPTY_STR;
    }
    return osFullName;
}

static const char *BuildVersionId(void)
{
M
Mupceet 已提交
271
    char value[VERSION_ID_MAX_LEN] = {0};
L
laiguizhong 已提交
272 273 274
    if (GetDeviceType() == NULL) {
        return NULL;
    }
M
Mupceet 已提交
275

M
Mupceet 已提交
276
    int len = sprintf_s(value, VERSION_ID_MAX_LEN, "%s/%s/%s/%s/%s/%s/%s/%s/%s/%s",
M
Mupceet 已提交
277 278
        GetDeviceType(), GetManufacture(), GetBrand(), GetProductSeries(),
        GetOSFullName(), GetProductModel(), GetSoftwareModel(),
M
Mupceet 已提交
279
        GetSdkApiVersion_(), GetIncrementalVersion(), GetBuildType());
M
Mupceet 已提交
280
    if (len <= 0) {
L
laiguizhong 已提交
281
        return NULL;
M
Mupceet 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    }
    const char *versionId = strdup(value);
    return versionId;
}

const char *GetVersionId(void)
{
    static const char *ohosVersionId = NULL;
    if (ohosVersionId != NULL) {
        return ohosVersionId;
    }
    ohosVersionId = BuildVersionId();
    if (ohosVersionId == NULL) {
        return EMPTY_STR;
    }
    return ohosVersionId;
}

int GetSdkApiVersion(void)
{
    static const char *sdkApiVersion = NULL;
    GetProperty("const.ohos.apiversion", &sdkApiVersion);
    if (sdkApiVersion == NULL) {
        return 0;
    }
    return atoi(sdkApiVersion);
}

const char *GetSecurityPatchTag(void)
{
    static const char *securityPatchTag = NULL;
    return GetProperty("const.ohos.version.security_patch", &securityPatchTag);
}

const char *GetBuildRootHash(void)
{
    static const char *buildRootHash = NULL;
    return GetProperty("const.ohos.buildroothash", &buildRootHash);
M
Mupceet 已提交
320 321 322 323 324
}

int32_t GetIntParameter(const char *key, int32_t def)
{
    char value[MAX_INT_LEN] = {0};
C
chengjinsong2 已提交
325 326 327
    size_t size = sizeof(value);
    int ret = SystemGetParameter(key, value, &size);
    if (ret != 0) {
M
Mupceet 已提交
328 329
        return def;
    }
C
chengjinsong2 已提交
330

M
Mupceet 已提交
331 332 333 334
    long long int result = 0;
    if (StringToLL(value, &result) != 0) {
        return def;
    }
C
chengjinsong2 已提交
335
    if (result <= INT32_MIN || result >= INT32_MAX) {
M
Mupceet 已提交
336 337 338 339 340 341 342 343
        return def;
    }
    return (int32_t)result;
}

uint32_t GetUintParameter(const char *key, uint32_t def)
{
    char value[MAX_INT_LEN] = {0};
C
chengjinsong2 已提交
344 345 346
    size_t size = sizeof(value);
    int ret = SystemGetParameter(key, value, &size);
    if (ret != 0) {
M
Mupceet 已提交
347 348
        return def;
    }
C
chengjinsong2 已提交
349

M
Mupceet 已提交
350 351 352 353 354 355 356 357
    unsigned long long int result = 0;
    if (StringToULL(value, &result) != 0) {
        return def;
    }
    if (result >= UINT32_MAX) {
        return def;
    }
    return (uint32_t)result;
M
Mupceet 已提交
358
}