1 /*
2  * Copyright (c) 2018-2021 mcumgr authors
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <string.h>
9 
10 #include <zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h>
11 
12 int
img_mgmt_ver_str(const struct image_version * ver,char * dst)13 img_mgmt_ver_str(const struct image_version *ver, char *dst)
14 {
15 	int rc = 0;
16 	int rc1 = 0;
17 
18 	rc = snprintf(dst, IMG_MGMT_VER_MAX_STR_LEN, "%hu.%hu.%hu",
19 		(uint16_t)ver->iv_major, (uint16_t)ver->iv_minor,
20 		ver->iv_revision);
21 
22 	if (rc >= 0 && ver->iv_build_num != 0) {
23 		rc1 = snprintf(&dst[rc], IMG_MGMT_VER_MAX_STR_LEN - rc, ".%u",
24 			ver->iv_build_num);
25 	}
26 
27 	if (rc1 >= 0 && rc >= 0) {
28 		rc = rc + rc1;
29 	} else {
30 		/* If any failed then all failed */
31 		rc = -1;
32 	}
33 
34 	return rc;
35 }
36