# Path: IoTPy/sfp_processor.py
# class SfpMachine(object):
# 
#     def __init__(self):
#         self.pin_states = {}
#         self.pin_id_list = []
#         for pin in pin_list:
#             pin_id = pin[0]
#             pin_caps = pin[1]
#             pin_extras = pin[2]
#             if pin_caps & CAP_ADC:
#                 secondary_function = AnalogPin()
#             elif pin_caps & CAP_PWM:
#                 secondary_function = PwmPin()
#             else:
#                 secondary_function = None
#             self.pin_states[pin_id] = (pin_caps, pin_extras, DigitalPin(), secondary_function)
#             self.pin_id_list.append(pin_id)
#         self.sfp_comands = {}
#         self.sfp_comands[1] = self.set_primary
#         self.sfp_comands[2] = self.set_secondary
#         self.sfp_comands[3] = self.set_pin_mode
#         self.sfp_comands[4] = self.digital_write
#         self.sfp_comands[5] = self.digital_read
#         self.sfp_comands[6] = self.attach_interrupt
#         self.sfp_comands[7] = self.detach_interrupt
#         # self.sfp_comands[9] =
#         self.sfp_comands[255] = self.get_device_info
# 
#     def set_primary(self, arg_list):
#         pin_id = arg_list[0]
#         if pin_id in self.pin_id_list:
#             self.pin_states[pin_id][2].set_primary()
# 
#     def set_secondary(self, arg_list):
#         pin_id = arg_list[0]
#         if pin_id in self.pin_id_list:
#             self.pin_states[pin_id][2].set_secondary()
# 
#     def set_pin_mode(self, arg_list):
#         pin_id = arg_list[0]
#         mode = arg_list[1]
#         if pin_id in self.pin_id_list:
#             self.pin_states[pin_id][2].set_mode(mode)
# 
#     def digital_write(self, arg_list):
#         pin_id = arg_list[0]
#         value = arg_list[1]
#         if pin_id in self.pin_id_list:
#             self.pin_states[pin_id][2].write(value)
# 
#     def digital_read(self, arg_list):
#         pin_id = arg_list[0]
#         if pin_id in self.pin_id_list:
#             return pin_id, self.pin_states[pin_id][2].read()
# 
#     def attach_interrupt(self, arg_list):
#         pass
# 
#     def detach_interrupt(self, arg_list):
#         pass
# 
#     def get_device_info(self, arg_list):
#         return 44, 0, 44, 0
# 
#     def execute_sfp(self, binary_sfp_command):
#         decoded_sfp_command = decode_sfp(binary_sfp_command)
#         results = self.sfp_comands[decoded_sfp_command[0]](decoded_sfp_command[1])
#         if results:
#             return encode_sfp(decoded_sfp_command[0], results)
# 
# Path: IoTPy/sfp.py
# def command_slicer(io_buffer):
#     commands_list = []
#     while io_buffer:
#         if not valid_message(io_buffer):
#             return io_buffer, commands_list
# 
#         cmd, length = unpack('!BH', io_buffer[:3])
# 
#         if len(io_buffer) < (length + 3):
#             return io_buffer, commands_list
# 
#         sfp_command = io_buffer[:length + 3]      # SFP header + data
#         io_buffer = io_buffer[length + 3:]
#         commands_list.append(sfp_command)
# 
#     return io_buffer, commands_list


import socket
import sys
from time import sleep
from IoTPy.sfp_processor import SfpMachine
from IoTPy.sfp import command_slicer

HOST = None              # Symbolic name meaning all available interfaces
PORT = 7777              # Arbitrary non-privileged port

s = None
sfp_machine_instance = SfpMachine()

for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except socket.error as msg:
        s = None
        continue
    try:
        s.bind(sa)
        s.listen(1)
    except socket.error as msg:

# Path: IoTPy/sfp_processor.py
# class SfpMachine(object):
# 
#     def __init__(self):
#         self.pin_states = {}
#         self.pin_id_list = []
#         for pin in pin_list:
#             pin_id = pin[0]
#             pin_caps = pin[1]
#             pin_extras = pin[2]
#             if pin_caps & CAP_ADC:
#                 secondary_function = AnalogPin()
#             elif pin_caps & CAP_PWM:
#                 secondary_function = PwmPin()
#             else:
#                 secondary_function = None
#             self.pin_states[pin_id] = (pin_caps, pin_extras, DigitalPin(), secondary_function)
#             self.pin_id_list.append(pin_id)
#         self.sfp_comands = {}
#         self.sfp_comands[1] = self.set_primary
#         self.sfp_comands[2] = self.set_secondary
#         self.sfp_comands[3] = self.set_pin_mode
#         self.sfp_comands[4] = self.digital_write
#         self.sfp_comands[5] = self.digital_read
#         self.sfp_comands[6] = self.attach_interrupt
#         self.sfp_comands[7] = self.detach_interrupt
#         # self.sfp_comands[9] =
#         self.sfp_comands[255] = self.get_device_info
# 
#     def set_primary(self, arg_list):
#         pin_id = arg_list[0]
#         if pin_id in self.pin_id_list:
#             self.pin_states[pin_id][2].set_primary()
# 
#     def set_secondary(self, arg_list):
#         pin_id = arg_list[0]
#         if pin_id in self.pin_id_list:
#             self.pin_states[pin_id][2].set_secondary()
# 
#     def set_pin_mode(self, arg_list):
#         pin_id = arg_list[0]
#         mode = arg_list[1]
#         if pin_id in self.pin_id_list:
#             self.pin_states[pin_id][2].set_mode(mode)
# 
#     def digital_write(self, arg_list):
#         pin_id = arg_list[0]
#         value = arg_list[1]
#         if pin_id in self.pin_id_list:
#             self.pin_states[pin_id][2].write(value)
# 
#     def digital_read(self, arg_list):
#         pin_id = arg_list[0]
#         if pin_id in self.pin_id_list:
#             return pin_id, self.pin_states[pin_id][2].read()
# 
#     def attach_interrupt(self, arg_list):
#         pass
# 
#     def detach_interrupt(self, arg_list):
#         pass
# 
#     def get_device_info(self, arg_list):
#         return 44, 0, 44, 0
# 
#     def execute_sfp(self, binary_sfp_command):
#         decoded_sfp_command = decode_sfp(binary_sfp_command)
#         results = self.sfp_comands[decoded_sfp_command[0]](decoded_sfp_command[1])
#         if results:
#             return encode_sfp(decoded_sfp_command[0], results)
# 
# Path: IoTPy/sfp.py
# def command_slicer(io_buffer):
#     commands_list = []
#     while io_buffer:
#         if not valid_message(io_buffer):
#             return io_buffer, commands_list
# 
#         cmd, length = unpack('!BH', io_buffer[:3])
# 
#         if len(io_buffer) < (length + 3):
#             return io_buffer, commands_list
# 
#         sfp_command = io_buffer[:length + 3]      # SFP header + data
#         io_buffer = io_buffer[length + 3:]
#         commands_list.append(sfp_command)
# 
#     return io_buffer, commands_list


# Path: Tests/virtual_sfp_on_socket_server.py
import socket
import sys
from time import sleep
from IoTPy.sfp_processor import SfpMachine
from IoTPy.sfp import command_slicer


HOST = None              # Symbolic name meaning all available interfaces
PORT = 7777              # Arbitrary non-privileged port

s = None
sfp_machine_instance = SfpMachine()

for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except socket.error as msg:
        s = None
        continue
    try:
        s.bind(sa)
        s.listen(1)
    except socket.error as msg:

        s.close()