Fix for new automake and 64 bit compatibility.
[platform/core/multimedia/avsystem.git] / avsys-audio-initializer.c
1 /*
2  * avsystem
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <signal.h>
26
27 #include "include/avsys-error.h"
28 #include "include/avsys-debug.h"
29
30 #include "include/avsys-audio-handle.h"
31 #include "include/avsys-audio-path.h"
32 #include "include/avsys-audio-logical-volume.h"
33
34 #define OP_NONE -1
35 #define OP_USAGE 0
36 #define OP_INIT 1
37 #define OP_UNINIT 2
38 #define OP_RESET 3
39 #define OP_DUMP 4
40 #define OP_DAEMON 5
41 #define OP_AMP 6
42 #define OP_SHUTDOWN 8
43 #define OP_REJUVE 9
44
45 static int usage(int argc, char *argv[]);
46 static int get_options(int argc, char *argv[]);
47
48 int main(int argc, char *argv[])
49 {
50         int operation = OP_NONE;
51         int result = 0;
52         int mute = 1;
53         pid_t pid;
54         mode_t old_umask = 0;
55
56         static char *str_errormsg[] = {
57                 "Operation is success.",
58                 "Handle Init Fail",
59                 "Path Init Fail",
60                 "Handle Fini Fail",
61                 "Path Fini Fail",
62                 "Handle Reset Fail",
63                 "Path Reset Fail",
64                 "Handle Dump Fail",
65                 "Path Dump Fail",
66                 "Global Mute Fail",
67                 "Handle Rejuvenation Fail",
68                 "Vconf Get Value Fail",
69                 "Sync Dump Fail",
70         };
71
72         operation = get_options(argc, argv);
73
74         switch (operation) {
75         case OP_INIT:
76                 old_umask = umask(0);
77                 fprintf(stderr, "old umask was [%o]\n", old_umask);
78                 result = avsys_audio_handle_init();
79                 if (AVSYS_FAIL(result)) {
80                         result = 1;
81                         umask(old_umask);
82                         fprintf(stderr, "set umask to old value\n");
83                         break;
84                 }
85                 result = avsys_audio_path_ex_init();
86                 if (AVSYS_FAIL(result)) {
87                         result = 2;
88                         umask(old_umask);
89                         fprintf(stderr, "set umask to old value\n");
90                         break;
91                 }
92                 result = avsys_audio_logical_volume_init();
93                 if (AVSYS_FAIL(result)) {
94                         result = 2;
95                         umask(old_umask);
96                         fprintf(stderr, "set umask to old value\n");
97                         break;
98                 }
99                 umask(old_umask);
100                 fprintf(stderr, "set umask to old value\n");
101                 break;
102
103         case OP_UNINIT:
104                 result = avsys_audio_handle_fini();
105                 if (AVSYS_FAIL(result)) {
106                         result = 3;
107                         break;
108                 }
109
110                 result = avsys_audio_path_ex_fini();
111                 if (AVSYS_FAIL(result)) {
112                         result = 4;
113                         break;
114                 }
115                 break;
116         case OP_RESET:
117                 result = avsys_audio_handle_reset(NULL);
118                 if (AVSYS_FAIL(result)) {
119                         result = 5;
120                         break;
121                 }
122
123                 result = avsys_audio_path_ex_reset(0);
124                 if (AVSYS_FAIL(result)) {
125                         result = 6;
126                         break;
127                 }
128                 break;
129         case OP_DUMP:
130                 result = avsys_audio_handle_dump();
131                 if (AVSYS_FAIL(result)) {
132                         result = 7;
133                         break;
134                 }
135                 result = avsys_audio_path_ex_dump();
136                 if (AVSYS_FAIL(result)) {
137                         result = 8;
138                         break;
139                 }
140
141                 result = avsys_audio_dump_sync();
142                 if (AVSYS_FAIL(result)) {
143                         result = 12;
144                         break;
145                 }
146                 break;
147         case OP_DAEMON:
148                 result = 0;
149                 break;
150         case OP_USAGE:
151                 break;
152         case OP_SHUTDOWN:
153                 result = avsys_audio_path_ex_set_mute(mute);
154                 if (AVSYS_FAIL(result)) {
155                         result = 9;
156                 }
157                 break;
158         case OP_REJUVE:
159                 result = avsys_audio_handle_rejuvenation();
160                 if (AVSYS_FAIL(result)) {
161                         result = 10;
162                 }
163                 break;
164         default:
165                 fprintf(stderr, "Unknown operation\n");
166                 break;
167         }
168
169         if (result != 0) {
170                 fprintf(stderr, "%s\n", str_errormsg[result]);
171                 return 1;
172         } else
173                 return 0;
174 }
175
176 static int get_options(int argc, char *argv[])
177 {
178         int ch = 0;
179         int operation = OP_NONE;
180
181         if (argc != 2)
182                 return usage(argc, argv);
183
184         while ((ch = getopt(argc, argv, "iurtdmjhap")) != EOF) {
185                 switch (ch) {
186                 case 'i':
187                         if (operation != OP_NONE)
188                                 return usage(argc, argv);
189                         operation = OP_INIT;
190                         break;
191                 case 'u':
192                         if (operation != OP_NONE)
193                                 return usage(argc, argv);
194                         operation = OP_UNINIT;
195                         break;
196                 case 'r':
197                         if (operation != OP_NONE)
198                                 return usage(argc, argv);
199                         operation = OP_RESET;
200                         break;
201                 case 'd':
202                         if (operation != OP_NONE)
203                                 return usage(argc, argv);
204                         operation = OP_DUMP;
205                         break;
206                 case 'h':
207                         if (operation != OP_NONE)
208                                 return usage(argc, argv);
209                         operation = OP_DAEMON;
210                         break;
211                 case 'm':
212                         if (operation != OP_NONE)
213                                 return usage(argc, argv);
214                         operation = OP_SHUTDOWN;
215                         break;
216                 case 'j':
217                         if (operation != OP_NONE)
218                                 return usage(argc, argv);
219                         operation = OP_REJUVE;
220                         break;
221                 default:
222                         return usage(argc, argv);
223                 }
224                 if (optind != argc)
225                         return usage(argc, argv);
226         }
227         return operation;
228 }
229
230 static int usage(int argc, char *argv[])
231 {
232         fprintf(stderr, "Usage : %s option\n", argv[0]);
233         fprintf(stderr, "[OPTIONS]\n");
234         fprintf(stderr, "  -i : Initialize audio system\n");
235         fprintf(stderr, "  -u : Uninitialize audio system\n");
236         fprintf(stderr, "  -r : Reset audio system\n");
237         fprintf(stderr, "  -d : Dump audio system\n");
238         fprintf(stderr, "  -m : Global mute\n");
239         fprintf(stderr, "  -j : Handle rejuvenation\n");
240         return OP_USAGE;
241 }
242