1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2023 Oxide Computer Company
14  */
15 
16 /*
17  * Basic test of libjedec temperature features.
18  */
19 
20 #include <stdlib.h>
21 #include <limits.h>
22 #include <err.h>
23 #include <sys/sysmacros.h>
24 #include <libjedec.h>
25 
26 typedef struct {
27 	uint32_t ltt_temp;
28 	boolean_t ltt_res;
29 	int32_t ltt_min;
30 	int32_t ltt_max;
31 	const char *ltt_desc;
32 } libjedec_temp_test_t;
33 
34 static const libjedec_temp_test_t temp_tests[] = {
35 	{ JEDEC_TEMP_CASE_A2T, B_TRUE, -40, 105, "Operating temperature A2T" },
36 	{ JEDEC_TEMP_CASE_RT, B_TRUE, 0, 45, "Operating temperature RT" },
37 	{ JEDEC_TEMP_AMB_CT, B_TRUE, 0, 70, "Ambient temperature CT" },
38 	{ JEDEC_TEMP_AMB_IOT, B_TRUE, -40, 85, "Ambient temperature IOT" },
39 	{ JEDEC_TEMP_AMB_AO1T, B_TRUE,  -40, 125, "Ambient temperature A01T" },
40 	{ JEDEC_TEMP_STOR_ST, B_TRUE, -40, 85, "Storage temperature ST" },
41 	{ 42, B_FALSE, 0, 0, "invalid temperature (42)" },
42 	{ INT32_MAX, B_FALSE, 0, 0, "invalid temperature (INT32_MAX)" },
43 	{ UINT32_MAX, B_FALSE, 0, 0, "invalid temperature (UINT32_MAX)" }
44 };
45 
46 static boolean_t
libjedec_temp_run_single(const libjedec_temp_test_t * test)47 libjedec_temp_run_single(const libjedec_temp_test_t *test)
48 {
49 	int32_t min = INT32_MIN, max = INT32_MAX;
50 	boolean_t res;
51 
52 	res = libjedec_temp_range(test->ltt_temp, &min, &max);
53 	if (res != test->ltt_res) {
54 		if (test->ltt_res) {
55 			warnx("libjedec_temp_range() succeeded, but we "
56 			    "expected failure!");
57 		} else {
58 			warnx("libjedec_temp_range() failed, but we expected "
59 			    "success!");
60 		}
61 		return (B_FALSE);
62 	}
63 
64 	if (!res) {
65 		return (B_TRUE);
66 	}
67 
68 	if (min != test->ltt_min) {
69 		warnx("received incorrect minimum temperature: expected %d, "
70 		    "found %d\n", test->ltt_min, min);
71 	}
72 
73 	if (max != test->ltt_max) {
74 		warnx("received incorrect maximum temperature: expected %d, "
75 		    "found %d\n", test->ltt_max, max);
76 	}
77 
78 	return (B_TRUE);
79 }
80 
81 int
main(void)82 main(void)
83 {
84 	int ret = EXIT_SUCCESS;
85 
86 	for (size_t i = 0; i < ARRAY_SIZE(temp_tests); i++) {
87 		const libjedec_temp_test_t *test = &temp_tests[i];
88 
89 		if (libjedec_temp_run_single(test)) {
90 			(void) printf("TEST PASSED: %s\n", test->ltt_desc);
91 		} else {
92 			(void) fprintf(stderr, "TEST FAILED: %s\n",
93 			    test->ltt_desc);
94 			ret = EXIT_FAILURE;
95 		}
96 	}
97 
98 	return (ret);
99 }
100