a8a11fedf799fc243732c04ed044d9227649b467
[platform/core/api/radio.git] / test / radio_test.c
1 /*
2  *
3  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
4  *
5  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 /* testsuite for radio api */
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <glib.h>
25 #include <string.h>
26
27 #include "radio.h"
28 #include "radio_test_type.h"
29 #include <unistd.h>
30
31 #define DEFAULT_TEST_FREQ       107700
32 #define MAX_STRING_LEN 100
33
34 enum {
35         CURRENT_STATUS_MAINMENU,
36         CURRENT_STATUS_CALL_API,
37         CURRENT_STATUS_SET_FREQ,
38         CURRENT_STATUS_SET_MUTE,
39         CURRENT_STATUS_SET_VOL,
40 };
41
42 int g_menu_state = CURRENT_STATUS_MAINMENU;
43
44 /* test items...*/
45 int __test_radio_init(void);
46 int __test_radio_listen_gorealra(void);
47 int __test_repeat_init_release(void);
48 int __test_repeat_start_stop(void);
49 int __test_repeat_seek(void);
50 int __test_repeat_whole(void);
51 int __test_manual_api_calling(void);
52 int __test_radio_hw_debug(void);
53
54 void __radio_seek_completed_cb(int frequency, void *user_data);
55 void __radio_scan_updated_cb(int freq, void *user_data);
56 void __radio_scan_stop_cb(void *user_data);
57 void __radio_set_scan_completed_cb(void *user_param);
58 void __radio_set_interrupted_cb(radio_interrupted_code_e code, void *user_param);
59
60 int g_num_of_tests = 0;
61 static radio_h g_my_radio = 0;
62 GMainLoop *loop = NULL;
63
64 static void display_sub_basic()
65 {
66         g_print("\n");
67         g_print("=============================================================\n");
68         g_print("                   FMRadio testing menu\n");
69         g_print("-------------------------------------------------------------\n");
70         g_print(" 1. init test\n");
71         g_print(" 2. listening gorelra\n");
72         g_print(" 3. repeat_init_release\n");
73         g_print(" 4. repeat_start_stop\n");
74         g_print(" 5. repeat_seek\n");
75         g_print(" 6. repeat_whole\n");
76         g_print(" 7. manual api calling test\n");
77         g_print(" q. quit\n");
78         g_print("-------------------------------------------------------------\n");
79
80 }
81 static void displaymenu(void)
82 {
83         if (g_menu_state == CURRENT_STATUS_MAINMENU) {
84                 display_sub_basic();
85         } else if (g_menu_state == CURRENT_STATUS_CALL_API) {
86                 g_print("---------------------------------------------------------------------------\n");
87                 g_print("\t radio api test. try now!\n");
88                 g_print("---------------------------------------------------------------------------\n");
89                 g_print("[1] radio_create\t");
90                 g_print("[2] radio_destroy\t");
91                 g_print("[3] radio_get_state\n");
92                 g_print("[4] radio_start\t\t");
93                 g_print("[5] radio_stop\n");
94                 g_print("[6] radio_seek_up\t");
95                 g_print("[7] radio_seek_down\n");
96                 g_print("[8] radio_set_frequency\t");
97                 g_print("[9] radio_get_frequency\n");
98                 g_print("[10] radio_scan_start\t");
99                 g_print("[11] radio_scan_stop\n");
100                 g_print("[12] radio_set_mute\t");
101                 g_print("[13] radio_is_muted\n");
102                 g_print("[14] radio_set_scan_completed_cb\t");
103                 g_print("[15] radio_unset_scan_completed_cb\n");
104                 g_print("[16] radio_set_interrupted_cb\t\t");
105                 g_print("[17] radio_unset_interrupted_cb\n");
106                 g_print("[18] radio_get_frequency_range\t\t");
107                 g_print("[19] radio_get_channel_spacing\n");
108                 g_print("[20] radio_signal_strength\n");
109                 g_print("[21] radio_set_volume\t");
110                 g_print("[22] radio_get_volume\t");
111                 g_print("[0] quit\n");
112                 g_print("---------------------------------------------------------------------------\n");
113                 g_print("choose one : ");
114         } else if (g_menu_state == CURRENT_STATUS_SET_FREQ) {
115                 g_print("input freq : ");
116         } else if (g_menu_state == CURRENT_STATUS_SET_MUTE) {
117                 g_print("select one(0:UNMUTE/1:MUTE) : ");
118         } else if (g_menu_state == CURRENT_STATUS_SET_VOL) {
119                 g_print("input volume (0.0 ~ 1.0) : ");
120         } else {
121                 g_print("*** unknown status.\n");
122                 /*  exit(0); */
123         }
124 //      g_print(" >>> ");
125 }
126
127 gboolean timeout_menu_display(void *data)
128 {
129         displaymenu();
130         return FALSE;
131 }
132
133 void _interpret_main_menu(char *cmd)
134 {
135         int len = strlen(cmd);
136         if (len == 1) {
137                 if (strncmp(cmd, "1", 1) == 0) {
138                         __test_radio_init();
139                 } else if (strncmp(cmd, "2", 1) == 0) {
140                         __test_radio_listen_gorealra();
141                 } else if (strncmp(cmd, "3", 1) == 0) {
142                         __test_repeat_init_release();
143                 } else if (strncmp(cmd, "4", 1) == 0) {
144                         __test_repeat_start_stop();
145                 } else if (strncmp(cmd, "5", 1) == 0) {
146                         __test_repeat_seek();
147                 } else if (strncmp(cmd, "6", 1) == 0) {
148                         __test_repeat_whole();
149                 } else if (strncmp(cmd, "7", 1) == 0) {
150                         g_menu_state = CURRENT_STATUS_CALL_API;
151                 } else if (strncmp(cmd, "q", 1) == 0) {
152                         exit(0);
153                 } else {
154                         g_print("unknown menu \n");
155                 }
156         }
157         return;
158 }
159
160
161 static void interpret(char *cmd)
162 {
163         int ret = RADIO_ERROR_NONE;
164         switch (g_menu_state) {
165         case CURRENT_STATUS_MAINMENU: {
166                 _interpret_main_menu(cmd);
167                 break;
168         }
169         case CURRENT_STATUS_CALL_API: {
170                 int len = strlen(cmd);
171                 if (len == 1) {
172                         if (strncmp(cmd, "1", len) == 0) {
173                                 RADIO_TEST__(radio_create(&g_my_radio);)
174                         } else if (strncmp(cmd, "2", len) == 0) {
175                                 RADIO_TEST__(radio_destroy(g_my_radio);)
176                         } else if (strncmp(cmd, "3", len) == 0) {
177                                 radio_state_e state;
178                                 RADIO_TEST__(radio_get_state(g_my_radio, &state);)
179                                 g_print("state : %d\n", state);
180                         } else if (strncmp(cmd, "4", len) == 0) {
181                                 RADIO_TEST__(radio_start(g_my_radio);)
182                         } else if (strncmp(cmd, "5", len) == 0) {
183                                 RADIO_TEST__(radio_stop(g_my_radio);)
184                         } else if (strncmp(cmd, "6", len) == 0) {
185                                 RADIO_TEST__(radio_seek_up(g_my_radio, __radio_seek_completed_cb, NULL);)
186                         } else if (strncmp(cmd, "7", len) == 0) {
187                                 RADIO_TEST__(radio_seek_down(g_my_radio, __radio_seek_completed_cb, NULL);)
188                         } else if (strncmp(cmd, "8", len) == 0) {
189                                 g_menu_state = CURRENT_STATUS_SET_FREQ;
190                         } else if (strncmp(cmd, "9", len) == 0) {
191                                 int freq = 0;
192                                 RADIO_TEST__(radio_get_frequency(g_my_radio, &freq);)
193                                 g_print("freq : %d\n", freq);
194                         } else if (strncmp(cmd, "0", len) == 0) {
195                                 g_menu_state = CURRENT_STATUS_MAINMENU;
196                         } else {
197                                 g_print("UNKNOW COMMAND\n");
198                         }
199                 } else if (len == 2) {
200                         if (strncmp(cmd, "10", len) == 0) {
201                                 RADIO_TEST__(radio_scan_start(g_my_radio, &__radio_scan_updated_cb, NULL);)
202                         } else if (strncmp(cmd, "11", len) == 0) {
203                                 RADIO_TEST__(radio_scan_stop(g_my_radio, &__radio_scan_stop_cb, NULL);)
204                         } else if (strncmp(cmd, "12", len) == 0) {
205                                 g_menu_state = CURRENT_STATUS_SET_MUTE;
206                         } else if (strncmp(cmd, "13", len) == 0) {
207                                 bool muted = 0;
208                                 RADIO_TEST__(radio_is_muted(g_my_radio, &muted);)
209                                 g_print("muted : %d\n", muted);
210                         } else if (strncmp(cmd, "14", len) == 0) {
211                                 RADIO_TEST__(radio_set_scan_completed_cb(g_my_radio, &__radio_set_scan_completed_cb, NULL);)
212                         } else if (strncmp(cmd, "15", len) == 0) {
213                                 RADIO_TEST__(radio_unset_scan_completed_cb(g_my_radio);)
214                         } else if (strncmp(cmd, "16", len) == 0) {
215                                 RADIO_TEST__(radio_set_interrupted_cb(g_my_radio, &__radio_set_interrupted_cb, NULL);)
216                         } else if (strncmp(cmd, "17", len) == 0) {
217                                 RADIO_TEST__(radio_unset_interrupted_cb(g_my_radio);)
218                         } else if (strncmp(cmd, "18", len) == 0) {
219                                 int min = 0;
220                                 int max = 0;
221                                 RADIO_TEST__(radio_get_frequency_range(g_my_radio, &min, &max);)
222                                 g_print("min : %d max: %d \n", min, max);
223                         } else if (strncmp(cmd, "19", len) == 0) {
224                                 int channel_spacing = 0;
225                                 RADIO_TEST__(radio_get_channel_spacing(g_my_radio, &channel_spacing);)
226                                 g_print("channel_spacing : %d \n", channel_spacing);
227                         } else if (strncmp(cmd, "20", len) == 0) {
228                                 int signal_strength = 0;
229                                 RADIO_TEST__(radio_get_signal_strength(g_my_radio, &signal_strength);)
230                                 g_print("signal strength is : %d \n", signal_strength);
231                         } else if (strncmp(cmd, "21", len) == 0) {
232                                 g_menu_state = CURRENT_STATUS_SET_VOL;
233                         } else if (strncmp(cmd, "22", len) == 0) {
234                                 float vol = 0.0;
235                                 RADIO_TEST__(radio_get_volume(g_my_radio, &vol);)
236                                 g_print("volume : %f\n", vol);
237                         } else {
238                                 g_print("UNKNOW COMMAND\n");
239                         }
240                 } else {
241                         g_print("UNKNOW COMMAND\n");
242                 }
243                 break;
244         }
245         case CURRENT_STATUS_SET_FREQ: {
246                 int freq = atoi(cmd);
247                 RADIO_TEST__(radio_set_frequency(g_my_radio, freq);)
248                 g_menu_state = CURRENT_STATUS_CALL_API;
249                 break;
250         }
251         case CURRENT_STATUS_SET_MUTE: {
252                 int muted = atoi(cmd);
253                 RADIO_TEST__(radio_set_mute(g_my_radio, muted);)
254                 g_menu_state = CURRENT_STATUS_CALL_API;
255                 break;
256         }
257         case CURRENT_STATUS_SET_VOL: {
258                 float vol = atof(cmd);
259                 RADIO_TEST__(radio_set_volume(g_my_radio, vol);)
260                 g_menu_state = CURRENT_STATUS_CALL_API;
261                 break;
262         }
263         default:
264                 break;
265         }
266         g_timeout_add(100, timeout_menu_display, 0);
267 }
268
269 /* test items...*/
270 int __test_radio_init(void)
271 {
272         printf("%s\n", __FUNCTION__);
273
274         int ret = RADIO_ERROR_NONE;
275         radio_h radio;
276
277         RADIO_TEST__(radio_create(&radio);)
278         RADIO_TEST__(radio_destroy(radio);)
279         return ret;
280 }
281
282 int __test_radio_listen_gorealra(void)
283 {
284         printf("%s\n", __FUNCTION__);
285
286         int ret = RADIO_ERROR_NONE;
287         radio_h radio;
288
289         RADIO_TEST__(radio_create(&radio);)
290         RADIO_TEST__(radio_set_frequency(radio, DEFAULT_TEST_FREQ);)
291         RADIO_TEST__(radio_start(radio);)
292         usleep(5000 * 1000);
293         RADIO_TEST__(radio_stop(radio);)
294         RADIO_TEST__(radio_destroy(radio);)
295         return ret;
296 }
297
298 int __test_repeat_init_release(void)
299 {
300         printf("%s\n", __FUNCTION__);
301
302         int ret = RADIO_ERROR_NONE;
303         int cnt = 0;
304         radio_h radio;
305
306         while (cnt < 1000) {
307                 RADIO_TEST__(radio_create(&radio);)
308                 RADIO_TEST__(radio_destroy(radio);)
309                 cnt++;
310
311                 printf("%s : repeat count : %d\n", __FUNCTION__, cnt);
312         }
313
314         return 0;
315 }
316
317 int __test_repeat_start_stop(void)
318 {
319         printf("%s\n", __FUNCTION__);
320         int ret = RADIO_ERROR_NONE;
321         int cnt = 0;
322         radio_h radio;
323
324         RADIO_TEST__(radio_create(&radio);)
325         RADIO_TEST__(radio_set_frequency(radio, DEFAULT_TEST_FREQ);)
326
327         while (cnt < 10) {
328                 RADIO_TEST__(radio_start(radio);)
329                 usleep(2000 * 1000);
330                 RADIO_TEST__(radio_stop(radio);)
331                 cnt++;
332                 printf("%s : repeat count : %d\n", __FUNCTION__, cnt);
333         }
334
335         return 0;
336 }
337
338 int __test_repeat_seek(void)
339 {
340         printf("__test_repeat_seek\n");
341         return 0;
342 }
343
344 int __test_repeat_whole(void)
345 {
346         printf("__test_repeat_whole\n");
347         return 0;
348 }
349
350 void __radio_seek_completed_cb(int frequency, void *user_data)
351 {
352         g_print("__radio_seek_completed_cb freq is %d\n", frequency);
353 }
354
355 void __radio_scan_updated_cb(int frequency, void *user_param)
356 {
357         g_print("__radio_scan_updated_cb freq is %d\n", frequency);
358 }
359
360 void __radio_scan_stop_cb(void *user_param)
361 {
362         g_print("__radio_scan_stop_cb\n");
363 }
364
365 void __radio_set_scan_completed_cb(void *user_param)
366 {
367         g_print("__radio_scan_completed_cb\n");
368 }
369
370 void __radio_set_interrupted_cb(radio_interrupted_code_e code, void *user_param)
371 {
372         g_print("__radio_set_interrupted_cb\n");
373 }
374
375 gboolean input(GIOChannel *channel)
376 {
377         gchar buf[MAX_STRING_LEN];
378         gsize read;
379         GError *error = NULL;
380         g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
381         buf[read] = '\0';
382         g_strstrip(buf);
383         interpret(buf);
384         return TRUE;
385 }
386
387
388 int main(int argc, char *argv[])
389 {
390         GIOChannel *stdin_channel;
391         loop = g_main_loop_new(NULL, 0);
392         stdin_channel = g_io_channel_unix_new(0);
393         g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
394         g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc) input, NULL);
395
396         displaymenu();
397
398         g_print("RUN main loop\n");
399         g_main_loop_run(loop);
400         g_print("STOP main loop\n");
401
402         g_main_loop_unref(loop);
403         return 0;
404 }
405