summaryrefslogtreecommitdiff
path: root/src/lib/adaptor_api.c
blob: 788fcca0a776ec4ec5df68413f069293c2dcdc23 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
 * @file        adaptor_api.c
 * @brief       library for providing adaptor 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 <stdint.h>
#include "adaptor_api.h"
#include "ipc_client.h"
#include "json_util.h"

/**
 * @fn        static char *__get_funcname_param_in(char *funcName)
 * @brief     This function to generate in params for container info api
 * @param     *funcName		[in] function name to make json data
 * @return    *char			whole json data
 */
static char *__get_funcname_param_in(const char *funcName)
{
	struct json_object *newObj;
	const char *buf = NULL;
	char *ret_buf = NULL;

	newObj = json_object_new_object();
	json_object_object_add(newObj, "func", json_object_new_string(funcName));
	buf = json_object_to_json_string_ext(newObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);

	ret_buf = (char *)malloc(strlen(buf) + 1);
	if (ret_buf != NULL) {
		memset(ret_buf, 0x00, strlen(buf) + 1);
		memcpy(ret_buf, buf, strlen(buf) + 1);
	}
	json_object_put(newObj);

	return ret_buf;
}

/**
 * @fn        static int __get_device_reboot_param_out(char *rcvData, containers_info_s *containers_info_h)
 * @brief     This function to generate out params for container info api
 * @param     *revData					[in] received data from response
 * @return    int						result of function
 */
static int __get_device_reboot_param_out(char *rcvData)
{
	int ret = -1;
	if (rcvData)
		ret = json_getNumber(rcvData, "result");
	else
		printf("{%s}(%d) ERROR!!! NULL pointer of rcvData\n", __FUNCTION__, __LINE__);

	return ret;
}

/**
 * @fn        static int __get_device_reboot_param_out(char *rcvData, os_info_s *os_info)
 * @brief     This function to generate out params for container info api
 * @param     *revData					[in] received data from response
 * @param     *os_info					[inout] fill os info into structure
 * @return    int						result of function
 */
static int __get_os_info_param_out(char *rcvData, os_info_s *os_info)
{
	if (os_info) {
		os_info->platformVer = json_getString(rcvData, "platformVer");
		os_info->baseOSVer = json_getString(rcvData, "baseOSVer");
		os_info->dockerVer = json_getString(rcvData, "dockerVer");
	} else {
		printf("{%s}(%d) ERROR!!! NULL pointer of os_info\n", __FUNCTION__, __LINE__);
		return -1;
	}
	return 0;
}

/**
 * @fn        static int __get_disk_info_param_out(char *rcvData, disk_info_s *disk_info)
 * @brief     This function to generate out params for disk info api
 * @param     *revData					[in] received data from response
 * @param     *disk_info				[inout] fill disk info into structure
 * @return    int						result of function
 */
static int __get_disk_info_param_out(char *rcvData, disk_info_s *disk_info)
{
	int index;
	int diskCnt = 0;

	diskCnt = json_getNumber(rcvData, "count");

	if (disk_info) {
		disk_info->count = diskCnt;
		for (index = 0; index < diskCnt; index++) {
			disk_info->disk[index].path = json_getStringFromArray(rcvData, "disk", index, "path");
			disk_info->disk[index].free = json_getIntFromArray(rcvData, "disk", index, "free");
			disk_info->disk[index].total = json_getIntFromArray(rcvData, "disk", index, "total");
			disk_info->disk[index].used = json_getIntFromArray(rcvData, "disk", index, "used");
			disk_info->disk[index].usedpercent = json_getIntFromArray(rcvData, "disk", index, "usedpercent");
		}
	} else {
		printf("{%s}(%d) ERROR!!! NULL pointer of disk_info\n", __FUNCTION__, __LINE__);
		return -1;
	}
	return 0;
}

/**
 * @fn        static int __get_factory_restore_param_out(char *rcvData)
 * @brief     This function to generate out params for factory restore
 * @param     *revData					[in] received data from response
 * @return    int						result of function
 */
static int __get_factory_restore_param_out(char *rcvData)
{
	int ret = -1;
	if (rcvData)
		ret = json_getNumber(rcvData, "result");
	else
		printf("{%s}(%d) ERROR!!! NULL pointer of rcvData\n", __FUNCTION__, __LINE__);

	return ret;
}


API int device_reboot()
{
	int ret = ADAPTOR_API_ERROR_NONE;
	char *send_data = NULL;
	char *ret_buf = NULL;

	send_data = __get_funcname_param_in(__FUNCTION__);
	printf("Client>> SendMessage = %s\n", send_data);

	if (send_data) {
		ret = IPC_CallFunction(send_data, strlen(send_data), &ret_buf);
		printf("Client>> IPC_CallFunction(%d), ret_buf(%p)\n", ret, ret_buf);
		if (!ret && ret_buf) {
			ret = __get_device_reboot_param_out(ret_buf);
			printf("ret = %d\n", ret);
		}
		if (ret_buf)
			free(ret_buf);
		free(send_data);
	}

	return ret;
}

API int get_os_info(os_info_s * os_info)
{
	int ret = ADAPTOR_API_ERROR_NONE;
	char *send_data = NULL;
	char *ret_buf = NULL;

	send_data = __get_funcname_param_in(__FUNCTION__);
	printf("Client>> SendMessage = %s\n", send_data);

	if (send_data) {
		ret = IPC_CallFunction(send_data, strlen(send_data), &ret_buf);
		printf("Client>> IPC_CallFunction(%d), ret_buf(%p)\n", ret, ret_buf);
		if (!ret && ret_buf) {
			ret = __get_os_info_param_out(ret_buf, os_info);
			/*
			if (!ret) {
			   printf("os_info.platformVer = %s\n", os_info->platformVer);
			   printf("os_info.baseOSVer = %s\n", os_info->baseOSVer);
			   printf("os_info.dockerVer = %s\n", os_info->dockerVer);
		   }
		   */
		}
		if (ret_buf)
			free(ret_buf);
		free(send_data);
	}

	return ret;
}

API int get_disk_info(disk_info_s *disk_info)
{
	int ret = ADAPTOR_API_ERROR_NONE;
	char *send_data = NULL;
	char *ret_buf = NULL;
	//int index;

	send_data = __get_funcname_param_in(__FUNCTION__);
	printf("Client>> SendMessage = %s\n", send_data);

	if (send_data) {
		ret = IPC_CallFunction(send_data, strlen(send_data), &ret_buf);
		printf("Client>> IPC_CallFunction(%d), ret_buf(%p)\n", ret, ret_buf);
		if (!ret && ret_buf) {
			ret = __get_disk_info_param_out(ret_buf, disk_info);
			/*
			if (!ret) {
				for (index = 0; index < disk_info->count; index++) {
					printf("disk_info[%d] path = %s\n", index, disk_info->disk[index].path);
					printf("disk_info[%d] free = %dM\n", index, disk_info->disk[index].free);
					printf("disk_info[%d] total = %dM\n", index, disk_info->disk[index].total);
					printf("disk_info[%d] used = %dM\n", index, disk_info->disk[index].used);
					printf("disk_info[%d] usedpercent = %.2f%%\n", index, (float)disk_info->disk[index].usedpercent/(float)100);
				}
			}
			*/
		}
		if (ret_buf)
			free(ret_buf);
		free(send_data);
	}

	return ret;
}

API int factory_restore()
{
	int ret = ADAPTOR_API_ERROR_NONE;
	char *send_data = NULL;
	char *ret_buf = NULL;

	send_data = __get_funcname_param_in(__FUNCTION__);
	printf("Client>> SendMessage = %s\n", send_data);

	if (send_data) {
		ret = IPC_CallFunction(send_data, strlen(send_data), &ret_buf);
		printf("Client>> IPC_CallFunction(%d), ret_buf(%p)\n", ret, ret_buf);
		if (!ret && ret_buf) {
			ret = __get_factory_restore_param_out(ret_buf);
			printf("ret = %d\n", ret);
		}
		if (ret_buf)
			free(ret_buf);
		free(send_data);
	}

	return ret;
}