33 from .
import OTAMessage
34 from .
import OTAPublisherException
44 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
45 return socket.inet_ntoa(fcntl.ioctl(
48 struct.pack(
'256s', ifname[:15].encode()
if sys.version_info >= (3,0)
else ifname[:15])
53 sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
55 except socket.error
as msg :
56 if sys.version_info >= (3,3):
57 raise OTAPublisherException(
"ota socket failure %s" % str(msg),
True)
59 raise OTAPublisherException(
"ota socket failure %s %s" % (str(msg[0]), msg[1]),
True)
62 sock.setsockopt(socket.IPPROTO_IP,socket.IP_MULTICAST_TTL,32)
63 except socket.error
as msg :
64 if sys.version_info >= (3,3):
65 raise OTAPublisherException(
"ota socket option failure %s" % str(msg),
True)
67 raise OTAPublisherException(
"ota socket option failure %s %s" % (str(msg[0]), msg[1]),
True)
70 sock.setsockopt(socket.IPPROTO_IP,socket.IP_MULTICAST_LOOP,1)
71 except socket.error
as msg :
72 if sys.version_info >= (3,3):
73 raise OTAPublisherException(
"ota socket option failure %s" % str(msg),
True)
75 raise OTAPublisherException(
"ota socket option failure %s %s" % (str(msg[0]), msg[1]),
True)
78 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
79 except socket.error
as msg :
80 if sys.version_info >= (3,3):
81 raise OTAPublisherException(
"ota socket option failure %s" % str(msg),
True)
83 raise OTAPublisherException(
"ota socket option failure %s %s" % (str(msg[0]), msg[1]),
True)
86 sock.bind((group,port))
87 except socket.error
as msg:
88 if sys.version_info >= (3,3):
89 raise OTAPublisherException(
"bind failure %s" % str(msg),
True)
91 raise OTAPublisherException(
"bind failure %s %s" % (str(msg[0]), msg[1]),
True)
98 devAddress = socket.inet_aton(
"0.0.0.0")
100 sock.setsockopt(socket.SOL_IP,
101 socket.IP_ADD_MEMBERSHIP,
102 socket.inet_aton(group) +
105 sock.setsockopt(socket.SOL_IP,
106 socket.IP_MULTICAST_IF,
109 except socket.error
as msg:
110 if sys.version_info >= (3,3):
111 raise OTAPublisherException(
"mulicast add membership failure %s" % str(msg),
True)
113 raise OTAPublisherException(
"mulicast add membership failure %s %s" % (str(msg[0]), msg[1]),
True)
116 raise OTAPublisherException(
"unknown device %s" % device,
True)
123 (self._multicastGroup,self._port,_) = otachannel
125 self.
_uuid = uuid.uuid4()
131 def publish(self,otaMessage,txTimeMicroseconds):
132 self.
_socket.sendto(otaMessage.generate(txTimeMicroseconds,
135 ,(self._multicastGroup,self._port))
def publish(self, otaMessage, txTimeMicroseconds)
def init_multicast_socket(group, port, device)
def __init__(self, otachannel)
def get_ip_address(ifname)