1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci *
4094332d3Sopenharmony_ci * HDF is dual licensed: you can use it either under the terms of
5094332d3Sopenharmony_ci * the GPL, or the BSD license, at your option.
6094332d3Sopenharmony_ci * See the LICENSE file in the root of this repository for complete details.
7094332d3Sopenharmony_ci */
8094332d3Sopenharmony_ci
9094332d3Sopenharmony_ci#include "temperature_sht30.h"
10094332d3Sopenharmony_ci#include <securec.h>
11094332d3Sopenharmony_ci#include "osal_mem.h"
12094332d3Sopenharmony_ci#include "osal_time.h"
13094332d3Sopenharmony_ci#include "sensor_config_controller.h"
14094332d3Sopenharmony_ci#include "sensor_device_manager.h"
15094332d3Sopenharmony_ci#include "sensor_temperature_driver.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci#define HDF_LOG_TAG    hdf_sensor_temperature_driver
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_cistatic struct Sht30DrvData *g_sht30DrvData = NULL;
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_cistatic struct Sht30DrvData *Sht30GetDrvData(void)
22094332d3Sopenharmony_ci{
23094332d3Sopenharmony_ci    return g_sht30DrvData;
24094332d3Sopenharmony_ci}
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cistatic uint8_t Sht30CalcCrc8(const uint8_t *data, uint32_t dataLen)
27094332d3Sopenharmony_ci{
28094332d3Sopenharmony_ci    uint8_t value = SHT30_TEMP_CRC8_BASE;
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ci    for (uint32_t i = dataLen; i; --i) {
31094332d3Sopenharmony_ci        value ^= *data++;
32094332d3Sopenharmony_ci        for (uint32_t j = SENSOR_DATA_WIDTH_8_BIT; j; --j) {
33094332d3Sopenharmony_ci            value = (value & SHT30_TEMP_CRC8_MASK) ? \
34094332d3Sopenharmony_ci                ((value << SHT30_TEMP_SHFIT_1_BIT) ^ SHT30_TEMP_CRC8_POLYNOMIAL) : (value << SHT30_TEMP_SHFIT_1_BIT);
35094332d3Sopenharmony_ci        }
36094332d3Sopenharmony_ci    }
37094332d3Sopenharmony_ci
38094332d3Sopenharmony_ci    return value;
39094332d3Sopenharmony_ci}
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_cistatic int32_t ReadSht30RawData(struct SensorCfgData *data, struct TemperatureData *rawData, uint64_t *timestamp)
42094332d3Sopenharmony_ci{
43094332d3Sopenharmony_ci    OsalTimespec time;
44094332d3Sopenharmony_ci    uint8_t value[SHT30_TEMP_DATA_BUF_LEN];
45094332d3Sopenharmony_ci    uint16_t tempValue;
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
52094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
53094332d3Sopenharmony_ci        return HDF_FAILURE;
54094332d3Sopenharmony_ci    }
55094332d3Sopenharmony_ci
56094332d3Sopenharmony_ci    *timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci    int32_t ret = ReadSensor(&data->busCfg, SHT30_TEMP_DATA_ADDR, value, sizeof(value));
59094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_ci    tempValue = value[SHT30_TEMP_VALUE_IDX_ZERO];
62094332d3Sopenharmony_ci    tempValue <<= SENSOR_DATA_WIDTH_8_BIT;
63094332d3Sopenharmony_ci    tempValue |= value[SHT30_TEMP_VALUE_IDX_ONE];
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    rawData->temperature = (SHT30_TEMP_CONSATNT + ((SHT30_TEMP_SLOPE *  tempValue) / 0xFFFF));
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    if (value[SHT30_TEMP_VALUE_IDX_TWO] != Sht30CalcCrc8(value, SHT30_TEMP_CRC8_LEN)) {
68094332d3Sopenharmony_ci        HDF_LOGE("%s: Calc temperature crc8 failed!", __func__);
69094332d3Sopenharmony_ci        return HDF_FAILURE;
70094332d3Sopenharmony_ci    }
71094332d3Sopenharmony_ci
72094332d3Sopenharmony_ci    return HDF_SUCCESS;
73094332d3Sopenharmony_ci}
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_cistatic int32_t ReadSht30Data(struct SensorCfgData *data)
76094332d3Sopenharmony_ci{
77094332d3Sopenharmony_ci    int32_t ret;
78094332d3Sopenharmony_ci    static int32_t temperature;
79094332d3Sopenharmony_ci    struct TemperatureData rawData = { 0 };
80094332d3Sopenharmony_ci    OsalTimespec time;
81094332d3Sopenharmony_ci    struct SensorReportEvent event;
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
84094332d3Sopenharmony_ci    (void)memset_s(&event, sizeof(event), 0, sizeof(event));
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
87094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
88094332d3Sopenharmony_ci        return HDF_FAILURE;
89094332d3Sopenharmony_ci    }
90094332d3Sopenharmony_ci
91094332d3Sopenharmony_ci    event.timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT;
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_ci    ret = ReadSht30RawData(data, &rawData, &event.timestamp);
94094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
95094332d3Sopenharmony_ci        HDF_LOGE("%s: SHT30 read raw data failed", __func__);
96094332d3Sopenharmony_ci        return HDF_FAILURE;
97094332d3Sopenharmony_ci    }
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_ci    temperature = rawData.temperature;
100094332d3Sopenharmony_ci
101094332d3Sopenharmony_ci    event.sensorId = SENSOR_TAG_TEMPERATURE;
102094332d3Sopenharmony_ci    event.mode = SENSOR_WORK_MODE_REALTIME;
103094332d3Sopenharmony_ci    event.dataLen = sizeof(temperature);
104094332d3Sopenharmony_ci    event.data = (uint8_t *)&temperature;
105094332d3Sopenharmony_ci    ret = ReportSensorEvent(&event);
106094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
107094332d3Sopenharmony_ci        HDF_LOGE("%s: report data failed", __func__);
108094332d3Sopenharmony_ci    }
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_ci    return ret;
111094332d3Sopenharmony_ci}
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_cistatic int32_t InitSht30(struct SensorCfgData *data)
114094332d3Sopenharmony_ci{
115094332d3Sopenharmony_ci    int32_t ret;
116094332d3Sopenharmony_ci
117094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
118094332d3Sopenharmony_ci    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
119094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
120094332d3Sopenharmony_ci        HDF_LOGE("%s: sensor init config failed", __func__);
121094332d3Sopenharmony_ci        return HDF_FAILURE;
122094332d3Sopenharmony_ci    }
123094332d3Sopenharmony_ci
124094332d3Sopenharmony_ci    return HDF_SUCCESS;
125094332d3Sopenharmony_ci}
126094332d3Sopenharmony_ci
127094332d3Sopenharmony_cistatic int32_t DispatchSht30(struct HdfDeviceIoClient *client,
128094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
129094332d3Sopenharmony_ci{
130094332d3Sopenharmony_ci    (void)client;
131094332d3Sopenharmony_ci    (void)cmd;
132094332d3Sopenharmony_ci    (void)data;
133094332d3Sopenharmony_ci    (void)reply;
134094332d3Sopenharmony_ci
135094332d3Sopenharmony_ci    return HDF_SUCCESS;
136094332d3Sopenharmony_ci}
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_cistatic int32_t Sht30BindDriver(struct HdfDeviceObject *device)
139094332d3Sopenharmony_ci{
140094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
141094332d3Sopenharmony_ci
142094332d3Sopenharmony_ci    struct Sht30DrvData *drvData = (struct Sht30DrvData *)OsalMemCalloc(sizeof(*drvData));
143094332d3Sopenharmony_ci    if (drvData == NULL) {
144094332d3Sopenharmony_ci        HDF_LOGE("%s: malloc drv data fail", __func__);
145094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
146094332d3Sopenharmony_ci    }
147094332d3Sopenharmony_ci
148094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchSht30;
149094332d3Sopenharmony_ci    drvData->device = device;
150094332d3Sopenharmony_ci    device->service = &drvData->ioService;
151094332d3Sopenharmony_ci    g_sht30DrvData = drvData;
152094332d3Sopenharmony_ci
153094332d3Sopenharmony_ci    return HDF_SUCCESS;
154094332d3Sopenharmony_ci}
155094332d3Sopenharmony_ci
156094332d3Sopenharmony_cistatic int32_t Sht30InitDriver(struct HdfDeviceObject *device)
157094332d3Sopenharmony_ci{
158094332d3Sopenharmony_ci    int32_t ret;
159094332d3Sopenharmony_ci    struct TemperatureOpsCall ops;
160094332d3Sopenharmony_ci
161094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
162094332d3Sopenharmony_ci    struct Sht30DrvData *drvData = (struct Sht30DrvData *)device->service;
163094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
164094332d3Sopenharmony_ci
165094332d3Sopenharmony_ci    drvData->sensorCfg = TemperatureCreateCfgData(device->property);
166094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
167094332d3Sopenharmony_ci        HDF_LOGE("%s: Creating temperature cfg failed because detection failed", __func__);
168094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
169094332d3Sopenharmony_ci    }
170094332d3Sopenharmony_ci
171094332d3Sopenharmony_ci    ops.Init = NULL;
172094332d3Sopenharmony_ci    ops.ReadData = ReadSht30Data;
173094332d3Sopenharmony_ci    ret = TemperatureRegisterChipOps(&ops);
174094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
175094332d3Sopenharmony_ci        HDF_LOGE("%s: Register temperature failed", __func__);
176094332d3Sopenharmony_ci        return HDF_FAILURE;
177094332d3Sopenharmony_ci    }
178094332d3Sopenharmony_ci
179094332d3Sopenharmony_ci    ret = InitSht30(drvData->sensorCfg);
180094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
181094332d3Sopenharmony_ci        HDF_LOGE("%s: Init SHT30 temperature sensor failed", __func__);
182094332d3Sopenharmony_ci        return HDF_FAILURE;
183094332d3Sopenharmony_ci    }
184094332d3Sopenharmony_ci
185094332d3Sopenharmony_ci    return HDF_SUCCESS;
186094332d3Sopenharmony_ci}
187094332d3Sopenharmony_ci
188094332d3Sopenharmony_cistatic void Sht30ReleaseDriver(struct HdfDeviceObject *device)
189094332d3Sopenharmony_ci{
190094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
191094332d3Sopenharmony_ci
192094332d3Sopenharmony_ci    struct Sht30DrvData *drvData = (struct Sht30DrvData *)device->service;
193094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
194094332d3Sopenharmony_ci
195094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
196094332d3Sopenharmony_ci        TemperatureReleaseCfgData(drvData->sensorCfg);
197094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
198094332d3Sopenharmony_ci    }
199094332d3Sopenharmony_ci    OsalMemFree(drvData);
200094332d3Sopenharmony_ci}
201094332d3Sopenharmony_ci
202094332d3Sopenharmony_cistruct HdfDriverEntry g_temperatureSht30DevEntry = {
203094332d3Sopenharmony_ci    .moduleVersion = 1,
204094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_TEMPERATURE_SHT30",
205094332d3Sopenharmony_ci    .Bind = Sht30BindDriver,
206094332d3Sopenharmony_ci    .Init = Sht30InitDriver,
207094332d3Sopenharmony_ci    .Release = Sht30ReleaseDriver,
208094332d3Sopenharmony_ci};
209094332d3Sopenharmony_ci
210094332d3Sopenharmony_ciHDF_INIT(g_temperatureSht30DevEntry);
211