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
192
193
194
195
196
197
198
199
|
/*
*
*
* Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact:
* JiHye Kim <jihye1128.kim@samsung.com>
* Hyunjun Son <hj79.son@samsung.com>
* GiWoong Kim <giwoong.kim@samsung.com>
* YeongKyoon Lee <yeongkyoon.lee@samsung.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributors:
* - S-Core Co., Ltd
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#endif
#include "guest_server.h"
#include "skin/maruskin_server.h"
#include "debug_ch.h"
MULTI_DEBUG_CHANNEL( qemu, guest_server );
#define RECV_BUF_SIZE 32
static void* run_guest_server( void* args );
static int svr_port = 0;
static int server_sock = 0;
static int parse_val( char *buff, unsigned char data, char *parsbuf );
pthread_t start_guest_server( int server_port ) {
svr_port = server_port;
pthread_t thread_id;
if ( 0 != pthread_create( &thread_id, NULL, run_guest_server, NULL ) ) {
ERR( "fail to create guest_server pthread.\n" );
}else {
INFO( "created guest server thread\n" );
}
return thread_id;
}
static void* run_guest_server( void* args ) {
INFO( "start guest server thread.\n" );
uint16_t port;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len;
port = svr_port;
if ( ( server_sock = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 ) {
ERR( "create listen socket error\n" );
perror( "create listen socket error\n" );
goto cleanup;
}
memset( &server_addr, '\0', sizeof( server_addr ) );
server_addr.sin_family = PF_INET;
server_addr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
server_addr.sin_port = htons( port );
int opt = 1;
setsockopt( server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) );
if ( 0 > bind( server_sock, (struct sockaddr*) &server_addr, sizeof( server_addr ) ) ) {
ERR( "guest server bind error: " );
perror( "bind" );
goto cleanup;
} else {
INFO( "success to bind port[127.0.0.1:%d/udp] for guest_server in host \n", port );
}
client_len = sizeof( client_addr );
char readbuf[RECV_BUF_SIZE];
INFO( "guest server start...port:%d\n", port );
while ( 1 ) {
memset( &readbuf, 0, RECV_BUF_SIZE );
int read_cnt = recvfrom( server_sock, readbuf, RECV_BUF_SIZE, 0, (struct sockaddr*) &client_addr, &client_len );
if ( 0 > read_cnt ) {
perror( "error : guest_server read" );
break;
} else {
if ( 0 == read_cnt ) {
ERR( "read_cnt is 0.\n" );
break;
}
TRACE( "================= recv =================\n" );
TRACE( "read_cnt:%d\n", read_cnt );
TRACE( "readbuf:%s\n", readbuf );
char command[RECV_BUF_SIZE];
memset( command, '\0', sizeof( command ) );
parse_val( readbuf, 0x0a, command );
TRACE( "----------------------------------------\n" );
if ( strcmp( command, "3\n" ) == 0 ) {
TRACE( "command:%s\n", command );
notify_sensor_daemon_start();
} else {
ERR( "!!! unknown command : %s\n", command );
}
TRACE( "========================================\n" );
}
}
cleanup:
#ifdef __MINGW32__
if( server_sock ) {
closesocket(server_sock);
}
#else
if ( server_sock ) {
close( server_sock );
}
#endif
return NULL;
}
static int parse_val( char* buff, unsigned char data, char* parsbuf ) {
int count = 0;
while ( 1 ) {
if ( count > 12 ) {
return -1;
}
if ( buff[count] == data ) {
count++;
strncpy( parsbuf, buff, count );
return count;
}
count++;
}
return 0;
}
void shutdown_guest_server( void ) {
INFO( "shutdown_guest_server.\n" );
#ifdef __MINGW32__
if( server_sock ) {
closesocket( server_sock );
}
#else
if ( server_sock ) {
close( server_sock );
}
#endif
}
|