1/*
2 * Copyright 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#include <poll.h>
18#include <poll_portable.h>
19
20#if POLLWRNORM_PORTABLE==POLLWRNORM
21#error Bad build environment
22#endif
23
24static inline short mips_change_portable_events(short portable_events)
25{
26    /* MIPS has different POLLWRNORM and POLLWRBAND. */
27    if (portable_events & POLLWRNORM_PORTABLE) {
28        portable_events &= ~POLLWRNORM_PORTABLE;
29        portable_events |= POLLWRNORM;
30    }
31    if (portable_events & POLLWRBAND_PORTABLE) {
32        portable_events &= ~POLLWRBAND_PORTABLE;
33        portable_events |= POLLWRBAND;
34    }
35
36    return portable_events;
37}
38
39static inline short change_mips_events(short mips_events)
40{
41    /* MIPS POLLWRNORM equals POLLOUT that is the same as POLLOUT_PORTABLE, so we just update POLLWRBNAD_PORTABLE. */
42    if (mips_events & POLLWRBAND) {
43        mips_events &= ~POLLWRBAND;
44        mips_events |= POLLWRBAND_PORTABLE;
45    }
46
47    return mips_events;
48}
49
50extern int poll(struct pollfd *, nfds_t, long);
51
52int poll_portable(struct pollfd *fds, nfds_t nfds, long timeout)
53{
54  nfds_t i;
55  int ret;
56
57  for (i = 0; i < nfds; i++)
58      fds->events = mips_change_portable_events(fds->events);
59
60  ret = poll(fds, nfds, timeout);
61
62  for (i = 0; i < nfds; i++) {
63      fds->events = change_mips_events(fds->events);
64      fds->revents = change_mips_events(fds->revents);
65  }
66
67  return ret;
68}
69