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
34 #define PREDEF_HAPTIC "haptic"
47 unsigned char **ppbuffer;
54 static BUFFER gbuffer;
56 static int _check_valid_haptic_format(HapticFile *file)
58 if (file->chunkID != HEADER_ID)
61 if (file->fmt.chunkID != FMT_ID)
64 if (file->data.chunkID != DATA_ID)
70 static int _create_thread(void* data, void*(*func)(void*))
73 MODULE_ERROR("pthread already created");
77 if (pthread_create(&tid, NULL, func, data) != 0) {
78 MODULE_ERROR("pthread_create is failed : %s", strerror(errno));
85 static int _cancel_thread(void)
91 MODULE_LOG("pthread not initialized");
95 if ((ret = pthread_cancel(tid)) < 0) {
96 MODULE_ERROR("pthread_cancel is failed : %s, ret(%d)", strerror(errno), ret);
100 if (pthread_join(tid, (void**)&ptr) < 0) {
102 MODULE_ERROR("pthread_join is failed : %s", strerror(errno));
107 if (ptr == PTHREAD_CANCELED) {
108 MODULE_LOG("pthread canceled");
110 MODULE_LOG("pthread already finished");
116 static int __haptic_predefine_action(int handle, int prop, int val)
123 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
124 snprintf(buf_prop, sizeof(buf_prop), "%d", prop);
125 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
126 snprintf(buf_val, sizeof(buf_val), "%d", val);
128 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
129 return __haptic_call_predef_action(PREDEF_HAPTIC, 4, buf_pid, buf_prop, buf_handle, buf_val);
132 static void __clean_up(void *arg)
134 BUFFER *pbuffer = (BUFFER*)arg;
137 MODULE_LOG("clean up handler!!! : %d", tid);
138 __haptic_predefine_action(pbuffer->handle, STOP, NULL);
140 for (i = 0; i < pbuffer->channels; ++i) {
141 free(pbuffer->ppbuffer[i]);
142 pbuffer->ppbuffer[i] = NULL;
145 free(pbuffer->ppbuffer);
146 pbuffer->ppbuffer = NULL;
148 pbuffer->channels = 0;
152 static void* __play_cb(void *arg)
154 BUFFER *pbuffer = (BUFFER*)arg;
157 unsigned char prev = -1;
159 MODULE_LOG("Start thread");
161 pthread_cleanup_push(__clean_up, arg);
163 __haptic_predefine_action(pbuffer->handle, PLAY, NULL);
165 /* Copy buffer from source buffer */
166 for (i = 0; i < pbuffer->iteration; i++) {
167 for (j = 0; j < pbuffer->length; ++j) {
168 for (k = 0; k < pbuffer->channels; ++k) {
169 ch = pbuffer->ppbuffer[k][j];
171 __haptic_predefine_action(pbuffer->handle, LEVEL, ch);
174 usleep(BITPERMS * 1000);
179 pthread_cleanup_pop(1);
180 pthread_exit((void *)0);
183 int GetHapticLevelMax(int *max)
186 status = device_get_property(DEVICE_TYPE_VIBRATOR, PROP_VIBRATOR_LEVEL_MAX, max);
188 MODULE_ERROR("device_get_property fail : %d", status);
194 int InitializeBuffer(unsigned char *vibe_buffer, int max_bufsize)
198 if (max_bufsize < sizeof(HapticFile)) {
199 MODULE_ERROR("buffer lacks a memory : size(%d) minimum size(%d)",
200 max_bufsize, sizeof(HapticFile));
204 memset(vibe_buffer, 0, sizeof(char)*max_bufsize);
206 pfile = (HapticFile*)vibe_buffer;
208 pfile->chunkID = HEADER_ID;
209 pfile->chunkSize = sizeof(HapticFile);
210 pfile->fmt.chunkID = FMT_ID;
211 pfile->fmt.chunkSize = sizeof(FormatChunk);
212 pfile->fmt.wChannels = 1;
213 pfile->fmt.wBlockAlign = 1; // wChannels*1byte
214 pfile->fmt.dwMagnitude = 99;
215 pfile->fmt.dwDuration = 0;
216 pfile->data.chunkID = DATA_ID;
217 pfile->data.chunkSize = sizeof(DataChunk);
221 int InsertElement(unsigned char *vibe_buffer, int max_bufsize, HapticElement *element)
230 pfile = (HapticFile*)vibe_buffer;
231 if (_check_valid_haptic_format(pfile) < 0) {
232 MODULE_ERROR("this buffer is not HapticFormat");
236 duration = element->duration/BITPERMS;
237 level = (unsigned char)((unsigned int)element->level*MAX_LEVEL/100);
239 databuf = max_bufsize - sizeof(HapticFile);
240 needbuf = (pfile->fmt.dwDuration + duration)*pfile->fmt.wBlockAlign;
241 MODULE_LOG("Need buffer size : %d", needbuf);
243 if (databuf < needbuf) {
244 MODULE_ERROR("buffer lacks a memory : data buf(%d), need buf(%d)", databuf, needbuf);
248 for (i = pfile->fmt.dwDuration; i < pfile->fmt.dwDuration+duration; i++) {
249 pfile->data.pData[i] = level;
252 pfile->chunkSize = sizeof(HapticFile)+needbuf ;
253 pfile->fmt.dwDuration = pfile->fmt.dwDuration+duration;
254 pfile->data.chunkSize = sizeof(DataChunk)+needbuf;
258 int GetBufferSize(const unsigned char *vibe_buffer, int *size)
262 pfile = (HapticFile*)vibe_buffer;
263 if (_check_valid_haptic_format(pfile) < 0) {
264 MODULE_ERROR("this buffer is not HapticFormat");
268 *size = pfile->chunkSize;
272 int GetBufferDuration(const unsigned char *vibe_buffer, int *duration)
276 pfile = (HapticFile*)vibe_buffer;
277 if (_check_valid_haptic_format(pfile) < 0) {
278 MODULE_ERROR("this buffer is not HapticFormat");
282 *duration = pfile->fmt.dwDuration;
286 int PlayOneshot(int handle, int duration, int level)
291 char buf_duration[32];
294 if (_cancel_thread() < 0) {
295 MODULE_ERROR("_cancel_thread fail");
299 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
300 snprintf(buf_prop, sizeof(buf_prop), "%d", ONESHOT);
301 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
302 snprintf(buf_duration, sizeof(buf_duration), "%d", duration);
303 snprintf(buf_level, sizeof(buf_level), "%d", level);
305 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
306 return __haptic_call_predef_action(PREDEF_HAPTIC, 5, buf_pid, buf_prop,
307 buf_handle, buf_duration, buf_level);
310 int PlayBuffer(int handle, const unsigned char *vibe_buffer, int iteration, int level)
313 unsigned char **ppbuffer;
314 unsigned int channels, length, align;
318 pfile = (HapticFile*)vibe_buffer;
319 if (_check_valid_haptic_format(pfile) < 0) {
320 MODULE_ERROR("this buffer is not HapticFormat");
325 This code does not support handle and multi channel concept.
326 Only this code adds to test for playing file. */
328 if (_cancel_thread() < 0) {
329 MODULE_ERROR("_cancel_thread fail");
333 channels = pfile->fmt.wChannels;
334 align = pfile->fmt.wBlockAlign;
335 length = (pfile->data.chunkSize-8)/align;
336 MODULE_LOG("channels : %d, length : %d, align : %d, level : %d", channels, length, align, level);
339 ppbuffer = (unsigned char**)malloc(sizeof(unsigned char*)*channels);
340 for (i = 0; i < channels; ++i) {
341 ppbuffer[i] = (unsigned char*)malloc(sizeof(unsigned char)*length);
342 memset(ppbuffer[i], 0, sizeof(unsigned char)*length);
345 /* Copy buffer from source buffer */
346 for (i = 0; i < length; ++i) {
347 for (j = 0; j < channels; ++j) {
348 data = (unsigned char)(pfile->data.pData[i*align+j]);
349 ppbuffer[j][i] = (unsigned char)(data*level/0xFF);
350 MODULE_LOG("ppbuffer[%2d][%2d] : data(%x) -> (%x)", j, i, data, ppbuffer[j][i]);
354 gbuffer.handle = handle;
355 gbuffer.ppbuffer = ppbuffer;
356 gbuffer.channels = channels;
357 gbuffer.length = length;
358 gbuffer.iteration = iteration;
361 if (_create_thread(&gbuffer, __play_cb) < 0) {
362 MODULE_ERROR("_create_thread fail");
375 if (_cancel_thread() < 0) {
376 MODULE_ERROR("_cancel_thread fail");
380 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
381 snprintf(buf_prop, sizeof(buf_prop), "%d", STOP);
382 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
384 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
385 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
388 int OpenDevice(int handle)
394 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
395 snprintf(buf_prop, sizeof(buf_prop), "%d", OPEN);
396 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
398 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
399 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
402 int CloseDevice(int handle)
408 if (_cancel_thread() < 0) {
409 MODULE_ERROR("_cancel_thread fail");
413 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
414 snprintf(buf_prop, sizeof(buf_prop), "%d", CLOSE);
415 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
417 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
418 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);