import grpc

import radio_config_pb2
import flowgraph_config_pb2
import transmitter_pb2
import message_valid_pb2
import flowgraph_dispatcher_service_pb2_grpc

GRPC_SERVER_URI = "localhost:50051"

def run():
    print("Will try to send radio configuration...")
    with grpc.insecure_channel(GRPC_SERVER_URI) as channel:
        stub = flowgraph_dispatcher_service_pb2_grpc.FlowgraphDispatcherServiceStub(channel)

        # Set radio parameters and send them
        radio = radio_config_pb2.RadioConfig()
        radio.antenna_name = "RX"
        radio.dev_driver = "rtlsdr"
        radio.freq = 435e6
        radio.gain = 30
        radio.samp_rate = 2.048e6
        radio.channels = 1

        response = stub.SetRadioConfigService(radio)
        if (not response.valid):
            print("Client received: Error")
            print(response.error_msg)
        else:
            print("Client received: " + str(response.valid))

        # Set flowgraph parameters and send them
        flowgraph = flowgraph_config_pb2.FlowgraphConfig()
        flowgraph.sps = 4
        flowgraph.preamble_threshold = 0
        flowgraph.sync_word_threshold = 1 
        lo_offset = 100e3
        
        response = stub.SetFlowgraphConfigService(flowgraph)
        if (not response.valid):
            print("Client received: Error")
            print(response.error_msg)
        else:
            print("Client received: " + str(response.valid))

        # Set transmission_mode parameters and send them
        transmission_mode = transmitter_pb2.TransmissionMode()
        transmission_mode.name = transmitter_pb2.TransmissionMode.FSK
        transmission_mode.modulation.fsk.baudrate = 50e3
        transmission_mode.modulation.fsk.modulation_index = 1
        transmission_mode.framing.ieee802_15_4.preamble = bytes.fromhex('55')
        transmission_mode.framing.ieee802_15_4.preamble_length = 12
        transmission_mode.framing.ieee802_15_4.sync_word = bytes.fromhex('1a cf fc 1d')
        transmission_mode.framing.ieee802_15_4.max_frame_length = 448 
        
        transmission_mode.framing.ieee802_15_4.generic_options.crc.size_in_bits = 32
        transmission_mode.framing.ieee802_15_4.generic_options.crc.polynomial = bytes.fromhex('1EDC6F41')
        transmission_mode.framing.ieee802_15_4.generic_options.crc.init_value = bytes.fromhex('ffffffff')
        transmission_mode.framing.ieee802_15_4.generic_options.crc.final_xor = bytes.fromhex('ffffffff')
        transmission_mode.framing.ieee802_15_4.generic_options.crc.reflect_input = True
        transmission_mode.framing.ieee802_15_4.generic_options.crc.reflect_output = True
        
        transmission_mode.framing.ieee802_15_4.generic_options.scrambler.mask = 0xa9
        transmission_mode.framing.ieee802_15_4.generic_options.scrambler.seed = 0xff
        transmission_mode.framing.ieee802_15_4.generic_options.scrambler.order = 7
        transmission_mode.framing.ieee802_15_4.generic_options.scrambler.msb = True
        transmission_mode.framing.ieee802_15_4.generic_options.scrambler.self_synchronizing = False
        
        # Set transmitter parameters and send them
        transmitter = transmitter_pb2.Transmitter()
        transmitter.name = "PHASMA"
        transmitter.center_freq = 400e6
        transmitter.transmission_mode.append(transmission_mode)
        
        response = stub.SetTransmitterService(transmitter)
        if (not response.valid):
            print("Client received: Error")
            print(response.error_msg)
        else:
            print("Client received: " + str(response.valid))

if __name__ == "__main__":
    run()
