blob: 9e003242605d6ebb4a787be4fc08ddac841277ce
1 | #define LOG_TAG "bridge" |
2 | #include <android/log.h> |
3 | #include <cutils/properties.h> |
4 | #include <binder/Binder.h> |
5 | #include <binder/IServiceManager.h> |
6 | #include <utils/Log.h> |
7 | #include <utils/RefBase.h> |
8 | #include <utils/String16.h> |
9 | #include <utils/String8.h> |
10 | #include <utils/threads.h> |
11 | #include <systemcontrol/ISystemControlService.h> |
12 | |
13 | #define SYST_SERVICES_NAME "system_control" |
14 | |
15 | |
16 | using namespace android; |
17 | extern "C" int amSystemWriteSetProperty(const char* key, const char* value, int lengthval); |
18 | extern "C" int amSystemWriteGetProperty(const char* key, char* value); |
19 | class DeathNotifier: public IBinder::DeathRecipient |
20 | { |
21 | public: |
22 | DeathNotifier() { |
23 | } |
24 | |
25 | void binderDied(const wp<IBinder>& who) { |
26 | ALOGW("system_write died!"); |
27 | } |
28 | }; |
29 | static sp<ISystemControlService> amSystemWriteService; |
30 | static sp<DeathNotifier> amDeathNotifier; |
31 | static Mutex amgLock; |
32 | |
33 | const sp<ISystemControlService>& getSystemWriteService() |
34 | { |
35 | Mutex::Autolock _l(amgLock); |
36 | if (amSystemWriteService.get() == 0) { |
37 | sp<IServiceManager> sm = defaultServiceManager(); |
38 | #if 0 |
39 | sp<IBinder> binder; |
40 | do { |
41 | binder = sm->getService(String16("system_write")); |
42 | if (binder != 0) |
43 | break; |
44 | ALOGW("SystemWriteService not published, waiting..."); |
45 | usleep(500000); // 0.5 s |
46 | } while(true); |
47 | if (amDeathNotifier == NULL) { |
48 | amDeathNotifier = new DeathNotifier(); |
49 | } |
50 | binder->linkToDeath(amDeathNotifier); |
51 | amSystemWriteService = interface_cast<ISystemWriteService>(binder); |
52 | #endif |
53 | |
54 | |
55 | amSystemWriteService = interface_cast<ISystemControlService>(sm->getService(String16(SYST_SERVICES_NAME))); |
56 | |
57 | } |
58 | ALOGE_IF(amSystemWriteService==0, "no SystemWrite Service!?"); |
59 | |
60 | return amSystemWriteService; |
61 | } |
62 | int amSystemWriteSetProperty(const char* key, const char* value, int leth) |
63 | { |
64 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
65 | char *buf = new char[leth+1]; |
66 | strncpy(buf, value, leth); |
67 | if (sws != 0) { |
68 | sws->writeSysfs(String16(key), String16(buf)); |
69 | delete[] buf; |
70 | ALOGE("write value %s %s\n",key,buf); |
71 | return 0; |
72 | } |
73 | delete[] buf; |
74 | return -1; |
75 | } |
76 | int amSystemWriteGetProperty(const char* key, char* value) |
77 | { |
78 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
79 | if (sws != 0) { |
80 | String16 read_value; |
81 | sws->readSysfs(String16(key), read_value); |
82 | String8 name8 = String8(read_value); |
83 | const char *C_name8 = (char *)name8.string(); |
84 | strcpy(value,C_name8); |
85 | ALOGE("read_value(%s) %s %d;\n",key,value,strlen(value)); |
86 | return strlen(value); |
87 | }else{ |
88 | return -1; |
89 | } |
90 | } |