Add a sample guide for the added usb host functions 28/146028/2
authorlokilee73 <changjoo.lee@samsung.com>
Thu, 24 Aug 2017 12:27:43 +0000 (21:27 +0900)
committerEditor Lionbridge <TizenEditor.SEL@lionbridge.com>
Fri, 25 Aug 2017 08:47:33 +0000 (11:47 +0300)
PS2: Reviewed

Change-Id: Ia3e095a1a025dd775f2367919f7cb083cc9f4657
Signed-off-by: lokilee73 <changjoo.lee@samsung.com>
org.tizen.guides/html/native/connectivity/usb_host_n.htm [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 62e0809..beee193
@@ -28,6 +28,7 @@
         <ul class="toc">
             <li><a href="#prerequisites">Prerequisites</a></li>
             <li><a href="#find_open">Finding and Opening a Device</a></li>
+            <li><a href="#usb_detection">Monitoring Device Connections</a></li>
             <li><a href="#find_endpoint">Finding the Interface and Endpoints</a></li>
             <li><a href="#transfer">Transferring Data</a></li>
                </ul>
@@ -54,7 +55,7 @@
 <p>You can set up your application for the USB host functionality by initializing the interface context.</p>
 </li>
 <li>Finding and opening a device through a device list
-<p>To use the USB host functionality, you have to <a href="#find_open">find a device and gather information about it</a>.</p>
+<p>To use the USB host functionality, you have to <a href="#find_open">find a device and gather information about it</a>. You can also <a href="#usb_detection">monitor when a USB device is attached or detached</a>.</p>
 </li>
 <li>Finding interface and endpoints
 <p>You can <a href="#find_endpoint">find the correct interface and configuration</a> to connect to a device using USB.</p></li>
@@ -184,6 +185,35 @@ else
 </li>
 </ol>
 
+       <h2 id="usb_detection">Monitoring Device Connections</h2>
+To receive notifications when a USB device is attached or detached:
+<ol>
+<li>Define and register a callback, which is called when the device is attached or detached.
+<p>Define the events you want to monitor in the third parameter of the <code>usb_host_set_hotplug_cb()</code> function. The available values are: <code>USB_HOST_HOTPLUG_EVENT_ATTACH</code>, <code>USB_HOST_HOTPLUG_EVENT_DETACH</code>, and <code>USB_HOST_HOTPLUG_EVENT_ANY</code>.</p>
+<pre class="prettyprint">
+void
+detected_cb(usb_host_device_h ndev, void *data)
+{
+    dlog_print(DLOG_INFO, LOG_TAG, "USB is attached or detached");
+}
+
+int ret;
+usb_host_hotplug_h hotplug;
+
+ret = usb_host_set_hotplug_cb(ctx, detected_cb, USB_HOST_HOTPLUG_EVENT_ANY, NULL, &amp;hotplug);
+if (ret == USB_HOST_ERROR_NONE)
+    dlog_print(DLOG_INFO, LOG_TAG, "USB callback is successfully registered");
+else
+    dlog_print(DLOG_INFO, LOG_TAG, "Unable to register USB callback function");
+</pre>
+</li>
+<li>When the notifications are no longer needed, deregister the callback:
+<pre class="prettyprint">
+usb_host_unset_hotplug_cb(hotplug);
+</pre>
+</li>
+</ol>
+
        <h2 id="find_endpoint">Finding the Interface and Endpoints</h2>
 
 <p>USB communication always happens between the host and one of the endpoints. The endpoints are grouped into interfaces and interfaces into configurations. A device can have multiple configurations, but only one can be active at a time. All interfaces inside an active configuration can be used at the same time.</p>