Step 5: Handle Session Notifications (Optional) (Compact 7)
3/12/2014
MTP Responder supports notifications to inform your device about the status of the connection with the MTP initiator. You can use notifications to help your device react to changes in the MTP session state and to authenticate incoming TCP/IP connections from MTP initiators.
When an MTP responder interacts with an MTP initiator, the MTP responder must have exclusive access to the media so that all content can be properly synchronized with the MTP initiator. Therefore, you must coordinate content access between the MTP responder and other applications on your device.
When MTP Responder connects to an MTP initiator, it generates a notification to inform your device that an MTP session has started. When your device receives this notification, it should do the following:
- Pause user interactions with any applications that access media files to prevent unsynchronized changes in this content.
- Ensure that all content file handles are unlocked and closed.
When the MTP session ends, MTP Responder generates a notification to indicate that the MTP connection is no longer active.
To register for MTP session notifications, you create named event objects for each event type and store the name of each event object in the MTP Responder registry location for that notification type. You can create a notification thread that waits on these event objects and then reacts to the change in MTP session state when the thread becomes active.
Specifically, for MTP session notifications, create two named event objects and store these names in the SessionActiveNamedEvent and SessionInactiveNamedEvent settings in the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\MTP\Responder.
These settings are described in the following table
Session Event Settings
Name | Type | Default value | Description |
---|---|---|---|
SessionActiveNamedEvent |
REG_SZ |
L"MTP_SESSION_ACTIVE" |
The name of the event object to be notified when MTP Responder connects to an MTP initiator. |
SessionInactiveNamedEvent |
REG_SZ |
L"MTP_SESSION_INACTIVE" |
The name of the event object to be notified when MTP Responder disconnects from the MTP initiator. |
The following list describes the sequence of steps that will handle session notifications between the device and MTP Responder [or whatever is correct].
- Populate the registry using the keys shown in the preceding table for the session active and session inactive events or use the default session named events.
- Create an application to perform steps 3 through 10.
- Read the registry values for the two events.
- Call CreateEvent to create events for MTP Responder connect (session active) and disconnect (session inactive), and an event for notification thread shutdown.
- Use CreateThread to create a notification thread that executes in a continuous loop, activates the shutdown event when a shutdown request (like pressing a key) is received, and then exits.
- Call WaitForMultipleObjects to wait on the shutdown event and the session active event. When MTP Responder connects, it activates the session active event and the event remains activated for the duration of the session, so that your application will receive the event notification even if it starts after the event is activated.
- Perform the actions needed for session activation.
- Call WaitForMultipleObjects to wait on the shutdown event and the session inactive event. When MTP Responder disconnects, it activates the session inactive event and the event remains activated. Your application will wait on the session active event if it starts after the inactive event is activated.
- Perform the actions needed for session inactivation.
- Repeat from step 6.
After you register for MTP session notifications, go to Step 6: Handle MTP IP Authentication (Optional).