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 DATA_RECEIVER_H 17 #define DATA_RECEIVER_H 18 19 #include <netinet/icmp6.h> 20 21 #include "netlink_define.h" 22 #include "netsys_event_message.h" 23 #include "wrapper_listener.h" 24 25 namespace OHOS { 26 namespace nmd { 27 class WrapperListener; 28 class DataReceiver { 29 public: 30 using EventCallback = std::function<void(std::shared_ptr<NetsysEventMessage>)>; 31 DataReceiver(int32_t socketFd, int32_t format); 32 DataReceiver() = delete; 33 ~DataReceiver() = default; 34 35 /** 36 * Register callback function to receive event message 37 * @param callback function pointer 38 */ 39 void RegisterCallback(EventCallback callback); 40 41 /** 42 * Start receive event message 43 * @return success or failed 44 */ 45 int32_t Start(); 46 47 /** 48 * Stop receive event message 49 * @return success or failed 50 */ 51 int32_t Stop(); 52 53 private: 54 void StartReceive(int32_t socket); 55 ssize_t ReceiveMessage(bool isRepair, uid_t &uid); 56 int32_t socket_; 57 int32_t format_; 58 std::unique_ptr<WrapperListener> listener_; 59 char buffer_[NetlinkDefine::BUFFER_SIZE] __attribute__((aligned(4))) = {0}; 60 EventCallback callback_; 61 }; 62 } // namespace nmd 63 } // namespace OHOS 64 65 #endif // DATA_RECEIVER_H 66