blob: 386ed04a506d0014356a62967d45e5e5a37a475d [file] [log] [blame]
Wind Yuan75564b12015-01-15 06:51:15 -05001/*
2 * xcam_thread.h - xcam basic thread
3 *
4 * Copyright (c) 2014 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
22#include "xcam_cpf_reader.h"
Wind Yuan75564b12015-01-15 06:51:15 -050023
24#include <string.h>
25#include <stdlib.h>
26#include "libtbd.h"
27
28#undef XCAM_FAIL_RETURN_VAL
29#define XCAM_FAIL_RETURN_VAL(exp, ret) \
30 if (!(exp)) { \
31 XCAM_LOG_WARNING ("XCAM_FAIL_RETURN_VAL %s", #exp); \
32 return ret; \
33 }
34
35#undef XCAM_FAIL_RETURN
36#define XCAM_FAIL_RETURN(exp) \
37 if (!(exp)) { \
38 XCAM_LOG_WARNING ("XCAM_FAIL_RETURN %s", #exp); \
39 return ; \
40 }
41
42void *xcam_new0(size_t size)
43{
44 void *buf = malloc (size);
45 memset (buf, 0, size);
46 return buf;
47}
48
49XCamCpfBlob * xcam_cpf_blob_new ()
50{
51 return (XCamCpfBlob*) xcam_new0 (sizeof(XCamCpfBlob));
52}
53
54void xcam_cpf_blob_free (XCamCpfBlob *blob)
55{
56 XCAM_FAIL_RETURN (blob);
57
58 if (blob->data)
59 xcam_free (blob->data);
60
61 xcam_free (blob);
62}
63
64static int32_t
65read_cpf_file (const char *cpf_file, uint8_t **buf)
66{
67 int32_t size = 0;
68 FILE *fp = fopen (cpf_file, "rb");
69 XCAM_FAIL_RETURN_VAL (fp, -1);
70
71 *buf = NULL;
72
73 if (fseek (fp, 0, SEEK_END) < 0)
74 goto read_error;
75 if ((size = ftell (fp)) <= 0)
76 goto read_error;
77 if (fseek( fp, 0, SEEK_SET) < 0)
78 goto read_error;
79
80 *buf = (uint8_t*) xcam_new0 (size);
81 if (fread (*buf, 1, size, fp) != size)
82 goto read_error;
83
84 fclose (fp);
85 return size;
86
87read_error:
88 XCAM_LOG_ERROR ("read cpf(%s) failed", cpf_file);
89 fclose (fp);
90 if (*buf) {
91 xcam_free (*buf);
92 *buf = NULL;
93 }
94 return -1;
95
96}
97
98boolean
99xcam_cpf_read (const char *cpf_file, XCamCpfBlob *aiq_cpf, XCamCpfBlob *hal_cpf)
100{
101 uint8_t *cpf_buf;
102 int32_t cpf_size;
103
104 uint8_t *blob;
105 int32_t blob_size;
106
107 XCAM_FAIL_RETURN_VAL (cpf_file, FALSE);
108 XCAM_FAIL_RETURN_VAL (aiq_cpf, FALSE);
109
110 /* read cpf */
111 if ((cpf_size = read_cpf_file(cpf_file, &cpf_buf)) <= 0) {
112 XCAM_LOG_ERROR ("read cpf_file(%s) failed.", cpf_file);
113 return FALSE;
114 }
115
116 /* check sum */
117 if (tbd_validate (cpf_buf, cpf_size, tbd_tag_cpff) != tbd_err_none) {
118 XCAM_LOG_ERROR ("tbd validate cpf file(%s) failed.", cpf_file);
119 goto free_buf;
120 }
121
122 /* fetch AIQ */
123 if ( (tbd_get_record (cpf_buf, tbd_class_aiq, tbd_format_any,
124 (void**)&blob, (size_t*)&blob_size) != tbd_err_none) ||
125 !blob || blob_size <= 0) {
126 XCAM_LOG_ERROR ("CPF parse AIQ record failed.");
127 goto free_buf;
128 }
129 aiq_cpf->data = (uint8_t*) malloc (blob_size);
130 aiq_cpf->size = blob_size;
131 memcpy (aiq_cpf->data, blob, blob_size);
132
133
134#if 0 //DRV not necessary
135 /* fetch DRV */
136 if (tbd_get_record (cpf_buf, tbd_class_drv, tbd_format_any,
137 &drv_blob.data, &drv_blob.size) != tbd_err_none) {
138 XCAM_LOG_ERROR ("CPF parse DRV record failed.");
139 return FALSE;
140 }
141#endif
142
143
144 /* fetch HAL */
145 if (hal_cpf) {
146 if (tbd_get_record (cpf_buf, tbd_class_hal, tbd_format_any,
147 (void**)&blob, (size_t*)&blob_size) != tbd_err_none) {
148 XCAM_LOG_WARNING ("CPF doesn't have HAL record.");
149 // ignore HAL, not necessary
150 } else if (blob && blob_size > 0) {
151 hal_cpf->data = (uint8_t*) malloc (blob_size);
152 hal_cpf->size = blob_size;
153 memcpy (hal_cpf->data, blob, blob_size);
154 }
155 }
156
157 xcam_free (cpf_buf);
158 return TRUE;
159
160free_buf:
161 xcam_free (cpf_buf);
162 return FALSE;
163
164}