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 
16 #include "light_dump.h"
17 #include <securec.h>
18 #include <stdio.h>
19 #include "devhost_dump_reg.h"
20 #include "hdf_base.h"
21 #include "light_uhdf_log.h"
22 #include "light_controller.h"
23 #include "light_type.h"
24 
25 #define HDF_LOG_TAG    uhdf_light_service
26 
27 #define STRING_LEN    1024
28 
29 static const char *g_dumpHelp =
30     " usage:\n"
31     " -h, --help: dump help\n"
32     " -c, --channel: dump the light channel info\n";
33 
ShowLightInfo(struct HdfSBuf * reply)34 static int32_t ShowLightInfo(struct HdfSBuf *reply)
35 {
36     uint32_t i;
37     int32_t ret;
38     uint8_t *lightState = NULL;
39     struct LightDevice *lightDevice = NULL;
40     char lightInfo[STRING_LEN] = {0};
41 
42     lightState = GetLightState();
43     if (lightState == NULL) {
44         HDF_LOGE("%{public}s: get light state failed", __func__);
45         return HDF_FAILURE;
46     }
47 
48     lightDevice = GetLightDevicePriv();
49     if (lightDevice == NULL || lightDevice->lightInfoEntry == NULL ||
50         lightDevice->lightNum == 0) {
51         HDF_LOGE("%{public}s: get light device info failed", __func__);
52         return HDF_FAILURE;
53     }
54     for (i = 0; i < lightDevice->lightNum; i++) {
55         ret = memset_s(lightInfo, STRING_LEN, 0, STRING_LEN);
56         if (ret != HDF_SUCCESS) {
57             HDF_LOGE("%{publuc}s: memset sensorInfoList is failed\n", __func__);
58             return HDF_FAILURE;
59         }
60 
61         ret = sprintf_s(lightInfo, STRING_LEN,
62             " lightId: %u\n state: %hhu\n lightNumber: %u\n lightName: %s\n lightType: %d\n",
63             lightDevice->lightInfoEntry->lightId,
64             lightState[lightDevice->lightInfoEntry->lightId],
65             lightDevice->lightInfoEntry->lightNumber,
66             lightDevice->lightInfoEntry->lightName,
67             lightDevice->lightInfoEntry->lightType);
68         if (ret < 0) {
69             HDF_LOGE("%{public}s: sprintf light info failed", __func__);
70             return HDF_FAILURE;
71         }
72 
73         if (!HdfSbufWriteString(reply, lightInfo)) {
74             HDF_LOGE("%{public}s: write lightInfo failed", __func__);
75             return HDF_FAILURE;
76         }
77     }
78 
79     return HDF_SUCCESS;
80 }
81 
ShowLightEffectInfo(struct HdfSBuf * reply)82 static int32_t ShowLightEffectInfo(struct HdfSBuf *reply)
83 {
84     int32_t ret;
85     struct LightEffect *lightEffect = NULL;
86     char lightEffectInfo[STRING_LEN] = {0};
87 
88     lightEffect = GetLightEffect();
89     if (lightEffect == NULL) {
90         HDF_LOGE("%{public}s: get light effect info failed", __func__);
91         return HDF_FAILURE;
92     }
93 
94     ret = memset_s(lightEffectInfo, STRING_LEN, 0, STRING_LEN);
95     if (ret != HDF_SUCCESS) {
96         HDF_LOGE("%{publuc}s: memset sensorInfoList is failed\n", __func__);
97         return HDF_FAILURE;
98     }
99 
100     ret = sprintf_s(lightEffectInfo, STRING_LEN,
101         " r: %hhu\n g: %hhu\n b: %hhu\n flashMode: %d\n onTime: %d\n offTime: %d\n",
102         lightEffect->lightColor.colorValue.rgbColor.r,
103         lightEffect->lightColor.colorValue.rgbColor.g,
104         lightEffect->lightColor.colorValue.rgbColor.b,
105         lightEffect->flashEffect.flashMode,
106         lightEffect->flashEffect.onTime,
107         lightEffect->flashEffect.offTime);
108     if (ret < 0) {
109         HDF_LOGE("%{public}s: sprintf light effect info failed", __func__);
110         return HDF_FAILURE;
111     }
112 
113     if (!HdfSbufWriteString(reply, lightEffectInfo)) {
114         HDF_LOGE("%{public}s: write lightEffectInfo failed", __func__);
115         return HDF_FAILURE;
116     }
117 
118     return HDF_SUCCESS;
119 }
120 
DumpLightChannel(struct HdfSBuf * reply)121 static int32_t DumpLightChannel(struct HdfSBuf *reply)
122 {
123     int32_t ret;
124 
125     ret = ShowLightInfo(reply);
126     if (ret != HDF_SUCCESS) {
127         HDF_LOGE("%{public}s: show light info failed", __func__);
128         return HDF_FAILURE;
129     }
130 
131     ret = ShowLightEffectInfo(reply);
132     if (ret != HDF_SUCCESS) {
133         HDF_LOGE("%{public}s: show light effect info failed", __func__);
134         return HDF_FAILURE;
135     }
136 
137     return HDF_SUCCESS;
138 }
139 
LightDriverDump(struct HdfSBuf * data,struct HdfSBuf * reply)140 static int32_t LightDriverDump(struct HdfSBuf *data, struct HdfSBuf *reply)
141 {
142     uint32_t i;
143     uint32_t argv = 0;
144 
145     if (data == NULL || reply == NULL) {
146         return HDF_FAILURE;
147     }
148 
149     if (!HdfSbufReadUint32(data, &argv)) {
150         HDF_LOGE("%{public}s: read argv failed", __func__);
151         return HDF_FAILURE;
152     }
153 
154     if (argv == 0) {
155         if (!HdfSbufWriteString(reply, g_dumpHelp)) {
156             HDF_LOGE("%{public}s: write -h failed", __func__);
157             return HDF_FAILURE;
158         }
159     }
160 
161     for (i = 0; i < argv; i++) {
162         const char *value = HdfSbufReadString(data);
163         if (value == NULL) {
164             HDF_LOGE("%{public}s value is invalid", __func__);
165             return HDF_FAILURE;
166         }
167 
168         if (strcmp(value, "-h") == HDF_SUCCESS) {
169             if (!HdfSbufWriteString(reply, g_dumpHelp)) {
170                 HDF_LOGE("%{public}s: write -h failed", __func__);
171                 return HDF_FAILURE;
172             }
173             continue;
174         } else if (strcmp(value, "-c") == HDF_SUCCESS) {
175             DumpLightChannel(reply);
176             continue;
177         }
178     }
179 
180     return HDF_SUCCESS;
181 }
182 
GetLightDump(struct HdfSBuf * data,struct HdfSBuf * reply)183 int32_t GetLightDump(struct HdfSBuf *data, struct HdfSBuf *reply)
184 {
185     int32_t ret = LightDriverDump(data, reply);
186     if (ret != HDF_SUCCESS) {
187         HDF_LOGE("%{public}s: get light dump failed", __func__);
188         return HDF_FAILURE;
189     }
190 
191     return HDF_SUCCESS;
192 }
193