Fixed: Coding rule issues.
[platform/core/connectivity/wifi-direct-manager.git] / src / wifi-direct-service.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5
6 #include <glib.h>
7
8 #include <wifi-direct.h>
9
10 #include "wifi-direct-ipc.h"
11 #include "wifi-direct-manager.h"
12 #include "wifi-direct-oem.h"
13 #include "wifi-direct-service.h"
14 #include "wifi-direct-util.h"
15 #include "wifi-direct-log.h"
16
17
18 int wfd_service_add(int type, char *info_str, int *service_id)
19 {
20         __WDS_LOG_FUNC_ENTER__;
21         wfd_manager_s *manager = wfd_get_manager();
22         wfd_service_s *service = NULL;
23         char *info1 = NULL;
24         char *info2 = NULL;
25         char *sep = NULL;
26         int res = 0;
27
28         if (!info_str) {
29                 WDS_LOGE("Invalid parameter");
30                 return -1;
31         }
32
33         if (type < WIFI_DIRECT_SERVICE_TYPE_BONJOUR ||
34                         type > WIFI_DIRECT_SERVICE_TYPE_VENDOR) {
35                 WDS_LOGE("Invalid service type");
36                 return -1;
37         }
38
39         service = (wfd_service_s*) g_try_malloc0(sizeof(wfd_service_s));
40         if (!service) {
41                 WDS_LOGE("Failed to allocate memory for service");
42                 return -1;
43         }
44
45         service->type = type;
46         service->id = (intptr_t) service;
47         info1 = g_strndup(info_str, strlen(info_str));
48         if (info1 == NULL) {
49                 WDS_LOGE("Failed to allocate memory for service");
50                 g_free(service);
51                 return -1;
52         }
53         sep = strchr(info1, '|');
54         if (sep == NULL) {
55                 WDS_LOGE("Failed to find delimiter");
56                 g_free(info1);
57                 g_free(service);
58                 return -1;
59         }
60
61         *sep = '\0';
62         info2 = sep + 1;
63
64         switch (service->type) {
65         case WIFI_DIRECT_SERVICE_TYPE_BONJOUR:
66                 service->data.bonjour.query = info1;
67                 if (strstr(info2, "ptr"))
68                         service->data.bonjour.rdata_type = WFD_BONJOUR_RDATA_PTR;
69                 else
70                         service->data.bonjour.rdata_type = WFD_BONJOUR_RDATA_TXT;
71
72                 service->data.bonjour.rdata = info2 +3;
73         break;
74         case WIFI_DIRECT_SERVICE_TYPE_UPNP:
75                 service->data.upnp.version = info1;
76                 service->data.upnp.service = info2;
77         break;
78         case WIFI_DIRECT_SERVICE_TYPE_WS_DISCOVERY:
79         case WIFI_DIRECT_SERVICE_TYPE_WIFI_DISPLAY:
80                 WDS_LOGE("Not supported yet");
81                 g_free(info1);
82                 g_free(service);
83                 return -1;
84         break;
85         case WIFI_DIRECT_SERVICE_TYPE_VENDOR:
86                 service->data.vendor.info1 = info1;
87                 service->data.vendor.info2 = info2;
88         break;
89         default:
90                 WDS_LOGE("Invalid service type");
91                 g_free(info1);
92                 g_free(service);
93                 return -1;
94         }
95
96         res = wfd_oem_serv_add(manager->oem_ops, (wfd_oem_new_service_s*) service);
97         if (res < 0) {
98                 WDS_LOGE("Failed to add service");
99                 g_free(info1);
100                 g_free(service);
101                 return -1;
102         }
103
104         service->str_ptr = info1;
105         manager->local->services = g_list_prepend(manager->local->services, service);
106         *service_id = service->id;
107
108         __WDS_LOG_FUNC_EXIT__;
109         return 0;
110 }
111
112 int wfd_service_del(int service_id)
113 {
114         __WDS_LOG_FUNC_ENTER__;
115         wfd_manager_s *manager = wfd_get_manager();
116         GList *temp = NULL;
117         wfd_service_s *service = NULL;
118         int res = 0;
119
120         if (!manager->local->services) {
121                 WDS_LOGE("No services to delete");
122                 return -1;
123         }
124
125         temp = g_list_first(manager->local->services);
126         while (temp) {
127                 service = (wfd_service_s*) temp->data;
128                 if (service->id == service_id) {
129                         WDS_LOGD("Service found");
130                         break;
131                 }
132                 service = NULL;
133                 temp = g_list_next(temp);
134         }
135
136         if (!service) {
137                 WDS_LOGE("Service not found");
138                 return -1;
139         }
140
141         res = wfd_oem_serv_del(manager->oem_ops, (wfd_oem_new_service_s*) service);
142         if (res < 0) {
143                 WDS_LOGE("Failed to add service");
144                 return -1;
145         }
146
147         manager->local->services = g_list_remove(manager->local->services, service);
148
149         g_free(service->str_ptr);
150         g_free(service);
151
152         __WDS_LOG_FUNC_EXIT__;
153         return 0;
154 }
155
156 #if 0
157 int wfd_service_disc_req(unsigned char *addr, int type, char *data)
158 {
159         __WDS_LOG_FUNC_ENTER__;
160         int handle = 0;
161         /* TODO: return identifier(handle) for the pending query */
162
163         if (!addr) {
164                 WDS_LOGE("Invalid parameter");
165                 return -1;
166         }
167
168         if (type < WFD_SERVICE_TYPE_ALL ||
169                         type > WFD_SERVICE_TYPE_VENDOR) {
170                 WDS_LOGE("Invalid service type");
171                 return -1;
172         }
173
174         /* TODO: call oem function */
175         /* TODO: add service information into service list */
176
177         __WDS_LOG_FUNC_EXIT__;
178         return handle;
179 }
180
181 int wfd_service_disc_cancel(int handle)
182 {
183         return 0;
184 }
185 #endif