add functions to save/load smart data blobs directly
[platform/upstream/libatasmart.git] / atasmart.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef fooatasmarthfoo
4 #define fooatasmarthfoo
5
6 /***
7     This file is part of libatasmart.
8
9     Copyright 2008 Lennart Poettering
10
11     libatasmart is free software; you can redistribute it and/or modify
12     it under the terms of the GNU Lesser General Public License as
13     published by the Free Software Foundation, either version 2.1 of the
14     License, or (at your option) any later version.
15
16     libatasmart is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19     Lesser General Public License for more details.
20
21     You should have received a copy of the GNU Lesser General Public
22     License along with libatasmart. If not, If not, see
23     <http://www.gnu.org/licenses/>.
24 ***/
25
26 #include <inttypes.h>
27
28 /* Please note that all enums defined here may be extended at any time
29  * without this being considered an ABI change. So take care when
30  * using them as indexes! */
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 typedef unsigned SkBool;
37
38 #ifndef FALSE
39 #define FALSE (0)
40 #endif
41
42 #ifndef TRUE
43 #define TRUE (!FALSE)
44 #endif
45
46 /* ATA SMART test type (ATA8 7.52.5.2) */
47 typedef enum SkSmartSelfTest {
48         SK_SMART_SELF_TEST_SHORT = 1,
49         SK_SMART_SELF_TEST_EXTENDED = 2,
50         SK_SMART_SELF_TEST_CONVEYANCE = 3,
51         SK_SMART_SELF_TEST_ABORT = 127
52 } SkSmartSelfTest;
53
54 const char* sk_smart_self_test_to_string(SkSmartSelfTest test);
55
56 typedef struct SkIdentifyParsedData {
57         char serial[21];
58         char firmware[9];
59         char model[41];
60
61         /* This structure may be extended at any time without this being
62          * considered an ABI change. So take care when you copy it. */
63 } SkIdentifyParsedData;
64
65 typedef enum SkSmartOfflineDataCollectionStatus {
66         SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_NEVER,
67         SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_SUCCESS,
68         SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_INPROGRESS,
69         SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_SUSPENDED,
70         SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_ABORTED,
71         SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_FATAL,
72         SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_UNKNOWN,
73         _SK_SMART_OFFLINE_DATA_COLLECTION_STATUS_MAX
74 } SkSmartOfflineDataCollectionStatus;
75
76 const char* sk_smart_offline_data_collection_status_to_string(SkSmartOfflineDataCollectionStatus status);
77
78 typedef enum SkSmartSelfTestExecutionStatus {
79         SK_SMART_SELF_TEST_EXECUTION_STATUS_SUCCESS_OR_NEVER = 0,
80         SK_SMART_SELF_TEST_EXECUTION_STATUS_ABORTED = 1,
81         SK_SMART_SELF_TEST_EXECUTION_STATUS_INTERRUPTED = 2,
82         SK_SMART_SELF_TEST_EXECUTION_STATUS_FATAL = 3,
83         SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_UNKNOWN = 4,
84         SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_ELECTRICAL = 5,
85         SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_SERVO = 6,
86         SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_READ = 7,
87         SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_HANDLING = 8,
88         SK_SMART_SELF_TEST_EXECUTION_STATUS_INPROGRESS = 15,
89         _SK_SMART_SELF_TEST_EXECUTION_STATUS_MAX
90 } SkSmartSelfTestExecutionStatus;
91
92 const char *sk_smart_self_test_execution_status_to_string(SkSmartSelfTestExecutionStatus status);
93
94 typedef struct SkSmartParsedData {
95         /* Volatile data */
96         SkSmartOfflineDataCollectionStatus offline_data_collection_status;
97         unsigned total_offline_data_collection_seconds;
98         SkSmartSelfTestExecutionStatus self_test_execution_status;
99         unsigned self_test_execution_percent_remaining;
100
101         /* Fixed data */
102         SkBool short_and_extended_test_available:1;
103         SkBool conveyance_test_available:1;
104         SkBool start_test_available:1;
105         SkBool abort_test_available:1;
106
107         unsigned short_test_polling_minutes;
108         unsigned extended_test_polling_minutes;
109         unsigned conveyance_test_polling_minutes;
110
111         /* This structure may be extended at any time without this being
112          * considered an ABI change. So take care when you copy it.  */
113 } SkSmartParsedData;
114
115 SkBool sk_smart_self_test_available(const SkSmartParsedData *d, SkSmartSelfTest test);
116 unsigned sk_smart_self_test_polling_minutes(const SkSmartParsedData *d, SkSmartSelfTest test);
117
118 typedef enum SkSmartAttributeUnit {
119         SK_SMART_ATTRIBUTE_UNIT_UNKNOWN,
120         SK_SMART_ATTRIBUTE_UNIT_NONE,
121         SK_SMART_ATTRIBUTE_UNIT_MSECONDS,    /* milliseconds */
122         SK_SMART_ATTRIBUTE_UNIT_SECTORS,
123         SK_SMART_ATTRIBUTE_UNIT_MKELVIN,     /* millikelvin */
124         _SK_SMART_ATTRIBUTE_UNIT_MAX
125 } SkSmartAttributeUnit;
126
127 const char* sk_smart_attribute_unit_to_string(SkSmartAttributeUnit unit);
128
129 typedef struct SkSmartAttributeParsedData {
130         /* Fixed data */
131         uint8_t id;
132         const char *name;
133         SkSmartAttributeUnit pretty_unit; /* for pretty_value */
134
135         uint16_t flags;
136
137         uint8_t threshold;
138         SkBool threshold_valid:1;
139
140         SkBool online:1;
141         SkBool prefailure:1;
142
143         /* Volatile data */
144         SkBool good:1, good_valid:1;
145         SkBool current_value_valid:1, worst_value_valid:1;
146         uint8_t current_value, worst_value;
147         uint64_t pretty_value;
148         uint8_t raw[6];
149
150         /* This structure may be extended at any time without this being
151          * considered an ABI change. So take care when you copy it. */
152 } SkSmartAttributeParsedData;
153
154 typedef struct SkDisk SkDisk;
155
156 typedef enum SkSmartOverall  {
157         SK_SMART_OVERALL_GOOD,
158         SK_SMART_OVERALL_BAD_STATUS,     /* Smart Self Assessment negative */
159         SK_SMART_OVERALL_BAD_SECTOR,     /* At least one bad sector */
160         SK_SMART_OVERALL_BAD_ATTRIBUTE,  /* At least one attribute exceeded its threshold */
161         _SK_SMART_OVERALL_MAX
162 } SkSmartOverall;
163
164 const char* sk_smart_overall_to_string(SkSmartOverall overall);
165
166 int sk_disk_open(const char *name, SkDisk **d);
167
168 int sk_disk_get_size(SkDisk *d, uint64_t *bytes);
169
170 int sk_disk_check_sleep_mode(SkDisk *d, SkBool *awake);
171
172 int sk_disk_identify_is_available(SkDisk *d, SkBool *available);
173 int sk_disk_identify_parse(SkDisk *d, const SkIdentifyParsedData **data);
174
175 typedef void (*SkSmartAttributeParseCallback)(SkDisk *d, const SkSmartAttributeParsedData *a, void* userdata);
176
177 int sk_disk_smart_is_available(SkDisk *d, SkBool *available);
178 int sk_disk_smart_status(SkDisk *d, SkBool *good);
179
180 /* Reading SMART data might cause the disk to wake up from
181  * sleep. Hence from monitoring daemons make sure to call
182  * sk_disk_check_power_mode() to check wether the disk is sleeping and
183  * skip the read if so. */
184 int sk_disk_smart_read_data(SkDisk *d);
185
186 int sk_disk_get_blob(SkDisk *d, const void **blob, size_t *size);
187 int sk_disk_set_blob(SkDisk *d, const void *blob, size_t size);
188
189 int sk_disk_smart_parse(SkDisk *d, const SkSmartParsedData **data);
190 int sk_disk_smart_parse_attributes(SkDisk *d, SkSmartAttributeParseCallback cb, void* userdata);
191 int sk_disk_smart_self_test(SkDisk *d, SkSmartSelfTest test);
192
193 /* High level API to get the power on time */
194 int sk_disk_smart_get_power_on(SkDisk *d, uint64_t *mseconds);
195
196 /* High level API to get the number of bad sectors (i.e. pending and reallocated) */
197 int sk_disk_smart_get_bad(SkDisk *d, uint64_t *sectors);
198
199 /* High level API to get the temperature */
200 int sk_disk_smart_get_temperature(SkDisk *d, uint64_t *mkelvin);
201
202 /* Get overall status. This integrates the values of a couple of fields into a single overall status */
203 int sk_disk_smart_get_overall(SkDisk *d, SkSmartOverall *overall);
204
205 /* Dump the current parsed status to STDOUT */
206 int sk_disk_dump(SkDisk *d);
207
208 void sk_disk_free(SkDisk *d);
209
210 #ifdef __cplusplus
211 }
212 #endif
213
214 #endif