• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #ifndef SEC_MESSENGER_UTILS_H
17 #define SEC_MESSENGER_UTILS_H
18 
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <sys/time.h>
22 
23 #include "securec.h"
24 #include "utils_hexstring.h"
25 
26 #include "messenger.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define MAX_TRY_TIMES 512
33 
MaskDeviceIdentity(const char * deviceId,uint32_t length)34 static inline uint32_t MaskDeviceIdentity(const char *deviceId, uint32_t length)
35 {
36 #define MASK_LEN 4U
37 #define SHIFT_LENGTH 8U
38 #define MASK_LOW 0x00ffU
39 #define MASK_HIGH 0xff00U
40     if (deviceId == NULL || length < MASK_LEN) {
41         return 0;
42     }
43 
44     uint16_t maskId = 0;
45     HexStringToByte(deviceId, MASK_LEN, (uint8_t *)&maskId, sizeof(maskId));
46     return ((maskId & MASK_HIGH) >> SHIFT_LENGTH) | ((maskId & MASK_LOW) << SHIFT_LENGTH);
47 }
48 
MessengerSleep(uint32_t seconds)49 static inline void MessengerSleep(uint32_t seconds)
50 {
51     int ret;
52     struct timeval tmv = {
53         .tv_sec = seconds,
54         .tv_usec = 0,
55     };
56     do {
57         ret = select(0, NULL, NULL, NULL, &tmv);
58     } while ((ret == -1) && (errno == EINTR));
59 }
60 
IsSameDevice(const DeviceIdentify * left,const DeviceIdentify * right)61 static inline bool IsSameDevice(const DeviceIdentify *left, const DeviceIdentify *right)
62 {
63     if ((left == NULL) || (right == NULL)) {
64         return false;
65     }
66 
67     if (left->length != right->length) {
68         return false;
69     }
70 
71     if (memcmp(left->identity, right->identity, left->length) != 0) {
72         return false;
73     }
74 
75     return true;
76 }
77 
78 #ifdef __cplusplus
79 }
80 #endif
81 
82 #endif // SEC_MESSENGER_UTILS_H
83