• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2016, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 
30 // Camera dependencies
31 #include "QCameraTrace.h"
32 
33 #define CAMSCOPE_MEMSTORE_SIZE 0x00100000 // 1MB
34 
35 volatile uint32_t kpi_camscope_flags = 0;
36 volatile uint32_t kpi_camscope_frame_count = 0;
37 
38 static const char * camscope_filenames[CAMSCOPE_SECTION_SIZE] = {
39     "/data/misc/camera/camscope_mmcamera.bin",
40     "/data/misc/camera/camscope_hal.bin",
41     "/data/misc/camera/camscope_jpeg.bin"
42 };
43 
44 static FILE * camscope_fd[CAMSCOPE_SECTION_SIZE];
45 static uint32_t camscope_num_bytes_stored[CAMSCOPE_SECTION_SIZE];
46 static char * camscope_memstore[CAMSCOPE_SECTION_SIZE];
47 static pthread_mutex_t camscope_mutex[CAMSCOPE_SECTION_SIZE];
48 
49 /* camscope_init:
50  *
51  *  @camscope_section: camscope section where this function is occurring
52  *
53  *  Initializes the CameraScope tool functionality
54  *
55  *  Return: N/A
56  */
camscope_init(camscope_section_type camscope_section)57 void camscope_init(camscope_section_type camscope_section) {
58     pthread_mutex_init(&(camscope_mutex[camscope_section]), NULL);
59     if (camscope_fd[camscope_section] == NULL) {
60         if(camscope_memstore[camscope_section] == NULL) {
61             camscope_memstore[camscope_section] =
62                 (char *)malloc(CAMSCOPE_MEMSTORE_SIZE);
63             if (camscope_memstore[camscope_section] == NULL) {
64               CLOGE(CAM_NO_MODULE, "Failed to allocate camscope memstore"
65                     "with size %d\n", CAMSCOPE_MEMSTORE_SIZE);
66             }
67         }
68         camscope_fd[camscope_section] =
69             fopen(camscope_filenames[camscope_section], "ab");
70     }
71 }
72 
73 /* camscope_flush:
74  *
75  *  @camscope_section: camscope section where this function is occurring
76  *
77  *  Flushes the camscope memstore to the file system
78  *
79  *  Return: N/A
80  */
camscope_flush(camscope_section_type camscope_section)81 static void camscope_flush(camscope_section_type camscope_section) {
82     if (camscope_fd[camscope_section] != NULL &&
83         camscope_memstore[camscope_section] != NULL) {
84         fwrite(camscope_memstore[camscope_section], sizeof(char),
85                camscope_num_bytes_stored[camscope_section],
86                camscope_fd[camscope_section]);
87         camscope_num_bytes_stored[camscope_section] = 0;
88     }
89 }
90 
91 /* camscope_destroy:
92  *
93  *  @camscope_section: camscope section where this function is occurring
94  *
95  *  Flushes any remaining data to the file system and cleans up CameraScope
96  *
97  *  Return: N/A
98  */
camscope_destroy(camscope_section_type camscope_section)99 void camscope_destroy(camscope_section_type camscope_section) {
100     if (camscope_fd[camscope_section] != NULL) {
101         pthread_mutex_lock(&(camscope_mutex[camscope_section]));
102         if(camscope_memstore[camscope_section] != NULL) {
103             camscope_flush(camscope_section);
104             free(camscope_memstore[camscope_section]);
105             camscope_memstore[camscope_section] = NULL;
106         }
107         fclose(camscope_fd[camscope_section]);
108         camscope_fd[camscope_section] = NULL;
109         pthread_mutex_unlock(&(camscope_mutex[camscope_section]));
110     }
111     pthread_mutex_destroy(&(camscope_mutex[camscope_section]));
112 }
113 
114 /* camscope_reserve:
115  *
116  *  @camscope_section:     camscope section where this function is occurring
117  *  @num_bytes_to_reserve: number in bytes to reserve on the memstore
118  *
119  *  Reserves a number of bytes on the memstore flushing to the
120  *  file system if remaining space is insufficient
121  *
122  *  Return: number of bytes successfully reserved on the memstore
123  */
camscope_reserve(camscope_section_type camscope_section,uint32_t num_bytes_to_reserve)124 uint32_t camscope_reserve(camscope_section_type camscope_section,
125                                  uint32_t num_bytes_to_reserve) {
126     uint32_t bytes_reserved = 0;
127     if (camscope_fd[camscope_section] != NULL &&
128         num_bytes_to_reserve <= CAMSCOPE_MEMSTORE_SIZE) {
129         int32_t size = CAMSCOPE_MEMSTORE_SIZE -
130                camscope_num_bytes_stored[camscope_section] -
131                num_bytes_to_reserve;
132         if (size < 0) {
133             camscope_flush(camscope_section);
134         }
135         bytes_reserved = num_bytes_to_reserve;
136     }
137     return bytes_reserved;
138 }
139 
140 /* camscope_store_data:
141  *
142  *  @camscope_section: camscope section where this function is occurring
143  *  @data:             data to be stored
144  *  @size:             size of data to be stored
145  *
146  *  Store the data to the memstore and calculate remaining space
147  *
148  *  Return: N/A
149  */
camscope_store_data(camscope_section_type camscope_section,void * data,uint32_t size)150 void camscope_store_data(camscope_section_type camscope_section,
151                        void* data, uint32_t size) {
152     if(camscope_memstore[camscope_section] != NULL) {
153         memcpy(camscope_memstore[camscope_section] +
154                camscope_num_bytes_stored[camscope_section], (char*)data, size);
155         camscope_num_bytes_stored[camscope_section] += size;
156     }
157 }
158 
159 /* camscope_mutex_lock:
160  *
161  *  @camscope_section: camscope section where this function is occurring
162  *
163  *  Lock the camscope mutex lock for the given camscope section
164  *
165  *  Return: N/A
166  */
camscope_mutex_lock(camscope_section_type camscope_section)167 void camscope_mutex_lock(camscope_section_type camscope_section) {
168     pthread_mutex_lock(&(camscope_mutex[camscope_section]));
169 }
170 
171 /* camscope_mutex_unlock:
172  *
173  *  @camscope_section: camscope section where this function is occurring
174  *
175  *  Unlock the camscope mutex lock for the given camscope section
176  *
177  *  Return: N/A
178  */
camscope_mutex_unlock(camscope_section_type camscope_section)179 void camscope_mutex_unlock(camscope_section_type camscope_section) {
180     pthread_mutex_unlock(&(camscope_mutex[camscope_section]));
181 }
182