Add --usage/--help commandline options
[sdk/target/sdbd.git] / test / test_track_jdwp.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* a simple test program, connects to SDB server, and opens a track-devices session */
18 #include <netdb.h>
19 #include <sys/socket.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <memory.h>
24
25 static void
26 panic( const char*  msg )
27 {
28     fprintf(stderr, "PANIC: %s: %s\n", msg, strerror(errno));
29     exit(1);
30 }
31
32 static int
33 unix_write( int  fd, const char*  buf, int  len )
34 {
35     int  result = 0;
36     while (len > 0) {
37         int  len2 = write(fd, buf, len);
38         if (len2 < 0) {
39             if (errno == EINTR || errno == EAGAIN)
40                 continue;
41             return -1;
42         }
43         result += len2;
44         len -= len2;
45         buf += len2;
46     }
47     return  result;
48 }
49
50 static int
51 unix_read( int  fd, char*  buf, int  len )
52 {
53     int  result = 0;
54     while (len > 0) {
55         int  len2 = read(fd, buf, len);
56         if (len2 < 0) {
57             if (errno == EINTR || errno == EAGAIN)
58                 continue;
59             return -1;
60         }
61         result += len2;
62         len -= len2;
63         buf += len2;
64     }
65     return  result;
66 }
67
68
69 int  main( void )
70 {
71     int                  ret, s;
72     struct sockaddr_in   server;
73     char                 buffer[1024];
74     const char*          request = "track-jdwp";
75     int                  len;
76
77     memset( &server, 0, sizeof(server) );
78     server.sin_family      = AF_INET;
79     server.sin_port        = htons(5037);
80     server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
81
82     s = socket( PF_INET, SOCK_STREAM, 0 );
83     ret = connect( s, (struct sockaddr*) &server, sizeof(server) );
84     if (ret < 0) panic( "could not connect to server" );
85
86     /* send the request */
87     len = snprintf( buffer, sizeof buffer, "%04x%s", strlen(request), request );
88     if (unix_write(s, buffer, len) < 0)
89         panic( "could not send request" );
90
91     /* read the OKAY answer */
92     if (unix_read(s, buffer, 4) != 4)
93         panic( "could not read request" );
94
95     printf( "server answer: %.*s\n", 4, buffer );
96
97     /* now loop */
98     for (;;) {
99         char  head[5] = "0000";
100
101         if (unix_read(s, head, 4) < 0)
102             panic("could not read length");
103
104         if ( sscanf( head, "%04x", &len ) != 1 )
105             panic("could not decode length");
106
107         if (unix_read(s, buffer, len) != len)
108             panic("could not read data");
109
110         printf( "received header %.*s (%d bytes):\n%.*s", 4, head, len, len, buffer );
111     }
112     close(s);
113 }