mmi-vision-provider : add free vision_data, when vision_init error case
[platform/core/uifw/mmi-manager.git] / src / modules / modality_vision / mmi-vision-provider.c
1 /*
2 * Copyright © 2021 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "mmi-vision-provider.h"
25 #include "vision.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <Ecore.h>
30
31 mmi_provider_op_mode _provider_mode = MODALITY_PROVIDER_MODE_NONE;
32
33 static mmi_provider_module_data *
34 vision_modality_init(void)
35 {
36         mmi_provider_module_data *vision_data = NULL;
37         LOGD("vision_modality_init \n");
38
39         vision_data = (mmi_provider_module_data *)calloc(1, sizeof(mmi_provider_module_data));
40         if(!vision_data) {
41                 LOGE("Failed alloc vision modality provider data\n");
42                 return NULL;
43         }
44
45         vision_data->get_mode = vision_get_mode;
46         vision_data->set_mode = vision_set_mode;
47
48         int err = vision_init();
49         if(err != 0)
50         {
51                 LOGE("Failed vision_init = %d", err);
52                 free(vision_data);
53                 vision_data = NULL;
54                 return NULL;
55         }
56         return vision_data;
57 }
58
59 bool vision_set_mode(mmi_provider_op_mode mode)
60 {
61         LOGD("vision_set_mode = %d\n", mode);
62         if(!mode)
63                 return false;
64         _provider_mode = mode;
65
66         if(_provider_mode == MODALITY_PROVIDER_MODE_PROPAGATE_EVENT)
67         {
68                 //TODO. vision_streaming();
69                 ecore_event_add(MMI_VISION_EVENT_PROPAGATE, NULL, NULL, NULL);
70         }
71         return true;
72 }
73
74 mmi_provider_op_mode vision_get_mode(void)
75 {
76         LOGD("vision_get_mode \n");
77         return _provider_mode;
78 }
79
80 static void
81 vision_modality_deinit(mmi_provider_module_data *module_data)
82 {
83         if(!module_data) {
84                 LOGE("No have module_data\n");
85                 return;
86         }
87
88         LOGD("vision_modality_deinit \n");
89
90         module_data->get_mode = NULL;
91         module_data->set_mode = NULL;
92
93         free(module_data);
94         module_data = NULL;
95         int err = vision_shutdown();
96         if(err != 0)
97         {
98                 LOGE("Failed vision_init = %d", err);
99                 return;
100         }
101 }
102
103 MMI_PROVIDER_API mmi_provider_module mmi_provider_module_info = {
104         "vision-provider!",
105         "libmmi_modality_vision.so",
106         MODALITY_PROVIDER_CAP_VISION_EVENT,
107         MMI_PROVIDER_SET_ABI_VERSION(1,0),
108         vision_modality_init,
109         vision_modality_deinit
110 };