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 HID_HOST_MESSAGE_H
17 #define HID_HOST_MESSAGE_H
18 
19 #include <string>
20 
21 #include "hid_host_defines.h"
22 #include "message.h"
23 #include "securec.h"
24 
25 namespace OHOS {
26 namespace bluetooth {
27 class HidHostMessage : public utility::Message {
28 public:
29     explicit HidHostMessage(int what = 0, int arg1 = 0, void *arg2 = nullptr) : utility::Message(what, arg1, arg2)
30     {}
HidHostMessage(const HidHostMessage & src)31     HidHostMessage(const HidHostMessage &src) : utility::Message(src.what_, src.arg1_, src.arg2_),
32         dev_(src.dev_),
33         sendData_(src.sendData_),
34         l2capInfo_(src.l2capInfo_),
35         data_(nullptr),
36         dataLength_(src.dataLength_)
37     {
38         if ((dataLength_ > 0) && (src.data_ != nullptr)) {
39             data_ = std::make_unique<uint8_t[]>(dataLength_);
40             if (memcpy_s(data_.get(), dataLength_, src.data_.get(), dataLength_) != EOK) {
41                 data_.reset(nullptr);
42                 dataLength_ = 0;
43             }
44         } else {
45             data_.reset(nullptr);
46             dataLength_ = 0;
47         }
48     }
49     ~HidHostMessage() = default;
50     std::string dev_ {""};
51     SendHidData sendData_ {};
52     HidHostL2capConnectionInfo l2capInfo_ {};
53     std::unique_ptr<uint8_t[]> data_ = nullptr;
54     int dataLength_ = 0;
55 
56     HidHostMessage operator=(const HidHostMessage &src)
57     {
58         if (this != &src) {
59             dev_ = src.dev_;
60             sendData_ = src.sendData_;
61             l2capInfo_ = src.l2capInfo_;
62             data_ = nullptr;
63             dataLength_ = src.dataLength_;
64 
65             if ((dataLength_ > 0) && (src.data_ != nullptr)) {
66                 data_ = std::make_unique<uint8_t[]>(dataLength_);
67                 if (memcpy_s(data_.get(), dataLength_, src.data_.get(), dataLength_) != EOK) {
68                     data_.reset(nullptr);
69                     dataLength_ = 0;
70                 }
71             } else {
72                 data_.reset(nullptr);
73                 dataLength_ = 0;
74             }
75         }
76         return *this;
77     }
78 };
79 }  // namespace bluetooth
80 }  // namespace OHOS
81 #endif // HID_HOST_MESSAGE_H
82