• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "lnn_node_weight.h"
17 
18 #include <string.h>
19 
20 #include "bus_center_manager.h"
21 #include "lnn_device_info.h"
22 #include "lnn_log.h"
23 #include "softbus_adapter_crypto.h"
24 #include "softbus_error_code.h"
25 
26 #define MAX_WEIGHT_VALUE        1000
27 #define BASE_WEIGHT_PHONE_VALUE 2000
28 #define BASE_WEIGHT_PAD_VALUE   3000
29 #define BASE_WEIGHT_TV_VALUE    8000
30 #define BASE_WEIGHT_AUDIO_VALUE 7000
31 #define BASE_WEIGHT_CAR_VALUE   5000
32 #define BASE_WEIGHT_PC_VALUE    4000
33 
LnnGetLocalWeight(void)34 int32_t LnnGetLocalWeight(void)
35 {
36     static int32_t weight;
37     static bool isGenWeight = false;
38     uint8_t randVal = 0;
39 
40     if (isGenWeight) {
41         return weight;
42     }
43     if (SoftBusGenerateRandomArray(&randVal, sizeof(randVal)) != SOFTBUS_OK) {
44         LNN_LOGE(LNN_BUILDER, "generate random weight fail");
45         return randVal;
46     }
47     weight = (int32_t)((randVal * MAX_WEIGHT_VALUE) / UINT8_MAX);
48     int32_t localDevTypeId = 0;
49     if (LnnGetLocalNumInfo(NUM_KEY_DEV_TYPE_ID, &localDevTypeId) != SOFTBUS_OK) {
50         localDevTypeId = 0;
51     }
52     switch (localDevTypeId) {
53         case TYPE_PHONE_ID:
54             weight += BASE_WEIGHT_PHONE_VALUE;
55             break;
56         case TYPE_PAD_ID:
57             weight += BASE_WEIGHT_PAD_VALUE;
58             break;
59         case TYPE_TV_ID:
60             weight += BASE_WEIGHT_TV_VALUE;
61             break;
62         case TYPE_AUDIO_ID:
63             weight += BASE_WEIGHT_AUDIO_VALUE;
64             break;
65         case TYPE_CAR_ID:
66             weight += BASE_WEIGHT_CAR_VALUE;
67             break;
68         case TYPE_PC_ID:
69             weight += BASE_WEIGHT_PC_VALUE;
70             break;
71         default:
72             break;
73     }
74     LNN_LOGD(LNN_BUILDER, "generate local weight=%{public}d", weight);
75     isGenWeight = true;
76     return weight;
77 }
78 
LnnCompareNodeWeight(int32_t weight1,const char * masterUdid1,int32_t weight2,const char * masterUdid2)79 int32_t LnnCompareNodeWeight(int32_t weight1, const char *masterUdid1, int32_t weight2,
80     const char *masterUdid2)
81 {
82     if (weight1 != weight2) {
83         return weight1 - weight2;
84     }
85     if (masterUdid1 == NULL || masterUdid2 == NULL) {
86         LNN_LOGE(LNN_BUILDER, "nullptr");
87         return weight1 - weight2;
88     }
89     return strcmp(masterUdid1, masterUdid2);
90 }