Fix for 64 bits compatibility
[platform/core/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 SHIFT_HOLD_KEY_BLOCK            16
39 #define TIMEOUT_RESET_BIT               0x80
40
41 struct pwr_msg {
42         pid_t pid;
43         unsigned int cond;
44         unsigned int timeout;
45 };
46
47 static int send_msg(unsigned int s_bits, unsigned int timeout)
48 {
49         int rc = 0;
50         int sock;
51         struct pwr_msg p;
52         struct sockaddr_un remote;
53
54         p.pid = getpid();
55         p.cond = s_bits;
56         p.timeout = timeout;
57
58         sock = socket(AF_UNIX, SOCK_DGRAM, 0);
59         if (sock == -1) {
60                 ERR("pm socket() failed");
61                 return -1;
62         }
63
64         remote.sun_family = AF_UNIX;
65         if(strlen(SOCK_PATH) >= sizeof(remote.sun_path)) {
66                 ERR("socket path is vey long");
67                 return -1;
68         }
69         strncpy(remote.sun_path, SOCK_PATH, sizeof(remote.sun_path));
70
71         rc = sendto(sock, (void *)&p, sizeof(p), 0, (struct sockaddr *)&remote,
72                     sizeof(struct sockaddr_un));
73         if (rc == -1) {
74                 ERR("pm socket sendto() failed");
75         } else
76                 rc = 0;
77
78         close(sock);
79         return rc;
80 }
81
82 API int pm_change_state(unsigned int s_bits)
83 {
84         /* s_bits is LCD_NORMAL 0x1, LCD_DIM 0x2, LCD_OFF 0x4, SUSPEND 0x8
85          * Stage change to NORMAL       0x100
86          * Stage change to LCDDIM       0x200
87          * Stage change to LCDOFF       0x400
88          * Stage change to SLEEP        0x800
89          * */
90         switch (s_bits) {
91         case LCD_NORMAL:
92         case LCD_DIM:
93         case LCD_OFF:
94         case SUSPEND:
95         case POWER_OFF:
96                 break;
97         default:
98                 return -1;
99         }
100         return send_msg(s_bits << SHIFT_CHANGE_STATE, 0);
101 }
102
103 API int pm_lock_state(unsigned int s_bits, unsigned int flag,
104                       unsigned int timeout)
105 {
106         switch (s_bits) {
107         case LCD_NORMAL:
108         case LCD_DIM:
109         case LCD_OFF:
110                 break;
111         default:
112                 return -1;
113         }
114         if (flag & GOTO_STATE_NOW)
115                 /* if the flag is true, go to the locking state directly */
116                 s_bits = s_bits | (s_bits << SHIFT_CHANGE_STATE);
117         if (flag & HOLD_KEY_BLOCK)
118                 s_bits = s_bits | (1 << SHIFT_HOLD_KEY_BLOCK);
119
120         return send_msg(s_bits, timeout);
121 }
122
123 API int pm_unlock_state(unsigned int s_bits, unsigned int flag)
124 {
125         switch (s_bits) {
126         case LCD_NORMAL:
127         case LCD_DIM:
128         case LCD_OFF:
129                 break;
130         default:
131                 return -1;
132         }
133
134         s_bits = (s_bits << SHIFT_UNLOCK);
135         s_bits = (s_bits | (flag << SHIFT_UNLOCK_PARAMETER));
136         return send_msg(s_bits, 0);
137 }
138