1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ofdbus/dbus/tsrc/testapps/method/src/method.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,470 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include<stdio.h>
1.23 +#include "test-utils.h"
1.24 +#include<string.h>
1.25 +#include<ctype.h>
1.26 +#include<unistd.h>
1.27 +#define TEST_BUS_NAME_TEMP "Test.Method.Call"
1.28 +
1.29 +void called_method(DBusMessage* msg, DBusConnection* connection)
1.30 +{
1.31 + DBusMessage* reply;
1.32 + DBusMessageIter args;
1.33 + DBusMessageIter in_args;
1.34 +
1.35 + char* in_str;
1.36 + DBusError error;
1.37 +
1.38 + dbus_uint32_t status = 94;
1.39 +
1.40 + dbus_error_init(&error);
1.41 + dbus_message_iter_init(msg, &in_args);
1.42 +
1.43 + reply = dbus_message_new_method_return(msg);
1.44 + dbus_message_iter_init_append(reply, &args);
1.45 +
1.46 + do{
1.47 + dbus_message_iter_get_basic(&in_args, &in_str);
1.48 +
1.49 + dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &in_str);
1.50 +
1.51 + }while(dbus_message_iter_next(&in_args));
1.52 +
1.53 + dbus_connection_send(connection, reply, NULL);
1.54 +
1.55 + dbus_connection_flush(connection);
1.56 +}
1.57 +
1.58 +
1.59 +void called_variable_args_method(DBusMessage* msg, DBusConnection* connection)
1.60 +{
1.61 + DBusMessage* reply;
1.62 + DBusMessageIter iter;
1.63 + DBusMessageIter sub_iter;
1.64 + void* arg = malloc(20);
1.65 + char* str;
1.66 + int no;
1.67 + int i;
1.68 + dbus_int32_t* array;
1.69 + dbus_int32_t return_value = 0;
1.70 + dbus_bool_t valid_flag = TRUE;
1.71 +
1.72 +
1.73 +
1.74 +
1.75 + reply = dbus_message_new_method_return(msg);
1.76 + dbus_message_iter_init(msg, &iter);
1.77 +
1.78 + do{
1.79 + switch(dbus_message_iter_get_arg_type(&iter))
1.80 + {
1.81 + case DBUS_TYPE_BOOLEAN:
1.82 + dbus_message_iter_get_basic(&iter, arg);
1.83 + if(*(dbus_bool_t*)arg != TRUE)
1.84 + valid_flag = FALSE;
1.85 + break;
1.86 + case DBUS_TYPE_INT16:
1.87 + dbus_message_iter_get_basic(&iter, arg);
1.88 + if(*(dbus_int16_t*)arg != -16)
1.89 + valid_flag = FALSE;
1.90 + break;
1.91 + case DBUS_TYPE_INT32:
1.92 + dbus_message_iter_get_basic(&iter, arg);
1.93 + if(*(dbus_int32_t*)arg != -32)
1.94 + valid_flag = FALSE;
1.95 + break;
1.96 + case DBUS_TYPE_INT64:
1.97 + dbus_message_iter_get_basic(&iter, arg);
1.98 + if(*(dbus_int64_t*)arg != -64)
1.99 + valid_flag = FALSE;
1.100 + break;
1.101 + case DBUS_TYPE_UINT16:
1.102 + dbus_message_iter_get_basic(&iter, arg);
1.103 + if(*(dbus_uint16_t*)arg != 16)
1.104 + valid_flag = FALSE;
1.105 + break;
1.106 + case DBUS_TYPE_UINT32:
1.107 + dbus_message_iter_get_basic(&iter, arg);
1.108 + if(*(dbus_uint32_t*)arg != 32)
1.109 + valid_flag = FALSE;
1.110 + break;
1.111 + case DBUS_TYPE_UINT64:
1.112 + dbus_message_iter_get_basic(&iter, arg);
1.113 + if(*(dbus_uint64_t*)arg != 64)
1.114 + valid_flag = FALSE;
1.115 + break;
1.116 + case DBUS_TYPE_DOUBLE:
1.117 + dbus_message_iter_get_basic(&iter, arg);
1.118 + if(*(double*)arg != 12.34567)
1.119 + valid_flag = FALSE;
1.120 + break;
1.121 + case DBUS_TYPE_STRING:
1.122 + dbus_message_iter_get_basic(&iter, &str);
1.123 + if(strcmp("DBus Testing", str))
1.124 + valid_flag = FALSE;
1.125 + break;
1.126 + case DBUS_TYPE_ARRAY:
1.127 + dbus_message_iter_recurse(&iter, &sub_iter);
1.128 + no = dbus_message_iter_get_array_len(&sub_iter);
1.129 + dbus_message_iter_get_fixed_array(&sub_iter, &array, &no);
1.130 + for(i=0;i<no;i++)
1.131 + if(array[i] != (i+1))
1.132 + valid_flag = FALSE;
1.133 + break;
1.134 + case DBUS_TYPE_STRUCT:
1.135 + dbus_message_iter_recurse(&iter, &sub_iter);
1.136 + do{
1.137 + switch(dbus_message_iter_get_arg_type(&sub_iter))
1.138 + {
1.139 + case DBUS_TYPE_INT32:
1.140 + dbus_message_iter_get_basic(&sub_iter, arg);
1.141 + if(*(dbus_int32_t*)arg != -32)
1.142 + valid_flag = FALSE;
1.143 + break;
1.144 + case DBUS_TYPE_STRING:
1.145 + dbus_message_iter_get_basic(&sub_iter, &str);
1.146 + if(strcmp("DBus Testing", str))
1.147 + valid_flag = FALSE;
1.148 + break;
1.149 + case DBUS_TYPE_DOUBLE:
1.150 + dbus_message_iter_get_basic(&sub_iter, arg);
1.151 + if(*(double*)arg != 12.34567)
1.152 + valid_flag = FALSE;
1.153 + break;
1.154 + default:
1.155 + valid_flag = FALSE;
1.156 +
1.157 + }
1.158 + }while(dbus_message_iter_next(&sub_iter));
1.159 +
1.160 + break;
1.161 + default:
1.162 + valid_flag = FALSE;
1.163 + }
1.164 + }while(dbus_message_iter_next(&iter));
1.165 +
1.166 + if(!valid_flag)
1.167 + return_value = 1;
1.168 + else
1.169 + return_value = 0;
1.170 +
1.171 + dbus_message_append_args(reply, DBUS_TYPE_INT32, &return_value, DBUS_TYPE_INVALID);
1.172 + dbus_connection_send(connection, reply, NULL);
1.173 + dbus_connection_flush(connection);
1.174 +}
1.175 +
1.176 +void called_simple(DBusMessage* msg, DBusConnection* connection)
1.177 +{
1.178 + DBusMessage* reply;
1.179 + dbus_int32_t return_value = 9090;
1.180 +
1.181 + reply = dbus_message_new_method_return(msg);
1.182 +
1.183 + dbus_message_append_args(reply, DBUS_TYPE_INT32, &return_value, DBUS_TYPE_INVALID);
1.184 + dbus_connection_send(connection, reply, NULL);
1.185 + dbus_connection_flush(connection);
1.186 +}
1.187 +
1.188 +
1.189 +void called_file_send(DBusMessage* msg, DBusConnection* connection)
1.190 +{
1.191 + DBusMessage* reply;
1.192 + int return_value = 8080;
1.193 + DBusMessageIter iter;
1.194 + DBusMessageIter sub_iter;
1.195 + int no;
1.196 + unsigned char* array;
1.197 + FILE* fp=NULL;
1.198 +
1.199 + dbus_message_iter_init(msg, &iter);
1.200 + dbus_message_iter_recurse(&iter, &sub_iter);
1.201 + no = dbus_message_iter_get_array_len(&sub_iter);
1.202 + dbus_message_iter_get_fixed_array(&sub_iter, &array, &no);
1.203 +
1.204 + fp = fopen("c:\\data\\images\\pictures\\bunbask1.jpg", "a+");
1.205 + if(!fp)
1.206 + {
1.207 + printf("Failed to open file");
1.208 + return;
1.209 + }
1.210 +
1.211 + fwrite(array, sizeof(unsigned char), no, fp);
1.212 +
1.213 + if(fp)
1.214 + fclose(fp);
1.215 +
1.216 + reply = dbus_message_new_method_return(msg);
1.217 + dbus_connection_send(connection, reply, NULL);
1.218 + dbus_connection_flush(connection);
1.219 +}
1.220 +
1.221 +void called_file_send_time(DBusMessage* msg,DBusConnection* connection)
1.222 + {
1.223 + DBusMessageIter iter;
1.224 + DBusMessageIter sub_iter;
1.225 + int no;
1.226 + unsigned char* array;
1.227 + FILE* fp=NULL;
1.228 + DBusMessage* reply;
1.229 + char* file_name;
1.230 +
1.231 + reply = dbus_message_new_method_return(msg);
1.232 + dbus_connection_send(connection, reply, NULL);
1.233 + dbus_connection_flush(connection);
1.234 +
1.235 + dbus_message_iter_init(msg, &iter);
1.236 + dbus_message_iter_get_basic(&iter, &file_name);
1.237 + dbus_message_iter_next(&iter);
1.238 + dbus_message_iter_recurse(&iter, &sub_iter);
1.239 + no = dbus_message_iter_get_array_len(&sub_iter);
1.240 + dbus_message_iter_get_fixed_array(&sub_iter, &array, &no);
1.241 +
1.242 + strcat(file_name, "_1");
1.243 + fp = fopen(file_name, "w+");
1.244 + if(!fp)
1.245 + {
1.246 + printf("Failed to open file %s", file_name);
1.247 + getchar();
1.248 + }
1.249 +
1.250 + fwrite(array, sizeof(unsigned char), no, fp);
1.251 + if(fp)
1.252 + fclose(fp);
1.253 + }
1.254 +
1.255 +void called_sign_test(DBusMessage* msg, DBusConnection* connection)
1.256 +{
1.257 + DBusError error;
1.258 + const char* signature;
1.259 + const char* msg_sign;
1.260 + const char* msg_sign1;
1.261 + DBusSignatureIter sign_iter;
1.262 + DBusSignatureIter sub_sign_iter;
1.263 + DBusSignatureIter sub_sign_iter1;
1.264 + DBusMessageIter iter;
1.265 + DBusMessage* reply;
1.266 + int type;
1.267 + int sub_type;
1.268 + const char* sub_signature;
1.269 +
1.270 + dbus_error_init(&error);
1.271 +
1.272 + dbus_message_iter_init(msg, &iter);
1.273 + msg_sign = dbus_message_iter_get_signature(&iter);
1.274 + dbus_message_iter_next(&iter);
1.275 + dbus_message_iter_next(&iter);
1.276 + msg_sign1 = dbus_message_iter_get_signature(&iter);
1.277 +
1.278 + signature = dbus_message_get_signature(msg);
1.279 + dbus_signature_iter_init(&sign_iter, signature);
1.280 + if(dbus_signature_validate_single(signature, &error))
1.281 + {
1.282 + exit(1);
1.283 + }
1.284 + do{
1.285 + type = dbus_signature_iter_get_current_type(&sign_iter);
1.286 + switch(type)
1.287 + {
1.288 + case DBUS_TYPE_ARRAY:
1.289 + dbus_signature_iter_recurse(&sign_iter, &sub_sign_iter1);
1.290 + sub_type = dbus_signature_iter_get_element_type(&sign_iter);
1.291 + break;
1.292 + case DBUS_TYPE_STRUCT:
1.293 + dbus_signature_iter_recurse(&sign_iter, &sub_sign_iter);
1.294 + sub_signature = dbus_signature_iter_get_signature(&sign_iter);
1.295 + dbus_error_init(&error);
1.296 + if(!dbus_signature_validate_single(sub_signature, &error))
1.297 + {
1.298 + exit(1);
1.299 + }
1.300 + break;
1.301 + }
1.302 + }while(dbus_signature_iter_next(&sign_iter));
1.303 +
1.304 + reply = dbus_message_new_method_return(msg);
1.305 + dbus_message_append_args(reply, DBUS_TYPE_SIGNATURE, &signature, DBUS_TYPE_SIGNATURE, &sub_signature, DBUS_TYPE_INT32, &sub_type, DBUS_TYPE_SIGNATURE, &msg_sign, DBUS_TYPE_SIGNATURE, &msg_sign1, DBUS_TYPE_INVALID);
1.306 + dbus_connection_send(connection, reply, NULL);
1.307 + dbus_connection_flush(connection);
1.308 +
1.309 + dbus_free((void*)msg_sign);
1.310 + dbus_free((void*)msg_sign1);
1.311 + dbus_message_unref(reply);
1.312 +}
1.313 +
1.314 +void called_file_send_whole(DBusMessage* msg, DBusConnection* connection)
1.315 + {
1.316 + DBusMessageIter iter;
1.317 + DBusMessageIter sub_iter;
1.318 + int no;
1.319 + unsigned char* array;
1.320 + FILE* fp=NULL;
1.321 + DBusMessage* reply;
1.322 +
1.323 + reply = dbus_message_new_method_return(msg);
1.324 + dbus_connection_send(connection, reply, NULL);
1.325 + dbus_connection_flush(connection);
1.326 +
1.327 + dbus_message_iter_init(msg, &iter);
1.328 + dbus_message_iter_recurse(&iter, &sub_iter);
1.329 + no = dbus_message_iter_get_array_len(&sub_iter);
1.330 + dbus_message_iter_get_fixed_array(&sub_iter, &array, &no);
1.331 +
1.332 + fp = fopen("c:\\data\\Images\\Pictures\\test.mp3", "a+");
1.333 + if(!fp)
1.334 + {
1.335 + printf("Failed to open file");
1.336 + getchar();
1.337 + }
1.338 +
1.339 + fwrite(array, sizeof(unsigned char), no, fp);
1.340 +
1.341 + if(fp)
1.342 + fclose(fp);
1.343 +
1.344 + dbus_message_unref(reply);
1.345 + }
1.346 +
1.347 +int main()
1.348 +{
1.349 + DBusError error;
1.350 + DBusError error1;
1.351 + DBusConnection* connection;
1.352 + DBusMessage* msg;
1.353 + DBusObjectPathVTable vtable =
1.354 + {
1.355 + NULL,NULL,NULL
1.356 + };
1.357 +
1.358 + dbus_error_init(&error);
1.359 + dbus_error_init(&error1);
1.360 +
1.361 + connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
1.362 +
1.363 + if(dbus_error_is_set(&error))
1.364 + {
1.365 + fprintf(stdout, "Error Occured :: %s", error.name);
1.366 + return 1;
1.367 + }
1.368 +
1.369 +
1.370 +
1.371 + if(dbus_connection_register_object_path (connection, "/Test/Method/Object", &vtable, NULL))
1.372 + {
1.373 + fprintf(stdout, "Object Path registered.");
1.374 + }
1.375 + else
1.376 + {
1.377 + fprintf(stdout, "Object Path not able to register.");
1.378 + return 1;
1.379 + }
1.380 +
1.381 +
1.382 +
1.383 + if(dbus_bus_request_name (connection, TEST_BUS_NAME_TEMP, DBUS_NAME_FLAG_ALLOW_REPLACEMENT, &error1) == -1)
1.384 + {
1.385 + fprintf(stdout, "Not able to request name.");
1.386 + }
1.387 + else
1.388 + {
1.389 + fprintf(stdout, "Name Request Successful");
1.390 + }
1.391 +
1.392 + while(TRUE)
1.393 + {
1.394 + dbus_connection_read_write(connection, 0);
1.395 +
1.396 + msg = dbus_connection_pop_message(connection);
1.397 +
1.398 + if(msg == NULL)
1.399 + {
1.400 + sleep(1);
1.401 + continue;
1.402 + }
1.403 +
1.404 + fprintf(stdout, "Message Detected");
1.405 +
1.406 + if(DBUS_MESSAGE_TYPE_SIGNAL == dbus_message_get_type(msg))
1.407 + {
1.408 + fprintf(stdout, "Message is Signal.");
1.409 + }
1.410 +
1.411 + if(DBUS_MESSAGE_TYPE_METHOD_CALL == dbus_message_get_type(msg))
1.412 + {
1.413 + fprintf(stdout, "Message is Method call.");
1.414 + }
1.415 +
1.416 +
1.417 + if(dbus_message_is_method_call(msg, "test.Method.Call", "method"))
1.418 + {
1.419 + called_method(msg, connection);
1.420 + break;
1.421 + }
1.422 +
1.423 + if(dbus_message_is_method_call(msg, "test.Method.Call", "variable_args_method"))
1.424 + {
1.425 +
1.426 + called_variable_args_method(msg, connection);
1.427 + break;
1.428 + }
1.429 +
1.430 + if(dbus_message_is_method_call(msg, "test.Method.Call", "simple"))
1.431 + {
1.432 +
1.433 + called_simple(msg, connection);
1.434 + // break;
1.435 + }
1.436 + if(dbus_message_is_method_call(msg, "test.Method.Call", "file_send"))
1.437 + {
1.438 +
1.439 + called_file_send(msg, connection);
1.440 + // break;
1.441 + }
1.442 + if(dbus_message_is_method_call(msg, "test.Method.Call", "sign_test"))
1.443 + {
1.444 +
1.445 + called_sign_test(msg, connection);
1.446 + break;
1.447 + }
1.448 + if(dbus_message_is_method_call(msg, "test.Method.Call", "file_send_whole"))
1.449 + {
1.450 +
1.451 + called_file_send_whole(msg, connection);
1.452 + break;
1.453 + }
1.454 +
1.455 + if(dbus_message_is_method_call(msg, "test.Method.Call", "file_send_time"))
1.456 + {
1.457 +
1.458 + called_file_send_time(msg, connection);
1.459 + // break;
1.460 + }
1.461 +
1.462 + dbus_message_unref(msg);
1.463 +
1.464 + }
1.465 +
1.466 + dbus_message_unref(msg);
1.467 + dbus_bus_release_name(connection,TEST_BUS_NAME_TEMP,&error1);
1.468 + dbus_connection_unregister_object_path(connection,"/Test/Method/Object");
1.469 +
1.470 + dbus_connection_unref(connection);
1.471 +
1.472 + return 0;
1.473 +}