下一篇:GDBus之dbus服务创建
本篇开发环境搭建:
u系:apt install libglib2.0-dev
一.案例源码结构
### 二. 数据部分源码
beijing.h 文件
#ifndef BEIJING_H
#define BEIJING_H
#include <glib.h> //提供GVariant类型定义
typedef struct _Beijing{
char ** districts; //区
double area; //总面积
double population; //人口
char * other_name; //别名
}Beijing;
extern const char beijing_dbus_server_name[]; //dbus服务名
extern const char beijing_dbus_interface_name[]; //dbus接口名
extern const char beijing_dbus_object_name[]; //dbus对象名
extern const char property_interface_name[]; //property接口名
//用于创建dbus信息的关键xml数据
extern const char beijing_node_xml[];
Beijing* beijing_node_new(); //创建并返回一个Beijing结构体对象信息
void beijing_node_free(Beijing* beijing); //释放Beijing结构体对象信息
GVariant* beijing_districts_dbus_data_new(Beijing* beijing); //创建并以数组形式返回北京所有区
#endif
beijing.c 文件
#include "beijing.h"
#include <glib.h>
#include <gio/gio.h>
#include <stdio.h>
const char beijing_dbus_server_name[] = {"world.china.beijing.test"}; //dbus服务名
const char beijing_dbus_interface_name[] = {"world.china.beijing"}; //dbus接口名
const char beijing_dbus_object_name[] = {"/world/china/beijing"}; //dbus对象名
const char property_interface_name[] = {"org.freedesktop.DBus.Properties"}; //property接口名
//用于创建dbus信息的关键xml数据
const char beijing_node_xml[] =
"<node>"
" <interface name='world.china.beijing'>" //指定dbus接口名
" <method name='GetAllDistrict'>" //dbus Method
" <arg name='districts' type='as' direction='out' />" //<!-- 以Array形式输出北京所有的区 -->
" </method>"
" <method name='SetPopulation'>"
" <arg name='new_counts' type='d' direction='in' />" //<!-- 该方法用于修改'Population'属性的值 -->
" </method>"
// <!-- read表示该属性只可读,readwrite表示该属性可读可写 -->
" <property name='Area' type='d' access='read' />" //<!-- 以Double形式输出Beijing的总面积 -->
" <property name='OtherName' type='s' access='readwrite' />" //<!-- 以String形式输出Beijing的其他名字 -->
" <property name='Population' type='d' access='readwrite' />"//<!-- 以Double形式输出Beijing的总人口(万) -->
" <signal name='PopulationChanged' />" //<!-- Beijing总人口熟了改变的信号 -->
" </interface>"
"</node>";
char dist[16][64] = {"密云区","延庆区","朝阳区","海淀区","丰台区","石景山区",
"门头沟区","房山区","通州区","顺义区","昌平区","大兴区",
"怀柔区","平谷区","东城区","西城区"};
Beijing *beijing_node_new(){
int index = -1;
char* tmp = NULL;
Beijing * beijing = NULL;
beijing = g_new0(Beijing, 1);
beijing->districts = (char**)g_new0(char*, 16);//二位数组
//在堆区初始化北京所有的区
for(index=0; index<16; ++index){
tmp = (char*)g_new0(char, 64);
strncpy(tmp,dist[index],64);
beijing->districts[index] = tmp;
tmp = NULL;
}
for(index=0; index<16; ++index)
printf("%s ",beijing->districts[index]);
printf("\n");
beijing->area = 16410;
beijing->population = 21893095;
beijing->other_name = g_new0(char,64);
strncpy(beijing->other_name, "北平", 64);
return beijing;
}
void beijing_node_free(Beijing* beijing){
int index = -1;
if(!beijing)
return;
//二维数组存放的全是一级指针
for(index=15; index>=0; --index){
g_free(beijing->districts[index]); //g_new0()分配的内存使用g_free()进行释放
beijing->districts[index] = NULL;
}
//释放二级指针
g_free(beijing->districts);
beijing->districts = NULL;
beijing->area = -1;
beijing->population = -1;
g_free(beijing->other_name);
beijing->other_name = NULL;
//释放结构体对象内存
g_free(beijing);
beijing = NULL;
}
GVariant* beijing_districts_dbus_data_new(Beijing* beijing){
int index = -1;
GVariant* variant = NULL;
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as")); //构建dbus类型的数组,用于存放char*的数组
for(index=0; index<16; ++index)
g_variant_builder_add_value(&builder, g_variant_new_string(beijing->districts[index]));//向数组内添加string类型元素
variant = g_variant_new("(as)", &builder);
return variant;
}
三. Makefile文件
#!/bin/bash
CC = gcc
CFLAGS = -g
#依赖gio-2.0库和glib-2.0库
EXTRA_INCLUDE = `pkg-config --cflags gio-2.0 gio-2.0`
EXTRA_LIBS = `pkg-config --libs gio-2.0 glib-2.0`
#所有要生成的二进制
all:server/server property/property-test method/method-test signal/signal-test
#生成server二进制
server/server: data/beijing.o server/server.o server/server-main.o
$(CC) $^ -o $@ $(EXTRA_LIBS)
data/beijing.o:data/beijing.c
$(CC) -c $< $(CFLAGS) -o $@ $(EXTRA_INCLUDE)
#server.c里用到了data/beijing.h里变量的声明,因此需要 -Idata
server/server.o:server/server.c
$(CC) -c $< $(CFLAGS) -o $@ $(EXTRA_INCLUDE) -Idata
#-Idata原因同上
server/server-main.o:server/server-main.c
$(CC) -c $< $(CFLAGS) -o $@ $(EXTRA_INCLUDE) -Idata
#生成property-test二进制
property/property-test:property/property-test.o
$(CC) $< -o $@ $(EXTRA_LIBS)
property/property-test.o:property/property-test.c
$(CC) -c $< -o $@ $(EXTRA_INCLUDE)
#生成method-test二进制
method/method-test:method/method-test.o
$(CC) $< -o $@ $(EXTRA_LIBS)
method/method-test.o:method/method-test.c
$(CC) -c $< -o $@ $(EXTRA_INCLUDE)
#生成signal-test二进制
signal/signal-test:signal/signal-test.o
$(CC) $< -o $@ $(EXTRA_LIBS)
signal/signal-test.o:signal/signal-test.c
$(CC) -c $< -o $@ $(EXTRA_INCLUDE)
.PHONY:clean
clean:
rm data/*.o
rm server/*.o server/server
rm method/*.o method/method-test
rm signal/*.o signal/signal-test
rm property/*.o property/property-test
四. 运行说明
编译成功后保持server
二进制在后台运行,再运行其他二进制,查看不同程序的输出