1 /** 2 * Copyright (c) 2019, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _BASE_TEST_METRICS_LISTENER_H_ 18 #define _BASE_TEST_METRICS_LISTENER_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include <binder/BinderService.h> 24 25 #include "android/net/metrics/BnNetdEventListener.h" 26 27 enum EventFlag : uint32_t { 28 onDnsEvent = 1 << 0, 29 onPrivateDnsValidationEvent = 1 << 1, 30 onConnectEvent = 1 << 2, 31 onWakeupEvent = 1 << 3, 32 onTcpSocketStatsEvent = 1 << 4, 33 onNat64PrefixEvent = 1 << 5, 34 }; 35 36 namespace android { 37 namespace net { 38 namespace metrics { 39 40 class BaseTestMetricsListener : public BnNetdEventListener { 41 public: 42 BaseTestMetricsListener() = default; 43 ~BaseTestMetricsListener() = default; 44 45 // Returns TRUE if the verification was successful. Otherwise, returns FALSE. 46 virtual bool isVerified() = 0; 47 getCv()48 std::condition_variable& getCv() { return mCv; } getCvMutex()49 std::mutex& getCvMutex() { return mCvMutex; } 50 51 android::binder::Status onDnsEvent(int32_t /*netId*/, int32_t /*eventType*/, 52 int32_t /*returnCode*/, int32_t /*latencyMs*/, 53 const std::string& /*hostname*/, 54 const ::std::vector<std::string>& /*ipAddresses*/, 55 int32_t /*ipAddressesCount*/, int32_t /*uid*/) override; 56 android::binder::Status onPrivateDnsValidationEvent(int32_t /*netId*/, 57 const ::android::String16& /*ipAddress*/, 58 const ::android::String16& /*hostname*/, 59 bool /*validated*/) override; 60 android::binder::Status onConnectEvent(int32_t /*netId*/, int32_t /*error*/, 61 int32_t /*latencyMs*/, 62 const ::android::String16& /*ipAddr*/, int32_t /*port*/, 63 int32_t /*uid*/) override; 64 android::binder::Status onWakeupEvent(const ::android::String16& /*prefix*/, int32_t /*uid*/, 65 int32_t /*ethertype*/, int32_t /*ipNextHeader*/, 66 const ::std::vector<uint8_t>& /*dstHw*/, 67 const ::android::String16& /*srcIp*/, 68 const ::android::String16& /*dstIp*/, int32_t /*srcPort*/, 69 int32_t /*dstPort*/, int64_t /*timestampNs*/) override; 70 android::binder::Status onTcpSocketStatsEvent( 71 const ::std::vector<int32_t>& /*networkIds*/, 72 const ::std::vector<int32_t>& /*sentPackets*/, 73 const ::std::vector<int32_t>& /*lostPackets*/, const ::std::vector<int32_t>& /*rttUs*/, 74 const ::std::vector<int32_t>& /*sentAckDiffMs*/) override; 75 android::binder::Status onNat64PrefixEvent(int32_t /*netId*/, bool /*added*/, 76 const ::std::string& /*prefixString*/, 77 int32_t /*prefixLength*/) override; 78 79 private: 80 // The verified event(s) as a bitwise-OR combination of enum EventFlag flags. 81 uint32_t mVerified{}; 82 83 // This lock prevents racing condition between signaling thread(s) and waiting thread(s). 84 std::mutex mCvMutex; 85 86 // Condition variable signaled when notify() is called. 87 std::condition_variable mCv; 88 89 protected: 90 // Notify who is waiting for test results. See also mCvMutex and mCv. 91 void notify(); 92 93 // Get current verified event(s). getVerified()94 uint32_t getVerified() const { return mVerified; } 95 96 // Set the specific event as verified if its verification was successful. 97 void setVerified(EventFlag event); 98 }; 99 100 } // namespace metrics 101 } // namespace net 102 } // namespace android 103 104 #endif // _BASE_TEST_METRICS_LISTENER_H_ 105