Add initial source files for ASP daemon 98/79798/1
authorYu Jiung <jiung.yu@samsung.com>
Wed, 13 Jul 2016 04:31:07 +0000 (13:31 +0900)
committerYu Jiung <jiung.yu@samsung.com>
Wed, 13 Jul 2016 04:31:09 +0000 (13:31 +0900)
Change-Id: Id59fa10544efcd99bd5b0a29f0528aa0f78dc615
Signed-off-by: Yu jiung <jiung.yu@samsung.com>
33 files changed:
CMakeLists.txt [new file with mode: 0755]
LICENSE [new file with mode: 0755]
asp-manager.manifest [new file with mode: 0755]
include/asp-manager-gdbus.h [new file with mode: 0755]
include/asp-manager-util.h [new file with mode: 0755]
include/asp-manager.h [new file with mode: 0755]
include/asp-service.h [new file with mode: 0755]
include/asp-session.h [new file with mode: 0755]
include/asp-tech.h [new file with mode: 0755]
interfaces/aspmanager-iface-service.xml [new file with mode: 0755]
interfaces/aspmanager-iface-session.xml [new file with mode: 0755]
packaging/asp-manager.spec [new file with mode: 0755]
packaging/dbus-asp-manager.conf [new file with mode: 0755]
packaging/net.asp.service [new file with mode: 0755]
src/CMakeLists.txt [new file with mode: 0755]
src/asp-manager-event.c [new file with mode: 0755]
src/asp-manager-gdbus.c [new file with mode: 0755]
src/asp-manager-util.c [new file with mode: 0755]
src/asp-manager.c [new file with mode: 0755]
src/service/asp-service.c [new file with mode: 0755]
src/session/asp-session.c [new file with mode: 0755]
src/session/asp-state.c [new file with mode: 0755]
src/tech/asp-tech-ble.c [new file with mode: 0755]
src/tech/asp-tech-ble.h [new file with mode: 0755]
src/tech/asp-tech-infra.c [new file with mode: 0755]
src/tech/asp-tech-infra.h [new file with mode: 0755]
src/tech/asp-tech-nan.c [new file with mode: 0755]
src/tech/asp-tech-nan.h [new file with mode: 0755]
src/tech/asp-tech-nfc.c [new file with mode: 0755]
src/tech/asp-tech-nfc.h [new file with mode: 0755]
src/tech/asp-tech-p2p.c [new file with mode: 0755]
src/tech/asp-tech-p2p.h [new file with mode: 0755]
src/tech/asp-tech.c [new file with mode: 0755]

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..ab21b5a
--- /dev/null
@@ -0,0 +1,5 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+
+ADD_SUBDIRECTORY(src)
diff --git a/LICENSE b/LICENSE
new file mode 100755 (executable)
index 0000000..d645695
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
diff --git a/asp-manager.manifest b/asp-manager.manifest
new file mode 100755 (executable)
index 0000000..81ace0c
--- /dev/null
@@ -0,0 +1,6 @@
+<manifest>
+       <request>
+               <domain name="_"/>
+       </request>
+</manifest>
+
diff --git a/include/asp-manager-gdbus.h b/include/asp-manager-gdbus.h
new file mode 100755 (executable)
index 0000000..0d3e53d
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_MANAGER_GDBUS_H__
+#define __ASP_MANAGER_GDBUS_H__
+
+#include <glib.h>
+
+#include "asp-manager.h"
+#include "asp-manager-util.h"
+
+#define SERVICE_DBUS_ERROR_NAME "net.asp.service.Error.Failed"
+#define SESSION_DBUS_ERROR_NAME "net.asp.session.Error.Failed"
+
+#define DEBUG_PARAMS(parameters) \
+       do {\
+               gchar *parameters_debug_str = NULL;\
+               if (parameters)\
+                       parameters_debug_str = g_variant_print((GVariant *)parameters, TRUE);\
+               ASP_LOGD("signal params [%s]", parameters_debug_str ? parameters_debug_str : "NULL");\
+               g_free(parameters_debug_str);\
+       } while (0)
+
+void asp_manager_gdbus_init(void *asp_manager);
+void asp_manager_gdbus_deinit(void *asp_manager);
+void asp_manager_gdbus_notify_advertise_status(unsigned int adv_id, unsigned char status, int reason);
+
+void asp_manager_gdbus_notify_search_result(unsigned int search_id,
+                                                                               const gchar *service_mac,
+                                                                               const gchar *service_device_name,
+                                                                               unsigned int adv_id,
+                                                                               const gchar *service_type,
+                                                                               const gchar *instance_name,
+                                                                               GHashTable *service_info_map,
+                                                                               unsigned char service_status);
+
+void asp_manager_gdbus_notify_session_request(unsigned int adv_id,
+               unsigned int sessoin_id, char *session_mac, char *device_name,
+               char *session_info, int get_pin, char *pin);
+
+void asp_manager_gdbus_notify_session_config_request(unsigned int sessoin_id,
+               int get_pin, char *pin);
+
+void asp_manager_gdbus_notify_connect_status(char *session_mac,
+               unsigned int session_id, int status, char *deferred);
+
+void asp_manager_gdbus_notify_session_status(char *session_mac,
+               unsigned int session_id, int state, int status, char *info);
+
+#endif /* __ASP_MANAGER_GDBUS_H__ */
diff --git a/include/asp-manager-util.h b/include/asp-manager-util.h
new file mode 100755 (executable)
index 0000000..a7710c7
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_MANAGER_UTIL_H__
+#define __ASP_MANAGER_UTIL_H__
+
+#ifdef USE_DLOG
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "ASP_MANAGER"
+
+#define ASP_LOGD(format, args...) LOGD(format, ##args)
+#define ASP_LOGI(format, args...) LOGI(format, ##args)
+#define ASP_LOGW(format, args...) LOGW(format, ##args)
+#define ASP_LOGE(format, args...) LOGE(format, ##args)
+
+#define __ASP_LOG_FUNC_ENTER__ LOGD("Enter")
+#define __ASP_LOG_FUNC_EXIT__ LOGD("Quit")
+
+#else /* USE_DLOG */
+
+#define ASP_LOGD(format, args...)
+#define ASP_LOGI(format, args...)
+#define ASP_LOGW(format, args...)
+#define ASP_LOGE(format, args...)
+
+#define __ASP_LOG_FUNC_ENTER__
+#define __ASP_LOG_FUNC_EXIT__
+
+#endif /* USE_DLOG */
+
+#define IPSTR_MAXLEN 40
+#define MACSTR_LEN 17
+#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
+#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
+
+typedef enum {
+       ASP_ERROR_NONE,
+       ASP_ERROR_MEMORY = 0x01,
+       ASP_ERROR_INVALIED_PARAMS = 0x02,
+       ASP_ERROR_OPERATION_FAILED = 0x03,
+       ASP_ERROR_SERVICE_NOT_FOUND = 0x04,
+       ASP_ERROR_SESSION_NOT_FOUND = 0x05,
+       ASP_ERROR_NOT_SUPPORTED_TECH = 0x06,
+       ASP_ERROR_NETWORK_ROLE_REJECTED = 0x07,
+       ASP_ERROR_UNKNOWN = 0x10,
+} asp_error;
+
+int macaddr_atoe(const char *p, unsigned char mac[]);
+int asp_dbus_unpack_ay(unsigned char *dst, GVariant *src, int size);
+
+#endif /* __ASP_MANAGER_UTIL_H__ */
diff --git a/include/asp-manager.h b/include/asp-manager.h
new file mode 100755 (executable)
index 0000000..498c42d
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_MANAGER_H__
+#define __ASP_MANAGER_H__
+
+#define EXPORT_API __attribute__((visibility("default")))
+
+#define MAC_LEN 6
+#define DEVICE_NAME_LEN 32
+
+typedef struct {
+       GMainLoop *main_loop;
+
+       GList *services;
+       GList *sessions;
+
+       void *service_obj;
+       void *session_obj;
+
+       GDBusObjectManagerServer *obj_mgr;
+       GDBusConnection *connection;
+       guint gdbus_owner_id;
+
+       unsigned char p2p_local_address[MAC_LEN];
+       char p2p_device_name[DEVICE_NAME_LEN + 1];
+} asp_s;
+
+asp_s *asp_get_manager();
+
+#endif /* __ASP_MANAGER__ */
diff --git a/include/asp-service.h b/include/asp-service.h
new file mode 100755 (executable)
index 0000000..3b825f4
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 20126 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_SERVICE_H__
+#define __ASP_SERVICE_H__
+
+#include <glib.h>
+
+#include "asp-manager-util.h"
+
+#define ASP_SERVICE_MACSTR_LEN 18
+#define ASP_SERVICE_DEVICE_NAME_LEN 33
+#define ASP_SERVICE_MACADDR_LEN 6
+#define ASP_SERVICE_IPADDR_LEN 4
+#define ASP_SERVICE_DEV_NAME_LEN 32
+#define ASP_SERVICE_IFACE_NAME_LEN 16
+#define ASP_SERVICE_ADV_ID_LEN 10
+
+typedef enum {
+       ASP_SERVICE_P2P_ROLE_NONE = 0x00,  /**< Session network role none */
+       ASP_SERVICE_P2P_ROLE_NEW = 0x01,  /**< Session network role new */
+       ASP_SERVICE_P2P_ROLE_CLIENT = 0x02,  /**< Session network role client */
+       ASP_SERVICE_P2P_ROLE_GO = 0x04,  /**< Session network role GO */
+} asp_service_p2p_role_e;
+
+typedef enum {
+       ASP_SERVICE_ADV_STATUS_ADVERTISED,
+       ASP_SERVICE_ADV_STATUS_NOT_ADVERTISED,
+} asp_service_advertise_status_e;
+
+typedef enum {
+       ASP_SERVICE_ADVERTISE_REASON_SUCCESS,
+       ASP_SERVICE_ADVERTISE_REASON_DUP,
+       ASP_SERVICE_ADVERTISE_REASON_OTHER,
+} asp_service_advertise_reason_e;
+
+typedef enum {
+       ASP_SERVICE_STATUS_NOT_AVAILABLE = 0,
+       ASP_SERVICE_STATUS_AVAILABLE = 1,
+} asp_service_status_e;
+
+typedef struct {
+       unsigned int adv_id;
+       unsigned int search_id;
+       int auto_accept;
+       int discovery_tech;
+       unsigned char preferred_connection;
+
+       unsigned char status;
+       unsigned char role;
+       unsigned int config_method;
+
+       char *instance_name;
+       char *service_type;
+       GHashTable *service_info_map;
+       char *rsp_info;
+} asp_service_advertise_s;
+
+typedef struct {
+       unsigned int adv_id;
+       unsigned int search_id;
+       int auto_accept;
+       int discovery_tech;
+       unsigned char preferred_connection;
+
+       unsigned char status;
+       unsigned char role;
+       unsigned int config_method;
+
+       char *instance_name;
+       char *service_type;
+       GHashTable *service_info_map;
+       char *rsp_info;
+} asp_service_seek_s;
+
+int asp_service_init();
+int asp_service_deinit();
+int asp_service_advertise(asp_service_advertise_s *service, int replace);
+int asp_service_cancel_advertise(asp_service_advertise_s *service);
+int asp_service_seek(asp_service_seek_s *service, unsigned long long *search_id);
+int asp_service_cancel_seek(asp_service_seek_s *service);
+
+int asp_service_event_cb(void *event);
+int asp_service_notify_advertise_status(unsigned int adv_id,
+                                                                               asp_service_advertise_status_e status,
+                                                                               asp_service_advertise_reason_e reason);
+int asp_service_notify_search_result(unsigned int search_id,
+                                                                       const gchar *service_mac,
+                                                                       const gchar *service_device_name,
+                                                                       unsigned int adv_id,
+                                                                       const gchar *service_type,
+                                                                       const gchar *instance_name,
+                                                                       GHashTable *service_info,
+                                                                       asp_service_status_e service_status);
+
+#endif /* __ASP_SERVICE_H__ */
diff --git a/include/asp-session.h b/include/asp-session.h
new file mode 100755 (executable)
index 0000000..f1748c0
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_SESSION_H__
+#define __ASP_SESSION_H__
+
+
+#define ASP_SESSION_MACSTR_LEN 18
+#define ASP_SESSION_IP_LEN 4
+
+typedef enum {
+       ASP_SESSION_ROLE_REJECTED = -2,
+       ASP_SESSION_NOMORE_CONNECT = -1,
+       ASP_SESSION_NONE = 0,
+       ASP_SESSION_REQUEST_STARTED = 0x01,
+       ASP_SESSION_REQUEST_DEFERRED = 0x02,
+       ASP_SESSION_REQUEST_ACCEPTED = 0x04,
+       ASP_SESSION_REQUEST_FAILED = 0x08,
+       ASP_GROUP_FORMATION_STARTED = 0x10,
+       ASP_GROUP_FORMATION_COMPLETED = 0x20,
+       ASP_GROUP_FORMATION_FAILED = 0x40,
+} asp_session_sub_state_e;
+
+typedef enum {
+       ASP_SESSION_CLOSE_OK,
+       ASP_SESSION_CLOSE_DISASSOCIATED,
+       ASP_SESSION_CLOSE_LOCAL_CLOSE,
+       ASP_SESSION_CLOSE_REMOTE_CLOSE,
+       ASP_SESSION_CLOSE_SYSTEM_FAILURE,
+       ASP_SESSION_CLOSE_NO_RESPONSE,
+} asp_session_close_status_e;
+
+typedef enum {
+       ASP_SESSION_CLOESD,
+       ASP_SESSION_INITIATED,
+       ASP_SESSION_REQUESTED,
+       ASP_SESSION_OPEN,
+} asp_session_state_e;
+
+typedef struct {
+       /* Can be used as session argument from below */
+       unsigned int advertisement_id;
+       unsigned char service_mac[6];
+       unsigned int session_id;
+       unsigned char session_mac[6]; /* the MAC address of the P2P device that generated the session ID */
+       unsigned char local_ip[ASP_SESSION_IP_LEN];
+       unsigned char remote_ip[ASP_SESSION_IP_LEN];
+       int local_bound_port;
+       int local_bound_proto;
+
+       int state;
+
+} asp_session_s;
+
+typedef struct {
+       unsigned char service_mac[6];
+       unsigned int advertisement_id;
+       unsigned char session_mac[6];
+       unsigned int session_id;
+       int network_config;
+       int network_role;
+       char *session_information;
+} asp_connect_session_params;
+
+asp_session_s * asp_session_create(unsigned char *session_mac, int session_id, unsigned int adv_id);
+int asp_session_destroy(unsigned char *session_mac, int session_id);
+
+int asp_session_connect_session(asp_connect_session_params *params, asp_session_s **session);
+asp_session_s *asp_session_get_session(unsigned char *session_mac, int session_id);
+int asp_session_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin);
+int asp_session_close_session(unsigned char *session_mac, int session_id);
+void asp_session_set_session_ready(unsigned char *session_mac, int session_id);
+
+int asp_session_receive_request_session(void *data);
+int asp_session_receive_session_config_request(void *data);
+int asp_session_receive_connect_status(void *data);
+
+void asp_state_set(asp_session_s *session, int state, int status, char *info);
+void asp_state_set_sub(asp_session_s * session,  int status, char *deferred);
+
+#endif /* __ASP_SESSION_H__ */
diff --git a/include/asp-tech.h b/include/asp-tech.h
new file mode 100755 (executable)
index 0000000..e98616c
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 20126 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_TECH_H__
+#define __ASP_TECH_H__
+
+#define PINSTR_LEN 8
+
+typedef enum {
+       ASP_TECH_NONE,
+       ASP_TECH_P2P = 0x01,
+       ASP_TECH_BTLE,
+       ASP_TECH_NFC,
+       ASP_TECH_INFRA,
+       ASP_TECH_NAN,
+       ASP_TECH_MAX,
+} asp_tech_e;
+
+typedef enum {
+       ASP_EVENT_NONE,
+       ASP_EVENT_SEARCH_RESULT,
+       ASP_EVENT_SEARCH_TERMINATED,
+       ASP_EVENT_SESSION_REQUEST,
+       ASP_EVENT_SESSION_CONFIG_REQUEST,
+       ASP_EVENT_CONNECT_STATUS,
+       ASP_EVENT_SESSION_STATUS,
+       ASP_EVENT_PORT_STATUS,
+       ASP_EVENT_MAX,
+} asp_event_type;
+
+typedef union {
+       /**
+        *  struct search_result -
+        * Data for ASP_EVENT_SEARCH_RESULT event
+        */
+       struct search_result {
+               int tech;
+               char service_mac[MACSTR_LEN + 1];
+               unsigned int search_id;
+               unsigned int advertisement_id;
+               int service_status;
+               char *device_name;
+               char *instance_name;
+               char *service_type;
+               GHashTable *service_info;
+       } search_result;
+       /**
+        *  struct search_terminated -
+        * Data for ASP_EVENT_SEARCH_TERMINATED event
+        */
+       struct search_terminated {
+               int tech;
+               unsigned int search_id;
+               int reason;
+
+       } search_terminated;
+       /**
+        *  struct session_request -
+        * Data for ASP_EVENT_SESSION_REQUEST event
+        */
+       struct session_request {
+               int tech;
+               unsigned int advertisement_id;
+               char session_mac[MACSTR_LEN + 1];
+               unsigned int session_id;
+               int get_pin;
+               char *device_name;
+               char pin[PINSTR_LEN + 1];
+               char *session_info;
+       } session_request;
+       /**
+        *  struct session_config_request -
+        * Data for ASP_EVENT_SESSION_CONFIG_REQUEST event
+        */
+       struct session_config_request {
+               int tech;
+               unsigned int session_id;
+               int get_pin;
+               char pin[PINSTR_LEN + 1];
+       } session_config_request;
+       /**
+        *  struct connect_status -
+        * Data for ASP_EVENT_CONNECT_STATUS event
+        */
+       struct connect_status {
+               int tech;
+               char session_mac[MACSTR_LEN + 1];
+               unsigned int session_id;
+               int status;
+               char *deferred;
+       } connect_status;
+       /**
+        *  struct session_status -
+        * Data for ASP_EVENT_SESSION_STATUS event
+        */
+       struct session_status {
+               int tech;
+               char session_mac[MACSTR_LEN + 1];
+               unsigned int session_id;
+               int state;
+               int status;
+               char *info;
+       } session_status ;
+       /**
+        *  struct port_status -
+        * Data for ASP_EVENT_PORT_STATUS event
+        */
+       struct port_status {
+               int tech;
+               char session_mac[MACSTR_LEN + 1];
+               unsigned int session_id;
+               unsigned char ip[4];
+               int port;
+               int proto;
+               int status;
+
+       } port_status;
+
+} asp_event_data;
+
+typedef struct {
+       int (*init)();
+       int (*deinit)();
+       int (*advertise)(asp_service_advertise_s *service, int replace);
+       int (*cancel_advertise)(asp_service_advertise_s *service);
+       int (*seek)(asp_service_seek_s *service);
+       int (*cancel_seek)(asp_service_seek_s *service);
+       int (*connect_session)(asp_connect_session_params *params);
+       int (*confirm_session)(unsigned char *session_mac, int session_id, int confirm, const char *pin);
+       int (*destroy_connection)();
+} asp_tech_ops_s;
+
+void asp_tech_init();
+void asp_tech_deinit();
+
+int asp_tech_advertise(int tech, asp_service_advertise_s *service, int replace);
+int asp_tech_cancel_advertise(int tech, asp_service_advertise_s *service);
+int asp_tech_seek(int tech, asp_service_seek_s *service);
+int asp_tech_cancel_seek(int tech, asp_service_seek_s *service);
+int asp_tech_connect_session(int tech, asp_connect_session_params *params);
+int asp_tech_confirm_session(int tech, unsigned char *session_mac, int session_id, int confirm, const char *pin);
+int asp_tech_destroy_connection(int tech);
+
+void asp_manager_event(void *ctx, asp_event_type event, asp_event_data *data);
+
+#endif /* __ASP_TECH_H__ */
diff --git a/interfaces/aspmanager-iface-service.xml b/interfaces/aspmanager-iface-service.xml
new file mode 100755 (executable)
index 0000000..490e85e
--- /dev/null
@@ -0,0 +1,34 @@
+<node>
+       <interface name="net.asp.service">
+               <method name='AdvertiseService'>
+                       <arg type='a{sv}' name='parameters' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>
+               <method name='ServiceChangeStatus'>
+                       <arg type='a{sv}' name='parameters' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>
+               <method name='CancelAdvertiseService'>
+                       <arg type='u' name='adv_id' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>
+               <method name='SeekService'>
+                       <arg type='a{sv}' name='parameters' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+                       <arg type='t' name='search_id' direction='out'/>
+               </method>
+               <method name='CancelSeekService'>
+                       <arg type='t' name='search_id' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>"
+
+               <signal name='SearchResult'>
+                       <arg type='a{sv}' name='parameters'/>
+               </signal>
+               <signal name='AdvertiseStatusChanged'>
+                       <arg type='u' name='advertisement_id'/>
+                       <arg type='y' name='status'/>
+                       <arg type='i' name='reason'/>
+               </signal>
+       </interface>
+</node>
diff --git a/interfaces/aspmanager-iface-session.xml b/interfaces/aspmanager-iface-session.xml
new file mode 100755 (executable)
index 0000000..56611a3
--- /dev/null
@@ -0,0 +1,42 @@
+<node>
+       <interface name="net.asp.session">
+               <method name='ConnectSession'>
+                       <arg type='a{sv}' name='parameters' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+                       <arg type='s' name='session_mac' direction='out'/>
+               </method>
+               <method name='ConfirmSession'>
+                       <arg type='a{sv}' name='parameters' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>
+               <method name='GetSession'>
+                       <arg type='s' name='session_mac' direction='in'/>
+                       <arg type='u' name='session_id' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+                       <arg type='a{sv}' name='session' direction='out'/>
+               </method>
+               <method name='SetSessionReady'>
+                       <arg type='s' name='session_mac' direction='in'/>
+                       <arg type='u' name='session_id' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>
+               <method name='CloseSession'>
+                       <arg type='s' name='session_mac' direction='in'/>
+                       <arg type='u' name='session_id' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>
+
+               <signal name='SessionRequest'>
+                       <arg type='a{sv}' name='parameters'/>
+               </signal>
+               <signal name='SessionConfigRequest'>
+                       <arg type='a{sv}' name='parameters'/>
+               </signal>
+               <signal name='ConnectStatus'>
+                       <arg type='a{sv}' name='parameters'/>
+               </signal>
+               <signal name='SessionStatus'>
+                       <arg type='a{sv}' name='parameters'/>
+               </signal>
+               </interface>
+</node>
diff --git a/packaging/asp-manager.spec b/packaging/asp-manager.spec
new file mode 100755 (executable)
index 0000000..e89a1b8
--- /dev/null
@@ -0,0 +1,66 @@
+Name:       asp-manager
+Summary:    ASP(application service platform) manager
+Version:    0.0.1
+Release:    2
+Group:      Network & Connectivity/Other
+License:    Apache-2.0
+Source0:    %{name}-%{version}.tar.gz
+Source1:    dbus-asp-manager.conf
+Source2:    net.asp.service
+
+BuildRequires:  cmake
+BuildRequires:  pkgconfig(glib-2.0)
+BuildRequires:  pkgconfig(gio-2.0)
+BuildRequires:  pkgconfig(gobject-2.0)
+BuildRequires:  pkgconfig(gio-unix-2.0)
+BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(libtzplatform-config)
+BuildRequires: pkgconfig(nsd-dns-sd)
+BuildRequires: pkgconfig(vconf)
+
+BuildRequires:  python
+BuildRequires:  python-xml
+
+%description
+An application service platform manager to manage services and sessions
+
+%prep
+%setup -q
+chmod 644 %{SOURCE0}
+chmod 644 %{SOURCE1}
+chmod 644 %{SOURCE2}
+cp -a %{SOURCE1} ./asp-manager.conf
+cp -a %{SOURCE2} .
+
+%build
+
+export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE"
+export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
+export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"
+
+%cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \
+-DBIN_DIR=%{_bindir}
+
+make %{?_smp_mflags}
+
+%install
+rm -rf %{buildroot}
+
+%make_install
+
+mkdir -p %{buildroot}%{_datadir}/dbus-1/system-services/
+cp net.asp.service %{buildroot}%{_datadir}/dbus-1/system-services/
+
+#DBus DAC (net-config.manifest enables DBus SMACK)
+mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d
+cp asp-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/asp-manager.conf
+
+%files
+%manifest %{name}.manifest
+%license LICENSE
+%defattr(-,root,root,-)
+%attr(500,root,root) %{_bindir}/*
+
+#DBus DAC
+%attr(644,root,root) %{_sysconfdir}/dbus-1/system.d/*
+%attr(644,-,-) %{_datadir}/dbus-1/system-services/*.service 
diff --git a/packaging/dbus-asp-manager.conf b/packaging/dbus-asp-manager.conf
new file mode 100755 (executable)
index 0000000..2e120c7
--- /dev/null
@@ -0,0 +1,13 @@
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+        "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+        <policy user="root">
+                <allow own="net.asp"/>
+                <allow send_destination="net.asp"/>
+                <allow send_interface="net.asp"/>
+                <allow receive_sender="net.asp"/>
+        </policy>
+        <policy context="default">
+                <check send_destination="net.asp" send_interface="net.asp" privilege="http://tizen.org/privilege/asp" />
+        </policy>
+</busconfig>
diff --git a/packaging/net.asp.service b/packaging/net.asp.service
new file mode 100755 (executable)
index 0000000..338bb16
--- /dev/null
@@ -0,0 +1,4 @@
+[D-BUS Service]
+Name=net.asp
+Exec=/usr/bin/asp-manager
+User=root
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..ed5e18f
--- /dev/null
@@ -0,0 +1,57 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(asp-manager C)
+SET(PACKAGE ${PROJECT_NAME})
+SET(INTERFACES "${CMAKE_SOURCE_DIR}/interfaces")
+
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(asp_pkgs REQUIRED 
+       glib-2.0 
+       gio-2.0 
+       gio-unix-2.0
+       dlog 
+       nsd-dns-sd
+       vconf
+       )
+
+FOREACH(flag ${asp_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/interfaces)
+
+SET(SRCS
+       ${CMAKE_SOURCE_DIR}/src/asp-manager.c
+       ${CMAKE_SOURCE_DIR}/src/asp-manager-event.c
+       ${CMAKE_SOURCE_DIR}/src/asp-manager-gdbus.c
+       ${CMAKE_SOURCE_DIR}/src/asp-manager-util.c
+       )
+
+FILE(GLOB SERVICE_SRCS ${CMAKE_SOURCE_DIR}/src/service/*.c)
+FILE(GLOB SESSION_SRCS ${CMAKE_SOURCE_DIR}/src/session/*.c)
+FILE(GLOB TECH_SRCS ${CMAKE_SOURCE_DIR}/src/tech/*.c)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fpic -Wall -Werror-implicit-function-declaration -fvisibility=hidden")
+SET(ARM_CFLAGS "${ARM_CFLAGS} -mapcs -mabi=aapcs-linux -msoft-float -Uarm -fpic")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+
+ADD_DEFINITIONS("-DUSE_DLOG")
+
+ADD_CUSTOM_COMMAND(
+       WORKING_DIRECTORY
+       OUTPUT ${CMAKE_SOURCE_DIR}/src/generated-code.c
+       COMMAND gdbus-codegen --interface-prefix net.asp.
+                       --generate-c-code generated-code
+                       --c-namespace Asp
+                       --c-generate-object-manager
+                       --generate-docbook generated-code-docs
+                       ${INTERFACES}/aspmanager-iface-service.xml ${INTERFACES}/aspmanager-iface-session.xml 
+                       COMMENT "Generating GDBus .c/.h")
+
+ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS} ${SERVICE_SRCS} ${SESSION_SRCS} ${TECH_SRCS} ${CMAKE_SOURCE_DIR}/src/generated-code.c)
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${asp_pkgs_LDFLAGS} -ldl)
+
+INSTALL(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${BIN_DIR})
diff --git a/src/asp-manager-event.c b/src/asp-manager-event.c
new file mode 100755 (executable)
index 0000000..cbe5bad
--- /dev/null
@@ -0,0 +1,238 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include<glib.h>
+#include <gio/gio.h>
+
+/*****************************************************************************
+ * Application Service Platform manager headers
+ *****************************************************************************/
+#include "asp-manager.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+#include "asp-manager-util.h"
+#include "asp-manager-gdbus.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+void __asp_process_search_result(void *ctx, asp_event_data *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       if (data == NULL) {
+               ASP_LOGE("Invalid parameters");
+               return;
+       }
+
+       asp_service_notify_search_result(
+                       data->search_result.search_id,
+                       data->search_result.service_mac,
+                       data->search_result.device_name,
+                       data->search_result.advertisement_id,
+                       data->search_result.service_type,
+                       data->search_result.instance_name,
+                       data->search_result.service_info,
+                       data->search_result.service_status);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void __asp_process_search_terminated(void *ctx, asp_event_data *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void __asp_process_session_request(void *ctx, asp_event_data *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       asp_s *asp = NULL;
+       asp_session_s * local_session = NULL;
+       unsigned char session_mac[MAC_LEN] = {0,};
+
+       asp = asp_get_manager();
+       if (asp == NULL) {
+               ASP_LOGE("memory allocation for session is failed");
+               return;
+       }
+
+       if (data == NULL) {
+               ASP_LOGE("Invalid parameters");
+               return;
+       }
+
+       macaddr_atoe(data->session_request.session_mac, session_mac);
+       /* Make local p2p address as session mac */
+       local_session = asp_session_create(
+                       session_mac,
+                       data->session_request.session_id,
+                       data->session_request.advertisement_id);
+       if (local_session == NULL) {
+               ASP_LOGE("memory allocation for session is failed");
+               return;
+       }
+       /* make local p2p address as service mac */
+       memcpy(local_session->service_mac, asp->p2p_local_address, MAC_LEN);
+
+       asp_manager_gdbus_notify_session_request(
+                       data->session_request.advertisement_id,
+                       data->session_request.session_id,
+                       data->session_request.session_mac,
+                       data->session_request.device_name,
+                       data->session_request.session_info,
+                       data->session_request.get_pin,
+                       data->session_request.pin);
+
+       if (data->session_request.tech == ASP_TECH_P2P)
+               asp_state_set(local_session, ASP_SESSION_REQUESTED, 0, "PDRequest");
+       else
+               asp_state_set(local_session, ASP_SESSION_REQUESTED, 0, "REQUEST_SESSION");
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void __asp_process_session_config_request(void *ctx, asp_event_data *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       if (data == NULL) {
+               ASP_LOGE("Invalid parameters");
+               return;
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void __asp_process_connect_status(void *ctx, asp_event_data *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       if (data == NULL) {
+               ASP_LOGE("Invalid parameters");
+               return;
+       }
+
+       asp_manager_gdbus_notify_connect_status(
+                       data->connect_status.session_mac,
+                       data->connect_status.session_id,
+                       data->connect_status.status,
+                       data->connect_status.deferred);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void __asp_process_session_status(void *ctx, asp_event_data *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void __asp_process_port_status(void *ctx, asp_event_data *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+static struct {
+       const int event_id;
+       void (*function) (void *ctx, asp_event_data *data);
+} asp_event_map[] = {
+               {
+                               ASP_EVENT_NONE,
+                               NULL
+               },
+               {
+                               ASP_EVENT_SEARCH_RESULT,
+                               __asp_process_search_result,
+               },
+               {
+                               ASP_EVENT_SEARCH_TERMINATED,
+                               __asp_process_search_terminated,
+               },
+               {
+                               ASP_EVENT_SESSION_REQUEST,
+                               __asp_process_session_request,
+               },
+               {
+                               ASP_EVENT_SESSION_CONFIG_REQUEST,
+                               __asp_process_session_config_request,
+               },
+               {
+                               ASP_EVENT_CONNECT_STATUS,
+                               __asp_process_connect_status,
+               },
+               {
+                               ASP_EVENT_SESSION_STATUS,
+                               __asp_process_session_status,
+               },
+               {
+                               ASP_EVENT_PORT_STATUS,
+                               __asp_process_port_status,
+               },
+               {
+                               ASP_EVENT_MAX,
+                               NULL
+               }
+};
+
+void asp_manager_event(void *ctx, asp_event_type event, asp_event_data *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       if (event > ASP_EVENT_NONE &&
+                       event < ASP_EVENT_MAX)
+                asp_event_map[event].function(ctx, data);
+       else
+               ASP_LOGE("Invalid event");
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
diff --git a/src/asp-manager-gdbus.c b/src/asp-manager-gdbus.c
new file mode 100755 (executable)
index 0000000..e7506c5
--- /dev/null
@@ -0,0 +1,1050 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include<glib.h>
+#include <gio/gio.h>
+
+#include<dlog.h>
+
+/*****************************************************************************
+ * Application Service Platform manager headers
+ *****************************************************************************/
+#include "asp-manager.h"
+#include "asp-manager-util.h"
+#include "asp-manager-gdbus.h"
+#include "asp-service.h"
+#include "asp-session.h"
+
+#include "generated-code.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+#define ASP_DBUS_SERVICE       "net.asp"
+#define ASP_DBUS_SERVICE_ASP_PATH              "/net/asp"
+#define ASP_DBUS_SERVICE_ASP_SERVICE_PATH              "/net/asp/service"
+#define ASP_DBUS_SERVICE_ASP_SESSION_PATH              "/net/asp/session"
+
+static const char * asp_err_strs[] = {
+               "NO_ERR",
+               "MEM_ERR",
+               "INVALID_PARAM",
+               "OPER_FAILED",
+               "NO_SERVICE",
+               "NO_SESSION",
+};
+
+#define ASP_RETURN_SERVICE_ERR(invocation, err_num) g_dbus_method_invocation_return_dbus_error \
+       ((invocation), \
+                       SERVICE_DBUS_ERROR_NAME, \
+                       asp_err_strs[-(err_num)])
+
+#define ASP_RETURN_SESSION_ERR(invocation, err_num) g_dbus_method_invocation_return_dbus_error \
+               ((invocation), \
+                               SESSION_DBUS_ERROR_NAME, \
+                               asp_err_strs[-(err_num)])
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+static void __g_variant_to_g_hash_table(GVariant *variant, GHashTable* hash, bool key_only)
+{
+       GVariantIter *iter = NULL;
+       GVariant* var = NULL;
+       gchar *key = NULL;
+       gchar *value = NULL;
+
+       if (key_only) {
+               g_variant_get(variant, "as", &iter);
+               while (g_variant_iter_loop(iter, "s", &key))
+                       g_hash_table_insert(hash, key, NULL);
+       } else {
+               g_variant_get(variant, "a{sv}", &iter);
+               while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+                       value = g_variant_dup_string(var, NULL);
+                       g_hash_table_insert(hash, key, value);
+               }
+       }
+}
+
+static gboolean __asp_manager_gdbus_advertise_service(AspService *object,
+               GDBusMethodInvocation *invocation,
+               GVariant *parameters,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantIter *iter = NULL;
+       gchar *key = NULL;
+       GVariant *var = NULL;
+       asp_service_advertise_s service;
+       asp_s *asp = NULL;
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+       DEBUG_PARAMS(parameters);
+
+       memset(&service, 0, sizeof(asp_service_advertise_s));
+       service.service_info_map = NULL;
+
+       g_variant_get(parameters, "a{sv}", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+
+               if (!g_strcmp0(key, "adv_id"))
+                       g_variant_get(var, "u", &(service.adv_id));
+               else if (!g_strcmp0(key, "discovery_tech"))
+                       g_variant_get(var, "i", &(service.discovery_tech));
+               else if (!g_strcmp0(key, "preferred_connection"))
+                       g_variant_get(var, "y", &(service.preferred_connection));
+               else if (!g_strcmp0(key, "auto_accept"))
+                       g_variant_get(var, "i", &(service.auto_accept));
+               else if (!g_strcmp0(key, "status"))
+                       g_variant_get(var, "y", &(service.status));
+               else if (!g_strcmp0(key, "role"))
+                       g_variant_get(var, "y", &(service.role));
+               else if (!g_strcmp0(key, "config_method"))
+                       g_variant_get(var, "u", &(service.config_method));
+               else if (!g_strcmp0(key, "instance_name"))
+                       g_variant_get(var, "&s", &(service.instance_name));
+               else if (!g_strcmp0(key, "service_type"))
+                       g_variant_get(var, "&s", &(service.service_type));
+               else if (!g_strcmp0(key, "service_info")) {
+                       service.service_info_map = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+                       __g_variant_to_g_hash_table(var, service.service_info_map, false);
+               } else if (!g_strcmp0(key, "rsp_info"))
+                       g_variant_get(var, "&s", &(service.rsp_info));
+               else
+                       ;/* Do Nothing */
+       }
+
+       result = asp_service_advertise(&service, 0);
+       if (result < 0) {
+               ASP_LOGE("Failed to advertise service");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       asp_service_complete_advertise_service(object, invocation, result);
+
+out:
+       g_variant_iter_free(iter);
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_service_status_change(AspService *object,
+               GDBusMethodInvocation *invocation,
+               GVariant *parameters,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantIter *iter = NULL;
+       gchar *key = NULL;
+       GVariant *var = NULL;
+       asp_service_advertise_s service;
+       asp_s *asp = NULL;
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+       DEBUG_PARAMS(parameters);
+
+       memset(&service, 0, sizeof(asp_service_advertise_s));
+       g_variant_get(parameters, "a{sv}", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+
+               if (!g_strcmp0(key, "adv_id"))
+                       g_variant_get(var, "u", &(service.adv_id));
+               else if (!g_strcmp0(key, "discovery_tech"))
+                       g_variant_get(var, "i", &(service.discovery_tech));
+               else if (!g_strcmp0(key, "preferred_connection"))
+                       g_variant_get(var, "y", &(service.preferred_connection));
+               else if (!g_strcmp0(key, "auto_accept"))
+                       g_variant_get(var, "i", &(service.auto_accept));
+               else if (!g_strcmp0(key, "status"))
+                       g_variant_get(var, "y", &(service.status));
+               else if (!g_strcmp0(key, "role"))
+                       g_variant_get(var, "y", &(service.role));
+               else if (!g_strcmp0(key, "config_method"))
+                       g_variant_get(var, "u", &(service.config_method));
+               else if (!g_strcmp0(key, "instance_name"))
+                               g_variant_get(var, "&s", &(service.instance_name));
+               else if (!g_strcmp0(key, "service_type"))
+                               g_variant_get(var, "&s", &(service.service_type));
+               else if (!g_strcmp0(key, "service_info"))
+                       __g_variant_to_g_hash_table(var, service.service_info_map, false);
+               else if (!g_strcmp0(key, "rsp_info"))
+                       g_variant_get(var, "&s", &(service.rsp_info));
+               else
+                       ;/* Do Nothing */
+       }
+
+       result = asp_service_advertise(&service, 1);
+       if (result < 0) {
+               ASP_LOGE("Failed to change service status");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       asp_service_complete_advertise_service(object, invocation, result);
+
+out:
+       g_variant_iter_free(iter);
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_cancel_advertise_service(AspService *object,
+               GDBusMethodInvocation *invocation,
+               const guint adv_id,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_service_advertise_s service;
+       asp_s *asp = NULL;
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       if (adv_id == 0) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+       ASP_LOGD("advertisement ID [%u]", adv_id);
+
+       memset(&service, 0x0, sizeof(asp_service_advertise_s));
+       service.adv_id = adv_id;
+       result = asp_service_cancel_advertise(&service);
+       if (result < 0) {
+               ASP_LOGE("Failed to cancel advertise service");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       asp_service_complete_cancel_advertise_service(object, invocation, result);
+
+out:
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_seek_service(AspService *object,
+               GDBusMethodInvocation *invocation,
+               GVariant *parameters,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantIter *iter = NULL;
+       gchar *key = NULL;
+       GVariant *var = NULL;
+       asp_service_seek_s service;
+       asp_s *asp = NULL;
+       int result = 0;
+       unsigned long long search_id = -1;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+       DEBUG_PARAMS(parameters);
+
+       memset(&service, 0, sizeof(asp_service_seek_s));
+       service.service_info_map = NULL;
+
+       g_variant_get(parameters, "a{sv}", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+
+               if (!g_strcmp0(key, "discovery_tech"))
+                       g_variant_get(var, "i", &(service.discovery_tech));
+               else if (!g_strcmp0(key, "preferred_connection"))
+                       g_variant_get(var, "y", &(service.preferred_connection));
+               else if (!g_strcmp0(key, "service_type"))
+                               g_variant_get(var, "&s", &(service.service_type));
+               else if (!g_strcmp0(key, "service_info")) {
+                       service.service_info_map = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+                       __g_variant_to_g_hash_table(var, service.service_info_map, true);
+               } else
+                       ;/* Do Nothing */
+       }
+
+       result = asp_service_seek(&service, &search_id);
+       if (result < 0) {
+               ASP_LOGE("Failed to seek service");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       if (search_id < 0) {
+               ASP_LOGE("Invalid search id");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       asp_service_complete_seek_service(object, invocation, result, search_id);
+
+out:
+       g_variant_iter_free(iter);
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_cancel_seek_service(AspService *object,
+               GDBusMethodInvocation *invocation,
+               const guint64 search_id,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_service_seek_s service;
+       asp_s *asp = NULL;
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       if (search_id == 0) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+
+       memset(&service, 0x0, sizeof(asp_service_seek_s));
+       service.search_id = search_id;
+       result = asp_service_cancel_seek(&service);
+       if (result < 0) {
+               ASP_LOGE("Failed to cancel seek service");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       asp_service_complete_cancel_seek_service(object, invocation, result);
+
+out:
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_service_init(asp_s *asp)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       gboolean ret = TRUE;
+       gchar *s = NULL;
+
+       AspObjectSkeleton *object = NULL;
+       AspService *service = NULL;
+       s = g_strdup_printf(ASP_DBUS_SERVICE_ASP_SERVICE_PATH);
+
+       /* Add interface to default object path */
+       object = asp_object_skeleton_new(s);
+       g_free(s);
+
+       /* Make the newly created object export the interface
+        * net.asp.service (note
+        * that @object takes its own reference to @service).
+        */
+
+       service = asp_service_skeleton_new();
+       asp_object_skeleton_set_service(object, service);
+       g_object_unref(service);
+
+       /* Register for method callbacks as signal callbacks */
+
+       g_signal_connect(
+                       service,
+                       "handle-advertise-service",
+                       G_CALLBACK(__asp_manager_gdbus_advertise_service),
+                       asp);
+
+       g_signal_connect(
+                       service,
+                       "handle-service-change-status",
+                       G_CALLBACK(__asp_manager_gdbus_service_status_change),
+                       asp);
+
+       g_signal_connect(
+                       service,
+                       "handle-cancel-advertise-service",
+                       G_CALLBACK(__asp_manager_gdbus_cancel_advertise_service),
+                       asp);
+
+       g_signal_connect(
+                       service,
+                       "handle-seek-service",
+                       G_CALLBACK(__asp_manager_gdbus_seek_service),
+                       asp);
+
+       g_signal_connect(
+                       service,
+                       "handle-cancel-seek-service",
+                       G_CALLBACK(__asp_manager_gdbus_cancel_seek_service),
+                       asp);
+
+       /* Export the object (@manager takes its own reference to @object) */
+       g_dbus_object_manager_server_export(asp->obj_mgr, G_DBUS_OBJECT_SKELETON(object));
+       g_object_unref(object);
+
+       asp->service_obj = (void *)service;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return ret;
+}
+
+static gboolean __asp_manager_gdbus_connect_session(AspSession *object,
+               GDBusMethodInvocation *invocation,
+               GVariant *parameters,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantIter *iter = NULL;
+       gchar *key = NULL;
+       GVariant *var = NULL;
+       asp_s *asp = NULL;
+       asp_session_s *session = NULL;
+       asp_connect_session_params params;
+       const char *service_mac = NULL;
+       char session_mac[MACSTR_LEN + 1] = {0,};
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+       DEBUG_PARAMS(parameters);
+       memset(&params, 0, sizeof(asp_connect_session_params));
+
+       g_variant_get(parameters, "a{sv}", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+
+               if (!g_strcmp0(key, "service_mac"))
+                       g_variant_get(var, "&s", &service_mac);
+               else if (!g_strcmp0(key, "adv_id"))
+                       g_variant_get(var, "u", &(params.advertisement_id));
+               else if (!g_strcmp0(key, "session_id"))
+                       g_variant_get(var, "i", &(params.session_id));
+               else if (!g_strcmp0(key, "role"))
+                       g_variant_get(var, "y", &(params.network_role));
+               else if (!g_strcmp0(key, "config_method"))
+                       g_variant_get(var, "u", &(params.network_config));
+               else if (!g_strcmp0(key, "session_info"))
+                       g_variant_get(var, "&s", &(params.session_information));
+               else
+                       ;/* Do Nothing */
+       }
+
+       if (params.session_id == 0 || service_mac == NULL) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+
+       if (strlen(service_mac) != 17) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+
+
+       macaddr_atoe(service_mac, params.service_mac);
+       result = asp_session_connect_session(&params, &session);
+       if (result < 0 || session == NULL) {
+               ASP_LOGE("Operation failed");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       g_snprintf(session_mac, MACSTR_LEN, MACSTR, MAC2STR(session->session_mac));
+       asp_session_complete_connect_session(object, invocation, result, session_mac);
+
+out:
+
+       g_variant_iter_free(iter);
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_confirm_session(AspSession *object,
+               GDBusMethodInvocation *invocation,
+               GVariant *parameters,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantIter *iter = NULL;
+       gchar *key = NULL;
+       GVariant *var = NULL;
+       asp_s *asp = NULL;
+       asp_session_s *session = NULL;
+
+       const char *session_mac = NULL;
+       unsigned int session_id = 0;
+       int confirmed = 0;
+       const char *pin = NULL;
+       unsigned char mac_addr[MAC_LEN] = {0,};
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+       DEBUG_PARAMS(parameters);
+
+       g_variant_get(parameters, "(a{sv})", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+               if (!g_strcmp0(key, "session_mac"))
+                       g_variant_get(var, "&s", &session_mac);
+               else if (!g_strcmp0(key, "session_id"))
+                       g_variant_get(var, "u", &session_id);
+               else if (!g_strcmp0(key, "confirmed"))
+                       g_variant_get(var, "i", &confirmed);
+               else if (!g_strcmp0(key, "pin"))
+                       g_variant_get(var, "&s", &pin);
+               else
+                       ;/* Do Nothing */
+       }
+       if (session_id == 0 || session_mac == NULL) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+
+       if (strlen(session_mac) != 17) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+       ASP_LOGD("session ID [%u] session mac [%s]", session_id, session_mac);
+
+       macaddr_atoe(session_mac, mac_addr);
+       session = asp_session_get_session(mac_addr, session_id);
+       if (session == NULL) {
+               ASP_LOGE("Failed to get session");
+               ASP_RETURN_SESSION_ERR(invocation, -ASP_ERROR_SESSION_NOT_FOUND);
+               goto out;
+       }
+
+       asp_session_complete_confirm_session(object, invocation, result);
+
+       asp_session_confirm_session(mac_addr, session_id, confirmed, pin);
+
+out:
+       g_variant_iter_free(iter);
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_get_session(AspSession *object,
+               GDBusMethodInvocation *invocation,
+               const gchar *session_mac,
+               const guint session_id,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariant *session = NULL;
+       GVariantBuilder *builder = NULL;
+       asp_s *asp = NULL;
+       asp_session_s *local_session = NULL;
+       unsigned char mac_addr[MAC_LEN] = {0,};
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       if (session_id == 0 || session_mac == NULL) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+
+       if (strlen(session_mac) != 17) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+       ASP_LOGD("session ID [%u] session mac [%s]", session_id, session_mac);
+
+       macaddr_atoe(session_mac, mac_addr);
+       local_session = asp_session_get_session(mac_addr, session_id);
+       if (local_session == 0) {
+               ASP_LOGE("Failed to get session");
+               ASP_RETURN_SESSION_ERR(invocation, -ASP_ERROR_SESSION_NOT_FOUND);
+               goto out;
+       }
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_add(builder, "{sv}", "adv_id", g_variant_new("u", local_session->advertisement_id));
+       g_variant_builder_add(builder, "{sv}", "service_mac", g_variant_new("s", local_session->service_mac));
+       g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", local_session->session_id));
+       g_variant_builder_add(builder, "{sv}", "session_mac", g_variant_new("s", local_session->session_mac));
+       g_variant_builder_add(builder, "{sv}", "state", g_variant_new("i", local_session->state));
+
+       session = g_variant_new("(a{sv})", builder);
+       g_variant_builder_unref(builder);
+
+       asp_session_complete_get_session(object, invocation, result, session);
+out:
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_set_session_ready(AspSession *object,
+               GDBusMethodInvocation *invocation,
+               const gchar *session_mac,
+               const guint session_id,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s *asp = NULL;
+       asp_session_s *session = NULL;
+       unsigned char mac_addr[MAC_LEN] = {0,};
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       if (session_id == 0 || session_mac == NULL) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+
+       if (strlen(session_mac) != 17) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+       ASP_LOGD("session ID [%u] session mac [%s]", session_id, session_mac);
+
+       macaddr_atoe(session_mac, mac_addr);
+       session = asp_session_get_session(mac_addr, session_id);
+       if (session == NULL) {
+               ASP_LOGE("Failed to get session");
+               ASP_RETURN_SESSION_ERR(invocation, -ASP_ERROR_SESSION_NOT_FOUND);
+               goto out;
+       }
+
+       asp_session_complete_set_session_ready(object, invocation, result);
+
+       asp_session_set_session_ready(mac_addr, session_id);
+
+out:
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_close_session(AspSession *object,
+               GDBusMethodInvocation *invocation,
+               const gchar *session_mac,
+               const guint session_id,
+               const gboolean *user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s *asp = NULL;
+       asp_session_s *session = NULL;
+       unsigned char mac_addr[MAC_LEN] = {0,};
+       int result = 0;
+
+       asp = (asp_s *)user_data;
+       if (!asp) {
+               ASP_LOGE("Failed to get memory for asp structure");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_OPERATION_FAILED);
+               goto out;
+       }
+
+       if (session_id == 0 || session_mac == NULL) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+
+       if (strlen(session_mac) != 17) {
+               ASP_LOGE("Invalid parameter");
+               ASP_RETURN_SERVICE_ERR(invocation, -ASP_ERROR_INVALIED_PARAMS);
+               goto out;
+       }
+       ASP_LOGD("session ID [%u] session mac [%s]", session_id, session_mac);
+
+       macaddr_atoe(session_mac, mac_addr);
+       session = asp_session_get_session(mac_addr, session_id);
+       if (session == NULL) {
+               ASP_LOGE("Failed to get session");
+               ASP_RETURN_SESSION_ERR(invocation, -ASP_ERROR_SESSION_NOT_FOUND);
+               goto out;
+       }
+
+       asp_session_complete_close_session(object, invocation, result);
+
+       asp_session_close_session(mac_addr, session_id);
+
+out:
+       __ASP_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
+static gboolean __asp_manager_gdbus_session_init(asp_s *asp)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       gboolean ret = TRUE;
+       gchar *s = NULL;
+
+       AspObjectSkeleton *object = NULL;
+       AspSession *session = NULL;
+       s = g_strdup_printf(ASP_DBUS_SERVICE_ASP_SESSION_PATH);
+
+       /* Add interface to default object path */
+       object = asp_object_skeleton_new(s);
+       g_free(s);
+
+       /* Make the newly created object export the interface
+        * net.asp.session (note
+        * that @object takes its own reference to @session).
+        */
+
+       session = asp_session_skeleton_new();
+       asp_object_skeleton_set_session(object, session);
+       g_object_unref(session);
+
+       /* Register for method callbacks as signal callbacks */
+       g_signal_connect(
+                       session,
+                       "handle-connect-session",
+                       G_CALLBACK(__asp_manager_gdbus_connect_session),
+                       asp);
+
+       g_signal_connect(
+                       session,
+                       "handle-confirm-session",
+                       G_CALLBACK(__asp_manager_gdbus_confirm_session),
+                       asp);
+
+       g_signal_connect(
+                       session,
+                       "handle-get-session",
+                       G_CALLBACK(__asp_manager_gdbus_get_session),
+                       asp);
+
+       g_signal_connect(
+                       session,
+                       "handle-set-session-ready",
+                       G_CALLBACK(__asp_manager_gdbus_set_session_ready),
+                       asp);
+
+       g_signal_connect(
+                       session,
+                       "handle-close-session",
+                       G_CALLBACK(__asp_manager_gdbus_close_session),
+                       asp);
+
+       /* Export the object (@manager takes its own reference to @object) */
+       g_dbus_object_manager_server_export(asp->obj_mgr, G_DBUS_OBJECT_SKELETON(object));
+       g_object_unref(object);
+
+       asp->session_obj = (void *)session;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return ret;
+}
+
+static void __asp_manager_gdbus_on_bus_acquired(GDBusConnection *connection,
+               const gchar *name,
+               gpointer user_data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s* asp = (asp_s*)user_data;
+
+       asp->obj_mgr = g_dbus_object_manager_server_new("/net/asp");
+
+       ASP_LOGE("path : %s", name);
+
+       asp->connection = connection;
+
+       if (__asp_manager_gdbus_service_init(asp) == FALSE) {
+               ASP_LOGE("Can not signal connect to service");
+               /* Deinitialize and quit manager */
+       }
+       if (__asp_manager_gdbus_session_init(asp) == FALSE) {
+               ASP_LOGE("Can not signal connect to session");
+               /* Deinitialize and quit manager */
+       }
+
+       g_dbus_object_manager_server_set_connection(asp->obj_mgr, asp->connection);
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+static void __asp_manager_gdbus_on_name_acquired(GDBusConnection *connection,
+               const gchar *name,
+               gpointer user_data)
+{
+       ASP_LOGE("name : %s", name);
+}
+
+static void __asp_manager_gdbus_on_name_lost(GDBusConnection *connection,
+               const gchar *name,
+               gpointer user_data)
+{
+       ASP_LOGE("name : %s", name);
+}
+
+static GVariant *__g_hash_table_to_g_variant(GHashTable *hash)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantBuilder builder;
+
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
+
+       GHashTableIter iter;
+       gpointer key, value;
+
+       g_hash_table_iter_init(&iter, hash);
+       while (g_hash_table_iter_next(&iter, &key, &value))
+               g_variant_builder_add(&builder, "{sv}", key, g_variant_new_string(value));
+
+       __ASP_LOG_FUNC_EXIT__;
+       return g_variant_builder_end(&builder);
+}
+
+void asp_manager_gdbus_init(void *asp_manager)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s *asp = (asp_s *)asp_manager;
+       asp->gdbus_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+                       ASP_DBUS_SERVICE,
+                       G_BUS_NAME_OWNER_FLAGS_NONE,
+                       __asp_manager_gdbus_on_bus_acquired,
+                       __asp_manager_gdbus_on_name_acquired,
+                       __asp_manager_gdbus_on_name_lost,
+                       asp,
+                       NULL);
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+void asp_manager_gdbus_deinit(void *asp_manager)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s *asp = (asp_s *)asp_manager;
+       g_bus_unown_name(asp->gdbus_owner_id);
+       asp->session_obj = NULL;
+       asp->service_obj = NULL;
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+void asp_manager_gdbus_notify_advertise_status(unsigned int adv_id, unsigned char status, int reason)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       AspService *service = asp_get_manager()->service_obj;
+       asp_service_emit_advertise_status_changed(service, adv_id, status, reason);
+
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+void asp_manager_gdbus_notify_search_result(unsigned int search_id,
+                                                                               const gchar *service_mac,
+                                                                               const gchar *service_device_name,
+                                                                               unsigned int adv_id,
+                                                                               const gchar *service_type,
+                                                                               const gchar *instance_name,
+                                                                               GHashTable *service_info_map,
+                                                                               unsigned char service_status)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       AspService *service = asp_get_manager()->service_obj;
+       GVariantBuilder *builder = NULL;
+       GVariant *params = NULL;
+
+       ASP_LOGD("Search ID: %u", search_id);
+       ASP_LOGD("Service MAC: %s", service_mac);
+       ASP_LOGD("Service Dev Name: %s", service_device_name);
+       ASP_LOGD("Advertisement ID: %u", adv_id);
+       ASP_LOGD("Instance Name: %s", instance_name);
+       ASP_LOGD("Service Status: %c", service_status);
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_add(builder, "{sv}", "search_id", g_variant_new("u", search_id));
+       g_variant_builder_add(builder, "{sv}", "service_mac", g_variant_new("s", service_mac));
+       if (service_device_name != NULL)
+               g_variant_builder_add(builder, "{sv}", "service_device_name", g_variant_new("s", service_device_name));
+       g_variant_builder_add(builder, "{sv}", "adv_id", g_variant_new("u", adv_id));
+
+       g_variant_builder_add(builder, "{sv}", "service_type", g_variant_new("s", service_type));
+       if (instance_name)
+               g_variant_builder_add(builder, "{sv}", "instance_name", g_variant_new("s", instance_name));
+       if (service_info_map != NULL) {
+               g_variant_builder_add(builder, "{sv}", "service_info", __g_hash_table_to_g_variant(service_info_map));
+               g_hash_table_unref(service_info_map);
+       }
+       g_variant_builder_add(builder, "{sv}", "service_status", g_variant_new("y", service_status));
+
+       params = g_variant_new("a{sv}", builder);
+       g_variant_builder_unref(builder);
+
+       ASP_LOGD("Created parameter for search result signal");
+       asp_service_emit_search_result(service, params);
+
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+void asp_manager_gdbus_notify_session_request(unsigned int adv_id,
+               unsigned int sessoin_id, char *session_mac, char *device_name,
+               char *session_info, int get_pin, char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       AspSession *session = asp_get_manager()->session_obj;
+       GVariantBuilder *builder = NULL;
+       GVariant *params = NULL;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_add(builder, "{sv}", "adv_id", g_variant_new("u", adv_id));
+       g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", sessoin_id));
+       g_variant_builder_add(builder, "{sv}", "session_mac", g_variant_new("s", session_mac));
+       g_variant_builder_add(builder, "{sv}", "device_name", g_variant_new("s", device_name));
+       g_variant_builder_add(builder, "{sv}", "session_info", g_variant_new("s", session_info));
+       g_variant_builder_add(builder, "{sv}", "get_pin", g_variant_new("b", get_pin));
+       if (pin[0] != 0)
+               g_variant_builder_add(builder, "{sv}", "pin", g_variant_new("s", pin));
+
+       params = g_variant_new("a{sv}", builder);
+       g_variant_builder_unref(builder);
+
+       ASP_LOGD("Created parameter for session requested signal");
+       asp_session_emit_session_request(session, params);
+
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+void asp_manager_gdbus_notify_session_config_request(unsigned int sessoin_id,
+               int get_pin, char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       AspSession *session = asp_get_manager()->session_obj;
+       GVariantBuilder *builder = NULL;
+       GVariant *params = NULL;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", sessoin_id));
+       g_variant_builder_add(builder, "{sv}", "get_pin", g_variant_new("b", get_pin));
+       if (pin[0] != 0)
+               g_variant_builder_add(builder, "{sv}", "pin", g_variant_new("s", pin));
+
+       params = g_variant_new("a{sv}", builder);
+       g_variant_builder_unref(builder);
+
+       ASP_LOGD("Created parameter for session config requested signal");
+       asp_session_emit_session_config_request(session, params);
+
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+void asp_manager_gdbus_notify_connect_status(char *session_mac,
+               unsigned int session_id, int status, char *deferred)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       AspSession *session = asp_get_manager()->session_obj;
+       GVariantBuilder *builder = NULL;
+       GVariant *params = NULL;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_add(builder, "{sv}", "session_mac", g_variant_new("s", session_mac));
+       g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", session_id));
+       g_variant_builder_add(builder, "{sv}", "status", g_variant_new("i", status));
+       g_variant_builder_add(builder, "{sv}", "deferred_resp", g_variant_new("s", deferred));
+
+       params = g_variant_new("a{sv}", builder);
+       g_variant_builder_unref(builder);
+
+       ASP_LOGD("Created parameter for connect status signal");
+       asp_session_emit_connect_status(session, params);
+
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+void asp_manager_gdbus_notify_session_status(char *session_mac,
+               unsigned int session_id, int state, int status, char *info)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       AspSession *session = asp_get_manager()->session_obj;
+       GVariantBuilder *builder = NULL;
+       GVariant *params = NULL;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_add(builder, "{sv}", "session_mac", g_variant_new("s", session_mac));
+       g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", session_id));
+       g_variant_builder_add(builder, "{sv}", "state", g_variant_new("i", state));
+       g_variant_builder_add(builder, "{sv}", "status", g_variant_new("i", status));
+       if (info)
+               g_variant_builder_add(builder, "{sv}", "requested_info", g_variant_new("s", info));
+
+       params = g_variant_new("a{sv}", builder);
+       g_variant_builder_unref(builder);
+
+       ASP_LOGD("Created parameter for session status signal");
+       asp_session_emit_session_status(session, params);
+
+       __ASP_LOG_FUNC_EXIT__;
+}
diff --git a/src/asp-manager-util.c b/src/asp-manager-util.c
new file mode 100755 (executable)
index 0000000..2296b52
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include<glib.h>
+#include <gio/gio.h>
+
+#include<dlog.h>
+
+/*****************************************************************************
+ * Application Service Platform manager headers
+ *****************************************************************************/
+#include "asp-manager.h"
+#include "asp-manager-util.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+int macaddr_atoe(const char *p, unsigned char mac[])
+{
+       int i = 0;
+
+       for (;;) {
+               mac[i++] = (char) strtoul(p, (char **) &p, 16);
+               if (!*p++ || i == 6)
+                       break;
+       }
+
+       return (i == 6);
+}
+
+int asp_dbus_unpack_ay(unsigned char *dst, GVariant *src, int size)
+{
+       GVariantIter *iter = NULL;
+       int length = 0;
+       int rv = 1;
+
+       if (!dst || !src || size == 0) {
+               ASP_LOGE("Invalid parameter");
+               return -1;
+       }
+       g_variant_get(src, "ay", &iter);
+       if (iter == NULL) {
+               ASP_LOGE("failed to get iterator");
+               return -1;
+       }
+
+       while (g_variant_iter_loop(iter, "y", &dst[length])) {
+               length++;
+               if (length >= size)
+                       break;
+       }
+       g_variant_iter_free(iter);
+
+       if (length < size) {
+               ASP_LOGE("array is shorter than size");
+               rv = -1;
+       }
+
+       return rv;
+}
diff --git a/src/asp-manager.c b/src/asp-manager.c
new file mode 100755 (executable)
index 0000000..58053ed
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include<glib.h>
+#include <gio/gio.h>
+
+/*****************************************************************************
+ * Application Service Platform manager headers
+ *****************************************************************************/
+#include "asp-manager.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+#include "asp-manager-gdbus.h"
+#include "asp-manager-util.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+static asp_s *g_asp = NULL;
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+asp_s *asp_get_manager()
+{
+       return g_asp;
+}
+
+static void __asp_manager_deinit()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int res = 0;
+
+       if (!g_asp) {
+               printf("Memory for manager structure is not allocated");
+               return;
+       }
+
+       asp_tech_deinit();
+
+       res = asp_service_deinit();
+       if (res < 0)
+               ASP_LOGE("Failed to deinitialize service");
+
+       asp_manager_gdbus_deinit((void *)g_asp);
+
+       g_free(g_asp);
+       g_asp = NULL;
+       __ASP_LOG_FUNC_EXIT__;
+}
+
+static asp_s *__asp_manager_init()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s *asp = NULL;
+       int res = 0;
+
+       asp = (asp_s*) g_try_malloc0(sizeof(asp_s));
+       if (!asp) {
+               ASP_LOGE("Failed to allocate memory for manager structure");
+               return NULL;
+       }
+       ASP_LOGD("ASP: manager alloc");
+       g_asp = asp;
+
+       asp_tech_init();
+       /* TODO : Handle tech failure */
+
+       res = asp_service_init();
+       if (res < 0) {
+               ASP_LOGE("Failed to initialize service");
+               g_free(asp);
+               return NULL;
+       }
+
+       asp_manager_gdbus_init((void *)asp);
+
+       ASP_LOGD("asp manager initialized");
+       __ASP_LOG_FUNC_EXIT__;
+       return asp;
+}
+
+int main(int argc, char *argv[])
+{
+       GMainLoop *main_loop = NULL;
+       int ret = -1;
+
+       /* Initialize required subsystems */
+#if !GLIB_CHECK_VERSION(2, 35, 0)
+       g_type_init();
+#endif
+
+       g_asp = __asp_manager_init();
+       if (!g_asp)
+               goto fail;
+
+       /* Crate the GLIB main loop */
+       main_loop = g_main_loop_new(NULL, FALSE);
+       g_asp->main_loop = main_loop;
+
+       /* Run the main loop */
+       g_main_loop_run(main_loop);
+
+       ret = 0;
+
+fail:
+
+       __asp_manager_deinit();
+
+       g_main_loop_unref(main_loop);
+
+       return ret;
+}
diff --git a/src/service/asp-service.c b/src/service/asp-service.c
new file mode 100755 (executable)
index 0000000..2f1bdd7
--- /dev/null
@@ -0,0 +1,193 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include<dlog.h>
+
+/*****************************************************************************
+ * Application Service Platform Daemon headers
+ *****************************************************************************/
+#include "asp-manager-util.h"
+#include "asp-manager-gdbus.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+
+#include "../generated-code.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/* TODO : define service data structure */
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+/* TODO : define local memory for service data structure */
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+unsigned long long asp_service_search_id;
+
+int asp_service_init()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+       asp_service_search_id = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_service_deinit()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_service_advertise(asp_service_advertise_s *service, int replace)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int discovery_tech = service->discovery_tech;
+       int result = 0;
+
+       ASP_LOGD("Discovery tech %x", discovery_tech);
+       result = asp_tech_advertise(discovery_tech, service, replace);
+       if (result < 0) {
+               /* TODO */
+               /* error handling */
+
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_service_cancel_advertise(asp_service_advertise_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int discovery_tech = service->discovery_tech;
+       int result = 0;
+
+       ASP_LOGD("Discovery tech %x", discovery_tech);
+       result = asp_tech_cancel_advertise(discovery_tech, service);
+       if (result < 0) {
+               /* TODO */
+               /* error handling */
+
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_service_seek(asp_service_seek_s *service, unsigned long long *search_id)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int discovery_tech = service->discovery_tech;
+       int result = 0;
+
+       ASP_LOGD("Discovery tech %x", discovery_tech);
+       result = asp_tech_seek(discovery_tech, service);
+       if (result < 0) {
+               /* TODO */
+               /* error handling */
+
+       }
+
+       *search_id = asp_service_search_id++;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_service_cancel_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int discovery_tech = service->discovery_tech;
+       int result = 0;
+
+       ASP_LOGD("Discovery tech %x", discovery_tech);
+       result = asp_tech_cancel_seek(discovery_tech, service);
+       if (result < 0) {
+               /* TODO */
+               /* error handling */
+
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_service_notify_advertise_status(unsigned int adv_id,
+                                                                               asp_service_advertise_status_e status,
+                                                                               asp_service_advertise_reason_e reason)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       asp_manager_gdbus_notify_advertise_status(adv_id, (unsigned char)status, (int)reason);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_service_notify_search_result(unsigned int search_id,
+                                                                       const gchar *service_mac,
+                                                                       const gchar *service_device_name,
+                                                                       unsigned int adv_id,
+                                                                       const gchar *service_type,
+                                                                       const gchar *instance_name,
+                                                                       GHashTable *service_info_map,
+                                                                       asp_service_status_e service_status)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       asp_manager_gdbus_notify_search_result(search_id,
+                                                                                       service_mac,
+                                                                                       service_device_name,
+                                                                                       adv_id,
+                                                                                       service_type,
+                                                                                       instance_name,
+                                                                                       service_info_map,
+                                                                                       (unsigned char)service_status);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
diff --git a/src/session/asp-session.c b/src/session/asp-session.c
new file mode 100755 (executable)
index 0000000..9aff5c8
--- /dev/null
@@ -0,0 +1,313 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include<glib.h>
+#include <gio/gio.h>
+
+#include<dlog.h>
+
+/*****************************************************************************
+ * Application Service Platform Daemon headers
+ *****************************************************************************/
+#include "asp-manager.h"
+#include "asp-manager-util.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-manager-gdbus.h"
+#include "asp-tech.h"
+
+#include "../generated-code.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/* TODO : define session data structure */
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+GList *g_sessions = NULL;
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+/* Allocate memory for session and initialized it */
+asp_session_s * asp_session_create(unsigned char *session_mac, int session_id, unsigned int adv_id)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_session_s *session = NULL;
+
+       session = (asp_session_s *)g_try_malloc0(sizeof(asp_session_s));
+       if (session == NULL) {
+               ASP_LOGE("memory allocation for session is failed");
+               return NULL;
+       }
+
+       memcpy(session->service_mac, session_mac, MAC_LEN);
+       session->session_id = session_id;
+       session->advertisement_id = adv_id;
+       session->state = ASP_SESSION_CLOESD;
+
+       g_sessions = g_list_append(g_sessions, session);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return session;
+}
+
+/* Deallocate memory for session */
+int asp_session_destroy(unsigned char *session_mac, int session_id)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_session_s *session = NULL;
+       int res = 0;
+
+       session = asp_session_get_session(session_mac, session_id);
+       if (session == NULL) {
+               ASP_LOGE("There's no session");
+               return -1;
+       }
+
+       g_sessions = g_list_remove(g_sessions, session);
+       g_free(session);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return res;
+}
+
+int asp_session_connect_session(asp_connect_session_params *params, asp_session_s **session)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s *asp = NULL;
+       asp_session_s * local_session = NULL;
+       int tech = 0;
+       int res = 0;
+
+       asp = asp_get_manager();
+       if (asp == NULL) {
+               ASP_LOGE("memory allocation for session is failed");
+               return -1;
+       }
+
+       /* Make local p2p address as session mac */
+       local_session = asp_session_create(asp->p2p_local_address,
+                       params->session_id, params->advertisement_id);
+       if (local_session == NULL) {
+               ASP_LOGE("memory allocation for session is failed");
+               return ASP_ERROR_MEMORY;
+       }
+
+       /* TODO : request connection on proper technology */
+       tech = ASP_TECH_P2P;
+       memcpy(params->session_mac, local_session->session_mac, MAC_LEN);
+       res = asp_tech_connect_session(tech, params);
+       if (res < 0) {
+               ASP_LOGE("request connect is failed");
+               asp_session_destroy(local_session->session_mac, local_session->session_id);
+               return -1;
+       }
+       *session = local_session;
+       asp_state_set(local_session, ASP_SESSION_INITIATED, 0, NULL);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return res;
+}
+
+int asp_session_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_session_s *session = NULL;
+       int res = 0;
+
+       session = asp_session_get_session(session_mac, session_id);
+       if (session == NULL) {
+               ASP_LOGE("There's no session");
+               return -1;
+       }
+       res = asp_tech_confirm_session(ASP_TECH_P2P, session_mac,
+                       session_id, confirm, pin);
+       if (res < 0) {
+               ASP_LOGE("confirm session is failed");
+               asp_state_set(session, ASP_SESSION_CLOESD, ASP_SESSION_CLOSE_SYSTEM_FAILURE, NULL);
+               asp_session_destroy(session_mac, session_id);
+               return -1;
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return res;
+}
+
+asp_session_s *asp_session_get_session(unsigned char *session_mac, int session_id)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GList *list;
+       asp_session_s *session = NULL;
+
+       for (list = g_sessions; list != NULL; list = list->next) {
+               session = list->data;
+               if (session != NULL) {
+                       if (session->session_id == session_id &&
+                                       memcmp(session->session_mac, session_mac, MAC_LEN)) {
+                               break;
+                       }
+               }
+               session = NULL;
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return session;
+}
+
+void asp_session_set_session_ready(unsigned char *session_mac, int session_id)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+int asp_session_close_session(unsigned char *session_mac, int session_id)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_session_s *session = NULL;
+       int res = 0;
+
+       session = asp_session_get_session(session_mac, session_id);
+       if (session == NULL) {
+               ASP_LOGE("There's no session");
+               return -1;
+       }
+
+       asp_state_set(session, ASP_SESSION_CLOESD, ASP_SESSION_CLOSE_LOCAL_CLOSE, NULL);
+       asp_session_destroy(session->session_mac, session->session_id);
+       __ASP_LOG_FUNC_EXIT__;
+       return res;
+}
+
+int asp_session_receive_request_session(void *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s *asp = NULL;
+       asp_session_s *session = NULL;
+       asp_event_data *event = data;
+       unsigned char session_mac[MAC_LEN] = {0,};
+       int res = 0;
+
+       asp = asp_get_manager();
+       if (asp == NULL) {
+               ASP_LOGE("memory allocation for session is failed");
+               return -1;
+       }
+
+       macaddr_atoe(event->session_request.session_mac, session_mac);
+       session = asp_session_create(session_mac, event->session_request.session_id,
+                       event->session_request.advertisement_id);
+       if (session == NULL) {
+               ASP_LOGE("memory allocation for session is failed");
+               return -1;
+       }
+
+       /* make local p2p address as service mac */
+       memcpy(session->service_mac, asp->p2p_local_address, MAC_LEN);
+
+       asp_manager_gdbus_notify_session_request(event->session_request.advertisement_id,
+                       event->session_request.session_id, event->session_request.session_mac,
+                       event->session_request.device_name, event->session_request.session_info,
+                       event->session_request.get_pin, event->session_request.pin);
+
+       asp_state_set(session, ASP_SESSION_REQUESTED, 0, "PDRequest");
+
+       __ASP_LOG_FUNC_EXIT__;
+       return res;
+}
+
+/* SessionConfigRequest event is triggered by the ASP on a Service Seeker */
+int asp_session_receive_session_config_request(void *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_s *asp = NULL;
+       asp_session_s *session = NULL;
+       asp_event_data *event = data;
+       int res = 0;
+
+       asp = asp_get_manager();
+       if (asp == NULL) {
+               ASP_LOGE("memory allocation for session is failed");
+               return -1;
+       }
+
+       /* session config request only received on seeker */
+       session = asp_session_get_session(asp->p2p_local_address, event->session_config_request.session_id);
+       if (session == NULL) {
+               ASP_LOGE("There's no session");
+               return -1;
+       }
+
+       asp_manager_gdbus_notify_session_config_request(
+                       event->session_config_request.session_id,
+                       event->session_config_request.get_pin,
+                       event->session_config_request.pin);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return res;
+}
+
+int asp_session_receive_connect_status(void *data)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       asp_session_s *session = NULL;
+       asp_event_data *event = data;
+       unsigned char session_mac[MAC_LEN] = {0,};
+       int res = 0;
+
+       macaddr_atoe(event->session_request.session_mac, session_mac);
+       session = asp_session_get_session(session_mac,
+                       event->connect_status.session_id);
+       if (session == NULL) {
+               ASP_LOGE("There's no session");
+               return -1;
+       }
+
+       /* set session status */
+       asp_state_set_sub(session, event->connect_status.status, event->connect_status.deferred);
+
+       switch (event->connect_status.status) {
+       case ASP_SESSION_ROLE_REJECTED:
+       case ASP_SESSION_NOMORE_CONNECT:
+       case ASP_SESSION_REQUEST_FAILED:
+       case ASP_GROUP_FORMATION_FAILED:
+               asp_state_set(session, ASP_SESSION_CLOESD, ASP_SESSION_CLOSE_LOCAL_CLOSE, NULL);
+               asp_session_destroy(session->session_mac, session->session_id);
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return res;
+}
diff --git a/src/session/asp-state.c b/src/session/asp-state.c
new file mode 100755 (executable)
index 0000000..ceaa88a
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ *      Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+
+/*****************************************************************************
+ *      System headers
+ *****************************************************************************/
+
+/*****************************************************************************
+ *      Application Service Platform headers
+ *****************************************************************************/
+
+#include "asp-manager-util.h"
+#include "asp-manager.h"
+#include "asp-manager-gdbus.h"
+#include "asp-session.h"
+
+/*****************************************************************************
+ *      Macros and Typedefs
+ *****************************************************************************/
+
+#define STATE_MASK 0x00ff
+#define SUB_STATE_BIT 8
+#define ASP_GET_SUB_STATE(val) ((val) >> SUB_STATE_BIT)
+
+#define SUB_STATE_MASK 0xff00
+#define ASP_SET_SUB_STATE(sub_state, val) (val) = ((val) & STATE_MASK) | ((sub_state) << SUB_STATE_BIT)
+
+/*****************************************************************************
+ *      Global Variables
+ *****************************************************************************/
+
+/*****************************************************************************
+ *      Local Functions Definition
+ *****************************************************************************/
+
+void asp_state_set(asp_session_s *session, int state, int status, char *info)
+{
+       char mac_address[MACSTR_LEN + 1] = {0,};
+       if (session == NULL) {
+               ASP_LOGE("Invalid parameter");
+               return;
+       }
+
+       switch (state) {
+       case ASP_SESSION_CLOESD:
+       {
+               ASP_LOGI("ASP_SESSION_CLOESD");
+       }
+               break;
+       case ASP_SESSION_INITIATED:
+       {
+               ASP_LOGI("ASP_SESSION_INITIATED");
+       }
+               break;
+       case ASP_SESSION_REQUESTED:
+       {
+               ASP_LOGI("ASP_SESSION_REQUESTED");
+       }
+               break;
+       case ASP_SESSION_OPEN:
+       {
+               ASP_LOGI("ASP_SESSION_OPEN");
+       }
+               break;
+       default:
+       {
+               ASP_LOGE("Invalid Session State");
+               return;
+       }
+               break;
+       }
+       session->state = state;
+       g_snprintf(mac_address, MACSTR_LEN, MACSTR, MAC2STR(session->session_mac));
+       asp_manager_gdbus_notify_session_status(mac_address,
+                       session->session_id, state, status, info);
+       return;
+}
+
+void asp_state_set_sub(asp_session_s * session,  int status, char *deferred)
+{
+       char mac_address[MACSTR_LEN + 1] = {0,};
+       switch (status) {
+       case ASP_SESSION_ROLE_REJECTED:
+       {
+               ASP_LOGI("ASP_SESSION_ROLE_REJECTED");
+       }
+               break;
+       case ASP_SESSION_NOMORE_CONNECT:
+       {
+               ASP_LOGI("ASP_SESSION_NOMORE_CONNECT");
+       }
+               break;
+       case ASP_SESSION_REQUEST_STARTED:
+       {
+               ASP_LOGI("ASP_SESSION_REQUEST_STARTED");
+       }
+               break;
+       case ASP_SESSION_REQUEST_DEFERRED:
+       {
+               ASP_LOGI("ASP_SESSION_REQUEST_DEFERRED");
+       }
+               break;
+       case ASP_SESSION_REQUEST_ACCEPTED:
+       {
+               ASP_LOGI("ASP_SESSION_REQUEST_ACCEPTED");
+       }
+               break;
+       case ASP_SESSION_REQUEST_FAILED:
+       {
+               ASP_LOGI("ASP_SESSION_REQUEST_FAILED");
+       }
+               break;
+       case ASP_GROUP_FORMATION_STARTED:
+       {
+               ASP_LOGI("ASP_GROUP_FORMATION_STARTED");
+       }
+               break;
+       case ASP_GROUP_FORMATION_COMPLETED:
+       {
+               ASP_LOGI("ASP_GROUP_FORMATION_COMPLETED");
+       }
+               break;
+       case ASP_GROUP_FORMATION_FAILED:
+       {
+               ASP_LOGI("ASP_GROUP_FORMATION_FAILED");
+       }
+               break;
+       default:
+       {
+               ASP_LOGE("Invalid Session State");
+               return;
+       }
+               break;
+       }
+
+       ASP_SET_SUB_STATE(status, session->state);
+       g_snprintf(mac_address, MACSTR_LEN, MACSTR, MAC2STR(session->session_mac));
+       asp_manager_gdbus_notify_connect_status(mac_address,
+                       session->session_id, status, deferred);
+       return;
+}
diff --git a/src/tech/asp-tech-ble.c b/src/tech/asp-tech-ble.c
new file mode 100755 (executable)
index 0000000..e6141b1
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include <glib.h>
+#include<dlog.h>
+
+/*****************************************************************************
+ * Application Service Platform Daemon headers
+ *****************************************************************************/
+#include "asp-manager-util.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+#include "asp-tech-ble.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/* TODO : define service data structure */
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+/* TODO : define local memory for service data structure */
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+int asp_tech_ble_init()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_ble_deinit()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_ble_advertise(asp_service_advertise_s *service, int replace)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_ble_cancel_advertise(asp_service_advertise_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_ble_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_ble_cancel_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_ble_connect_session(asp_connect_session_params *params)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_ble_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_ble_destroy_connection()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+asp_tech_ops_s asp_tech_ble_ops = {
+               .init = asp_tech_ble_init,
+               .deinit = asp_tech_ble_deinit,
+               .advertise = asp_tech_ble_advertise,
+               .cancel_advertise = asp_tech_ble_cancel_advertise,
+               .seek = asp_tech_ble_seek,
+               .cancel_seek = asp_tech_ble_cancel_seek,
+               .connect_session = asp_tech_ble_connect_session,
+               .confirm_session = asp_tech_ble_confirm_session,
+               .destroy_connection = asp_tech_ble_destroy_connection,
+};
diff --git a/src/tech/asp-tech-ble.h b/src/tech/asp-tech-ble.h
new file mode 100755 (executable)
index 0000000..5a77e44
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_TECH_BLE_H__
+#define __ASP_TECH_BLE_H__
+
+#endif /* __ASP_TECH_BLE_H__ */
diff --git a/src/tech/asp-tech-infra.c b/src/tech/asp-tech-infra.c
new file mode 100755 (executable)
index 0000000..3ee8a5d
--- /dev/null
@@ -0,0 +1,528 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include <glib.h>
+#include <dlog.h>
+
+#include <vconf.h>
+#include <vconf-keys.h>
+
+#include <dns-sd.h>
+
+/*****************************************************************************
+ * Application Service Platform Daemon headers
+ *****************************************************************************/
+#include "asp-manager-util.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+#include "asp-tech-infra.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/* TODO : define service data structure */
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+static __thread GHashTable *g_local_service_hash = NULL;
+static __thread GHashTable *g_browser_hash = NULL;
+
+
+/* TODO : define local memory for service data structure */
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+static void _advertise_status_cb(dnssd_error_e result,
+               dnssd_service_h local_service, void *user_data)
+{
+       unsigned int adv_id = GPOINTER_TO_UINT(user_data);
+       asp_service_advertise_status_e status = 0;
+       asp_service_advertise_reason_e reason = 0;
+
+       switch (result) {
+       case DNSSD_ERROR_NONE:
+       case DNSSD_ERROR_ALREADY_REGISTERED:
+               status = ASP_SERVICE_ADV_STATUS_ADVERTISED;
+               reason = ASP_SERVICE_ADVERTISE_REASON_SUCCESS;
+               break;
+       case DNSSD_ERROR_NAME_CONFLICT:
+               status = ASP_SERVICE_ADV_STATUS_NOT_ADVERTISED;
+               reason = ASP_SERVICE_ADVERTISE_REASON_DUP;
+               break;
+       default:
+               status = ASP_SERVICE_ADV_STATUS_NOT_ADVERTISED;
+               reason = ASP_SERVICE_ADVERTISE_REASON_OTHER;
+       }
+
+       asp_service_notify_advertise_status(adv_id, status, reason);
+}
+
+static void __txt_record_to_g_hash(unsigned short txt_len, const char *txt_records,
+               GHashTable *service_info_map)
+{
+       const gchar *ptr = txt_records;
+       const gchar *max = txt_records + txt_len;
+
+       ASP_LOGD("Parsing TXT record. Length: %hu", txt_len);
+
+       while (ptr < max) {
+               unsigned short len = ptr[0];
+               char *buf = g_strndup(ptr + 1, len);
+               ASP_LOGD("buf: %s", buf);
+               char *key;
+               char *value;
+               char *save_str = NULL;
+               key = strtok_r(buf, "=", &save_str);
+               value = strtok_r(NULL, "=", &save_str);
+               g_hash_table_replace(service_info_map, g_strdup(key), g_strdup(value));
+               ASP_LOGD("Insert (%s, %s) to hash. len: %d)", key, value, len);
+               g_free(buf);
+               ptr = ptr + 1 + len;
+       }
+}
+
+static void _search_result_cb(dnssd_service_state_e service_state,
+               dnssd_service_h remote_service, void *user_data)
+{
+       asp_event_data event;
+       unsigned int search_id = -1;
+       char *service_mac = NULL;
+       const gchar *service_device_name = NULL;
+       unsigned int adv_id = -1;
+       char *service_name = NULL;
+       GHashTable *service_info_map = NULL;
+       asp_service_status_e service_status;
+       unsigned short txt_len = 0;
+       char *txt = NULL;
+       const gchar *adv_id_str = NULL;
+
+       ASP_LOGD("A service is found %u", remote_service);
+
+       search_id = GPOINTER_TO_UINT(user_data);
+       dnssd_service_get_name(remote_service, &service_name);
+       ASP_LOGD("Service name: %s", service_name);
+
+       switch (service_state) {
+       case DNSSD_SERVICE_STATE_AVAILABLE:
+               service_status = ASP_SERVICE_STATUS_AVAILABLE;
+               break;
+       case DNSSD_SERVICE_STATE_UNAVAILABLE:
+       case DNSSD_SERVICE_STATE_NAME_LOOKUP_FAILED:
+       case DNSSD_SERVICE_STATE_HOST_NAME_LOOKUP_FAILED:
+       case DNSSD_SERVICE_STATE_ADDRESS_LOOKUP_FAILED:
+               service_status = ASP_SERVICE_STATUS_NOT_AVAILABLE;
+               break;
+       default:
+               service_status = ASP_SERVICE_STATUS_NOT_AVAILABLE;
+       }
+       ASP_LOGD("Service status: %s",
+                       service_status == ASP_SERVICE_STATUS_AVAILABLE ? "Available" : "Unavailable");
+
+       dnssd_service_get_all_txt_record(remote_service, &txt_len, (void *)&txt);
+       if (txt_len > 1) {
+               service_info_map = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+               __txt_record_to_g_hash(txt_len, txt, service_info_map);
+               if (g_hash_table_contains(service_info_map, ASP_INFRA_ADV_KEY)) {
+                       adv_id_str = g_hash_table_lookup(service_info_map, ASP_INFRA_ADV_KEY);
+                       adv_id = g_ascii_strtoll(adv_id_str, NULL, 10);
+                       ASP_LOGD("Advertisement ID: %u", adv_id);
+               } else {
+                       ASP_LOGE("TXT records don't contain advertisement ID");
+                       return;
+               }
+
+               if (g_hash_table_contains(service_info_map, ASP_INFRA_MAC_ADDR_KEY)) {
+                       service_mac = g_strdup(g_hash_table_lookup(service_info_map, ASP_INFRA_MAC_ADDR_KEY));
+                       ASP_LOGD("Advertisement MAC Address: %s\n", service_mac);
+               } else {
+                       ASP_LOGE("TXT records don't contain advertisement MAC");
+                       return;
+               }
+
+               if (g_hash_table_contains(service_info_map, ASP_INFRA_DEV_NAME)) {
+                       service_device_name = g_strdup(g_hash_table_lookup(service_info_map, ASP_INFRA_DEV_NAME));
+                       ASP_LOGD("Service device name: %s\n", service_device_name);
+               }
+       } else {
+               ASP_LOGE("Found service should has at least one TXT record");
+               return;
+       }
+
+       memset(&event, 0x0, sizeof(asp_event_data));
+       event.search_result.tech = ASP_TECH_INFRA;
+       event.search_result.search_id = search_id;
+       event.search_result.advertisement_id = adv_id;
+       event.search_result.service_status = service_status;
+       g_strlcpy(event.search_result.service_mac, service_mac, MACSTR_LEN);
+       event.search_result.device_name = NULL;
+       event.search_result.instance_name = service_name;
+       event.search_result.service_info = service_info_map;
+       asp_manager_event(NULL, ASP_EVENT_SEARCH_RESULT, &event);
+
+       if (service_mac)
+               g_free(service_mac);
+}
+
+static int __asp_service_infra_convert_error_type(int dnssd_error)
+{
+       switch (dnssd_error) {
+       case DNSSD_ERROR_NONE:
+               return ASP_ERROR_NONE;
+       case DNSSD_ERROR_OUT_OF_MEMORY:
+               return ASP_ERROR_MEMORY;
+       case DNSSD_ERROR_INVALID_PARAMETER:
+               return ASP_ERROR_INVALIED_PARAMS;
+       case DNSSD_ERROR_NOT_SUPPORTED:
+               return ASP_ERROR_NOT_SUPPORTED_TECH;
+       case DNSSD_ERROR_SERVICE_NOT_FOUND:
+               return ASP_ERROR_SERVICE_NOT_FOUND;
+       case DNSSD_ERROR_INVALID_OPERATION:
+       case DNSSD_ERROR_NOT_INITIALIZED:
+       case DNSSD_ERROR_ALREADY_REGISTERED:
+       case DNSSD_ERROR_NAME_CONFLICT:
+       case DNSSD_ERROR_SERVICE_NOT_RUNNING:
+       case DNSSD_ERROR_OPERATION_FAILED:
+               return ASP_ERROR_OPERATION_FAILED;
+       default:
+               return ASP_ERROR_UNKNOWN;
+       }
+       return dnssd_error;
+}
+
+static void __init_hash_tables()
+{
+       g_local_service_hash = g_hash_table_new(g_int_hash, g_int_equal);
+       g_browser_hash = g_hash_table_new(g_int_hash, g_int_equal);
+}
+
+static void __g_hash_free_handle(gpointer key, gpointer value, gpointer user_data)
+{
+}
+
+static void __deinit_hash_tables()
+{
+       g_hash_table_foreach(g_local_service_hash, (GHFunc)__g_hash_free_handle, NULL);
+       g_hash_table_foreach(g_browser_hash, (GHFunc)__g_hash_free_handle, NULL);
+
+       g_local_service_hash = NULL;
+       g_browser_hash = NULL;
+}
+
+static int __add_txt_record(dnssd_service_h handle, const char* key, const char* value, int len)
+{
+       int result = 0;
+       unsigned short txt_length;
+       void *txt_data;
+
+       result = dnssd_service_add_txt_record(handle, key, len, value);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Add (%s:%s) TXT record. length: %d", key, value, len);
+
+       result = dnssd_service_get_all_txt_record(handle, &txt_length, &txt_data);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Get all TXT records");
+
+       result = dnssd_service_set_record(handle, 16, txt_length, txt_data);
+       if (result != DNSSD_ERROR_NONE) {
+               ASP_LOGE("Failed to add record");
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Add TXT records");
+       return result;
+}
+
+int asp_tech_infra_init()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       result = dnssd_initialize();
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+
+       __init_hash_tables();
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_infra_deinit()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       result = dnssd_deinitialize();
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+
+       __deinit_hash_tables();
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_infra_advertise(asp_service_advertise_s *service, int replace)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+       dnssd_service_h service_handle = 0;
+       char adv_id[ASP_SERVICE_ADV_ID_LEN + 1];
+       int adv_id_len = 0;
+       char *mac_addr;
+       int mac_addr_len;
+       int port = 1234; /* TODO */
+
+       ASP_LOGD("Infrastructure advertise service. replace: %d", replace);
+
+       if (replace) {
+               /*  TODO */
+               ;
+       }
+
+       result = dnssd_create_local_service(service->service_type, &service_handle);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Success to create local service handle: %u, service type: %s",
+                       service_handle, service->service_type);
+
+       result = dnssd_service_set_name(service_handle, service->instance_name);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Success to set service name %s", service->instance_name);
+
+       /*  TODO set port */
+       result = dnssd_service_set_port(service_handle, port);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Success to set port %d", port);
+
+       result = dnssd_register_local_service(service_handle,
+                       _advertise_status_cb, GUINT_TO_POINTER(service->adv_id));
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Success to register local service %u", service_handle);
+
+       g_hash_table_insert(g_local_service_hash,
+                       &service->adv_id, &service_handle);
+
+       ASP_LOGD("Insert (%u, %u) to g_local_service_hash (size: %d)",
+                       service->adv_id, service_handle,
+                       g_hash_table_size(g_local_service_hash));
+
+
+       /* Add TXT records */
+       /* First of all, add Advertisement ID */
+       snprintf(adv_id, ASP_SERVICE_ADV_ID_LEN + 1, "%u", service->adv_id);
+       adv_id_len = strlen(adv_id);
+       result = __add_txt_record(service_handle, ASP_INFRA_ADV_KEY, adv_id, adv_id_len);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+
+       /* Add MAC Address to TXT record */
+       mac_addr = (char *)g_try_malloc0(ASP_SERVICE_MACSTR_LEN + 1);
+       if (mac_addr == NULL) {
+               ASP_LOGE("Failed to malloc");
+               return ASP_ERROR_MEMORY;
+       }
+       mac_addr = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
+       mac_addr_len = strlen(mac_addr);
+       result = __add_txt_record(service_handle, ASP_INFRA_MAC_ADDR_KEY, mac_addr, mac_addr_len);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       g_free(mac_addr);
+
+       /* Other TXT records */
+       if (service->service_info_map != NULL) {
+               GHashTableIter iter;
+               gpointer key, value;
+
+               g_hash_table_iter_init(&iter, service->service_info_map);
+               while (g_hash_table_iter_next(&iter, &key, &value)) {
+                       unsigned short len = (unsigned short)strlen(value);
+                       result = __add_txt_record(service_handle, key, value, len);
+               }
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_infra_cancel_advertise(asp_service_advertise_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+       dnssd_service_h service_handle;
+
+       service_handle = *(dnssd_service_h*)g_hash_table_lookup(g_local_service_hash,
+                                                                                                               &service->adv_id);
+
+       if (!service_handle) {
+               /* TODO */
+               ASP_LOGE("Failed to lookup advertisement id %u", service->adv_id);
+               return result;
+       }
+
+       result = dnssd_deregister_local_service(service_handle);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Success to deregister local service: %u", service_handle);
+
+       result = dnssd_destroy_local_service(service_handle);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Success to destroy local service: %u", service_handle);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_infra_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+       dnssd_service_h browser_handle;
+
+       result = dnssd_start_browsing_service(service->service_type,
+                                                                                       &browser_handle,
+                                                                                       _search_result_cb,
+                                                                                       GUINT_TO_POINTER(service->search_id));
+
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Success to start browsing service. service_type: %s, browser_handle: %u",
+                       service->service_type, browser_handle);
+
+       g_hash_table_insert(g_browser_hash,
+                       &service->search_id, &browser_handle);
+
+       ASP_LOGD("Insert (%u, %u) to g_browser_hash (size: %d)",
+                       service->search_id, browser_handle,
+                       g_hash_table_size(g_browser_hash));
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_infra_cancel_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+       dnssd_service_h browser_handle;
+
+       browser_handle = *(dnssd_service_h*)g_hash_table_lookup(g_browser_hash,
+                                                                                                               &service->search_id);
+
+       if (!browser_handle) {
+               /* TODO */
+               ASP_LOGE("Failed to lookup search id %u", service->search_id);
+               return result;
+       }
+
+       result = dnssd_stop_browsing_service(browser_handle);
+       if (result != DNSSD_ERROR_NONE) {
+               result = __asp_service_infra_convert_error_type(result);
+               return result;
+       }
+       ASP_LOGD("Success to stop browsing. browser_handle: %u", browser_handle);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_infra_connect_session(asp_connect_session_params *params)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_infra_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_infra_destroy_connection()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+asp_tech_ops_s asp_tech_infra_ops = {
+               .init = asp_tech_infra_init,
+               .deinit = asp_tech_infra_deinit,
+               .advertise = asp_tech_infra_advertise,
+               .cancel_advertise = asp_tech_infra_cancel_advertise,
+               .seek = asp_tech_infra_seek,
+               .cancel_seek = asp_tech_infra_cancel_seek,
+               .connect_session = asp_tech_infra_connect_session,
+               .confirm_session = asp_tech_infra_confirm_session,
+               .destroy_connection = asp_tech_infra_destroy_connection,
+};
diff --git a/src/tech/asp-tech-infra.h b/src/tech/asp-tech-infra.h
new file mode 100755 (executable)
index 0000000..d15a2dc
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_TECH_INFRA_H__
+#define __ASP_TECH_INFRA_H__
+
+#define ASP_INFRA_ADV_KEY "wfa_asp_adv_id"
+#define ASP_INFRA_MAC_ADDR_KEY "wfa_asp_mac_addr"
+#define ASP_INFRA_DEV_NAME "wfa_asp_dev_name"
+
+#endif /* __ASP_TECH_INFRA_H__ */
diff --git a/src/tech/asp-tech-nan.c b/src/tech/asp-tech-nan.c
new file mode 100755 (executable)
index 0000000..49344e5
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include <glib.h>
+#include<dlog.h>
+
+/*****************************************************************************
+ * Application Service Platform Daemon headers
+ *****************************************************************************/
+#include "asp-manager-util.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+#include "asp-tech-nan.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/* TODO : define service data structure */
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+/* TODO : define local memory for service data structure */
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+int asp_tech_nan_init()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nan_deinit()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nan_advertise(asp_service_advertise_s *service, int replace)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nan_cancel_advertise(asp_service_advertise_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nan_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nan_cancel_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nan_connect_session(asp_connect_session_params *params)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nan_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nan_destroy_connection()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+asp_tech_ops_s asp_tech_nan_ops = {
+               .init = asp_tech_nan_init,
+               .deinit = asp_tech_nan_deinit,
+               .advertise = asp_tech_nan_advertise,
+               .cancel_advertise = asp_tech_nan_cancel_advertise,
+               .seek = asp_tech_nan_seek,
+               .cancel_seek = asp_tech_nan_cancel_seek,
+               .connect_session = asp_tech_nan_connect_session,
+               .confirm_session = asp_tech_nan_confirm_session,
+               .destroy_connection = asp_tech_nan_destroy_connection,
+};
diff --git a/src/tech/asp-tech-nan.h b/src/tech/asp-tech-nan.h
new file mode 100755 (executable)
index 0000000..ea06a36
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_TECH_NAN_H__
+#define __ASP_TECH_NAN_H__
+
+#endif /* __ASP_TECH_NAN_H__ */
diff --git a/src/tech/asp-tech-nfc.c b/src/tech/asp-tech-nfc.c
new file mode 100755 (executable)
index 0000000..dca58d5
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include <glib.h>
+#include<dlog.h>
+
+/*****************************************************************************
+ * Application Service Platform Daemon headers
+ *****************************************************************************/
+#include "asp-manager-util.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+#include "asp-tech-nfc.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/* TODO : define service data structure */
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+/* TODO : define local memory for service data structure */
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+int asp_tech_nfc_advertise(asp_service_advertise_s *service, int replace)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nfc_cancel_advertise(asp_service_advertise_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nfc_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nfc_cancel_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nfc_init()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nfc_deinit()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nfc_connect_session(asp_connect_session_params *params)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nfc_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_nfc_destroy_connection()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+asp_tech_ops_s asp_tech_nfc_ops = {
+               .init = asp_tech_nfc_init,
+               .deinit = asp_tech_nfc_deinit,
+               .advertise = asp_tech_nfc_advertise,
+               .cancel_advertise = asp_tech_nfc_cancel_advertise,
+               .seek = asp_tech_nfc_seek,
+               .cancel_seek = asp_tech_nfc_cancel_seek,
+               .connect_session = asp_tech_nfc_connect_session,
+               .confirm_session = asp_tech_nfc_confirm_session,
+               .destroy_connection = asp_tech_nfc_destroy_connection,
+};
diff --git a/src/tech/asp-tech-nfc.h b/src/tech/asp-tech-nfc.h
new file mode 100755 (executable)
index 0000000..a4e2192
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_TECH_NFC_H__
+#define __ASP_TECH_NFC_H__
+
+#endif /* __ASP_TECH_NFC_H__ */
diff --git a/src/tech/asp-tech-p2p.c b/src/tech/asp-tech-p2p.c
new file mode 100755 (executable)
index 0000000..979bc35
--- /dev/null
@@ -0,0 +1,991 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include <glib.h>
+#include <dlog.h>
+#include <gio/gio.h>
+
+/*****************************************************************************
+ * Application Service Platform Daemon headers
+ *****************************************************************************/
+#include "asp-manager.h"
+#include "asp-manager-util.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+#include "asp-tech-p2p.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+#define ASP_TECH_P2P_ROLE_NEW 1
+#define ASP_TECH_P2P_ROLE_GC 2
+#define ASP_TECH_P2P_ROLE_GO 4
+
+#define WFD_MANAGER_SERVICE                     "net.wifidirect"
+#define WFD_MANAGER_PATH                        "/net/wifidirect"
+#define WFD_MANAGER_MANAGE_INTERFACE            WFD_MANAGER_SERVICE
+#define WFD_MANAGER_CONFIG_INTERFACE            WFD_MANAGER_SERVICE ".config"
+#define WFD_MANAGER_GROUP_INTERFACE             WFD_MANAGER_SERVICE ".group"
+#define WFD_MANAGER_ASP_INTERFACE                        WFD_MANAGER_SERVICE ".asp"
+
+#define DBUS_REPLY_TIMEOUT_SYNC     10 * 1000
+#define DBUS_OBJECT_PATH_MAX                    150
+
+#define WFD_MANAGER_QUARK (g_quark_from_string("wifi-direct-manager"))
+
+#define asp_tech_p2p_dbus_method_call_sync(interface_name, method, params, error) \
+       __asp_tech_p2p_dbus_method_call_sync_debug(interface_name, method, params, error, __func__)
+
+#define DBUS_DEBUG_VARIANT(parameters) \
+       do {\
+               gchar *parameters_debug_str = NULL;\
+               if (parameters)\
+                       parameters_debug_str = g_variant_print(parameters, TRUE);\
+               ASP_LOGD("signal params [%s]", parameters_debug_str ? parameters_debug_str : "NULL");\
+               g_free(parameters_debug_str);\
+       } while (0)
+
+void asp_tech_p2p_process_activation(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters);
+
+void asp_tech_p2p_process_deactivation(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters);
+
+void asp_tech_p2p_process_search_result(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters);
+
+void asp_tech_p2p_process_session_request(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters);
+
+void asp_tech_p2p_process_session_config_request(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters);
+
+void asp_tech_p2p_process_connect_status(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters);
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+static GDBusConnection *g_wfd_gdbus_conn;
+static guint g_wfd_signal_id;
+
+static struct {
+       const char *interface;
+       const char *member;
+       void (*function) (GDBusConnection *connection,
+                         const gchar *object_path,
+                         GVariant *parameters);
+} asp_tech_p2p_dbus_signal_map[] = {
+               {
+                               WFD_MANAGER_MANAGE_INTERFACE,
+                               "Activation",
+                               asp_tech_p2p_process_activation
+               },
+               {
+                               WFD_MANAGER_MANAGE_INTERFACE,
+                               "Deactivation",
+                               asp_tech_p2p_process_deactivation
+               },
+               {
+                               WFD_MANAGER_ASP_INTERFACE,
+                               "SearchResult",
+                               asp_tech_p2p_process_search_result
+               },
+               {
+                               WFD_MANAGER_ASP_INTERFACE,
+                               "SessionRequest",
+                               asp_tech_p2p_process_session_request
+               },
+               {
+                               WFD_MANAGER_ASP_INTERFACE,
+                               "SessionConfigRequest",
+                               asp_tech_p2p_process_session_config_request
+               },
+               {
+                               WFD_MANAGER_ASP_INTERFACE,
+                               "ConnectStatus",
+                               asp_tech_p2p_process_connect_status
+               },
+               {
+                               NULL,
+                               NULL,
+                               NULL
+               }
+};
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+static int __net_wifidirect_gerror_to_enum(GError* error)
+{
+       int res = 0;
+       if (error == NULL) {
+               ASP_LOGI("GError is NULL!!");
+               return res;
+       }
+
+       ASP_LOGE("wifi_direct_dbus_method_call_sync() failed. error [%d: %s]",
+                       error->code, error->message);
+
+       if (NULL == strstr(error->message, "net.wifidirect.Error")) {
+               if (NULL != strstr(error->message, ".AccessDenied")) {
+                       ASP_LOGE("Client doesn't have wifidirect privilege");
+                       res = -1;
+               } else {
+                       ASP_LOGE("DBus failure");
+                       res = -1;
+               }
+       } else {
+               if (NULL != strstr(error->message, "InvalidParameter"))
+                       res = -1;
+               else if (NULL != strstr(error->message, "NotPermitted"))
+                       res = -1;
+               else if (NULL != strstr(error->message, "OperationFailed"))
+                       res = -1;
+               else if (NULL != strstr(error->message, "TooManyClient"))
+                       res = -1;
+               else
+                       res = -1;
+       }
+       return res;
+}
+
+
+static void __asp_tech_p2p_dbus_signal_cb(GDBusConnection *connection,
+               const gchar *sender, const gchar *object_path,
+               const gchar *interface, const gchar *signal,
+               GVariant *parameters, gpointer user_data)
+{
+       int i = 0;
+
+       ASP_LOGD("Signal Name [%s]", signal);
+       DBUS_DEBUG_VARIANT(parameters);
+
+       for (i = 0; asp_tech_p2p_dbus_signal_map[i].member != NULL; i++) {
+               if (!g_strcmp0(signal, asp_tech_p2p_dbus_signal_map[i].member) &&
+                   !g_strcmp0(interface, asp_tech_p2p_dbus_signal_map[i].interface) &&
+                   asp_tech_p2p_dbus_signal_map[i].function != NULL) {
+                       asp_tech_p2p_dbus_signal_map[i].function(connection, object_path, parameters);
+                       break;
+               }
+       }
+}
+
+static GVariant *__g_hash_table_to_g_variant(GHashTable *hash)
+{
+       GVariantBuilder builder;
+
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
+
+       GHashTableIter iter;
+       gpointer key, value;
+
+       g_hash_table_iter_init(&iter, hash);
+       while (g_hash_table_iter_next(&iter, &key, &value))
+               g_variant_builder_add(&builder, "{sv}", key, g_variant_new_string(value));
+
+       return g_variant_builder_end(&builder);
+}
+
+static GVariant *__g_hash_keys_to_g_variant(GHashTable *hash)
+{
+       GVariantBuilder builder;
+
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("(as)"));
+
+       GHashTableIter iter;
+       gpointer key, value;
+
+       g_hash_table_iter_init(&iter, hash);
+       while (g_hash_table_iter_next(&iter, &key, &value))
+               g_variant_builder_add(&builder, "s", key);
+
+       return g_variant_builder_end(&builder);
+}
+
+GVariant *__asp_tech_p2p_dbus_method_call_sync_debug(const char* interface_name,
+                                                 const char* method,
+                                                 GVariant *params,
+                                                 GError **error,
+                                                 const char *calling_func)
+{
+       GVariant *reply = NULL;
+
+       if (g_wfd_gdbus_conn == NULL) {
+               ASP_LOGE("GDBusconnection is NULL");
+               return reply;
+       }
+
+       ASP_LOGD("[%s][%s.%s]", calling_func, interface_name, method);
+       DBUS_DEBUG_VARIANT(params);
+
+       reply = g_dbus_connection_call_sync(g_wfd_gdbus_conn,
+                                           WFD_MANAGER_SERVICE, /* bus name */
+                                           WFD_MANAGER_PATH, /* object path */
+                                           interface_name, /* interface name */
+                                           method, /* method name */
+                                           params, /* GVariant *params */
+                                           NULL, /* reply_type */
+                                           G_DBUS_CALL_FLAGS_NONE, /* flags */
+                                           DBUS_REPLY_TIMEOUT_SYNC, /* timeout */
+                                           NULL, /* cancellable */
+                                           error); /* error */
+       DBUS_DEBUG_VARIANT(reply);
+       return reply;
+}
+
+void asp_tech_p2p_process_activation(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       ASP_LOGE("Wi-Fi Direct activated");
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void asp_tech_p2p_process_deactivation(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       ASP_LOGE("Wi-Fi Direct deactivated");
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void asp_tech_p2p_process_search_result(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       GVariantIter *iter = NULL;
+       GVariant *var = NULL;
+       asp_event_data event;
+       unsigned char service_mac[MAC_LEN] = {0,};
+       const char *key = NULL;
+       const char *str = NULL;
+       long long unsigned search_id = 0;
+       char *device_name = NULL;
+       unsigned int advertisement_id = 0;
+       unsigned int config_method = 0;
+       char *service_type = NULL;
+       char *instance_name = NULL;
+       char *service_info = NULL;
+       unsigned char status = 0;
+
+       if (!parameters) {
+               __ASP_LOG_FUNC_EXIT__;
+               return;
+       }
+
+       g_variant_get(parameters, "(a{sv})", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+               if (!g_strcmp0(key, "search_id")) {
+                       g_variant_get(var, "t", &search_id);
+               } else if (!g_strcmp0(key, "service_mac")) {
+                       if (asp_dbus_unpack_ay(service_mac, var, MAC_LEN) < 0)
+                               return;
+               } else if (!g_strcmp0(key, "device_name")) {
+                       g_variant_get(var, "&s", &str);
+                       device_name = g_strndup(str, ASP_SERVICE_DEVICE_NAME_LEN);
+               }  else if (!g_strcmp0(key, "advertisement_id")) {
+                       g_variant_get(var, "u", &advertisement_id);
+               } else if (!g_strcmp0(key, "config_method")) {
+                       g_variant_get(var, "u", &config_method);
+               } else if (!g_strcmp0(key, "service_type")) {
+                       g_variant_get(var, "&s", &str);
+                       service_type = g_strdup(str);
+               } else if (!g_strcmp0(key, "service_type")) {
+                       g_variant_get(var, "&s", &str);
+                       service_type = g_strdup(str);
+               }  else if (!g_strcmp0(key, "instance_name")) {
+                       g_variant_get(var, "&s", &str);
+                       instance_name = g_strdup(str);
+               } else if (!g_strcmp0(key, "status")) {
+                       g_variant_get(var, "y", &status);
+               }  else {
+                               ;/* Do Nothing */
+               }
+       }
+
+       memset(&event, 0x0, sizeof(asp_event_data));
+       event.search_result.tech = ASP_TECH_P2P;
+       event.search_result.search_id = search_id;
+       event.search_result.advertisement_id = advertisement_id;
+       event.search_result.service_status = status;
+       g_snprintf(event.search_result.service_mac, MACSTR_LEN,
+                       MACSTR, MAC2STR(service_mac));
+       event.search_result.device_name = device_name;
+       event.search_result.service_type = service_type;
+       event.search_result.instance_name = instance_name;
+       event.search_result.service_info = NULL;
+       asp_manager_event(NULL, ASP_EVENT_SEARCH_RESULT, &event);
+
+       g_free(device_name);
+       g_free(service_type);
+       g_free(instance_name);
+       g_free(service_info);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void asp_tech_p2p_process_session_request(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       GVariantIter *iter = NULL;
+       GVariant *var = NULL;
+       asp_event_data event;
+       unsigned char session_mac[MAC_LEN] = {0,};
+       const char *key = NULL;
+       const char *str = NULL;
+       unsigned int advertisement_id = 0;
+       unsigned int session_id = 0;
+       gboolean get_pin = FALSE;
+       const char *pin = NULL;
+       char *device_name = NULL;
+       char *session_info = NULL;
+
+       if (!parameters) {
+               __ASP_LOG_FUNC_EXIT__;
+               return;
+       }
+
+       g_variant_get(parameters, "(a{sv})", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+               if (!g_strcmp0(key, "adv_id")) {
+                       g_variant_get(var, "u", &advertisement_id);
+               } else if (!g_strcmp0(key, "session_mac")) {
+                       if (asp_dbus_unpack_ay(session_mac, var, MAC_LEN) < 0)
+                               return;
+               }  else if (!g_strcmp0(key, "session_id")) {
+                       g_variant_get(var, "u", &session_id);
+               }  else if (!g_strcmp0(key, "device_name")) {
+                       g_variant_get(var, "&s", &str);
+                       device_name = g_strndup(str, ASP_SERVICE_DEVICE_NAME_LEN);
+               } else if (!g_strcmp0(key, "get_pin")) {
+                       g_variant_get(var, "b", &get_pin);
+               } else if (!g_strcmp0(key, "pin")) {
+                       g_variant_get(var, "&s", &pin);
+               } else if (!g_strcmp0(key, "session_info")) {
+                       g_variant_get(var, "&s", &str);
+                       session_info = g_strdup(str);
+               } else {
+                               ;/* Do Nothing */
+               }
+       }
+
+       memset(&event, 0x0, sizeof(asp_event_data));
+       event.session_request.tech = ASP_TECH_P2P;
+       event.session_request.advertisement_id = advertisement_id;
+       g_snprintf(event.session_request.session_mac, MACSTR_LEN, MACSTR,
+                       MAC2STR(session_mac));
+       event.session_request.session_id = session_id;
+       event.session_request.device_name = device_name;
+       event.session_request.get_pin = get_pin;
+       if (pin)
+               g_strlcpy(event.session_request.pin, pin, PINSTR_LEN);
+       event.session_request.session_info = session_info;
+       asp_manager_event(NULL, ASP_EVENT_SESSION_REQUEST, &event);
+
+       g_free(device_name);
+       g_free(session_info);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void asp_tech_p2p_process_session_config_request(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       GVariantIter *iter = NULL;
+       GVariant *var = NULL;
+       asp_event_data event;
+       const char *key = NULL;
+       unsigned int session_id = 0;
+       gboolean get_pin = FALSE;
+       const char *pin = NULL;
+
+       if (!parameters) {
+               __ASP_LOG_FUNC_EXIT__;
+               return;
+       }
+
+       g_variant_get(parameters, "(a{sv})", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+               if (!g_strcmp0(key, "session_id"))
+                       g_variant_get(var, "u", &session_id);
+               else if (!g_strcmp0(key, "get_pin"))
+                       g_variant_get(var, "b", &get_pin);
+               else if (!g_strcmp0(key, "pin"))
+                       g_variant_get(var, "&s", &pin);
+               else
+                       ;/* Do Nothing */
+       }
+
+       memset(&event, 0x0, sizeof(asp_event_data));
+       event.session_config_request.tech = ASP_TECH_P2P;
+       event.session_config_request.session_id = session_id;
+       event.session_config_request.get_pin = get_pin;
+       if (pin)
+               g_strlcpy(event.session_request.pin, pin, PINSTR_LEN);
+       asp_manager_event(NULL, ASP_EVENT_SESSION_CONFIG_REQUEST, &event);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void asp_tech_p2p_process_connect_status(GDBusConnection *connection,
+               const gchar *object_path, GVariant *parameters)
+{
+       __ASP_LOG_FUNC_ENTER__;
+
+       GVariantIter *iter = NULL;
+       GVariant *var = NULL;
+       asp_event_data event;
+       unsigned char session_mac[MAC_LEN] = {0,};
+       const char *key = NULL;
+       const char *str = NULL;
+       unsigned int session_id = 0;
+       char status = 0;
+       char *deferred = NULL;
+
+       if (!parameters) {
+               __ASP_LOG_FUNC_EXIT__;
+               return;
+       }
+
+       g_variant_get(parameters, "(a{sv})", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
+               if (!g_strcmp0(key, "session_mac")) {
+                       if (asp_dbus_unpack_ay(session_mac, var, MAC_LEN) < 0)
+                               return;
+               } else if (!g_strcmp0(key, "session_id")) {
+                       g_variant_get(var, "u", &session_id);
+               } else if (!g_strcmp0(key, "status")) {
+                       g_variant_get(var, "i", &status);
+               } else if (!g_strcmp0(key, "deferred")) {
+                       g_variant_get(var, "&s", &str);
+                       deferred = g_strdup(str);
+               } else {
+                       ;/* Do Nothing */
+               }
+       }
+
+       memset(&event, 0x0, sizeof(asp_event_data));
+       event.connect_status.tech = ASP_TECH_P2P;
+       g_snprintf(event.connect_status.session_mac, MACSTR_LEN, MACSTR,
+                       MAC2STR(session_mac));
+       event.connect_status.session_id = session_id;
+       event.connect_status.status = status;
+       event.connect_status.deferred = deferred;
+       asp_manager_event(NULL, ASP_EVENT_CONNECT_STATUS, &event);
+
+       g_free(deferred);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+int asp_tech_p2p_advertise(asp_service_advertise_s *service, int replace)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantBuilder *builder = NULL;
+       GVariant *params = NULL;
+       GError *error = NULL;
+       GVariant *reply = NULL;
+       int result = 0;
+
+       if (!service || !g_wfd_gdbus_conn) {
+               ASP_LOGE("Invalid parameter!");
+               __ASP_LOG_FUNC_EXIT__;
+               return -1;
+       }
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_add(builder, "{sv}", "adv_id", g_variant_new("u", service->adv_id));
+       g_variant_builder_add(builder, "{sv}", "discovery_tech", g_variant_new("i", service->discovery_tech));
+       g_variant_builder_add(builder, "{sv}", "preferred_connection", g_variant_new("y", service->preferred_connection));
+       g_variant_builder_add(builder, "{sv}", "auto_accept", g_variant_new("i", service->auto_accept));
+       g_variant_builder_add(builder, "{sv}", "status", g_variant_new("y", service->status));
+       g_variant_builder_add(builder, "{sv}", "role", g_variant_new("y", service->role));
+       g_variant_builder_add(builder, "{sv}", "replace", g_variant_new("i", replace));
+       g_variant_builder_add(builder, "{sv}", "config_method", g_variant_new("u", service->config_method));
+
+       if (service->instance_name)
+               g_variant_builder_add(builder, "{sv}", "instance_name", g_variant_new("s", service->instance_name));
+       if (service->service_type)
+               g_variant_builder_add(builder, "{sv}", "service_type", g_variant_new("s", service->service_type));
+       if (service->service_info_map != NULL)
+               g_variant_builder_add(builder, "{sv}", "service_info",
+                               __g_hash_table_to_g_variant(service->service_info_map));
+       if (service->rsp_info)
+               g_variant_builder_add(builder, "{sv}", "rsp_info", g_variant_new("s", service->rsp_info));
+       params = g_variant_new("(a{sv})", builder);
+       g_variant_builder_unref(builder);
+
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_ASP_INTERFACE,
+                                                 "AdvertiseService", params, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+
+       g_variant_get(reply, "(i)", &result);
+       g_variant_unref(reply);
+
+       ASP_LOGD("%s() return : [%d]", __func__, result);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_cancel_advertise(asp_service_advertise_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariant *params = NULL;
+       GError *error = NULL;
+       GVariant *reply = NULL;
+       int result = 0;
+
+       if (!service || !g_wfd_gdbus_conn) {
+               ASP_LOGE("Invalid parameter!");
+               __ASP_LOG_FUNC_EXIT__;
+               return -1;
+       }
+
+       params = g_variant_new("(u)", service->adv_id);
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_ASP_INTERFACE,
+                                                 "CancelAdvertiseService", params, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+
+       g_variant_get(reply, "(i)", &result);
+       g_variant_unref(reply);
+
+       ASP_LOGD("%s() return : [%d]", __func__, result);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantBuilder *builder = NULL;
+       GVariant *params = NULL;
+       GError *error = NULL;
+       GVariant *reply = NULL;
+       int result = 0;
+
+       if (!service || !g_wfd_gdbus_conn) {
+               ASP_LOGE("Invalid parameter!");
+               __ASP_LOG_FUNC_EXIT__;
+               return -1;
+       }
+
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       g_variant_builder_add(builder, "{sv}", "discovery_tech", g_variant_new("i", service->discovery_tech));
+       g_variant_builder_add(builder, "{sv}", "preferred_connection", g_variant_new("y", service->preferred_connection));
+       g_variant_builder_add(builder, "{sv}", "service_type", g_variant_new("s", service->service_type));
+       if (service->service_info_map != NULL)
+               g_variant_builder_add(builder, "{sv}", "service_info", __g_hash_keys_to_g_variant(service->service_info_map));
+       params = g_variant_new("(a{sv})", builder);
+       g_variant_builder_unref(builder);
+       ASP_LOGI("instance name (%s)", service->instance_name);
+
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_ASP_INTERFACE,
+                                                 "SeekService", params, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+
+       g_variant_get(reply, "(it)", &result, &(service->search_id));
+       g_variant_unref(reply);
+
+       ASP_LOGD("%s() return : [%d]", __func__, result);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_cancel_seek(asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariant *params = NULL;
+       GError *error = NULL;
+       GVariant *reply = NULL;
+       int result = 0;
+
+       if (!service || !g_wfd_gdbus_conn) {
+               ASP_LOGE("Invalid parameter!");
+               __ASP_LOG_FUNC_EXIT__;
+               return -1;
+       }
+
+       params = g_variant_new("(t)", service->search_id);
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_ASP_INTERFACE,
+                                                 "CancelSeekService", params, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+
+       g_variant_get(reply, "(i)", &result);
+       g_variant_unref(reply);
+
+       ASP_LOGD("%s() return : [%d]", __func__, result);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_init()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GError* error = NULL;
+       GVariant *reply = NULL;
+       g_wfd_gdbus_conn = NULL;
+       asp_s *asp = NULL;
+       const char *str = NULL;
+       int result = 0;
+
+       asp = asp_get_manager();
+       if (asp == NULL) {
+               ASP_LOGE("Failed to get asp");
+               __ASP_LOG_FUNC_EXIT__;
+               return -1;
+       }
+
+       g_wfd_gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+       if (g_wfd_gdbus_conn == NULL) {
+               ASP_LOGE("Failed to get connection, Error[%s]", error->message);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return -1;
+       }
+
+       /* subscribe signal handler */
+       g_wfd_signal_id = g_dbus_connection_signal_subscribe(g_wfd_gdbus_conn,
+                       WFD_MANAGER_SERVICE, /* bus name */
+                       NULL, /* interface */
+                       NULL, /* member */
+                       WFD_MANAGER_PATH, /* object_path */
+                       NULL, /* arg0 */
+                       G_DBUS_SIGNAL_FLAGS_NONE,
+                       __asp_tech_p2p_dbus_signal_cb,
+                       NULL,
+                       NULL);
+       ASP_LOGI("Subscribed dbus signals [%d]", g_wfd_signal_id);
+
+       /* Get local P2P device address*/
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_CONFIG_INTERFACE,
+                                                 "GetMacAddress", NULL, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+       g_variant_get(reply, "(i&s)", &result, &str);
+       macaddr_atoe(str, asp->p2p_local_address);
+
+       g_variant_unref(reply);
+
+       /* Get local P2P Device Name*/
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_CONFIG_INTERFACE,
+                                                 "GetDeviceName", NULL, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+       g_variant_get(reply, "(i&s)", &result, &str);
+       g_strlcpy(asp->p2p_device_name, str, DEVICE_NAME_LEN);
+
+       g_variant_unref(reply);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_deinit()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       if (g_wfd_gdbus_conn == NULL)
+               return -1;
+
+       /* unsubscribe signal handler */
+       g_dbus_connection_signal_unsubscribe(g_wfd_gdbus_conn, g_wfd_signal_id);
+       g_wfd_signal_id = 0;
+
+       /* unref gdbus connection */
+       g_object_unref(g_wfd_gdbus_conn);
+       g_wfd_gdbus_conn = NULL;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_get_peer_role(unsigned char *mac_addr, int *is_connected, int *role)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariant *params = NULL;
+       GVariant *reply = NULL;
+       GError* error = NULL;
+
+       GVariantIter *iter_peer = NULL;
+       GVariant *var = NULL;
+       gchar *key = NULL;
+       char mac_address[18] = {0,};
+       gboolean is_group_client = FALSE;
+       gboolean is_group_owner = FALSE;
+       gboolean is_in_group = FALSE;
+       int result = 0;
+
+       /* Get P2P peer info*/
+       g_snprintf(mac_address, MACSTR_LEN, MACSTR, MAC2STR(mac_addr));
+       params = g_variant_new("(s)", mac_address);
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_MANAGE_INTERFACE,
+                                                 "GetPeerInfo", params, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+
+       g_variant_get(reply, "(ia{sv})", &result, &iter_peer);
+       if (result != 0) {
+               __ASP_LOG_FUNC_EXIT__;
+               return -1;
+       }
+
+       while (g_variant_iter_loop(iter_peer, "{sv}", &key, &var)) {
+               if (!g_strcmp0(key, "IsConnected"))
+                       is_group_client = g_variant_get_boolean(var);
+
+               else if (!g_strcmp0(key, "IsGroupOwner"))
+                       is_group_owner = g_variant_get_boolean(var);
+
+               else if (!g_strcmp0(key, "IsInGroup"))
+                       is_in_group = g_variant_get_boolean(var);
+
+               else
+                       ;/* Do Nothing */
+       }
+
+       g_variant_unref(reply);
+       if (is_group_owner)
+               *role = ASP_TECH_P2P_ROLE_GO;
+       if (is_group_client)
+               *role = ASP_TECH_P2P_ROLE_GC;
+
+       if (is_in_group)
+               *is_connected = is_in_group;
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int __asp_tech_p2p_send_prov(asp_connect_session_params *params)
+{
+       GVariantBuilder *builder = NULL;
+       GVariant *variant_params = NULL;
+       GVariant *reply = NULL;
+       GError* error = NULL;
+       char mac_str[MACSTR_LEN + 1] = {0, };
+       int result = 0;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+       g_snprintf(mac_str, MACSTR_LEN, MACSTR, MAC2STR(params->service_mac));
+       g_variant_builder_add(builder, "{sv}", "service_mac", g_variant_new("s", mac_str));
+       g_variant_builder_add(builder, "{sv}", "adv_id", g_variant_new("u", params->advertisement_id));
+
+       g_snprintf(mac_str, MACSTR_LEN, MACSTR, MAC2STR(params->session_mac));
+       g_variant_builder_add(builder, "{sv}", "session_mac", g_variant_new("s", mac_str));
+       g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", params->session_id));
+       g_variant_builder_add(builder, "{sv}", "role", g_variant_new("y", params->network_role));
+       g_variant_builder_add(builder, "{sv}", "config_method", g_variant_new("u", params->network_config));
+       if (params->session_information)
+               g_variant_builder_add(builder, "{sv}", "session _info", g_variant_new("s", params->session_information));
+
+       variant_params = g_variant_new("(a{sv})", builder);
+       g_variant_builder_unref(builder);
+
+       /* Connect ASP session via P2P group */
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_ASP_INTERFACE,
+                                                 "ConnectSession", variant_params, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+
+       g_variant_get(reply, "(i)", &result);
+       g_variant_unref(reply);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_connect_session(asp_connect_session_params *params)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int is_connected = 0;
+       int role = ASP_TECH_P2P_ROLE_NEW;
+       int result = 0;
+
+       result = asp_tech_p2p_get_peer_role(params->service_mac, &is_connected, &role);
+       if (result < 0) {
+               ASP_LOGE("get peer role failed");
+                       __ASP_LOG_FUNC_EXIT__;
+                       return ASP_ERROR_OPERATION_FAILED;
+       }
+
+       if (params->network_role != ASP_TECH_P2P_ROLE_NEW &&
+                       params->network_role == role) {
+               ASP_LOGE("Device can't be requested role");
+                       __ASP_LOG_FUNC_EXIT__;
+                       return ASP_ERROR_NETWORK_ROLE_REJECTED;
+       }
+
+       if (!is_connected) {
+               ASP_LOGD("Device is not connected");
+               result = __asp_tech_p2p_send_prov(params);
+       } else {
+               ASP_LOGD("Device is connected");
+
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_confirm_session(unsigned char *session_mac, int session_id, int confirm, const char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariantBuilder *builder = NULL;
+       GVariant *params = NULL;
+       GVariant *reply = NULL;
+       GError* error = NULL;
+       char mac_str[MACSTR_LEN + 1] = {0, };
+       int result = 0;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+       g_snprintf(mac_str, MACSTR_LEN, MACSTR, MAC2STR(session_mac));
+       g_variant_builder_add(builder, "{sv}", "service_mac", g_variant_new("s", mac_str));
+       g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", session_id));
+       g_variant_builder_add(builder, "{sv}", "confirm", g_variant_new("i", confirm));
+       if (pin)
+               g_variant_builder_add(builder, "{sv}", "pin", g_variant_new("s", pin));
+
+       params = g_variant_new("(a{sv})", builder);
+       g_variant_builder_unref(builder);
+
+       /* Confirm session requested via P2P */
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_MANAGE_INTERFACE,
+                                                 "ConfirmSession", params, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+
+       g_variant_get(reply, "(i)", &result);
+       g_variant_unref(reply);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_p2p_destroy_connection()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       GVariant *reply = NULL;
+       GError* error = NULL;
+       int result = 0;
+
+       /* Destroy ASP P2P Group */
+       reply = asp_tech_p2p_dbus_method_call_sync(WFD_MANAGER_GROUP_INTERFACE,
+                                                 "DestroyGroup", NULL, &error);
+       if (error != NULL) {
+               result = __net_wifidirect_gerror_to_enum(error);
+               g_error_free(error);
+               __ASP_LOG_FUNC_EXIT__;
+               return result;
+       }
+
+       g_variant_get(reply, "(i)", &result);
+       g_variant_unref(reply);
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+asp_tech_ops_s asp_tech_p2p_ops = {
+               .init = asp_tech_p2p_init,
+               .deinit = asp_tech_p2p_deinit,
+               .advertise = asp_tech_p2p_advertise,
+               .cancel_advertise = asp_tech_p2p_cancel_advertise,
+               .seek = asp_tech_p2p_seek,
+               .cancel_seek = asp_tech_p2p_cancel_seek,
+               .connect_session = asp_tech_p2p_connect_session,
+               .confirm_session = asp_tech_p2p_confirm_session,
+               .destroy_connection = asp_tech_p2p_destroy_connection,
+};
diff --git a/src/tech/asp-tech-p2p.h b/src/tech/asp-tech-p2p.h
new file mode 100755 (executable)
index 0000000..c6182c6
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __ASP_TECH_P2P_H__
+#define __ASP_TECH_P2P_H__
+
+#endif /* __ASP_TECH_P2P_H__ */
diff --git a/src/tech/asp-tech.c b/src/tech/asp-tech.c
new file mode 100755 (executable)
index 0000000..5db7fb5
--- /dev/null
@@ -0,0 +1,258 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************
+ * Standard headers
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/*****************************************************************************
+ * System headers
+ *****************************************************************************/
+
+#include <glib.h>
+#include<dlog.h>
+
+/*****************************************************************************
+ * Application Service Platform Daemon headers
+ *****************************************************************************/
+#include "asp-manager-util.h"
+#include "asp-service.h"
+#include "asp-session.h"
+#include "asp-tech.h"
+
+/*****************************************************************************
+ * Macros and Typedefs
+ *****************************************************************************/
+
+/* TODO : define service data structure */
+
+/*****************************************************************************
+ * Global Variables
+ *****************************************************************************/
+
+/* TODO : define local memory for service data structure */
+
+extern asp_tech_ops_s asp_tech_p2p_ops;
+extern asp_tech_ops_s asp_tech_ble_ops;
+extern asp_tech_ops_s asp_tech_nfc_ops;
+extern asp_tech_ops_s asp_tech_infra_ops;
+extern asp_tech_ops_s asp_tech_nan_ops;
+
+asp_tech_ops_s *asp_techs[] = {
+               NULL,
+               &asp_tech_p2p_ops,
+               &asp_tech_ble_ops,
+               &asp_tech_nfc_ops,
+               &asp_tech_infra_ops,
+               &asp_tech_nan_ops,
+               NULL
+};
+
+/*****************************************************************************
+ * Local Functions Definition
+ *****************************************************************************/
+
+void asp_tech_init()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       result = asp_techs[ASP_TECH_P2P]->init();
+       if (result < 0) {
+               ASP_LOGE("Failed to initialize p2p");
+               /* TODO : exclude p2p technology */
+       }
+
+
+       result = asp_techs[ASP_TECH_INFRA]->init();
+       if (result < 0) {
+               ASP_LOGE("Failed to initialize infra");
+               /* TODO : exclude infra technology */
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+void asp_tech_deinit()
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int result = 0;
+
+       result = asp_techs[ASP_TECH_P2P]->deinit();
+       if (result < 0) {
+               ASP_LOGE("Failed to deinitialize p2p");
+               /* TODO : exclude p2p technology */
+       }
+
+
+       result = asp_techs[ASP_TECH_INFRA]->deinit();
+       if (result < 0) {
+               ASP_LOGE("Failed to deinitialize infra");
+               /* TODO : exclude infra technology */
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return;
+}
+
+int asp_tech_advertise(int tech, asp_service_advertise_s *service, int replace)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int tech_idx = 0;
+       int i = 0;
+       int result = 0;
+
+       for (i = ASP_TECH_P2P; i != ASP_TECH_MAX; i++) {
+               tech_idx =  1 << (i - 1);
+               if (tech & tech_idx) {
+                       ASP_LOGD("Advertise using tech %d", tech);
+                       result = asp_techs[i]->advertise(service, replace);
+               }
+               /* TODO */
+               /* error handling */
+       }
+
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_cancel_advertise(int tech, asp_service_advertise_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int tech_idx = 0;
+       int i = 0;
+       int result = 0;
+
+       for (i = ASP_TECH_P2P; i != ASP_TECH_MAX; i++) {
+               tech_idx =  1 << (i - 1);
+               if (tech & tech_idx) {
+                       ASP_LOGD("Cancel advertise using tech %d", tech_idx);
+                       result = asp_techs[i]->cancel_advertise(service);
+               }
+               /* TODO */
+               /* error handling */
+       }
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_seek(int tech, asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int tech_idx = 0;
+       int i = 0;
+       int result = 0;
+
+       for (i = 1; i != ASP_TECH_MAX; i++) {
+               tech_idx =  1 << (i - 1);
+               if (tech & tech_idx) {
+                       ASP_LOGD("Seek service using tech %d", tech_idx);
+                       result = asp_techs[i]->seek(service);
+               }
+               /* TODO */
+               /* error handling */
+       }
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_cancel_seek(int tech, asp_service_seek_s *service)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int tech_idx = 0;
+       int i = 0;
+       int result = 0;
+
+       for (i = 1; i != ASP_TECH_MAX; i++) {
+               tech_idx =  1 << (i - 1);
+               if (tech & tech_idx) {
+                       ASP_LOGD("Cancel seek service using tech %d", tech_idx);
+                       result = asp_techs[i]->cancel_seek(service);
+               }
+               /* TODO */
+               /* error handling */
+       }
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_connect_session(int tech, asp_connect_session_params *params)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int tech_idx = 0;
+       int i = 0;
+       int result = 0;
+
+       for (i = 1; i != ASP_TECH_MAX; i++) {
+               tech_idx =  1 << (i - 1);
+               if (tech & tech_idx) {
+                       ASP_LOGD("Connect session using tech %d", tech_idx);
+                       result = asp_techs[i]->connect_session(params);
+                       /* TODO */
+                       /* error handling */
+                       break;
+               }
+       }
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_confirm_session(int tech, unsigned char *session_mac, int session_id, int confirm, const char *pin)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int tech_idx = 0;
+       int i = 0;
+       int result = 0;
+
+       for (i = 1; i != ASP_TECH_MAX; i++) {
+               tech_idx =  1 << (i - 1);
+               if (tech & tech_idx) {
+                       ASP_LOGD("Confirm session using tech %d", tech_idx);
+                       result = asp_techs[i]->confirm_session(session_mac, session_id, confirm, pin);
+                       /* TODO */
+                       /* error handling */
+                       break;
+               }
+       }
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}
+
+int asp_tech_destroy_connection(int tech)
+{
+       __ASP_LOG_FUNC_ENTER__;
+       int tech_idx = 0;
+       int i = 0;
+       int result = 0;
+
+       for (i = 1; i != ASP_TECH_MAX; i++) {
+               tech_idx =  1 << (i - 1);
+               if (tech & tech_idx) {
+                       ASP_LOGD("Destroy connection using tech %d", tech_idx);
+                       result = asp_techs[i]->destroy_connection();
+               }
+               /* TODO */
+               /* error handling */
+       }
+       __ASP_LOG_FUNC_EXIT__;
+       return result;
+}