blob: 3d895e6f9a94d9916f32e3ff4841b08c47d0965d
1 | /* |
2 | * Copyright (c) 2014 Amlogic, Inc. All rights reserved. |
3 | * |
4 | * This source code is subject to the terms and conditions defined in the |
5 | * file 'LICENSE' which is part of this source code package. |
6 | * |
7 | * Description: |
8 | */ |
9 | |
10 | |
11 | #ifndef NO_USE_SYSWRITE |
12 | #define LOG_TAG "amSystemWrite" |
13 | |
14 | |
15 | #include <binder/Binder.h> |
16 | #include <binder/IServiceManager.h> |
17 | #include <utils/Atomic.h> |
18 | #include <utils/Log.h> |
19 | #include <utils/RefBase.h> |
20 | #include <utils/String8.h> |
21 | #include <utils/String16.h> |
22 | #include <utils/threads.h> |
23 | #include <Amsyswrite.h> |
24 | #include <unistd.h> |
25 | #if ANDROID_PLATFORM_SDK_VERSION >= 21 //5.0 |
26 | #include <systemcontrol/ISystemControlService.h> |
27 | #else |
28 | #include <systemwrite/ISystemWriteService.h> |
29 | #endif |
30 | |
31 | using namespace android; |
32 | |
33 | class DeathNotifier: public IBinder::DeathRecipient |
34 | { |
35 | public: |
36 | DeathNotifier() { |
37 | } |
38 | |
39 | void binderDied(const wp<IBinder>& who) { |
40 | ALOGW("system_write died!"); |
41 | } |
42 | }; |
43 | |
44 | |
45 | #if ANDROID_PLATFORM_SDK_VERSION >= 21 //5.0 |
46 | //used ISystemControlService |
47 | #define SYST_SERVICES_NAME "system_control" |
48 | #else |
49 | //used amSystemWriteService |
50 | #define ISystemControlService ISystemWriteService |
51 | #define SYST_SERVICES_NAME "system_write" |
52 | #endif |
53 | |
54 | static sp<ISystemControlService> amSystemWriteService; |
55 | static sp<DeathNotifier> amDeathNotifier; |
56 | static Mutex amLock; |
57 | static Mutex amgLock; |
58 | |
59 | const sp<ISystemControlService>& getSystemWriteService() |
60 | { |
61 | Mutex::Autolock _l(amgLock); |
62 | if (amSystemWriteService.get() == 0) { |
63 | sp<IServiceManager> sm = defaultServiceManager(); |
64 | #if 0 |
65 | sp<IBinder> binder; |
66 | do { |
67 | binder = sm->getService(String16("system_write")); |
68 | if (binder != 0) |
69 | break; |
70 | ALOGW("SystemWriteService not published, waiting..."); |
71 | usleep(500000); // 0.5 s |
72 | } while(true); |
73 | if (amDeathNotifier == NULL) { |
74 | amDeathNotifier = new DeathNotifier(); |
75 | } |
76 | binder->linkToDeath(amDeathNotifier); |
77 | amSystemWriteService = interface_cast<ISystemWriteService>(binder); |
78 | #endif |
79 | |
80 | |
81 | amSystemWriteService = interface_cast<ISystemControlService>(sm->getService(String16(SYST_SERVICES_NAME))); |
82 | |
83 | } |
84 | ALOGE_IF(amSystemWriteService==0, "no SystemWrite Service!?"); |
85 | |
86 | return amSystemWriteService; |
87 | } |
88 | |
89 | int amSystemWriteGetProperty(const char* key, char* value) |
90 | { |
91 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
92 | if (sws != 0) { |
93 | String16 v; |
94 | if (sws->getProperty(String16(key), v)) { |
95 | strcpy(value, String8(v).string()); |
96 | return 0; |
97 | } |
98 | } |
99 | return -1; |
100 | |
101 | } |
102 | |
103 | int amSystemWriteGetPropertyStr(const char* key, char* def, char* value) |
104 | { |
105 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
106 | if (sws != 0) { |
107 | String16 v; |
108 | String16 d(def); |
109 | sws->getPropertyString(String16(key), v, d); |
110 | strcpy(value, String8(v).string()); |
111 | } |
112 | strcpy(value, def); |
113 | return -1; |
114 | } |
115 | |
116 | int amSystemWriteGetPropertyInt(const char* key, int def) |
117 | { |
118 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
119 | if (sws != 0) { |
120 | return sws->getPropertyInt(String16(key), def); |
121 | } |
122 | return def; |
123 | } |
124 | |
125 | |
126 | long amSystemWriteGetPropertyLong(const char* key, long def) |
127 | { |
128 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
129 | if (sws != 0) { |
130 | return sws->getPropertyLong(String16(key), def); |
131 | } |
132 | return def; |
133 | } |
134 | |
135 | |
136 | int amSystemWriteGetPropertyBool(const char* key, int def) |
137 | { |
138 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
139 | if (sws != 0) { |
140 | if (sws->getPropertyBoolean(String16(key), def)) { |
141 | return 1; |
142 | } else { |
143 | return 0; |
144 | } |
145 | } |
146 | return def; |
147 | |
148 | } |
149 | |
150 | void amSystemWriteSetProperty(const char* key, const char* value) |
151 | { |
152 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
153 | if (sws != 0) { |
154 | sws->setProperty(String16(key), String16(value)); |
155 | } |
156 | } |
157 | |
158 | int amSystemWriteReadSysfs(const char* path, char* value) |
159 | { |
160 | //ALOGD("amSystemWriteReadNumSysfs:%s",path); |
161 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
162 | if (sws != 0) { |
163 | String16 v; |
164 | if (sws->readSysfs(String16(path), v)) { |
165 | strcpy(value, String8(v).string()); |
166 | return 0; |
167 | } |
168 | } |
169 | return -1; |
170 | } |
171 | |
172 | int amSystemWriteReadNumSysfs(const char* path, char* value, int size) |
173 | { |
174 | //ALOGD("amSystemWriteReadNumSysfs:%s",path); |
175 | |
176 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
177 | if (sws != 0 && value != NULL && access(path, 0) != -1) { |
178 | String16 v; |
179 | if (sws->readSysfs(String16(path), v)) { |
180 | if (v.size() != 0) { |
181 | //ALOGD("readSysfs ok:%s,%s,%d", path, String8(v).string(), String8(v).size()); |
182 | memset(value, 0, size); |
183 | if (size <= String8(v).size() + 1) { |
184 | memcpy(value, String8(v).string(), size - 1); |
185 | value[strlen(value)] = '\0'; |
186 | |
187 | } else { |
188 | strcpy(value, String8(v).string()); |
189 | |
190 | } |
191 | return 0; |
192 | } |
193 | } |
194 | |
195 | } |
196 | //ALOGD("[false]amSystemWriteReadNumSysfs%s,",path); |
197 | return -1; |
198 | } |
199 | |
200 | int amSystemWriteWriteSysfs(const char* path, char* value) |
201 | { |
202 | //ALOGD("amSystemWriteWriteSysfs:%s",path); |
203 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
204 | if (sws != 0) { |
205 | String16 v(value); |
206 | if (sws->writeSysfs(String16(path), v)) { |
207 | //ALOGD("writeSysfs ok"); |
208 | return 0; |
209 | } |
210 | } |
211 | //ALOGD("[false]amSystemWriteWriteSysfs%s,",path); |
212 | return -1; |
213 | } |
214 | |
215 | #if ANDROID_PLATFORM_SDK_VERSION >= 21 //5.0 |
216 | extern "C" int amSystemControlSetNativeWindowRect(int x, int y, int w, int h) { |
217 | const sp<ISystemControlService>& sws = getSystemWriteService(); |
218 | if (sws != 0) { |
219 | sws->setNativeWindowRect(x, y, w, h); |
220 | return 0; |
221 | } |
222 | |
223 | return -1; |
224 | } |
225 | #endif |
226 | |
227 | #endif |
228 |