blob: 5ed41106230db060c159246f1c31b59c7d3e7cbc
1 | #include <stdio.h> |
2 | #include <unistd.h> |
3 | #include <string.h> |
4 | #include <fcntl.h> |
5 | #include <errno.h> |
6 | #include <android/log.h> |
7 | |
8 | #include "ssm_api.h" |
9 | |
10 | #define LOG_TAG "ssm_test" |
11 | #include "CTvLog.h" |
12 | |
13 | // The follow is R/W test struct declare. |
14 | // The size of it is 11 when it aligned with 1 Byte |
15 | // and is 16 when it aligned with 4 Bytes. |
16 | // You should use the 1 Byte aligned mode when R/W ssm by using struct. |
17 | |
18 | #if 1 // memory aligned with 1 Bytes |
19 | typedef struct tagS_TEST_STRUCT { |
20 | char tmp_ch0; |
21 | char tmp_ch1; |
22 | int tmp_val0; |
23 | unsigned char tmp_ch2; |
24 | unsigned char tmp_ch3; |
25 | unsigned char tmp_ch4; |
26 | short tmp_short0; |
27 | } __attribute__((packed)) S_TEST_STRUCT; |
28 | #else // memory aligned with 4 Bytes |
29 | typedef struct tagS_TEST_STRUCT { |
30 | char tmp_ch0; |
31 | char tmp_ch1; |
32 | int tmp_val0; |
33 | unsigned char tmp_ch2; |
34 | unsigned char tmp_ch3; |
35 | unsigned char tmp_ch4; |
36 | short tmp_short0; |
37 | } S_TEST_STRUCT; |
38 | #endif |
39 | |
40 | static void TestRWOneByte(int tmp_rw_offset, int tmp_w_val, unsigned char tmp_w_ch); |
41 | static void TestRWNBytes(int tmp_rw_offset); |
42 | static void TestRWOneStruct(int tmp_rw_offset); |
43 | |
44 | int main() |
45 | { |
46 | TestRWOneByte(0, 1, -1); |
47 | TestRWOneByte(1, 2, -2); |
48 | TestRWOneByte(30, 3, -3); |
49 | TestRWOneByte(31, -1, 1); |
50 | TestRWOneByte(32, -2, 2); |
51 | TestRWOneByte(33, -3, 3); |
52 | |
53 | TestRWNBytes(31); |
54 | |
55 | TestRWOneStruct(0); |
56 | } |
57 | |
58 | static void TestRWOneByte(int tmp_rw_offset, int tmp_w_val, unsigned char tmp_w_ch) |
59 | { |
60 | int tmp_r_val = 0; |
61 | unsigned char tmp_r_ch = 0; |
62 | |
63 | LOGD("\n\n"); |
64 | LOGD("**************Test R/W One Byte **************\n\n"); |
65 | |
66 | LOGD("tmp_rw_offset = %d.\n", tmp_rw_offset); |
67 | |
68 | SSMWriteOneByte(tmp_rw_offset, tmp_w_ch); |
69 | SSMReadOneByte(tmp_rw_offset, &tmp_r_ch); |
70 | LOGD("tmp_w_ch = %d, tmp_r_ch = %d.\n", tmp_w_ch, tmp_r_ch); |
71 | |
72 | SSMWriteOneByte(tmp_rw_offset, tmp_w_val); |
73 | SSMReadOneByte(tmp_rw_offset, &tmp_r_val); |
74 | LOGD("tmp_w_val = %d, tmp_r_val = %d.\n", tmp_w_val, tmp_r_val); |
75 | } |
76 | |
77 | static void TestRWNBytes(int tmp_rw_offset) |
78 | { |
79 | int i = 0, tmp_op_buf_size = 0; |
80 | int device_size = 0, tmp_w_page_size = 0, tmp_r_page_size = 0; |
81 | int *tmp_op_int_w_buf = NULL, *tmp_op_int_r_buf = NULL; |
82 | unsigned char *tmp_op_char_w_buf = NULL, *tmp_op_char_r_buf = NULL; |
83 | |
84 | LOGD("\n\n"); |
85 | LOGD("**************Test R/W N Bytes **************\n\n"); |
86 | |
87 | SSMGetDeviceTotalSize(&device_size); |
88 | SSMGetDeviceWritePageSize(&tmp_w_page_size); |
89 | SSMGetDeviceReadPageSize(&tmp_r_page_size); |
90 | |
91 | if (tmp_w_page_size < tmp_r_page_size) { |
92 | tmp_op_buf_size = tmp_w_page_size * 2 / 3; |
93 | } else if (tmp_r_page_size < tmp_w_page_size) { |
94 | tmp_op_buf_size = tmp_r_page_size * 2 / 3; |
95 | } else { |
96 | tmp_op_buf_size = tmp_w_page_size; |
97 | } |
98 | |
99 | if (tmp_op_buf_size > device_size) { |
100 | tmp_op_buf_size = device_size; |
101 | } |
102 | |
103 | if (tmp_op_buf_size > 0) { |
104 | LOGD("tmp_rw_offset = %d.\n", tmp_rw_offset); |
105 | |
106 | tmp_op_char_w_buf = new unsigned char[tmp_op_buf_size]; |
107 | if (tmp_op_char_w_buf != NULL) { |
108 | tmp_op_char_r_buf = new unsigned char[tmp_op_buf_size]; |
109 | if (tmp_op_char_r_buf != NULL) { |
110 | for (i = 0; i < tmp_op_buf_size; i++) { |
111 | tmp_op_char_w_buf[i] = (tmp_op_buf_size / 2) - i; |
112 | LOGD("tmp_op_char_w_buf[%d] = %d\n", i, tmp_op_char_w_buf[i]); |
113 | } |
114 | SSMWriteNTypes(tmp_rw_offset, tmp_op_buf_size, tmp_op_char_w_buf); |
115 | |
116 | for (i = 0; i < tmp_op_buf_size; i++) { |
117 | tmp_op_char_r_buf[i] = 0; |
118 | } |
119 | SSMReadNTypes(tmp_rw_offset, tmp_op_buf_size, tmp_op_char_r_buf); |
120 | |
121 | for (i = 0; i < tmp_op_buf_size; i++) { |
122 | LOGD("tmp_op_char_r_buf[%d] = %d\n", i, tmp_op_char_r_buf[i]); |
123 | } |
124 | |
125 | delete tmp_op_char_r_buf; |
126 | tmp_op_char_r_buf = NULL; |
127 | } |
128 | |
129 | delete tmp_op_char_w_buf; |
130 | tmp_op_char_w_buf = NULL; |
131 | } |
132 | |
133 | tmp_op_int_w_buf = new int[tmp_op_buf_size]; |
134 | if (tmp_op_int_w_buf != NULL) { |
135 | tmp_op_int_r_buf = new int[tmp_op_buf_size]; |
136 | if (tmp_op_int_r_buf != NULL) { |
137 | for (i = 0; i < tmp_op_buf_size; i++) { |
138 | tmp_op_int_w_buf[i] = (tmp_op_buf_size / 2) - i; |
139 | LOGD("tmp_op_int_w_buf[%d] = %d\n", i, tmp_op_int_w_buf[i]); |
140 | } |
141 | SSMWriteNTypes(tmp_rw_offset, tmp_op_buf_size, tmp_op_int_w_buf); |
142 | |
143 | for (i = 0; i < tmp_op_buf_size; i++) { |
144 | tmp_op_int_r_buf[i] = 0; |
145 | } |
146 | SSMReadNTypes(tmp_rw_offset, tmp_op_buf_size, tmp_op_int_r_buf); |
147 | |
148 | for (i = 0; i < tmp_op_buf_size; i++) { |
149 | LOGD("tmp_op_int_r_buf[%d] = %d\n", i, tmp_op_int_r_buf[i]); |
150 | } |
151 | |
152 | delete tmp_op_int_r_buf; |
153 | tmp_op_int_r_buf = NULL; |
154 | } |
155 | |
156 | delete tmp_op_int_w_buf; |
157 | tmp_op_int_w_buf = NULL; |
158 | } |
159 | } |
160 | } |
161 | |
162 | static void TestRWOneStruct(int tmp_rw_offset) |
163 | { |
164 | S_TEST_STRUCT TestWriteStruct, TestReadStruct; |
165 | |
166 | LOGD("\n\n"); |
167 | LOGD("**************Test R/W One Struct **************\n\n"); |
168 | |
169 | LOGD("tmp_rw_offset = %d.\n", tmp_rw_offset); |
170 | |
171 | TestWriteStruct.tmp_ch0 = -9; |
172 | TestWriteStruct.tmp_ch1 = -8; |
173 | TestWriteStruct.tmp_val0 = 9; |
174 | TestWriteStruct.tmp_ch2 = 255; |
175 | TestWriteStruct.tmp_ch3 = 254; |
176 | TestWriteStruct.tmp_ch4 = 250; |
177 | TestWriteStruct.tmp_short0 = -9; |
178 | |
179 | SSMWriteNTypes(tmp_rw_offset, sizeof(S_TEST_STRUCT), (unsigned char *) &TestWriteStruct); |
180 | |
181 | LOGD("\n\n"); |
182 | LOGD("write struct length = %d.\n", sizeof(S_TEST_STRUCT)); |
183 | LOGD("TestWriteStruct.tmp_ch0 = %d.\n", TestWriteStruct.tmp_ch0); |
184 | LOGD("TestWriteStruct.tmp_ch1 = %d.\n", TestWriteStruct.tmp_ch1); |
185 | LOGD("TestWriteStruct.tmp_val0 = %d.\n", TestWriteStruct.tmp_val0); |
186 | LOGD("TestWriteStruct.tmp_ch2 = %d.\n", TestWriteStruct.tmp_ch2); |
187 | LOGD("TestWriteStruct.tmp_ch3 = %d.\n", TestWriteStruct.tmp_ch3); |
188 | LOGD("TestWriteStruct.tmp_ch4 = %d.\n", TestWriteStruct.tmp_ch4); |
189 | LOGD("TestWriteStruct.tmp_short0 = %d.\n", TestWriteStruct.tmp_short0); |
190 | |
191 | TestReadStruct.tmp_ch0 = 0; |
192 | TestReadStruct.tmp_ch1 = 0; |
193 | TestReadStruct.tmp_val0 = 0; |
194 | TestReadStruct.tmp_ch2 = 0; |
195 | TestReadStruct.tmp_ch3 = 0; |
196 | TestWriteStruct.tmp_ch4 = 0; |
197 | TestWriteStruct.tmp_short0 = 0; |
198 | |
199 | SSMReadNTypes(tmp_rw_offset, sizeof(S_TEST_STRUCT), (unsigned char *) &TestReadStruct); |
200 | |
201 | LOGD("\n\n"); |
202 | LOGD("read struct length = %d.\n", sizeof(S_TEST_STRUCT)); |
203 | LOGD("TestReadStruct.tmp_ch0 = %d.\n", TestReadStruct.tmp_ch0); |
204 | LOGD("TestReadStruct.tmp_ch1 = %d.\n", TestReadStruct.tmp_ch1); |
205 | LOGD("TestReadStruct.tmp_val0 = %d.\n", TestReadStruct.tmp_val0); |
206 | LOGD("TestReadStruct.tmp_ch2 = %d.\n", TestReadStruct.tmp_ch2); |
207 | LOGD("TestReadStruct.tmp_ch3 = %d.\n", TestReadStruct.tmp_ch3); |
208 | LOGD("TestReadStruct.tmp_ch4 = %d.\n", TestReadStruct.tmp_ch4); |
209 | LOGD("TestReadStruct.tmp_short0 = %d.\n", TestReadStruct.tmp_short0); |
210 | } |
211 |