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 AUTH_COMMON_H 17 #define AUTH_COMMON_H 18 19 #include <stdint.h> 20 #include <stdbool.h> 21 22 #include "auth_interface.h" 23 #include "softbus_common.h" 24 #include "softbus_conn_interface.h" 25 #include "softbus_errcode.h" 26 #include "softbus_log.h" 27 #include "softbus_utils.h" 28 29 #ifdef __cplusplus 30 #if __cplusplus 31 extern "C" { 32 #endif 33 #endif 34 35 typedef enum { 36 /* data type for device authentication */ 37 DATA_TYPE_AUTH = 0xFFFF0001, 38 /* data type for synchronizing peer device information */ 39 DATA_TYPE_DEVICE_INFO = 0xFFFF0002, 40 /* data type for synchronizing peer device id */ 41 DATA_TYPE_DEVICE_ID = 0xFFFF0003, 42 /* data type for connection */ 43 DATA_TYPE_CONNECTION = 0xFFFF0004, 44 /* data type for closing ack */ 45 DATA_TYPE_CLOSE_ACK = 0xFFFF0005, 46 /* data type for meta negotiation */ 47 DATA_TYPE_META_NEGOTIATION = 0xFFFF0006, 48 } AuthDataType; 49 50 #define CLIENT_SIDE_FLAG 0 51 #define SERVER_SIDE_FLAG 1 52 53 #define CHECK_NULL_PTR_RETURN_VOID(item) \ 54 if ((item) == NULL) { \ 55 return; \ 56 } 57 58 #define CHECK_NULL_PTR_RETURN_VALUE(item, value) \ 59 if ((item) == NULL) { \ 60 return value; \ 61 } 62 63 #define CHECK_EXPRESSION_RETURN_VOID(expression) \ 64 if (expression) { \ 65 return; \ 66 } 67 68 #define CHECK_EXPRESSION_RETURN_VALUE(expression, value) \ 69 if (expression) { \ 70 return value; \ 71 } 72 73 #define SEQ_INTERVAL 2 74 #define BYTES_BIT_NUM 8 75 #define INT32_BIT_NUM 32 76 #define INT32_MASK 0xFFFFFFFF 77 #define MASK_UINT64_L32 0x00000000FFFFFFFF 78 #define MASK_UINT64_H32 0xFFFFFFFF00000000 79 80 #define TO_INT32(value) ((int32_t)(((uint32_t)(value)) & INT32_MASK)) 81 #define TO_UINT32(value) ((uint32_t)(((uint32_t)(value)) & INT32_MASK)) 82 83 typedef struct { 84 uint32_t dataType; 85 int32_t module; 86 int64_t seq; 87 int32_t flag; 88 uint32_t len; 89 } AuthDataHead; 90 91 typedef struct { 92 void (*OnDataReceived)(int64_t authId, const AuthDataHead *head, const uint8_t *data, uint32_t len); 93 void (*OnDisconnected)(int64_t authId); 94 } AuthTransCallback; 95 96 /* Auth handler */ 97 typedef enum { 98 EVENT_CONNECT_CMD, 99 EVENT_CONNECT_RESULT, 100 EVENT_CONNECT_TIMEOUT, 101 EVENT_UPDATE_SESSION_KEY, 102 EVENT_AUTH_META_TIMEOUT, 103 EVENT_AUTH_DISCONNECT, 104 } EventType; 105 typedef void(*EventHandler)(const void *obj); 106 int32_t PostAuthEvent(EventType event, EventHandler handler, 107 const void *obj, uint32_t size, uint64_t delayMs); 108 typedef int(*RemoveCompareFunc)(const void *obj, void *param); 109 int32_t RemoveAuthEvent(EventType event, RemoveCompareFunc func, void *param); 110 111 /* Auth Lock */ 112 bool RequireAuthLock(void); 113 void ReleaseAuthLock(void); 114 115 /* auth config */ 116 bool GetConfigSupportAsServer(void); 117 118 /* Common Functions */ 119 uint8_t *DupMemBuffer(const uint8_t *buf, uint32_t size); 120 int64_t GenSeq(bool isServer); 121 uint64_t GetCurrentTimeMs(void); 122 const char *GetAuthSideStr(bool isServer); 123 bool CompareConnInfo(const AuthConnInfo *info1, const AuthConnInfo *info2); 124 int32_t ConvertToConnectOption(const AuthConnInfo *connInfo, ConnectOption *option); 125 int32_t ConvertToAuthConnInfo(const ConnectionInfo *info, AuthConnInfo *connInfo); 126 127 int32_t AuthCommonInit(void); 128 void AuthCommonDeinit(void); 129 130 #ifdef __cplusplus 131 #if __cplusplus 132 } 133 #endif 134 #endif 135 #endif /* AUTH_COMMON_H */ 136