diff options
Diffstat (limited to 'src/lib/ipc_client.c')
-rwxr-xr-x | src/lib/ipc_client.c | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/src/lib/ipc_client.c b/src/lib/ipc_client.c new file mode 100755 index 0000000..8c0adcf --- /dev/null +++ b/src/lib/ipc_client.c @@ -0,0 +1,220 @@ +/** + * @file ipc_client.c + * @brief client part using ipc methods + + * 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 <stdarg.h> +#include <string.h> +#include <stdlib.h> +#include <pthread.h> +#include "json_util.h" +#include "ipc.h" +#include "ipc_client.h" +#include "ipc_config.h" + +/** + * @fn static int __client_open_connection(char *server, SOCKET_HANDLE* phndl) + * @brief This function to open connection for client + * @param *server [in] server information + * @param *phndl [in] socket handle + * @return int result of function + */ +static int __client_open_connection(char *server, SOCKET_HANDLE* phndl) +{ + int ret = 0; + + printf("<<Client>> __client_open_connection\n"); + ret = IPC_OpenClientConnection(server, phndl); + if (0 == ret) { + printf("<<Client>> phndl->server_h = %d\n", phndl->server_h); + printf("<<Client>> phndl->client_h = %d\n", phndl->client_h); + } else { + printf("<<Client>> connection_init ERROR(%d) !!! {%s}\n", ret, SERVER); + } + return ret; +} +/** + * @fn static int __client_close_connection(SOCKET_HANDLE* phndl) + * @brief This function to close connection for client + * @param *phndl [in] socket handle + * @return int result of function + */ +static int __client_close_connection(SOCKET_HANDLE* phndl) +{ + int ret = 0; + + printf("<<Client>> __client_close_connection(*hndl=%p)\n", phndl); + if (phndl) + ret = IPC_OpenClientClose(phndl); + return ret; +} +/** + * @fn static int __client_send_data(void* data, int size, SOCKET_HANDLE* phndl) + * @brief This function to send data in client side + * @param *data [in] data to send + * @param *size [in] size of data + * @param *phndl [in] socket handle + * @return int result of function + */ +static int __client_send_data(void* data, int size, SOCKET_HANDLE* phndl) +{ + int ret = 0; + + printf("<<Client>> __client_send_data(*hndl=%p)\n", phndl); + if (phndl) { + ret = IPC_SendMessage(phndl, data, size); + printf("<<Client>> IPC_SendMessage(ret=%d)\n", ret); + } else { + ret = -1; + } + return ret; +} +/** + * @fn static int __client_read__data(char** data, SOCKET_HANDLE* phndl) + * @brief This function to send data in client side + * @param **data [inout] buffter to copy for reading data + * @param *phndl [in] socket handle + * @return int result of function + */ +static int __client_read__data(char** data, SOCKET_HANDLE* phndl) +{ + int ret = 0; + int msg_size = 0; + char *buf = NULL; + + printf("<<Client>> __client_read__data(*hndl=%p) waiting...\n", phndl); + if (phndl) { + // read first message : data size + ret = IPC_GetMessage(phndl, &msg_size); + printf("<<Client>> IPC_GendMessage(ret=%d) : SIZE(%d)\n", ret, msg_size); + if (msg_size > 0) { + buf = (char *)malloc(msg_size +1); + memset(buf, 0x00, msg_size+1); + + // read second message : real data buffer + ret = IPC_RecvMessage(phndl, buf, msg_size); + printf("<<Client>> IPC_GendMessage(ret=%d) : DATA=\n%s\n", ret, buf); + + *data = buf; + } + } else { + ret = -1; + } + return ret; +} + +static pthread_t callback_thread = (pthread_t) NULL; +/** + * @fn static char * __client_callback_thread(void * pv) + * @brief This function to wait response when a API calls + * @param *pv + * @return void * result of function + */ +static void * __client_callback_thread(void * pv) +{ + SOCKET_HANDLE* cbhndl = NULL; + SOCKET_HANDLE* pcbhndl = NULL; + int ret = 0; + char *buf = NULL; + char *data = NULL; + + if (NULL == pcbhndl) { + printf("<<Client-CB>> __client_callback_thread\n"); + cbhndl = (SOCKET_HANDLE*)malloc(sizeof(struct _IPC_SOCKET_HANDLE_s)); + if (cbhndl == NULL) + return NULL; + + ret = __client_open_connection(EVENT_SERVER, cbhndl); + if (0 == ret) { + pcbhndl = cbhndl; + printf("<<Client-CB>> pcbhndl->server_h = %d\n", pcbhndl->server_h); + printf("<<Client-CB>> pcbhndl->client_h = %d\n", pcbhndl->client_h); + + do { + ret = __client_read__data(&buf, cbhndl); + if (0 == ret && buf) { + printf("<<Client-CB>> receive {%s}\n", buf); + + //////////////////////////////////////////////// + // TO-DO return real callback function~~~ + //////////////////////////////////////////////// + typedef int(*func)(char*); + + data = json_getObject(buf, "callback_api"); + printf("<<Client-CB>> callback_api=%p\n", data); + if (data) { + (*(func)data)(buf); + free(data); + } + //(*(container_update_cb)token->callback_wrap)(NULL, user_data); + + //////////////////////////////////////////////// + free(buf); + buf = NULL; + } else { + if (buf) { + free(buf); + buf = NULL; + } + printf("<<Client-CB>> callback receive ERROR!! (ret=%d)\n", ret); + break; + } + } while (1); + } else { + printf("<<Client-CB>> __client_callback_thread ERROR(%d) !!! {%s}\n", ret, EVENT_SERVER); + } + } + + callback_thread = (pthread_t)NULL; + + __client_close_connection(cbhndl); + if (cbhndl != NULL) + free(cbhndl); + return NULL; +} + +int IPC_CallFunction(char * data, int size, char **retbuf) +{ + //SOCKET_HANDLE hndl = {0, 0}; //socket handle is allocated for each function call + SOCKET_HANDLE *hndl = NULL; + char *buf = NULL; + int ret = 0; + //char *data; + hndl = (SOCKET_HANDLE *)malloc(sizeof(struct _IPC_SOCKET_HANDLE_s)); + if (hndl == NULL) + return -1; +/* + if ((pthread_t)NULL == callback_thread) { + if (pthread_create(&callback_thread, NULL, &__client_callback_thread, NULL) < 0) { + printf("<<Client>> __client_callback_thread thread create error\n"); + free(hndl); + return -1; + } + } +*/ + ret = __client_open_connection(SERVER, hndl); + if (0 == ret) { + ret = __client_send_data(data, strlen(data), hndl); + if (0 == ret) + ret = __client_read__data(&buf, hndl); + } else { + printf("<<Client>> server connection error !!\n"); + ret = -1; + } + if (buf) + *retbuf = buf; + __client_close_connection(hndl); + + if (hndl != NULL) + free(hndl); + + return ret; +} |