1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "easy_event_builder.h"
17
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "easy_def.h"
24 #include "easy_event_encoder.h"
25 #include "easy_util.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 static const uint8_t STR_PARAM_VALUE_TYPE = 12; // refer to enum ValueType: ValueType::STRING
32 static const int DATA_MAX_LEN = 1024;
33
AppendHeader(uint8_t* eventBuffer, const size_t bufferLen, size_t* offset, const struct HiSysEventEasyHeader* header)34 int AppendHeader(uint8_t* eventBuffer, const size_t bufferLen, size_t* offset,
35 const struct HiSysEventEasyHeader* header)
36 {
37 if ((eventBuffer == NULL) || (offset == NULL) || (header == NULL)) {
38 return ERR_EVENT_BUF_INVALID;
39 }
40 if ((bufferLen < *offset) || ((bufferLen - *offset) < sizeof(struct HiSysEventEasyHeader))) {
41 return ERR_MEM_OPT_FAILED;
42 }
43 *((struct HiSysEventEasyHeader*)(eventBuffer + *offset)) = *header;
44 *offset += sizeof(struct HiSysEventEasyHeader);
45 return SUCCESS;
46 }
47
AppendStringParam(uint8_t* eventBuffer, const size_t bufferLen, size_t* offset, const char* key, const char* val)48 int AppendStringParam(uint8_t* eventBuffer, const size_t bufferLen, size_t* offset, const char* key, const char* val)
49 {
50 if ((eventBuffer == NULL) || (offset == NULL) || (key == NULL)) {
51 return ERR_EVENT_BUF_INVALID;
52 }
53 if ((val == NULL) || (strlen(val) > DATA_MAX_LEN)) {
54 return ERR_PARAM_VALUE_INVALID;
55 }
56 // append key
57 int ret = EncodeStringValue(eventBuffer, bufferLen, offset, key);
58 if (ret != SUCCESS) {
59 return ret;
60 }
61 // append value type
62 struct HiSysEventEasyParamValueType paramValueType;
63 paramValueType.isArray = 0;
64 paramValueType.valueType = STR_PARAM_VALUE_TYPE;
65 paramValueType.valueByteCnt = 0;
66 ret = EncodeValueType(eventBuffer, bufferLen, offset, ¶mValueType);
67 if (ret != SUCCESS) {
68 return ret;
69 }
70 // append value
71 ret = EncodeStringValue(eventBuffer, bufferLen, offset, val);
72 if (ret != SUCCESS) {
73 return ret;
74 }
75 return SUCCESS;
76 }
77
78 #ifdef __cplusplus
79 }
80 #endif