def send_msg(hwa, port, descr_str, d_id, writer_func, smb=0):
    rc, lvl3_msg = writer_func()
    if rc != 0:
        print "fail ", descr_str, ", rc = ", rc
        return
    else:
        print "sending ", descr_str
        msg = ("msg_lvl2", ("msg_metadata", hwa, port, smb, d_id), lvl3_msg)
        lnet.send(msg)
def test_write():
    sync_timeout = 1500  # *1000us = 1.5s
    drv_send_timeout = 500000  # *1us = 0.5s
    net_confirm_timeout = 1000  # *1000us = 1s
    lnet.init(1, 10, sync_timeout, drv_send_timeout, net_confirm_timeout)
    lnet.send(("msg_lvl2", ("msg_metadata", 15, 1, 0, 18), "STARTING_TEST"))

    hwa = 15
    port = 2
    s = 1
    send_msg(hwa, port, "button.w_up", button_1_id, button.w_up)
    send_msg(hwa, port, "button.w_down", button_2_id, button.w_down)
    send_msg(hwa, port, "led.w_on", led_1_id, led.w_on)
    send_msg(hwa, port, "led.w_off", led_2_id, led.w_off)
    send_msg(hwa, port, "led.w_toggle", led_2_id, led.w_toggle)
	def try_register(msg, own_port, reg_slave_f, check_slave_reg_f):
		hwa = msg[1][1]
		
		#if NORMAL msg:
		if ( (msg[1][3] == 0) | ((msg[1][4] != sys.Ids['HEARTBEAT']) & (msg[1][4] != sys.Ids['CONFIG'])) ):
			return (rc_ok)
		
		#if CONFIG msg:
		if (msg[1][4] == sys.Ids['CONFIG']):
			if (reg_states.get(hwa) == reg_state_req_sent):
				#send command to set slave regime to Normal
				packer_rc, data = sys.set_op_w_normal()
				if (packer_rc != 0):
					return (rc_packer_err, packer_rc)
				msg_setop = ("msg_lvl2", ("msg_metadata", hwa, own_port, 1, sys.Ids['SET_OP']), data)
				snd_rc = lnet.send(msg_setop)
				if (snd_rc != 0):
					return (rc_snd_err, snd_rc)
				
				reg_slave_f(msg)
				del reg_states[hwa]
				
				return (rc_processed)
			#else: unexpected config - panic
		
		#if HEARTBEAT msg:
		#else if already registered - quit:  (this way allows app to delete slaves without asking receive_message)
		if (check_slave_reg_f(hwa)):
			return (rc_processed)
		
		#send request for config (if it hvent been done already):
		if (reg_states.get(hwa) != reg_state_req_sent):
			packer_rc, data = sys.config_w_request()
			if (packer_rc != 0):
				return (rc_packer_err, packer_rc)
			msg_req = ("msg_lvl2", ("msg_metadata", hwa, own_port, 1, sys.Ids['CONFIG']), data)
			snd_rc = lnet.send(msg_req)
			if (snd_rc != 0):
				return (rc_snd_err, snd_rc)
			else:
				#quit - only one msg handling on one registration_handler call
				reg_states[hwa] = reg_state_req_sent
				return (rc_ok)
def virt_slave():
	port = 3
	rci = can_net_sync_init(comm.port, comm.sync_msgs_limit, comm.sync_timeout, comm.send_frame_timeout_us, comm.net_confirm_timeout);
	if (rci):
		printf "initialization return code:", rci
		return 0;
	else:
		print "successful initialization\n"
	
	base_period = 0.1 # 100ms
	hwa = 13
	not_sys = 0
	is_sys = 1

	#button_id = random.randrange(0, 4)			
	button_id = 5
	rcp, btn_down_data = button.w_down()
	msg_btn_down = ("msg_lvl2", ("msg_metadata", hwa, comm.br_port, not_sys, button_id), btn_down_data)
	rcp, btn_up_data = button.w_up()
	msg_btn_up = ("msg_lvl2", ("msg_metadata", hwa, comm.br_port, not_sys, button_id), btn_up_data)

	no_id = 0
	rcp, hb_msg_data = sys.heartbeat_w()
	hb_msg = ("msg_lvl2", ("msg_metadata", hwa, comm.br_port, is_sys, no_id), hb_msg_data)
	
	x = 0
	while(True):
		msg = comm.recv_msg(port)
		if (msg[1][1] != hwa):
			print "Slave:", hwa, "recieved msg for slave:", msg[1][1]
		else:
			slave_msg_handler(msg)
		
		x += 1
		if (x == 9):
			snd_rc = lnet.send(msg_btn_down)
		if (x == 10):
			x = 0
			snd_rc = lnet.send(msg_btn_up)
		
		snd_rc = lnet.send(hb_msg)
		time.sleep(base_period)
 def do_led(self, line):
     '''
     Control led.
     @ 1) led id;
     @ 2) command (on, off, toggle)
      '''
     args = line.split(" ")
     if len(args) < 2:
         self.args_error("led")
         return
     if args[1] not in {"on", "off", "toggle"}:
         self.args_error("led")
         return
     r = {"on": pmd_net_led.w_on,
          "off": pmd_net_led.w_off,
          "toggle": pmd_net_led.w_toggle}[args[1]]()
     if r[0] != 0:
         print "Fail with led.w_xxx, r[0] = " + repr(r[0])
         return
     msg = ("msg_lvl2", ("msg_metadata", self.cur_hw_addr, self.cur_port, 0, int(args[0])), r[1])
     rc = can_net_sync.send(msg)
     if rc != 0:
         print "Sending faild with return code %i" % rc
import can_net_sync as cn
import pmd_net_system as sys

cn.init(1, 100, 100, 100, 100)
msg = ("msg_lvl2", ("msg_metadata", 123, 1, 1, sys.Ids["SET_OP"]), sys.set_op_w_configuration()[1])
print msg
cn.send(msg)