1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "os/mynewt.h"
21 #include "modlog/modlog.h"
22 #include "cborattr/cborattr.h"
23 #include "img_mgmt/img_mgmt.h"
24
25 /**
26 * Log event types (all events are CBOR-encoded):
27 *
28 * upstart:
29 * When: upon receiving an upload request with an offset of 0.
30 * Structure:
31 * {
32 * "ev": "upstart",
33 * "rc": <mgmt-error-code (int)>
34 * }
35 *
36 * updone:
37 * When: upon receiving an upload request containing the final chunk of an
38 * image OR a failed upload request with a non-zero offset.
39 * Structure:
40 * {
41 * "ev": "updone",
42 * "rc": <mgmt-error-code (int)>
43 * "hs": <image-hash (byte-string)> (only present on success)
44 * }
45 *
46 * pend:
47 * When: upon receiving a non-permanent `set-pending` request.
48 * Structure:
49 * {
50 * "ev": "pend",
51 * "rc": <mgmt-error-code (int)>,
52 * "hs": <image-hash (byte-string)>
53 * }
54 *
55 * conf:
56 * When: upon receiving a `confirm` request OR a permanent `set-pending`
57 * request.
58 * Structure:
59 * {
60 * "ev": "conf",
61 * "rc": <mgmt-error-code (int)>,
62 * "hs": <image-hash (byte-string)> (only present for `set-pending`)
63 * }
64 */
65
66 #define IMG_MGMT_LOG_EV_UPSTART "upstart"
67 #define IMG_MGMT_LOG_EV_UPDONE "updone"
68 #define IMG_MGMT_LOG_EV_PEND "pend"
69 #define IMG_MGMT_LOG_EV_CONF "conf"
70
71 static int
img_mgmt_log_gen(const char * ev,int status,const uint8_t * hash)72 img_mgmt_log_gen(const char *ev, int status, const uint8_t *hash)
73 {
74 #if LOG_MOD_LEVEL_IS_ACTIVE(MYNEWT_VAL(IMG_MGMT_LOG_LVL), LOG_LEVEL_INFO)
75
76 struct os_mbuf *om;
77 int rc;
78
79 const struct cbor_out_attr_t attrs[] = {
80 {
81 .attribute = "ev",
82 .val = {
83 .type = CborAttrTextStringType,
84 .string = ev,
85 },
86 },
87 {
88 .attribute = "rc",
89 .val = {
90 .type = CborAttrIntegerType,
91 .integer = status,
92 },
93 },
94 {
95 .attribute = "hs",
96 .val = {
97 .type = CborAttrByteStringType,
98 .bytestring.data = hash,
99 .bytestring.len = IMG_MGMT_HASH_LEN,
100 },
101 .omit = hash == NULL,
102 },
103 { 0 }
104 };
105
106 rc = cbor_write_object_msys(attrs, &om);
107 if (rc != 0) {
108 return rc;
109 }
110
111 modlog_append_mbuf(MYNEWT_VAL(IMG_MGMT_LOG_MOD), LOG_LEVEL_INFO,
112 LOG_ETYPE_CBOR, om);
113 #endif
114
115 return 0;
116 }
117
118 int
img_mgmt_impl_log_upload_start(int status)119 img_mgmt_impl_log_upload_start(int status)
120 {
121 return img_mgmt_log_gen(IMG_MGMT_LOG_EV_UPSTART, status, NULL);
122 }
123
124 int
img_mgmt_impl_log_upload_done(int status,const uint8_t * hash)125 img_mgmt_impl_log_upload_done(int status, const uint8_t *hash)
126 {
127 return img_mgmt_log_gen(IMG_MGMT_LOG_EV_UPDONE, 0, hash);
128 }
129
130 int
img_mgmt_impl_log_pending(int status,const uint8_t * hash)131 img_mgmt_impl_log_pending(int status, const uint8_t *hash)
132 {
133 return img_mgmt_log_gen(IMG_MGMT_LOG_EV_PEND, status, hash);
134 }
135
136 int
img_mgmt_impl_log_confirm(int status,const uint8_t * hash)137 img_mgmt_impl_log_confirm(int status, const uint8_t *hash)
138 {
139 return img_mgmt_log_gen(IMG_MGMT_LOG_EV_CONF, status, hash);
140 }
141