1/*
2 * xcam_common.cpp - xcam common
3 *
4 *  Copyright (c) 2014-2015 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <base/xcam_common.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <sys/ioctl.h>
29#include <stdarg.h>
30
31static char log_file_name[XCAM_MAX_STR_SIZE] = {0};
32
33uint32_t xcam_version ()
34{
35    return XCAM_VERSION;
36}
37
38void * xcam_malloc(size_t size)
39{
40    return malloc (size);
41}
42
43void * xcam_malloc0(size_t size)
44{
45    void * ptr = malloc (size);
46    memset (ptr, 0, size);
47    return ptr;
48}
49
50void xcam_free(void *ptr)
51{
52    if (ptr)
53        free (ptr);
54}
55
56int xcam_device_ioctl (int fd, int cmd, void *arg)
57{
58    int ret = 0;
59    int tried_time = 0;
60
61    if (fd < 0)
62        return -1;
63
64    while (1) {
65        ret = ioctl (fd, cmd, arg);
66        if (ret >= 0)
67            break;
68        if (errno != EINTR && errno != EAGAIN)
69            break;
70        if (++tried_time > 5)
71            break;
72    }
73
74    if (ret >= 0) {
75        XCAM_LOG_DEBUG ("ioctl return ok on fd(%d), cmd:%d", fd, cmd);
76    } else {
77        XCAM_LOG_DEBUG ("ioctl failed on fd(%d), cmd:%d, error:%s",
78                        fd, cmd, strerror(errno));
79    }
80    return ret;
81}
82
83const char *
84xcam_fourcc_to_string (uint32_t fourcc)
85{
86    static char str[5];
87
88    xcam_mem_clear (str);
89    memcpy (str, &fourcc, 4);
90    return str;
91}
92
93void xcam_print_log (const char* format, ...) {
94    char buffer[XCAM_MAX_STR_SIZE] = {0};
95
96    va_list va_list;
97    va_start (va_list, format);
98    vsnprintf (buffer, XCAM_MAX_STR_SIZE, format, va_list);
99    va_end (va_list);
100
101    if (strlen (log_file_name) > 0) {
102        FILE* p_file = fopen (log_file_name, "ab+");
103        if (NULL != p_file) {
104            fwrite (buffer, sizeof (buffer[0]), strlen (buffer), p_file);
105            fclose (p_file);
106        } else {
107            printf ("%s", buffer);
108        }
109    } else {
110        printf ("%s", buffer);
111    }
112}
113
114void xcam_set_log (const char* file_name) {
115    if (NULL != file_name) {
116        memset (log_file_name, 0, XCAM_MAX_STR_SIZE);
117        strncpy (log_file_name, file_name, XCAM_MAX_STR_SIZE);
118    }
119}
120
121