summaryrefslogtreecommitdiff
path: root/src/lib/ipc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/ipc.c')
-rwxr-xr-xsrc/lib/ipc.c252
1 files changed, 252 insertions, 0 deletions
diff --git a/src/lib/ipc.c b/src/lib/ipc.c
new file mode 100755
index 0000000..1fea126
--- /dev/null
+++ b/src/lib/ipc.c
@@ -0,0 +1,252 @@
+/**
+ * @file ipc.c
+ * @brief IPC implementation for API
+
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * This software is the confidential and proprietary information
+ * of Samsung Electronics, Inc. ("Confidential Information"). You
+ * shall not disclose such Confidential Information and shall use
+ * it only in accordance with the terms of the license agreement
+ * you entered into with Samsung.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <stdbool.h>
+#include "ipc.h"
+
+#define MAXLINE 1024
+
+void ltoa(int val, char *string, int base, int str_size)
+{
+ char buf[32] = {0};
+ int i = 30;
+ char *pos = NULL;
+ for (; val && i; --i, val /= base) {
+ buf[i] = "0123456789abcdef"[val%base];
+ }
+ if (string != NULL) {
+ pos = &buf[i+1];
+ snprintf(string, str_size, "%s", pos);
+ }
+}
+
+int IPC_OpenServerConnection(char *server, SOCKET_HANDLE *phandle)
+{
+ return IPC_SocketOpenServerConnection(server, phandle);
+}
+
+int IPC_OpenServerClose(SOCKET_HANDLE *phandle)
+{
+ return IPC_SocketOpenServerClose(phandle);
+}
+
+int IPC_SendMessage(SOCKET_HANDLE *phandle, char *buf, int size)
+{
+ char size_buf[32] = {0};
+ char recvbuf[32] = {0};
+
+ ltoa(size, size_buf, 10, sizeof(size_buf));
+
+ //printf("<<IPC>> sendmessage!!\n");
+ IPC_SocketSendMessage(phandle, size_buf, strlen(size_buf));
+ printf("<<IPC>> SendMessage : SIZE(%d)=%s\n", (unsigned int)strlen(size_buf), size_buf);
+ while (1) {
+ if (IPC_SocketRecvMessage(phandle, recvbuf, 10) >= 0) {
+ //printf("<<IPC>> sendmessage recvbuf=%s!!\n", recvbuf);
+ break;
+ }
+ }
+ //printf("<<IPC>> SendMessage : DATA(%d)=\n%s\n", strlen(buf), buf);
+ IPC_SocketSendMessage(phandle, buf, size);
+
+ return 0;
+}
+
+int IPC_RecvMessage(SOCKET_HANDLE *phandle, char *buf, int size)
+{
+ IPC_SocketRecvMessage(phandle, buf, size);
+ return 0;
+}
+
+int IPC_GetMessage(SOCKET_HANDLE *phandle, int *msgSize)
+{
+ return IPC_SocketGetMessage(phandle, msgSize);
+}
+
+int IPC_ClientAccept(SOCKET_HANDLE *phandle)
+{
+ return IPC_SocketClientAccept(phandle);
+}
+
+int IPC_OpenClientConnection(char *server, SOCKET_HANDLE *phandle)
+{
+ return IPC_SocketOpenClientConnection(server, phandle);
+}
+
+int IPC_OpenClientClose(SOCKET_HANDLE *phandle)
+{
+ return IPC_SocketOpenClientClose(phandle);
+}
+
+int IPC_SocketOpenServerConnection(char *server, SOCKET_HANDLE *phandle)
+{
+ int state;
+ int server_sockfd;
+ struct sockaddr_un serveraddr;
+
+ if (access(server, F_OK) == 0) {
+ unlink(server);
+ }
+
+ if ((server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ printf("<<IPC>> socket error !!! \n");
+ return -1;
+ }
+
+ bzero(&serveraddr, sizeof(serveraddr));
+ serveraddr.sun_family = AF_UNIX;
+ snprintf(serveraddr.sun_path, sizeof(serveraddr.sun_path), "%s", server);
+
+ state = bind(server_sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
+ if (state == -1) {
+ printf("<<IPC>> bind error !\n");
+ close(server_sockfd);
+ return -1;
+ }
+
+ state = listen(server_sockfd, 5);
+ if (state == -1) {
+ printf("<<IPC>> listen error !!! \n");
+ close(server_sockfd);
+ return -1;
+ }
+
+ (phandle->server_h) = server_sockfd;
+
+ return 0;
+
+}
+
+int IPC_SocketOpenServerClose(SOCKET_HANDLE *phandle)
+{
+ return close(phandle->server_h);
+}
+
+int IPC_SocketOpenClientClose(SOCKET_HANDLE *phandle)
+{
+ return close(phandle->client_h);
+}
+
+int IPC_SocketClientAccept(SOCKET_HANDLE *phandle)
+{
+ struct sockaddr_un clientaddr;
+ socklen_t client_len;
+ int client_sockfd;
+
+ printf("<< Accept>> server_h=%d\n", phandle->server_h);
+ client_len = sizeof(clientaddr);
+
+ while (1) {
+ client_sockfd = accept(phandle->server_h, (struct sockaddr *)&clientaddr, &client_len);
+ if (client_sockfd == -1) {
+ printf("<<IPC>> [%s][%d] Error!! \n", __FUNCTION__, __LINE__);
+ sleep(1);
+ continue;
+ }
+
+ break;
+ }
+
+ phandle->client_h = client_sockfd;
+ printf("<<IPC>> client = %d\n", phandle->client_h);
+
+ return 0;
+}
+
+int IPC_SocketGetMessage(SOCKET_HANDLE *phandle, int *msgSize)
+{
+ char buf[100] = {0,};
+
+ IPC_SocketRecvMessage(phandle, buf, 32);
+ //printf("<<IPC>> GetMessage = %s\n", buf);
+ IPC_SocketSendMessage(phandle, buf, 10);
+
+ (*msgSize) = atoi(buf);
+ return 0;
+}
+
+int IPC_SocketRecvMessage(SOCKET_HANDLE *phandle, char * buf, int size)
+{
+ int ret_size = 0;
+ //printf("<<IPC>> recvMessage buf = %s\n", buf);
+ while (1) {
+ ret_size = read(phandle->client_h, buf, size);
+ if (ret_size > 0)
+ break;
+ }
+ //printf("<<IPC>> recvMessage = %s\n, size = %d, ret_size = %d", buf, size, ret_size);
+ return 0;
+}
+
+int IPC_SocketSendMessage(SOCKET_HANDLE *phandle, char *buf, int size)
+{
+ return write(phandle->client_h, buf, size);
+}
+
+
+int IPC_SocketOpenClientConnection(char *server, SOCKET_HANDLE *phandle)
+{
+ struct sockaddr_un clientaddr;
+ int client_sockfd;
+ socklen_t client_len;
+ int cnt = 0;
+ bool connected = false;
+ int ret = 0;
+
+ client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (client_sockfd == -1) {
+ printf("<<IPC>> client error !!\n");
+ return -1;
+ }
+
+ //bzero(&clientaddr, sizeof(clientaddr));
+ memset(&clientaddr, 0, sizeof(clientaddr));
+ clientaddr.sun_family = AF_UNIX;
+ if (strlen(server) + 1 >= sizeof(clientaddr.sun_path)) {
+ printf("<<IPC>> ERROR: server path(%d) too long >= UNIX_PATH_MAX(%d)\n", (unsigned int)strlen(server), (unsigned int)sizeof(clientaddr.sun_path));
+ close(client_sockfd);
+ return -1;
+ } else {
+ memcpy(clientaddr.sun_path, server, strlen(server) + 1);
+ }
+ client_len = sizeof(clientaddr);
+
+ do {
+ if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) >= 0) {
+ printf("<<IPC>> server connection OK!!\n");
+ connected = true;
+ break;
+ } else {
+ printf("<<IPC>> connect error !!\n");
+ sleep(1);
+ }
+ } while (++cnt < 3);
+
+ if (false == connected) {
+ printf("<<IPC>> client connect error !!\n");
+ phandle->client_h = -1;
+ ret = -1;
+ close(client_sockfd);
+ } else {
+ phandle->client_h = client_sockfd;
+ }
+
+ return ret;
+}