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 #ifndef DMS_TEST_UTILS_PACKET_H 17 #define DMS_TEST_UTILS_PACKET_H 18 19 #include <cstdint> 20 #include <string> 21 22 constexpr int PACKET_DATA_SIZE = 2048; 23 24 enum class MSG_TYPE_SEQ : uint8_t { 25 COMMAND_ID = 1, 26 CALLEE_PKG_NAME, 27 CALLEE_ABILITY_NAME, 28 CALLER_SIGNATURE, 29 }; 30 31 enum class DMSLITE_COMMAND : uint8_t { 32 START_ABILITY_FROM_REMOTE = 1, 33 }; 34 35 struct DmsMsgInfo { 36 uint8_t commandId {0}; 37 std::string calleePkgName; 38 std::string calleeAbilityName; 39 std::string callerSignature; 40 DmsMsgInfoDmsMsgInfo41 DmsMsgInfo(DMSLITE_COMMAND cmdId, std::string pkgName, std::string abilityName, std::string callerSignature) 42 : commandId(static_cast<uint8_t>(cmdId)), calleePkgName(std::move(pkgName)), 43 calleeAbilityName(std::move(abilityName)), callerSignature(std::move(callerSignature)) {} 44 }; 45 46 class DmsPacket { 47 public: 48 DmsPacket(char* buffer, uint16_t bufferSize); 49 virtual ~DmsPacket() = default; 50 51 bool BuildDmsPacket(const DmsMsgInfo& dmsMsgInfo, uint16_t& bufferSize); 52 53 private: 54 bool MarshallStringField(const std::string& field, MSG_TYPE_SEQ type, uint16_t& dataLength); 55 bool MarshallDmsMsgInfo(const DmsMsgInfo& dmsMsgInfo); 56 57 bool StringToHex(const std::string& stringValue); 58 void ToHex(uint64_t value, uint8_t typeSize); 59 uint8_t EncodeLengthOfTlv(uint16_t length); 60 61 template<typename T> 62 typename std::enable_if<std::is_unsigned<T>::value, bool>::type IntToHex(uint64_t integerValue); 63 template<typename T> 64 typename std::enable_if<std::is_signed<T>::value, bool>::type IntToHex(int64_t integerValue); 65 66 private: 67 char* buffer_ {nullptr}; 68 uint16_t bufferCapacity_ {0}; 69 uint16_t counter_ {0}; 70 }; 71 72 #endif 73