Hi, Michael,
I've updated the patch:
  - add tramp-adb-connect-if-not-connected feature
  - remind user if unnecessary port number is provided in USB mode
  - discover port number for user if port number is not provided in TCP mode

Tests result is:

Ran 35 tests, 26 results as expected, 1 unexpected, 8 skipped (2014-11-11
08:48:17+0800)
1 expected failures

1 unexpected results:
   FAILED  tramp-test31-utf8

8 skipped results:
  SKIPPED  tramp-test20-file-modes
  SKIPPED  tramp-test21-file-links
  SKIPPED  tramp-test22-file-times
  SKIPPED  tramp-test27-start-file-process
  SKIPPED  tramp-test28-shell-command
  SKIPPED  tramp-test29-vc-registered
  SKIPPED  tramp-test30-special-characters
  SKIPPED  tramp-test32-asynchronous-requests

Best regards,
Zhongwei

On Sun, Nov 9, 2014 at 9:43 PM, Ashi <[email protected]> wrote:

> Hi, Michael,
>
> On Sun, Nov 9, 2014 at 5:37 PM, Michael Albinus <[email protected]>
> wrote:
>
>> Ashi <[email protected]> writes:
>>
>> > Hi, Michael,
>>
>> Hi Zhongwei,
>>
>> > Yes, there is no adb command can be used to detect which mode it is
>> > in.
>> >
>> > I'll update my patch by searching the port number instead of searching
>> > the ipv4 address.
>>
>> Yep, let's use this as indication.
>>
>> >     That's OK. The listed port number shall be used than.
>> >
>> >     Could it be, that one could access a device twice, on different
>> >     ports?
>> >
>> > Yes, but it only can be done sequentially. For example:
>> > $adb connect 192.168.1.162:5555 //adb is connected to device's 5555
>> > port
>> > $adb -s 192.168.1.162:5555 tcpip 5554 //restart device's adb daemon,
>> > let it listen on 5554 port. And adb will be disconnected on 5555 port
>> > now.
>> > $adb connect 192.168.1.162:5554 //adb is connected to devices's 5554
>> > port
>>
>> So we have no problem. Per devices there can be exactly one tcpip
>> connection only at a given time.
>
>
>> >     Furtheron, I found some other use cases which are not handled yet,
>> >     I believe. If a device is not listed in "adb devices", Tramp shall
>> >     first connect to that device with "adb connect
>> >     device:port". Afterwards, Tramp could access to this device via
>> >     "adb -s device:port shell", as it does already for usb devices.
>> >
>> > But if that device can not be connected, user has to wait adb connect
>> > until timeout (about 10s). It could be annoying if user realize the
>> > device name is wrong and want to try again immediately. What do you
>> > think about it?
>>
>> Yes, this would be bad. So let the user control, whether "adb connect ..."
>> is called. Either we ask interactively, or we install a defcustom
>> tramp-adb-connect-if-not-connected, which is set to nil by default.
>>
>
> Agree, adding a defcustom makes sense here.
>
>>
>> > Best regards,
>> > Zhongwei
>>
>> Best regards, Michael.
>>
> Best regards,
> Zhongwei
>
>
diff --git a/lisp/tramp-adb.el b/lisp/tramp-adb.el
index 1a7a34f..2a16066 100644
--- a/lisp/tramp-adb.el
+++ b/lisp/tramp-adb.el
@@ -45,6 +45,13 @@
   :version "24.4"
   :type 'string)
 
+(defcustom tramp-adb-connect-if-not-connected nil
+  "Try to run adb connect if provided device is not connected currently. It is
+in tramp-adb-get-host-for-execution."
+  :group 'tramp
+  :version "24.4"
+  :type '(choice (const nil) (const t)))
+
 ;;;###tramp-autoload
 (defconst tramp-adb-method "adb"
   "*When this method name is used, forward all calls to Android Debug Bridge.")
@@ -991,11 +998,60 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
 	  (tramp-set-connection-property v "process-buffer" nil))))))
 
 ;; Helper functions.
+(defun tramp-adb-search-host-in-devices (host devices)
+  "Returns host:port string by searching host in the devices list. If
+failed, return nil."
+  (if (null devices) nil
+    (if (string-match (concat "^" host) (car devices))
+        (car devices)
+      (tramp-adb-search-host-in-devices host (cdr devices)))))
+
+(defun tramp-adb-get-host-for-execution (vec)
+  "Returns full host name from VEC to be used in shell exceution.
+E.g. a host name \"192.168.1.1#5555\" returns \"192.168.1.1:5555\"
+     a host name \"R38273882DE\" returns \"R38273882DE\"."
+  (let* ((port (tramp-file-name-port vec))
+         (host (tramp-file-name-real-host vec))
+         (exe-name (if port
+                       (format "%s:%s" host port)
+                     host))
+         (devices (mapcar 'cadr (tramp-adb-parse-device-names nil))))
+    ;; Checking whether exe-name is in devices. Follow cases are checked:
+    ;;  - yes, correct exe-name returns.
+    ;;  - not, true device name is: xxxx
+    ;;    user types: xxxx:port-number. Error returns with tips of correct name
+    ;;  - not, true device name is: xxxx:port-number
+    ;;    user types: xxxx. Tramp will auto-complete port-number for user
+    ;;  - not, no device name is similar as user gives. if
+    ;;    tramp-adb-connect-if-not-connected is t, try to connect that device,
+    ;;    else return error
+    (cond ((member exe-name devices) exe-name)
+          ((member host devices) (tramp-error
+                                  vec 'file-error
+                                  "Port number is not needed for device %s"
+                                  host))
+          ((tramp-adb-search-host-in-devices host devices))
+          (tramp-adb-connect-if-not-connected
+           ;; Try to connect exe-name device.
+           (if (tramp-adb-execute-adb-command
+                '["adb" nil "" "/" nil] "connect" exe-name)
+               (tramp-error vec 'file-error "Could not connect %s" exe-name)
+             ;; If port is not provided, adb will connect to default port number
+             ;; 5555, which must be appended to return to get a full host name.
+             (if port
+                 exe-name
+               (format "%s:%s" host "5555"))))
+          (t (tramp-error
+              vec
+              'file-error
+              "Could not find device %s"
+              exe-name)))))
 
 (defun tramp-adb-execute-adb-command (vec &rest args)
   "Returns nil on success error-output on failure."
-  (when (> (length (tramp-file-name-host vec)) 0)
-    (setq args (append (list "-s" (tramp-file-name-host vec)) args)))
+  (let ((host (tramp-file-name-host vec)))
+    (when (> (length host) 0)
+      (setq args (append (list "-s" (tramp-adb-get-host-for-execution vec)) args))))
   (with-temp-buffer
     (prog1
 	(unless
@@ -1097,9 +1153,12 @@ connection if a previous connection has died for some reason."
   (let* ((buf (tramp-get-connection-buffer vec))
 	 (p (get-buffer-process buf))
 	 (host (tramp-file-name-host vec))
+         (exe-host (tramp-adb-get-host-for-execution vec))
 	 (user (tramp-file-name-user vec))
-	 devices)
-
+         ;; Adding the exe-host to devices ensures a new connected exe-host
+         ;; is in devices. Because tramp-adb-parse-device-names can't
+         ;; discover new connected device.
+	 (devices (list exe-host)))
     ;; Maybe we know already that "su" is not supported.  We cannot
     ;; use a connection property, because we have not checked yet
     ;; whether it is still the same device.
@@ -1110,10 +1169,10 @@ connection if a previous connection has died for some reason."
 	(and p (processp p) (memq (process-status p) '(run open)))
       (save-match-data
 	(when (and p (processp p)) (delete-process p))
-	(setq devices (mapcar 'cadr (tramp-adb-parse-device-names nil)))
+	(append devices (mapcar 'cadr (tramp-adb-parse-device-names nil)))
 	(if (not devices)
 	    (tramp-error vec 'file-error "No device connected"))
-	(if (and (> (length host) 0) (not (member host devices)))
+	(if (and (> (length host) 0) (not (member exe-host devices)))
 	    (tramp-error vec 'file-error "Device %s not connected" host))
 	(if (and (> (length devices) 1) (zerop (length host)))
 	    (tramp-error
@@ -1123,7 +1182,7 @@ connection if a previous connection has died for some reason."
 	  (let* ((coding-system-for-read 'utf-8-dos) ;is this correct?
 		 (process-connection-type tramp-process-connection-type)
 		 (args (if (> (length host) 0)
-			   (list "-s" host "shell")
+			   (list "-s" exe-host "shell")
 			 (list "shell")))
 		 (p (let ((default-directory
 			    (tramp-compat-temporary-file-directory)))
_______________________________________________
Tramp-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/tramp-devel

Reply via email to