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;
60 static int _check_valid_haptic_format(HapticFile *file)
62 if (file->chunkID != HEADER_ID)
65 if (file->fmt.chunkID != FMT_ID)
68 if (file->data.chunkID != DATA_ID)
74 static int __haptic_predefine_action(int handle, int prop, int val)
81 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
82 snprintf(buf_prop, sizeof(buf_prop), "%d", prop);
83 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
84 snprintf(buf_val, sizeof(buf_val), "%d", val);
86 MODULE_LOG("pid : %s(%d), prop : %s, handle : %s", buf_pid, pthread_self(), buf_prop, buf_handle);
87 return __haptic_call_predef_action(PREDEF_HAPTIC, 4, buf_pid, buf_prop, buf_handle, buf_val);
90 static int _create_thread(void* data, void*(*func)(void*))
93 MODULE_ERROR("pthread already created");
97 if (pthread_create(&tid, NULL, func, data) != 0) {
98 MODULE_ERROR("pthread_create is failed : %s", strerror(errno));
105 static int _cancel_thread(void)
111 MODULE_LOG("pthread not initialized");
115 MODULE_LOG("lock state : %d", lock);
118 MODULE_LOG("already locked...");
121 __haptic_predefine_action(gbuffer.handle, STOP, NULL);
123 if ((ret = pthread_cancel(tid)) < 0) {
124 MODULE_ERROR("pthread_cancel is failed : %s, ret(%d)", strerror(errno), ret);
128 if (pthread_join(tid, (void**)&ptr) < 0) {
130 MODULE_ERROR("pthread_join is failed : %s", strerror(errno));
135 if (ptr == PTHREAD_CANCELED) {
136 MODULE_LOG("pthread canceled");
138 MODULE_LOG("pthread already finished");
144 static void __clean_up(void *arg)
146 BUFFER *pbuffer = (BUFFER*)arg;
149 MODULE_LOG("clean up handler!!! : %d", tid);
151 for (i = 0; i < pbuffer->channels; ++i) {
152 free(pbuffer->ppbuffer[i]);
153 pbuffer->ppbuffer[i] = NULL;
156 free(pbuffer->ppbuffer);
157 pbuffer->ppbuffer = NULL;
159 pbuffer->channels = 0;
163 static void* __play_cb(void *arg)
165 BUFFER *pbuffer = (BUFFER*)arg;
168 unsigned char prev = -1;
170 MODULE_LOG("Start thread");
172 pthread_cleanup_push(__clean_up, arg);
174 /* Copy buffer from source buffer */
175 for (i = 0; i < pbuffer->iteration; i++) {
176 for (j = 0; j < pbuffer->length; ++j) {
177 for (k = 0; k < pbuffer->channels; ++k) {
178 ch = pbuffer->ppbuffer[k][j];
181 __haptic_predefine_action(pbuffer->handle, LEVEL, ch);
185 usleep(BITPERMS * 1000);
190 pthread_cleanup_pop(1);
191 pthread_exit((void *)0);
194 int GetHapticLevelMax(int *max)
197 status = device_get_property(DEVICE_TYPE_VIBRATOR, PROP_VIBRATOR_LEVEL_MAX, max);
199 MODULE_ERROR("device_get_property fail : %d", status);
205 int InitializeBuffer(unsigned char *vibe_buffer, int max_bufsize)
209 if (max_bufsize < sizeof(HapticFile)) {
210 MODULE_ERROR("buffer lacks a memory : size(%d) minimum size(%d)",
211 max_bufsize, sizeof(HapticFile));
215 memset(vibe_buffer, 0, sizeof(char)*max_bufsize);
217 pfile = (HapticFile*)vibe_buffer;
219 pfile->chunkID = HEADER_ID;
220 pfile->chunkSize = sizeof(HapticFile);
221 pfile->fmt.chunkID = FMT_ID;
222 pfile->fmt.chunkSize = sizeof(FormatChunk);
223 pfile->fmt.wChannels = 1;
224 pfile->fmt.wBlockAlign = 1; // wChannels*1byte
225 pfile->fmt.dwMagnitude = 99;
226 pfile->fmt.dwDuration = 0;
227 pfile->data.chunkID = DATA_ID;
228 pfile->data.chunkSize = sizeof(DataChunk);
232 int InsertElement(unsigned char *vibe_buffer, int max_bufsize, HapticElement *element)
241 pfile = (HapticFile*)vibe_buffer;
242 if (_check_valid_haptic_format(pfile) < 0) {
243 MODULE_ERROR("this buffer is not HapticFormat");
247 duration = element->duration/BITPERMS;
248 level = (unsigned char)((unsigned int)element->level*MAX_LEVEL/100);
250 databuf = max_bufsize - sizeof(HapticFile);
251 needbuf = (pfile->fmt.dwDuration + duration)*pfile->fmt.wBlockAlign;
252 MODULE_LOG("Need buffer size : %d", needbuf);
254 if (databuf < needbuf) {
255 MODULE_ERROR("buffer lacks a memory : data buf(%d), need buf(%d)", databuf, needbuf);
259 for (i = pfile->fmt.dwDuration; i < pfile->fmt.dwDuration+duration; i++) {
260 pfile->data.pData[i] = level;
263 pfile->chunkSize = sizeof(HapticFile)+needbuf ;
264 pfile->fmt.dwDuration = pfile->fmt.dwDuration+duration;
265 pfile->data.chunkSize = sizeof(DataChunk)+needbuf;
269 int GetBufferSize(const unsigned char *vibe_buffer, int *size)
273 pfile = (HapticFile*)vibe_buffer;
274 if (_check_valid_haptic_format(pfile) < 0) {
275 MODULE_ERROR("this buffer is not HapticFormat");
279 *size = pfile->chunkSize;
283 int GetBufferDuration(const unsigned char *vibe_buffer, int *duration)
287 pfile = (HapticFile*)vibe_buffer;
288 if (_check_valid_haptic_format(pfile) < 0) {
289 MODULE_ERROR("this buffer is not HapticFormat");
293 *duration = pfile->fmt.dwDuration;
297 int PlayOneshot(int handle, int duration, int level)
302 char buf_duration[32];
305 if (_cancel_thread() < 0) {
306 MODULE_ERROR("_cancel_thread fail");
310 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
311 snprintf(buf_prop, sizeof(buf_prop), "%d", ONESHOT);
312 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
313 snprintf(buf_duration, sizeof(buf_duration), "%d", duration);
314 snprintf(buf_level, sizeof(buf_level), "%d", level);
316 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
317 return __haptic_call_predef_action(PREDEF_HAPTIC, 5, buf_pid, buf_prop,
318 buf_handle, buf_duration, buf_level);
321 int PlayBuffer(int handle, const unsigned char *vibe_buffer, int iteration, int level)
324 unsigned char **ppbuffer;
325 unsigned int channels, length, align;
329 pfile = (HapticFile*)vibe_buffer;
330 if (_check_valid_haptic_format(pfile) < 0) {
331 MODULE_ERROR("this buffer is not HapticFormat");
336 This code does not support handle and multi channel concept.
337 Only this code adds to test for playing file. */
339 if (_cancel_thread() < 0) {
340 MODULE_ERROR("_cancel_thread fail");
344 channels = pfile->fmt.wChannels;
345 align = pfile->fmt.wBlockAlign;
346 length = (pfile->data.chunkSize-8)/align;
347 MODULE_LOG("channels : %d, length : %d, align : %d, level : %d", channels, length, align, level);
350 ppbuffer = (unsigned char**)malloc(sizeof(unsigned char*)*channels);
351 for (i = 0; i < channels; ++i) {
352 ppbuffer[i] = (unsigned char*)malloc(sizeof(unsigned char)*length);
353 memset(ppbuffer[i], 0, sizeof(unsigned char)*length);
356 /* Copy buffer from source buffer */
357 for (i = 0; i < length; ++i) {
358 for (j = 0; j < channels; ++j) {
359 data = (unsigned char)(pfile->data.pData[i*align+j]);
360 ppbuffer[j][i] = (unsigned char)(data*level/0xFF);
361 MODULE_LOG("ppbuffer[%2d][%2d] : data(%x) -> (%x)", j, i, data, ppbuffer[j][i]);
365 gbuffer.handle = handle;
366 gbuffer.ppbuffer = ppbuffer;
367 gbuffer.channels = channels;
368 gbuffer.length = length;
369 gbuffer.iteration = iteration;
371 __haptic_predefine_action(gbuffer.handle, PLAY, NULL);
374 if (_create_thread(&gbuffer, __play_cb) < 0) {
375 MODULE_ERROR("_create_thread fail");
388 if (_cancel_thread() < 0) {
389 MODULE_ERROR("_cancel_thread fail");
393 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
394 snprintf(buf_prop, sizeof(buf_prop), "%d", STOP);
395 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
397 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
398 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
401 int OpenDevice(int handle)
407 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
408 snprintf(buf_prop, sizeof(buf_prop), "%d", OPEN);
409 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
411 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
412 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
415 int CloseDevice(int handle)
421 if (_cancel_thread() < 0) {
422 MODULE_ERROR("_cancel_thread fail");
426 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
427 snprintf(buf_prop, sizeof(buf_prop), "%d", CLOSE);
428 snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
430 MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
431 return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
434 int GetState(int handle, int *state)
436 if (gbuffer.handle == handle) {