update for beta universally
[framework/telephony/libslp-tapi.git] / src / test_apps / tapi_power_test.c
1 /*
2  * libslp-tapi
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Kyeongchul Kim <kyeongchul.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdlib.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <glib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "TapiCommon.h"
32 #include "ITapiPower.h"
33 #include "TelDisplay.h"
34 #include "TapiEvent.h"
35
36 #define TEST_DEBUG(frmt, args...)       \
37 {do { fprintf(stderr, "[Pow Test][%s:%04d] "frmt "\n", __func__, __LINE__, ##args); } while (FALSE) ;}
38
39
40 #ifndef false
41 #define false 0
42 #endif
43 #ifndef true
44 #define true  !0
45 #endif
46 #ifndef bool
47 #define bool  char
48 #endif
49
50 #define NUM_OF_POWER_EVENT                      4
51
52 static unsigned int power_subscription_id[NUM_OF_POWER_EVENT] = {0, };
53
54 static unsigned int  display_subscription_id = 0;
55
56 void __PrintBatteryStatus(void *pData)
57 {
58         tapi_power_battery_status_level_t*  pBattLevel;
59
60         pBattLevel = (tapi_power_battery_status_level_t*)pData;
61         switch(*pBattLevel){
62                 case TAPI_POWER_BATT_STAT_POWER_OFF_LEVEL:
63                         TEST_DEBUG("Battery Level  [Power Off]");
64                         break;
65                 case TAPI_POWER_BATT_STAT_CRIT_LOW_LEVEL:
66                         TEST_DEBUG("Battery Level [Critical-Low]");
67                         break;
68                 case TAPI_POWER_BATT_STAT_LOW_LEVEL:
69                         TEST_DEBUG("Battery Level [Low Battery]");
70                         break;
71                 case TAPI_POWER_BATT_STAT_NORMAL_LEVEL:
72                         TEST_DEBUG("Battery Level [Normal Level]");
73                         break;
74                 default:
75                         TEST_DEBUG("Unknown Battery Level");
76                         break;
77         }
78
79         return;
80 }
81
82 void __PrintFlightModeStatus(void *pData)
83 {
84         tapi_power_flight_mode_resp_type_t*  pFlightStatus;
85
86         pFlightStatus = (tapi_power_flight_mode_resp_type_t *)pData;
87
88         switch(*pFlightStatus){
89                 case TAPI_POWER_FLIGHT_MODE_RESP_ON:
90                         TEST_DEBUG("Flight Mode  [On]");
91                         break;
92                 case TAPI_POWER_FLIGHT_MODE_RESP_OFF:
93                         TEST_DEBUG("Flight Mode  [Off]");
94                         break;
95                 case TAPI_POWER_FLIGHT_MODE_RESP_FAIL:
96                         TEST_DEBUG("Flight Mode  [Mode Change Fail]");
97                         break;
98                 default:
99                         TEST_DEBUG("Unknown Flight Mode");
100                         break;
101         }
102         return;
103 }
104
105 void __PrintDisplayIconInfo(void *pData)
106 {
107         tapi_display_icon_info_t*  pDisplayIcon = NULL;
108
109         pDisplayIcon = (tapi_display_icon_info_t*)pData;
110
111         switch(pDisplayIcon->rssi){
112                 case TAPI_DISPLAY_RSSI_0:
113                         TEST_DEBUG("RSSI [0]");
114                         break;
115                 case TAPI_DISPLAY_RSSI_1:
116                         TEST_DEBUG("RSSI [1]");
117                         break;
118                 case TAPI_DISPLAY_RSSI_2:
119                         TEST_DEBUG("RSSI [2]");
120                         break;
121                 case TAPI_DISPLAY_RSSI_3:
122                         TEST_DEBUG("RSSI [3]");
123                         break;
124                 case TAPI_DISPLAY_RSSI_4:
125                         TEST_DEBUG("RSSI [4]");
126                         break;
127                 case TAPI_DISPLAY_RSSI_5:
128                         TEST_DEBUG("RSSI [5]");
129                         break;
130                 case TAPI_DISPLAY_RSSI_6:
131                         TEST_DEBUG("RSSI [6]");
132                         break;
133                 default:
134                         TEST_DEBUG("Unknow RSSI Value");
135                         break;
136
137         }
138
139         switch(pDisplayIcon->batt){
140                 case TAPI_POWER_PHONE_BATT_LEVEL0:
141                         TEST_DEBUG("Battery Level [0]");;
142                         break;
143                 case TAPI_POWER_PHONE_BATT_LEVEL1:
144                         TEST_DEBUG("Battery Level [1]");
145                         break;
146                 case TAPI_POWER_PHONE_BATT_LEVEL2:
147                         TEST_DEBUG("Battery Level [2]");
148                         break;
149                 case TAPI_POWER_PHONE_BATT_LEVEL3:
150                         TEST_DEBUG("Battery Level [3]");
151                         break;
152                 case TAPI_POWER_PHONE_BATT_LEVEL4:
153                         TEST_DEBUG("Battery Level [4]");
154                         break;
155                 case TAPI_POWER_PHONE_BATT_LEVEL5:
156                         TEST_DEBUG("Battery Level [5]");
157                         break;
158                 case TAPI_POWER_PHONE_BATT_LEVEL_INIT:
159                         TEST_DEBUG("Battery Level [Init]");
160                         break;
161                 default:
162                         TEST_DEBUG("Unknow Battery Level Value");
163                         break;
164
165         }
166
167 }
168
169 void  power_async_event_callback(TelTapiEvent_t* event,void *data )
170 {
171
172         TEST_DEBUG("$$$$$$ POWER EVENT NOTIFICATION $$$$$$");
173         TEST_DEBUG("Request ID : [0x%x]", event->RequestId);
174
175         switch(event->EventType)
176         {
177                 case TAPI_EVENT_POWER_PHONE_OFF:
178                         TEST_DEBUG("Event Type: [TAPI_EVENT_POWER_PHONE_OFF]");
179                         break;
180                 case TAPI_EVENT_POWER_BATT_STAT_IND:
181                         TEST_DEBUG("Event Type: [TAPI_EVENT_POWER_BATT_STAT_IND]");
182                         __PrintBatteryStatus(event->pData);
183                         break;
184                 case TAPI_EVENT_POWER_FLIGHT_MODE_RESP:
185                         TEST_DEBUG("Event Type: [TAPI_EVENT_POWER_FLIGHT_MODE_RESP]");
186                         __PrintFlightModeStatus(event->pData);
187                         break;
188                 case TAPI_EVENT_DISPLAY_ICON_INFO_IND:
189                         TEST_DEBUG("Event Type: [TAPI_EVENT_DISPLAY_ICON_INFO_IND]");
190                         __PrintDisplayIconInfo(event->pData);
191                         break;
192                 default:
193                         TEST_DEBUG("Event Type: unknown event:[%d]", event->EventType);
194                         break;
195         }
196         TEST_DEBUG("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
197
198 }
199
200
201 int power_read_key_input(void)
202 {
203         int ret;
204         char buf[512] = {0, };
205         char msg[512];
206         TapiResult_t ret_val = TAPI_API_SUCCESS;
207
208         memset(buf,0, sizeof(buf));
209         memset(msg,0,sizeof(msg));
210
211         printf("******* Power & Display Test MENU ******************\n");
212         printf("*************** Asynchronous API *****************\n");
213         printf("Power Off[PwrOff]\n");
214         printf("Power On[PwrOn]\n");
215         printf("Reset [Reset]\n");
216         printf("Flight Mode On[FlightModeOn]  \n");
217         printf("Flight Mode Off[FlightModeOff] \n");
218         printf("Reboot Modem[RebootModem] \n");
219         printf("Ramdump [Ramdump] \n");
220         printf("**************** Synchronous API *****************\n");
221         printf("Get Power status[GetPwrStatus] \n");
222         printf("Quit Power Test[quit] \n");
223         printf("************************************************\n");
224
225         ret = read(0, buf, sizeof(buf));
226         if (ret < 0)
227         {
228                 if (errno == EINTR)
229                         perror("read(1)");
230                 return -1;
231         }
232         else if (ret == 0)
233                 return ret;
234
235         if(strncmp(buf, "PwrOff", sizeof("PwrOff")-1) == 0)
236         {
237                 ret_val = tel_process_power_command(TAPI_PHONE_POWER_OFF);
238                 TEST_DEBUG("tel_process_power_command return value is %d \n",ret_val);
239         }
240
241         else if(strncmp(buf, "PwrOn", sizeof("PwrOn")-1) == 0)
242         {
243                 ret_val = tel_process_power_command(TAPI_PHONE_POWER_ON);
244                 TEST_DEBUG("tel_process_power_command return value is %d \n",ret_val);
245         }
246
247         else if(strncmp(buf, "Reset", sizeof("Reset")-1) == 0)
248         {
249                 ret_val = tel_process_power_command(TAPI_PHONE_POWER_RESET);
250                 TEST_DEBUG("tel_process_power_command return value is %d \n",ret_val);
251         }
252
253         else if(strncmp(buf, "FlightModeOn", sizeof("FlightModeOn")-1) == 0)
254         {
255                 ret_val = tel_set_flight_mode(TAPI_POWER_FLIGHT_MODE_ENTER);
256                 TEST_DEBUG("tel_set_flight_mode return value is %d \n",ret_val);
257         }
258
259         else if(strncmp(buf, "FlightModeOff", sizeof("FlightModeOff")-1) == 0)
260         {
261                 ret_val = tel_set_flight_mode(TAPI_POWER_FLIGHT_MODE_LEAVE);
262                 TEST_DEBUG("tel_set_flight_mode return value is %d \n",ret_val);
263         }
264
265         else if(strncmp(buf, "RebootModem", sizeof("RebootModem")-1) == 0)
266         {
267                 ret_val = tel_reset_modem();
268                 TEST_DEBUG("tel_reset_modem return value is %d \n",ret_val);
269         }
270
271         else if(strncmp(buf, "Ramdump", sizeof("Ramdump")-1) == 0)
272         {
273                 ret_val = tel_enforce_ramdump_of_modem();
274                 TEST_DEBUG("tel_enforce_ramdump_of_modem return value is %d",ret_val);
275         }
276         else if(strncmp(buf, "quit", sizeof("quit")-1) == 0)
277         {
278                 return -1;
279         }
280
281         memset(buf, 0, 512);
282
283         return 0;
284 }
285
286
287 void power_select_loop(void)
288 {
289         int ret;
290         fd_set readfds;
291
292         while (1)
293         {
294                 FD_ZERO(&readfds);
295                 FD_SET(0, &readfds);
296
297                 ret = select(0 + 1, &readfds, NULL, NULL, NULL);
298                 if (ret)
299                 {
300                         if (FD_ISSET(0, &readfds))
301                         {
302                                 if(power_read_key_input() < 0)
303                                         break;
304                         }
305                 }
306         }
307         return;
308 }
309
310 int power_test_unsubscribe_tapi_event(void)
311 {
312         int i = 0;
313         TapiResult_t    api_err = TAPI_API_SUCCESS;
314         int ret_val = TRUE;
315
316         for(i=0;i<NUM_OF_POWER_EVENT; i++){
317                 api_err = tel_deregister_event(power_subscription_id[i]);
318                 if(api_err != TAPI_API_SUCCESS){
319                         TEST_DEBUG("tel_deregister_event isn't unsubscribed. sub id is %d api_err is %d\n",power_subscription_id[i],api_err);
320                         ret_val = FALSE;
321                         break;
322                 }
323
324         }
325
326         tel_deinit();
327
328         return ret_val;
329 }
330
331 int power_test_subscribe_tapi_events (void)
332 {
333         TEST_DEBUG("1");
334
335         int i = 0;
336         int ret_val = TRUE;
337         TapiResult_t    api_err = TAPI_API_SUCCESS;
338
339         int PowerEvtList[NUM_OF_POWER_EVENT] = {
340                 TAPI_EVENT_POWER_PHONE_OFF,
341                 TAPI_EVENT_POWER_BATT_STAT_IND,
342                 TAPI_EVENT_POWER_FLIGHT_MODE_RESP,
343                 TAPI_EVENT_POWER_SERVICE_READY_IND,
344         };
345
346         //TEST_DEBUG("########### Power TEST #############");
347         if(tel_init() == TAPI_API_SUCCESS)
348         {
349                 TEST_DEBUG("2");
350                 for(i=0;i<NUM_OF_POWER_EVENT; i++){
351                         TEST_DEBUG("3");
352                         api_err = tel_register_event(PowerEvtList[i], &power_subscription_id[i],(TelAppCallback)&power_async_event_callback,NULL);
353                         TEST_DEBUG("4");
354
355                         if(api_err != TAPI_API_SUCCESS){
356                                 TEST_DEBUG("tel_register_event isn't subscribed. sub id is %d api_err is %d\n",power_subscription_id[i],api_err);
357                                 ret_val = FALSE;
358                                 break;
359                         }
360                 }
361
362                 TEST_DEBUG("5");
363
364                 api_err = tel_register_event(TAPI_EVENT_DISPLAY_ICON_INFO_IND, &display_subscription_id,(TelAppCallback)&power_async_event_callback,NULL);
365                 TEST_DEBUG("6");
366                 if(api_err != TAPI_API_SUCCESS){
367                         TEST_DEBUG("tel_register_event isn't subscribed. sub id is %d api_err is %d\n",display_subscription_id,api_err);
368                         ret_val = FALSE;
369                 }
370         }
371         else {
372
373                 TEST_DEBUG("tel_init() failed");
374         }
375         return ret_val;
376 }
377
378 int power_test_main(int argc, char *argv[])
379 {
380         return 0;
381 }