eea51e86dc718ea8b9754ea0161d51d459e82f9b
[platform/core/system/haptic-module-tizen.git] / tizen / DEVICE / src / file.c
1 /*
2  * haptic-module-tizen
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <pthread.h>
25 #include <device-node.h>
26
27 #include "file.h"
28 #include "haptic_module_log.h"
29
30 #define BITPERMS                                50
31 #define MAX_LEVEL                               255.0f
32 #define DEFAULT_EFFECT_HANDLE   0x02
33
34 #define STATE_PLAY      0
35 #define STATE_STOP      1
36
37 #define PREDEF_HAPTIC           "haptic"
38
39 enum {
40         OPEN = 0,
41         CLOSE,
42         PLAY,
43         ONESHOT,
44         STOP,
45         LEVEL,
46 };
47
48 typedef struct {
49         int handle;
50         unsigned char **ppbuffer;
51         int channels;
52         int length;
53         int iteration;
54 } BUFFER;
55
56 static pthread_t tid;
57 static BUFFER gbuffer;
58 static int stop;
59 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
60
61 static int _check_valid_haptic_format(HapticFile *file)
62 {
63         if (file->chunkID != HEADER_ID)
64                 return -1;
65
66         if (file->fmt.chunkID != FMT_ID)
67                 return -1;
68
69         if (file->data.chunkID != DATA_ID)
70                 return -1;
71
72         return 0;
73 }
74
75 static int __haptic_predefine_action(int handle, int prop, int val)
76 {
77         char buf_pid[32];
78         char buf_prop[32];
79         char buf_handle[32];
80         char buf_val[32];
81
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);
86
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);
89 }
90
91 static int _create_thread(void* data, void*(*func)(void*))
92 {
93         if (tid) {
94                 MODULE_ERROR("pthread already created");
95                 return -1;
96         }
97
98         if (pthread_create(&tid, NULL, func, data) != 0) {
99                 MODULE_ERROR("pthread_create is failed : %s", strerror(errno));
100                 return -1;
101         }
102
103         return 0;
104 }
105
106 static int _cancel_thread(void)
107 {
108         int *ptr;
109         int ret;
110
111         if (!tid) {
112                 MODULE_LOG("pthread not initialized");
113                 return 0;
114         }
115
116         MODULE_LOG("cancel thread!!!");
117
118         stop = 1;
119
120         while (pthread_mutex_trylock(&mutex) == EBUSY) {
121                 usleep(100);
122                 MODULE_LOG("Already locked..");
123         }
124         __haptic_predefine_action(gbuffer.handle, STOP, NULL);
125         pthread_mutex_unlock(&mutex);
126
127         if ((ret = pthread_cancel(tid)) < 0) {
128                 MODULE_ERROR("pthread_cancel is failed : %s, ret(%d)", strerror(errno), ret);
129                 return -1;
130         }
131
132         if (pthread_join(tid, (void**)&ptr) < 0) {
133         tid = 0;
134                 MODULE_ERROR("pthread_join is failed : %s", strerror(errno));
135                 return -1;
136         }
137
138     tid = 0;
139         if (ptr == PTHREAD_CANCELED) {
140                 MODULE_LOG("pthread canceled");
141         } else {
142                 MODULE_LOG("pthread already finished");
143         }
144
145         return 0;
146 }
147
148 static void __clean_up(void *arg)
149 {
150         BUFFER *pbuffer = (BUFFER*)arg;
151         int i;
152
153         MODULE_LOG("clean up handler!!! : %d", tid);
154
155         stop = 0;
156
157         for (i = 0; i < pbuffer->channels; ++i) {
158                 free(pbuffer->ppbuffer[i]);
159                 pbuffer->ppbuffer[i] = NULL;
160         }
161
162         free(pbuffer->ppbuffer);
163         pbuffer->ppbuffer = NULL;
164
165         pbuffer->channels = 0;
166         pbuffer->length = 0;
167 }
168
169 static void* __play_cb(void *arg)
170 {
171         BUFFER *pbuffer = (BUFFER*)arg;
172         int i, j, k;
173         unsigned char ch;
174         unsigned char prev = -1;
175
176         MODULE_LOG("Start thread");
177
178         pthread_cleanup_push(__clean_up, arg);
179
180         /* Copy buffer from source buffer */
181         for (i = 0; i < pbuffer->iteration; i++) {
182                 for (j = 0; j < pbuffer->length; ++j) {
183                         for (k = 0; k < pbuffer->channels; ++k) {
184                                 pthread_mutex_lock(&mutex);
185                                 if (stop) {
186                                         pthread_mutex_unlock(&mutex);
187                                         pthread_exit((void*)0);
188                                 }
189                                 ch = pbuffer->ppbuffer[k][j];
190                                 if (ch != prev) {
191                                         __haptic_predefine_action(pbuffer->handle, LEVEL, ch);
192                                         prev = ch;
193                                 }
194                                 pthread_mutex_unlock(&mutex);
195                                 usleep(BITPERMS * 1000);
196                         }
197                 }
198         }
199
200         pthread_cleanup_pop(1);
201         pthread_exit((void *)0);
202 }
203
204 int GetHapticLevelMax(int *max)
205 {
206         int status;
207         status = device_get_property(DEVICE_TYPE_VIBRATOR, PROP_VIBRATOR_LEVEL_MAX, max);
208         if (status < 0) {
209                 MODULE_ERROR("device_get_property fail : %d", status);
210                 return -1;
211         }
212         return 0;
213 }
214
215 int InitializeBuffer(unsigned char *vibe_buffer, int max_bufsize)
216 {
217         HapticFile *pfile;
218
219         if (max_bufsize < sizeof(HapticFile)) {
220                 MODULE_ERROR("buffer lacks a memory : size(%d) minimum size(%d)",
221                                         max_bufsize, sizeof(HapticFile));
222                 return -1;
223         }
224
225         memset(vibe_buffer, 0, sizeof(char)*max_bufsize);
226
227         pfile = (HapticFile*)vibe_buffer;
228
229         pfile->chunkID = HEADER_ID;
230         pfile->chunkSize = sizeof(HapticFile);
231         pfile->fmt.chunkID = FMT_ID;
232         pfile->fmt.chunkSize = sizeof(FormatChunk);
233         pfile->fmt.wChannels = 1;
234         pfile->fmt.wBlockAlign = 1;     // wChannels*1byte
235         pfile->fmt.dwMagnitude = 99;
236         pfile->fmt.dwDuration = 0;
237         pfile->data.chunkID = DATA_ID;
238         pfile->data.chunkSize = sizeof(DataChunk);
239         return 0;
240 }
241
242 int InsertElement(unsigned char *vibe_buffer, int max_bufsize, HapticElement *element)
243 {
244         HapticFile *pfile;
245         int databuf;
246         int needbuf;
247         int duration;
248         unsigned char level;
249         int i;
250
251         pfile = (HapticFile*)vibe_buffer;
252         if (_check_valid_haptic_format(pfile) < 0) {
253                 MODULE_ERROR("this buffer is not HapticFormat");
254                 return -1;
255         }
256
257         duration = element->duration/BITPERMS;
258         level = (unsigned char)((unsigned int)element->level*MAX_LEVEL/100);
259
260         databuf = max_bufsize - sizeof(HapticFile);
261         needbuf = (pfile->fmt.dwDuration + duration)*pfile->fmt.wBlockAlign;
262         MODULE_LOG("Need buffer size : %d", needbuf);
263
264         if (databuf < needbuf) {
265                 MODULE_ERROR("buffer lacks a memory : data buf(%d), need buf(%d)", databuf, needbuf);
266                 return -1;
267         }
268
269         for (i = pfile->fmt.dwDuration; i < pfile->fmt.dwDuration+duration; i++) {
270                 pfile->data.pData[i] = level;
271         }
272
273         pfile->chunkSize = sizeof(HapticFile)+needbuf ;
274         pfile->fmt.dwDuration = pfile->fmt.dwDuration+duration;
275         pfile->data.chunkSize = sizeof(DataChunk)+needbuf;
276         return 0;
277 }
278
279 int GetBufferSize(const unsigned char *vibe_buffer, int *size)
280 {
281         HapticFile *pfile;
282
283         pfile = (HapticFile*)vibe_buffer;
284         if (_check_valid_haptic_format(pfile) < 0) {
285                 MODULE_ERROR("this buffer is not HapticFormat");
286                 return -1;
287         }
288
289         *size = pfile->chunkSize;
290         return 0;
291 }
292
293 int GetBufferDuration(const unsigned char *vibe_buffer, int *duration)
294 {
295         HapticFile *pfile;
296
297         pfile = (HapticFile*)vibe_buffer;
298         if (_check_valid_haptic_format(pfile) < 0) {
299                 MODULE_ERROR("this buffer is not HapticFormat");
300                 return -1;
301         }
302
303         *duration = pfile->fmt.dwDuration;
304         return 0;
305 }
306
307 int PlayOneshot(int handle, int duration, int level)
308 {
309         char buf_pid[32];
310         char buf_prop[32];
311         char buf_handle[32];
312         char buf_duration[32];
313         char buf_level[32];
314
315         if (_cancel_thread() < 0) {
316                 MODULE_ERROR("_cancel_thread fail");
317                 return -1;
318         }
319
320         snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
321         snprintf(buf_prop, sizeof(buf_prop), "%d", ONESHOT);
322         snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
323         snprintf(buf_duration, sizeof(buf_duration), "%d", duration);
324         snprintf(buf_level, sizeof(buf_level), "%d", level);
325
326         MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
327         return __haptic_call_predef_action(PREDEF_HAPTIC, 5, buf_pid, buf_prop,
328                                                                                 buf_handle, buf_duration, buf_level);
329 }
330
331 int PlayBuffer(int handle, const unsigned char *vibe_buffer, int iteration, int level)
332 {
333         HapticFile *pfile;
334         unsigned char **ppbuffer;
335         unsigned int channels, length, align;
336         unsigned char data;
337         int i, j;
338
339         pfile = (HapticFile*)vibe_buffer;
340         if (_check_valid_haptic_format(pfile) < 0) {
341                 MODULE_ERROR("this buffer is not HapticFormat");
342                 return -1;
343         }
344
345         /* Temporary code
346            This code does not support handle and multi channel concept.
347            Only this code adds to test for playing file. */
348
349         if (_cancel_thread() < 0) {
350                 MODULE_ERROR("_cancel_thread fail");
351                 return -1;
352         }
353
354         channels = pfile->fmt.wChannels;
355         align = pfile->fmt.wBlockAlign;
356         length = (pfile->data.chunkSize-8)/align;
357         MODULE_LOG("channels : %d, length : %d, align : %d, level : %d", channels, length, align, level);
358
359         /* Create buffer */
360         ppbuffer = (unsigned char**)malloc(sizeof(unsigned char*)*channels);
361         for (i = 0; i < channels; ++i) {
362                 ppbuffer[i] = (unsigned char*)malloc(sizeof(unsigned char)*length);
363                 memset(ppbuffer[i], 0, sizeof(unsigned char)*length);
364         }
365
366         /* Copy buffer from source buffer */
367         for (i = 0; i < length; ++i) {
368                 for (j = 0; j < channels; ++j) {
369                         data = (unsigned char)(pfile->data.pData[i*align+j]);
370                         ppbuffer[j][i] = (unsigned char)(data*level/0xFF);
371                         MODULE_LOG("ppbuffer[%2d][%2d] : data(%x) -> (%x)", j, i, data, ppbuffer[j][i]);
372                 }
373         }
374
375         gbuffer.handle = handle;
376         gbuffer.ppbuffer = ppbuffer;
377         gbuffer.channels = channels;
378         gbuffer.length = length;
379         gbuffer.iteration = iteration;
380
381         __haptic_predefine_action(gbuffer.handle, PLAY, NULL);
382
383         /* Start thread */
384         if (_create_thread(&gbuffer, __play_cb) < 0) {
385                 MODULE_ERROR("_create_thread fail");
386                 return -1;
387         }
388
389         return 0;
390 }
391
392 int Stop(int handle)
393 {
394         char buf_pid[32];
395         char buf_prop[32];
396         char buf_handle[32];
397
398         if (_cancel_thread() < 0) {
399                 MODULE_ERROR("_cancel_thread fail");
400                 return -1;
401         }
402
403         snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
404         snprintf(buf_prop, sizeof(buf_prop), "%d", STOP);
405         snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
406
407         MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
408         return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
409 }
410
411 int OpenDevice(int handle)
412 {
413         char buf_pid[32];
414         char buf_prop[32];
415         char buf_handle[32];
416
417         snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
418         snprintf(buf_prop, sizeof(buf_prop), "%d", OPEN);
419         snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
420
421         MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
422         return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
423 }
424
425 int CloseDevice(int handle)
426 {
427         char buf_pid[32];
428         char buf_prop[32];
429         char buf_handle[32];
430
431         if (_cancel_thread() < 0) {
432                 MODULE_ERROR("_cancel_thread fail");
433                 return -1;
434         }
435
436         snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
437         snprintf(buf_prop, sizeof(buf_prop), "%d", CLOSE);
438         snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
439
440         MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
441         return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
442 }
443
444 int GetState(int handle, int *state)
445 {
446         if (gbuffer.handle == handle) {
447                 *state = STATE_PLAY;
448                 return 0;
449         }
450
451         *state = STATE_STOP;
452         return 0;
453 }