huangcm
2025-08-30 0269911b91ed7e03c24005924cc6423abf245fb8
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
/*
 * Copyright 2016 The Android Open Source Project
 *
 * 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.
 */
 
#include "hal_camera.h"
 
#include <pthread.h>
#include <stdlib.h>
 
#include <iostream>
 
#include <hardware/camera.h>
#include <hardware/camera_common.h>
#include <hardware/hardware.h>
 
#include "test/vts/proto/ComponentSpecificationMessage.pb.h"
 
#include "vts_datatype.h"
 
using namespace std;
 
namespace android {
namespace vts {
 
// Callbacks {
static void vts_camera_device_status_change(
    const struct camera_module_callbacks*, int /*camera_id*/,
    int /*new_status*/) {}
 
static void vts_torch_mode_status_change(
    const struct camera_module_callbacks*, const char* /*camera_id*/,
    int /*new_status*/) {}
 
static void vts_camera_notify_callback(
    int32_t /*msg_type*/, int32_t /*ext1*/, int32_t /*ext2*/,
    void* /*user*/) {}
 
static void vts_camera_data_callback(
    int32_t /*msg_type*/, const camera_memory_t* /*data*/,
    unsigned int /*index*/, camera_frame_metadata_t* /*metadata*/,
    void* /*user*/) {}
 
static void vts_camera_data_timestamp_callback(
    int64_t /*timestamp*/, int32_t /*msg_type*/,
    const camera_memory_t* /*data*/, unsigned int /*index*/, void* /*user*/) {
}
 
static camera_memory_t* vts_camera_request_memory(
    int /*fd*/, size_t /*buf_size*/, unsigned int /*num_bufs*/,
    void* /*user*/) {
  cout << __func__ << endl;
  return NULL;
}
// } Callbacks
 
camera_module_callbacks_t* GenerateCameraModuleCallbacks() {
  cout << __func__ << endl;
  if (RandomBool()) {
    return NULL;
  } else {
    camera_module_callbacks_t* callbacks =
        (camera_module_callbacks_t*)malloc(sizeof(camera_module_callbacks_t));
    callbacks->camera_device_status_change = vts_camera_device_status_change;
    callbacks->torch_mode_status_change = vts_torch_mode_status_change;
    return callbacks;
  }
}
 
camera_notify_callback GenerateCameraNotifyCallback() {
  return vts_camera_notify_callback;
}
 
camera_data_callback GenerateCameraDataCallback() {
  return vts_camera_data_callback;
}
 
camera_data_timestamp_callback GenerateCameraDataTimestampCallback() {
  return vts_camera_data_timestamp_callback;
}
 
camera_request_memory GenerateCameraRequestMemory() {
  return vts_camera_request_memory;
}
 
camera_info_t* GenerateCameraInfo() {
  cout << __func__ << endl;
  if (RandomBool()) {
    return NULL;
  } else {
    camera_info_t* caminfo = (camera_info_t*)malloc(sizeof(camera_info_t));
    caminfo->facing = RandomBool() ? CAMERA_FACING_BACK : CAMERA_FACING_FRONT;
    // support CAMERA_FACING_EXTERNAL if CAMERA_MODULE_API_VERSION_2_4 or above
    caminfo->orientation =
        RandomBool() ? (RandomBool() ? 0 : 90) : (RandomBool() ? 180 : 270);
    caminfo->device_version = CAMERA_MODULE_API_VERSION_2_1;
    caminfo->static_camera_characteristics = NULL;
    caminfo->resource_cost = 50;  // between 50 and 100.
    caminfo->conflicting_devices = NULL;
    caminfo->conflicting_devices_length = 0;
 
    return caminfo;
  }
  /**
   * The camera's fixed characteristics, which include all static camera
  metadata
   * specified in system/media/camera/docs/docs.html. This should be a sorted
  metadata
   * buffer, and may not be modified or freed by the caller. The pointer should
  remain
   * valid for the lifetime of the camera module, and values in it may not
   * change after it is returned by get_camera_info().
   *
   * Version information (based on camera_module_t.common.module_api_version):
   *
   *  CAMERA_MODULE_API_VERSION_1_0:
   *
   *    Not valid. Extra characteristics are not available. Do not read this
   *    field.
   *
   *  CAMERA_MODULE_API_VERSION_2_0 or higher:
   *
   *    Valid if device_version >= CAMERA_DEVICE_API_VERSION_2_0. Do not read
   *    otherwise.
   *
  const camera_metadata_t *static_camera_characteristics;
   */
 
  /**
   * An array of camera device IDs represented as NULL-terminated strings
   * indicating other devices that cannot be simultaneously opened while this
   * camera device is in use.
   *
   * This field is intended to be used to indicate that this camera device
   * is a composite of several other camera devices, or otherwise has
   * hardware dependencies that prohibit simultaneous usage. If there are no
   * dependencies, a NULL may be returned in this field to indicate this.
   *
   * The camera service will never simultaneously open any of the devices
   * in this list while this camera device is open.
   *
   * The strings pointed to in this field will not be cleaned up by the camera
   * service, and must remain while this device is plugged in.
   *
   * Version information (based on camera_module_t.common.module_api_version):
   *
   *  CAMERA_MODULE_API_VERSION_2_3 or lower:
   *
   *    Not valid.  Can be assumed to be NULL.  Do not read this field.
   *
   *  CAMERA_MODULE_API_VERSION_2_4 or higher:
   *
   *    Always valid.
  char** conflicting_devices;
   */
 
  /**
   * The length of the array given in the conflicting_devices field.
   *
   * Version information (based on camera_module_t.common.module_api_version):
   *
   *  CAMERA_MODULE_API_VERSION_2_3 or lower:
   *
   *    Not valid.  Can be assumed to be 0.  Do not read this field.
   *
   *  CAMERA_MODULE_API_VERSION_2_4 or higher:
   *
   *    Always valid.
  size_t conflicting_devices_length;
   */
}
 
camera_info_t* GenerateCameraInfoUsingMessage(
    const VariableSpecificationMessage& /*msg*/) {
  cout << __func__ << endl;
  // TODO: acutally use msg.
  camera_info_t* caminfo = (camera_info_t*)malloc(sizeof(camera_info_t));
  caminfo->facing = RandomBool() ? CAMERA_FACING_BACK : CAMERA_FACING_FRONT;
  // support CAMERA_FACING_EXTERNAL if CAMERA_MODULE_API_VERSION_2_4 or above
  caminfo->orientation =
      RandomBool() ? (RandomBool() ? 0 : 90) : (RandomBool() ? 180 : 270);
  caminfo->device_version = CAMERA_MODULE_API_VERSION_2_1;
  caminfo->static_camera_characteristics = NULL;
  caminfo->resource_cost = 50;  // between 50 and 100.
  caminfo->conflicting_devices = NULL;
  caminfo->conflicting_devices_length = 0;
 
  return caminfo;
}
 
bool ConvertCameraInfoToProtobuf(camera_info_t* raw,
                                 VariableSpecificationMessage* msg) {
  cout << __func__ << ":" << __LINE__ << endl;
 
  if (msg->struct_value_size() > 0) msg->clear_struct_value();
 
  if (!raw) return false;
 
  cout << __func__ << ":" << __LINE__ << endl;
  // TODO: use primitive_name and put in the expected order.
  msg->set_type(TYPE_STRUCT);
 
  VariableSpecificationMessage* sub_msg;
 
  sub_msg = msg->add_struct_value();
  sub_msg->set_type(TYPE_SCALAR);
  sub_msg->set_scalar_type("int32_t");
  sub_msg->mutable_scalar_value()->set_int32_t(raw->facing);
 
  sub_msg = msg->add_struct_value();
  sub_msg->set_type(TYPE_SCALAR);
  sub_msg->set_scalar_type("int32_t");
  sub_msg->mutable_scalar_value()->set_int32_t(raw->orientation);
 
  sub_msg = msg->add_struct_value();
  sub_msg->set_type(TYPE_SCALAR);
  sub_msg->set_scalar_type("uint32_t");
  sub_msg->mutable_scalar_value()->set_uint32_t(raw->device_version);
 
  // TODO: update for static_camera_characteristics and others
  // msg.add_primitive_value()->set_int32_t(raw->static_camera_characteristics);
  sub_msg = msg->add_struct_value();
  sub_msg->set_type(TYPE_SCALAR);
  sub_msg->set_scalar_type("int32_t");
  sub_msg->mutable_scalar_value()->set_int32_t(raw->resource_cost);
 
  // TODO: support pointer. conflicting_devices is pointer pointer.
  // msg.add_primitive_value()->set_pointer(raw->conflicting_devices);
  // msg.add_primitive_value()->set_int32_t(raw->conflicting_devices_length);
  sub_msg = msg->add_struct_value();
  sub_msg->set_type(TYPE_SCALAR);
  sub_msg->set_scalar_type("int32_t");
  sub_msg->mutable_scalar_value()->set_int32_t(0);
  return true;
}
 
}  // namespace vts
}  // namespace android