分享

Android-vold源码分析之runCommand(7)

 free_light 2014-06-23
作者:gzshun. 原创作品,转载请标明出处!

说了这么多,该到执行命令的函数了,要不黄花菜都凉了。上一篇文章讲了vold如何开启接收framework下发命令的线程,最终到了runCommand函数的实现,总共有6个版本,由于重复性大,只讲VolumeCmd类的runCommand函数的实现。
VolumeCmd可以说是最重要的,该类的实现处理了list,debug,mount,unmount,format,unshare,shared等等操作,详细说明:
list: 在启动vold之后,接收的第一条命令就是list,这里是获取系统的所有磁盘对象,一般只有sd卡。
debug: 设置USB的调试模式
mount: 挂载磁盘
unmount: 卸载磁盘
format: 格式化磁盘
unshare: 关闭USB的大容量存储模式,相当于断开手机与电脑的连接
shared: 开启USB的大容量存储模式,相当于手机与电脑连接,并挂载在电脑

下面把该函数的源码贴出来,一目了然,程序员,代码说话:

  1. int CommandListener::VolumeCmd::runCommand(SocketClient *cli,  
  2.                                                       int argc, char **argv) {  
  3.     dumpArgs(argc, argv, -1);  
  4.   
  5.     if (argc < 2) {  
  6.         cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);  
  7.         return 0;  
  8.     }  
  9.   
  10.     VolumeManager *vm = VolumeManager::Instance();  
  11.     int rc = 0;  
  12.   
  13.     if (!strcmp(argv[1], "list")) {  
  14.         return vm->listVolumes(cli);  
  15.     } else if (!strcmp(argv[1], "debug")) {  
  16.         if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) {  
  17.             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false);  
  18.             return 0;  
  19.         }  
  20.         vm->setDebug(!strcmp(argv[2], "on") ? true : false);  
  21.     } else if (!strcmp(argv[1], "mount")) {  
  22.         if (argc != 3) {  
  23.             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false);  
  24.             return 0;  
  25.         }  
  26.         rc = vm->mountVolume(argv[2]);  
  27.     } else if (!strcmp(argv[1], "unmount")) {  
  28.         if (argc < 3 || argc > 4 || (argc == 4 && strcmp(argv[3], "force"))) {  
  29.             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force]", false);  
  30.             return 0;  
  31.         }  
  32.   
  33.         bool force = false;  
  34.         if (argc >= 4 && !strcmp(argv[3], "force")) {  
  35.             force = true;  
  36.         }  
  37.         rc = vm->unmountVolume(argv[2], force);  
  38.     } else if (!strcmp(argv[1], "format")) {  
  39.         if (argc != 3) {  
  40.             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path>", false);  
  41.             return 0;  
  42.         }  
  43.         rc = vm->formatVolume(argv[2]);  
  44.     } else if (!strcmp(argv[1], "share")) {  
  45.         if (argc != 4) {  
  46.             cli->sendMsg(ResponseCode::CommandSyntaxError,  
  47.                     "Usage: volume share <path> <method>", false);  
  48.             return 0;  
  49.         }  
  50.         rc = vm->shareVolume(argv[2], argv[3]);  
  51.     } else if (!strcmp(argv[1], "unshare")) {  
  52.         if (argc != 4) {  
  53.             cli->sendMsg(ResponseCode::CommandSyntaxError,  
  54.                     "Usage: volume unshare <path> <method>", false);  
  55.             return 0;  
  56.         }  
  57.         rc = vm->unshareVolume(argv[2], argv[3]);  
  58.     } else if (!strcmp(argv[1], "shared")) {  
  59.         bool enabled = false;  
  60.         if (argc != 4) {  
  61.             cli->sendMsg(ResponseCode::CommandSyntaxError,  
  62.                     "Usage: volume shared <path> <method>", false);  
  63.             return 0;  
  64.         }  
  65.   
  66.         if (vm->shareEnabled(argv[2], argv[3], &enabled)) {  
  67.             cli->sendMsg(  
  68.                     ResponseCode::OperationFailed, "Failed to determine share enable state", true);  
  69.         } else {  
  70.             cli->sendMsg(ResponseCode::ShareEnabledResult,  
  71.                     (enabled ? "Share enabled" : "Share disabled"), false);  
  72.         }  
  73.         return 0;  
  74.     } else {  
  75.         cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false);  
  76.     }  
  77.   
  78.     if (!rc) {  
  79.         cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false);  
  80.     } else {  
  81.         int erno = errno;  
  82.         rc = ResponseCode::convertFromErrno();  
  83.         cli->sendMsg(rc, "volume operation failed", true);  
  84.     }  
  85.   
  86.     return 0;  
  87. }  

每次操作完成后,都要将操作结果汇报给framework,阶级负责工作,一定要把工作结果汇报给领导,这里使用了sendMsg函数。
1.搜索需要挂载的磁盘对象信息列表。先从listVolumes函数开始,贴源码:

  1. int VolumeManager::listVolumes(SocketClient *cli) {  
  2.     VolumeCollection::iterator i;  
  3.   
  4.     for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {  
  5.         char *buffer;  
  6.         asprintf(&buffer, "%s %s %d",  
  7.                  (*i)->getLabel(), (*i)->getMountpoint(),  
  8.                  (*i)->getState());  
  9.         cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);  
  10.         free(buffer);  
  11.     }  
  12.     cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);  
  13.     return 0;  
  14. }  

这函数扫描了mVolumes容器,里面保存着从配置文件/etc/vold.fstab解析出来的磁盘。
该容器是在main函数中添加的:vm->addVolume(dv);
这函数先将每个磁盘的标签,挂载点,状态汇报给framework,然后再循环工作结束后,再次向领导汇报工作结束。
2.设置调试模式。贴源码:

  1. void VolumeManager::setDebug(bool enable) {  
  2.     mDebug = enable;  
  3.     VolumeCollection::iterator it;  
  4.     for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {  
  5.         (*it)->setDebug(enable);  
  6.     }  
  7. }  

每个Volume对象都有一个setDebug函数设置调试状态:

  1. void Volume::setDebug(bool enable) {  
  2.     mDebug = enable;  
  3. }  

挂载磁盘涉及到很多内容和源码,将在下一篇文章继续说明,待续。。。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多