-
#include <dbus/dbus.h> #include <stdbool.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct _chiMsgType{ int size; char *chiId; }chiMsgType; typedef struct _parMsgType{ int size; char *parId; chiMsgType childMsg; }parMsgType; void sendStruct(){ printf("In dbus struct send function.......\n"); parMsgType parentMsg; parentMsg.size = sizeof(parMsgType); parentMsg.parId = "parent"; parentMsg.childMsg.size = sizeof(chiMsgType); parentMsg.childMsg.chiId = "children"; DBusError err; DBusMessage *msg; DBusMessageIter args; DBusMessageIter subarg; DBusConnection *conn; DBusPendingCall *pending; void *msgContent = malloc(sizeof(parMsgType) + 1); //assert(smgContent); memcpy((char *)msgContent, (char *)&parentMsg, parentMsg.size); dbus_error_init(&err); conn = dbus_bus_get_private(DBUS_BUS_SESSION, &err); if (dbus_error_is_set(&err)){ fprintf(stderr, "Connection Error(%s)\n", err.message); dbus_error_free(&err); } if (NULL == conn){ fprintf(stderr, "connection point to NULL\n"); exit(1); } msg = dbus_message_new_method_call("test.method.server", "/test/method/Object", "test.method.Type", "Method"); if (NULL == msg){ fprintf(stderr, "Message Null\n"); exit(1); } dbus_message_set_no_reply(msg, TRUE); dbus_message_iter_init_append(msg, &args); dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &subarg); dbus_message_iter_append_fixed_array(&subarg, DBUS_TYPE_BYTE, &msgContent, parentMsg.size); dbus_message_iter_close_container(&args, &subarg); if (!dbus_connection_send(conn, msg, NULL)){ fprintf(stderr, "Out Of Memory!\n"); exit(1); } dbus_connection_flush(conn); dbus_message_unref(msg); printf("send a struct : \nparent size : %d\n parent ID : %s\nchildren size : %d\nchildren ID : %s\n", parentMsg.size, parentMsg.parId, parentMsg.childMsg.size, parentMsg.childMsg.chiId); } void recvStruct(){ printf("in dbus receive struct function..................\n"); DBusError err; DBusMessage *msg; DBusMessageIter ags; DBusMessageIter subags; DBusConnection *conn; int ret; void *ptr; int n_elements = 0; parMsgType *parentMsg; dbus_error_init(&err); conn = dbus_bus_get_private(DBUS_BUS_SESSION, &err); if (dbus_error_is_set(&err)){ fprintf(stderr, "Connection Error (%s)\n", err.message); dbus_error_free(&err); } if (NULL == conn){ fprintf(stderr, "Connection Null\n"); exit(1); } ret = dbus_bus_request_name(conn, "test.method.server", DBUS_NAME_FLAG_REPLACE_EXISTING, &err); if (dbus_error_is_set(&err)){ fprintf(stderr, "Name Error (%s)\n", err.message); dbus_error_free(&err); } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret){ fprintf(stderr, "Not Primary Owner (%d)\n", ret); exit(1); } int receivetime = 0; while (true){ dbus_connection_read_write(conn, 0); msg = dbus_connection_pop_message(conn); if (NULL == msg){ //usleep(100); continue; } if (dbus_message_is_method_call(msg, "test.method.Type", "Method")){ printf("receive a method call \n"); if (!dbus_message_iter_init(msg, &ags)){ fprintf(stderr, "Message has no arguments\n"); } dbus_message_iter_recurse(&ags, &subags); dbus_message_iter_get_fixed_array(&subags, &ptr, &n_elements); parentMsg = (parMsgType*)ptr; printf("receive a struct : \nparent size : %d\n parent ID : %s\nchildren size : %d\nchildren ID : %s\n", parentMsg->size, parentMsg->parId, parentMsg->childMsg.size, parentMsg->childMsg.chiId); break; } printf("receive time : %d\n", receivetime++); dbus_message_unref(msg); } } int main(int argc, char **argv){ if(0 == strcmp(argv[1], "send")) sendStruct(); else if(0 == strcmp(argv[1], "receive")) recvStruct(); else{ printf("error parameter, restart\n"); exit(1); } }
DBUS 结构体传输示例
猜你喜欢
转载自blog.csdn.net/linuxarmsummary/article/details/88134922
今日推荐
周排行