summaryrefslogtreecommitdiff
path: root/avsys-audio-initializer.c
blob: 31b92cc63bd2ca84bc80ab415c9c4677c349abeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * avsystem
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#include "include/avsys-error.h"
#include "include/avsys-debug.h"

#include "include/avsys-audio-handle.h"
#include "include/avsys-audio-path.h"
#include "include/avsys-audio-logical-volume.h"

#define OP_NONE -1
#define OP_USAGE 0
#define OP_INIT 1
#define OP_UNINIT 2
#define OP_RESET 3
#define OP_DUMP 4
#define OP_DAEMON 5
#define OP_AMP 6
#define OP_SHUTDOWN 8
#define OP_REJUVE 9

static int usage(int argc, char *argv[]);
static int get_options(int argc, char *argv[]);

int main(int argc, char *argv[])
{
	int operation = OP_NONE;
	int result = 0;
	int mute = 1;
	pid_t pid;
	mode_t old_umask = 0;

	static char *str_errormsg[] = {
		"Operation is success.",
		"Handle Init Fail",
		"Path Init Fail",
		"Handle Fini Fail",
		"Path Fini Fail",
		"Handle Reset Fail",
		"Path Reset Fail",
		"Handle Dump Fail",
		"Path Dump Fail",
		"Global Mute Fail",
		"Handle Rejuvenation Fail",
		"Vconf Get Value Fail",
		"Sync Dump Fail",
	};

	operation = get_options(argc, argv);

	switch (operation) {
	case OP_INIT:
		old_umask = umask(0);
		fprintf(stderr, "old umask was [%o]\n", old_umask);
		result = avsys_audio_handle_init();
		if (AVSYS_FAIL(result)) {
			result = 1;
			umask(old_umask);
			fprintf(stderr, "set umask to old value\n");
			break;
		}
		result = avsys_audio_path_ex_init();
		if (AVSYS_FAIL(result)) {
			result = 2;
			umask(old_umask);
			fprintf(stderr, "set umask to old value\n");
			break;
		}
		result = avsys_audio_logical_volume_init();
		if (AVSYS_FAIL(result)) {
			result = 2;
			umask(old_umask);
			fprintf(stderr, "set umask to old value\n");
			break;
		}
		umask(old_umask);
		fprintf(stderr, "set umask to old value\n");
		break;

	case OP_UNINIT:
		result = avsys_audio_handle_fini();
		if (AVSYS_FAIL(result)) {
			result = 3;
			break;
		}

		result = avsys_audio_path_ex_fini();
		if (AVSYS_FAIL(result)) {
			result = 4;
			break;
		}
		break;
	case OP_RESET:
		result = avsys_audio_handle_reset(NULL);
		if (AVSYS_FAIL(result)) {
			result = 5;
			break;
		}

		result = avsys_audio_path_ex_reset(0);
		if (AVSYS_FAIL(result)) {
			result = 6;
			break;
		}
		break;
	case OP_DUMP:
		result = avsys_audio_handle_dump();
		if (AVSYS_FAIL(result)) {
			result = 7;
			break;
		}
		result = avsys_audio_path_ex_dump();
		if (AVSYS_FAIL(result)) {
			result = 8;
			break;
		}

		result = avsys_audio_dump_sync();
		if (AVSYS_FAIL(result)) {
			result = 12;
			break;
		}
		break;
	case OP_DAEMON:
		result = 0;
		break;
	case OP_USAGE:
		break;
	case OP_SHUTDOWN:
		result = avsys_audio_path_ex_set_mute(mute);
		if (AVSYS_FAIL(result)) {
			result = 9;
		}
		break;
	case OP_REJUVE:
		result = avsys_audio_handle_rejuvenation();
		if (AVSYS_FAIL(result)) {
			result = 10;
		}
		break;
	default:
		fprintf(stderr, "Unknown operation\n");
		break;
	}

	if (result != 0) {
		fprintf(stderr, "%s\n", str_errormsg[result]);
		return 1;
	} else
		return 0;
}

static int get_options(int argc, char *argv[])
{
	int ch = 0;
	int operation = OP_NONE;

	if (argc != 2)
		return usage(argc, argv);

	while ((ch = getopt(argc, argv, "iurtdmjhap")) != EOF) {
		switch (ch) {
		case 'i':
			if (operation != OP_NONE)
				return usage(argc, argv);
			operation = OP_INIT;
			break;
		case 'u':
			if (operation != OP_NONE)
				return usage(argc, argv);
			operation = OP_UNINIT;
			break;
		case 'r':
			if (operation != OP_NONE)
				return usage(argc, argv);
			operation = OP_RESET;
			break;
		case 'd':
			if (operation != OP_NONE)
				return usage(argc, argv);
			operation = OP_DUMP;
			break;
		case 'h':
			if (operation != OP_NONE)
				return usage(argc, argv);
			operation = OP_DAEMON;
			break;
		case 'm':
			if (operation != OP_NONE)
				return usage(argc, argv);
			operation = OP_SHUTDOWN;
			break;
		case 'j':
			if (operation != OP_NONE)
				return usage(argc, argv);
			operation = OP_REJUVE;
			break;
		default:
			return usage(argc, argv);
		}
		if (optind != argc)
			return usage(argc, argv);
	}
	return operation;
}

static int usage(int argc, char *argv[])
{
	fprintf(stderr, "Usage : %s option\n", argv[0]);
	fprintf(stderr, "[OPTIONS]\n");
	fprintf(stderr, "  -i : Initialize audio system\n");
	fprintf(stderr, "  -u : Uninitialize audio system\n");
	fprintf(stderr, "  -r : Reset audio system\n");
	fprintf(stderr, "  -d : Dump audio system\n");
	fprintf(stderr, "  -m : Global mute\n");
	fprintf(stderr, "  -j : Handle rejuvenation\n");
	return OP_USAGE;
}