aboutsummaryrefslogtreecommitdiffstats
path: root/host/tests/eeprom_c_test.c
blob: 1b29dfd44ea93eb1716ec91e43370fcc0c810afa (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
//
// Copyright 2015 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 <uhd.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define UHD_TEST_EXECUTE_OR_GOTO(label, ...) \
    if(__VA_ARGS__){ \
        fprintf(stderr, "Error occurred at %s:%d\n", __FILE__, (__LINE__-1)); \
        return_code = EXIT_FAILURE; \
        goto label; \
    }

#define BUFFER_SIZE 1024

int main(){

    // Variables
    int return_code;
    uhd_mboard_eeprom_handle mb_eeprom;
    uhd_dboard_eeprom_handle db_eeprom;
    int db_revision;
    char str_buffer[BUFFER_SIZE];

    return_code = EXIT_SUCCESS;

    /*
     * Motherboard EEPROM test
     */

    // Create EEPROM handle
    UHD_TEST_EXECUTE_OR_GOTO(end_of_test,
        uhd_mboard_eeprom_make(&mb_eeprom)
    )

    // Set a value, retrieve it, and make sure it matches
    UHD_TEST_EXECUTE_OR_GOTO(free_mboard_eeprom,
        uhd_mboard_eeprom_set_value(
            mb_eeprom,
            "serial",
            "F12345"
        )
    )
    UHD_TEST_EXECUTE_OR_GOTO(free_mboard_eeprom,
        uhd_mboard_eeprom_get_value(
            mb_eeprom,
            "serial",
            str_buffer,
            BUFFER_SIZE
        )
    )
    if(strcmp(str_buffer, "F12345")){
        return_code = EXIT_FAILURE;
        fprintf(stderr, "%s:%d: Mismatched EEPROM value: \"%s\" vs. \"F12345\"\n",
                        __FILE__, __LINE__,
                        str_buffer);
        goto free_mboard_eeprom;
    }

    /*
     * Daughterboard EEPROM test
     */

    // Create EEPROM handle
    UHD_TEST_EXECUTE_OR_GOTO(free_mboard_eeprom,
        uhd_dboard_eeprom_make(&db_eeprom)
    )

    // Set the ID, retrieve it, and make sure it matches
    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom,
        uhd_dboard_eeprom_set_id(db_eeprom, "0x0067")
    )
    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom,
        uhd_dboard_eeprom_get_id(
            db_eeprom,
            str_buffer,
            BUFFER_SIZE
        )
    )
    if(strcmp(str_buffer, "0x0067")){
        return_code = EXIT_FAILURE;
        fprintf(stderr, "%s:%d: Mismatched daughterboard ID: \"%s\" vs. \"0x0067\"\n",
                        __FILE__, __LINE__,
                        str_buffer);
        goto free_dboard_eeprom;
    }

    // Set the serial, retrieve it, and make sure it matches
    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom,
        uhd_dboard_eeprom_set_serial(db_eeprom, "F12345")
    )
    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom,
        uhd_dboard_eeprom_get_serial(
            db_eeprom,
            str_buffer,
            BUFFER_SIZE
        )
    )
    if(strcmp(str_buffer, "F12345")){
        return_code = EXIT_FAILURE;
        fprintf(stderr, "%s:%d: Mismatched daughterboard serial: \"%s\" vs. \"F12345\"\n",
                        __FILE__, __LINE__,
                        str_buffer);
        goto free_dboard_eeprom;
    }

    // Set the revision, retrieve it, and make sure it matches
    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom,
        uhd_dboard_eeprom_set_revision(db_eeprom, 4)
    )
    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom,
        uhd_dboard_eeprom_get_revision(db_eeprom, &db_revision)
    )
    if(db_revision != 4){
        return_code = EXIT_FAILURE;
        fprintf(stderr, "%s:%d: Mismatched daughterboard revision: \"%d\" vs. 4\n",
                        __FILE__, __LINE__, db_revision);
        goto free_dboard_eeprom;
    }

    free_dboard_eeprom:
        if(return_code){
            uhd_dboard_eeprom_last_error(db_eeprom, str_buffer, BUFFER_SIZE);
            fprintf(stderr, "db_eeprom error: %s\n", str_buffer);
        }
	uhd_dboard_eeprom_free(&db_eeprom);

    free_mboard_eeprom:
        if(return_code){
            uhd_mboard_eeprom_last_error(mb_eeprom, str_buffer, BUFFER_SIZE);
            fprintf(stderr, "mb_eeprom error: %s\n", str_buffer);
        }
	uhd_mboard_eeprom_free(&mb_eeprom);

    end_of_test:
        if(!return_code){
            printf("\nNo errors detected\n");
        }
        return return_code;
}