This repository was archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathiotc_bsp_io_fs.h
203 lines (186 loc) · 6.47 KB
/
iotc_bsp_io_fs.h
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Copyright 2018-2020 Google LLC
*
* This is part of the Google Cloud IoT Device SDK for Embedded C.
* It is licensed under the BSD 3-Clause license; you may not use this file
* except in compliance with the License.
*
* You may obtain a copy of the License at:
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __IOTC_BSP_IO_FS_H__
#define __IOTC_BSP_IO_FS_H__
/**
* @file iotc_bsp_io_fs.h
* @brief Manages files.
*/
#include <iotc_error.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @typedef iotc_bsp_io_fs_resource_handle_t
* @brief A pointer to an open file.
*/
typedef intptr_t iotc_bsp_io_fs_resource_handle_t;
/** The handle doesn't point to an open file. */
#define IOTC_BSP_IO_FS_INVALID_RESOURCE_HANDLE -1
/** The handle points to an open file. */
#define iotc_bsp_io_fs_init_resource_handle() \
IOTC_BSP_IO_FS_INVALID_RESOURCE_HANDLE
/**
* @typedef iotc_bsp_io_fs_state_t
* @brief The file management function states.
*
* @see #iotc_bsp_io_fs_state_e
*/
typedef enum iotc_bsp_io_fs_state_e {
/** The file management function succeeded. */
IOTC_BSP_IO_FS_STATE_OK = 0,
/** Something went wrong. */
IOTC_BSP_IO_FS_ERROR = 1,
/** A parameter is invalid. */
IOTC_BSP_IO_FS_INVALID_PARAMETER = 2,
/** The file isn't available. */
IOTC_BSP_IO_FS_RESOURCE_NOT_AVAILABLE = 3,
/** The device is out of memory. */
IOTC_BSP_IO_FS_OUT_OF_MEMORY = 4,
/** The function isn't implmented on your platform. */
IOTC_BSP_IO_FS_NOT_IMPLEMENTED = 5,
/** Can't open file. **/
IOTC_BSP_IO_FS_OPEN_ERROR = 6,
/** The file is read-only so the SDK can't open it. */
IOTC_BSP_IO_FS_OPEN_READ_ONLY = 7,
/** The file can't be removed. */
IOTC_BSP_IO_FS_REMOVE_ERROR = 8,
/** Can't write data to file. */
IOTC_BSP_IO_FS_WRITE_ERROR = 9,
/** Can't be read file. */
IOTC_BSP_IO_FS_READ_ERROR = 10,
/** Can't close file. */
IOTC_BSP_IO_FS_CLOSE_ERROR = 11,
} iotc_bsp_io_fs_state_t;
/**
* @typedef iotc_bsp_io_fs_resource_type_t
* @brief The resource type of TLS certificates.
*
* @see #iotc_bsp_io_fs_resource_type_e
*/
typedef enum iotc_bsp_io_fs_resource_type_e {
/** A TLS certificate resource. */
IOTC_BSP_IO_FS_CERTIFICATE = 0,
} iotc_bsp_io_fs_resource_type_t;
/**
* @typedef iotc_bsp_io_fs_stat_t
* @brief The size of TLS server authentication certificates.
* @see #iotc_bsp_io_fs_stat_s
*
* @struct iotc_bsp_io_fs_stat_s
* @brief The size of TLS server authentication certificates.
*/
typedef struct iotc_bsp_io_fs_stat_s {
/** The size, in bytes, of the resource. */
size_t resource_size;
} iotc_bsp_io_fs_stat_t;
/**
* @typedef iotc_bsp_io_fs_open_flags_t
* @brief The file operations.
*
* @see #iotc_bsp_io_fs_open_flags
*/
typedef enum iotc_bsp_io_fs_open_flags {
/** Open and read the file. */
IOTC_BSP_IO_FS_OPEN_READ = 1 << 0,
/** Open and write to the file. */
IOTC_BSP_IO_FS_OPEN_WRITE = 1 << 1,
/** Open and append to the file. */
IOTC_BSP_IO_FS_OPEN_APPEND = 1 << 2,
} iotc_bsp_io_fs_open_flags_t;
/**
* @brief Gets the size of a file.
*
* @param [in] resource_name The file name.
* @param [out] resource_stat The size, in bytes, of the file.
*/
iotc_bsp_io_fs_state_t iotc_bsp_io_fs_stat(
const char* const resource_name, iotc_bsp_io_fs_stat_t* resource_stat);
/**
* @details Opens a file.
*
* @param [in] resource_name The filename.
* @param [in] size The size, in bytes, of the file.
* @param [in] open_flags A {@link ::iotc_bsp_io_fs_open_flags_t file operation}
* bitmask.
* @param [out] resource_handle_out A
* {@link ::iotc_bsp_io_fs_resource_handle_t handle to an open file}.
*/
iotc_bsp_io_fs_state_t iotc_bsp_io_fs_open(
const char* const resource_name, const size_t size,
const iotc_bsp_io_fs_open_flags_t open_flags,
iotc_bsp_io_fs_resource_handle_t* resource_handle_out);
/**
* @brief Reads a file.
*
* @details The function must fill the buffer at offset 0. The function can
* allocate buffers by:
* - Allocating the buffer once, reusing it at each function call,
* and freeing it when the file closes.
* - Creating a new buffer each time the function is called and
* freeing the old buffer before the function returns.
*
* For example, see the <a href="../../../src/bsp/platform/posix/iotc_bsp_io_fs_posix.c#L193">POSIX implementation of this function</a>.
*
* @param [in] resource_handle A
* {@link ::iotc_bsp_io_fs_resource_handle_t handle to an open file}.
* @param [in] offset The position within the resource, in bytes, from which
* to start read operations.
* @param [out] buffer A pointer to a buffer with the bytes read from the file.
* The buffer is already allocated by the SDK.
* @param [out] buffer_size The number of bytes read from the file and stored
* in the buffer.
*/
iotc_bsp_io_fs_state_t iotc_bsp_io_fs_read(
const iotc_bsp_io_fs_resource_handle_t resource_handle, const size_t offset,
const uint8_t** buffer, size_t* const buffer_size);
/**
* @details Writes to a file.
*
* @param [in] resource_handle A
* {@link ::iotc_bsp_io_fs_resource_handle_t handle to an open file}.
* @param [in] buffer A pointer to a byte array with the data to write to the
* file.
* @param [in] buffer_size The size, in bytes of the buffer.
* @param [in] offset The position within the resource, in bytes, from which to
* start to the write operation.
* @param [out] bytes_written The number of bytes written to the file.
*/
iotc_bsp_io_fs_state_t iotc_bsp_io_fs_write(
const iotc_bsp_io_fs_resource_handle_t resource_handle,
const uint8_t* const buffer, const size_t buffer_size, const size_t offset,
size_t* const bytes_written);
/**
* @details Closes a file and frees all of the resources from reading or writing
* to the file.
*
* @param [in] resource_handle A
* {@link ::iotc_bsp_io_fs_resource_handle_t handle to an open file}.
*/
iotc_bsp_io_fs_state_t iotc_bsp_io_fs_close(
const iotc_bsp_io_fs_resource_handle_t resource_handle);
/**
* @brief Deletes a file.
*
* @param [in] resource_name The filename.
*/
iotc_bsp_io_fs_state_t iotc_bsp_io_fs_remove(const char* const resource_name);
#ifdef __cplusplus
}
#endif
#endif /* __IOTC_BSP_IO_FS_H__ */