oFono学习笔记——oFono启动代码分析

摘要

主要说明oFono启动流程。

1.启动分析

 1 int main(int argc, char **argv)
 2 {
 3     /*....*/
 4     g_dbus_set_disconnect_function(conn, system_bus_disconnected,
 5                     NULL, NULL);
 6 
 7     __ofono_dbus_init(conn);/*(1)*/
 8 
 9     __ofono_modemwatch_init();/*(2)*/
10 
11     __ofono_manager_init();/*(3)*/
12 
13     __ofono_plugin_init(option_plugin, option_noplugin);/*(4)*/
14 
15     g_free(option_plugin);
16     g_free(option_noplugin);
17 
18     g_main_loop_run(event_loop);/*(5)*/
19 
20     __ofono_plugin_cleanup();
21 
22     __ofono_manager_cleanup();
23 
24     __ofono_modemwatch_cleanup();
25 
26     __ofono_dbus_cleanup();
27     dbus_connection_unref(conn);
28 
29 cleanup:
30     g_source_remove(signal);
31 
32     g_main_loop_unref(event_loop);
33 
34     __ofono_log_cleanup();
35 
36     return 0;
37 }

上面摘选了oFono中main函数部分内容。oFono的初始化,包括了五大部分。分别为

1.初始化D-BUS

2.初始化modem监控列表,oFono的监控机制主要就是依靠watchlist来实现的,除了modemwatch之外,还有很多其他不同种类的watchlist。在以后的文章中会做介绍,现在我们只要知道这是一个列表就可以了。

3.初始化oFono manager

4.初始化oFono Plugins

5.激活mainloop,使得oFono进入事件循环。

2. plugin加载细节

 1 int __ofono_plugin_init(const char *pattern, const char *exclude)
 2 {
 3     gchar **patterns = NULL;
 4     gchar **excludes = NULL;
 5     GSList *list;
 6     GDir *dir;
 7     const gchar *file;
 8     gchar *filename;
 9     unsigned int i;
10 
11     DBG("");
12 
13     if (pattern)
14         patterns = g_strsplit_set(pattern, ":, ", -1);
15 
16     if (exclude)
17         excludes = g_strsplit_set(exclude, ":, ", -1);
18         
19     /*先载入built-in plugins*/
20     for (i = 0; __ofono_builtin[i]; i++) {
21         if (check_plugin(__ofono_builtin[i],
22                     patterns, excludes) == FALSE)
23             continue;
24 
25         add_plugin(NULL, __ofono_builtin[i]);
26     }
27 
28     /*给定文件夹中的plugins*/
29     dir = g_dir_open(PLUGINDIR, 0, NULL);
30     if (dir != NULL) {
31         while ((file = g_dir_read_name(dir)) != NULL) {
32             void *handle;
33             struct ofono_plugin_desc *desc;
34 
35             if (g_str_has_prefix(file, "lib") == TRUE ||
36                     g_str_has_suffix(file, ".so") == FALSE)
37                 continue;
38 
39             filename = g_build_filename(PLUGINDIR, file, NULL);
40 
41             handle = dlopen(filename, RTLD_NOW);
42             if (handle == NULL) {
43                 ofono_error("Can't load %s: %s",
44                             filename, dlerror());
45                 g_free(filename);
46                 continue;
47             }
48 
49             g_free(filename);
50 
51             desc = dlsym(handle, "ofono_plugin_desc");
52             if (desc == NULL) {
53                 ofono_error("Can't load symbol: %s",
54                                 dlerror());
55                 dlclose(handle);
56                 continue;
57             }
58 
59             if (check_plugin(desc, patterns, excludes) == FALSE) {
60                 dlclose(handle);
61                 continue;
62             }
63 
64             if (add_plugin(handle, desc) == FALSE)
65                 dlclose(handle);
66         }
67 
68         g_dir_close(dir);
69     }
70         
71     /*初始化所有成功加载的plugins*/
72     for (list = plugins; list; list = list->next) {
73         struct ofono_plugin *plugin = list->data;
74 
75         if (plugin->desc->init() < 0)
76             continue;
77 
78         plugin->active = TRUE;
79     }
80 
81     g_strfreev(patterns);
82     g_strfreev(excludes);
83 
84     return 0;
85 }

从代码中可以发现,plugin的初始化分为三个部分。首先,oFono会将所有的built-in plugin加载到plugins列表中。接下来会检查在指定的路径下是否有合法的plugin存在,如有存在又合法的plugin,也将他们加载到plugins列表中。最后,依次对各个plugin进行初始化。

plugin的初始化工作一般是将plugin中包含的驱动(modem/atom)注册到oFono中去。当然,plugin的初始化并不只局限与上诉的功能。利用plugin的初始化,我们还可以扩展oFono的功能等,比如实现设备的热插拔检测等。

posted @ 2013-07-04 15:50  ZHX_1Q89  阅读(1455)  评论(0编辑  收藏  举报