1/*
2 * Copyright (c) 2021-2022 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 <securec.h>
17#include "battery_device.h"
18
19static BatInfo battInfo = {
20    80,
21    10,
22    20,
23    60,
24    CHARGE_STATE_ENABLE,
25    PLUGGED_TYPE_WIRELESS,
26    "Ternary_Lithium",
27    HEALTH_STATE_GOOD,
28};
29
30static BatteeryDeviceFeatureApi *g_batteryFeatureHandle = NULL;
31static IBattery g_device;
32static IBattery *g_ibattery = NULL;
33
34static const char *BATTERY_GetName(Service *service)
35{
36    (void)service;
37    return BATTERY_DEVICE;
38}
39
40static BOOL Initialize(Service *service, Identity identity)
41{
42    (void)service;
43    (void)identity;
44    return TRUE;
45}
46
47static BOOL MessageHandle(Service *service, Request *msg)
48{
49    (void)service;
50    (void)msg;
51    return TRUE;
52}
53
54static TaskConfig GetTaskConfig(Service *service)
55{
56    (void)service;
57    TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, TASK_CONFIG_STACK_SIZE, TASK_CONFIG_QUEUE_SIZE, SHARED_TASK};
58    return config;
59}
60
61static int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
62{
63    (void)iProxy;
64    (void)funcId;
65    (void)origin;
66    (void)req;
67    (void)reply;
68    return BATTERY_OK;
69}
70
71int32_t GetSocImpl(void)
72{
73    return battInfo.batSoc;
74}
75BatteryChargeState GetChargingStatusImpl(void)
76{
77    return battInfo.chargingStatus;
78}
79BatteryHealthState GetHealthStatusImpl(void)
80{
81    return battInfo.healthStatus;
82}
83BatteryPluggedType GetPluggedTypeImpl(void)
84{
85    return battInfo.pluggedType;
86}
87int32_t GetVoltageImpl(void)
88{
89    return battInfo.batVoltage;
90}
91char *GetTechnologyImpl(void)
92{
93    return battInfo.BatTechnology;
94}
95int32_t GetTemperatureImpl(void)
96{
97    return battInfo.BatTemp;
98}
99int TurnOnLedImpl(int red, int green, int blue)
100{
101    (void)red;
102    (void)green;
103    (void)blue;
104    return BATTERY_OK;
105}
106int TurnOffLedImpl(void)
107{
108    return BATTERY_OK;
109}
110int SetLedColorImpl(int red, int green, int blue)
111{
112    (void)red;
113    (void)green;
114    (void)blue;
115    return BATTERY_OK;
116}
117int GetLedColorImpl(int *red, int *green, int *blue)
118{
119    (void)red;
120    (void)green;
121    (void)blue;
122    return BATTERY_OK;
123}
124void ShutDownImpl(void)
125{
126}
127void UpdateBatInfoImpl(BatInfo *battery)
128{
129    if (battery == NULL) {
130        return;
131    }
132    if (strcpy_s(battery->BatTechnology, BATTECHNOLOGY_LEN, battInfo.BatTechnology) != EOK) {
133        return;
134    }
135    battery->batSoc = battInfo.batSoc;
136    battery->batVoltage = battInfo.batVoltage;
137    battery->BatTemp = battInfo.BatTemp;
138    battery->batCapacity = battInfo.batCapacity;
139    battery->chargingStatus = battInfo.chargingStatus;
140    battery->pluggedType = battInfo.pluggedType;
141    battery->healthStatus = battInfo.healthStatus;
142}
143
144static BatteryDevice g_batteryDevice = {
145    .GetName = BATTERY_GetName,
146    .Initialize = Initialize,
147    .MessageHandle = MessageHandle,
148    .GetTaskConfig = GetTaskConfig,
149    SERVER_IPROXY_IMPL_BEGIN,
150    .Invoke = Invoke,
151    .GetSoc = GetSocImpl,
152    .GetChargingStatus = GetChargingStatusImpl,
153    .GetHealthStatus = GetHealthStatusImpl,
154    .GetPluggedType =  GetPluggedTypeImpl,
155    .GetVoltage = GetVoltageImpl,
156    .GetTechnology = GetTechnologyImpl,
157    .GetTemperature = GetTemperatureImpl,
158    .TurnOnLed = TurnOnLedImpl,
159    .TurnOffLed = TurnOffLedImpl,
160    .SetLedColor = SetLedColorImpl,
161    .GetLedColor = GetLedColorImpl,
162    .ShutDown = ShutDownImpl,
163    .UpdateBatInfo = UpdateBatInfoImpl,
164    IPROXY_END,
165};
166
167static void ChargingApiGet(void)
168{
169    if (g_batteryFeatureHandle != NULL) {
170        return;
171    }
172    IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(BATTERY_DEVICE);
173    if (iUnknown == NULL) {
174        return;
175    }
176    int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)(&g_batteryFeatureHandle));
177    if (ret != BATTERY_OK) {
178        return;
179    }
180}
181
182static void Init(void)
183{
184    BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_batteryDevice);
185    if (result == FALSE) {
186        return;
187    }
188    BOOL apiResult = SAMGR_GetInstance()->RegisterDefaultFeatureApi(BATTERY_DEVICE, GET_IUNKNOWN(g_batteryDevice));
189    if (apiResult == FALSE) {
190        return;
191    }
192}
193SYSEX_SERVICE_INIT(Init);
194
195
196static IBattery *GetBatteryDeviceMethods(void)
197{
198    if (g_batteryFeatureHandle == NULL) {
199        return NULL;
200    }
201    g_device.GetSoc = g_batteryFeatureHandle->GetSoc;
202    g_device.GetChargingStatus = g_batteryFeatureHandle->GetChargingStatus;
203    g_device.GetHealthStatus = g_batteryFeatureHandle->GetHealthStatus;
204    g_device.GetPluggedType = g_batteryFeatureHandle->GetPluggedType;
205    g_device.GetVoltage = g_batteryFeatureHandle->GetVoltage;
206    g_device.GetTechnology = g_batteryFeatureHandle->GetTechnology;
207    g_device.GetTemperature = g_batteryFeatureHandle->GetTemperature;
208    g_device.TurnOnLed = g_batteryFeatureHandle->TurnOnLed;
209    g_device.TurnOffLed = g_batteryFeatureHandle->TurnOffLed;
210    g_device.SetLedColor = g_batteryFeatureHandle->SetLedColor;
211    g_device.GetLedColor = g_batteryFeatureHandle->GetLedColor;
212    g_device.ShutDown = g_batteryFeatureHandle->ShutDown;
213    g_device.UpdateBatInfo = g_batteryFeatureHandle->UpdateBatInfo;
214    return &g_device;
215}
216
217IBattery *NewBatterInterfaceInstance(void)
218{
219    if (g_batteryFeatureHandle == NULL) {
220        ChargingApiGet();
221    }
222
223    g_ibattery = GetBatteryDeviceMethods();
224    if (g_ibattery == NULL) {
225        return NULL;
226    }
227    return g_ibattery;
228}
229
230int32_t FreeBatterInterfaceInstance(void)
231{
232    return 0;
233}
234