PageRenderTime 63ms CodeModel.GetById 14ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/fbusgen/coder_client.c

http://ftk.googlecode.com/
C | 381 lines | 305 code | 47 blank | 29 comment | 48 complexity | 88f40f4f0c3305e9cbf0c4404e35c6b2 MD5 | raw file
  1/*
  2 * File:    coder_client.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   coder generate client marshal/unmarshal code.
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2010-07-31 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "coder_intf.h"
 33
 34#define STR_LENGTH 127
 35
 36typedef struct _PrivInfo
 37{
 38	char* name;
 39	char* lower_name;
 40	GString* func_create;
 41	GString* func_marshal;
 42	gboolean  has_interface;
 43    char interface[STR_LENGTH+1];
 44    char interface_lower[STR_LENGTH+1];
 45    char interface_upper[STR_LENGTH+1];
 46}PrivInfo;
 47
 48static gboolean coder_client_end_interface(Coder* thiz)
 49{
 50	FILE* fp = NULL;
 51	char h_filename[260] = {0};
 52	char c_filename[260] = {0};
 53	char create_func[STR_LENGTH+1] = {0};
 54	PrivInfo* priv = (PrivInfo*)thiz->priv;
 55
 56	if(!priv->has_interface)
 57	{
 58		return TRUE;
 59	}
 60
 61	mkdir(priv->interface, 0777);
 62	snprintf(create_func, STR_LENGTH, "%s_%s_create(void)", priv->interface_lower, priv->lower_name);
 63	
 64	snprintf(h_filename, sizeof(h_filename) - 1, "%s/%s_%s.h", priv->interface, priv->interface_lower, priv->lower_name);
 65	fp = fopen(h_filename, "w+");
 66	if(fp != NULL)
 67	{
 68		coder_write_header(fp);
 69		fprintf(fp, "#ifndef %s_CLIENT_H\n", priv->interface_upper);
 70		fprintf(fp, "#define %s_CLIENT_H\n\n", priv->interface_upper);
 71		fprintf(fp, "#include \"fbus_typedef.h\"\n\n");
 72		fprintf(fp, "#include \"%s.h\"\n\n", priv->interface_lower);
 73		fprintf(fp, "FTK_BEGIN_DECLS\n\n");
 74		fprintf(fp, "%s* %s;\n\n", priv->interface, create_func);
 75		fprintf(fp, "FTK_END_DECLS\n\n");
 76		fprintf(fp, "#endif/*%s_CLIENT_H*/\n", priv->interface_upper);
 77		fclose(fp);
 78	}
 79	
 80	snprintf(h_filename, sizeof(h_filename) - 1, "%s_%s.h", priv->interface_lower, priv->lower_name);
 81	snprintf(c_filename, sizeof(c_filename) - 1, "%s/%s_%s.c", priv->interface, priv->interface_lower, priv->name);
 82	fp = fopen(c_filename, "w+");
 83	if(fp != NULL)
 84	{
 85		coder_write_header(fp);
 86		fprintf(fp, "#include \"fbus_proxy.h\"\n");
 87		fprintf(fp, "#include \"%s_share.h\"\n\n", priv->interface_lower);
 88		fprintf(fp, "#include \"%s\"\n\n", h_filename);
 89		fprintf(fp, "typedef struct _PrivInfo\n");
 90		fprintf(fp, "{\n");
 91		fprintf(fp, "	FBusProxy* proxy;\n");
 92		fprintf(fp, "}PrivInfo;\n\n");
 93		fprintf(fp, "%s\n", priv->func_marshal->str);
 94		fprintf(fp, "static void %s_%s_destroy(%s* thiz)\n", priv->interface_lower, priv->lower_name, priv->interface);
 95		fprintf(fp, "{\n");
 96		fprintf(fp, "	if(thiz != NULL)\n");
 97		fprintf(fp, "	{\n");
 98		fprintf(fp, "		DECL_PRIV(thiz, priv);\n");
 99		fprintf(fp, "		fbus_proxy_destroy(priv->proxy);\n");
100		fprintf(fp, "		FTK_FREE(thiz);\n");
101		fprintf(fp, "	}\n");
102		fprintf(fp, "	return;\n");
103		fprintf(fp, "}\n\n");
104
105		fprintf(fp, "%s* %s\n", priv->interface, create_func);
106		fprintf(fp, "{\n");
107		fprintf(fp, "	%s* thiz = FTK_ZALLOC(sizeof(%s) + sizeof(PrivInfo));\n\n", priv->interface, priv->interface);
108		fprintf(fp, "	if(thiz != NULL)\n");
109		fprintf(fp, "	{\n");
110		fprintf(fp, "		DECL_PRIV(thiz, priv);\n");
111		fprintf(fp, "		priv->proxy = fbus_proxy_create(FBUS_SERVICE_%s);\n", priv->interface_upper);
112		fprintf(fp, "%s", priv->func_create->str);
113		fprintf(fp, "		thiz->destroy = %s_%s_destroy;\n", priv->interface_lower, priv->lower_name);
114		fprintf(fp, "	}\n\n");
115		fprintf(fp, "	return thiz;\n");
116		fprintf(fp, "}\n");
117		fclose(fp);
118	}
119
120	g_string_free(priv->func_marshal, 1);
121	g_string_free(priv->func_create, 1);
122
123	priv->has_interface = FALSE;
124
125	return TRUE;
126}
127
128static gboolean coder_client_on_interface(Coder* thiz, const char* name, const char* parent)
129{
130	PrivInfo* priv = (PrivInfo*)thiz->priv;
131
132	coder_client_end_interface(thiz);
133	
134	priv->has_interface = 1;
135	strncpy(priv->interface, name, STR_LENGTH);
136	coder_name_to_lower(name, priv->interface_lower);
137
138	strcpy(priv->interface_upper, priv->interface_lower);
139	coder_to_upper(priv->interface_upper);
140
141	priv->func_marshal = g_string_sized_new(4096);
142	priv->func_create = g_string_sized_new(4096);
143
144	return TRUE;
145}
146
147typedef struct _TypeInfo
148{
149	int attr;
150	gboolean is_binary;
151	char name[STR_LENGTH+1];
152	char org_name[STR_LENGTH+1];
153}TypeInfo;
154
155static gboolean coder_get_type_info(IDL_tree tree, int attr, TypeInfo* type)
156{
157	const char* name = "";
158	type->attr = attr;
159	if(attr == IDL_PARAM_INOUT)
160	{
161		assert(!"Not supported.");
162	}
163
164	memset(type, 0x00, sizeof(TypeInfo));
165	if(IDL_NODE_IS_TYPE(tree))
166	{
167		switch(IDL_NODE_TYPE(tree))
168		{
169			case IDLN_TYPE_INTEGER:
170			{
171				name = "int";
172				break;
173			}
174			case IDLN_TYPE_STRING:
175			{
176				name = "String";
177				break;
178			}
179			default:
180			{
181				assert(!"Not supported");
182				break;
183			}
184
185		}
186	}
187	else if(IDL_NODE_TYPE(tree) == IDLN_IDENT)
188	{
189		name = IDL_IDENT(tree).str;
190	}
191	
192	strcat(type->org_name, name);
193	if(strcmp(name, "int") == 0)
194	{
195		strcat(type->name, "int");
196		strcat(type->name, attr == IDL_PARAM_OUT ? "*" : "");
197	}
198	else if(strcmp(name, "String") == 0)
199	{
200		strcat(type->name, attr == IDL_PARAM_OUT ? "" : "const ");
201		strcat(type->name, "char*");
202		strcat(type->name, attr == IDL_PARAM_OUT ? "*" : "");
203	}
204	else if(strcmp(name, "FBusBinary") == 0)
205	{
206		type->is_binary = TRUE;
207	}
208	else
209	{
210		strcat(type->name, attr == IDL_PARAM_OUT ? "" : "const ");
211		strcat(type->name, name);
212		strcat(type->name, "*");
213	}
214
215	return TRUE;
216}
217
218
219static gboolean coder_client_on_function(Coder* thiz, struct _IDL_OP_DCL f)
220{
221	int i = 0;
222	TypeInfo  type = {0};
223	const char* param_name = NULL;
224	struct _IDL_LIST iter = {0};
225	char lower_func_name[STR_LENGTH+1] = {0};
226	PrivInfo* priv = (PrivInfo*)thiz->priv;
227	char req_coder_name[STR_LENGTH+1];
228	const char* func_name = IDL_IDENT(f.ident).str;
229	GString* params_list = g_string_sized_new(1024);
230	GString* params_in = g_string_sized_new(1024);
231	GString* params_out = g_string_sized_new(1024);
232
233	coder_name_to_lower(func_name, lower_func_name);
234	coder_get_type_info(f.op_type_spec, 0, &type);
235	
236	if(f.parameter_dcls != NULL)
237	{
238		for (i = 0, iter = IDL_LIST(f.parameter_dcls); iter.data != NULL; iter = IDL_LIST(iter.next))
239		{
240			int attr = IDL_PARAM_DCL(iter.data).attr;
241			param_name = IDL_IDENT(IDL_PARAM_DCL(iter.data).simple_declarator).str;
242			coder_get_type_info(IDL_PARAM_DCL(iter.data).param_type_spec, attr, &type);
243
244			g_string_append_printf(params_list, ", %s %s", type.name, param_name);
245
246			if(attr == IDL_PARAM_IN)
247			{
248				if(strcmp(type.org_name, "int") == 0)
249				{
250					g_string_append_printf(params_in, "\tfbus_parcel_write_int(parcel, %s);\n", param_name);
251				}
252				else if(strcmp(type.org_name, "String") == 0)
253				{
254					g_string_append_printf(params_in, "\tfbus_parcel_write_string(parcel, %s);\n", param_name);
255				}
256				else
257				{
258					g_string_append_printf(params_in, "\tfbus_parcel_write_data(parcel, %s, sizeof(*%s));\n", 
259						param_name, param_name);
260				}
261			}
262
263			if(attr == IDL_PARAM_OUT)
264			{
265				if(strcmp(type.org_name, "int") == 0)
266				{
267					g_string_append_printf(params_out, "\t\t\t*%s = fbus_parcel_get_int(parcel);\n", param_name);
268				}
269				else if(strcmp(type.org_name, "String") == 0)
270				{
271					g_string_append_printf(params_out, "\t\t\t*%s = (char*)fbus_parcel_get_string(parcel);\n", param_name);
272				}
273				else
274				{
275					g_string_append_printf(params_out, "\t\t\t*%s = *(%s)fbus_parcel_get_data(parcel, sizeof(*%s));\n", 
276						 param_name, type.name, param_name);
277				}
278			}
279
280			if(iter.next == NULL)
281			{
282				break;
283			}
284		}
285	}
286	coder_name_to_lower(func_name, lower_func_name);
287	snprintf(req_coder_name, sizeof(req_coder_name)-1, "%s_%s", priv->interface_lower, lower_func_name);
288	coder_to_upper(req_coder_name);
289	g_string_append_printf(priv->func_marshal, "static Ret %s_%s_%s(%s* thiz%s)\n{\n", 
290		priv->interface_lower, priv->lower_name, lower_func_name, priv->interface,  params_list->str);
291	g_string_append_printf(priv->func_marshal, "\tRet ret = RET_FAIL;\n");
292	g_string_append_printf(priv->func_marshal, "\tDECL_PRIV(thiz, priv);\n");
293	g_string_append_printf(priv->func_marshal, "\tFBusParcel* parcel = fbus_proxy_get_parcel(priv->proxy);;\n");
294	g_string_append_printf(priv->func_marshal,  
295		"	return_val_if_fail(priv != NULL && priv->proxy != NULL && parcel != NULL, RET_FAIL);\n\n");
296	g_string_append_printf(priv->func_marshal, "\tfbus_parcel_write_int(parcel, %s);\n", req_coder_name);
297	g_string_append_printf(priv->func_marshal, "%s\n", params_in->str);
298	g_string_append_printf(priv->func_marshal, "\tif((ret = fbus_proxy_request(priv->proxy, parcel)) == RET_OK)\n");
299	g_string_append_printf(priv->func_marshal, "\t{\n");
300	if(strlen(params_out->str) > 0)
301	{
302		g_string_append_printf(priv->func_marshal, "\t\tif((ret = fbus_parcel_get_int(parcel)) == RET_OK)\n");
303		g_string_append_printf(priv->func_marshal, "\t\t{\n");
304		g_string_append_printf(priv->func_marshal, "%s", params_out->str);
305		g_string_append_printf(priv->func_marshal, "\t\t}\n");
306	}
307	else
308	{
309		g_string_append_printf(priv->func_marshal, "\t\tret = fbus_parcel_get_int(parcel);\n");
310	}
311	g_string_append_printf(priv->func_marshal, "\t}\n\n");
312	g_string_append_printf(priv->func_marshal, "\treturn ret;\n");
313	g_string_append_printf(priv->func_marshal, "}\n\n");
314	
315	g_string_append_printf(priv->func_create, "		thiz->%s = %s_%s_%s;\n",
316		lower_func_name, priv->interface_lower, priv->lower_name, lower_func_name);
317
318	g_string_free(params_list, 1);
319	g_string_free(params_in, 1);
320	g_string_free(params_out, 1);
321
322	return TRUE;
323}
324
325static gboolean coder_client_on_const(Coder* thiz, struct _IDL_CONST_DCL c)
326{
327	return TRUE;
328}
329
330static gboolean coder_client_on_struct(Coder* thiz, struct _IDL_TYPE_STRUCT s)
331{
332	return TRUE;
333}
334
335static gboolean coder_client_on_enum(Coder* thiz, struct _IDL_TYPE_ENUM e)
336{
337	return TRUE;
338}
339
340static gboolean coder_client_on_union(Coder* thiz, struct _IDL_TYPE_UNION u)
341{
342	return TRUE;
343}
344
345static void coder_client_destroy(Coder* thiz)
346{
347	if(thiz != NULL)
348	{
349		PrivInfo* priv = (PrivInfo*)thiz->priv;
350		
351		coder_client_end_interface(thiz);
352		g_free(priv->name);
353		g_free(priv->lower_name);
354		g_free(thiz);
355	}
356
357	return;
358}
359
360Coder* coder_client_create(const char* name)
361{
362	Coder* thiz = g_malloc0(sizeof(Coder) + sizeof(PrivInfo));
363
364	if(thiz != NULL)
365	{
366		PrivInfo* priv = (PrivInfo*)thiz->priv;
367		
368		priv->name = g_strdup(name);
369		priv->lower_name = g_strdup(name);
370		coder_to_lower(priv->lower_name);
371		thiz->on_interface = coder_client_on_interface;
372		thiz->on_function  = coder_client_on_function;
373		thiz->on_const     = coder_client_on_const;
374		thiz->on_enum      = coder_client_on_enum;
375		thiz->on_struct    = coder_client_on_struct;
376		thiz->on_union     = coder_client_on_union;
377		thiz->destroy      = coder_client_destroy;
378	}
379
380	return thiz;
381}