1 /* 2 * Copyright (C) 2021 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 #pragma once 18 19 #include <android-base/logging.h> 20 21 #include <chrono> 22 #include <regex> 23 #include <thread> 24 #include <vector> 25 26 namespace aidl::android::hardware::biometrics { 27 28 #define SLEEP_MS(x) \ 29 if (x > 0) std::this_thread::sleep_for(std::chrono::milliseconds(x)) 30 #define BEGIN_OP(x) \ 31 do { \ 32 LOG(INFO) << __func__; \ 33 SLEEP_MS(x); \ 34 } while (0) 35 #define IS_TRUE(x) ((x == "1") || (x == "true")) 36 37 // This is for non-test situations, such as casual cuttlefish users, that don't 38 // set an explicit value. 39 // Some operations (i.e. enroll, authenticate) will be executed in tight loops 40 // by parts of the UI or fail if there is no latency. For example, the 41 // Face settings page constantly runs auth and the enrollment UI uses a 42 // cancel/restart cycle that requires some latency while the activities change. 43 #define DEFAULT_LATENCY 400 44 45 class Util { 46 public: getSystemNanoTime()47 static int64_t getSystemNanoTime() { 48 timespec now; 49 clock_gettime(CLOCK_MONOTONIC, &now); 50 return now.tv_sec * 1000000000LL + now.tv_nsec; 51 } 52 hasElapsed(int64_t start,int64_t durationMillis)53 static bool hasElapsed(int64_t start, int64_t durationMillis) { 54 auto now = getSystemNanoTime(); 55 if (now < start) return true; 56 if (durationMillis <= 0) return true; 57 return ((now - start) / 1000000LL) > durationMillis; 58 } 59 split(const std::string & str,const std::string & sep)60 static std::vector<std::string> split(const std::string& str, const std::string& sep) { 61 std::regex regex(sep); 62 std::vector<std::string> parts( 63 std::sregex_token_iterator(str.begin(), str.end(), regex, -1), 64 std::sregex_token_iterator()); 65 return parts; 66 } 67 }; 68 69 } // namespace aidl::android::hardware::biometrics 70