Fix error return logic since refactoring
[platform/core/api/motion.git] / src / Activity.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
18 #include <stdlib.h>
19 #include "ActivityProxy.h"
20
21 #define FEATURE_ACTIVITY "tizen.org/feature/sensor.activity_recognition"
22
23 #define ACTIVITY_FIRST  ACTIVITY_STATIONARY
24 #define ACTIVITY_LAST   ACTIVITY_IN_VEHICLE
25 #define IS_VALID_ACTIVITY(X)    (ACTIVITY_FIRST <= (X) && (X) <= ACTIVITY_LAST)
26
27 using namespace motion;
28
29 struct _activity_handle_s {
30         ActivityProxy *proxy;
31 };
32
33 EXTAPI int activity_is_supported(activity_type_e activity, bool* supported)
34 {
35         if (supported)
36                 *supported = false;
37
38         ASSERT_SUPPORTED(FEATURE_ACTIVITY);
39         ASSERT_NOT_NULL(supported);
40         IF_FAIL_RETURN(IS_VALID_ACTIVITY(activity), ERR_INVALID_PARAMETER);
41
42         *supported = ActivityProxy::isSupported(activity);
43         return ERR_NONE;
44 }
45
46 EXTAPI int activity_create(activity_h *handle)
47 {
48         ASSERT_SUPPORTED(FEATURE_ACTIVITY);
49         ASSERT_NOT_NULL(handle);
50
51         _activity_handle_s *hdl = static_cast<activity_h>(malloc(sizeof(_activity_handle_s)));
52         IF_FAIL_RETURN_TAG(hdl, ERR_OPERATION_FAILED, _E, "Memory allocation failed");
53
54         hdl->proxy = new(std::nothrow) ActivityProxy();
55         if (hdl->proxy == NULL) {
56                 _E("Memory allocation failed");
57                 free(hdl);
58                 return ERR_OPERATION_FAILED;
59         }
60
61         *handle = hdl;
62         return ERR_NONE;
63 }
64
65 EXTAPI int activity_release(activity_h handle)
66 {
67         ASSERT_SUPPORTED(FEATURE_ACTIVITY);
68         ASSERT_NOT_NULL(handle);
69
70         delete handle->proxy;
71         free(handle);
72
73         return ERR_NONE;
74 }
75
76 EXTAPI int activity_start_recognition(activity_h handle, activity_type_e activity, activity_recognition_cb callback, void *user_data)
77 {
78         ASSERT_SUPPORTED(FEATURE_ACTIVITY);
79         ASSERT_NOT_NULL(handle);
80         ASSERT_NOT_NULL(callback);
81         IF_FAIL_RETURN(IS_VALID_ACTIVITY(activity), ERR_INVALID_PARAMETER);
82
83         if (!handle->proxy->setActivity(activity)) {
84                 return ERR_INVALID_PARAMETER;
85         }
86
87         handle->proxy->setPowerSave(false);
88         handle->proxy->setCb(callback);
89         handle->proxy->setUserData(user_data);
90
91         if (!handle->proxy->start()) {
92                 return ERR_OPERATION_FAILED;
93         }
94
95         return ERR_NONE;
96 }
97
98 EXTAPI int activity_stop_recognition(activity_h handle)
99 {
100         ASSERT_SUPPORTED(FEATURE_ACTIVITY);
101         ASSERT_NOT_NULL(handle);
102
103         IF_FAIL_RETURN(handle->proxy->stop(), ACTIVITY_ERROR_NOT_STARTED);
104
105         return ERR_NONE;
106 }
107
108 EXTAPI int activity_get_accuracy(const activity_data_h data, activity_accuracy_e *accuracy)
109 {
110         ASSERT_SUPPORTED(FEATURE_ACTIVITY);
111         ASSERT_NOT_NULL(data);
112         ASSERT_NOT_NULL(accuracy);
113
114         *accuracy = static_cast<activity_accuracy_e>(data->accuracy);
115
116         return ERR_NONE;
117 }