summaryrefslogtreecommitdiff
path: root/mm_sound_bootsound.c
blob: a505231d2e70a7786d4e7d1967ad7ae1cac10222 (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
/*
 * libmm-sound
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungbae Shin <seungbae.shin@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 <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <vconf.h>

#include <semaphore.h>

#include <mm_error.h>
#include <mm_debug.h>

#define KEYTONE_PATH        "/tmp/keytone"  /* Keytone pipe path */
#define FILE_FULL_PATH 1024				/* File path lenth */
#define MAX_RETRY 40
#define RETRY_INTERVAL_USEC 50000

#define ROLE_NAME_LEN 64				/* Role name length */
#define VOLUME_GAIN_TYPE_LEN 64		/* Volume gain type length */

typedef struct {
	char filename[FILE_FULL_PATH];
	char role[ROLE_NAME_LEN];
	char volume_gain_type[VOLUME_GAIN_TYPE_LEN];
} ipc_t;

#define MMSOUND_STRNCPY(dst, src, size) \
do { \
	if (src != NULL && dst != NULL && size > 0) { \
		strncpy(dst, src, size - 1); \
		dst[size - 1] = '\0'; \
	} else if (dst == NULL) { \
		debug_error("STRNCPY ERROR: Destination String is NULL"); \
	} \
	else if (size <= 0) { \
		debug_error("STRNCPY ERROR: Destination String is NULL"); \
	} \
	else { \
		debug_error("STRNCPY ERROR: Destination String is NULL"); \
	} \
} while (0)

EXPORT_API
int mm_sound_boot_ready(int timeout_sec)
{
	struct timespec ts;
	sem_t* sem = NULL;

	debug_msg("[BOOT] check for sync....");
	if ((sem = sem_open("booting-sound", O_CREAT, 0660, 0)) == SEM_FAILED) {
		debug_error("error creating sem : %d", errno);
		return -1;
	}

	debug_msg("[BOOT] start to wait ready....timeout is set to %d sec", timeout_sec);
	clock_gettime(CLOCK_REALTIME, &ts);
	ts.tv_sec += timeout_sec;

	if (sem_timedwait(sem, &ts) == -1) {
		if (errno == ETIMEDOUT)
			debug_warning("[BOOT] timeout!");
	} else {
		debug_msg("[BOOT] ready wait success!!!!");
		sem_post(sem);
	}

	return 0;
}

EXPORT_API
int mm_sound_boot_play_sound(char* path)
{
	int err = 0;
	int fd = -1;
	int size = 0;
	ipc_t data = { { 0, }, { 0, }, { 0, } };

	debug_msg("[BOOT] play boot sound [%s]!!!!", path);
	if (path == NULL)
		return MM_ERROR_SOUND_INVALID_FILE;

	/* Check whether file exists */
	fd = open(path, O_RDONLY);
	if (fd == -1) {
		char str_error[256];
		int errsv = errno;
		strerror_r(errsv, str_error, sizeof(str_error));
		debug_error("file open failed with [%s][%d]", str_error, errsv);
		switch (errsv) {
		case ENOENT:
			return MM_ERROR_SOUND_FILE_NOT_FOUND;
		default:
			return MM_ERROR_SOUND_INTERNAL;
		}
	}
	close(fd);
	fd = -1;

	/* Open PIPE */
	fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
	if (fd == -1) {
		debug_error("Fail to open pipe");
		return MM_ERROR_SOUND_FILE_NOT_FOUND;
	}

	MMSOUND_STRNCPY(data.filename, path, FILE_FULL_PATH);
	MMSOUND_STRNCPY(data.role, "system", ROLE_NAME_LEN);
	MMSOUND_STRNCPY(data.volume_gain_type, "booting", VOLUME_GAIN_TYPE_LEN);

	debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]", data.filename, data.role, data.volume_gain_type);
	size = sizeof(ipc_t);

	/* Write to PIPE */
	err = write(fd, &data, size);
	if (err < 0) {
		char str_error[256];
		strerror_r(errno, str_error, sizeof(str_error));
		debug_error("Fail to write data: [%s][%d]", str_error, errno);
		close(fd);
		return MM_ERROR_SOUND_INTERNAL;
	}
	/* Close PIPE */
	close(fd);

	return MM_ERROR_NONE;
}