Fixed continuous vibration along with system hang.
[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
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                 MODULE_ERROR("pthread_join is failed : %s", strerror(errno));
134                 return -1;
135         }
136
137         stop = 0;
138
139     tid = 0;
140         if (ptr == PTHREAD_CANCELED) {
141                 MODULE_LOG("pthread canceled");
142         } else {
143                 MODULE_LOG("pthread already finished");
144         }
145
146         return 0;
147 }
148
149 static void __clean_up(void *arg)
150 {
151         BUFFER *pbuffer = (BUFFER*)arg;
152         int i;
153
154         MODULE_LOG("clean up handler!!! : %d", tid);
155
156         for (i = 0; i < pbuffer->channels; ++i) {
157                 free(pbuffer->ppbuffer[i]);
158                 pbuffer->ppbuffer[i] = NULL;
159         }
160
161         free(pbuffer->ppbuffer);
162         pbuffer->ppbuffer = NULL;
163
164         pbuffer->channels = 0;
165         pbuffer->length = 0;
166
167         if(stop){
168              __haptic_predefine_action(gbuffer.handle, STOP, NULL);
169              pthread_mutex_unlock(&mutex);
170         }
171 }
172
173 static void* __play_cb(void *arg)
174 {
175         BUFFER *pbuffer = (BUFFER*)arg;
176         int i, j, k;
177         unsigned char ch;
178         unsigned char prev = -1;
179
180         MODULE_LOG("Start thread");
181
182         pthread_cleanup_push(__clean_up, arg);
183
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);
189                                 if (stop) {
190                                         pthread_exit((void*)0);
191                                 }
192                                 ch = pbuffer->ppbuffer[k][j];
193                                 if (ch != prev) {
194                                         __haptic_predefine_action(pbuffer->handle, LEVEL, ch);
195                                         prev = ch;
196                                 }
197                                 pthread_mutex_unlock(&mutex);
198                                 usleep(BITPERMS * 1000);
199                         }
200                 }
201         }
202
203         pthread_mutex_lock(&mutex);
204         __haptic_predefine_action(gbuffer.handle, STOP, NULL);
205         pthread_mutex_unlock(&mutex);
206
207         pthread_cleanup_pop(1);
208         pthread_exit((void *)0);
209 }
210
211 int GetHapticLevelMax(int *max)
212 {
213         int status;
214         status = device_get_property(DEVICE_TYPE_VIBRATOR, PROP_VIBRATOR_LEVEL_MAX, max);
215         if (status < 0) {
216                 MODULE_ERROR("device_get_property fail : %d", status);
217                 return -1;
218         }
219         return 0;
220 }
221
222 int InitializeBuffer(unsigned char *vibe_buffer, int max_bufsize)
223 {
224         HapticFile *pfile;
225
226         if (max_bufsize < sizeof(HapticFile)) {
227                 MODULE_ERROR("buffer lacks a memory : size(%d) minimum size(%d)",
228                                         max_bufsize, sizeof(HapticFile));
229                 return -1;
230         }
231
232         memset(vibe_buffer, 0, sizeof(char)*max_bufsize);
233
234         pfile = (HapticFile*)vibe_buffer;
235
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);
246         return 0;
247 }
248
249 int InsertElement(unsigned char *vibe_buffer, int max_bufsize, HapticElement *element)
250 {
251         HapticFile *pfile;
252         int databuf;
253         int needbuf;
254         int duration;
255         unsigned char level;
256         int i;
257
258         pfile = (HapticFile*)vibe_buffer;
259         if (_check_valid_haptic_format(pfile) < 0) {
260                 MODULE_ERROR("this buffer is not HapticFormat");
261                 return -1;
262         }
263
264         duration = element->duration/BITPERMS;
265         level = (unsigned char)((unsigned int)element->level*MAX_LEVEL/100);
266
267         databuf = max_bufsize - sizeof(HapticFile);
268         needbuf = (pfile->fmt.dwDuration + duration)*pfile->fmt.wBlockAlign;
269         MODULE_LOG("Need buffer size : %d", needbuf);
270
271         if (databuf < needbuf) {
272                 MODULE_ERROR("buffer lacks a memory : data buf(%d), need buf(%d)", databuf, needbuf);
273                 return -1;
274         }
275
276         for (i = pfile->fmt.dwDuration; i < pfile->fmt.dwDuration+duration; i++) {
277                 pfile->data.pData[i] = level;
278         }
279
280         pfile->chunkSize = sizeof(HapticFile)+needbuf ;
281         pfile->fmt.dwDuration = pfile->fmt.dwDuration+duration;
282         pfile->data.chunkSize = sizeof(DataChunk)+needbuf;
283         return 0;
284 }
285
286 int GetBufferSize(const unsigned char *vibe_buffer, int *size)
287 {
288         HapticFile *pfile;
289
290         pfile = (HapticFile*)vibe_buffer;
291         if (_check_valid_haptic_format(pfile) < 0) {
292                 MODULE_ERROR("this buffer is not HapticFormat");
293                 return -1;
294         }
295
296         *size = pfile->chunkSize;
297         return 0;
298 }
299
300 int GetBufferDuration(const unsigned char *vibe_buffer, int *duration)
301 {
302         HapticFile *pfile;
303
304         pfile = (HapticFile*)vibe_buffer;
305         if (_check_valid_haptic_format(pfile) < 0) {
306                 MODULE_ERROR("this buffer is not HapticFormat");
307                 return -1;
308         }
309
310         *duration = pfile->fmt.dwDuration;
311         return 0;
312 }
313
314 int PlayOneshot(int handle, int duration, int level)
315 {
316         char buf_pid[32];
317         char buf_prop[32];
318         char buf_handle[32];
319         char buf_duration[32];
320         char buf_level[32];
321
322         if (_cancel_thread() < 0) {
323                 MODULE_ERROR("_cancel_thread fail");
324                 return -1;
325         }
326
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);
332
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);
336 }
337
338 int PlayBuffer(int handle, const unsigned char *vibe_buffer, int iteration, int level)
339 {
340         HapticFile *pfile;
341         unsigned char **ppbuffer;
342         unsigned int channels, length, align;
343         unsigned char data;
344         int i, j;
345
346         pfile = (HapticFile*)vibe_buffer;
347         if (_check_valid_haptic_format(pfile) < 0) {
348                 MODULE_ERROR("this buffer is not HapticFormat");
349                 return -1;
350         }
351
352         /* Temporary code
353            This code does not support handle and multi channel concept.
354            Only this code adds to test for playing file. */
355
356         if (_cancel_thread() < 0) {
357                 MODULE_ERROR("_cancel_thread fail");
358                 return -1;
359         }
360
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);
365
366         /* Create buffer */
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);
371         }
372
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]);
379                 }
380         }
381
382         gbuffer.handle = handle;
383         gbuffer.ppbuffer = ppbuffer;
384         gbuffer.channels = channels;
385         gbuffer.length = length;
386         gbuffer.iteration = iteration;
387
388         __haptic_predefine_action(gbuffer.handle, PLAY, NULL);
389
390         /* Start thread */
391         if (_create_thread(&gbuffer, __play_cb) < 0) {
392                 MODULE_ERROR("_create_thread fail");
393                 return -1;
394         }
395
396         return 0;
397 }
398
399 int Stop(int handle)
400 {
401         char buf_pid[32];
402         char buf_prop[32];
403         char buf_handle[32];
404
405         if (_cancel_thread() < 0) {
406                 MODULE_ERROR("_cancel_thread fail");
407                 return -1;
408         }
409
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);
413
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);
416 }
417
418 int OpenDevice(int handle)
419 {
420         char buf_pid[32];
421         char buf_prop[32];
422         char buf_handle[32];
423
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);
427
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);
430 }
431
432 int CloseDevice(int handle)
433 {
434         char buf_pid[32];
435         char buf_prop[32];
436         char buf_handle[32];
437
438         if (_cancel_thread() < 0) {
439                 MODULE_ERROR("_cancel_thread fail");
440                 return -1;
441         }
442
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);
446
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);
449 }
450
451 int GetState(int handle, int *state)
452 {
453         if (gbuffer.handle == handle) {
454                 *state = STATE_PLAY;
455                 return 0;
456         }
457
458         *state = STATE_STOP;
459         return 0;
460 }