John Grossman | 0693887 | 2012-03-17 17:05:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 __UTILS_H__ |
| 18 | #define __UTILS_H__ |
| 19 | |
| 20 | #include <netinet/in.h> |
| 21 | |
| 22 | #include <media/stagefright/foundation/ABase.h> |
| 23 | #include <utils/Timers.h> |
| 24 | |
John Grossman | 2921612 | 2012-03-18 16:35:36 -0700 | [diff] [blame^] | 25 | #define IP_PRINTF_HELPER(a) ((a >> 24) & 0xFF), ((a >> 16) & 0xFF), \ |
| 26 | ((a >> 8) & 0xFF), (a & 0xFF) |
| 27 | |
John Grossman | 0693887 | 2012-03-17 17:05:50 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
| 30 | // Definition of a helper class used to track things like when we need to |
| 31 | // transmit a heartbeat, or when we will need to wake up and trim the retry |
| 32 | // buffers. |
| 33 | class Timeout { |
| 34 | public: |
| 35 | Timeout() : mSystemEndTime(0) { } |
| 36 | |
| 37 | // Set a timeout which should occur msec milliseconds from now. |
| 38 | // Negative values will cancel any current timeout; |
| 39 | void setTimeout(int msec); |
| 40 | |
| 41 | // Return the number of milliseconds until the timeout occurs, or -1 if |
| 42 | // no timeout is scheduled. |
| 43 | int msecTillTimeout(nsecs_t nowTime); |
| 44 | int msecTillTimeout() { return msecTillTimeout(systemTime()); } |
| 45 | |
| 46 | private: |
| 47 | // The systemTime() at which the timeout will be complete, or 0 if no |
| 48 | // timeout is currently scheduled. |
| 49 | nsecs_t mSystemEndTime; |
| 50 | |
| 51 | DISALLOW_EVIL_CONSTRUCTORS(Timeout); |
| 52 | }; |
| 53 | |
| 54 | inline bool matchSockaddrs(const struct sockaddr_in* a, |
| 55 | const struct sockaddr_in* b) { |
| 56 | return ((a->sin_family == b->sin_family) && |
| 57 | (a->sin_addr.s_addr == b->sin_addr.s_addr) && |
| 58 | (a->sin_port == b->sin_port)); |
| 59 | } |
| 60 | |
John Grossman | 2921612 | 2012-03-18 16:35:36 -0700 | [diff] [blame^] | 61 | inline bool isMulticastSockaddr(const struct sockaddr_in* sa) { |
| 62 | if (sa->sin_family != AF_INET) |
| 63 | return false; |
| 64 | |
| 65 | uint32_t addr = ntohl(sa->sin_addr.s_addr); |
| 66 | return ((addr & 0xF0000000) == 0xE0000000); |
| 67 | } |
| 68 | |
John Grossman | 0693887 | 2012-03-17 17:05:50 -0700 | [diff] [blame] | 69 | // Return the minimum timeout between a and b where timeouts less than 0 are |
| 70 | // considered to be infinite |
| 71 | inline int minTimeout(int a, int b) { |
| 72 | if (a < 0) { |
| 73 | return b; |
| 74 | } |
| 75 | |
| 76 | if (b < 0) { |
| 77 | return a; |
| 78 | } |
| 79 | |
| 80 | return ((a < b) ? a : b); |
| 81 | } |
| 82 | |
| 83 | } // namespace android |
| 84 | |
| 85 | #endif // __UTILS_H__ |