• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "vibrator_decoder_creator.h"
16 
17 #include <fcntl.h>
18 #include <unistd.h>
19 
20 #include "default_vibrator_decoder_factory.h"
21 #include "file_utils.h"
22 #include "he_vibrator_decoder_factory.h"
23 #include "sensors_errors.h"
24 
25 #undef LOG_TAG
26 #define LOG_TAG "VibratorDecoderCreator"
27 
28 namespace OHOS {
29 namespace Sensors {
30 
CreateDecoder(const RawFileDescriptor & fd)31 IVibratorDecoder *VibratorDecoderCreator::CreateDecoder(const RawFileDescriptor &fd)
32 {
33     CALL_LOG_ENTER;
34     DecoderType type = GetDecoderType(fd);
35     if (type == DECODER_TYPE_HE) {
36         MISC_HILOGD("Get he decoder");
37         HEVibratorDecoderFactory factory;
38         return factory.CreateDecoder();
39     } else if (type == DECODER_TYPE_OH_JSON) {
40         MISC_HILOGD("Get oh_json decoder");
41         DefaultVibratorDecoderFactory factory;
42         return factory.CreateDecoder();
43     }
44     MISC_HILOGE("Invalid decoder type");
45     return nullptr;
46 }
47 
GetDecoderType(const RawFileDescriptor & rawFd)48 DecoderType VibratorDecoderCreator::GetDecoderType(const RawFileDescriptor &rawFd)
49 {
50     std::string extName = "";
51     int32_t ret = GetFileExtName(rawFd.fd, extName);
52     if (ret != SUCCESS) {
53         MISC_HILOGE("GetFileExtName failed");
54         return DECODER_TYPE_BUTT;
55     }
56     if (extName == "he") {
57         return DECODER_TYPE_HE;
58     } else if (extName == "json") {
59         return DECODER_TYPE_OH_JSON;
60     } else {
61         MISC_HILOGE("Invalid decoder extend name");
62         return DECODER_TYPE_BUTT;
63     }
64 }
65 
Create(const RawFileDescriptor & rawFd)66 extern "C" IVibratorDecoder *Create(const RawFileDescriptor &rawFd)
67 {
68     VibratorDecoderCreator creator;
69     return creator.CreateDecoder(rawFd);
70 }
71 
Destroy(IVibratorDecoder * decoder)72 extern "C" void Destroy(IVibratorDecoder *decoder)
73 {
74     if (decoder != nullptr) {
75         delete decoder;
76         decoder = nullptr;
77     }
78 }
79 }  // namespace Sensors
80 }  // namespace OHOS