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 "humidity_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_humidity_driver.h" 16094332d3Sopenharmony_ci 17094332d3Sopenharmony_ci#define HDF_LOG_TAG hdf_sensor_humidity_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_HUM_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_HUM_CRC8_MASK) ? ((value << SHT30_HUM_SHFIT_1_BIT) ^ SHT30_HUM_CRC8_POLYNOMIAL) : \ 34094332d3Sopenharmony_ci (value << SHT30_HUM_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 HumidityData *rawData, uint64_t *timestamp) 42094332d3Sopenharmony_ci{ 43094332d3Sopenharmony_ci OsalTimespec time; 44094332d3Sopenharmony_ci uint8_t value[SHT30_HUM_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_HUM_DATA_ADDR, value, sizeof(value)); 59094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 60094332d3Sopenharmony_ci 61094332d3Sopenharmony_ci tempValue = value[SHT30_HUM_VALUE_INDEX_THREE]; 62094332d3Sopenharmony_ci tempValue <<= SENSOR_DATA_WIDTH_8_BIT; 63094332d3Sopenharmony_ci tempValue |= value[SHT30_HUM_VALUE_INDEX_FOUR]; 64094332d3Sopenharmony_ci 65094332d3Sopenharmony_ci rawData->humidity = ((SHT30_HUM_SLOPE * tempValue) / SHT30_HUM_RESOLUTION); 66094332d3Sopenharmony_ci 67094332d3Sopenharmony_ci if (value[SHT30_HUM_VALUE_INDEX_FIVE] != \ 68094332d3Sopenharmony_ci Sht30CalcCrc8(value + SHT30_HUM_VALUE_INDEX_THREE, SHT30_HUM_CRC8_LEN)) { 69094332d3Sopenharmony_ci HDF_LOGE("%s: Calc humidity crc8 failed!", __func__); 70094332d3Sopenharmony_ci return HDF_FAILURE; 71094332d3Sopenharmony_ci } 72094332d3Sopenharmony_ci 73094332d3Sopenharmony_ci return HDF_SUCCESS; 74094332d3Sopenharmony_ci} 75094332d3Sopenharmony_ci 76094332d3Sopenharmony_cistatic int32_t ReadSht30Data(struct SensorCfgData *data) 77094332d3Sopenharmony_ci{ 78094332d3Sopenharmony_ci int32_t ret; 79094332d3Sopenharmony_ci static int32_t humidity; 80094332d3Sopenharmony_ci struct HumidityData rawData = { 0 }; 81094332d3Sopenharmony_ci OsalTimespec time; 82094332d3Sopenharmony_ci struct SensorReportEvent event; 83094332d3Sopenharmony_ci 84094332d3Sopenharmony_ci (void)memset_s(&time, sizeof(time), 0, sizeof(time)); 85094332d3Sopenharmony_ci (void)memset_s(&event, sizeof(event), 0, sizeof(event)); 86094332d3Sopenharmony_ci 87094332d3Sopenharmony_ci if (OsalGetTime(&time) != HDF_SUCCESS) { 88094332d3Sopenharmony_ci HDF_LOGE("%s: Get time failed", __func__); 89094332d3Sopenharmony_ci return HDF_FAILURE; 90094332d3Sopenharmony_ci } 91094332d3Sopenharmony_ci 92094332d3Sopenharmony_ci event.timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; 93094332d3Sopenharmony_ci 94094332d3Sopenharmony_ci ret = ReadSht30RawData(data, &rawData, &event.timestamp); 95094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 96094332d3Sopenharmony_ci HDF_LOGE("%s: SHT30 read raw data failed", __func__); 97094332d3Sopenharmony_ci return HDF_FAILURE; 98094332d3Sopenharmony_ci } 99094332d3Sopenharmony_ci 100094332d3Sopenharmony_ci humidity = rawData.humidity; 101094332d3Sopenharmony_ci 102094332d3Sopenharmony_ci event.sensorId = SENSOR_TAG_HUMIDITY; 103094332d3Sopenharmony_ci event.mode = SENSOR_WORK_MODE_REALTIME; 104094332d3Sopenharmony_ci event.dataLen = sizeof(humidity); 105094332d3Sopenharmony_ci event.data = (uint8_t *)&humidity; 106094332d3Sopenharmony_ci ret = ReportSensorEvent(&event); 107094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 108094332d3Sopenharmony_ci HDF_LOGE("%s: report data failed", __func__); 109094332d3Sopenharmony_ci } 110094332d3Sopenharmony_ci 111094332d3Sopenharmony_ci return ret; 112094332d3Sopenharmony_ci} 113094332d3Sopenharmony_ci 114094332d3Sopenharmony_cistatic int32_t InitSht30(struct SensorCfgData *data) 115094332d3Sopenharmony_ci{ 116094332d3Sopenharmony_ci int32_t ret; 117094332d3Sopenharmony_ci 118094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM); 119094332d3Sopenharmony_ci ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]); 120094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 121094332d3Sopenharmony_ci HDF_LOGE("%s: sensor init config failed", __func__); 122094332d3Sopenharmony_ci return HDF_FAILURE; 123094332d3Sopenharmony_ci } 124094332d3Sopenharmony_ci 125094332d3Sopenharmony_ci return HDF_SUCCESS; 126094332d3Sopenharmony_ci} 127094332d3Sopenharmony_ci 128094332d3Sopenharmony_cistatic int32_t DispatchSht30(struct HdfDeviceIoClient *client, 129094332d3Sopenharmony_ci int cmd, struct HdfSBuf *data, struct HdfSBuf *reply) 130094332d3Sopenharmony_ci{ 131094332d3Sopenharmony_ci (void)client; 132094332d3Sopenharmony_ci (void)cmd; 133094332d3Sopenharmony_ci (void)data; 134094332d3Sopenharmony_ci (void)reply; 135094332d3Sopenharmony_ci 136094332d3Sopenharmony_ci return HDF_SUCCESS; 137094332d3Sopenharmony_ci} 138094332d3Sopenharmony_ci 139094332d3Sopenharmony_cistatic int32_t Sht30BindDriver(struct HdfDeviceObject *device) 140094332d3Sopenharmony_ci{ 141094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); 142094332d3Sopenharmony_ci 143094332d3Sopenharmony_ci struct Sht30DrvData *drvData = (struct Sht30DrvData *)OsalMemCalloc(sizeof(*drvData)); 144094332d3Sopenharmony_ci if (drvData == NULL) { 145094332d3Sopenharmony_ci HDF_LOGE("%s: malloc drv data fail", __func__); 146094332d3Sopenharmony_ci return HDF_ERR_MALLOC_FAIL; 147094332d3Sopenharmony_ci } 148094332d3Sopenharmony_ci 149094332d3Sopenharmony_ci drvData->ioService.Dispatch = DispatchSht30; 150094332d3Sopenharmony_ci drvData->device = device; 151094332d3Sopenharmony_ci device->service = &drvData->ioService; 152094332d3Sopenharmony_ci g_sht30DrvData = drvData; 153094332d3Sopenharmony_ci 154094332d3Sopenharmony_ci return HDF_SUCCESS; 155094332d3Sopenharmony_ci} 156094332d3Sopenharmony_ci 157094332d3Sopenharmony_cistatic int32_t Sht30InitDriver(struct HdfDeviceObject *device) 158094332d3Sopenharmony_ci{ 159094332d3Sopenharmony_ci int32_t ret; 160094332d3Sopenharmony_ci struct HumidityOpsCall ops; 161094332d3Sopenharmony_ci 162094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); 163094332d3Sopenharmony_ci struct Sht30DrvData *drvData = (struct Sht30DrvData *)device->service; 164094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM); 165094332d3Sopenharmony_ci 166094332d3Sopenharmony_ci drvData->sensorCfg = HumidityCreateCfgData(device->property); 167094332d3Sopenharmony_ci if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) { 168094332d3Sopenharmony_ci HDF_LOGE("%s: Creating humidity cfg failed because detection failed", __func__); 169094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 170094332d3Sopenharmony_ci } 171094332d3Sopenharmony_ci 172094332d3Sopenharmony_ci ops.Init = NULL; 173094332d3Sopenharmony_ci ops.ReadData = ReadSht30Data; 174094332d3Sopenharmony_ci ret = HumidityRegisterChipOps(&ops); 175094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 176094332d3Sopenharmony_ci HDF_LOGE("%s: Register humidity failed", __func__); 177094332d3Sopenharmony_ci return HDF_FAILURE; 178094332d3Sopenharmony_ci } 179094332d3Sopenharmony_ci 180094332d3Sopenharmony_ci ret = InitSht30(drvData->sensorCfg); 181094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 182094332d3Sopenharmony_ci HDF_LOGE("%s: Init SHT30 humidity sensor failed", __func__); 183094332d3Sopenharmony_ci return HDF_FAILURE; 184094332d3Sopenharmony_ci } 185094332d3Sopenharmony_ci 186094332d3Sopenharmony_ci return HDF_SUCCESS; 187094332d3Sopenharmony_ci} 188094332d3Sopenharmony_ci 189094332d3Sopenharmony_cistatic void Sht30ReleaseDriver(struct HdfDeviceObject *device) 190094332d3Sopenharmony_ci{ 191094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(device); 192094332d3Sopenharmony_ci 193094332d3Sopenharmony_ci struct Sht30DrvData *drvData = (struct Sht30DrvData *)device->service; 194094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(drvData); 195094332d3Sopenharmony_ci 196094332d3Sopenharmony_ci if (drvData->sensorCfg != NULL) { 197094332d3Sopenharmony_ci HumidityReleaseCfgData(drvData->sensorCfg); 198094332d3Sopenharmony_ci drvData->sensorCfg = NULL; 199094332d3Sopenharmony_ci } 200094332d3Sopenharmony_ci OsalMemFree(drvData); 201094332d3Sopenharmony_ci} 202094332d3Sopenharmony_ci 203094332d3Sopenharmony_cistruct HdfDriverEntry g_humiditySht30DevEntry = { 204094332d3Sopenharmony_ci .moduleVersion = 1, 205094332d3Sopenharmony_ci .moduleName = "HDF_SENSOR_HUMIDITY_SHT30", 206094332d3Sopenharmony_ci .Bind = Sht30BindDriver, 207094332d3Sopenharmony_ci .Init = Sht30InitDriver, 208094332d3Sopenharmony_ci .Release = Sht30ReleaseDriver, 209094332d3Sopenharmony_ci}; 210094332d3Sopenharmony_ci 211094332d3Sopenharmony_ciHDF_INIT(g_humiditySht30DevEntry); 212