tizen beta release
[framework/system/libslp-pm.git] / pm.c
1 /*
2  *  libslp-pm
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@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
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <linux/limits.h>
30
31 #include "pmapi.h"
32 #include "pm.h"
33
34 #define SOCK_PATH                       "/tmp/pm_sock"
35 #define SHIFT_UNLOCK                    4
36 #define SHIFT_UNLOCK_PARAMETER          12
37 #define SHIFT_CHANGE_STATE              8
38 #define TIMEOUT_RESET_BIT               0x80
39
40 struct pwr_msg {
41         pid_t pid;
42         unsigned int cond;
43         unsigned int timeout;
44 };
45
46 static int send_msg(unsigned int s_bits, unsigned int timeout)
47 {
48         int rc = 0;
49         int sock;
50         struct pwr_msg p;
51         struct sockaddr_un remote;
52
53         p.pid = getpid();
54         p.cond = s_bits;
55         p.timeout = timeout;
56
57         sock = socket(AF_UNIX, SOCK_DGRAM, 0);
58         if (sock == -1) {
59                 ERR("pm socket() failed");
60                 return -1;
61         }
62
63         remote.sun_family = AF_UNIX;
64         if(strlen(SOCK_PATH) >= sizeof(remote.sun_path)) {
65                 ERR("socket path is vey long");
66                 return -1;
67         }
68         strncpy(remote.sun_path, SOCK_PATH, sizeof(remote.sun_path));
69
70         rc = sendto(sock, (void *)&p, sizeof(p), 0, (struct sockaddr *)&remote,
71                     sizeof(struct sockaddr_un));
72         if (rc == -1) {
73                 ERR("pm socket sendto() failed");
74         } else
75                 rc = 0;
76
77         close(sock);
78         return rc;
79 }
80
81 API int pm_change_state(unsigned int s_bits)
82 {
83         /* s_bits is LCD_NORMAL 0x1, LCD_DIM 0x2, LCD_OFF 0x4, SUSPEND 0x8
84          * Stage change to NORMAL       0x100
85          * Stage change to LCDDIM       0x200
86          * Stage change to LCDOFF       0x400
87          * Stage change to SLEEP        0x800
88          * */
89         switch (s_bits) {
90         case LCD_NORMAL:
91         case LCD_DIM:
92         case LCD_OFF:
93         case SUSPEND:
94         case POWER_OFF:
95                 break;
96         default:
97                 return -1;
98         }
99         return send_msg(s_bits << SHIFT_CHANGE_STATE, 0);
100 }
101
102 API int pm_lock_state(unsigned int s_bits, unsigned int flag,
103                       unsigned int timeout)
104 {
105         switch (s_bits) {
106         case LCD_NORMAL:
107         case LCD_DIM:
108         case LCD_OFF:
109                 break;
110         default:
111                 return -1;
112         }
113         if (flag & GOTO_STATE_NOW)
114                 /* if the flag is true, go to the locking state directly */
115                 s_bits = s_bits | (s_bits << SHIFT_CHANGE_STATE);
116         return send_msg(s_bits, timeout);
117 }
118
119 API int pm_unlock_state(unsigned int s_bits, unsigned int flag)
120 {
121         switch (s_bits) {
122         case LCD_NORMAL:
123         case LCD_DIM:
124         case LCD_OFF:
125                 break;
126         default:
127                 return -1;
128         }
129
130         s_bits = (s_bits << SHIFT_UNLOCK);
131         s_bits = (s_bits | (flag << SHIFT_UNLOCK_PARAMETER));
132         return send_msg(s_bits, 0);
133 }
134