Projects
Eulaceura:Mainline:GA
distributed-beget
_service:obs_scm:0006-feat-auto-generated-udid-...
Sign Up
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File _service:obs_scm:0006-feat-auto-generated-udid-and-add-new-interfaces.patch of Package distributed-beget
From 75f7f930e9bb433a486d401ceb7fb3371b7676d7 Mon Sep 17 00:00:00 2001 From: peng_langyuan <peng_langyuan@hoperun.com> Date: Tue, 7 May 2024 16:03:58 +0800 Subject: [PATCH] feat-auto-generated-udid-and-add-new-interfaces --- bundle.json | 1 + etc/BUILD.gn | 28 +++++ etc/default.para | 18 +++ interfaces/innerkits/BUILD.gn | 5 + .../innerkits/include/syspara/parameter.h | 2 + interfaces/innerkits/syspara/param_comm.c | 108 ++++++++++++++++-- interfaces/innerkits/syspara/param_comm.h | 2 + interfaces/innerkits/syspara/parameter.c | 64 +++-------- 8 files changed, 169 insertions(+), 59 deletions(-) create mode 100644 etc/BUILD.gn create mode 100644 etc/default.para diff --git a/bundle.json b/bundle.json index 7d22547..ed9d605 100755 --- a/bundle.json +++ b/bundle.json @@ -42,6 +42,7 @@ ] }, "sub_component": [ + "//base/startup/init/etc:startup_etc", "//base/startup/init/services/param_service:param_service" ], "inner_kits": [ diff --git a/etc/BUILD.gn b/etc/BUILD.gn new file mode 100644 index 0000000..8b663dc --- /dev/null +++ b/etc/BUILD.gn @@ -0,0 +1,28 @@ +# Copyright (c) 2023 HopeRun Device Co., Ltd. +# 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. + +import("//build/ohos.gni") + +group("startup_etc") { + deps = [ + ":default.para", + ] +} + +ohos_prebuilt_etc("default.para") { + source = "default.para" + relative_install_dir = "param" + part_name = "init" + subsystem_name = "startup" +} + diff --git a/etc/default.para b/etc/default.para new file mode 100644 index 0000000..680ef43 --- /dev/null +++ b/etc/default.para @@ -0,0 +1,18 @@ +# Copyright (c) 2024 HopeRun Device Co., Ltd. +# 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. + +const.product.devicetype=default +const.product.manufacturer=default +const.product.model=oeos +const.default.account=0 +const.device.name=openEuler diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index a68eaee..65be6f2 100755 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -50,6 +50,11 @@ ohos_shared_library("libbegetutil") { "//base/startup/init/services/utils:libinit_utils", ] deps += [ "//base/startup/init/services/param/base:param_base" ] + + libs = [ + "/usr/lib64/libcrypto.so" + ] + external_deps = [ "c_utils:utils", ] diff --git a/interfaces/innerkits/include/syspara/parameter.h b/interfaces/innerkits/include/syspara/parameter.h index 36dc05e..e688ffd 100755 --- a/interfaces/innerkits/include/syspara/parameter.h +++ b/interfaces/innerkits/include/syspara/parameter.h @@ -119,6 +119,8 @@ int RemoveParameterWatcher(const char *keyPrefix, ParameterChgPtr callback, void const char *GetDeviceType(void); int GetDevUdid(char *udid, int size); +int GetOsAccount(int *id); +int GetDeviceName(char* deviceName, int size); int32_t GetIntParameter(const char *key, int32_t def); uint32_t GetUintParameter(const char *key, uint32_t def); diff --git a/interfaces/innerkits/syspara/param_comm.c b/interfaces/innerkits/syspara/param_comm.c index a05c2ab..c139f4b 100644 --- a/interfaces/innerkits/syspara/param_comm.c +++ b/interfaces/innerkits/syspara/param_comm.c @@ -17,6 +17,7 @@ #include <stdlib.h> #include <string.h> +#include <openssl/sha.h> #include "init_param.h" #ifdef LITEOS_SUPPORT @@ -28,6 +29,30 @@ #include "securec.h" #include "beget_ext.h" +static int GetSha256Value(const char *input, char *udid, int udidSize) +{ + if (input == NULL) { + return EC_FAILURE; + } + + unsigned char hash[SHA256_DIGEST_LENGTH]; + SHA256_CTX ctx; + SHA256_Init(&ctx); + SHA256_Update(&ctx, input, strlen(input)); + SHA256_Final(hash, &ctx); + + char buf[DEV_BUF_LENGTH] = { 0 }; + for (size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) { + unsigned char value = hash[i]; + memset_s(buf, DEV_BUF_LENGTH, 0, DEV_BUF_LENGTH); + int len = sprintf_s(buf, sizeof(buf), "%02X", value); + if (len > 0 && strcat_s(udid, udidSize, buf) != 0) { + return EC_FAILURE; + } + } + return EC_SUCCESS; +} + BEGET_LOCAL_API int GetSystemError(int err) { switch (err) { @@ -124,18 +149,24 @@ BEGET_LOCAL_API const char *GetManufacture_(void) BEGET_LOCAL_API const char *GetSerial_(void) { -#ifdef LITEOS_SUPPORT - return HalGetSerial(); -#else + // A-Z, 0-9随机生成 + char ch[36]; static char *ohosSerial = NULL; + for (int i = 0; i < 26; ++i) { + ch[i] = i + 'A'; + } + for (int i = 26; i < 36; ++i) { + ch[i] = '0' + i - 26; + } if (ohosSerial == NULL) { - BEGET_CHECK((ohosSerial = (char *)calloc(1, PARAM_VALUE_LEN_MAX)) != NULL, return NULL); + ohosSerial = (char *)calloc(1, PARAM_VALUE_LEN_MAX); + } + srand(time(0)); + for (int i = 0; i < 7; ++i) { + *(ohosSerial+i) = ch[rand()%36]; } - uint32_t len = PARAM_VALUE_LEN_MAX; - int ret = SystemGetParameter("ohos.boot.sn", ohosSerial, &len); - BEGET_CHECK(ret == 0, return NULL); return ohosSerial; -#endif + } BEGET_LOCAL_API int GetDevUdid_(char *udid, int size) @@ -145,8 +176,65 @@ BEGET_LOCAL_API int GetDevUdid_(char *udid, int size) } uint32_t len = (uint32_t)size; - int ret = SystemGetParameter("const.product.udid", udid, &len); + int ret = SystemGetParameter("persist.product.udid", udid, &len); BEGET_CHECK(ret != 0, return ret); + const char *manufacture = GetManufacture_(); + const char *model = GetProductModel_(); + const char *sn = GetSerial_(); + if (manufacture == NULL || model == NULL || sn == NULL) { + return -1; + } + int tmpSize = strlen(manufacture) + strlen(model) + strlen(sn) + 1; + if (tmpSize <= 1 || tmpSize > DEV_BUF_MAX_LENGTH) { + return -1; + } + char *tmp = (char *)malloc(tmpSize); + if (tmp == NULL) { + return -1; + } + + (void)memset_s(tmp, tmpSize, 0, tmpSize); + if ((strcat_s(tmp, tmpSize, manufacture) != 0) || (strcat_s(tmp, tmpSize, model) != 0) || + (strcat_s(tmp, tmpSize, sn) != 0)) { + free(tmp); + return -1; + } + + ret = GetSha256Value(tmp, udid, size); + if (ret == EC_SUCCESS) { + SystemSetParameter("persist.product.udid", udid); + } return ret; -} \ No newline at end of file +} + +BEGET_LOCAL_API int GetDeviceName_(char *deviceName, int size) +{ + if (size < 0 || deviceName == NULL) { + return EC_FAILURE; + } + + uint32_t len; + if (size > PARAM_BUFFER_MAX) { + len = PARAM_BUFFER_MAX; + } else { + len = (uint32_t)size; + } + + int ret = SystemGetParameter("const.device.name", deviceName, &len); + BEGET_CHECK(ret == 0, return ret); + + return ret; +} + +BEGET_LOCAL_API int GetOsAccount_(int *id) +{ + char account[10] = {0}; + int len = sizeof(account); + + int ret = SystemGetParameter("const.default.account", account, &len); + BEGET_CHECK(ret == 0, return ret); + + *id = atoi(account); + return ret; +} diff --git a/interfaces/innerkits/syspara/param_comm.h b/interfaces/innerkits/syspara/param_comm.h index b847c74..d3279cd 100755 --- a/interfaces/innerkits/syspara/param_comm.h +++ b/interfaces/innerkits/syspara/param_comm.h @@ -36,6 +36,8 @@ BEGET_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, BEGET_LOCAL_API const char *GetManufacture_(void); BEGET_LOCAL_API const char *GetSerial_(void); BEGET_LOCAL_API int GetDevUdid_(char *udid, int size); +BEGET_LOCAL_API int GetDeviceName_(char *devcieName, int size); +BEGET_LOCAL_API int GetOsAccount_(int *id); BEGET_LOCAL_API int IsValidParamValue(const char *value, uint32_t len); BEGET_LOCAL_API int GetSystemError(int err); #ifdef __cplusplus diff --git a/interfaces/innerkits/syspara/parameter.c b/interfaces/innerkits/syspara/parameter.c index 060f8ce..1132d4a 100644 --- a/interfaces/innerkits/syspara/parameter.c +++ b/interfaces/innerkits/syspara/parameter.c @@ -25,8 +25,6 @@ #include "securec.h" #include "beget_ext.h" -#define SN_LEN 65 -#define SN_FILE "/etc/SN" int WaitParameter(const char *key, const char *value, int timeout) { @@ -55,60 +53,28 @@ int SetParameter(const char *key, const char *value) const char *GetDeviceType(void) { - // static const char *productType = NULL; - // const char *deviceType = GetProperty("const.product.devicetype", &productType); - // if (deviceType != NULL) { - // return deviceType; - // } - // return GetProperty("const.build.characteristics", &productType); - return "UNKNOWN"; + static const char *productType = NULL; + const char *deviceType = GetProperty("const.product.devicetype", &productType); + if (deviceType != NULL) { + return deviceType; + } + return GetProperty("const.build.characteristics", &productType); } -// 暂时使用/etc/SN里面的数字代表udid int GetDevUdid(char *udid, int size) { - FILE *fp; - char *realPath = NULL; - char sn[SN_LEN] = {0}; - // char out[UDID_LEN] = {0}; - int ret; - - realPath = realpath(SN_FILE, NULL); - if (realPath == NULL) { - printf("realpath fail.\n"); - goto err_realpath; - } - - fp = fopen(realPath, "r"); - if (fp == NULL) { - printf("open SN fail.\n"); - goto err_fopen; - } - - ret = fscanf_s(fp, "%s", sn, SN_LEN); - if (ret < 1) { - printf("get sn fail.\n"); - goto err_out; - } - - if (strcpy_s(udid, size, sn) != EOK) { - return 1; - } + return GetDevUdid_(udid, size); +} - fclose(fp); - return 0; -err_out: - fclose(fp); -err_fopen: - free(realPath); -err_realpath: - return -1; +int GetDeviceName(char* deviceName, int size) +{ + return GetDeviceName_(deviceName, size); } -// int GetDevUdid(char *udid, int size) -// { -// return GetDevUdid_(udid, size); -// } +int GetOsAccount(int *id) +{ + return GetOsAccount_(id); +} int32_t GetIntParameter(const char *key, int32_t def) { -- 2.33.0
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.
浙ICP备2022010568号-2