• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <ohos_init.h>
17 #include <samgr_lite.h>
18 #include "ibattery.h"
19 #include "battery_manage_feature.h"
20 #include "battery_manage_service.h"
21 #include "battery_mgr.h"
22 #include "power_mgr_time_util.h"
23 #include "power_mgr_timer_util.h"
24 
25 #define BATTERY_STACK_SIZE      0x800
26 #define BATTERY_QUEUE_SIZE      20
27 
28 typedef enum {
29     /* add new msg */
30     BATT_SRV_MSG_UPDATE,
31     BATT_SRV_MSG_ID_MAX,
32 } BatteryServiceMsgID;
33 
34 static int64_t DEFAULT_INTERVAL_MSECS = 5000;
35 static PowerTimer *time_ = NULL;
36 static BatInfo battpoint;
37 
38 static void BatteryFeatureTimeerInit(void);
39 static BatteryService *BatteryGetInstance(void);
40 
Initialize(Service * service,Identity identity)41 static BOOL Initialize(Service *service, Identity identity)
42 {
43     BatteryService *batterService = BatteryGetInstance();
44     batterService->identity = identity;
45     BatteryFeatureTimeerInit();
46     return TRUE;
47 }
48 
BatteryGetName(Service * service)49 static const char *BatteryGetName(Service *service)
50 {
51     return BATTERY_SERVICE;
52 }
53 
UpdateBatteryMsg(BatInfo * battery)54 void UpdateBatteryMsg(BatInfo* battery)
55 {
56     g_batteryDevice = NewBatterInterfaceInstance();
57     if (g_batteryDevice == NULL) {
58         return;
59     }
60     g_batteryDevice->UpdateBatInfo(battery);
61     return;
62 }
63 
MessageHandle(Service * service,Request * request)64 static BOOL MessageHandle(Service *service, Request *request)
65 {
66     (void)service;
67     if ((request == NULL) ||
68         ((BatteryServiceMsgID)(request->msgId) >= BATT_SRV_MSG_ID_MAX)) {
69         return false;
70     }
71     switch (request->msgId) {
72         case BATT_SRV_MSG_UPDATE:
73             UpdateBatteryMsg(&battpoint);
74             break;
75         default:
76             break;
77     }
78     return true;
79 }
GetTaskConfig(Service * service)80 static TaskConfig GetTaskConfig(Service *service)
81 {
82     TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, BATTERY_STACK_SIZE, BATTERY_QUEUE_SIZE, SHARED_TASK};
83     return config;
84 }
85 
86 static BatteryService g_batteryService = {
87     .GetName = BatteryGetName,
88     .Initialize = Initialize,
89     .MessageHandle = MessageHandle,
90     .GetTaskConfig = GetTaskConfig,
91     { -1, -1, NULL },
92 };
93 
BatteryGetInstance(void)94 static BatteryService *BatteryGetInstance(void)
95 {
96     return &g_batteryService;
97 }
98 
BatteryMonitorHandle(void * arg)99 void BatteryMonitorHandle(void *arg)
100 {
101     Request request = {
102         .msgId = BATT_SRV_MSG_UPDATE,
103         .data = NULL,
104         .len = 0,
105         .msgValue = 0
106     };
107     SAMGR_SendRequest(&(g_batteryService.identity), &request, NULL);
108 }
109 
BatteryFeatureTimeerInit(void)110 static void BatteryFeatureTimeerInit(void)
111 {
112     if (time_ == NULL) {
113         time_  =  PowerMgrCreateTimer(DEFAULT_INTERVAL_MSECS, DEFAULT_INTERVAL_MSECS, BatteryMonitorHandle);
114         if (time_ == NULL) {
115             return;
116         }
117         if (PowerMgrStartTimer(time_, NULL) == FALSE) {
118             return;
119         }
120     }
121 }
122 
Init(void)123 static void Init(void)
124 {
125     BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_batteryService);
126     if (result == FALSE) {
127         return;
128     }
129 }
130 SYSEX_SERVICE_INIT(Init);