40 #include "fsm_foe.h" |
40 #include "fsm_foe.h" |
41 #include "foe.h" |
41 #include "foe.h" |
42 |
42 |
43 /*****************************************************************************/ |
43 /*****************************************************************************/ |
44 |
44 |
45 /** Maximum time in ms to wait for responses when reading out the dictionary. |
45 /** Maximum time in jiffies to wait for responses when reading out the |
46 */ |
46 * dictionary. |
47 #define EC_FSM_FOE_TIMEOUT 3000 |
47 */ |
|
48 #define EC_FSM_FOE_TIMEOUT_JIFFIES (3 * HZ) |
48 |
49 |
49 /** Size of the FoE header. |
50 /** Size of the FoE header. |
50 */ |
51 */ |
51 #define EC_FOE_HEADER_SIZE 6 |
52 #define EC_FOE_HEADER_SIZE 6 |
52 // uint8_t OpCode |
53 // uint8_t OpCode |
254 return -1; |
255 return -1; |
255 } |
256 } |
256 |
257 |
257 EC_WRITE_U16(data, EC_FOE_OPCODE_DATA); // OpCode = DataBlock req. |
258 EC_WRITE_U16(data, EC_FOE_OPCODE_DATA); // OpCode = DataBlock req. |
258 EC_WRITE_U32(data + 2, fsm->tx_packet_no); // PacketNo, Password |
259 EC_WRITE_U32(data + 2, fsm->tx_packet_no); // PacketNo, Password |
|
260 #ifdef DEBUG_FOE |
|
261 EC_SLAVE_DBG(fsm->slave, 0, "sending opcode %u packet %u\n", |
|
262 EC_FOE_OPCODE_DATA, fsm->tx_packet_no); |
|
263 #endif |
259 |
264 |
260 memcpy(data + EC_FOE_HEADER_SIZE, |
265 memcpy(data + EC_FOE_HEADER_SIZE, |
261 fsm->tx_buffer + fsm->tx_buffer_offset, current_size); |
266 fsm->tx_buffer + fsm->tx_buffer_offset, current_size); |
262 fsm->tx_current_size = current_size; |
267 fsm->tx_current_size = current_size; |
263 |
268 |
291 return -1; |
296 return -1; |
292 } |
297 } |
293 |
298 |
294 EC_WRITE_U16( data, EC_FOE_OPCODE_WRQ); // fsm write request |
299 EC_WRITE_U16( data, EC_FOE_OPCODE_WRQ); // fsm write request |
295 EC_WRITE_U32( data + 2, fsm->tx_packet_no ); |
300 EC_WRITE_U32( data + 2, fsm->tx_packet_no ); |
|
301 #ifdef DEBUG_FOE |
|
302 EC_SLAVE_DBG(fsm->slave, 0, "sending opcode %u packet %u\n", |
|
303 EC_FOE_OPCODE_WRQ, fsm->tx_packet_no); |
|
304 #endif |
296 |
305 |
297 memcpy(data + EC_FOE_HEADER_SIZE, fsm->tx_filename, current_size); |
306 memcpy(data + EC_FOE_HEADER_SIZE, fsm->tx_filename, current_size); |
298 |
307 |
299 return 0; |
308 return 0; |
300 } |
309 } |
363 return; |
372 return; |
364 } |
373 } |
365 |
374 |
366 if (!ec_slave_mbox_check(fsm->datagram)) { |
375 if (!ec_slave_mbox_check(fsm->datagram)) { |
367 // slave did not put anything in the mailbox yet |
376 // slave did not put anything in the mailbox yet |
368 unsigned long diff_ms = (fsm->datagram->jiffies_received - |
377 if (time_after(fsm->datagram->jiffies_received, |
369 fsm->jiffies_start) * 1000 / HZ; |
378 fsm->jiffies_start + EC_FSM_FOE_TIMEOUT_JIFFIES)) { |
370 if (diff_ms >= EC_FSM_FOE_TIMEOUT) { |
|
371 ec_foe_set_tx_error(fsm, FOE_TIMEOUT_ERROR); |
379 ec_foe_set_tx_error(fsm, FOE_TIMEOUT_ERROR); |
372 EC_SLAVE_ERR(slave, "Timeout while waiting for ack response.\n"); |
380 EC_SLAVE_ERR(slave, "Timeout while waiting for ack response.\n"); |
373 return; |
381 return; |
374 } |
382 } |
375 |
383 |
429 mbox_prot); |
437 mbox_prot); |
430 return; |
438 return; |
431 } |
439 } |
432 |
440 |
433 opCode = EC_READ_U8(data); |
441 opCode = EC_READ_U8(data); |
|
442 #ifdef DEBUG_FOE |
|
443 EC_SLAVE_DBG(fsm->slave, 0, "received opcode %u\n", opCode); |
|
444 #endif |
434 |
445 |
435 if (opCode == EC_FOE_OPCODE_BUSY) { |
446 if (opCode == EC_FOE_OPCODE_BUSY) { |
436 // slave not ready |
447 // slave not ready |
437 if (ec_foe_prepare_data_send(fsm, datagram)) { |
448 if (ec_foe_prepare_data_send(fsm, datagram)) { |
438 ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); |
449 ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); |
441 } |
452 } |
442 fsm->state = ec_fsm_foe_state_data_sent; |
453 fsm->state = ec_fsm_foe_state_data_sent; |
443 return; |
454 return; |
444 } |
455 } |
445 |
456 |
|
457 if (opCode == EC_FOE_OPCODE_ERR) { |
|
458 fsm->request->error_code = EC_READ_U32(data + 2); |
|
459 EC_SLAVE_ERR(slave, "Received FoE Error Request (code 0x%08x).\n", |
|
460 fsm->request->error_code); |
|
461 if (rec_size > 6 && data[6]) { |
|
462 uint8_t text[256]; |
|
463 strncpy(text, data + 6, min(rec_size - 6, sizeof(text))); |
|
464 text[sizeof(text)-1] = 0; |
|
465 EC_SLAVE_ERR(slave, "FoE Error Text: %s\n", text); |
|
466 } |
|
467 ec_foe_set_tx_error(fsm, FOE_OPCODE_ERROR); |
|
468 return; |
|
469 } |
|
470 |
446 if (opCode == EC_FOE_OPCODE_ACK) { |
471 if (opCode == EC_FOE_OPCODE_ACK) { |
447 fsm->tx_packet_no++; |
472 fsm->tx_packet_no++; |
448 fsm->tx_buffer_offset += fsm->tx_current_size; |
473 fsm->tx_buffer_offset += fsm->tx_current_size; |
449 |
474 |
450 if (fsm->tx_last_packet) { |
475 if (fsm->tx_last_packet) { |
564 } |
589 } |
565 |
590 |
566 EC_WRITE_U16(data, EC_FOE_OPCODE_RRQ); // fsm read request |
591 EC_WRITE_U16(data, EC_FOE_OPCODE_RRQ); // fsm read request |
567 EC_WRITE_U32(data + 2, 0x00000000); // no passwd |
592 EC_WRITE_U32(data + 2, 0x00000000); // no passwd |
568 memcpy(data + EC_FOE_HEADER_SIZE, fsm->rx_filename, current_size); |
593 memcpy(data + EC_FOE_HEADER_SIZE, fsm->rx_filename, current_size); |
|
594 #ifdef DEBUG_FOE |
|
595 EC_SLAVE_DBG(fsm->slave, 0, "sending opcode %u\n", EC_FOE_OPCODE_RRQ); |
|
596 #endif |
569 |
597 |
570 if (fsm->slave->master->debug_level) { |
598 if (fsm->slave->master->debug_level) { |
571 EC_SLAVE_DBG(fsm->slave, 1, "FoE Read Request:\n"); |
599 EC_SLAVE_DBG(fsm->slave, 1, "FoE Read Request:\n"); |
572 ec_print_data(data, current_size + EC_FOE_HEADER_SIZE); |
600 ec_print_data(data, current_size + EC_FOE_HEADER_SIZE); |
573 } |
601 } |
594 return -1; |
622 return -1; |
595 } |
623 } |
596 |
624 |
597 EC_WRITE_U16(data, EC_FOE_OPCODE_ACK); |
625 EC_WRITE_U16(data, EC_FOE_OPCODE_ACK); |
598 EC_WRITE_U32(data + 2, fsm->rx_expected_packet_no); |
626 EC_WRITE_U32(data + 2, fsm->rx_expected_packet_no); |
|
627 #ifdef DEBUG_FOE |
|
628 EC_SLAVE_DBG(fsm->slave, 0, "sending opcode %u packet %u\n", |
|
629 EC_FOE_OPCODE_ACK, fsm->rx_expected_packet_no); |
|
630 #endif |
599 |
631 |
600 return 0; |
632 return 0; |
601 } |
633 } |
602 |
634 |
603 /*****************************************************************************/ |
635 /*****************************************************************************/ |
702 ec_datagram_print_wc_error(fsm->datagram); |
734 ec_datagram_print_wc_error(fsm->datagram); |
703 return; |
735 return; |
704 } |
736 } |
705 |
737 |
706 if (!ec_slave_mbox_check(fsm->datagram)) { |
738 if (!ec_slave_mbox_check(fsm->datagram)) { |
707 unsigned long diff_ms = (fsm->datagram->jiffies_received - |
739 if (time_after(fsm->datagram->jiffies_received, |
708 fsm->jiffies_start) * 1000 / HZ; |
740 fsm->jiffies_start + EC_FSM_FOE_TIMEOUT_JIFFIES)) { |
709 if (diff_ms >= EC_FSM_FOE_TIMEOUT) { |
|
710 ec_foe_set_tx_error(fsm, FOE_TIMEOUT_ERROR); |
741 ec_foe_set_tx_error(fsm, FOE_TIMEOUT_ERROR); |
711 EC_SLAVE_ERR(slave, "Timeout while waiting for ack response.\n"); |
742 EC_SLAVE_ERR(slave, "Timeout while waiting for ack response.\n"); |
712 return; |
743 return; |
713 } |
744 } |
714 |
745 |
732 ec_fsm_foe_t *fsm, /**< FoE statemachine. */ |
763 ec_fsm_foe_t *fsm, /**< FoE statemachine. */ |
733 ec_datagram_t *datagram /**< Datagram to use. */ |
764 ec_datagram_t *datagram /**< Datagram to use. */ |
734 ) |
765 ) |
735 { |
766 { |
736 size_t rec_size; |
767 size_t rec_size; |
737 uint8_t *data, opCode, packet_no, mbox_prot; |
768 uint32_t packet_no; |
|
769 uint8_t *data, opCode, mbox_prot; |
738 |
770 |
739 ec_slave_t *slave = fsm->slave; |
771 ec_slave_t *slave = fsm->slave; |
740 |
772 |
741 #ifdef DEBUG_FOE |
773 #ifdef DEBUG_FOE |
742 EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); |
774 EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); |
768 ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); |
800 ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); |
769 return; |
801 return; |
770 } |
802 } |
771 |
803 |
772 opCode = EC_READ_U8(data); |
804 opCode = EC_READ_U8(data); |
|
805 #ifdef DEBUG_FOE |
|
806 EC_SLAVE_DBG(fsm->slave, 0, "received opcode %u\n", opCode); |
|
807 #endif |
773 |
808 |
774 if (opCode == EC_FOE_OPCODE_BUSY) { |
809 if (opCode == EC_FOE_OPCODE_BUSY) { |
|
810 fsm->rx_expected_packet_no--; |
775 if (ec_foe_prepare_send_ack(fsm, datagram)) { |
811 if (ec_foe_prepare_send_ack(fsm, datagram)) { |
776 ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); |
812 ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); |
|
813 } else { |
|
814 fsm->state = ec_fsm_foe_state_sent_ack; |
777 } |
815 } |
|
816 #ifdef DEBUG_FOE |
|
817 EC_SLAVE_DBG(fsm->slave, 0, "%s() busy. Next pkt %u\n", __func__, |
|
818 fsm->rx_expected_packet_no); |
|
819 #endif |
778 return; |
820 return; |
779 } |
821 } |
780 |
822 |
781 if (opCode == EC_FOE_OPCODE_ERR) { |
823 if (opCode == EC_FOE_OPCODE_ERR) { |
782 fsm->request->error_code = EC_READ_U32(data + 2); |
824 fsm->request->error_code = EC_READ_U32(data + 2); |
783 EC_SLAVE_ERR(slave, "Received FoE Error Request (code 0x%08x).\n", |
825 EC_SLAVE_ERR(slave, "Received FoE Error Request (code 0x%08x).\n", |
784 fsm->request->error_code); |
826 fsm->request->error_code); |
785 if (rec_size > 6) { |
827 if (rec_size > 6 && data[6]) { |
786 uint8_t text[256]; |
828 uint8_t text[256]; |
787 strncpy(text, data + 6, min(rec_size - 6, sizeof(text))); |
829 strncpy(text, data + 6, min(rec_size - 6, sizeof(text))); |
|
830 text[sizeof(text)-1] = 0; |
788 EC_SLAVE_ERR(slave, "FoE Error Text: %s\n", text); |
831 EC_SLAVE_ERR(slave, "FoE Error Text: %s\n", text); |
789 } |
832 } |
790 ec_foe_set_rx_error(fsm, FOE_OPCODE_ERROR); |
833 ec_foe_set_rx_error(fsm, FOE_OPCODE_ERROR); |
791 return; |
834 return; |
792 } |
835 } |
797 fsm->request->error_code = 0x00000000; |
840 fsm->request->error_code = 0x00000000; |
798 ec_foe_set_rx_error(fsm, FOE_OPCODE_ERROR); |
841 ec_foe_set_rx_error(fsm, FOE_OPCODE_ERROR); |
799 return; |
842 return; |
800 } |
843 } |
801 |
844 |
802 packet_no = EC_READ_U16(data + 2); |
845 packet_no = EC_READ_U32(data + 2); |
803 if (packet_no != fsm->rx_expected_packet_no) { |
846 if (packet_no != fsm->rx_expected_packet_no) { |
804 EC_SLAVE_ERR(slave, "Received unexpected packet number.\n"); |
847 EC_SLAVE_ERR(slave, "Received packet number %u, expected %u.\n", |
|
848 packet_no, fsm->rx_expected_packet_no); |
805 ec_foe_set_rx_error(fsm, FOE_PACKETNO_ERROR); |
849 ec_foe_set_rx_error(fsm, FOE_PACKETNO_ERROR); |
806 return; |
850 return; |
807 } |
851 } |
808 |
852 |
809 rec_size -= EC_FOE_HEADER_SIZE; |
853 rec_size -= EC_FOE_HEADER_SIZE; |