2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <service_app.h>
19 #include <resource/resource_co2_sensor.h>
20 #include "st_things.h"
24 #define JSON_PATH "device_def.json"
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";
30 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
31 extern int thread_done; /* resource_co2_sensor.c */
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);
40 extern void *thread_sensor_main(void *arg);
41 extern void *thread_sensor_notify(void *arg);
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)
46 //DBG("resource_uri [%s]", req_msg->resource_uri);
48 if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_SWITCH)) {
49 return handle_get_request_on_resource_capability_switch(req_msg, resp_rep);
51 if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_THERMOSTATCOOLINGSETPOINT)) {
52 return handle_get_request_on_resource_capability_thermostatcoolingsetpoint(req_msg, resp_rep);
54 if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_AIRQUALITYSENSOR)) {
55 return handle_get_request_on_resource_capability_airqualitysensor(req_msg, resp_rep);
58 ERR("not supported uri");
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)
65 DBG("resource_uri [%s]", req_msg->resource_uri);
67 if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_SWITCH)) {
68 return handle_set_request_on_resource_capability_switch(req_msg, resp_rep);
70 if (0 == strcmp(req_msg->resource_uri, RES_CAPABILITY_THERMOSTATCOOLINGSETPOINT)) {
71 return handle_set_request_on_resource_capability_thermostatcoolingsetpoint(req_msg, resp_rep);
74 ERR("not supported uri");
78 /* callback functions */
79 bool handle_reset_request(void)
81 DBG("Received a request for RESET.");
85 void handle_reset_result(bool result)
87 DBG("Reset %s.\n", result ? "succeeded" : "failed");
90 bool handle_ownership_transfer_request(void)
92 DBG("Received a request for Ownership-transfer.");
96 void handle_things_status_change(st_things_status_e things_status)
98 DBG("Things status is changed: %d\n", things_status);
101 void sig_handler(int sig)
105 INFO("SIGINT received");
109 INFO("SIGSEGV received");
113 INFO("SIGTERM received");
117 ERR("wasn't expecting that sig [%d]", sig);
123 void handle_main_loop(void)
127 pthread_t p_thread[2];
130 signal(SIGSEGV, sig_handler);
131 signal(SIGINT, sig_handler);
132 signal(SIGTERM, sig_handler);
134 pthread_mutex_init(&mutex, NULL);
136 ret = pthread_create(&p_thread[0], NULL, &thread_sensor_main, NULL);
138 ERR("[ERROR] thread_sensor_main create failed, ret=%d", ret);
141 ret = pthread_create(&p_thread[1], NULL, &thread_sensor_notify, NULL);
143 ERR("[ERROR] thread_sensor_notify create failed, ret=%d", ret);
152 static bool binitialized = false;
154 DBG("Already initialized!!");
158 bool easysetup_complete = false;
160 char app_json_path[128] = {0,};
161 char *app_res_path = NULL;
162 char *app_data_path = NULL;
164 app_res_path = app_get_resource_path();
166 ERR("app_res_path is NULL!!");
170 app_data_path = app_get_data_path();
171 if (!app_data_path) {
172 ERR("app_data_path is NULL!!");
177 snprintf(app_json_path, sizeof(app_json_path), "%s/%s", app_res_path, JSON_PATH);
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!!");
189 if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
190 ERR("st_things_initialize() failed!!");
196 DBG("easysetup_complete:[%d] ", easysetup_complete);
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);
210 static bool service_app_create(void *user_data)
217 static void service_app_terminate(void *user_data)
225 int status = pthread_mutex_destroy(&mutex);
226 INFO("mutex destroy status = %d", status);
229 static void service_app_control(app_control_h app_control, void *user_data)
232 if (app_control == NULL) {
233 ERR("app_control is NULL");
240 int main(int argc, char *argv[])
244 service_app_lifecycle_callback_s event_callback;
246 event_callback.create = service_app_create;
247 event_callback.terminate = service_app_terminate;
248 event_callback.app_control = service_app_control;
250 return service_app_main(argc, argv, &event_callback, NULL);