Fix C# TCT RemovePersistentGroup_CHECK_NO_EXCEPTION issue
[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__;//LCOV_EXCL_LINE
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");//LCOV_EXCL_LINE
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");//LCOV_EXCL_LINE
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");//LCOV_EXCL_LINE
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");//LCOV_EXCL_LINE
50                 g_free(service);
51                 return -1;
52         }
53         sep = strchr(info1, '|');
54         if (sep == NULL) {
55                 WDS_LOGE("Failed to find delimiter");//LCOV_EXCL_LINE
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");//LCOV_EXCL_LINE
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");//LCOV_EXCL_LINE
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");//LCOV_EXCL_LINE
99                 g_free(info1);
100                 g_free(service);
101                 return -1;
102         }
103
104 #if defined(BUILD_GTESTS)
105                         service->id = 1;
106 #endif /* BUILD_GTESTS */
107         service->str_ptr = info1;
108         manager->local->services = g_list_prepend(manager->local->services, service);
109         *service_id = service->id;
110
111         __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
112         return 0;
113 }
114
115 int wfd_service_del(int service_id)
116 {
117         __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
118         wfd_manager_s *manager = wfd_get_manager();
119         GList *temp = NULL;
120         wfd_service_s *service = NULL;
121         int res = 0;
122
123         if (!manager->local->services) {
124                 WDS_LOGE("No services to delete");//LCOV_EXCL_LINE
125                 return -1;
126         }
127
128         temp = g_list_first(manager->local->services);
129         while (temp) {
130                 service = (wfd_service_s*) temp->data;
131                 if (service->id == service_id) {
132                         WDS_LOGD("Service found");//LCOV_EXCL_LINE
133                         break;
134                 }
135                 service = NULL;
136                 temp = g_list_next(temp);
137         }
138
139         if (!service) {
140                 WDS_LOGE("Service not found");//LCOV_EXCL_LINE
141                 return -1;
142         }
143
144         res = wfd_oem_serv_del(manager->oem_ops, (wfd_oem_new_service_s*) service);
145         if (res < 0) {
146                 WDS_LOGE("Failed to add service");//LCOV_EXCL_LINE
147                 return -1;
148         }
149
150         manager->local->services = g_list_remove(manager->local->services, service);
151
152         g_free(service->str_ptr);
153         g_free(service);
154
155         __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
156         return 0;
157 }
158
159 #if 0
160 int wfd_service_disc_req(unsigned char *addr, int type, char *data)
161 {
162         __WDS_LOG_FUNC_ENTER__;//LCOV_EXCL_LINE
163         int handle = 0;
164         /* TODO: return identifier(handle) for the pending query */
165
166         if (!addr) {
167                 WDS_LOGE("Invalid parameter");
168                 return -1;
169         }
170
171         if (type < WFD_SERVICE_TYPE_ALL ||
172                         type > WFD_SERVICE_TYPE_VENDOR) {
173                 WDS_LOGE("Invalid service type");
174                 return -1;
175         }
176
177         /* TODO: call oem function */
178         /* TODO: add service information into service list */
179
180         __WDS_LOG_FUNC_EXIT__;//LCOV_EXCL_LINE
181         return handle;
182 }
183
184 int wfd_service_disc_cancel(int handle)
185 {
186         return 0;
187 }
188 #endif