summaryrefslogtreecommitdiff
path: root/src/ipc_server.c
blob: a17deb893b7a024ea4f243fe16f1b3b412a62e9d (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
/**
 * @file        interface.c
 * @brief       API interface server

 * 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 <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "sa_common.h"
#include "sa_types.h"
#include "ipc.h"
#include "ipc_config.h"

#define MAXLINE 1024

int IPC_StartServer(char *(*pFunc) (char *, int))
{
	SOCKET_HANDLE handle;
	int ret = 0;
	char *buf = NULL;
	int msgSize = 0;
	char *ret_buf = NULL;

	_D("adaptor_api_server start");

	ret = IPC_OpenServerConnection(SERVER, &handle);

	_D("IPC_OpenServerConnection ret=%d", ret);

	if (ret >= 0) {
		while (1) {
			if (IPC_ClientAccept(&handle) != -1) {
				ret = IPC_GetMessage(&handle, &msgSize);
				_D("<<Server>> msgSize = %d", msgSize);

				if (msgSize > 0) {
					buf = (char *)malloc(msgSize + 1);
					if (buf == NULL) {
						_D("adaptor_api_server buff is null!!");
						IPC_SocketOpenClientClose(&handle);
						IPC_OpenServerClose(&handle);
						return -1;
					}
					memset(buf, 0x00, msgSize + 1);

					IPC_RecvMessage(&handle, buf, msgSize);
					//_D("<<Server>> recvData = %s", buf);
					/* Server mainloop */
					ret_buf = pFunc(buf, strlen(buf));

					//_D("<<Server>> sendData = %s", ret_buf);
					if (ret_buf != NULL) {
						IPC_SendMessage(&handle, ret_buf, strlen(ret_buf));
						free(ret_buf);
					}

					if (buf != NULL)
						free(buf);
				}
				IPC_SocketOpenClientClose(&handle);
			}
		}
		ret = IPC_OpenServerClose(&handle);
	} else {
		_E("<<Server>> server open error !!");
		return -1;
	}

	_D("<<Server>> close");
	return 0;

}

static SOCKET_HANDLE __cbhndl;
static SOCKET_HANDLE *__pcbhndl = NULL;

static int __open_callback_connection(void)
{
	int ret = 0;

	_D("[SERVER-CB] adaptor_server start (%s)", EVENT_SERVER);

	ret = IPC_OpenServerConnection(EVENT_SERVER, &__cbhndl);

	_D("[SERVER-CB] IPC_OpenServerConnection (ret=%d)", ret);

	if (ret >= 0) {
		__pcbhndl = &__cbhndl;

		ret = IPC_ClientAccept(__pcbhndl);
		_D("[SERVER-CB] IPC_ClientAccept (ret=%d)", ret);
		if (0 != ret)
			_E("[SERVER-CB] error (%d)", ret);
	} else {
		_E("[SERVER-CB] servercallback_init_connection open error !!");
	}

	return ret;

}

int callback_init_connection(void)
{
	int ret = 0;
	_D("[SERVER-CB] callback_thread");

	ret = __open_callback_connection();
	if (0 != ret)
		_E("[SERVER-CB] __open_callback_connection Failed");

	return ret;
}