• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "sample_plugin.h"
16 
17 #include "securec.h"
18 namespace {
19 constexpr int MAX_INT = 10;
20 constexpr int MAX_DOUBLE = 100;
21 } // namespace
22 
SamplePlugin()23 SamplePlugin::SamplePlugin() {}
24 
~SamplePlugin()25 SamplePlugin::~SamplePlugin() {}
26 
GetTimeMS()27 uint64_t SamplePlugin::GetTimeMS()
28 {
29     const int MS_PER_S = 1000;
30     const int NS_PER_MS = 1000000;
31     struct timespec ts;
32     clock_gettime(CLOCK_BOOTTIME, &ts);
33     return ts.tv_sec * MS_PER_S + ts.tv_nsec / NS_PER_MS;
34 }
35 
Start(const uint8_t * configData,uint32_t configSize)36 int SamplePlugin::Start(const uint8_t* configData, uint32_t configSize)
37 {
38     HILOG_INFO(LOG_CORE, "%s:config data -->configSize=%d", __func__, configSize);
39     CHECK_TRUE(configData != nullptr, -1, "SamplePlugin: param invalid!!!");
40     for (uint32_t i = 0; i < configSize; i++) {
41         HILOG_INFO(LOG_CORE, "%s:configData[%d] = 0x%02x", __func__, i, configData[i]);
42     }
43 
44     // 反序列化
45     if (protoConfig_.ParseFromArray(configData, configSize) <= 0) {
46         HILOG_ERROR(LOG_CORE, "%s:parseFromArray failed!", __func__);
47         return -1;
48     }
49     HILOG_INFO(LOG_CORE, "%s:pid = %d", __func__, protoConfig_.pid());
50     // 插件准备工作
51 
52     return 0;
53 }
54 
Report(uint8_t * data,uint32_t dataSize)55 int SamplePlugin::Report(uint8_t* data, uint32_t dataSize)
56 {
57     SampleData dataProto;
58 
59     // 回填数据
60     int intData = rand() % MAX_INT;
61     double doubleData = rand() % MAX_DOUBLE;
62     dataProto.set_time_ms(GetTimeMS());
63     dataProto.set_int_data(intData);
64     dataProto.set_double_data(doubleData);
65 
66     uint32_t length = dataProto.ByteSizeLong();
67     if (length > dataSize) {
68         return -length;
69     }
70     // 序列化
71     if (dataProto.SerializeToArray(data, length) > 0) {
72         HILOG_DEBUG(LOG_CORE, "%s:report success! length = %d", __func__, length);
73         return length;
74     }
75     return 0;
76 }
77 
Stop()78 int SamplePlugin::Stop()
79 {
80     HILOG_INFO(LOG_CORE, "%s:stop success!", __func__);
81     return 0;
82 }