K230 OTA API Reference#
Overview#
K230 provides an independent OTA HAL interface for streaming kdimg images to the OTA device node.
User-space HAL header file:
src/rtsmart/libs/rtsmart_hal/components/k230_ota/k230_ota.hUser-space HAL implementation:
src/rtsmart/libs/rtsmart_hal/components/k230_ota/k230_ota.cReference example:
src/rtsmart/examples/peripheral/ota/test_ota.c
Main Interfaces#
k230_ota_create#
k230_ota_t* k230_ota_create(void);
Creates an OTA session and opens the OTA device.
Return value: returns a non-NULL handle on success; returns
NULLon failure.
k230_ota_update#
int k230_ota_update(k230_ota_t* ctx, const void* buf, size_t size);
Writes a chunk of image data to the OTA device, suitable for loop-based chunked calls.
ctx: session handle returned byk230_ota_create()buf: data buffer to be writtensize: number of bytes to writeReturn value: returns
0on success; returns a negative value on failure
k230_ota_destroy#
void k230_ota_destroy(k230_ota_t* ctx);
Destroys the OTA session, releases resources, and closes the device.
k230_ota_write_file#
int k230_ota_write_file(const char* image_path, size_t chunk_size);
Convenience interface: directly writes the image file to the OTA device in chunks of chunk_size.
image_path: image pathchunk_size: size of each write (in bytes)Return value: returns
0on success; returns a negative value on failure
Recommended Usage Flow#
Prepare and verify the image file (recommended to first perform size, format, and integrity checks)
Call
k230_ota_create()to establish a sessionLoop to read the image and call
k230_ota_update()to write in chunksCall
k230_ota_destroy()to close the sessionReboot the device for the new firmware to take effect
Minimal Example#
#include "k230_ota.h"
#include <fcntl.h>
#include <unistd.h>
int ota_from_file(const char *path)
{
int fd = -1;
int ret = -1;
char buf[64 * 1024];
ssize_t rd;
k230_ota_t *ctx = NULL;
fd = open(path, O_RDONLY, 0);
if (fd < 0)
return -1;
ctx = k230_ota_create();
if (!ctx)
goto out;
while ((rd = read(fd, buf, sizeof(buf))) > 0) {
if (k230_ota_update(ctx, buf, (size_t)rd) < 0)
goto out;
}
ret = (rd < 0) ? -1 : 0;
out:
if (ctx)
k230_ota_destroy(ctx);
if (fd >= 0)
close(fd);
return ret;
}
Notes#
Avoid power loss or forced reboot during the OTA process.
It is recommended to complete image verification and version check before upgrading.
Network download capability can be implemented in combination with the NetMgmt API Documentation.
