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 #ifndef WIFI_DIRECT_PROTOCOL_H 16 #define WIFI_DIRECT_PROTOCOL_H 17 18 #include "wifi_direct_types.h" 19 #include "data/info_container.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 #define TLV_TAG_SIZE 1 26 #define TLV_LENGTH_SIZE1 1 27 #define TLV_LENGTH_SIZE2 2 28 29 enum WifiDirectProtocolType { 30 WIFI_DIRECT_PROTOCOL_JSON = 0, 31 WIFI_DIRECT_PROTOCOL_TLV = 1, 32 33 WIFI_DIRECT_PROTOCOL_MAX, 34 }; 35 36 struct ProtocolFormat { 37 uint32_t tagSize; 38 uint32_t lengthSize; 39 }; 40 41 struct InfoContainer; 42 #define WIFI_DIRECT_PROTOCOL_BASE \ 43 enum WifiDirectProtocolType (*getType)(void); \ 44 bool (*pack)(struct WifiDirectProtocol *base, struct InfoContainer *container, \ 45 uint8_t **outBuffer, size_t *outSize); \ 46 bool (*setDataSource)(struct WifiDirectProtocol *self, const uint8_t *data, size_t size); \ 47 void (*setFormat)(struct WifiDirectProtocol *self, struct ProtocolFormat *format); \ 48 bool (*unpack)(struct WifiDirectProtocol *base, struct InfoContainer *container); \ 49 \ 50 /* for tlv protocol */ \ 51 bool (*writeData)(struct WifiDirectProtocol *base, struct InfoContainerKeyProperty *keyProperty, \ 52 uint8_t *data, size_t size); \ 53 bool (*readData)(struct WifiDirectProtocol *base, struct InfoContainerKeyProperty *keyProperty, \ 54 uint8_t **data, size_t *size); \ 55 \ 56 /* for json protocol */ \ 57 bool (*writeInt)(struct WifiDirectProtocol *base, struct InfoContainerKeyProperty *keyProperty, int32_t data); \ 58 bool (*writeBoolean)(struct WifiDirectProtocol *base, struct InfoContainerKeyProperty *keyProperty, bool data); \ 59 bool (*writeString)(struct WifiDirectProtocol *base, struct InfoContainerKeyProperty *keyProperty, char *data); \ 60 \ 61 bool (*readInt)(struct WifiDirectProtocol *base, struct InfoContainerKeyProperty *keyProperty, int32_t *data); \ 62 bool (*readBoolen)(struct WifiDirectProtocol *base, struct InfoContainerKeyProperty *keyProperty, bool *data); \ 63 bool (*readString)(struct WifiDirectProtocol *base, struct InfoContainerKeyProperty *keyProperty, char **data); \ 64 \ 65 void (*destructor)(struct WifiDirectProtocol *base); \ 66 struct ProtocolFormat format 67 68 struct WifiDirectProtocol { 69 WIFI_DIRECT_PROTOCOL_BASE; 70 }; 71 72 #ifdef __cplusplus 73 } 74 #endif 75 #endif