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 #include <linux/socket.h> 17 18 #include <common_time/ICommonTimeConfig.h> 19 #include <binder/Parcel.h> 20 21 #include "utils.h" 22 23 namespace android { 24 25 /***** ICommonTimeConfig *****/ 26 27 enum { 28 GET_MASTER_ELECTION_PRIORITY = IBinder::FIRST_CALL_TRANSACTION, 29 SET_MASTER_ELECTION_PRIORITY, 30 GET_MASTER_ELECTION_ENDPOINT, 31 SET_MASTER_ELECTION_ENDPOINT, 32 GET_MASTER_ELECTION_GROUP_ID, 33 SET_MASTER_ELECTION_GROUP_ID, 34 GET_INTERFACE_BINDING, 35 SET_INTERFACE_BINDING, 36 GET_MASTER_ANNOUNCE_INTERVAL, 37 SET_MASTER_ANNOUNCE_INTERVAL, 38 GET_CLIENT_SYNC_INTERVAL, 39 SET_CLIENT_SYNC_INTERVAL, 40 GET_PANIC_THRESHOLD, 41 SET_PANIC_THRESHOLD, 42 GET_AUTO_DISABLE, 43 SET_AUTO_DISABLE, 44 FORCE_NETWORKLESS_MASTER_MODE, 45 }; 46 47 const String16 ICommonTimeConfig::kServiceName("common_time.config"); 48 49 class BpCommonTimeConfig : public BpInterface<ICommonTimeConfig> 50 { 51 public: 52 BpCommonTimeConfig(const sp<IBinder>& impl) 53 : BpInterface<ICommonTimeConfig>(impl) {} 54 55 virtual status_t getMasterElectionPriority(uint8_t *priority) { 56 Parcel data, reply; 57 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 58 status_t status = remote()->transact(GET_MASTER_ELECTION_PRIORITY, 59 data, 60 &reply); 61 if (status == OK) { 62 status = reply.readInt32(); 63 if (status == OK) { 64 *priority = static_cast<uint8_t>(reply.readInt32()); 65 } 66 } 67 68 return status; 69 } 70 71 virtual status_t setMasterElectionPriority(uint8_t priority) { 72 Parcel data, reply; 73 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 74 data.writeInt32(static_cast<int32_t>(priority)); 75 status_t status = remote()->transact(SET_MASTER_ELECTION_PRIORITY, 76 data, 77 &reply); 78 if (status == OK) { 79 status = reply.readInt32(); 80 } 81 82 return status; 83 } 84 85 virtual status_t getMasterElectionEndpoint(struct sockaddr_storage *addr) { 86 Parcel data, reply; 87 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 88 status_t status = remote()->transact(GET_MASTER_ELECTION_ENDPOINT, 89 data, 90 &reply); 91 if (status == OK) { 92 status = reply.readInt32(); 93 if (status == OK) { 94 deserializeSockaddr(&reply, addr); 95 } 96 } 97 98 return status; 99 } 100 101 virtual status_t setMasterElectionEndpoint( 102 const struct sockaddr_storage *addr) { 103 Parcel data, reply; 104 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 105 if (!canSerializeSockaddr(addr)) 106 return BAD_VALUE; 107 if (NULL == addr) { 108 data.writeInt32(0); 109 } else { 110 data.writeInt32(1); 111 serializeSockaddr(&data, addr); 112 } 113 status_t status = remote()->transact(SET_MASTER_ELECTION_ENDPOINT, 114 data, 115 &reply); 116 if (status == OK) { 117 status = reply.readInt32(); 118 } 119 120 return status; 121 } 122 123 virtual status_t getMasterElectionGroupId(uint64_t *id) { 124 Parcel data, reply; 125 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 126 status_t status = remote()->transact(GET_MASTER_ELECTION_GROUP_ID, 127 data, 128 &reply); 129 130 if (status == OK) { 131 status = reply.readInt32(); 132 if (status == OK) { 133 *id = static_cast<uint64_t>(reply.readInt64()); 134 } 135 } 136 137 return status; 138 } 139 140 virtual status_t setMasterElectionGroupId(uint64_t id) { 141 Parcel data, reply; 142 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 143 data.writeInt64(id); 144 status_t status = remote()->transact(SET_MASTER_ELECTION_GROUP_ID, 145 data, 146 &reply); 147 148 if (status == OK) { 149 status = reply.readInt32(); 150 } 151 152 return status; 153 } 154 155 virtual status_t getInterfaceBinding(String16& ifaceName) { 156 Parcel data, reply; 157 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 158 status_t status = remote()->transact(GET_INTERFACE_BINDING, 159 data, 160 &reply); 161 if (status == OK) { 162 status = reply.readInt32(); 163 if (status == OK) { 164 ifaceName = reply.readString16(); 165 } 166 } 167 168 return status; 169 } 170 171 virtual status_t setInterfaceBinding(const String16& ifaceName) { 172 Parcel data, reply; 173 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 174 data.writeString16(ifaceName); 175 status_t status = remote()->transact(SET_INTERFACE_BINDING, 176 data, 177 &reply); 178 if (status == OK) { 179 status = reply.readInt32(); 180 } 181 182 return status; 183 } 184 185 virtual status_t getMasterAnnounceInterval(int *interval) { 186 Parcel data, reply; 187 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 188 status_t status = remote()->transact(GET_MASTER_ANNOUNCE_INTERVAL, 189 data, 190 &reply); 191 if (status == OK) { 192 status = reply.readInt32(); 193 if (status == OK) { 194 *interval = reply.readInt32(); 195 } 196 } 197 198 return status; 199 } 200 201 virtual status_t setMasterAnnounceInterval(int interval) { 202 Parcel data, reply; 203 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 204 data.writeInt32(interval); 205 status_t status = remote()->transact(SET_MASTER_ANNOUNCE_INTERVAL, 206 data, 207 &reply); 208 if (status == OK) { 209 status = reply.readInt32(); 210 } 211 212 return status; 213 } 214 215 virtual status_t getClientSyncInterval(int *interval) { 216 Parcel data, reply; 217 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 218 status_t status = remote()->transact(GET_CLIENT_SYNC_INTERVAL, 219 data, 220 &reply); 221 if (status == OK) { 222 status = reply.readInt32(); 223 if (status == OK) { 224 *interval = reply.readInt32(); 225 } 226 } 227 228 return status; 229 } 230 231 virtual status_t setClientSyncInterval(int interval) { 232 Parcel data, reply; 233 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 234 data.writeInt32(interval); 235 status_t status = remote()->transact(SET_CLIENT_SYNC_INTERVAL, 236 data, 237 &reply); 238 if (status == OK) { 239 status = reply.readInt32(); 240 } 241 242 return status; 243 } 244 245 virtual status_t getPanicThreshold(int *threshold) { 246 Parcel data, reply; 247 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 248 status_t status = remote()->transact(GET_PANIC_THRESHOLD, 249 data, 250 &reply); 251 if (status == OK) { 252 status = reply.readInt32(); 253 if (status == OK) { 254 *threshold = reply.readInt32(); 255 } 256 } 257 258 return status; 259 } 260 261 virtual status_t setPanicThreshold(int threshold) { 262 Parcel data, reply; 263 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 264 data.writeInt32(threshold); 265 status_t status = remote()->transact(SET_PANIC_THRESHOLD, 266 data, 267 &reply); 268 if (status == OK) { 269 status = reply.readInt32(); 270 } 271 272 return status; 273 } 274 275 virtual status_t getAutoDisable(bool *autoDisable) { 276 Parcel data, reply; 277 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 278 status_t status = remote()->transact(GET_AUTO_DISABLE, 279 data, 280 &reply); 281 if (status == OK) { 282 status = reply.readInt32(); 283 if (status == OK) { 284 *autoDisable = (0 != reply.readInt32()); 285 } 286 } 287 288 return status; 289 } 290 291 virtual status_t setAutoDisable(bool autoDisable) { 292 Parcel data, reply; 293 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 294 data.writeInt32(autoDisable ? 1 : 0); 295 status_t status = remote()->transact(SET_AUTO_DISABLE, 296 data, 297 &reply); 298 299 if (status == OK) { 300 status = reply.readInt32(); 301 } 302 303 return status; 304 } 305 306 virtual status_t forceNetworklessMasterMode() { 307 Parcel data, reply; 308 data.writeInterfaceToken(ICommonTimeConfig::getInterfaceDescriptor()); 309 status_t status = remote()->transact(FORCE_NETWORKLESS_MASTER_MODE, 310 data, 311 &reply); 312 313 if (status == OK) { 314 status = reply.readInt32(); 315 } 316 317 return status; 318 } 319 }; 320 321 IMPLEMENT_META_INTERFACE(CommonTimeConfig, "android.os.ICommonTimeConfig"); 322 323 status_t BnCommonTimeConfig::onTransact(uint32_t code, 324 const Parcel& data, 325 Parcel* reply, 326 uint32_t flags) { 327 switch(code) { 328 case GET_MASTER_ELECTION_PRIORITY: { 329 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 330 uint8_t priority; 331 status_t status = getMasterElectionPriority(&priority); 332 reply->writeInt32(status); 333 if (status == OK) { 334 reply->writeInt32(static_cast<int32_t>(priority)); 335 } 336 return OK; 337 } break; 338 339 case SET_MASTER_ELECTION_PRIORITY: { 340 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 341 uint8_t priority = static_cast<uint8_t>(data.readInt32()); 342 status_t status = setMasterElectionPriority(priority); 343 reply->writeInt32(status); 344 return OK; 345 } break; 346 347 case GET_MASTER_ELECTION_ENDPOINT: { 348 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 349 struct sockaddr_storage addr; 350 status_t status = getMasterElectionEndpoint(&addr); 351 352 if ((status == OK) && !canSerializeSockaddr(&addr)) { 353 status = UNKNOWN_ERROR; 354 } 355 356 reply->writeInt32(status); 357 358 if (status == OK) { 359 serializeSockaddr(reply, &addr); 360 } 361 362 return OK; 363 } break; 364 365 case SET_MASTER_ELECTION_ENDPOINT: { 366 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 367 struct sockaddr_storage addr; 368 int hasAddr = data.readInt32(); 369 370 status_t status; 371 if (hasAddr) { 372 deserializeSockaddr(&data, &addr); 373 status = setMasterElectionEndpoint(&addr); 374 } else { 375 status = setMasterElectionEndpoint(&addr); 376 } 377 378 reply->writeInt32(status); 379 return OK; 380 } break; 381 382 case GET_MASTER_ELECTION_GROUP_ID: { 383 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 384 uint64_t id; 385 status_t status = getMasterElectionGroupId(&id); 386 reply->writeInt32(status); 387 if (status == OK) { 388 reply->writeInt64(id); 389 } 390 return OK; 391 } break; 392 393 case SET_MASTER_ELECTION_GROUP_ID: { 394 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 395 uint64_t id = static_cast<uint64_t>(data.readInt64()); 396 status_t status = setMasterElectionGroupId(id); 397 reply->writeInt32(status); 398 return OK; 399 } break; 400 401 case GET_INTERFACE_BINDING: { 402 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 403 String16 ret; 404 status_t status = getInterfaceBinding(ret); 405 reply->writeInt32(status); 406 if (status == OK) { 407 reply->writeString16(ret); 408 } 409 return OK; 410 } break; 411 412 case SET_INTERFACE_BINDING: { 413 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 414 String16 ifaceName; 415 ifaceName = data.readString16(); 416 status_t status = setInterfaceBinding(ifaceName); 417 reply->writeInt32(status); 418 return OK; 419 } break; 420 421 case GET_MASTER_ANNOUNCE_INTERVAL: { 422 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 423 int interval; 424 status_t status = getMasterAnnounceInterval(&interval); 425 reply->writeInt32(status); 426 if (status == OK) { 427 reply->writeInt32(interval); 428 } 429 return OK; 430 } break; 431 432 case SET_MASTER_ANNOUNCE_INTERVAL: { 433 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 434 int interval = data.readInt32(); 435 status_t status = setMasterAnnounceInterval(interval); 436 reply->writeInt32(status); 437 return OK; 438 } break; 439 440 case GET_CLIENT_SYNC_INTERVAL: { 441 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 442 int interval; 443 status_t status = getClientSyncInterval(&interval); 444 reply->writeInt32(status); 445 if (status == OK) { 446 reply->writeInt32(interval); 447 } 448 return OK; 449 } break; 450 451 case SET_CLIENT_SYNC_INTERVAL: { 452 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 453 int interval = data.readInt32(); 454 status_t status = setClientSyncInterval(interval); 455 reply->writeInt32(status); 456 return OK; 457 } break; 458 459 case GET_PANIC_THRESHOLD: { 460 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 461 int threshold; 462 status_t status = getPanicThreshold(&threshold); 463 reply->writeInt32(status); 464 if (status == OK) { 465 reply->writeInt32(threshold); 466 } 467 return OK; 468 } break; 469 470 case SET_PANIC_THRESHOLD: { 471 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 472 int threshold = data.readInt32(); 473 status_t status = setPanicThreshold(threshold); 474 reply->writeInt32(status); 475 return OK; 476 } break; 477 478 case GET_AUTO_DISABLE: { 479 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 480 bool autoDisable; 481 status_t status = getAutoDisable(&autoDisable); 482 reply->writeInt32(status); 483 if (status == OK) { 484 reply->writeInt32(autoDisable ? 1 : 0); 485 } 486 return OK; 487 } break; 488 489 case SET_AUTO_DISABLE: { 490 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 491 bool autoDisable = (0 != data.readInt32()); 492 status_t status = setAutoDisable(autoDisable); 493 reply->writeInt32(status); 494 return OK; 495 } break; 496 497 case FORCE_NETWORKLESS_MASTER_MODE: { 498 CHECK_INTERFACE(ICommonTimeConfig, data, reply); 499 status_t status = forceNetworklessMasterMode(); 500 reply->writeInt32(status); 501 return OK; 502 } break; 503 } 504 return BBinder::onTransact(code, data, reply, flags); 505 } 506 507 }; // namespace android 508 509