Add AgentCommander for sending commands to the contextd-agent 73/141273/9
authorMu-Woong Lee <muwoong.lee@samsung.com>
Sat, 29 Jul 2017 17:06:19 +0000 (02:06 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 1 Aug 2017 09:01:35 +0000 (18:01 +0900)
Change-Id: I25feea55db4fd7cfbe03abb8836c4bdb2164a3d9
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/AgentCommander.h [new file with mode: 0644]
include/ContextTypes.h
include/IAgentPlugin.h [new file with mode: 0644]
src/server/AgentCommander.cpp [new file with mode: 0644]

diff --git a/include/AgentCommander.h b/include/AgentCommander.h
new file mode 100644 (file)
index 0000000..a76b214
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2017 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 __CONTEXT_AGENT_SOCKET_CLIENT_H__
+#define __CONTEXT_AGENT_SOCKET_CLIENT_H__
+
+#include <ContextTypes.h>
+
+namespace ctx {
+
+       class EXPORT_API AgentCommander {
+       public:
+               AgentCommander(uid_t uid);
+               ~AgentCommander();
+
+               bool send(uint16_t id, uint8_t length, const void* command);
+
+       private:
+               bool __init();
+               void __release();
+
+               uid_t __uid;
+               int __sockFd;
+       };
+
+}
+
+#endif
index a4bf832..d44210f 100644 (file)
 #ifndef __CONTEXT_TYPES_H__
 #define __CONTEXT_TYPES_H__
 
+#include <stdint.h>
 #include <sys/types.h>
 #include <glib.h>
 #include <gio/gio.h>
 #include <dlog.h>
 #include <tizen.h>
 
-/* DBus Parameters */
+/* IPC Parameters */
 #define CTX_DBUS_DEST                  "org.tizen.context"
 #define CTX_DBUS_PATH                  "/org/tizen/context"
 #define CTX_DBUS_IFACE                 CTX_DBUS_DEST
 #define CTX_DBUS_TIMEOUT               3000
 #define CTX_SIGNAL_NEW_CLIENT  "NewClient"
+#define CTX_AGENT_SOCKET               "/run/user_ext/%u/.contextd-agent.socket"
 
 /* Error Shortcuts */
 #define E_NONE         (int)TIZEN_ERROR_NONE                           /* Successful */
diff --git a/include/IAgentPlugin.h b/include/IAgentPlugin.h
new file mode 100644 (file)
index 0000000..f72441d
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2017 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 __CONTEXT_I_AGENT_PLUGIN_H__
+#define __CONTEXT_I_AGENT_PLUGIN_H__
+
+#include <stdint.h>
+#include <gio/gio.h>
+
+#define CTX_AGENT_COMMAND_LIMIT                32
+#define CTX_AGENT_PLUGIN_DIR           "context-agent"
+#define CTX_AGENT_CREATE_FUNC          create_plugins
+#define CTX_AGENT_CREATE_FUNC_SYM      "create_plugins"
+
+#define CTX_AGENT_DOMAIN_APP_HISTORY           0x0100
+#define CTX_AGENT_DOMAIN_CONTEXT_STORE         0x0200
+#define CTX_AGENT_DOMAIN_JOB_SCHEDULER         0x0300
+#define CTX_AGENT_DOMAIN_SENSOR_RECORDER       0x0400
+
+namespace ctx {
+
+       class IAgentUtil {
+       public:
+               virtual ~IAgentUtil() {}
+
+               // Functionalities provided by the contextd-agent daemon.
+               virtual GDBusConnection* getDBusConnection() = 0;
+       };
+
+       class IAgentPlugin {
+       public:
+               virtual ~IAgentPlugin() {}
+
+               virtual uint16_t getId() = 0;
+
+               virtual bool doAction(uint8_t length, const void* command) = 0;
+
+       };
+
+       typedef IAgentPlugin** (*create_plugins_t)(IAgentUtil* agentUtil);
+
+}
+
+#endif
diff --git a/src/server/AgentCommander.cpp b/src/server/AgentCommander.cpp
new file mode 100644 (file)
index 0000000..916a2e6
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2017 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.
+ */
+
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <IAgentPlugin.h>
+#include <AgentCommander.h>
+
+#define PATH_LENGTH    48
+
+using namespace ctx;
+
+AgentCommander::AgentCommander(uid_t uid) :
+       __uid(uid),
+       __sockFd(-1)
+{
+}
+
+AgentCommander::~AgentCommander()
+{
+}
+
+bool AgentCommander::send(uint16_t id, uint8_t length, const void* command)
+{
+       IF_FAIL_RETURN(length <= CTX_AGENT_COMMAND_LIMIT, false);
+
+       if (!__init()) {
+               __release();
+               return false;
+       }
+
+       //TODO: handle the degeneracy case failed to write all input data
+
+       int sent = 0;
+
+       sent += write(__sockFd, &id, sizeof(id));
+       sent += write(__sockFd, &length, sizeof(length));
+       sent += write(__sockFd, command, length);
+
+       __release();
+
+       _D("Given: %u bytes, Sent: %d bytes", sizeof(id) + sizeof(length) + length, sent);
+
+       return (sizeof(id) + sizeof(length) + length == static_cast<unsigned>(sent));
+}
+
+bool AgentCommander::__init()
+{
+       int sock = -1;
+       char path[PATH_LENGTH];
+       sockaddr_un addr;
+
+       g_snprintf(path, PATH_LENGTH, CTX_AGENT_SOCKET, static_cast<unsigned>(__uid));
+       IF_FAIL_RETURN_TAG(strlen(path) < sizeof(addr.sun_path), false, _E, "Invalid path");
+
+       sock = socket(AF_UNIX, SOCK_STREAM, 0);
+       IF_FAIL_RETURN_TAG(sock > 0, false, _E, "socket creation failed");
+
+       bzero(&addr, sizeof(addr));
+       addr.sun_family = AF_UNIX;
+       strncpy(addr.sun_path, path, sizeof(addr.sun_path));
+       addr.sun_path[sizeof(path)] = '\0';
+
+       if (connect(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
+               close(sock);
+               _E("Connection failed");
+               return false;
+       }
+
+       __sockFd = sock;
+       _I("Socket(%d) connected", sock);
+
+       return true;
+}
+
+void AgentCommander::__release()
+{
+       if (__sockFd > 0) {
+               close(__sockFd);
+               __sockFd = -1;
+       }
+
+       _I("Socket closed");
+}