blob: 2e0ebe314b5046549c2d12444d9e538bd1e2d754
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | #include <unistd.h> |
5 | #include <fcntl.h> |
6 | #include <errno.h> |
7 | |
8 | #include "CBlobDeviceE2prom.h" |
9 | #include "../tvconfig/tvconfig.h" |
10 | #include "../tvutils/tvutils.h" |
11 | #include "CTvLog.h" |
12 | |
13 | #define LOG_TAG "CBlobDeviceE2prom" |
14 | |
15 | #define msleep(x) usleep(x*1000) |
16 | |
17 | CBlobDeviceE2prom::CBlobDeviceE2prom() |
18 | { |
19 | |
20 | } |
21 | |
22 | CBlobDeviceE2prom::~CBlobDeviceE2prom() |
23 | { |
24 | } |
25 | |
26 | int CBlobDeviceE2prom::WriteBytes(int offset, int size, unsigned char *buf) |
27 | { |
28 | return 0; |
29 | } |
30 | int CBlobDeviceE2prom::ReadBytes(int offset, int size, unsigned char *buf) |
31 | { |
32 | return 0; |
33 | } |
34 | int CBlobDeviceE2prom::EraseAllData() |
35 | { |
36 | return 0; |
37 | } |
38 | int CBlobDeviceE2prom::InitCheck() |
39 | { |
40 | return 0; |
41 | } |
42 | int CBlobDeviceE2prom::OpenDevice() |
43 | { |
44 | return 0; |
45 | } |
46 | int CBlobDeviceE2prom::CloseDevice() |
47 | { |
48 | return 0; |
49 | } |
50 | //int CBlobDeviceE2prom::InitCheck() { |
51 | /* int i, tmp_dev_total_size = 0, tmp_offset = 0, rw_check_count = 4; |
52 | unsigned char *tmp_buf = NULL; |
53 | unsigned char w_ch = 0, r_ch = 0; |
54 | |
55 | SetWriteProtectStatus (CC_SSM_PROTECT_ON); |
56 | |
57 | if (ValidOperateCheck() < 0) { |
58 | return -1; |
59 | } |
60 | |
61 | tmp_offset = E2P_DEV_RW_TEST_OFFSET; |
62 | |
63 | if (tmp_offset >= 0) { |
64 | for (i = 0; i < rw_check_count; i++) { |
65 | w_ch = 0x5A; |
66 | WriteNBytes(tmp_offset, 1, &w_ch); |
67 | |
68 | r_ch = 0x00; |
69 | ReadNBytes(tmp_offset, 1, &r_ch); |
70 | |
71 | if (r_ch != w_ch) { |
72 | LOGE("%s, Device rw check error, write %d, read %d.\n", "TV", w_ch, r_ch); |
73 | return -1; |
74 | } |
75 | |
76 | w_ch = 0xA5; |
77 | WriteNBytes(tmp_offset, 1, &w_ch); |
78 | |
79 | r_ch = 0x00; |
80 | ReadNBytes(tmp_offset, 1, &r_ch); |
81 | |
82 | if (r_ch != w_ch) { |
83 | LOGE("%s, Device rw check error, write %d, read %d.\n", "TV", w_ch, r_ch); |
84 | return -1; |
85 | } |
86 | } |
87 | } |
88 | |
89 | if (device_buf != NULL) { |
90 | delete device_buf; |
91 | device_buf = NULL; |
92 | } |
93 | |
94 | if (device_use_buffer) { |
95 | tmp_dev_total_size = GetDeviceTotalSize(); |
96 | if (tmp_dev_total_size <= 0) { |
97 | LOGE("%s, Device file size must be more than 0.\n", "TV"); |
98 | return -1; |
99 | } |
100 | |
101 | tmp_buf = new unsigned char[tmp_dev_total_size]; |
102 | if (tmp_buf != NULL) { |
103 | if (ReadNBytes(0, tmp_dev_total_size, tmp_buf) < 0) { |
104 | delete tmp_buf; |
105 | tmp_buf = NULL; |
106 | |
107 | LOGE("%s, Read data from device error when read data to device rw buffer!!!.\n", "TV"); |
108 | return -1; |
109 | } |
110 | |
111 | device_buf = new unsigned char[tmp_dev_total_size]; |
112 | if (device_buf != NULL) { |
113 | memcpy((void *) device_buf, (void *) tmp_buf, tmp_dev_total_size); |
114 | } |
115 | |
116 | delete tmp_buf; |
117 | tmp_buf = NULL; |
118 | } |
119 | } |
120 | */ |
121 | // return 0; |
122 | //} |
123 | |
124 | //int CBlobDeviceE2prom::OpenDevice() { |
125 | /*int device_fd = CC_ERR_FILE_HANDLE; |
126 | |
127 | if (gFilePathBuf == NULL) { |
128 | LOGE("%s, Device file name is NULL.\n", "TV"); |
129 | return CC_ERR_FILE_HANDLE; |
130 | } |
131 | |
132 | device_fd = open(gFilePathBuf, O_RDWR); |
133 | if (device_fd < 0) { |
134 | LOGE("%s, Open device file \"%s\" error: %s.\n", "TV", gFilePathBuf, strerror(errno)); |
135 | return CC_ERR_FILE_HANDLE; |
136 | } |
137 | |
138 | return device_fd;*/ |
139 | // return 0; |
140 | //} |
141 | |
142 | //int CBlobDeviceE2prom::CloseDevice() { |
143 | /*if (*device_fd >= 0) { |
144 | close(*device_fd); |
145 | *device_fd = CC_ERR_FILE_HANDLE; |
146 | }*/ |
147 | |
148 | // return 0; |
149 | //} |
150 | |
151 | /*int CBlobDeviceE2prom::SetWriteProtectStatus(int protect_status) { |
152 | }*/ |
153 | |
154 | /*int CBlobDeviceE2prom::CheckDeviceWrAvaliable(int offset, int len) { |
155 | int tmp_dev_start_offset = 0; |
156 | int tmp_dev_end_offset = 0; |
157 | |
158 | GetDeviceRWStartOffset(&tmp_dev_start_offset); |
159 | if (tmp_dev_start_offset < 0) { |
160 | LOGE("%s, Device file r/w start offset must be greater than or euqal to 0.\n", "TV"); |
161 | return -1; |
162 | } |
163 | |
164 | GetDeviceRWEndOffset(&tmp_dev_end_offset); |
165 | if (tmp_dev_end_offset < 0) { |
166 | LOGE("%s, Device file r/w end offset must be more than 0.\n", "TV"); |
167 | return -1; |
168 | } |
169 | |
170 | if (len <= 0) { |
171 | LOGE("%s, The w/r length should be more than 0.\n", "TV"); |
172 | return -1; |
173 | } |
174 | |
175 | if ((len + tmp_dev_start_offset + offset) > tmp_dev_end_offset + 1) { |
176 | LOGE("%s, w/r would be overflow!!! len = %d, start offset = %d, offset = %d, end offset = %d.\n", "TV", len, tmp_dev_start_offset, offset, tmp_dev_end_offset); |
177 | return -1; |
178 | } |
179 | |
180 | if (ValidOperateCheck() < 0) { |
181 | return -1; |
182 | } |
183 | |
184 | return 0; |
185 | } |
186 | |
187 | /*int CBlobDeviceE2prom::WriteSpecialBytes(int offset, int len, unsigned char data_buf[]) { |
188 | int i = 0, tmp_ret = 0; |
189 | int tmp_dev_w_page_size = 0; |
190 | int tmp_dev_slave_addr = 0; |
191 | int device_fd = CC_ERR_FILE_HANDLE; |
192 | struct i2c_rdwr_ioctl_data ctl_data; |
193 | struct i2c_msg msg; |
194 | unsigned char msg_buf[E2P_MSG_BUF_SIZE]; |
195 | |
196 | //GetDeviceWritePageSize(&tmp_dev_w_page_size); |
197 | //if (len > tmp_dev_w_page_size) { |
198 | // LOGE("%s, The write length should be less than page size(%d).\n", "TV", tmp_dev_w_page_size); |
199 | // return -1; |
200 | //} |
201 | |
202 | if (CheckDeviceWrAvaliable(offset, len) < 0) { |
203 | return -1; |
204 | } |
205 | |
206 | device_fd = OpenDevice(); |
207 | if (device_fd == CC_ERR_FILE_HANDLE) { |
208 | return -1; |
209 | } |
210 | |
211 | memset((void *) msg_buf, 0, E2P_MSG_BUF_SIZE); |
212 | |
213 | msg_buf[0] = (unsigned char) (offset >> 8); |
214 | msg_buf[1] = (unsigned char) (offset & 0x00ff); |
215 | |
216 | memcpy((void *) &msg_buf[2], data_buf, len); |
217 | |
218 | //GetDeviceSlaveAddress(&tmp_dev_slave_addr); |
219 | |
220 | //showboz |
221 | msg.addr = // |
222 | //tmp_dev_slave_addr//0; |
223 | msg.flags = I2C_M_WR; |
224 | msg.len = 2 + len; |
225 | msg.buf = msg_buf; |
226 | ctl_data.nmsgs = 1; |
227 | ctl_data.msgs = &msg; |
228 | |
229 | SetWriteProtectStatus (CC_SSM_PROTECT_OFF); |
230 | |
231 | tmp_ret = ioctl(device_fd, I2C_RDWR, &ctl_data); |
232 | |
233 | SetWriteProtectStatus (CC_SSM_PROTECT_ON); |
234 | |
235 | if (tmp_ret < 0) { |
236 | LOGE("%s, ioctl failed, error %d (%s).\n", "TV", errno, strerror(errno)); |
237 | |
238 | CloseDevice(&device_fd); |
239 | return -1; |
240 | } |
241 | |
242 | if (device_use_buffer && device_buf != NULL) { |
243 | memcpy((void *) (device_buf + offset), (void *) data_buf, len); |
244 | } |
245 | |
246 | CloseDevice(&device_fd); |
247 | |
248 | msleep(10); |
249 | |
250 | return 0; |
251 | }*/ |
252 | |
253 | /*int CBlobDeviceE2prom::ReadSpecialBytes(int offset, int len, unsigned char data_buf[]) { |
254 | int i = 0, tmp_ret = 0; |
255 | int device_fd = CC_ERR_FILE_HANDLE; |
256 | int tmp_dev_slave_addr = 0; |
257 | struct i2c_rdwr_ioctl_data ctl_data; |
258 | struct i2c_msg msg; |
259 | unsigned char msg_buf[E2P_MSG_BUF_SIZE]; |
260 | |
261 | if (CheckDeviceWrAvaliable(offset, len) < 0) { |
262 | return -1; |
263 | } |
264 | |
265 | if (device_use_buffer && device_buf != NULL) { |
266 | memcpy((void *) data_buf, (void *) (device_buf + offset), len); |
267 | return 0; |
268 | } |
269 | |
270 | device_fd = OpenDevice(); |
271 | if (device_fd == CC_ERR_FILE_HANDLE) { |
272 | return -1; |
273 | } |
274 | |
275 | //GetDeviceSlaveAddress(&tmp_dev_slave_addr); |
276 | |
277 | msg_buf[0] = (unsigned char) (offset >> 8); |
278 | msg_buf[1] = (unsigned char) (offset & 0x00ff); |
279 | msg.addr = tmp_dev_slave_addr; |
280 | msg.flags = I2C_M_WR; |
281 | msg.len = 2; |
282 | msg.buf = msg_buf; |
283 | ctl_data.nmsgs = 1; |
284 | ctl_data.msgs = &msg; |
285 | |
286 | tmp_ret = ioctl(device_fd, I2C_RDWR, &ctl_data); |
287 | |
288 | if (tmp_ret < 0) { |
289 | LOGE("%s, ioctl failed(erase), error %d (%s).\n", "TV", errno, strerror(errno)); |
290 | CloseDevice(&device_fd); |
291 | return -1; |
292 | } |
293 | |
294 | msg.addr = tmp_dev_slave_addr; |
295 | msg.flags |= I2C_M_RD; |
296 | msg.len = len; |
297 | msg.buf = data_buf; |
298 | ctl_data.nmsgs = 1; |
299 | ctl_data.msgs = &msg; |
300 | tmp_ret = ioctl(device_fd, I2C_RDWR, &ctl_data); |
301 | if (tmp_ret < 0) { |
302 | LOGE("%s, ioctl failed, error %d (%s).\n", "TV", errno, strerror(errno)); |
303 | CloseDevice(&device_fd); |
304 | return -1; |
305 | } |
306 | |
307 | CloseDevice(&device_fd); |
308 | |
309 | msleep(10); |
310 | |
311 | return 0; |
312 | } |
313 | |
314 | int CTvSettingDeviceE2prom::ValidOperateCheck() { |
315 | int tmp_dev_total_size = 0; |
316 | int tmp_dev_start_offset = 0; |
317 | int tmp_dev_end_offset = 0; |
318 | |
319 | if (gFilePathBuf == NULL) { |
320 | LOGE("%s, Device file name is NULL.\n", "TV"); |
321 | return -1; |
322 | } |
323 | |
324 | if (IsFileExist(gFilePathBuf) < 0) { |
325 | LOGE("%s, Device file is not exist.\n", "TV"); |
326 | return -1; |
327 | } |
328 | |
329 | tmp_dev_total_size = GetDeviceTotalSize(); |
330 | if (tmp_dev_total_size < 0) { |
331 | LOGE("%s, Device file total size must be greater than or euqal to 0.\n", "TV"); |
332 | return -1; |
333 | } |
334 | |
335 | //GetDeviceRWStartOffset(&tmp_dev_start_offset); |
336 | //if (tmp_dev_start_offset < 0) { |
337 | // LOGE("%s, Device file r/w start offset must be greater than or euqal to 0.\n", "TV"); |
338 | // return -1; |
339 | //} |
340 | |
341 | //GetDeviceRWEndOffset(&tmp_dev_end_offset); |
342 | //if (tmp_dev_end_offset < 0) { |
343 | // LOGE("%s, Device file r/w end offset must be more than 0.\n", "TV"); |
344 | // return -1; |
345 | //} |
346 | |
347 | //if (tmp_dev_end_offset - tmp_dev_start_offset + 1 > tmp_dev_total_size) { |
348 | // LOGE("%s, end offset (%d) - start offset (%d) + 1 is more than total size(%d).\n", "TV", tmp_dev_end_offset, tmp_dev_start_offset, tmp_dev_total_size); |
349 | // return -1; |
350 | //} |
351 | |
352 | return 0; |
353 | }*/ |
354 |