summaryrefslogtreecommitdiffstats
path: root/camera/OMXCameraAdapter/OMXDccDataSave.cpp
blob: 754774384893ec5bec224085ac39d55ac2de7fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
* @file OMXDccDataSave.cpp
*
* This file contains functionality for handling DCC data save
*
*/

#include "CameraHal.h"
#include "OMXCameraAdapter.h"


namespace Ti {
namespace Camera {

status_t OMXCameraAdapter::initDccFileDataSave(OMX_HANDLETYPE* omxHandle, int portIndex)
{
    OMX_CONFIG_EXTRADATATYPE extraDataControl;
    status_t ret = NO_ERROR;
    OMX_ERRORTYPE eError = OMX_ErrorNone;

    LOG_FUNCTION_NAME;

    OMX_INIT_STRUCT_PTR (&extraDataControl, OMX_CONFIG_EXTRADATATYPE);
    extraDataControl.nPortIndex = portIndex;
    extraDataControl.eExtraDataType = OMX_TI_DccData;
    extraDataControl.bEnable = OMX_TRUE;

    eError =  OMX_SetConfig(*omxHandle,
                        ( OMX_INDEXTYPE ) OMX_IndexConfigOtherExtraDataControl,
                        &extraDataControl);

    if ( OMX_ErrorNone != eError )
        {
        CAMHAL_LOGEB("Error while configuring dcc data overwrite extra data 0x%x",
                    eError);

        ret =  NO_INIT;
        }

    if (mDccData.pData) {
        free(mDccData.pData);
        mDccData.pData = NULL;
    }
    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

status_t OMXCameraAdapter::sniffDccFileDataSave(OMX_BUFFERHEADERTYPE* pBuffHeader)
{
    OMX_OTHER_EXTRADATATYPE *extraData;
    OMX_TI_DCCDATATYPE* dccData;
    status_t ret = NO_ERROR;

    LOG_FUNCTION_NAME;

    android::AutoMutex lock(mDccDataLock);

    if ( NULL == pBuffHeader ) {
        CAMHAL_LOGEA("Invalid Buffer header");
        LOG_FUNCTION_NAME_EXIT;
        return -EINVAL;
    }

    extraData = getExtradata(pBuffHeader->pPlatformPrivate,
                             (OMX_EXTRADATATYPE)OMX_TI_DccData);

    if ( NULL != extraData ) {
        CAMHAL_LOGVB("Size = %d, sizeof = %d, eType = 0x%x, nDataSize= %d, nPortIndex = 0x%x, nVersion = 0x%x",
                     extraData->nSize,
                     sizeof(OMX_OTHER_EXTRADATATYPE),
                     extraData->eType,
                     extraData->nDataSize,
                     extraData->nPortIndex,
                     extraData->nVersion);
    } else {
        CAMHAL_LOGVA("Invalid OMX_TI_DCCDATATYPE");
        LOG_FUNCTION_NAME_EXIT;
        return NO_ERROR;
    }

    dccData = ( OMX_TI_DCCDATATYPE * ) extraData->data;

    if (NULL == dccData) {
        CAMHAL_LOGVA("OMX_TI_DCCDATATYPE is not found in extra data");
        LOG_FUNCTION_NAME_EXIT;
        return NO_ERROR;
    }

    if (mDccData.pData) {
        free(mDccData.pData);
    }

    memcpy(&mDccData, dccData, sizeof(mDccData));

    int dccDataSize = (int)dccData->nSize - (int)(&(((OMX_TI_DCCDATATYPE*)0)->pData));

    mDccData.pData = (OMX_PTR)malloc(dccDataSize);

    if (NULL == mDccData.pData) {
        CAMHAL_LOGVA("not enough memory for DCC data");
        LOG_FUNCTION_NAME_EXIT;
        return NO_ERROR;
    }

    memcpy(mDccData.pData, &(dccData->pData), dccDataSize);

    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

// Recursively searches given directory contents for the correct DCC file.
// The directory must be opened and its stream pointer + path passed
// as arguments. As this function is called recursively, to avoid excessive
// stack usage the path param is reused -> this MUST be char array with
// enough length!!! (260 should suffice). Path must end with "/".
// The directory must also be closed in the caller function.
// If the correct camera DCC file is found (based on the OMX measurement data)
// its file stream pointer is returned. NULL is returned otherwise
FILE * OMXCameraAdapter::parseDCCsubDir(DIR *pDir, char *path)
{
    FILE *pFile;
    DIR *pSubDir;
    struct dirent *dirEntry;
    int initialPathLength = strlen(path);

    LOG_FUNCTION_NAME;

    /* check each directory entry */
    while ((dirEntry = readdir(pDir)) != NULL)
    {
        if (dirEntry->d_name[0] == '.')
            continue;

        strcat(path, dirEntry->d_name);
        // dirEntry might be sub directory -> check it
        pSubDir = opendir(path);
        if (pSubDir) {
            // dirEntry is sub directory -> parse it
            strcat(path, "/");
            pFile = parseDCCsubDir(pSubDir, path);
            closedir(pSubDir);
            if (pFile) {
                // the correct DCC file found!
                LOG_FUNCTION_NAME_EXIT;
                return pFile;
            }
        } else {
            // dirEntry is file -> open it
            pFile = fopen(path, "rb");
            if (pFile) {
                // now check if this is the correct DCC file for that camera
                OMX_U32 dccFileIDword;
                OMX_U32 *dccFileDesc = (OMX_U32 *) &mDccData.nCameraModuleId;
                int i;

                // DCC file ID is 3 4-byte words
                for (i = 0; i < 3; i++) {
                    if (fread(&dccFileIDword, sizeof(OMX_U32), 1, pFile) != 1) {
                        // file too short
                        break;
                    }
                    if (dccFileIDword != dccFileDesc[i]) {
                        // DCC file ID word i does not match
                        break;
                    }
                }

                fclose(pFile);
                if (i == 3) {
                    // the correct DCC file found!
                    CAMHAL_LOGDB("DCC file to be updated: %s", path);
                    // reopen it for modification
                    pFile = fopen(path, "rb+");
                    if (!pFile)
                        CAMHAL_LOGEB("ERROR: DCC file %s failed to open for modification", path);
                    LOG_FUNCTION_NAME_EXIT;
                    return pFile;
                }
            } else {
                CAMHAL_LOGEB("ERROR: Failed to open file %s for reading", path);
            }
        }
        // restore original path
        path[initialPathLength] = '\0';
    }

    LOG_FUNCTION_NAME_EXIT;

    // DCC file not found in this directory tree
    return NULL;
}

// Finds the DCC file corresponding to the current camera based on the
// OMX measurement data, opens it and returns the file stream pointer
// (NULL on error or if file not found).
// The folder string dccFolderPath must end with "/"
FILE * OMXCameraAdapter::fopenCameraDCC(const char *dccFolderPath)
{
    FILE *pFile;
    DIR *pDir;
    char dccPath[260];

    LOG_FUNCTION_NAME;

    strcpy(dccPath, dccFolderPath);

    pDir = opendir(dccPath);
    if (!pDir) {
        CAMHAL_LOGEB("ERROR: Opening DCC directory %s failed", dccPath);
        LOG_FUNCTION_NAME_EXIT;
        return NULL;
    }

    pFile = parseDCCsubDir(pDir, dccPath);
    closedir(pDir);
    if (pFile) {
        CAMHAL_LOGDB("DCC file %s opened for modification", dccPath);
    }

    LOG_FUNCTION_NAME_EXIT;

    return pFile;
}

// Positions the DCC file stream pointer to the correct offset within the
// correct usecase based on the OMX mesurement data. Returns 0 on success
status_t OMXCameraAdapter::fseekDCCuseCasePos(FILE *pFile)
{
    OMX_U32 dccNumUseCases = 0;
    OMX_U32 dccUseCaseData[3];
    OMX_U32 i;

    LOG_FUNCTION_NAME;

    // position the file pointer to the DCC use cases section
    if (fseek(pFile, 80, SEEK_SET)) {
        CAMHAL_LOGEA("ERROR: Unexpected end of DCC file");
        LOG_FUNCTION_NAME_EXIT;
        return -EINVAL;
    }

    if (fread(&dccNumUseCases, sizeof(OMX_U32), 1, pFile) != 1 ||
        dccNumUseCases == 0) {
        CAMHAL_LOGEA("ERROR: DCC file contains 0 use cases");
        LOG_FUNCTION_NAME_EXIT;
        return -EINVAL;
    }

    for (i = 0; i < dccNumUseCases; i++) {
        if (fread(dccUseCaseData, sizeof(OMX_U32), 3, pFile) != 3) {
            CAMHAL_LOGEA("ERROR: Unexpected end of DCC file");
            LOG_FUNCTION_NAME_EXIT;
            return -EINVAL;
        }

        if (dccUseCaseData[0] == mDccData.nUseCaseId) {
            // DCC use case match!
            break;
        }
    }

    if (i == dccNumUseCases) {
        CAMHAL_LOGEB("ERROR: Use case ID %lu not found in DCC file", mDccData.nUseCaseId);
        LOG_FUNCTION_NAME_EXIT;
        return -EINVAL;
    }

    // dccUseCaseData[1] is the offset to the beginning of the actual use case
    // from the beginning of the file
    // mDccData.nOffset is the offset within the actual use case (from the
    // beginning of the use case to the data to be modified)

    if (fseek(pFile,dccUseCaseData[1] + mDccData.nOffset, SEEK_SET ))
    {
        CAMHAL_LOGEA("ERROR: Error setting the correct offset");
        LOG_FUNCTION_NAME_EXIT;
        return -EINVAL;
    }

    LOG_FUNCTION_NAME_EXIT;

    return NO_ERROR;
}

status_t OMXCameraAdapter::saveDccFileDataSave()
{
    status_t ret = NO_ERROR;

    LOG_FUNCTION_NAME;

    android::AutoMutex lock(mDccDataLock);

    if (mDccData.pData)
        {
        FILE *fd = fopenCameraDCC(DCC_PATH);

        if (fd)
            {
            if (!fseekDCCuseCasePos(fd))
                {
                int dccDataSize = (int)mDccData.nSize - (int)(&(((OMX_TI_DCCDATATYPE*)0)->pData));

                if (fwrite(mDccData.pData, dccDataSize, 1, fd) != 1)
                    {
                    CAMHAL_LOGEA("ERROR: Writing to DCC file failed");
                    }
                else
                    {
                    CAMHAL_LOGDA("DCC file successfully updated");
                    }
                }
            fclose(fd);
            }
        else
            {
            CAMHAL_LOGEA("ERROR: Correct DCC file not found or failed to open for modification");
            }
        }

    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

status_t OMXCameraAdapter::closeDccFileDataSave()
{
    status_t ret = NO_ERROR;

    LOG_FUNCTION_NAME;

    android::AutoMutex lock(mDccDataLock);

    if (mDccData.pData) {
        free(mDccData.pData);
        mDccData.pData = NULL;
    }
    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

} // namespace Camera
} // namespace Ti