aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/octoclock/lib/udp_handlers.c
blob: daf2a7612d3e3a86cbcc71bd1652fa4e6b353038 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright 2014 Ettus Research LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include <avr/eeprom.h>
#include <avr/io.h>
#include <avr/wdt.h>

#include <octoclock.h>
#include <gpsdo.h>
#include <network.h>
#include <state.h>
#include <net/udp_handlers.h>

void handle_udp_ctrl_packet(
    struct socket_address src, struct socket_address dst,
    unsigned char *payload, int payload_len
){
    const octoclock_packet_t *pkt_in = (octoclock_packet_t*)payload;
    octoclock_packet_t pkt_out;
    pkt_out.proto_ver = OCTOCLOCK_FW_COMPAT_NUM;
    pkt_out.sequence = pkt_in->sequence;

    if(pkt_in->proto_ver == OCTOCLOCK_FW_COMPAT_NUM){
        switch(pkt_in->code){
            case OCTOCLOCK_QUERY_CMD:
                pkt_out.code = OCTOCLOCK_QUERY_ACK;
                pkt_out.len = 0;
                break;

            case SEND_EEPROM_CMD:
                pkt_out.code = SEND_EEPROM_ACK;
                pkt_out.len = sizeof(octoclock_fw_eeprom_t);

                octoclock_fw_eeprom_t *eeprom_info = (octoclock_fw_eeprom_t*)pkt_out.data;

                //Read values from EEPROM into packet
                eeprom_read_block(eeprom_info, (void*)0, sizeof(octoclock_fw_eeprom_t));

                //If EEPROM network fields are not fully populated, copy defaults
                if(eeprom_read_byte((uint8_t*)OCTOCLOCK_EEPROM_IP_ADDR) == 0xFF ||
                   eeprom_read_byte((uint8_t*)OCTOCLOCK_EEPROM_DR_ADDR) == 0xFF ||
                   eeprom_read_byte((uint8_t*)OCTOCLOCK_EEPROM_MAC_ADDR) == 0xFF){

                    memcpy(eeprom_info->mac_addr, default_mac, 6);
                    eeprom_info->ip_addr = default_ip;
                    eeprom_info->dr_addr = default_dr;
                    eeprom_info->netmask = default_netmask;
                }

                //Check if strings or revision is empty
                if(eeprom_info->serial[0] == 0xFF) memset(eeprom_info->serial, 0, 10);
                if(eeprom_info->name[0] == 0xFF) memset(eeprom_info->name, 0, 10);
                if(eeprom_info->revision == 0xFF) eeprom_info->revision = 0;
                break;

            case BURN_EEPROM_CMD:{
                //Confirm length of data
                if(pkt_in->len != sizeof(octoclock_fw_eeprom_t)){
                    pkt_out.code = BURN_EEPROM_FAILURE_ACK;
                    break;
                }

                /*
                 * In all cases, a full octoclock_fw_eeprom_t is written to lower the overall
                 * number of writes due to this EEPROM's smaller amount of safe writes.
                 * It is up to the host to make sure that the values that should be
                 * preserved are present in the octoclock_fw_eeprom_t struct.
                 */
                const octoclock_fw_eeprom_t *eeprom_pkt = (octoclock_fw_eeprom_t*)pkt_in->data;
                pkt_out.len = 0;

                //Write EEPROM data from packet
                eeprom_write_block(eeprom_pkt, (void*)0, sizeof(octoclock_fw_eeprom_t));

                //Read back and compare to packet to confirm successful write
                uint8_t eeprom_contents[sizeof(octoclock_fw_eeprom_t)];
                eeprom_read_block(eeprom_contents, (void*)0, sizeof(octoclock_fw_eeprom_t));
                uint8_t n = memcmp(eeprom_contents, eeprom_pkt, sizeof(octoclock_fw_eeprom_t));
                pkt_out.code = n ? BURN_EEPROM_FAILURE_ACK
                                 : BURN_EEPROM_SUCCESS_ACK;
                break;
            }

            case SEND_STATE_CMD:
                pkt_out.code = SEND_STATE_ACK;
                pkt_out.len = sizeof(octoclock_state_t);

                //Populate octoclock_state_t fields
                octoclock_state_t *state = (octoclock_state_t*)pkt_out.data;
                state->external_detected = (is_ext_ref_present()) ? 1 : 0;
                state->gps_detected = (PIND & _BV(DDD4)) ? 1 : 0;
                state->which_ref = (uint8_t)which_ref();
                state->switch_pos = (uint8_t)get_switch_pos();
                break;

            case RESET_CMD:
                pkt_out.code = RESET_ACK;
                send_udp_pkt(OCTOCLOCK_UDP_CTRL_PORT, src, (void*)&pkt_out, sizeof(octoclock_packet_t));
                wdt_enable(WDTO_30MS);
                while(1);
                return;

            default:
                return;
        }

        send_udp_pkt(OCTOCLOCK_UDP_CTRL_PORT, src, (void*)&pkt_out, sizeof(octoclock_packet_t));
    }
}

void handle_udp_gpsdo_packet(
    struct socket_address src, struct socket_address dst,
    unsigned char *payload, int payload_len
){
    const octoclock_packet_t *pkt_in = (octoclock_packet_t*)payload;
    octoclock_packet_t pkt_out;
    pkt_out.proto_ver = OCTOCLOCK_FW_COMPAT_NUM;
    pkt_out.sequence = pkt_in->sequence;

    if(pkt_in->proto_ver == OCTOCLOCK_FW_COMPAT_NUM){
        switch(pkt_in->code){
            case HOST_SEND_TO_GPSDO_CMD:
                send_gpsdo_cmd((char*)pkt_in->data, pkt_in->len);
                pkt_out.code = HOST_SEND_TO_GPSDO_ACK;
                pkt_out.len = 0;
                break;

            case SEND_POOLSIZE_CMD:
                pkt_out.code = SEND_POOLSIZE_ACK;
                pkt_out.len = 0;
                pkt_out.poolsize = POOLSIZE;
                break;

            case SEND_CACHE_STATE_CMD:
                pkt_out.code = SEND_CACHE_STATE_ACK;
                pkt_out.state = gpsdo_state;
                break;

            case SEND_GPSDO_CACHE_CMD:
                pkt_out.code = SEND_GPSDO_CACHE_ACK;
                pkt_out.state = gpsdo_state;
                memcpy(pkt_out.data, gpsdo_buf, POOLSIZE);
                break;

            default:
                return;
        }

        send_udp_pkt(OCTOCLOCK_UDP_GPSDO_PORT, src, (void*)&pkt_out, sizeof(octoclock_packet_t));
    }
}