UidRange.cpp revision dc0d578a69cc5a57167a508207e2198590142d51
1/*
2 * Copyright (C) 2016 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#include "android/net/UidRange.h"
18
19#define LOG_TAG "UidRange"
20
21#include <binder/IBinder.h>
22#include <binder/Parcel.h>
23#include <log/log.h>
24#include <utils/Errors.h>
25
26using android::BAD_VALUE;
27using android::NO_ERROR;
28using android::Parcel;
29using android::status_t;
30
31namespace android {
32
33namespace net {
34
35UidRange::UidRange(int32_t start, int32_t stop) {
36    ALOG_ASSERT(start <= stop, "start UID must be less than or equal to stop UID");
37    mStart = start;
38    mStop = stop;
39}
40
41status_t UidRange::writeToParcel(Parcel* parcel) const {
42    /*
43     * Keep implementation in sync with writeToParcel() in
44     * frameworks/base/core/java/android/net/UidRange.java.
45     */
46    if (status_t err = parcel->writeInt32(mStart)) {
47        return err;
48    }
49    if (status_t err = parcel->writeInt32(mStop)) {
50        return err;
51    }
52    return NO_ERROR;
53}
54
55status_t UidRange::readFromParcel(const Parcel* parcel) {
56    /*
57     * Keep implementation in sync with readFromParcel() in
58     * frameworks/base/core/java/android/net/UidRange.java.
59     */
60    if (status_t err = parcel->readInt32(&mStart)) {
61        return err;
62    }
63    if (status_t err = parcel->readInt32(&mStop)) {
64        return err;
65    }
66    if (mStart > mStop) return BAD_VALUE;
67    return NO_ERROR;
68}
69
70void UidRange::setStart(int32_t uid) {
71    if (mStop != -1) {
72        ALOG_ASSERT(uid <= mStop, "start UID must be less than or equal to stop UID");
73    }
74    mStart = uid;
75}
76
77void UidRange::setStop(int32_t uid) {
78    if (mStart != -1) {
79        ALOG_ASSERT(mStart <= uid, "stop UID must be greater than or equal to start UID");
80    }
81    mStop = uid;
82}
83
84int32_t UidRange::getStart() const {
85    return mStart;
86}
87
88int32_t UidRange::getStop() const {
89    return mStop;
90}
91
92uint32_t UidRange::length() const {
93    if (mStart == -1 || mStop == -1) {
94        return 0;
95    }
96    return static_cast<uint32_t>(mStop - mStart + 1);
97}
98
99}  // namespace net
100
101}  // namespace android
102