3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 #include <device-node.h>
28 #include "haptic_module_log.h"
31 #define MAX_LEVEL 255.0f
32 #define DEFAULT_EFFECT_HANDLE 0x02
37 #define PREDEF_HAPTIC "haptic"
50 unsigned char **ppbuffer;
57 static BUFFER gbuffer;
59 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
61 static int _check_valid_haptic_format(HapticFile *file)
63 if (file->chunkID != HEADER_ID)
66 if (file->fmt.chunkID != FMT_ID)
69 if (file->data.chunkID != DATA_ID)
75 static int __haptic_predefine_action(int handle, int prop, int val)
82 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
83 snprintf(buf_prop, sizeof(buf_prop), "%d", prop);
84 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
85 snprintf(buf_val, sizeof(buf_val), "%d", val);
87 MODULE_LOG("pid : %s(%d), prop : %s, handle : %s", buf_pid, pthread_self(), buf_prop, buf_handle);
88 return __haptic_call_predef_action(PREDEF_HAPTIC, 4, buf_pid, buf_prop, buf_handle, buf_val);
91 static int _create_thread(void* data, void*(*func)(void*))
94 MODULE_ERROR("pthread already created");
98 if (pthread_create(&tid, NULL, func, data) != 0) {
99 MODULE_ERROR("pthread_create is failed : %s", strerror(errno));
106 static int _cancel_thread(void)
112 MODULE_LOG("pthread not initialized");
116 MODULE_LOG("cancel thread!!!");
120 while (pthread_mutex_trylock(&mutex) == EBUSY) {
122 MODULE_LOG("Already locked..");
125 pthread_mutex_unlock(&mutex);
127 if ((ret = pthread_cancel(tid)) < 0) {
128 MODULE_ERROR("pthread_cancel is failed : %s, ret(%d)", strerror(errno), ret);
132 if (pthread_join(tid, (void**)&ptr) < 0) {
133 MODULE_ERROR("pthread_join is failed : %s", strerror(errno));
140 if (ptr == PTHREAD_CANCELED) {
141 MODULE_LOG("pthread canceled");
143 MODULE_LOG("pthread already finished");
149 static void __clean_up(void *arg)
151 BUFFER *pbuffer = (BUFFER*)arg;
154 MODULE_LOG("clean up handler!!! : %d", tid);
156 for (i = 0; i < pbuffer->channels; ++i) {
157 free(pbuffer->ppbuffer[i]);
158 pbuffer->ppbuffer[i] = NULL;
161 free(pbuffer->ppbuffer);
162 pbuffer->ppbuffer = NULL;
164 pbuffer->channels = 0;
168 __haptic_predefine_action(gbuffer.handle, STOP, NULL);
169 pthread_mutex_unlock(&mutex);
173 static void* __play_cb(void *arg)
175 BUFFER *pbuffer = (BUFFER*)arg;
178 unsigned char prev = -1;
180 MODULE_LOG("Start thread");
182 pthread_cleanup_push(__clean_up, arg);
184 /* Copy buffer from source buffer */
185 for (i = 0; i < pbuffer->iteration; i++) {
186 for (j = 0; j < pbuffer->length; ++j) {
187 for (k = 0; k < pbuffer->channels; ++k) {
188 pthread_mutex_lock(&mutex);
190 pthread_exit((void*)0);
192 ch = pbuffer->ppbuffer[k][j];
194 __haptic_predefine_action(pbuffer->handle, LEVEL, ch);
197 pthread_mutex_unlock(&mutex);
198 usleep(BITPERMS * 1000);
203 pthread_mutex_lock(&mutex);
204 __haptic_predefine_action(gbuffer.handle, STOP, NULL);
205 pthread_mutex_unlock(&mutex);
207 pthread_cleanup_pop(1);
208 pthread_exit((void *)0);
211 int GetHapticLevelMax(int *max)
214 status = device_get_property(DEVICE_TYPE_VIBRATOR, PROP_VIBRATOR_LEVEL_MAX, max);
216 MODULE_ERROR("device_get_property fail : %d", status);
222 int InitializeBuffer(unsigned char *vibe_buffer, int max_bufsize)
226 if (max_bufsize < sizeof(HapticFile)) {
227 MODULE_ERROR("buffer lacks a memory : size(%d) minimum size(%d)",
228 max_bufsize, sizeof(HapticFile));
232 memset(vibe_buffer, 0, sizeof(char)*max_bufsize);
234 pfile = (HapticFile*)vibe_buffer;
236 pfile->chunkID = HEADER_ID;
237 pfile->chunkSize = sizeof(HapticFile);
238 pfile->fmt.chunkID = FMT_ID;
239 pfile->fmt.chunkSize = sizeof(FormatChunk);
240 pfile->fmt.wChannels = 1;
241 pfile->fmt.wBlockAlign = 1; // wChannels*1byte
242 pfile->fmt.dwMagnitude = 99;
243 pfile->fmt.dwDuration = 0;
244 pfile->data.chunkID = DATA_ID;
245 pfile->data.chunkSize = sizeof(DataChunk);
249 int InsertElement(unsigned char *vibe_buffer, int max_bufsize, HapticElement *element)
258 pfile = (HapticFile*)vibe_buffer;
259 if (_check_valid_haptic_format(pfile) < 0) {
260 MODULE_ERROR("this buffer is not HapticFormat");
264 duration = element->duration/BITPERMS;
265 level = (unsigned char)((unsigned int)element->level*MAX_LEVEL/100);
267 databuf = max_bufsize - sizeof(HapticFile);
268 needbuf = (pfile->fmt.dwDuration + duration)*pfile->fmt.wBlockAlign;
269 MODULE_LOG("Need buffer size : %d", needbuf);
271 if (databuf < needbuf) {
272 MODULE_ERROR("buffer lacks a memory : data buf(%d), need buf(%d)", databuf, needbuf);
276 for (i = pfile->fmt.dwDuration; i < pfile->fmt.dwDuration+duration; i++) {
277 pfile->data.pData[i] = level;
280 pfile->chunkSize = sizeof(HapticFile)+needbuf ;
281 pfile->fmt.dwDuration = pfile->fmt.dwDuration+duration;
282 pfile->data.chunkSize = sizeof(DataChunk)+needbuf;
286 int GetBufferSize(const unsigned char *vibe_buffer, int *size)
290 pfile = (HapticFile*)vibe_buffer;
291 if (_check_valid_haptic_format(pfile) < 0) {
292 MODULE_ERROR("this buffer is not HapticFormat");
296 *size = pfile->chunkSize;
300 int GetBufferDuration(const unsigned char *vibe_buffer, int *duration)
304 pfile = (HapticFile*)vibe_buffer;
305 if (_check_valid_haptic_format(pfile) < 0) {
306 MODULE_ERROR("this buffer is not HapticFormat");
310 *duration = pfile->fmt.dwDuration;
314 int PlayOneshot(int handle, int duration, int level)
319 char buf_duration[32];
322 if (_cancel_thread() < 0) {
323 MODULE_ERROR("_cancel_thread fail");
327 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
328 snprintf(buf_prop, sizeof(buf_prop), "%d", ONESHOT);
329 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
330 snprintf(buf_duration, sizeof(buf_duration), "%d", duration);
331 snprintf(buf_level, sizeof(buf_level), "%d", level);
333 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
334 return __haptic_call_predef_action(PREDEF_HAPTIC, 5, buf_pid, buf_prop,
335 buf_handle, buf_duration, buf_level);
338 int PlayBuffer(int handle, const unsigned char *vibe_buffer, int iteration, int level)
341 unsigned char **ppbuffer;
342 unsigned int channels, length, align;
346 pfile = (HapticFile*)vibe_buffer;
347 if (_check_valid_haptic_format(pfile) < 0) {
348 MODULE_ERROR("this buffer is not HapticFormat");
353 This code does not support handle and multi channel concept.
354 Only this code adds to test for playing file. */
356 if (_cancel_thread() < 0) {
357 MODULE_ERROR("_cancel_thread fail");
361 channels = pfile->fmt.wChannels;
362 align = pfile->fmt.wBlockAlign;
363 length = (pfile->data.chunkSize-8)/align;
364 MODULE_LOG("channels : %d, length : %d, align : %d, level : %d", channels, length, align, level);
367 ppbuffer = (unsigned char**)malloc(sizeof(unsigned char*)*channels);
368 for (i = 0; i < channels; ++i) {
369 ppbuffer[i] = (unsigned char*)malloc(sizeof(unsigned char)*length);
370 memset(ppbuffer[i], 0, sizeof(unsigned char)*length);
373 /* Copy buffer from source buffer */
374 for (i = 0; i < length; ++i) {
375 for (j = 0; j < channels; ++j) {
376 data = (unsigned char)(pfile->data.pData[i*align+j]);
377 ppbuffer[j][i] = (unsigned char)(data*level/0xFF);
378 MODULE_LOG("ppbuffer[%2d][%2d] : data(%x) -> (%x)", j, i, data, ppbuffer[j][i]);
382 gbuffer.handle = handle;
383 gbuffer.ppbuffer = ppbuffer;
384 gbuffer.channels = channels;
385 gbuffer.length = length;
386 gbuffer.iteration = iteration;
388 __haptic_predefine_action(gbuffer.handle, PLAY, NULL);
391 if (_create_thread(&gbuffer, __play_cb) < 0) {
392 MODULE_ERROR("_create_thread fail");
405 if (_cancel_thread() < 0) {
406 MODULE_ERROR("_cancel_thread fail");
410 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
411 snprintf(buf_prop, sizeof(buf_prop), "%d", STOP);
412 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
414 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
415 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
418 int OpenDevice(int handle)
424 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
425 snprintf(buf_prop, sizeof(buf_prop), "%d", OPEN);
426 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
428 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
429 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
432 int CloseDevice(int handle)
438 if (_cancel_thread() < 0) {
439 MODULE_ERROR("_cancel_thread fail");
443 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
444 snprintf(buf_prop, sizeof(buf_prop), "%d", CLOSE);
445 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
447 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
448 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
451 int GetState(int handle, int *state)
453 if (gbuffer.handle == handle) {