Mike J. Chen | 0ba6214 | 2011-08-15 11:14:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 <aah_timesrv/ICommonClock.h> |
| 18 | #include <binder/Parcel.h> |
| 19 | |
| 20 | namespace android { |
| 21 | |
| 22 | /***** ICommonClock *****/ |
| 23 | |
| 24 | enum { |
| 25 | IS_COMMON_TIME_VALID = IBinder::FIRST_CALL_TRANSACTION, |
| 26 | COMMON_TIME_TO_LOCAL_TIME, |
| 27 | LOCAL_TIME_TO_COMMON_TIME, |
| 28 | GET_COMMON_TIME, |
| 29 | GET_COMMON_FREQ, |
| 30 | GET_LOCAL_TIME, |
| 31 | GET_LOCAL_FREQ, |
| 32 | REGISTER_LISTENER, |
| 33 | UNREGISTER_LISTENER, |
| 34 | }; |
| 35 | |
| 36 | const String16 ICommonClock::kServiceName("aah.common_clock"); |
| 37 | const uint32_t ICommonClock::kInvalidTimelineID = 0; |
| 38 | |
| 39 | class BpCommonClock : public BpInterface<ICommonClock> |
| 40 | { |
| 41 | public: |
| 42 | BpCommonClock(const sp<IBinder>& impl) |
| 43 | : BpInterface<ICommonClock>(impl) {} |
| 44 | |
| 45 | virtual status_t isCommonTimeValid(bool* valid, uint32_t* timelineID) { |
| 46 | Parcel data, reply; |
| 47 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 48 | status_t status = remote()->transact(IS_COMMON_TIME_VALID, |
| 49 | data, |
| 50 | &reply); |
| 51 | if (status == OK) { |
| 52 | *valid = reply.readInt32(); |
| 53 | *timelineID = reply.readInt32(); |
| 54 | } |
| 55 | return status; |
| 56 | } |
| 57 | |
| 58 | virtual status_t commonTimeToLocalTime(int64_t commonTime, |
| 59 | int64_t* localTime) { |
| 60 | Parcel data, reply; |
| 61 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 62 | data.writeInt64(commonTime); |
| 63 | status_t status = remote()->transact(COMMON_TIME_TO_LOCAL_TIME, |
| 64 | data, &reply); |
| 65 | if (status == OK) { |
| 66 | *localTime = reply.readInt64(); |
| 67 | } |
| 68 | return status; |
| 69 | } |
| 70 | |
| 71 | virtual status_t localTimeToCommonTime(int64_t localTime, |
| 72 | int64_t* commonTime) { |
| 73 | Parcel data, reply; |
| 74 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 75 | data.writeInt64(localTime); |
| 76 | status_t status = remote()->transact(LOCAL_TIME_TO_COMMON_TIME, |
| 77 | data, &reply); |
| 78 | if (status == OK) { |
| 79 | *commonTime = reply.readInt64(); |
| 80 | } |
| 81 | return status; |
| 82 | } |
| 83 | |
| 84 | virtual status_t getCommonTime(int64_t* commonTime) { |
| 85 | Parcel data, reply; |
| 86 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 87 | status_t status = remote()->transact(GET_COMMON_TIME, data, &reply); |
| 88 | if (status == OK) { |
| 89 | *commonTime = reply.readInt64(); |
| 90 | } |
| 91 | return status; |
| 92 | } |
| 93 | |
| 94 | virtual status_t getCommonFreq(uint64_t* freq) { |
| 95 | Parcel data, reply; |
| 96 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 97 | status_t status = remote()->transact(GET_COMMON_FREQ, data, &reply); |
| 98 | if (status == OK) { |
| 99 | *freq = reply.readInt64(); |
| 100 | } |
| 101 | return status; |
| 102 | } |
| 103 | |
| 104 | virtual status_t getLocalTime(int64_t* localTime) { |
| 105 | Parcel data, reply; |
| 106 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 107 | status_t status = remote()->transact(GET_LOCAL_TIME, data, &reply); |
| 108 | if (status == OK) { |
| 109 | *localTime = reply.readInt64(); |
| 110 | } |
| 111 | return status; |
| 112 | } |
| 113 | |
| 114 | virtual status_t getLocalFreq(uint64_t* freq) { |
| 115 | Parcel data, reply; |
| 116 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 117 | status_t status = remote()->transact(GET_LOCAL_FREQ, data, &reply); |
| 118 | if (status == OK) { |
| 119 | *freq = reply.readInt64(); |
| 120 | } |
| 121 | return status; |
| 122 | } |
| 123 | |
| 124 | virtual status_t registerListener( |
| 125 | const sp<ICommonClockListener>& listener) { |
| 126 | Parcel data, reply; |
| 127 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 128 | data.writeStrongBinder(listener->asBinder()); |
| 129 | return remote()->transact(REGISTER_LISTENER, data, &reply); |
| 130 | } |
| 131 | |
| 132 | virtual status_t unregisterListener( |
| 133 | const sp<ICommonClockListener>& listener) { |
| 134 | Parcel data, reply; |
| 135 | data.writeInterfaceToken(ICommonClock::getInterfaceDescriptor()); |
| 136 | data.writeStrongBinder(listener->asBinder()); |
| 137 | return remote()->transact(UNREGISTER_LISTENER, data, &reply); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | IMPLEMENT_META_INTERFACE(CommonClock, "android.aah.CommonClock"); |
| 142 | |
| 143 | status_t BnCommonClock::onTransact(uint32_t code, |
| 144 | const Parcel& data, |
| 145 | Parcel* reply, |
| 146 | uint32_t flags) { |
| 147 | switch(code) { |
| 148 | case IS_COMMON_TIME_VALID: { |
| 149 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 150 | bool valid; |
| 151 | uint32_t timelineID; |
| 152 | status_t status = isCommonTimeValid(&valid, &timelineID); |
| 153 | if (status == OK) { |
| 154 | reply->writeInt32(valid); |
| 155 | reply->writeInt32(timelineID); |
| 156 | } |
| 157 | return status; |
| 158 | } break; |
| 159 | |
| 160 | case COMMON_TIME_TO_LOCAL_TIME: { |
| 161 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 162 | int64_t commonTime = data.readInt64(); |
| 163 | int64_t localTime; |
| 164 | status_t status = commonTimeToLocalTime(commonTime, &localTime); |
| 165 | if (status == OK) { |
| 166 | reply->writeInt64(localTime); |
| 167 | } |
| 168 | return status; |
| 169 | } break; |
| 170 | |
| 171 | case LOCAL_TIME_TO_COMMON_TIME: { |
| 172 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 173 | int64_t localTime = data.readInt64(); |
| 174 | int64_t commonTime; |
| 175 | status_t status = localTimeToCommonTime(localTime, &commonTime); |
| 176 | if (status == OK) { |
| 177 | reply->writeInt64(commonTime); |
| 178 | } |
| 179 | return status; |
| 180 | } break; |
| 181 | |
| 182 | case GET_COMMON_TIME: { |
| 183 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 184 | int64_t commonTime; |
| 185 | status_t status = getCommonTime(&commonTime); |
| 186 | if (status == OK) { |
| 187 | reply->writeInt64(commonTime); |
| 188 | } |
| 189 | return status; |
| 190 | } break; |
| 191 | |
| 192 | case GET_COMMON_FREQ: { |
| 193 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 194 | uint64_t freq; |
| 195 | status_t status = getCommonFreq(&freq); |
| 196 | if (status == OK) { |
| 197 | reply->writeInt64(freq); |
| 198 | } |
| 199 | return status; |
| 200 | } break; |
| 201 | |
| 202 | case GET_LOCAL_TIME: { |
| 203 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 204 | int64_t localTime; |
| 205 | status_t status = getLocalTime(&localTime); |
| 206 | if (status == OK) { |
| 207 | reply->writeInt64(localTime); |
| 208 | } |
| 209 | return status; |
| 210 | } break; |
| 211 | |
| 212 | case GET_LOCAL_FREQ: { |
| 213 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 214 | uint64_t freq; |
| 215 | status_t status = getLocalFreq(&freq); |
| 216 | if (status == OK) { |
| 217 | reply->writeInt64(freq); |
| 218 | } |
| 219 | return status; |
| 220 | } break; |
| 221 | |
| 222 | case REGISTER_LISTENER: { |
| 223 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 224 | sp<ICommonClockListener> listener = |
| 225 | interface_cast<ICommonClockListener>(data.readStrongBinder()); |
| 226 | return registerListener(listener); |
| 227 | } break; |
| 228 | |
| 229 | case UNREGISTER_LISTENER: { |
| 230 | CHECK_INTERFACE(ICommonClock, data, reply); |
| 231 | sp<ICommonClockListener> listener = |
| 232 | interface_cast<ICommonClockListener>(data.readStrongBinder()); |
| 233 | return unregisterListener(listener); |
| 234 | } break; |
| 235 | } |
| 236 | return BBinder::onTransact(code, data, reply, flags); |
| 237 | } |
| 238 | |
| 239 | /***** ICommonClockListener *****/ |
| 240 | |
| 241 | enum { |
| 242 | ON_CLOCK_SYNC = IBinder::FIRST_CALL_TRANSACTION, |
| 243 | ON_CLOCK_SYNC_LOSS, |
| 244 | }; |
| 245 | |
| 246 | class BpCommonClockListener : public BpInterface<ICommonClockListener> |
| 247 | { |
| 248 | public: |
| 249 | BpCommonClockListener(const sp<IBinder>& impl) |
| 250 | : BpInterface<ICommonClockListener>(impl) {} |
| 251 | |
| 252 | virtual void onClockSync(uint32_t timelineID) { |
| 253 | Parcel data, reply; |
| 254 | data.writeInterfaceToken( |
| 255 | ICommonClockListener::getInterfaceDescriptor()); |
| 256 | data.writeInt32(timelineID); |
| 257 | remote()->transact(ON_CLOCK_SYNC, data, &reply); |
| 258 | } |
| 259 | |
| 260 | virtual void onClockSyncLoss() { |
| 261 | Parcel data, reply; |
| 262 | data.writeInterfaceToken( |
| 263 | ICommonClockListener::getInterfaceDescriptor()); |
| 264 | remote()->transact(ON_CLOCK_SYNC_LOSS, data, &reply); |
| 265 | } |
| 266 | }; |
| 267 | |
| 268 | IMPLEMENT_META_INTERFACE(CommonClockListener, |
| 269 | "android.aah.CommonClockListener"); |
| 270 | |
| 271 | status_t BnCommonClockListener::onTransact( |
| 272 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 273 | switch(code) { |
| 274 | case ON_CLOCK_SYNC: { |
| 275 | CHECK_INTERFACE(ICommonClockListener, data, reply); |
| 276 | uint32_t timelineID = data.readInt32(); |
| 277 | onClockSync(timelineID); |
| 278 | return NO_ERROR; |
| 279 | } break; |
| 280 | |
| 281 | case ON_CLOCK_SYNC_LOSS: { |
| 282 | CHECK_INTERFACE(ICommonClockListener, data, reply); |
| 283 | onClockSyncLoss(); |
| 284 | return NO_ERROR; |
| 285 | } break; |
| 286 | } |
| 287 | |
| 288 | return BBinder::onTransact(code, data, reply, flags); |
| 289 | } |
| 290 | |
| 291 | }; // namespace android |