1 /* 2 * Copyright (C) 2023 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 BASE_PROTOCOL_H 17 #define BASE_PROTOCOL_H 18 19 #include "json_utils.h" 20 #include "string_util.h" 21 22 #define PROTECTED_MSG_MAX_LEN 1024 23 24 typedef struct { 25 Uint8Buff selfMsg; 26 Uint8Buff peerMsg; 27 } ProtectedMsg; 28 29 typedef struct BaseProtocol BaseProtocol; 30 struct BaseProtocol { 31 int32_t name; 32 int32_t curState; 33 int32_t beginState; 34 int32_t finishState; 35 int32_t failState; 36 Uint8Buff sessionKey; 37 ProtectedMsg protectedMsg; 38 int32_t (*start)(BaseProtocol *, CJson **); 39 int32_t (*process)(BaseProtocol *, const CJson *, CJson **); 40 int32_t (*setPsk)(BaseProtocol *, const Uint8Buff *); 41 int32_t (*setSelfProtectedMsg)(BaseProtocol *, const Uint8Buff *); 42 int32_t (*setPeerProtectedMsg)(BaseProtocol *, const Uint8Buff *); 43 int32_t (*getSessionKey)(BaseProtocol *, Uint8Buff *); 44 void (*destroy)(BaseProtocol *); 45 }; 46 47 typedef enum { 48 START_AUTH_EVENT = 0, 49 CLEINT_START_REQ_EVENT, 50 SERVER_START_RSP_EVENT, 51 CLEINT_FINISH_REQ_EVENT, 52 SERVER_FINISH_RSP_EVENT, 53 FAIL_EVENT, 54 UNKNOWN_EVENT, 55 } ProtocolEventEnum; 56 57 typedef enum { 58 CREATE_AS_CLIENT_STATE = 0, 59 CREATE_AS_SERVER_STATE, 60 CLIENT_REQ_STATE, 61 SERVER_RSP_STATE, 62 CLIENT_FINISH_REQ_STATE, 63 /* FINISH STATE */ 64 SERVER_FINISH_STATE, 65 CLIENT_FINISH_STATE, 66 /* FAIL STATE */ 67 FAIL_STATE 68 } ProtocolStateEnum; 69 70 #endif 71