1 /*
2  * Copyright (C) 2021-2023 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include "ulog/ulog.h"
8 #include "aos/kernel.h"
9 #include "ucamera_service.h"
10 #include "ucamera_device.h"
11 
12 #define TAG "UCAMERA_SERVICE"
13 
14 static ucamera_device_t *g_ucamera_dev = NULL;
15 
16 /* Available camera context */
17 static ucamera_context_t *camera_ctx[] = {
18     &wifi_camera,
19     NULL
20 };
21 
22 /*
23  * Initialize the ucamera device
24  */
ucamera_device_init(const char * dev_name)25 ucamera_device_t *ucamera_device_init(const char *dev_name)
26 {
27     ucamera_device_t *device;
28     int32_t index;
29     int32_t i;
30 
31     /*Reset camera service*/
32     if (g_ucamera_dev != NULL)
33         ucamera_device_uninit();
34 
35     /* Select the proper device driver */
36     index = 0;
37     device = NULL;
38     if (dev_name == NULL)
39         return NULL;
40 
41     if (dev_name != NULL) {
42         for (i = 0; camera_ctx[i]; ++i) {
43             if (strncasecmp(camera_ctx[i]->name, dev_name, strlen(dev_name)) == 0) {
44                 if (camera_ctx[i]->available()) {
45                     device = camera_ctx[i]->create(index);
46                     break;
47                 }
48             }
49         }
50     } else {
51         for (i = 0; camera_ctx[i]; ++i) {
52             if (camera_ctx[i]->available()) {
53                 device = camera_ctx[i]->create(index);
54                 if (device != NULL)
55                     break;
56 
57             }
58         }
59     }
60     if (device == NULL) {
61         if (dev_name) {
62             LOGE(TAG, "%s not available", dev_name);
63             return NULL;
64         }
65         LOGE(TAG, "No available device device");
66         return NULL;
67     }
68     g_ucamera_dev = device;
69     g_ucamera_dev->name = camera_ctx[i]->name;
70 
71     /* Initialize the camera device */
72     if (g_ucamera_dev->camera_init(g_ucamera_dev) < 0) {
73         ucamera_device_uninit();
74         return NULL;
75     }
76 
77     return g_ucamera_dev;
78 }
79 
80 /*
81  * Uninitialize the ucamera device
82  */
ucamera_device_uninit(void)83 void ucamera_device_uninit(void)
84 {
85     int32_t i, j;
86 
87     if (!g_ucamera_dev)
88         return;
89 
90     g_ucamera_dev->camera_uninit(g_ucamera_dev);
91     g_ucamera_dev->camera_free(g_ucamera_dev);
92     g_ucamera_dev = NULL;
93 
94     return;
95 }
96 
ucamera_get_device_name(void)97 const char *ucamera_get_device_name(void)
98 {
99     if (!g_ucamera_dev)
100         return NULL;
101 
102     return g_ucamera_dev->name;
103 }
104 
ucamera_get_device(void)105 ucamera_device_t *ucamera_get_device(void)
106 {
107     return g_ucamera_dev;
108 }
109