summaryrefslogtreecommitdiff
path: root/src/dzl_server.c
blob: 94b32d61e2e315d03831a4bba19c451ace9ee940 (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
/**
* @file        dzl_server.c
* @brief       server 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 <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "ipc.h"
#include "dzl_server.h"
#include "ipc_config.h"
#include "dockzen_types.h"
#include "dzl_internal_types.h"

int IPC_StartServer(char *(*pFunc) (char *, int))
{
	SOCKET_HANDLE handle;

	int ret = 0;
	char *buf = NULL;
	int msgSize = 0;
	char *ret_buf = NULL;

	_D("dzl_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("dzl_server buff is null!!");
					IPC_OpenServerClose(&handle);
					IPC_SocketOpenClientClose(&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);
			}
		}
	}
	ret = IPC_OpenServerClose(&handle);

	} else {
		_E("<<Server>> server open error !!");
		return -1;
	}
	_D("<<Server>> close");
	return 0;
}

int IPC_RegisterCallback(char *data, int size)
{
	SOCKET_HANDLE * handle = NULL;
	if (data == NULL) {
		printf("<<Server>> SendMessage empty!!!\n");
		return -1;
	}
	handle = (SOCKET_HANDLE *) malloc(sizeof(struct _IPC_SOCKET_HANDLE_s));
	if (handle == NULL)
		return -1;

	if (IPC_OpenClientConnection(EVENT_SERVER, handle) >= 0) {
		if (handle->client_h >= 0) {
			printf("Server>> Send Message data = %s, size = %d\n", data, size);
			IPC_SendMessage(handle, data, strlen(data));
			IPC_OpenClientClose(handle);
		} else
			_E("<<Server>> handle->client_h error !!");
	} else {
		printf("<<Server>> notify server connection error!!\n");
		if (handle->client_h >= 0) {
			IPC_OpenClientClose(handle);
			if (handle != NULL)
				free(handle);
			return -1;
		} else
			_E("<<Server>> handle->client_h error !!");
	}
	if (handle != NULL)
		free(handle);
	return 0;
}