summaryrefslogtreecommitdiff
path: root/src/lib/ipc_client.c
blob: 8f8cf71c06ecec6432e64be564c0579efadb4e02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
 * @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->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;
}