1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hal_camera.h"
18 
19 #include <pthread.h>
20 #include <stdlib.h>
21 
22 #include <iostream>
23 
24 #include <hardware/camera.h>
25 #include <hardware/camera_common.h>
26 #include <hardware/hardware.h>
27 
28 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
29 
30 #include "vts_datatype.h"
31 
32 using namespace std;
33 
34 namespace android {
35 namespace vts {
36 
37 // Callbacks {
vts_camera_device_status_change(const struct camera_module_callbacks *,int,int)38 static void vts_camera_device_status_change(
39     const struct camera_module_callbacks*, int /*camera_id*/,
40     int /*new_status*/) {}
41 
vts_torch_mode_status_change(const struct camera_module_callbacks *,const char *,int)42 static void vts_torch_mode_status_change(
43     const struct camera_module_callbacks*, const char* /*camera_id*/,
44     int /*new_status*/) {}
45 
vts_camera_notify_callback(int32_t,int32_t,int32_t,void *)46 static void vts_camera_notify_callback(
47     int32_t /*msg_type*/, int32_t /*ext1*/, int32_t /*ext2*/,
48     void* /*user*/) {}
49 
vts_camera_data_callback(int32_t,const camera_memory_t *,unsigned int,camera_frame_metadata_t *,void *)50 static void vts_camera_data_callback(
51     int32_t /*msg_type*/, const camera_memory_t* /*data*/,
52     unsigned int /*index*/, camera_frame_metadata_t* /*metadata*/,
53     void* /*user*/) {}
54 
vts_camera_data_timestamp_callback(int64_t,int32_t,const camera_memory_t *,unsigned int,void *)55 static void vts_camera_data_timestamp_callback(
56     int64_t /*timestamp*/, int32_t /*msg_type*/,
57     const camera_memory_t* /*data*/, unsigned int /*index*/, void* /*user*/) {
58 }
59 
vts_camera_request_memory(int,size_t,unsigned int,void *)60 static camera_memory_t* vts_camera_request_memory(
61     int /*fd*/, size_t /*buf_size*/, unsigned int /*num_bufs*/,
62     void* /*user*/) {
63   cout << __func__ << endl;
64   return NULL;
65 }
66 // } Callbacks
67 
GenerateCameraModuleCallbacks()68 camera_module_callbacks_t* GenerateCameraModuleCallbacks() {
69   cout << __func__ << endl;
70   if (RandomBool()) {
71     return NULL;
72   } else {
73     camera_module_callbacks_t* callbacks =
74         (camera_module_callbacks_t*)malloc(sizeof(camera_module_callbacks_t));
75     callbacks->camera_device_status_change = vts_camera_device_status_change;
76     callbacks->torch_mode_status_change = vts_torch_mode_status_change;
77     return callbacks;
78   }
79 }
80 
GenerateCameraNotifyCallback()81 camera_notify_callback GenerateCameraNotifyCallback() {
82   return vts_camera_notify_callback;
83 }
84 
GenerateCameraDataCallback()85 camera_data_callback GenerateCameraDataCallback() {
86   return vts_camera_data_callback;
87 }
88 
GenerateCameraDataTimestampCallback()89 camera_data_timestamp_callback GenerateCameraDataTimestampCallback() {
90   return vts_camera_data_timestamp_callback;
91 }
92 
GenerateCameraRequestMemory()93 camera_request_memory GenerateCameraRequestMemory() {
94   return vts_camera_request_memory;
95 }
96 
GenerateCameraInfo()97 camera_info_t* GenerateCameraInfo() {
98   cout << __func__ << endl;
99   if (RandomBool()) {
100     return NULL;
101   } else {
102     camera_info_t* caminfo = (camera_info_t*)malloc(sizeof(camera_info_t));
103     caminfo->facing = RandomBool() ? CAMERA_FACING_BACK : CAMERA_FACING_FRONT;
104     // support CAMERA_FACING_EXTERNAL if CAMERA_MODULE_API_VERSION_2_4 or above
105     caminfo->orientation =
106         RandomBool() ? (RandomBool() ? 0 : 90) : (RandomBool() ? 180 : 270);
107     caminfo->device_version = CAMERA_MODULE_API_VERSION_2_1;
108     caminfo->static_camera_characteristics = NULL;
109     caminfo->resource_cost = 50;  // between 50 and 100.
110     caminfo->conflicting_devices = NULL;
111     caminfo->conflicting_devices_length = 0;
112 
113     return caminfo;
114   }
115   /**
116    * The camera's fixed characteristics, which include all static camera
117   metadata
118    * specified in system/media/camera/docs/docs.html. This should be a sorted
119   metadata
120    * buffer, and may not be modified or freed by the caller. The pointer should
121   remain
122    * valid for the lifetime of the camera module, and values in it may not
123    * change after it is returned by get_camera_info().
124    *
125    * Version information (based on camera_module_t.common.module_api_version):
126    *
127    *  CAMERA_MODULE_API_VERSION_1_0:
128    *
129    *    Not valid. Extra characteristics are not available. Do not read this
130    *    field.
131    *
132    *  CAMERA_MODULE_API_VERSION_2_0 or higher:
133    *
134    *    Valid if device_version >= CAMERA_DEVICE_API_VERSION_2_0. Do not read
135    *    otherwise.
136    *
137   const camera_metadata_t *static_camera_characteristics;
138    */
139 
140   /**
141    * An array of camera device IDs represented as NULL-terminated strings
142    * indicating other devices that cannot be simultaneously opened while this
143    * camera device is in use.
144    *
145    * This field is intended to be used to indicate that this camera device
146    * is a composite of several other camera devices, or otherwise has
147    * hardware dependencies that prohibit simultaneous usage. If there are no
148    * dependencies, a NULL may be returned in this field to indicate this.
149    *
150    * The camera service will never simultaneously open any of the devices
151    * in this list while this camera device is open.
152    *
153    * The strings pointed to in this field will not be cleaned up by the camera
154    * service, and must remain while this device is plugged in.
155    *
156    * Version information (based on camera_module_t.common.module_api_version):
157    *
158    *  CAMERA_MODULE_API_VERSION_2_3 or lower:
159    *
160    *    Not valid.  Can be assumed to be NULL.  Do not read this field.
161    *
162    *  CAMERA_MODULE_API_VERSION_2_4 or higher:
163    *
164    *    Always valid.
165   char** conflicting_devices;
166    */
167 
168   /**
169    * The length of the array given in the conflicting_devices field.
170    *
171    * Version information (based on camera_module_t.common.module_api_version):
172    *
173    *  CAMERA_MODULE_API_VERSION_2_3 or lower:
174    *
175    *    Not valid.  Can be assumed to be 0.  Do not read this field.
176    *
177    *  CAMERA_MODULE_API_VERSION_2_4 or higher:
178    *
179    *    Always valid.
180   size_t conflicting_devices_length;
181    */
182 }
183 
GenerateCameraInfoUsingMessage(const VariableSpecificationMessage &)184 camera_info_t* GenerateCameraInfoUsingMessage(
185     const VariableSpecificationMessage& /*msg*/) {
186   cout << __func__ << endl;
187   // TODO: acutally use msg.
188   camera_info_t* caminfo = (camera_info_t*)malloc(sizeof(camera_info_t));
189   caminfo->facing = RandomBool() ? CAMERA_FACING_BACK : CAMERA_FACING_FRONT;
190   // support CAMERA_FACING_EXTERNAL if CAMERA_MODULE_API_VERSION_2_4 or above
191   caminfo->orientation =
192       RandomBool() ? (RandomBool() ? 0 : 90) : (RandomBool() ? 180 : 270);
193   caminfo->device_version = CAMERA_MODULE_API_VERSION_2_1;
194   caminfo->static_camera_characteristics = NULL;
195   caminfo->resource_cost = 50;  // between 50 and 100.
196   caminfo->conflicting_devices = NULL;
197   caminfo->conflicting_devices_length = 0;
198 
199   return caminfo;
200 }
201 
ConvertCameraInfoToProtobuf(camera_info_t * raw,VariableSpecificationMessage * msg)202 bool ConvertCameraInfoToProtobuf(camera_info_t* raw,
203                                  VariableSpecificationMessage* msg) {
204   cout << __func__ << ":" << __LINE__ << endl;
205 
206   if (msg->struct_value_size() > 0) msg->clear_struct_value();
207 
208   if (!raw) return false;
209 
210   cout << __func__ << ":" << __LINE__ << endl;
211   // TODO: use primitive_name and put in the expected order.
212   msg->set_type(TYPE_STRUCT);
213 
214   VariableSpecificationMessage* sub_msg;
215 
216   sub_msg = msg->add_struct_value();
217   sub_msg->set_type(TYPE_SCALAR);
218   sub_msg->set_scalar_type("int32_t");
219   sub_msg->mutable_scalar_value()->set_int32_t(raw->facing);
220 
221   sub_msg = msg->add_struct_value();
222   sub_msg->set_type(TYPE_SCALAR);
223   sub_msg->set_scalar_type("int32_t");
224   sub_msg->mutable_scalar_value()->set_int32_t(raw->orientation);
225 
226   sub_msg = msg->add_struct_value();
227   sub_msg->set_type(TYPE_SCALAR);
228   sub_msg->set_scalar_type("uint32_t");
229   sub_msg->mutable_scalar_value()->set_uint32_t(raw->device_version);
230 
231   // TODO: update for static_camera_characteristics and others
232   // msg.add_primitive_value()->set_int32_t(raw->static_camera_characteristics);
233   sub_msg = msg->add_struct_value();
234   sub_msg->set_type(TYPE_SCALAR);
235   sub_msg->set_scalar_type("int32_t");
236   sub_msg->mutable_scalar_value()->set_int32_t(raw->resource_cost);
237 
238   // TODO: support pointer. conflicting_devices is pointer pointer.
239   // msg.add_primitive_value()->set_pointer(raw->conflicting_devices);
240   // msg.add_primitive_value()->set_int32_t(raw->conflicting_devices_length);
241   sub_msg = msg->add_struct_value();
242   sub_msg->set_type(TYPE_SCALAR);
243   sub_msg->set_scalar_type("int32_t");
244   sub_msg->mutable_scalar_value()->set_int32_t(0);
245   return true;
246 }
247 
248 }  // namespace vts
249 }  // namespace android
250