Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MCUmgr and SMP Client support #56934

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
193 changes: 193 additions & 0 deletions include/zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef H_IMG_MGMT_CLIENT_
#define H_IMG_MGMT_CLIENT_

#include <inttypes.h>
#include <zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h>
#include <zephyr/mgmt/mcumgr/smp/smp_client.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Image list flags.
*/
struct mcumgr_image_list_flags {
/** Bootable image */
bool bootable: 1;
/** Pending update state */
bool pending: 1;
/** Confirmed image */
bool confirmed: 1;
/** Active image */
bool active: 1;
/** Permanent image state */
bool permanent: 1;
};

/**
* @brief Image list data.
*/
struct mcumgr_image_data {
/** Image slot num */
uint32_t slot_num;
/** Image number */
uint32_t img_num;
/** Image SHA256 checksum */
char hash[IMG_MGMT_HASH_LEN];
/** Image Version */
char version[IMG_MGMT_VER_MAX_STR_LEN + 1];
/** Image Flags */
struct mcumgr_image_list_flags flags;
};

/**
* @brief MCUmgr Image list response.
*/
struct mcumgr_image_state {
/** Status */
enum mcumgr_err_t status;
/** Length of image_list */
int image_list_length;
nordicjm marked this conversation as resolved.
Show resolved Hide resolved
/** Image list pointer */
struct mcumgr_image_data *image_list;
};

/**
* @brief MCUmgr Image upload response.
*/
struct mcumgr_image_upload {
/** Status */
enum mcumgr_err_t status;
/** Reported image offset */
size_t image_upload_offset;
};

/**
* @brief IMG mgmt client upload structure
*
* Structure is used internally by the client
*/
struct img_gr_upload {
/** Image 256-bit hash */
char sha256[IMG_MGMT_HASH_LEN];
/** True when Hash is configured, false when not */
bool hash_initialized;
/** Image size */
size_t image_size;
/** Image upload offset state */
size_t offset;
/** Worst case init upload message size */
size_t upload_header_size;
/** Image slot num */
uint32_t image_num;
};

/**
* @brief IMG mgmt client object.
*/
struct img_mgmt_client {
/** SMP client object */
struct smp_client_object *smp_client;
/** Image Upload state data for client internal use */
struct img_gr_upload upload;
/** Client image list buffer size */
int image_list_length;
/** Image list buffer */
struct mcumgr_image_data *image_list;
/** Command status */
int status;
};

/**
* @brief Inilialize image group client.
*
* Function initializes image group client for given SMP client using supplied image data.
*
* @param client IMG mgmt client object
* @param smp_client SMP client object
* @param image_list_size Length of image_list buffer.
* @param image_list Image list buffer pointer.
*
*/
void img_mgmt_client_init(struct img_mgmt_client *client, struct smp_client_object *smp_client,
int image_list_size, struct mcumgr_image_data *image_list);

/**
* @brief Initialize image upload.
*
* @param client IMG mgmt client object
* @param image_size Size of image in bytes.
* @param image_num Image slot Num.
* @param image_hash Pointer to HASH for image must be SHA256 hash of entire upload
* if present (32 bytes). Use NULL when HASH from image is not available.
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/
int img_mgmt_client_upload_init(struct img_mgmt_client *client, size_t image_size,
uint32_t image_num, const char *image_hash);

/**
* @brief Upload part of image.
*
* @param client IMG mgmt client object
* @param data Pointer to data.
* @param length Length of data
* @param res_buf Pointer for command response structure.
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/
int img_mgmt_client_upload(struct img_mgmt_client *client, const uint8_t *data, size_t length,
struct mcumgr_image_upload *res_buf);

/**
* @brief Write image state.
*
* @param client IMG mgmt client object
* @param hash Pointer to Hash (Needed for test).
* @param confirm Set false for test and true for confirmation.
* @param res_buf Pointer for command response structure.
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/

int img_mgmt_client_state_write(struct img_mgmt_client *client, char *hash, bool confirm,
struct mcumgr_image_state *res_buf);

/**
* @brief Read image state.
*
* @param client IMG mgmt client object
* @param res_buf Pointer for command response structure.
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/
int img_mgmt_client_state_read(struct img_mgmt_client *client, struct mcumgr_image_state *res_buf);

/**
* @brief Erase selected Image Slot
*
* @param client IMG mgmt client object
* @param slot Slot number
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/

int img_mgmt_client_erase(struct img_mgmt_client *client, uint32_t slot);

#ifdef __cplusplus
}
#endif

#endif /* H_IMG_MGMT_CLIENT_ */
61 changes: 61 additions & 0 deletions include/zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef H_OS_MGMT_CLIENT_
#define H_OS_MGMT_CLIENT_

#include <inttypes.h>
#include <zephyr/mgmt/mcumgr/smp/smp_client.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief OS mgmt client object
*/
struct os_mgmt_client {
/** SMP client object */
jheiskan81 marked this conversation as resolved.
Show resolved Hide resolved
struct smp_client_object *smp_client;
/** Command status */
int status;
};

/**
* @brief Initialize OS management client.
*
* @param client OS mgmt client object
* @param smp_client SMP client object
*
*/
void os_mgmt_client_init(struct os_mgmt_client *client, struct smp_client_object *smp_client);

/**
* @brief Send SMP message for Echo command.
*
* @param client OS mgmt client object
* @param echo_string Echo string
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/
int os_mgmt_client_echo(struct os_mgmt_client *client, const char *echo_string);

/**
* @brief Send SMP Reset command.
*
* @param client OS mgmt client object
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/
int os_mgmt_client_reset(struct os_mgmt_client *client);

#ifdef __cplusplus
}
#endif

#endif /* H_OS_MGMT_CLIENT_ */
109 changes: 109 additions & 0 deletions include/zephyr/mgmt/mcumgr/smp/smp_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
jheiskan81 marked this conversation as resolved.
Show resolved Hide resolved
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef H_SMP_CLIENT_
#define H_SMP_CLIENT_

#include <zephyr/kernel.h>
#include <zephyr/net/buf.h>
#include <mgmt/mcumgr/transport/smp_internal.h>
#include <zephyr/mgmt/mcumgr/smp/smp.h>
#include <zephyr/mgmt/mcumgr/transport/smp.h>

/**
* @brief SMP client object
*/
struct smp_client_object {
/** Must be the first member. */
struct k_work work;
/** FIFO for client TX queue */
struct k_fifo tx_fifo;
/** SMP transport object */
struct smp_transport *smpt;
/** SMP SEQ */
uint8_t smp_seq;
};

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Initialize a SMP client object.
*
* @param smp_client The Client to construct.
* @param smp_type SMP transport type for discovering transport object
*
* @return 0 if successful
* @return mcumgr_err_t code on failure
*/
int smp_client_object_init(struct smp_client_object *smp_client, int smp_type);

/**
* @brief Response callback for SMP send.
*
* @param nb net_buf for response
* @param user_data same user data that was provided as part of the request
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/
typedef int (*smp_client_res_fn)(struct net_buf *nb, void *user_data);
jheiskan81 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief SMP client response handler.
*
* @param nb response net_buf
* @param res_hdr Parsed SMP header
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/
int smp_client_single_response(struct net_buf *nb, const struct smp_hdr *res_hdr);

/**
* @brief Allocate buffer and initialize with SMP header.
*
* @param smp_client SMP client object
* @param group SMP group id
* @param command_id SMP command id
* @param op SMP operation type
* @param version SMP MCUmgr version
*
* @return A newly-allocated buffer net_buf on success
* @return NULL on failure.
*/
struct net_buf *smp_client_buf_allocation(struct smp_client_object *smp_client, uint16_t group,
uint8_t command_id, uint8_t op,
enum smp_mcumgr_version_t version);
/**
* @brief Free a SMP client buffer.
*
* @param nb The net_buf to free.
*/
void smp_client_buf_free(struct net_buf *nb);

/**
* @brief SMP client data send request.
*
* @param smp_client SMP client object
* @param nb net_buf packet for send
* @param cb Callback for response handler
* @param user_data user defined data pointer which will be returned back to response callback
* @param timeout_in_sec Timeout in seconds for send process. Client will retry transport
* based CONFIG_SMP_CMD_RETRY_TIME
*
* @return 0 on success.
* @return @ref mcumgr_err_t code on failure.
*/
int smp_client_send_cmd(struct smp_client_object *smp_client, struct net_buf *nb,
smp_client_res_fn cb, void *user_data, int timeout_in_sec);

#ifdef __cplusplus
}
#endif

#endif /* H_SMP_CLIENT_ */