add web notification feature, but not enabled yet
[apps/native/position-finder-server.git] / src / webnotify.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *          Geunsun Lee <gs86.lee@samsung.com>
6  *          Eunyoung Lee <ey928.lee@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8  *          Jeonghoon Park <jh1979.park@samsung.com>
9  *
10  * Licensed under the Flora License, Version 1.1 (the License);
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://floralicense.org/license/
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an AS IS BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 #include <stdbool.h>
24 #include <glib.h>
25 #include <curl/curl.h>
26 #include "log.h"
27
28 static int _web_notify(const char *resource, const char *data)
29 {
30         CURL *curl;
31         CURLcode response;
32         curl = curl_easy_init();
33         int ret = 0;
34
35         if(!curl) {
36                 _E("fail to init curl");
37                 return -1;
38         }
39
40     curl_easy_setopt(curl, CURLOPT_URL, resource);
41     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
42
43     response = curl_easy_perform(curl);
44
45     if(response != CURLE_OK) {
46                 _E("curl_easy_perform() failed: %s",
47                         curl_easy_strerror(response));
48                 ret = -1;
49     }
50
51         curl_easy_cleanup(curl);
52
53         return ret;
54 }
55
56 int web_notify_init(void)
57 {
58         int ret = 0;
59         CURLcode result;
60     result = curl_global_init(CURL_GLOBAL_DEFAULT);
61     if(result != CURLE_OK) {
62                 _E("curl_global_init() failed: %s",
63                         curl_easy_strerror(result));
64                 ret = -1;
65     }
66         return ret;
67 }
68
69 void web_notify_fini(void)
70 {
71         curl_global_cleanup();
72         return;
73 }
74
75 int web_notify_data(const char *resource, const char *data)
76 {
77         return _web_notify(resource, data);
78 }
79
80 int web_notify_bool(const char *resource, bool value)
81 {
82         int ret = 0;
83
84         _D("Notify to %s - value[%d]", resource, value);
85
86         if(value)
87                 ret = _web_notify(resource, "TRUE");
88         else
89                 ret = _web_notify(resource, "FALSE");
90
91         return ret;
92 }
93
94 int web_notify_int(const char *resource, int value)
95 {
96         int ret = 0;
97         char *data = NULL;
98
99         data = g_strdup_printf("%d", value);
100         retv_if(NULL == data, -1);
101
102         ret = _web_notify(resource, data);
103         g_free(data);
104
105         return ret;
106 }
107
108 int web_notify_double(const char *resource, double value)
109 {
110         int ret = 0;
111         char *data = NULL;
112
113         data = g_strdup_printf("%lf", value);
114         retv_if(NULL == data, -1);
115
116         ret = _web_notify(resource, data);
117         g_free(data);
118
119         return ret;
120 }