alexmohr / sonyapilib

Contains a python api to control sony devices.
MIT License
20 stars 11 forks source link

Trouble configuring Sony BDP-S4200 #5

Closed dilruacs closed 5 years ago

dilruacs commented 5 years ago

If I try to use the example code, I get the following errors:

Enter the PIN displayed at your device: 0000
HTTPError: 403 Client Error: Forbidden for url: http://192.168.240.4:52323/dmr.xml
HTTPError: 500 Server Error: Internal Server Error for url: http://192.168.240.4:50002/register?name=SonyApiLib%20Python%20Test&registrationType=initial&deviceId=SonyApiLib%20Python%20Test&wolSupport=true
alexmohr commented 5 years ago

I assume your device did not display a pin because you've entered 0000. I took a look at the example and it seems like a call to the register method was missing. I've updated the readme to include the call. Unfortunately I do not have access to a device until next week to test it. If it still does not work with the updated example please post the content of http://192.168.240.4:52323/Ircc.xml here.

dilruacs commented 5 years ago

Thanks for the quick reaction. Adding the device.register() call did not help either.

My device still does not show a pin and the output looks like this now:

HTTPError: 403 Client Error: Forbidden for url: http://192.168.240.4:52323/dmr.xml
HTTPError: 500 Server Error: Internal Server Error for url: http://192.168.240.4:50002/register?name=SonyApiLib%20Python%20Test&registrationType=initial&deviceId=SonyApiLib%20Python%20Test&wolSupport=true
[W] HTTPError: 500 Server Error: Internal Server Error for url: http://192.168.240.4:50002/register?name=SonyApiLib%20Python%20Test&registrationType=initial&deviceId=SonyApiLib%20Python%20Test&wolSupport=true
Enter the PIN displayed at your device: 0000
HTTPError: 500 Server Error: Internal Server Error for url: http://192.168.240.4:50002/register?name=SonyApiLib%20Python%20Test&registrationType=initial&deviceId=SonyApiLib%20Python%20Test&wolSupport=true

The contents of the Ircc.xml file look like this:

<?xml version="1.0"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
  <specVersion>
    <major>1</major>
    <minor>0</minor>
  </specVersion>
  <device>
    <deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>
    <friendlyName>Blu-ray Disc Player</friendlyName>
    <manufacturer>Sony Corporation</manufacturer>
    <manufacturerURL>http://www.sony.net/</manufacturerURL>
    <modelDescription></modelDescription>
    <modelName>Blu-ray Disc Player</modelName>
    <modelURL></modelURL>
    <UDN>uuid:00000003-0000-1010-8000-fcf1524e7a1e</UDN>
    <iconList>
      <icon>
        <mimetype>image/jpeg</mimetype>
        <width>120</width>
        <height>120</height>
        <depth>24</depth>
        <url>/bdp_ax3d_device_icon_large.jpg</url>
      </icon>
      <icon>
        <mimetype>image/png</mimetype>
        <width>120</width>
        <height>120</height>
        <depth>24</depth>
        <url>/bdp_ax3d_device_icon_large.png</url>
      </icon>
      <icon>
        <mimetype>image/jpeg</mimetype>
        <width>48</width>
        <height>48</height>
        <depth>24</depth>
        <url>/bdp_ax3d_device_icon_small.jpg</url>
      </icon>
      <icon>
        <mimetype>image/png</mimetype>
        <width>48</width>
        <height>48</height>
        <depth>24</depth>
        <url>/bdp_ax3d_device_icon_small.png</url>
      </icon>
    </iconList>
    <serviceList>
      <service>
        <serviceType>urn:schemas-sony-com:service:IRCC:1</serviceType>
        <serviceId>urn:schemas-sony-com:serviceId:IRCC</serviceId>
        <SCPDURL>/IRCCSCPD.xml</SCPDURL>
        <controlURL>/upnp/control/IRCC</controlURL>
        <eventSubURL></eventSubURL>
      </service>
    </serviceList>
    <presentationURL></presentationURL>
    <av:X_IRCC_DeviceInfo xmlns:av="urn:schemas-sony-com:av">
      <av:X_IRCC_Version>1.0</av:X_IRCC_Version>
      <av:X_IRCC_CategoryList>
        <av:X_IRCC_Category>
          <av:X_CategoryInfo>AAMAABxa</av:X_CategoryInfo>
        </av:X_IRCC_Category>
      </av:X_IRCC_CategoryList>
    </av:X_IRCC_DeviceInfo>
    <av:X_UNR_DeviceInfo xmlns:av="urn:schemas-sony-com:av">
      <av:X_UNR_Version>1.3</av:X_UNR_Version>
      <av:X_CERS_ActionList_URL>http://192.168.240.4:50002/actionList</av:X_CERS_ActionList_URL>
    </av:X_UNR_DeviceInfo>
    <av:X_RDIS_DeviceInfo xmlns:av="urn:schemas-sony-com:av">
      <av:X_RDIS_Version>1.0</av:X_RDIS_Version>
      <av:X_RDIS_SESSION_CONTROL>false</av:X_RDIS_SESSION_CONTROL>
      <av:X_RDIS_ENTRY_PORT>50004</av:X_RDIS_ENTRY_PORT>
    </av:X_RDIS_DeviceInfo>
  </device>
</root>
alexmohr commented 5 years ago

please post http://192.168.240.4:50002/actionList aswell. Thanks.

alexmohr commented 5 years ago

I've changed the registration URL for devices with API version 2. I'm not sure if this will fix your issue but it was certainly wrong. It might be necessary to put your device in registration mode. The BDP-SD500 for example only allow registration if the remote control menu of the device is open.

dilruacs commented 5 years ago

This is the actionList, I will test your changes and suggestions.

<?xml version="1.0" encoding="UTF-8"?>
<actionList>
  <action name="register" mode="3" url="http://192.168.240.4:50002/register"/>
  <action name="getText" url="http://192.168.240.4:50002/getText"/>
  <action name="sendText" url="http://192.168.240.4:50002/sendText"/>
  <action name="getContentInformation" url="http://192.168.240.4:50002/getContentInformation"/>
  <action name="getSystemInformation" url="http://192.168.240.4:50002/getSystemInformation"/>
  <action name="getRemoteCommandList" url="http://192.168.240.4:50002/getRemoteCommandList"/>
  <action name="getStatus" url="http://192.168.240.4:50002/getStatus"/>
  <action name="getHistoryList" url="http://192.168.240.4:50002/getHistoryList"/>
  <action name="getContentUrl" url="http://192.168.240.4:50002/getContentUrl"/>
  <action name="sendContentUrl" url="http://192.168.240.4:50002/sendContentUrl"/>
</actionList>
dilruacs commented 5 years ago

I installed your bugfix branch with pip like this: pip install git+https://github.com/alexmohr/sonyapilib.git@bugfix/Trouble-configuring-Sony-BDP-S4200

And noticed:

[…]
Installing collected packages: sonyapilib
  Found existing installation: sonyapilib 0.3.10
    Uninstalling sonyapilib-0.3.10:
      Successfully uninstalled sonyapilib-0.3.10
Successfully installed sonyapilib-0.3.8

Is there a small slip in the version numbers somewhere?

dilruacs commented 5 years ago

The symptoms are still unchanged with your bugfix branch. I understand that remote troubleshooting is quite difficult. If you need me to attach more output or fetch more XML from the device, just tell me. I could also send you a tcpdump of the registration process, if that helps.

Also: I do not have a menu item that is called "remote control", there are several items under the network settings such as "Connection server settings", "Registered server settings" etc.

alexmohr commented 5 years ago

The last released version has been made from the dev branch therefore the versioning is a little off, I've noticed that too late.

It is odd that you encounter these issues as your device is API version 3 which is the same as the device I have used for testing.

I believe this is an error in the registration process. Did you follow the manual for registration of a new device? You can find it at: http://info.tvsideview.sony.net/en_ww/faq/regist_bdp_wfd.html Scroll down to "Sony Blu-ray Disc Player / Sony Home Theatre System (Released in 2011-2012)" The menu entry I am refering to is called "Media Remote Device Registration" It is possible that you have no such entry in the menu because it has to be enabled somewhere in the device settings.

If you are trying it again please do use version 0.3.10

dilruacs commented 5 years ago

The menu of my Blu-ray Player is missing this exact entry. The latest manual that can be downloaded from Sony for this device startes that "TV Sideview" is needed and that the device can be registered from the Home screen only.

My device is still using Firmware M20.R.0164, which is from 2014/2015. I am unable to update the firmware as I have to confirm the update, which does not work with my HDMI-CEC remote control and I am unable to locate the original remote for the player at the moment. I don't know if a firmware update is needed, but I will try to locate the original remote and update the device.

dilruacs commented 5 years ago

I was able to connect to my player with the Android App and ran tcpdump during the registration process.

alexmohr commented 5 years ago

This project is using the same interface as the app so the registration process should at least be started. It would be great if you could post the tcpdump. Probably it will take until next week when I have access to a similar device to reproduce the problem.

dilruacs commented 5 years ago

I will unregister the device, start with a clean slate and commit the tcpdump to my github repo.

I had to do the packet capture on my access point and my mobile phone roamed to another access point halfway through the registration process so the dump I currently have is incomplete. That's what happens if you add too many devices/features to your home network ;-)

dilruacs commented 5 years ago

You can find the tcpdump here: registration.pcap.gz

alexmohr commented 5 years ago

The firmware you are using seems to implement a different authentication mechanism. The later firmware versions use the pin base 64 encoded in the http header or a cookie in case of version 4. Your device encodes the authentication information in the header but I have not figured out how this string is build. In https://github.com/KHerron/SonyAPILib/blob/master/SonyAPILib/SonyAPILib/sonyAPILib.cs this string is build from the mac address which is also wrong because neither the mac address of the server nor the client matches this string. Nevertheless starting the registration process should work.

The process is started in the app by this http get GET /register?name=Nexus+6+%28Video+%26+TV+SideView%29&registrationType=initial&deviceId=TVSideView%3A0a367a16-3a81-43aa-95e4-d59f4acbd81b&wolSupport=true HTTP/1.1 The python api will make nearly the same call expect the name and the deviceId parameter will be the same string. Most probable this the cause of the problem. Before figuring out how the deviceId string is build please update the firmware of your device to the latest version. If the problem persists it would be great if you could post a tcpdump for the registration with the python script. This should make it a lot easier to find the problem.

Thanks for your contribution :)

dilruacs commented 5 years ago

After I first succeeded registering the App with my Blu-ray player, I was able to update the firmware on my device.

The tcpdump I uploaded was made with the newest firmware available for my device: M20.R.0181

Both the Lrcc.xml and the actionList were completely the same before and after the update.

I will create a tcpdump for the registration with the python script add a link to it here.

dilruacs commented 5 years ago

registration-apilib.pcap.gz

dilruacs commented 5 years ago

The string 0a367a16-3a81-43aa-95e4-d59f4acbd81b is not a mac address, but a RFC-4122 UUID. One can generate one with the python-uuid library, e.g. by using the uuid.uuid4() function. Maybe the device expects the nickname to contain ":" followed by a unique string (in this case a UUID, or a MAC address in the KHerron implementation) to identify the client that wants to register itself?

dilruacs commented 5 years ago

I uploaded a fresh tcpdump here: registration.pcap.gz This is a second mobile phone using the Android App. I filtered on source and target with the previous tcpdump. Some relevant UDP and broadcast traffic might have been left out.

I noticed that each client device gets its own UUID at the registration.

I tried to replay the requests from the capture by hand using curl, mimicing the headers as close as possible.

curl […] http://bluray:50201/dial.xml
# HTTP/1.1 200 OK
curl […] http://bluray:50001/Ircc.xml
# HTTP/1.1 200 OK
curl […] http://bluray:50002/actionList
# HTTP/1.1 200 OK
curl […] http://bluray:52323/dmr.xml
# HTTP/1.1 403 Forbidden

It tries a few other files under port 52323, I get 403 Forbidden for all of them if I do it on my own. If I try the registration URL from the packet dump I get a 500 Internal Server Error; even when I set the additional headers X-CERS-DEVICE-INFO and X-CERS-DEVICE-ID and using a UUID in both the request and the device-id header.

There is a lot of UDP traffic coming from the Blu-ray player (UPnP) and even a HTTP request from the Blu-ray player to my mobile phone (target port 5000): NOTIFY /GENA/CALLBACK/PATH HTTP/1.1

I fail to understand what is missing and why the GET /register[…] is failing at this moment.

alexmohr commented 5 years ago

Great idea trying to replicate the communication with curl. I think The uuid is solely used to generate a unique identifier for the device ID and is not that important for the authentication. Your tests with curl kind of confirm that it is not failing due to the device id. It is strange that access to dmr.xml is denied because the android app is requesting this before the actual authentication takes part. This means there must be another way to identify the device as valid. I would have guessed it is something the app sets in the http headers but as it doesn't work with curl it must be something different. I do not think the udp is relevant to this but I will take a closer look at it maybe I can find something.

dilruacs commented 5 years ago

I solved the problem.

Your example code works fine. I was able to register my Blu-ray player.

The cause was that the player and the vm weren't on the same subnet. Even though the firewall was completely open between the source and destination IP, the Sony Firmware checks the source-IP and throws a HTTP/1.1 403 Forbidden when accessing http://<device>:52323/dmr.xml and a HTTP/1.1 500 Internal Server Errror when accessing http://<device>:50002/register[…]

The Android App was able to connect without problems because I put it in a WLAN that was on the same subnet as the player.

I hope I did not waste too much of your time ;-) Will continue testing the Home Assistant component with my device now.

alexmohr commented 5 years ago

Not at all I'm glad you found the issue and happy to hear that someone is taking use out of this project :)

hinglau886 commented 9 months ago

這是,我將測試您的更改和建議。actionList

<?xml version="1.0" encoding="UTF-8"?>
<actionList>
  <action name="register" mode="3" url="http://192.168.240.4:50002/register"/>
  <action name="getText" url="http://192.168.240.4:50002/getText"/>
  <action name="sendText" url="http://192.168.240.4:50002/sendText"/>
  <action name="getContentInformation" url="http://192.168.240.4:50002/getContentInformation"/>
  <action name="getSystemInformation" url="http://192.168.240.4:50002/getSystemInformation"/>
  <action name="getRemoteCommandList" url="http://192.168.240.4:50002/getRemoteCommandList"/>
  <action name="getStatus" url="http://192.168.240.4:50002/getStatus"/>
  <action name="getHistoryList" url="http://192.168.240.4:50002/getHistoryList"/>
  <action name="getContentUrl" url="http://192.168.240.4:50002/getContentUrl"/>
  <action name="sendContentUrl" url="http://192.168.240.4:50002/sendContentUrl"/>
</actionList>