Tizen 2.0 Release
[platform/core/connectivity/neard-tizen-plugin.git] / src / neard-plugin.c
1 /*
2  * Copyright (c) 2012, Intel Corportation
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 <stdlib.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <malloc.h>
25 #include <syslog.h>
26 #include <glib.h>
27 #include <errno.h>
28 #include <sys/socket.h>
29 #include <linux/socket.h>
30
31 #include "net_nfc_oem_controller.h"
32 #include "net_nfc_typedef.h"
33 #include "net_nfc_util_private.h"
34 #include "net_nfc_util_ndef_message.h"
35 #include "net_nfc_util_ndef_record.h"
36
37 #include "neardal.h"
38 #include "nfc.h"
39
40 #ifndef NET_NFC_EXPORT_API
41 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
42 #endif
43
44 /* HACK HACK */
45 #ifndef AF_NFC
46 #define AF_NFC 39
47 #endif
48
49 /* TODO get real data */
50 #define NET_NFC_JEWEL_PICC_MAX_SIZE             116
51 #define NET_NFC_MIFARE_ULTRA_PICC_MAX_SIZE      48
52 #define NET_NFC_FELICA_PICC_MAX_SIZE            256
53 #define NET_NFC_MIFARE_DESFIRE_PICC_MAX_SIZE    2048
54
55 /*************************** INTERFACE CALLBACKS *****************************/
56 static target_detection_listener_cb  interface_target_detection_cb;
57 static se_transaction_listener_cb    interface_se_transaction_cb;
58 static llcp_event_listener_cb        interface_llcp_event_cb;
59
60 /*************************** INTERFACE START *********************************/
61 static bool neard_plugin_init(net_nfc_error_e* result);
62 static bool neard_plugin_deinit(void);
63 static bool neard_plugin_register_listener(
64                         target_detection_listener_cb target_detection_listener,
65                         se_transaction_listener_cb se_transaction_listener,
66                         llcp_event_listener_cb llcp_event_listener,
67                         net_nfc_error_e* result);
68 static bool neard_plugin_unregister_listener();
69 static bool neard_plugin_check_firmware_version(net_nfc_error_e* result);
70 static bool neard_plugin_update_firmware(net_nfc_error_e* result);
71 static bool neard_plugin_get_stack_information(
72                                 net_nfc_stack_information_s* stack_info,
73                                 net_nfc_error_e* result);
74 static bool neard_plugin_configure_discovery(
75                                 net_nfc_discovery_mode_e mode,
76                                 net_nfc_event_filter_e config,
77                                 net_nfc_error_e* result);
78 static bool neard_plugin_get_secure_element_list(
79                                 net_nfc_secure_element_info_s* list,
80                                 int* count, net_nfc_error_e* result);
81 static bool neard_plugin_set_secure_element_mode(
82                                 net_nfc_secure_element_type_e element_type,
83                                 net_nfc_secure_element_mode_e mode,
84                                 net_nfc_error_e* result);
85 static bool neard_plugin_connect(net_nfc_target_handle_s* handle,
86                                         net_nfc_error_e* result);
87 static bool neard_plugin_disconnect(net_nfc_target_handle_s* handle,
88                                         net_nfc_error_e* result);
89 static bool neard_plugin_check_ndef(net_nfc_target_handle_s* handle,
90                                         uint8_t *ndef_card_state,
91                                         int* max_data_size,
92                                         int* real_data_size,
93                                         net_nfc_error_e* result);
94 static bool neard_plugin_check_target_presence(net_nfc_target_handle_s* handle,
95                                                 net_nfc_error_e* result);
96 static bool neard_plugin_read_ndef(net_nfc_target_handle_s* handle,
97                                         data_s** data,
98                                         net_nfc_error_e* result);
99 static bool neard_plugin_write_ndef(net_nfc_target_handle_s* handle,
100                                         data_s* data, net_nfc_error_e* result);
101 static bool neard_plugin_make_read_only_ndef(net_nfc_target_handle_s* handle,
102                                         net_nfc_error_e* result);
103 static bool neard_plugin_transceive(net_nfc_target_handle_s* handle,
104                                         net_nfc_transceive_info_s* info,
105                                         data_s** data,
106                                         net_nfc_error_e* result);
107 static bool neard_plugin_format_ndef(net_nfc_target_handle_s* handle,
108                                         data_s* secure_key,
109                                         net_nfc_error_e* result);
110 static bool neard_plugin_exception_handler(void);
111 static bool neard_plugin_is_ready(net_nfc_error_e* error);
112
113 static bool neard_plugin_llcp_config(net_nfc_llcp_config_info_s *config,
114                                                 net_nfc_error_e *result);
115 static bool neard_plugin_llcp_check_llcp(net_nfc_target_handle_s *handle,
116                                                 net_nfc_error_e *result);
117 static bool neard_plugin_llcp_activate_llcp(net_nfc_target_handle_s *handle,
118                                                 net_nfc_error_e *result);
119 static bool neard_plugin_llcp_create_socket(net_nfc_llcp_socket_t *socket,
120                                         net_nfc_socket_type_e socketType,
121                                         uint16_t miu, uint8_t rw,
122                                         net_nfc_error_e* result,
123                                         void * user_param);
124 static bool neard_plugin_llcp_bind(net_nfc_llcp_socket_t socket,
125                                         uint8_t service_access_point,
126                                         net_nfc_error_e* result);
127 static bool neard_plugin_llcp_listen(net_nfc_target_handle_s* handle,
128                                         uint8_t* service_access_name,
129                                         net_nfc_llcp_socket_t socket,
130                                         net_nfc_error_e* result,
131                                         void * user_param);
132 static bool neard_plugin_llcp_accept(net_nfc_llcp_socket_t socket,
133                                         net_nfc_error_e* result);
134 static bool neard_plugin_llcp_connect_by_url(net_nfc_target_handle_s* handle,
135                                         net_nfc_llcp_socket_t socket,
136                                         uint8_t* service_access_name,
137                                         net_nfc_error_e* result,
138                                         void * user_param);
139 static bool neard_plugin_llcp_connect(net_nfc_target_handle_s* handle,
140                                         net_nfc_llcp_socket_t socket,
141                                         uint8_t service_access_point,
142                                         net_nfc_error_e* result,
143                                         void * user_param);
144 static bool neard_plugin_llcp_reject(net_nfc_target_handle_s* handle,
145                                         net_nfc_llcp_socket_t  socket,
146                                         net_nfc_error_e* result);
147 static bool neard_plugin_llcp_disconnect(net_nfc_target_handle_s* handle,
148                                         net_nfc_llcp_socket_t socket,
149                                         net_nfc_error_e* result,
150                                         void * user_param);
151 static bool neard_plugin_llcp_socket_close(net_nfc_llcp_socket_t socket,
152                                         net_nfc_error_e* result);
153 static bool neard_plugin_llcp_recv(net_nfc_target_handle_s* handle,
154                                         net_nfc_llcp_socket_t socket,
155                                         data_s* data, net_nfc_error_e* result,
156                                         void * user_param);
157 static bool neard_plugin_llcp_send(net_nfc_target_handle_s* handle,
158                                         net_nfc_llcp_socket_t socket,
159                                         data_s* data, net_nfc_error_e* result,
160                                         void * user_param);
161 static bool neard_plugin_llcp_recv_from(net_nfc_target_handle_s* handle,
162                                         net_nfc_llcp_socket_t socket,
163                                         data_s * data,
164                                         net_nfc_error_e* result,
165                                         void * user_param);
166 static bool neard_plugin_llcp_send_to(net_nfc_target_handle_s* handle,
167                                         net_nfc_llcp_socket_t socket,
168                                         data_s* data,
169                                         uint8_t service_access_point,
170                                         net_nfc_error_e* result,
171                                         void * user_param);
172 static bool neard_plugin_llcp_get_remote_config(
173                                         net_nfc_target_handle_s* handle,
174                                         net_nfc_llcp_config_info_s *config,
175                                         net_nfc_error_e* result);
176 static bool neard_plugin_llcp_get_remote_socket_info(
177                                         net_nfc_target_handle_s* handle,
178                                         net_nfc_llcp_socket_t socket,
179                                         net_nfc_llcp_socket_option_s * option,
180                                         net_nfc_error_e* result);
181 static bool neard_plugin_support_nfc(net_nfc_error_e* result);
182 static bool neard_plugin_eedata_register_set(net_nfc_error_e* result,
183                                         uint32_t mode, uint32_t reg_id,
184                                         data_s* data);
185
186 /*************************** INTERFACE END ***********************************/
187
188 struct neard_plugin_p2p_data {
189         uint32_t adapter_idx;
190         uint32_t target_idx;
191         int socket;
192         GIOChannel *channel;
193         guint watch;
194         void *user_param;
195         GList *client_list;
196         net_nfc_target_handle_s* handle;
197 };
198
199 static char *neard_adp_path = NULL;
200 static bool neard_adp_powered = false;
201 static bool neard_adp_polling = false;
202 static char *neard_adp_mode   = NULL;
203 static net_nfc_target_handle_s *current_handle = NULL;
204 static neardal_tag *tag = NULL;
205 static neardal_dev *dev = NULL;
206 static GHashTable *server_hash;
207
208 static void neard_plugin_debug(const char *format, ...)
209                         __attribute__((format(printf, 1, 2)));
210
211 #define DBG(fmt, arg...) neard_plugin_debug("%d:%s() " fmt, \
212                                 __LINE__ ,__FUNCTION__ , ## arg);
213
214 /* neard plugin logging functions */
215 static void neard_plugin_log_open(const char *name)
216 {
217         openlog(name, LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
218 }
219
220 static void neard_plugin_log_close(void)
221 {
222         closelog();
223 }
224
225 static void neard_plugin_debug(const char *format, ...)
226 {
227         va_list ap;
228
229         va_start(ap, format);
230         vsyslog(LOG_DEBUG, format, ap);
231         va_end(ap);
232 }
233
234 /* Target handle related functions */
235 static bool __neard_plugin_is_valid_target_handle(
236                                 net_nfc_target_handle_s *handle)
237 {
238         return (current_handle == handle);
239 }
240
241 static void __neard_plugin_make_valid_target_handle(
242                                 net_nfc_target_handle_s **handle)
243 {
244         if (current_handle != NULL) {
245                 *handle = current_handle;
246                 return;
247         }
248
249         *handle = g_try_malloc0(sizeof(net_nfc_target_handle_s));
250         if (*handle != NULL)
251                 current_handle = *handle;
252 }
253
254 static void __neard_plugin_make_invalid_target_handle()
255
256 {
257         if (current_handle != NULL) {
258                 g_free(current_handle);
259                 current_handle = NULL;
260         }
261 }
262
263 /* neard plugin helper funtions */
264 static int __neard_plugin_convert_target_type(const char *type)
265 {
266         int t_type;
267
268         DBG(" %s", type);
269
270         /* TODO tag sub types */
271
272         if (type == NULL)
273                 return NET_NFC_UNKNOWN_TARGET;
274
275         if (!g_strcmp0(type, "Type 1"))
276                 t_type = NET_NFC_JEWEL_PICC;
277         else if (!g_strcmp0(type, "Type 2"))
278                 t_type = NET_NFC_MIFARE_ULTRA_PICC;
279         /*
280                 NET_NFC_MIFARE_MINI_PICC
281                 NET_NFC_MIFARE_1K_PICC
282                 NET_NFC_MIFARE_4K_PICC
283                 NET_NFC_MIFARE_ULTRA_PICC
284         */
285         else if (!g_strcmp0(type, "Type 3"))
286                 t_type = NET_NFC_FELICA_PICC;
287         else if (!g_strcmp0(type, "Type 4"))
288                 t_type = NET_NFC_MIFARE_DESFIRE_PICC;
289         else if (!g_strcmp0(type, "Target"))
290                 t_type = NET_NFC_NFCIP1_TARGET;
291         else if (!g_strcmp0(type, "Initiator"))
292                 t_type = NET_NFC_NFCIP1_INITIATOR;
293         else
294                 t_type = NET_NFC_UNKNOWN_TARGET;
295
296         return t_type;
297 }
298
299 static uint32_t __neard_plugin_get_tag_id(const char *name)
300 {
301         uint32_t id;
302         char **s;
303
304         s = g_strsplit(name, "tag", 2);
305         id = atoi(s[1]);
306         g_strfreev(s);
307
308         return id;
309 }
310
311 static uint32_t __neard_plugin_get_dev_id(const char *name)
312 {
313         uint32_t id;
314         char **s;
315
316         s = g_strsplit(name, "device", 2);
317         id = atoi(s[1]);
318         g_strfreev(s);
319
320         return id;
321 }
322
323 static uint32_t __neard_plugin_get_adapter_id(const char *name)
324 {
325         uint32_t id;
326         char **s;
327
328         s = g_strsplit(name, "nfc", 2);
329         id = atoi(s[1]);
330         g_strfreev(s);
331
332         return id;
333 }
334
335 static void __neard_plugin_free_client(gpointer data, gpointer user_data)
336 {
337         int socket = GPOINTER_TO_INT(data);
338
339         DBG("");
340         close(socket);
341 }
342
343 static gboolean __neard_plugin_free_server(gpointer key, gpointer value,
344                                                 gpointer user_data)
345 {
346         int socket = GPOINTER_TO_INT(key);
347         struct neard_plugin_p2p_data *server = value;
348
349         if (server == NULL)
350                 return TRUE;
351
352         if (server->watch > 0)
353                 g_source_remove(server->watch);
354
355         server->watch = 0;
356
357         if (server->client_list != NULL) {
358                 g_list_foreach(server->client_list,
359                                 __neard_plugin_free_client, NULL);
360                 g_list_free(server->client_list);
361                 server->client_list = NULL;
362         }
363
364         close(socket);
365
366         return TRUE;
367 }
368
369 static void __neard_plugin_free_server_hash(void)
370 {
371         DBG("");
372
373         g_hash_table_foreach_remove(server_hash,
374                                         __neard_plugin_free_server,
375                                         NULL);
376 }
377
378 /* neardal callbaks */
379 static void __neardal_adp_added_cb(const char *adpName, void *user_data)
380 {
381         DBG("");
382
383         if (neard_adp_path) {
384                 g_free(neard_adp_path);
385                 neard_adp_path = NULL;
386         }
387
388         if (adpName != NULL)
389                 neard_adp_path = g_strdup(adpName);
390 }
391
392 static void __neardal_adp_removed_cb(const char *adpName, void *user_data)
393 {
394         DBG("");
395
396         if (neard_adp_path) {
397                 g_free(neard_adp_path);
398                 neard_adp_path = NULL;
399         }
400 }
401
402 static void __neardal_adp_prop_changed_cb(char *adpName, char *propName,
403                                                 void *value, void *user_data)
404 {
405         DBG(" %s", propName);
406
407         if (!g_strcmp0(propName, "Polling")) {
408                 if ((int*) value == 0) {
409                         neard_adp_polling = false;
410                         DBG(" Stopped");
411                 } else {
412                         neard_adp_polling = true;
413                         DBG(" Started");
414                 }
415         } else if (!g_strcmp0(propName, "Mode")) {
416                 if (neard_adp_mode) {
417                         g_free(neard_adp_mode);
418                         neard_adp_mode = NULL;
419                 }
420
421                 neard_adp_mode = g_strdup((char *)value);
422                 DBG(" '%s'", neard_adp_mode);
423         }
424 }
425
426 static void __neardal_tag_found_cb(const char *tagName, void *user_data)
427 {
428         DBG("");
429
430         net_nfc_target_handle_s *handle = NULL;
431         net_nfc_request_target_detected_t *tag_detected = NULL;
432
433         if (neardal_get_tag_properties(tagName, &tag) != NEARDAL_SUCCESS)
434                 return;
435
436         __neard_plugin_make_valid_target_handle(&handle);
437         if (handle == NULL)
438                 goto out_err;
439
440         handle->connection_id   = __neard_plugin_get_tag_id(tagName);
441         handle->connection_type = NET_NFC_TAG_CONNECTION;
442
443         tag_detected = g_try_malloc0(sizeof(net_nfc_request_target_detected_t));
444         if (tag_detected == NULL)
445                 goto out_err;
446
447         tag_detected->length = sizeof(net_nfc_request_target_detected_t);
448         tag_detected->request_type = NET_NFC_MESSAGE_SERVICE_STANDALONE_TARGET_DETECTED;
449         tag_detected->devType = __neard_plugin_convert_target_type(tag->type);
450         tag_detected->number_of_keys = 0;
451         tag_detected->handle = handle;
452         tag_detected->target_info_values.length = 0;
453
454         if (interface_target_detection_cb != NULL)
455                 interface_target_detection_cb(tag_detected, NULL);
456
457         return;
458
459 out_err:
460         g_free(tag_detected);
461 }
462
463 static void __neardal_tag_lost_cb(const char *tagName, void *userdata)
464 {
465         DBG("");
466
467         if (tag != NULL) {
468                 neardal_free_tag(tag);
469                 tag = NULL;
470         }
471
472         if (neard_adp_polling == true)
473                 return;
474
475         if (neardal_start_poll_loop(neard_adp_path,
476                         NEARD_ADP_MODE_DUAL) == NEARDAL_SUCCESS)
477                 neard_adp_polling = true;
478
479 }
480
481 static void __neardal_record_found_cb(const char *rcdName, void *user_data)
482 {
483
484 }
485
486 static void __neardal_device_found_cb(const char *devName, void *user_data)
487 {
488         net_nfc_target_handle_s *handle = NULL;
489         net_nfc_request_target_detected_t *dev_detected = NULL;
490
491         DBG("");
492
493         if (g_hash_table_size(server_hash) > 0) {
494                  __neard_plugin_free_server_hash();
495         }
496
497         if (neardal_get_dev_properties(devName, &dev) != NEARDAL_SUCCESS)
498                 return;
499
500         __neard_plugin_make_valid_target_handle(&handle);
501         if (handle == NULL)
502                 goto out_err;
503
504         handle->connection_id = __neard_plugin_get_dev_id(devName);
505
506         dev_detected = g_try_malloc0(sizeof(net_nfc_request_target_detected_t));
507         if (dev_detected == NULL)
508                 goto out_err;
509
510         dev_detected->length = sizeof(net_nfc_request_target_detected_t);
511         dev_detected->request_type = NET_NFC_MESSAGE_SERVICE_STANDALONE_TARGET_DETECTED;
512         /* Note: This one is neard role, depends upon neard role we are
513          * deciding detected device role. */
514         dev_detected->devType = __neard_plugin_convert_target_type(neard_adp_mode);
515         dev_detected->number_of_keys = 0;
516         dev_detected->target_info_values.length = 0;
517
518         if (dev_detected->devType == NET_NFC_NFCIP1_INITIATOR)
519                 handle->connection_type = NET_NFC_P2P_CONNECTION_TARGET;
520         else if(dev_detected->devType == NET_NFC_NFCIP1_TARGET)
521                 handle->connection_type = NET_NFC_P2P_CONNECTION_INITIATOR;
522
523         dev_detected->handle = handle;
524
525         if (interface_target_detection_cb != NULL)
526                 interface_target_detection_cb(dev_detected, NULL);
527
528         return;
529
530 out_err:
531         g_free(dev_detected);
532 }
533
534 static void __neardal_device_lost_cb(const char *devName, void *user_data)
535 {
536         net_nfc_request_llcp_msg_t *message;
537
538         DBG("");
539
540         __neard_plugin_free_server_hash();
541         __neard_plugin_make_invalid_target_handle();
542
543         if (dev != NULL) {
544                 neardal_free_device(dev);
545                 dev = NULL;
546         }
547
548         message = g_try_malloc0(sizeof(net_nfc_request_llcp_msg_t));
549         if (message != NULL) {
550                 message->length = sizeof(net_nfc_request_llcp_msg_t);
551                 message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_DEACTIVATED;
552                 interface_llcp_event_cb(message, NULL);
553         }
554
555         if (neard_adp_polling == true)
556                 return;
557
558         if (neardal_start_poll_loop(neard_adp_path,
559                         NEARD_ADP_MODE_DUAL) == NEARDAL_SUCCESS)
560                 neard_adp_polling = true;
561 }
562
563 static errorCode_t __neardal_init(void)
564 {
565         errorCode_t err;
566         neardal_adapter *neard_adp = NULL;
567         char **adapters = NULL;
568         int len, powered;
569
570         DBG("");
571
572         server_hash = g_hash_table_new_full(g_direct_hash,
573                                                 g_direct_equal,
574                                                 NULL, NULL);
575
576         /* Get neard adapters */
577         err = neardal_get_adapters(&adapters, &len);
578         if (err != NEARDAL_SUCCESS)
579                 goto out_err;
580
581         if (!(len > 0 && adapters != NULL))
582                 goto out_err;
583
584         /* Cache the current/default adapter path */
585         neard_adp_path = g_strdup(adapters[0]);
586         neardal_free_array(&adapters);
587         adapters = NULL;
588
589         /* Get adapters properties */
590         err = neardal_get_adapter_properties(neard_adp_path, &neard_adp);
591         if (err != NEARDAL_SUCCESS)
592                 goto out_err;
593
594         if (neard_adp == NULL) {
595                 err = NEARDAL_ERROR_DBUS;
596                 goto out_err;
597         }
598
599         neard_adp_powered = (neard_adp->powered) ? true : false;
600         neard_adp_polling = (neard_adp->polling) ? true : false;
601         neardal_free_adapter(neard_adp);
602         neard_adp = NULL;       
603
604         /* If adpater is not powered on then power it on */
605         if (neard_adp_powered == false) {
606                 powered = 1;
607                 err = neardal_set_adapter_property(neard_adp_path,
608                                 NEARD_ADP_PROP_POWERED, (void*) powered);
609                 if (err != NEARDAL_SUCCESS)
610                         goto out_err;
611
612                 neard_adp_powered = true;
613         }
614
615         if (neard_adp_polling == false) {
616                 err = neardal_start_poll_loop(neard_adp_path,
617                                                 NEARD_ADP_MODE_DUAL);
618                 if (err != NEARDAL_SUCCESS)
619                         goto out_err;
620
621                 neard_adp_polling = true;
622         }
623
624         return NEARDAL_SUCCESS;
625                                                         
626 out_err:
627
628         if (adapters)
629                 neardal_free_array(&adapters);
630
631         if (neard_adp)
632                 neardal_free_adapter(neard_adp);
633
634         return err;
635 }
636
637 static bool neard_plugin_init(net_nfc_error_e *result)
638 {
639         neard_plugin_log_open("neard-plugin");
640
641         DBG("");
642
643         if (neardal_set_cb_adapter_added(
644                         __neardal_adp_added_cb, NULL) != NEARDAL_SUCCESS ||
645                 neardal_set_cb_adapter_removed(
646                         __neardal_adp_removed_cb, NULL)  != NEARDAL_SUCCESS ||
647                 neardal_set_cb_adapter_property_changed(
648                         __neardal_adp_prop_changed_cb, NULL)  != NEARDAL_SUCCESS ||
649                 neardal_set_cb_tag_found(
650                         __neardal_tag_found_cb, NULL) != NEARDAL_SUCCESS ||
651                 neardal_set_cb_tag_lost(
652                         __neardal_tag_lost_cb, NULL) != NEARDAL_SUCCESS ||
653                 neardal_set_cb_record_found(
654                         __neardal_record_found_cb, NULL) != NEARDAL_SUCCESS ||
655                 neardal_set_cb_dev_found(
656                         __neardal_device_found_cb, NULL) != NEARDAL_SUCCESS ||
657                 neardal_set_cb_dev_lost(
658                         __neardal_device_lost_cb, NULL) != NEARDAL_SUCCESS) {
659
660                 *result = NET_NFC_INVALID_HANDLE;
661
662                 return false;
663         }
664
665         if (__neardal_init() != NEARDAL_SUCCESS) {
666                 *result = NET_NFC_UNKNOWN_ERROR;
667
668                 return false;
669         }
670
671         *result = NET_NFC_OK;
672
673         return true;
674 }
675
676 static bool neard_plugin_deinit(void)
677 {
678         int powered = 0;
679
680         DBG("");
681
682         if (neard_adp_polling == true)
683                 neardal_stop_poll(neard_adp_path);
684
685         if (neard_adp_powered == true)
686                 neardal_set_adapter_property(neard_adp_path,
687                                 NEARD_ADP_PROP_POWERED, (void*) powered);
688
689         if (neard_adp_path) {
690                 g_free(neard_adp_path);
691                 neard_adp_path = NULL;
692         }
693
694         if (neard_adp_mode) {
695                 g_free(neard_adp_mode);
696                 neard_adp_mode = NULL;
697         }
698
699         if (tag != NULL) {
700                 neardal_free_tag(tag);
701                 tag = NULL;
702         }
703
704         if (dev != NULL) {
705                 neardal_free_device(dev);
706                 dev = NULL;
707         }
708
709         if (server_hash != NULL) {
710                 __neard_plugin_free_server_hash();
711                 g_hash_table_destroy(server_hash);
712                 server_hash = NULL;
713         }
714
715         neardal_destroy();
716         neard_plugin_log_close();
717
718         return true;
719 }
720
721 static bool neard_plugin_register_listener(
722                         target_detection_listener_cb target_detection_listener,
723                         se_transaction_listener_cb se_transaction_listener,
724                         llcp_event_listener_cb llcp_event_listener,
725                         net_nfc_error_e* result)
726 {
727         if (result == NULL)
728                 return false;
729
730         DBG("");
731
732         interface_target_detection_cb  = target_detection_listener;
733         interface_se_transaction_cb    = se_transaction_listener;
734         interface_llcp_event_cb        = llcp_event_listener;
735
736         *result = NET_NFC_OK;
737
738         return true;
739 }
740
741 static bool neard_plugin_unregister_listener()
742 {
743         DBG("");
744
745         interface_target_detection_cb = NULL;
746         interface_se_transaction_cb   = NULL;
747         interface_llcp_event_cb   = NULL;
748
749         return true;
750 }
751
752 static bool neard_plugin_support_nfc(net_nfc_error_e* result)
753 {
754         char **adapters = NULL;
755         int len;
756         errorCode_t err;
757
758         DBG("");
759
760         if (result == NULL)
761                 return false;
762
763         /* Get neard adapters */
764         err = neardal_get_adapters(&adapters, &len);
765         if (err != NEARDAL_SUCCESS) {
766                 *result = NET_NFC_NOT_SUPPORTED;
767                 return false;
768         }
769
770         if (!(len > 0 && adapters != NULL)) {
771                 *result = NET_NFC_NOT_SUPPORTED;
772                 return false;
773         }
774
775         neardal_free_array(&adapters);
776         adapters = NULL;
777
778         *result = NET_NFC_OK;
779         return true;
780 }
781
782 static bool neard_plugin_check_firmware_version(net_nfc_error_e* result)
783 {
784         DBG("");
785
786         if (result == NULL)
787                 return false;
788
789         *result = NET_NFC_NOT_SUPPORTED;
790
791         return false;
792 }
793
794 static bool neard_plugin_update_firmware(net_nfc_error_e* result)
795 {
796         DBG("");
797
798         if (result == NULL)
799                 return false;
800
801         *result = NET_NFC_NOT_SUPPORTED;
802
803         return false;
804 }
805
806 static bool neard_plugin_get_stack_information(
807                                 net_nfc_stack_information_s* stack_info,
808                                 net_nfc_error_e* result)
809 {
810         DBG("");
811
812         if (result == NULL)
813                 return false;
814
815         *result = NET_NFC_NOT_SUPPORTED;
816
817         return false;
818 }
819
820 static bool neard_plugin_configure_discovery(
821                                 net_nfc_discovery_mode_e mode,
822                                 net_nfc_event_filter_e config,
823                                 net_nfc_error_e* result)
824 {
825         DBG("");
826
827         if (result == NULL)
828                 return false;
829
830         if (mode == NET_NFC_DISCOVERY_MODE_CONFIG &&
831                         config == NET_NFC_ALL_ENABLE) {
832                 *result = NET_NFC_OK;
833                 return true;
834         }
835
836         *result = NET_NFC_NOT_SUPPORTED;
837
838         return false;
839 }
840
841 static bool neard_plugin_get_secure_element_list(
842                                 net_nfc_secure_element_info_s* list,
843                                 int* count, net_nfc_error_e* result)
844 {
845         DBG("");
846
847         if (result == NULL)
848                 return false;
849
850         *result = NET_NFC_NOT_SUPPORTED;
851
852         return false;
853 }
854
855 static bool neard_plugin_set_secure_element_mode(
856                                 net_nfc_secure_element_type_e element_type,
857                                 net_nfc_secure_element_mode_e mode,
858                                 net_nfc_error_e* result)
859 {
860         DBG("");
861
862         if (result == NULL)
863                 return false;
864
865         *result = NET_NFC_NOT_SUPPORTED;
866
867         return false;
868 }
869
870 static bool neard_plugin_connect(net_nfc_target_handle_s* handle,
871                                         net_nfc_error_e* result)
872 {
873         DBG("");
874
875         if (result == NULL)
876                 return false;
877
878         if (handle == NULL) {
879                 *result = NET_NFC_NULL_PARAMETER;
880                 return false;
881         }
882
883         if (!__neard_plugin_is_valid_target_handle(handle)) {
884                 *result = NET_NFC_INVALID_HANDLE;
885                 return false;
886         }
887
888         *result = NET_NFC_OK;
889
890         return true;
891 }
892
893 static bool neard_plugin_disconnect(net_nfc_target_handle_s* handle,
894                                         net_nfc_error_e* result)
895 {
896         DBG("");
897
898         if (result == NULL)
899                 return false;
900
901         if (handle == NULL) {
902                 *result = NET_NFC_NULL_PARAMETER;
903                 return false;
904         }
905
906         __neard_plugin_make_invalid_target_handle();
907         *result = NET_NFC_OK;
908
909         return true;
910 }
911
912 /* TODO */
913 static bool neard_plugin_check_ndef(net_nfc_target_handle_s* handle,
914                                         uint8_t *ndef_card_state,
915                                         int* max_data_size,
916                                         int* real_data_size,
917                                         net_nfc_error_e* result)
918 {
919         char *ndef = NULL;
920         int len = 0;
921
922         DBG("");
923
924         if (result == NULL)
925                 return false;
926
927         if (handle == NULL || ndef_card_state == NULL ||
928                 max_data_size == NULL || real_data_size == NULL) {
929
930                 *result = NET_NFC_NULL_PARAMETER;
931                 return false;
932         }
933
934         if (!__neard_plugin_is_valid_target_handle(handle)) {
935                 *result = NET_NFC_INVALID_HANDLE;
936                 return false;
937         }
938
939         if (tag == NULL) {
940                 *result = NET_NFC_UNKNOWN_ERROR;
941                 return false;
942         }
943
944         if (neardal_tag_get_rawNDEF(tag->name, &ndef, &len)
945                         != NEARDAL_SUCCESS) {
946                 *ndef_card_state = 0;
947                 *max_data_size   = -1;
948                 *real_data_size  = -1;
949                 *result = NET_NFC_TAG_READ_FAILED;
950         }
951
952         if (tag->readOnly)
953                 *ndef_card_state = NET_NFC_NDEF_CARD_READ_ONLY;
954         else
955                 *ndef_card_state = NET_NFC_NDEF_CARD_READ_WRITE;
956
957         /* TODO get real data */
958         switch (__neard_plugin_convert_target_type(tag->type)) {
959         case NET_NFC_JEWEL_PICC:
960                 *max_data_size = NET_NFC_JEWEL_PICC_MAX_SIZE;
961                 break;
962
963         case NET_NFC_MIFARE_ULTRA_PICC:
964                 *max_data_size = NET_NFC_MIFARE_ULTRA_PICC_MAX_SIZE;;
965                 break;
966
967         case NET_NFC_FELICA_PICC:
968                 *max_data_size = NET_NFC_FELICA_PICC_MAX_SIZE;
969                 break;
970
971         case NET_NFC_MIFARE_DESFIRE_PICC:
972                 *max_data_size = NET_NFC_MIFARE_DESFIRE_PICC_MAX_SIZE;
973                 break;
974         }
975
976         if (ndef != NULL)
977                 g_free(ndef);
978
979         *real_data_size = len;
980         *result = NET_NFC_OK;
981
982         return true;
983 }
984
985 static bool neard_plugin_check_target_presence(net_nfc_target_handle_s* handle,
986                                                 net_nfc_error_e* result)
987 {
988 //      DBG("");
989
990         if (result == NULL)
991                 return false;
992
993         if (handle == NULL) {
994                 *result = NET_NFC_NULL_PARAMETER;
995                 return false;
996         }
997
998         if (!__neard_plugin_is_valid_target_handle(handle)) {
999                 *result = NET_NFC_INVALID_HANDLE;
1000                 return false;
1001         }
1002
1003         if (tag == NULL) {
1004                 DBG(" not connected");
1005                 *result = NET_NFC_NOT_CONNECTED;
1006                 return false;
1007         }
1008
1009         *result = NET_NFC_OK;
1010
1011         return true;
1012 }
1013
1014 static bool neard_plugin_read_ndef(net_nfc_target_handle_s* handle,
1015                                         data_s** data,
1016                                         net_nfc_error_e* result)
1017 {
1018         char *buffer = NULL;
1019         int len = 0;
1020
1021         DBG("");
1022
1023         if (result == NULL)
1024                 return false;
1025
1026         if (handle == NULL || data == NULL) {
1027                 *result = NET_NFC_NULL_PARAMETER;
1028                 return false;
1029         }
1030
1031         if (!__neard_plugin_is_valid_target_handle(handle)) {
1032                 *result = NET_NFC_INVALID_HANDLE;
1033                 return false;
1034         }
1035
1036         *data = g_try_malloc0(sizeof(data_s));
1037         if (*data == NULL) {
1038                 *result = NET_NFC_ALLOC_FAIL;
1039                 return false;
1040         }
1041
1042         if (neardal_tag_get_rawNDEF(tag->name, &buffer,
1043                                 &len) != NEARDAL_SUCCESS) {
1044                 g_free(*data);
1045                 *data = NULL;
1046                 *result = NET_NFC_TAG_READ_FAILED;
1047
1048                 return false;
1049         }
1050
1051         (*data)->length = len;
1052         (*data)->buffer = g_try_malloc0((*data)->length);
1053         if ((*data)->buffer == NULL) {
1054                 g_free(buffer);
1055                 g_free(*data);
1056                 *result = NET_NFC_ALLOC_FAIL;
1057                 return false;
1058         }
1059
1060         memcpy((*data)->buffer, buffer, (*data)->length);
1061
1062         g_free(buffer);
1063
1064         *result = NET_NFC_OK;
1065
1066         return true;
1067 }
1068
1069 static bool neard_plugin_write_ndef(net_nfc_target_handle_s* handle,
1070                                         data_s* data, net_nfc_error_e* result)
1071 {
1072         neardal_record *record;
1073
1074         DBG("");
1075
1076         if (result == NULL)
1077                 return false;
1078
1079         if (handle == NULL || data == NULL) {
1080                 *result = NET_NFC_NULL_PARAMETER;
1081                 return false;
1082         }
1083
1084         if (!__neard_plugin_is_valid_target_handle(handle)) {
1085                 *result = NET_NFC_INVALID_HANDLE;
1086                 return false;
1087         }
1088
1089         if ((data->buffer == NULL && data->length != 0) ||
1090                 (data->buffer != NULL && data->length == 0)) {
1091                 *result = NET_NFC_NULL_PARAMETER;
1092                 return false;
1093         }
1094
1095         record = g_try_malloc0(sizeof(neardal_record));
1096         if (record == NULL) {
1097                 *result = NET_NFC_ALLOC_FAIL;
1098                 return false;
1099         }
1100
1101         record->name = g_strdup(tag->records[0]);
1102         record->type = g_strdup("Raw");
1103         record->rawNDEF = g_try_malloc0(data->length);
1104         if (record->rawNDEF == NULL) {
1105                 neardal_free_record(record);
1106                 *result = NET_NFC_ALLOC_FAIL;
1107                 return false;
1108         }
1109
1110         memcpy(record->rawNDEF, data->buffer, data->length);
1111         record->rawNDEFSize = data->length;
1112
1113         if (neardal_tag_write(record) != NEARDAL_SUCCESS) {
1114                 neardal_free_record(record);
1115                 *result = NET_NFC_TAG_WRITE_FAILED;
1116                 return false;
1117         }
1118
1119         neardal_free_record(record);
1120         *result = NET_NFC_OK;
1121
1122         return true;
1123 }
1124
1125 static bool neard_plugin_make_read_only_ndef(net_nfc_target_handle_s* handle,
1126                                         net_nfc_error_e* result)
1127 {
1128         DBG("");
1129
1130         if (result == NULL)
1131                 return false;
1132
1133         if (handle == NULL) {
1134                 *result = NET_NFC_NULL_PARAMETER;
1135                 return false;
1136         }
1137
1138         if (!__neard_plugin_is_valid_target_handle(handle)) {
1139                 *result = NET_NFC_INVALID_HANDLE;
1140                 return false;
1141         }
1142
1143         *result = NET_NFC_NOT_SUPPORTED;
1144
1145         return false;
1146 }
1147
1148 static bool neard_plugin_transceive(net_nfc_target_handle_s* handle,
1149                                         net_nfc_transceive_info_s* info,
1150                                         data_s** data,
1151                                         net_nfc_error_e* result)
1152 {
1153         DBG("");
1154
1155         return false;
1156 }
1157
1158 static bool neard_plugin_format_ndef(net_nfc_target_handle_s* handle,
1159                                         data_s* secure_key,
1160                                         net_nfc_error_e* result)
1161 {
1162         DBG("");
1163
1164         if (result == NULL)
1165                 return false;
1166
1167         if (handle == NULL || secure_key  == NULL) {
1168                 *result = NET_NFC_NULL_PARAMETER;
1169                 return false;
1170         }
1171
1172         if (!__neard_plugin_is_valid_target_handle(handle)) {
1173                 *result = NET_NFC_INVALID_HANDLE;
1174                 return false;
1175         }
1176
1177         *result = NET_NFC_NOT_SUPPORTED;
1178
1179         return false;
1180 }
1181
1182 static bool neard_plugin_exception_handler(void)
1183 {
1184         DBG("");
1185
1186         return true;
1187 }
1188
1189 static bool neard_plugin_is_ready(net_nfc_error_e* result)
1190 {
1191         DBG("");
1192
1193         if (result == NULL)
1194                 return false;
1195
1196         *result = NET_NFC_OK;
1197
1198         return true;
1199 }
1200
1201
1202 static bool neard_plugin_llcp_config(net_nfc_llcp_config_info_s *config,
1203                                                 net_nfc_error_e *result)
1204 {
1205         DBG("");
1206
1207         if (result == NULL)
1208                 return false;
1209
1210         if (config == NULL) {
1211                 *result = NET_NFC_NULL_PARAMETER;
1212                 return false;
1213         }
1214
1215         /* TODO : have to set these config options */
1216         DBG("set llcp config params lto=%d, miu=%d, option=0x%02x, wks=0x%02x): ",
1217                 config->lto, config->miu, config->option, config->wks);
1218
1219         *result = NET_NFC_OK;
1220
1221         return true;
1222 }
1223
1224 static bool neard_plugin_llcp_check_llcp(net_nfc_target_handle_s *handle,
1225                                                 net_nfc_error_e *result)
1226 {
1227         DBG("");
1228
1229         if (result == NULL)
1230                 return false;
1231
1232         if (handle == NULL) {
1233                 *result = NET_NFC_NULL_PARAMETER;
1234                 return false;
1235         }
1236
1237         if (!__neard_plugin_is_valid_target_handle(handle)) {
1238                 *result = NET_NFC_INVALID_HANDLE;
1239                 return false;
1240         }
1241
1242         /* TODO : how ??? (adapter->protocals == NFC_DEP)*/
1243
1244         *result = NET_NFC_OK;
1245
1246         return true;
1247 }
1248
1249 static bool neard_plugin_llcp_activate_llcp(net_nfc_target_handle_s *handle,
1250                                                 net_nfc_error_e *result)
1251 {
1252         DBG("");
1253
1254         if (result == NULL)
1255                 return false;
1256
1257         if (handle == NULL) {
1258                 *result = NET_NFC_NULL_PARAMETER;
1259                 return false;
1260         }
1261
1262         if (!__neard_plugin_is_valid_target_handle(handle)) {
1263                 *result = NET_NFC_INVALID_HANDLE;
1264                 return false;
1265         }
1266
1267         *result = NET_NFC_OK;
1268         return true;
1269 }
1270
1271 static bool neard_plugin_llcp_create_socket(net_nfc_llcp_socket_t *s,
1272                                         net_nfc_socket_type_e socketType,
1273                                         uint16_t miu, uint8_t rw,
1274                                         net_nfc_error_e* result,
1275                                         void* user_param)
1276 {
1277         int fd;
1278         struct neard_plugin_p2p_data *server;
1279
1280         DBG("");
1281
1282         if (result == NULL)
1283                 return false;
1284
1285         if (s == NULL) {
1286                 *result = NET_NFC_NULL_PARAMETER;
1287                 return false;
1288         }
1289
1290         if (socketType != NET_NFC_LLCP_SOCKET_TYPE_CONNECTIONORIENTED) {
1291                 *result = NET_NFC_NOT_SUPPORTED;
1292                 return false;
1293         }
1294
1295         fd = socket(AF_NFC, SOCK_STREAM, NFC_SOCKPROTO_LLCP);
1296         if (fd < 0) {
1297                 *result = NET_NFC_OPERATION_FAIL;
1298                 return false;
1299         }
1300
1301         /* TODO : set these config options for socket */
1302 //      DBG(" socketType : %d, miu : %d, rw : %d", socketType, miu, rw);
1303
1304         server = g_try_malloc0(sizeof(struct neard_plugin_p2p_data));
1305         if (server == NULL) {
1306                 close(fd);
1307                 *result = NET_NFC_ALLOC_FAIL;
1308                 return false;
1309         }
1310
1311         g_hash_table_insert(server_hash, GINT_TO_POINTER(fd), server);
1312
1313         *s = fd;
1314         *result = NET_NFC_OK;
1315
1316         return true;
1317 }
1318
1319 static bool neard_plugin_llcp_bind(net_nfc_llcp_socket_t socket,
1320                                         uint8_t service_access_point,
1321                                         net_nfc_error_e* result)
1322 {
1323         DBG("service_access_point : %d", service_access_point);
1324
1325         if (result == NULL)
1326                 return false;
1327
1328         /* Note: binding requires service access name, and it is
1329          * available in listen call, so binind and listen in 
1330          * listen call.
1331          */
1332         *result = NET_NFC_OK;
1333
1334         return true;
1335 }
1336
1337 static gboolean __neard_plugin_p2p_listen_cb(GIOChannel *channel,
1338                                                 GIOCondition condition,
1339                                                 gpointer user_data)
1340 {
1341         net_nfc_request_accept_socket_t *message;
1342         struct neard_plugin_p2p_data *server = user_data;
1343         struct sockaddr_nfc_llcp client_addr;
1344         socklen_t client_addr_len;
1345         int client_fd;
1346
1347         DBG("condition 0x%x", condition);
1348
1349         message = g_try_malloc0(sizeof(net_nfc_request_accept_socket_t));
1350         if (message == NULL)
1351                 return FALSE;
1352
1353         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1354                 DBG("error in listener event");
1355                 message->length = sizeof(net_nfc_request_accept_socket_t);
1356                 message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SOCKET_ERROR;
1357                 message->result = NET_NFC_OPERATION_FAIL;
1358
1359                 interface_llcp_event_cb(message, server->user_param);
1360
1361                 return FALSE;
1362         }
1363
1364         client_addr_len = sizeof(client_addr);
1365         client_fd = accept(server->socket, (struct sockaddr *) &client_addr,
1366                                                         &client_addr_len);
1367         if (client_fd < 0) {
1368                 DBG("accept failed");
1369                 message->length = sizeof(net_nfc_request_accept_socket_t);
1370                 message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SOCKET_ACCEPTED_ERROR;
1371                 message->result = NET_NFC_OPERATION_FAIL;
1372
1373                 interface_llcp_event_cb(message, server->user_param);
1374
1375                 return FALSE;
1376         }
1377
1378         server->client_list = g_list_append(server->client_list, &client_fd);
1379
1380         message->length = sizeof(net_nfc_request_accept_socket_t);
1381         message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_ACCEPT;
1382         message->handle = server->handle;
1383         message->incomming_socket = client_fd;
1384         message->trans_param = server->user_param;
1385         message->result = NET_NFC_OK;
1386
1387         interface_llcp_event_cb(message, server->user_param);
1388
1389         return TRUE;
1390 }
1391
1392 static bool neard_plugin_llcp_listen(net_nfc_target_handle_s* handle,
1393                                         uint8_t* service_access_name,
1394                                         net_nfc_llcp_socket_t socket,
1395                                         net_nfc_error_e* result,
1396                                         void* user_param)
1397 {
1398         struct sockaddr_nfc_llcp addr;
1399         struct neard_plugin_p2p_data *server;
1400         net_nfc_request_accept_socket_t *message;
1401         int err;
1402
1403         DBG("");
1404
1405         if (result == NULL)
1406                 return false;
1407
1408         if (handle == NULL || service_access_name == NULL) {
1409                 *result = NET_NFC_NULL_PARAMETER;
1410                 return false;
1411         }
1412
1413         if (!__neard_plugin_is_valid_target_handle(handle)) {
1414                 *result = NET_NFC_INVALID_HANDLE;
1415                 return false;
1416         }
1417
1418         DBG("service_access_name : %s, socket : %d",
1419                         (char *)service_access_name, socket);
1420
1421         memset(&addr, 0, sizeof(struct sockaddr_nfc_llcp));
1422         addr.sa_family = AF_NFC;
1423         addr.dev_idx = __neard_plugin_get_adapter_id(neard_adp_path);
1424         addr.nfc_protocol = NFC_PROTO_NFC_DEP;
1425         addr.service_name_len = strlen((char *)service_access_name);
1426         strcpy(addr.service_name, (char *)service_access_name);
1427
1428         /* binding */
1429         err = bind(socket, (struct sockaddr *) &addr,
1430                         sizeof(struct sockaddr_nfc_llcp));
1431         if (err < 0) {
1432                 if (errno == EADDRINUSE) {
1433                         DBG("%s is already bound", addr.service_name);
1434                         *result = NET_NFC_OPERATION_FAIL;
1435                         return false;
1436                 }
1437
1438                 DBG("%s bind failed %d", addr.service_name, err);
1439
1440                 message = g_try_malloc0(sizeof(net_nfc_request_accept_socket_t));
1441                 if (message == NULL)
1442                         return FALSE;
1443
1444                 message->length = sizeof(net_nfc_request_accept_socket_t);
1445                 message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SOCKET_ERROR;
1446                 message->result = NET_NFC_OPERATION_FAIL;
1447
1448                 interface_llcp_event_cb(message, user_param);
1449
1450                 close(socket);
1451                 *result = NET_NFC_OPERATION_FAIL;
1452                 return false;
1453         }
1454
1455         /* listening */
1456         err = listen(socket, 10);
1457         if (err < 0) {
1458                 DBG("%s listen failed %d", addr.service_name, err);
1459
1460                 message = g_try_malloc0(sizeof(net_nfc_request_accept_socket_t));
1461                 if (message == NULL)
1462                         return FALSE;
1463
1464                 message->length = sizeof(net_nfc_request_accept_socket_t);
1465                 message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SOCKET_ERROR;
1466                 message->result = NET_NFC_OPERATION_FAIL;
1467
1468                 interface_llcp_event_cb(message, user_param);
1469
1470                 close(socket);
1471                 *result = NET_NFC_OPERATION_FAIL;
1472                 return false;
1473         }
1474
1475         server = g_hash_table_lookup(server_hash, GINT_TO_POINTER(socket));
1476         if (server == NULL) {
1477                 *result = NET_NFC_UNKNOWN_ERROR;
1478                 return false;
1479         }
1480
1481         server->adapter_idx = addr.dev_idx;
1482         server->socket = socket;
1483         server->handle = handle;
1484         server->user_param = user_param;
1485
1486         server->channel = g_io_channel_unix_new(socket);
1487         g_io_channel_set_close_on_unref(server->channel, TRUE);
1488
1489         server->watch = g_io_add_watch(server->channel,
1490                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1491                                 __neard_plugin_p2p_listen_cb,
1492                                 (gpointer) server);
1493         g_io_channel_unref(server->channel);
1494
1495         *result = NET_NFC_OK;
1496         return true;
1497 }
1498
1499 static bool neard_plugin_llcp_accept(net_nfc_llcp_socket_t socket,
1500                                         net_nfc_error_e* result)
1501 {
1502         DBG("");
1503
1504         if (result == NULL)
1505                 return false;
1506
1507         *result = NET_NFC_OK;
1508         return true;
1509 }
1510
1511 static bool neard_plugin_llcp_connect_by_url(net_nfc_target_handle_s* handle,
1512                                         net_nfc_llcp_socket_t socket,
1513                                         uint8_t* service_access_name,
1514                                         net_nfc_error_e* result,
1515                                         void* user_param)
1516 {
1517         int err = 0;
1518         struct sockaddr_nfc_llcp addr;
1519         net_nfc_request_llcp_msg_t *message;
1520
1521         DBG("");
1522
1523         if (result == NULL)
1524                 return false;
1525
1526         if (handle == NULL || service_access_name == NULL) {
1527                 *result = NET_NFC_NULL_PARAMETER;
1528                 return false;
1529         }
1530
1531         if (!__neard_plugin_is_valid_target_handle(handle)) {
1532                 *result = NET_NFC_INVALID_HANDLE;
1533                 return false;
1534         }
1535
1536         memset(&addr, 0, sizeof(struct sockaddr_nfc_llcp));
1537         addr.sa_family = AF_NFC;
1538         addr.dev_idx =  __neard_plugin_get_adapter_id(neard_adp_path);
1539         addr.target_idx = handle->connection_id;
1540         addr.nfc_protocol = NFC_PROTO_NFC_DEP;
1541         addr.service_name_len = strlen((char *)service_access_name);
1542         strcpy(addr.service_name, (char *)service_access_name);
1543
1544         err = connect(socket, (struct sockaddr *) &addr,
1545                         sizeof(struct sockaddr_nfc_llcp));
1546         if (err < 0) {
1547                 DBG("Connect failed  %d", err);
1548                 *result = NET_NFC_LLCP_INVALID_SOCKET;
1549                 return false;
1550         }
1551
1552         message = g_try_malloc0(sizeof(net_nfc_request_llcp_msg_t));
1553         if (message == NULL) {
1554                 *result = NET_NFC_ALLOC_FAIL;
1555                 return false;
1556         }
1557
1558         message->length = sizeof(net_nfc_request_llcp_msg_t);
1559         message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_CONNECT;
1560         message->result = NET_NFC_OK;
1561
1562         interface_llcp_event_cb(message, user_param);
1563
1564         *result = NET_NFC_OK;
1565         return true;
1566 }
1567
1568 static bool neard_plugin_llcp_connect(net_nfc_target_handle_s* handle,
1569                                         net_nfc_llcp_socket_t socket,
1570                                         uint8_t service_access_point,
1571                                         net_nfc_error_e* result,
1572                                         void* user_param)
1573 {
1574         DBG("");
1575
1576         if (result == NULL)
1577                 return false;
1578
1579         if (handle == NULL) {
1580                 *result = NET_NFC_NULL_PARAMETER;
1581                 return false;
1582         }
1583
1584         if (!__neard_plugin_is_valid_target_handle(handle)) {
1585                 *result = NET_NFC_INVALID_HANDLE;
1586                 return false;
1587         }
1588
1589         *result = NET_NFC_NOT_SUPPORTED;
1590         return false;
1591 }
1592
1593 static bool neard_plugin_llcp_reject(net_nfc_target_handle_s* handle,
1594                                         net_nfc_llcp_socket_t  socket,
1595                                         net_nfc_error_e* result)
1596 {
1597         DBG("");
1598
1599         if (result == NULL)
1600                 return false;
1601
1602         if (handle == NULL) {
1603                 *result = NET_NFC_NULL_PARAMETER;
1604                 return false;
1605         }
1606
1607         if (!__neard_plugin_is_valid_target_handle(handle)) {
1608                 *result = NET_NFC_INVALID_HANDLE;
1609                 return false;
1610         }
1611
1612         *result = NET_NFC_NOT_SUPPORTED;
1613         return false;
1614 }
1615
1616 static bool neard_plugin_llcp_disconnect(net_nfc_target_handle_s* handle,
1617                                         net_nfc_llcp_socket_t socket,
1618                                         net_nfc_error_e* result,
1619                                         void* user_param)
1620 {
1621         DBG("");
1622
1623         if (result == NULL)
1624                 return false;
1625
1626         if (handle == NULL) {
1627                 *result = NET_NFC_NULL_PARAMETER;
1628                 return false;
1629         }
1630
1631         if (!__neard_plugin_is_valid_target_handle(handle)) {
1632                 *result = NET_NFC_INVALID_HANDLE;
1633                 return false;
1634         }
1635
1636         *result = NET_NFC_NOT_SUPPORTED;
1637         return false;
1638 }
1639
1640 static bool neard_plugin_llcp_socket_close(net_nfc_llcp_socket_t socket,
1641                                         net_nfc_error_e* result)
1642 {
1643         struct neard_plugin_p2p_data *data;
1644
1645         DBG("");
1646
1647         if (result == NULL)
1648                 return false;
1649
1650         data = g_hash_table_lookup(server_hash, GINT_TO_POINTER(socket));
1651         if (data != NULL) {
1652                 __neard_plugin_free_server(GINT_TO_POINTER(socket),
1653                                                 data, NULL);
1654                 g_hash_table_remove(server_hash, GINT_TO_POINTER(socket));
1655         }
1656
1657         *result = NET_NFC_OK;
1658
1659         return true;
1660 }
1661
1662 static bool neard_plugin_llcp_recv(net_nfc_target_handle_s* handle,
1663                                         net_nfc_llcp_socket_t socket,
1664                                         data_s* data, net_nfc_error_e* result,
1665                                         void* user_param)
1666 {
1667         net_nfc_request_llcp_msg_t *message;
1668         uint8_t buffer[128];
1669         int bytes_recv;
1670
1671         DBG("");
1672
1673         if (result == NULL)
1674                 return false;
1675
1676         if (handle == NULL || data == NULL) {
1677                 *result = NET_NFC_NULL_PARAMETER;
1678                 return false;
1679         }
1680
1681         if (!__neard_plugin_is_valid_target_handle(handle)) {
1682                 *result = NET_NFC_INVALID_HANDLE;
1683                 return false;
1684         }
1685
1686         bytes_recv = recv(socket, buffer, 128, 0);
1687         DBG("bytes read %d", bytes_recv);
1688
1689         if (bytes_recv < 0) {
1690                 DBG("Could not recv data from socket");
1691
1692                 *result = NET_NFC_LLCP_INVALID_SOCKET;
1693                 return false;
1694         }
1695
1696         if (bytes_recv == 0) {
1697                 data->length = 0;
1698                 *result = NET_NFC_OK;
1699                 return true;
1700         }
1701
1702         data->length = bytes_recv;
1703         memcpy(data->buffer, buffer, data->length);
1704
1705         message = g_try_malloc0(sizeof(net_nfc_request_llcp_msg_t));
1706         if (message == NULL) {
1707                 *result = NET_NFC_ALLOC_FAIL;
1708                 return false;
1709         }
1710
1711         message->length = sizeof(net_nfc_request_llcp_msg_t);
1712         message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_RECEIVE;
1713         message->result = NET_NFC_OK;
1714
1715         interface_llcp_event_cb(message, user_param);
1716
1717         *result = NET_NFC_OK;
1718
1719         return true;
1720 }
1721
1722 static bool neard_plugin_llcp_send(net_nfc_target_handle_s* handle,
1723                                         net_nfc_llcp_socket_t socket,
1724                                         data_s* data, net_nfc_error_e* result,
1725                                         void* user_param)
1726 {
1727         net_nfc_request_llcp_msg_t *message;
1728         int err;
1729
1730         DBG("");
1731
1732         if (result == NULL)
1733                 return false;
1734
1735         if (handle == NULL || data == NULL) {
1736                 *result = NET_NFC_NULL_PARAMETER;
1737                 return false;
1738         }
1739
1740         if (!__neard_plugin_is_valid_target_handle(handle)) {
1741                 *result = NET_NFC_INVALID_HANDLE;
1742                 return false;
1743         }
1744
1745         err = send(socket, data->buffer, data->length, 0);
1746         if (err < 0) {
1747                 DBG("sending failed %d", err);
1748                 *result = NET_NFC_P2P_SEND_FAIL;
1749                 return false;
1750         }
1751
1752         DBG("Sent %d bytes", data->length);
1753
1754         message = g_try_malloc0(sizeof(net_nfc_request_llcp_msg_t));
1755         if (message == NULL) {
1756                 *result = NET_NFC_ALLOC_FAIL;
1757                 return false;
1758         }
1759
1760         message->length = sizeof(net_nfc_request_llcp_msg_t);
1761         message->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SEND;
1762         message->result = NET_NFC_OK;
1763
1764         interface_llcp_event_cb(message, user_param);
1765
1766         *result = NET_NFC_OK;
1767         return true;
1768 }
1769
1770 static bool neard_plugin_llcp_recv_from(net_nfc_target_handle_s* handle,
1771                                         net_nfc_llcp_socket_t socket,
1772                                         data_s * data,
1773                                         net_nfc_error_e* result,
1774                                         void* user_param)
1775 {
1776         DBG("");
1777
1778         if (result == NULL)
1779                 return false;
1780
1781         if (handle == NULL || data == NULL) {
1782                 *result = NET_NFC_NULL_PARAMETER;
1783                 return false;
1784         }
1785
1786         if (!__neard_plugin_is_valid_target_handle(handle)) {
1787                 *result = NET_NFC_INVALID_HANDLE;
1788                 return false;
1789         }
1790
1791         *result = NET_NFC_NOT_SUPPORTED;
1792
1793         return false;
1794 }
1795
1796 static bool neard_plugin_llcp_send_to(net_nfc_target_handle_s* handle,
1797                                         net_nfc_llcp_socket_t socket,
1798                                         data_s* data,
1799                                         uint8_t service_access_point,
1800                                         net_nfc_error_e* result,
1801                                         void* user_param)
1802 {
1803         DBG("");
1804
1805         if (result == NULL)
1806                 return false;
1807
1808         if (handle == NULL) {
1809                 *result = NET_NFC_NULL_PARAMETER;
1810                 return false;
1811         }
1812
1813         if (!__neard_plugin_is_valid_target_handle(handle)) {
1814                 *result = NET_NFC_INVALID_HANDLE;
1815                 return false;
1816         }
1817
1818         *result = NET_NFC_NOT_SUPPORTED;
1819
1820         return false;
1821 }
1822
1823 static bool neard_plugin_llcp_get_remote_config(
1824                                         net_nfc_target_handle_s* handle,
1825                                         net_nfc_llcp_config_info_s *config,
1826                                         net_nfc_error_e* result)
1827 {
1828         DBG("");
1829
1830         if (result == NULL)
1831                 return false;
1832
1833         if (handle == NULL) {
1834                 *result = NET_NFC_NULL_PARAMETER;
1835                 return false;
1836         }
1837
1838         if (!__neard_plugin_is_valid_target_handle(handle)) {
1839                 *result = NET_NFC_INVALID_HANDLE;
1840                 return false;
1841         }
1842
1843         *result = NET_NFC_NOT_SUPPORTED;
1844
1845         return false;
1846 }
1847
1848 static bool neard_plugin_llcp_get_remote_socket_info(
1849                                         net_nfc_target_handle_s* handle,
1850                                         net_nfc_llcp_socket_t socket,
1851                                         net_nfc_llcp_socket_option_s *option,
1852                                         net_nfc_error_e* result)
1853 {
1854         DBG("");
1855
1856         if (result == NULL)
1857                 return false;
1858
1859         if (handle == NULL || option == NULL) {
1860                 *result = NET_NFC_NULL_PARAMETER;
1861                 return false;
1862         }
1863
1864         if (!__neard_plugin_is_valid_target_handle(handle)) {
1865                 *result = NET_NFC_INVALID_HANDLE;
1866                 return false;
1867         }
1868
1869         /* TODO : get real values */
1870         option->miu = 128;
1871         option->rw = 1;
1872         option->type = NET_NFC_LLCP_SOCKET_TYPE_CONNECTIONORIENTED;
1873
1874         *result = NET_NFC_OK;
1875         return true;
1876 }
1877
1878 static bool neard_plugin_eedata_register_set(net_nfc_error_e* result,
1879                                         uint32_t mode, uint32_t reg_id,
1880                                         data_s* data)
1881 {
1882         DBG("");
1883
1884         if (result == NULL)
1885                 return false;
1886
1887         if (data == NULL) {
1888                 *result = NET_NFC_NULL_PARAMETER;
1889                 return false;
1890         }
1891
1892         *result = NET_NFC_NOT_SUPPORTED;
1893         return false;
1894 }
1895
1896 NET_NFC_EXPORT_API bool onload(net_nfc_oem_interface_s* iface)
1897 {
1898         iface->init = neard_plugin_init;
1899         iface->deinit = neard_plugin_deinit;
1900         iface->register_listener = neard_plugin_register_listener;
1901         iface->unregister_listener = neard_plugin_unregister_listener;
1902         iface->support_nfc = neard_plugin_support_nfc;
1903         iface->check_firmware_version = neard_plugin_check_firmware_version;
1904         iface->update_firmeware = neard_plugin_update_firmware;
1905         iface->get_stack_information = neard_plugin_get_stack_information;
1906         iface->configure_discovery = neard_plugin_configure_discovery;
1907         iface->get_secure_element_list = neard_plugin_get_secure_element_list;
1908         iface->set_secure_element_mode = neard_plugin_set_secure_element_mode;
1909         iface->connect = neard_plugin_connect;
1910         iface->disconnect = neard_plugin_disconnect;
1911         iface->check_ndef = neard_plugin_check_ndef;
1912         iface->check_presence = neard_plugin_check_target_presence;
1913         iface->read_ndef = neard_plugin_read_ndef;
1914         iface->write_ndef = neard_plugin_write_ndef;
1915         iface->make_read_only_ndef = neard_plugin_make_read_only_ndef;
1916         iface->transceive = neard_plugin_transceive;
1917         iface->format_ndef = neard_plugin_format_ndef;
1918         iface->exception_handler = neard_plugin_exception_handler;
1919         iface->is_ready = neard_plugin_is_ready;
1920
1921         iface->config_llcp = neard_plugin_llcp_config;
1922         iface->check_llcp_status = neard_plugin_llcp_check_llcp;
1923         iface->activate_llcp = neard_plugin_llcp_activate_llcp;
1924         iface->create_llcp_socket = neard_plugin_llcp_create_socket;
1925         iface->bind_llcp_socket = neard_plugin_llcp_bind;
1926         iface->listen_llcp_socket = neard_plugin_llcp_listen;
1927         iface->accept_llcp_socket = neard_plugin_llcp_accept;
1928         iface->connect_llcp_by_url = neard_plugin_llcp_connect_by_url;
1929         iface->connect_llcp = neard_plugin_llcp_connect;
1930         iface->disconnect_llcp = neard_plugin_llcp_disconnect;
1931         iface->close_llcp_socket = neard_plugin_llcp_socket_close;
1932         iface->recv_llcp = neard_plugin_llcp_recv;
1933         iface->send_llcp = neard_plugin_llcp_send;
1934         iface->recv_from_llcp = neard_plugin_llcp_recv_from;
1935         iface->send_to_llcp = neard_plugin_llcp_send_to;
1936         iface->reject_llcp = neard_plugin_llcp_reject;
1937         iface->get_remote_config = neard_plugin_llcp_get_remote_config;
1938         iface->get_remote_socket_info = neard_plugin_llcp_get_remote_socket_info;
1939         iface->eedata_register_set = neard_plugin_eedata_register_set;
1940
1941         return true;
1942 }