1 /*
2  * Copyright (c) 2022 - 2023 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include <securec.h>
10 #include <hdf_log.h>
11 #include <osal_mem.h>
12 #include "camera_config_parser.h"
13 #include "camera_device_manager.h"
14 
15 #define HDF_LOG_TAG HDF_CAMERA_DEVICE_MANAGER
16 
17 static struct CameraDeviceDriverFactory *g_cameraDeviceDriverFactory[DEVICE_DRIVER_MAX_NUM];
18 static struct CameraDevice *g_cameraDeviceTable[CAMERA_MAX_NUM];
19 
ReleaseCameraDevice(struct CameraDevice * camDev)20 static void ReleaseCameraDevice(struct CameraDevice *camDev)
21 {
22     OsalMemFree(camDev);
23     HDF_LOGI("%s: success!", __func__);
24     return;
25 }
26 
FindAvailableIndex(uint32_t * index)27 static bool FindAvailableIndex(uint32_t *index)
28 {
29     uint32_t i;
30     for (i = 0; i < CAMERA_MAX_NUM; i++) {
31         if (g_cameraDeviceTable[i] == NULL) {
32             *index = i;
33             return true;
34         }
35     }
36     return false;
37 }
38 
AddCameraDeviceToTable(uint32_t index,struct CameraDevice * cameraDevice)39 static bool AddCameraDeviceToTable(uint32_t index, struct CameraDevice *cameraDevice)
40 {
41     if (index >= CAMERA_MAX_NUM) {
42         HDF_LOGE("%s: error: index out of range!", __func__);
43         return false;
44     }
45     if (g_cameraDeviceTable[index] != NULL) {
46         HDF_LOGE("%s: error: g_cameraDeviceTable[%{public}d] is already exist!", __func__, index);
47         return false;
48     }
49     g_cameraDeviceTable[index] = cameraDevice;
50     return true;
51 }
52 
RemoveCameraDeviceFromTable(const struct CameraDevice * cameraDevice)53 static void RemoveCameraDeviceFromTable(const struct CameraDevice *cameraDevice)
54 {
55     int32_t i;
56     for (i = 0; i < CAMERA_MAX_NUM; i++) {
57         if (g_cameraDeviceTable[i] == cameraDevice) {
58             g_cameraDeviceTable[i] = NULL;
59             return;
60         }
61     }
62     return;
63 }
64 
CameraDeviceRelease(struct CameraDevice * camDev)65 int32_t CameraDeviceRelease(struct CameraDevice *camDev)
66 {
67     if (camDev == NULL) {
68         HDF_LOGI("%s: success: already deinit!", __func__);
69         return HDF_SUCCESS;
70     }
71     RemoveCameraDeviceFromTable(camDev);
72     OsalMemFree(camDev);
73     return HDF_SUCCESS;
74 }
75 
CameraDeviceCreate(const char * deviceName,uint32_t len)76 struct CameraDevice *CameraDeviceCreate(const char *deviceName, uint32_t len)
77 {
78     struct CameraDevice *camDevice = NULL;
79     uint32_t index = 0;
80 
81     if ((deviceName == NULL) || (strlen(deviceName) != len) || (strlen(deviceName) > DEVICE_NAME_SIZE - 1)) {
82         HDF_LOGE("%s: fail: deviceName is null or len not right!", __func__);
83         return NULL;
84     }
85     camDevice = (struct CameraDevice *)OsalMemCalloc(sizeof(struct CameraDevice));
86     if (camDevice == NULL) {
87         HDF_LOGE("%s: fail: OsalMemCalloc fail!", __func__);
88         return NULL;
89     }
90     if (strcpy_s(camDevice->deviceName, DEVICE_NAME_SIZE, deviceName) != EOK) {
91         HDF_LOGE("%s: fail: strcpy_s fail!", __func__);
92         OsalMemFree(camDevice);
93         return NULL;
94     }
95     if (FindAvailableIndex(&index)) {
96         AddCameraDeviceToTable(index, camDevice);
97     } else {
98         ReleaseCameraDevice(camDevice);
99         HDF_LOGE("%s: fail: Not extra table.", __func__);
100         return NULL;
101     }
102     HDF_LOGI("%s: Init Camera Device success!", __func__);
103 
104     return camDevice;
105 }
106 
CameraDeviceGetByName(const char * deviceName)107 struct CameraDevice *CameraDeviceGetByName(const char *deviceName)
108 {
109     int32_t i;
110     if (deviceName == NULL) {
111         HDF_LOGE("%s: fail: deviceName is NULL.", __func__);
112         return NULL;
113     }
114 
115     for (i = 0; i < CAMERA_MAX_NUM; i++) {
116         if (g_cameraDeviceTable[i] == NULL) {
117             continue;
118         }
119         if (strcmp(g_cameraDeviceTable[i]->deviceName, deviceName) == 0) {
120             return g_cameraDeviceTable[i];
121         }
122     }
123 
124     HDF_LOGE("%s: fail: %{public}s: deviceName not exist.", __func__, deviceName);
125     return NULL;
126 }
127 
CameraDeviceDriverFactoryGetByName(const char * deviceName)128 struct CameraDeviceDriverFactory *CameraDeviceDriverFactoryGetByName(const char *deviceName)
129 {
130     int32_t i;
131     if (deviceName == NULL) {
132         HDF_LOGE("%s: fail: deviceName is NULL", __func__);
133         return NULL;
134     }
135 
136     for (i = 0; i < DEVICE_DRIVER_MAX_NUM; i++) {
137         if (g_cameraDeviceDriverFactory[i] == NULL || g_cameraDeviceDriverFactory[i]->deviceName == NULL) {
138             continue;
139         }
140         struct CameraDeviceDriverFactory *factory = g_cameraDeviceDriverFactory[i];
141         if (strcmp(factory->deviceName, deviceName) == 0) {
142             return factory;
143         }
144     }
145     HDF_LOGE("%s: fail: return NULL", __func__);
146     return NULL;
147 }
148 
CameraDeviceDriverFactoryRegister(struct CameraDeviceDriverFactory * obj)149 int32_t CameraDeviceDriverFactoryRegister(struct CameraDeviceDriverFactory *obj)
150 {
151     int32_t index;
152     if (obj == NULL || obj->deviceName == NULL) {
153         HDF_LOGE("%s: CameraDeviceDriverFactory obj is NULL", __func__);
154         return HDF_ERR_INVALID_PARAM;
155     }
156     if (CameraDeviceDriverFactoryGetByName(obj->deviceName) != NULL) {
157         HDF_LOGI("%s: devicedriver factory is already registered. name=%{public}s", __func__, obj->deviceName);
158         return HDF_SUCCESS;
159     }
160     for (index = 0; index < DEVICE_DRIVER_MAX_NUM; index++) {
161         if (g_cameraDeviceDriverFactory[index] == NULL) {
162             g_cameraDeviceDriverFactory[index] = obj;
163             HDF_LOGI("%s: device driver %{public}s registered.", __func__, obj->deviceName);
164             return HDF_SUCCESS;
165         }
166     }
167     HDF_LOGE("%s: Factory table is full", __func__);
168     return HDF_FAILURE;
169 }
170 
DeviceDriverManagerDeInit(void)171 int32_t DeviceDriverManagerDeInit(void)
172 {
173     int32_t cnt;
174     for (cnt = 0; cnt < DEVICE_DRIVER_MAX_NUM; cnt++) {
175         if (g_cameraDeviceDriverFactory[cnt] == NULL) {
176             continue;
177         }
178         if (g_cameraDeviceDriverFactory[cnt]->releaseFactory != NULL) {
179             g_cameraDeviceDriverFactory[cnt]->releaseFactory(g_cameraDeviceDriverFactory[cnt]);
180         }
181         g_cameraDeviceDriverFactory[cnt] = NULL;
182     }
183     return HDF_SUCCESS;
184 }
185 
186 static struct CameraDeviceDriverManager g_deviceDriverManager = {
187     .deviceFactoryInsts = g_cameraDeviceDriverFactory,
188     .regDeviceDriverFactory = CameraDeviceDriverFactoryRegister,
189     .getDeviceDriverFactoryByName = CameraDeviceDriverFactoryGetByName,
190 };
191 
CameraDeviceDriverManagerGet(void)192 struct CameraDeviceDriverManager *CameraDeviceDriverManagerGet(void)
193 {
194     return &g_deviceDriverManager;
195 }
196