b1320a8d84d2607c3c4994099251139213147230
[apps/native/co2-meter.git] / src / co2sensor.c
1 /*
2  * Copyright (c) 2018 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 #include <service_app.h>
18 #include <stdio.h>
19 #include <resource/resource_co2_sensor.h>
20 #include "st_things.h"
21 #include "log.h"
22 #include <signal.h>
23
24 #define JSON_PATH "device_def.json"
25
26 static const char* RES_CAPABILITY_SWITCH = "/capability/switch/main/0";
27 static const char* RES_CAPABILITY_THERMOSTATCOOLINGSETPOINT = "/capability/thermostatCoolingSetpoint/main/0";
28 static const char* RES_CAPABILITY_AIRQUALITYSENSOR = "/capability/airQualitySensor/main/0";
29
30 pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;
31 extern int thread_done; /* resource_co2_sensor.c */
32
33 /* get and set request handlers */
34 extern bool handle_get_request_on_resource_capability_switch(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep);
35 extern bool handle_set_request_on_resource_capability_switch(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep);
36 extern bool handle_get_request_on_resource_capability_thermostatcoolingsetpoint(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep);
37 extern bool handle_set_request_on_resource_capability_thermostatcoolingsetpoint(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep);
38 extern bool handle_get_request_on_resource_capability_airqualitysensor(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep);
39
40 extern void *thread_sensor_main(void *arg);
41 extern void *thread_sensor_notify(void *arg);
42
43 /* handle : for getting request on resources */
44 bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
45 {
46         //DBG("resource_uri [%s]", req_msg->resource_uri);
47
48         if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_SWITCH)) {
49                 return handle_get_request_on_resource_capability_switch(req_msg, resp_rep);
50         }
51         if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_THERMOSTATCOOLINGSETPOINT)) {
52                 return handle_get_request_on_resource_capability_thermostatcoolingsetpoint(req_msg, resp_rep);
53         }
54         if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_AIRQUALITYSENSOR)) {
55                 return handle_get_request_on_resource_capability_airqualitysensor(req_msg, resp_rep);
56         }
57
58         ERR("not supported uri");
59         return false;
60 }
61
62 /* handle : for setting request on resources */
63 bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
64 {
65         DBG("resource_uri [%s]", req_msg->resource_uri);
66
67         if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_SWITCH)) {
68                 return handle_set_request_on_resource_capability_switch(req_msg, resp_rep);
69         }
70         if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_THERMOSTATCOOLINGSETPOINT)) {
71                 return handle_set_request_on_resource_capability_thermostatcoolingsetpoint(req_msg, resp_rep);
72         }
73
74         ERR("not supported uri");
75         return false;
76 }
77
78 /* callback functions */
79 bool handle_reset_request(void)
80 {
81     DBG("Received a request for RESET.");
82     return false;
83 }
84
85 void handle_reset_result(bool result)
86 {
87     DBG("Reset %s.\n", result ? "succeeded" : "failed");
88 }
89
90 bool handle_ownership_transfer_request(void)
91 {
92     DBG("Received a request for Ownership-transfer.");
93     return true;
94 }
95
96 void handle_things_status_change(st_things_status_e things_status)
97 {
98     DBG("Things status is changed: %d\n", things_status);
99 }
100
101 void sig_handler(int sig)
102 {
103         switch (sig) {
104         case SIGINT:
105                 INFO("SIGINT received");
106                 thread_done = 1;
107                 break;
108         case SIGSEGV:
109                 INFO("SIGSEGV received");
110                 thread_done = 1;
111                 break;
112         case SIGTERM:
113                 INFO("SIGTERM received");
114                 thread_done = 1;
115                 break;
116         default:
117                 ERR("wasn't expecting that sig [%d]", sig);
118                 abort();
119         }
120 }
121
122 /* main loop */
123 void handle_main_loop(void)
124 {
125         FN_CALL;
126
127         pthread_t p_thread[2];
128         int ret = 0;
129
130         signal(SIGSEGV, sig_handler);
131         signal(SIGINT, sig_handler);
132         signal(SIGTERM, sig_handler);
133
134         pthread_mutex_init(&mutex, NULL);
135
136         ret = pthread_create(&p_thread[0], NULL, &thread_sensor_main, NULL);
137         if (ret != 0) {
138                 ERR("[ERROR] thread_sensor_main create failed, ret=%d", ret);
139                 return;
140         }
141         ret = pthread_create(&p_thread[1], NULL, &thread_sensor_notify, NULL);
142         if (ret != 0) {
143                 ERR("[ERROR] thread_sensor_notify create failed, ret=%d", ret);
144                 return;
145         }
146 }
147
148 /* initialize */
149 void init_thing()
150 {
151         FN_CALL;
152         static bool binitialized = false;
153         if (binitialized) {
154                 DBG("Already initialized!!");
155                 return;
156         }
157
158         bool easysetup_complete = false;
159
160         char app_json_path[128] = {0,};
161         char *app_res_path = NULL;
162         char *app_data_path = NULL;
163
164         app_res_path = app_get_resource_path();
165         if (!app_res_path) {
166                 ERR("app_res_path is NULL!!");
167                 return;
168         }
169
170         app_data_path = app_get_data_path();
171         if (!app_data_path) {
172                 ERR("app_data_path is NULL!!");
173                 free(app_res_path);
174                 return;
175         }
176
177         snprintf(app_json_path, sizeof(app_json_path), "%s/%s", app_res_path, JSON_PATH);
178
179         if (0 != st_things_set_configuration_prefix_path((const char *)app_res_path, (const char *)app_data_path)) {
180                 ERR("st_things_set_configuration_prefix_path() failed!!");
181                 free(app_res_path);
182                 free(app_data_path);
183                 return;
184         }
185
186         free(app_res_path);
187         free(app_data_path);
188
189         if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
190                 ERR("st_things_initialize() failed!!");
191                 return;
192         }
193
194         binitialized = true;
195
196         DBG("easysetup_complete:[%d] ", easysetup_complete);
197
198         st_things_register_request_cb(handle_get_request, handle_set_request);
199         st_things_register_reset_cb(handle_reset_request, handle_reset_result);
200         st_things_register_user_confirm_cb(handle_ownership_transfer_request);
201         st_things_register_things_status_change_cb(handle_things_status_change);
202
203         st_things_start();
204
205         handle_main_loop();
206
207         FN_END;
208 }
209
210 static bool service_app_create(void *user_data)
211 {
212         UNUSED(user_data);
213
214         return true;
215 }
216
217 static void service_app_terminate(void *user_data)
218 {
219         UNUSED(user_data);
220
221         MUTEX_LOCK;
222         thread_done = 1;
223         MUTEX_UNLOCK;
224
225         int status = pthread_mutex_destroy(&mutex);
226     INFO("mutex destroy status = %d", status);
227 }
228
229 static void service_app_control(app_control_h app_control, void *user_data)
230 {
231         UNUSED(user_data);
232         if (app_control == NULL) {
233                 ERR("app_control is NULL");
234                 return;
235         }
236
237         init_thing();
238 }
239
240 int main(int argc, char *argv[])
241 {
242         FN_CALL;
243
244         service_app_lifecycle_callback_s event_callback;
245
246         event_callback.create = service_app_create;
247         event_callback.terminate = service_app_terminate;
248         event_callback.app_control = service_app_control;
249
250         return service_app_main(argc, argv, &event_callback, NULL);
251 }