-
Notifications
You must be signed in to change notification settings - Fork 1
/
upil_monitor.c
192 lines (146 loc) · 4.34 KB
/
upil_monitor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
** Copyright 2012, albuer@gmail.com
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
创建 socket: /dev/socket/upil.evt,然后等待外部程序的连接
当外部程序连接到该socket之后,外部程序就可以通过该socket得到upil的状态
*/
#include <stdio.h>
#include <errno.h>
#include <cutils/sockets.h>
#include <fcntl.h>
#include <upil_config.h>
#include "upil.h"
static struct upil_event s_listen_event;
static struct upil_event s_monitor_event;
static uew_context* s_event_loop=NULL;
static int s_fdMonitor;
extern void upil_ril_unsolicited(int msg);
static int event_monitor_cb(int fd, short flags, void *param)
{
int ret = 0;
char buf[16];
ENTER_LOG();
if(recv(fd, buf, 1, 0) > 0)
{
upil_warn("Don't write to this socket.");
}
else
{
upil_warn("Event socket disconnected\n");
close(fd);
uew_event_del(s_event_loop, &s_monitor_event, 0);
/* start listening for new connections again */
uew_event_add_wakeup(s_event_loop, &s_listen_event);
}
LEAVE_LOG();
return 0;
}
static int listenCallback (int fd, short flags, void *param) {
int ret;
ENTER_LOG();
s_fdMonitor = accept(fd, NULL, NULL);
if (s_fdMonitor < 0 ) {
upil_error("Error on accept() errno:%d\n", errno);
/* start listening for new connections again */
uew_event_add_wakeup(&g_upil_s.event_loop, &s_listen_event);
LEAVE_LOG();
return -1;
}
ret = fcntl(s_fdMonitor, F_SETFL, O_NONBLOCK);
if (ret < 0) {
upil_error("Error setting O_NONBLOCK errno:%d\n", errno);
}
uew_event_set(&s_monitor_event, s_fdMonitor, 1, event_monitor_cb, NULL);
uew_event_add_wakeup(s_event_loop, &s_monitor_event);
LEAVE_LOG();
return 0;
}
// Send event/msg to monitor sockets
int sendto_monitor(int msg, uchar* data, size_t data_size)
{
char event[512];
switch (msg)
{
case UPIL_STATE_OFFLINE:
sprintf(event, "STATE OFFLINE");
break;
case UPIL_STATE_SYNCING:
sprintf(event, "STATE SYNCING");
break;
case UPIL_STATE_IDLE:
sprintf(event, "STATE IDLE");
break;
case UPIL_STATE_DIALING:
sprintf(event, "STATE DIALING");
break;
case UPIL_STATE_INCOMING:
sprintf(event, "STATE INCOMING");
break;
case UPIL_STATE_CONVERSATION:
sprintf(event, "STATE CONVERSATION");
break;
case UPIL_STATE_MEMO:
sprintf(event, "STATE MEMO");
break;
case UPIL_EVT_RING:
sprintf(event, "RING");
break;
case UPIL_EVT_CALL_ID:
sprintf(event, "CID %s", (char*)data);
break;
case UPIL_EVT_USB_ATTACH:
sprintf(event, "INITIALIZE");
break;
default:
upil_warn("unknown event!");;
return 0;
}
send(s_fdMonitor, event, strlen(event), 0);
upil_ril_unsolicited(msg);
return 0;//
}
/*
* 安装事件监视器,把程序的内部事件、信息等报给外部程序
* 参数:
* event_loop - command socket 将添加到该event loop中
* 返回值:
* 成功返回 0
* 出现异常,则返回 -1
*/
int setup_event_monitor(void* event_loop)
{
int fd;
ENTER_LOG();
if (event_loop==NULL)
{
upil_error("event loop can not be empty!");
LEAVE_LOG();
return -1;
}
unlink(UPIL_SOCKET_EVT_PATH);
// add listen to event loop for event channel connect
fd = socket_local_server (UPIL_SOCKET_EVT, 1, SOCK_STREAM);
if (fd < 0) {
upil_error("Unable to bind socket: %s\n", strerror(errno));
LEAVE_LOG();
return -1;
}
s_event_loop = (uew_context*)event_loop;
uew_event_set (&s_listen_event, fd, 0, listenCallback, NULL);
uew_event_add_wakeup(s_event_loop, &s_listen_event);
LEAVE_LOG();
return 0;
}