Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logging to ddp messages #5

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions pyps4/ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import re
import socket
import logging

UDP_IP = '0.0.0.0'
UDP_IP = ''
UDP_PORT = 0

DDP_PORT = 987
DDP_VERSION = '00020020'

_LOGGER = logging.getLogger(__name__)

def get_ddp_message(msg_type, data=None):
"""Get DDP message."""
Expand Down Expand Up @@ -66,18 +68,36 @@ def get_ddp_launch_message(credential):

def _send_recv_msg(host, broadcast, msg, receive=True):
"""Send a ddp message and receive the response."""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
sock.settimeout(3.0)

if broadcast:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
host = '255.255.255.255'

sock.sendto(msg.encode('utf-8'), (host, DDP_PORT))

if receive:
return sock.recvfrom(1024)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except OSError as error:
_LOGGER.error('failed to create socket, %s', error)
return [ None, host ]
try:
sock.bind((UDP_IP, UDP_PORT))
sock.settimeout(3.0)
except OSError as error:
_LOGGER.error('failed to bind socket %s:%s, %s', UDP_IP, UDP_PORT, error)
sock.close()
return [ None, host ]

try:
if broadcast:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
host = '255.255.255.255'

_LOGGER.debug('send_recv_msg %s [%s]', host, msg)
sock.sendto(msg.encode('utf-8'), (host, DDP_PORT))

if receive:
return sock.recvfrom(1024)

sock.close()
except OSError as error:
_LOGGER.error('failed to send data using socket, %s', error)
sock.close()
return [ None, host ]


def _send_msg(host, broadcast, msg):
Expand All @@ -87,20 +107,22 @@ def _send_msg(host, broadcast, msg):

def search(host=None, broadcast=True):
"""Discover PS4s."""
ps_list = []
msg = get_ddp_search_message()
data, addr = _send_recv_msg(host, broadcast, msg)

ps_list = []
data = parse_ddp_response(data.decode('utf-8'))
data[u'host-ip'] = addr[0]
ps_list.append(data)
data, addr = _send_recv_msg(host, broadcast, msg)
if data is not None:
data = parse_ddp_response(data.decode('utf-8'))
data[u'host-ip'] = addr[0]
ps_list.append(data)
return ps_list


def get_status(host):
"""Get status."""
ps_list = search(host=host)
return ps_list[0]
for ps_list in search(host=host):
return ps_list
return {}


def wakeup(host, credential, broadcast=None):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Topic :: Software Development :: Libraries :: Python Modules',
],
keywords = 'playstation sony ps4',
Expand Down