~ubuntu-branches/ubuntu/edgy/pilot-link/edgy

« back to all changes in this revision

Viewing changes to src/pilot-debugsh.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Rousseau
  • Date: 2006-09-22 11:51:36 UTC
  • mfrom: (3.1.8 edgy)
  • Revision ID: james.westby@ubuntu.com-20060922115136-qqmy17bx8j5x0y72
Tags: 0.12.1-5
* urgency medium since libpisock-dev was not usable to build any package
* libpisock-dev now depends on libusb-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * pilot-debugsh.c:  Simple debugging console
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the
 
6
 * Free Software Foundation; either version 2 of the License, or (at your
 
7
 * option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 
12
 * Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
16
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 *
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include <config.h>
 
22
#endif
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <unistd.h>
 
27
#include <signal.h>
 
28
#include <sys/time.h>
 
29
#include <sys/types.h>
 
30
#include <string.h>
 
31
#include <netinet/in.h>
 
32
 
 
33
#include "pi-source.h"
 
34
#include "pi-dlp.h"
 
35
#include "pi-syspkt.h"
 
36
#include "pi-header.h"
 
37
#include "pi-debug.h"
 
38
 
 
39
#ifdef HAVE_SYS_SELECT_H
 
40
#include <sys/select.h>
 
41
#endif
 
42
 
 
43
/* Declare prototypes */
 
44
void read_user(int sd);
 
45
void read_pilot(int sd);
 
46
void sig(int signal);
 
47
 
 
48
int     done = 0;
 
49
 
 
50
void read_user(int sd)
 
51
{
 
52
        char    line[256];
 
53
        int     l = read(fileno(stdin), line, 256);
 
54
        
 
55
        if (l > 0)
 
56
                line[l - 1] = 0;
 
57
 
 
58
        if (strcmp(line, "apps") == 0) {
 
59
                sys_RemoteEvent(sd, 1, 5, 170, 0, 0, 0, 0);     /* Set the pen down */
 
60
                sys_RemoteEvent(sd, 0, 5, 170, 0, 0, 0, 0);     /* And then lift it up */
 
61
        } else if (strcmp(line, "menu") == 0) {
 
62
                sys_RemoteEvent(sd, 1, 5, 200, 0, 0, 0, 0);     /* Set the pen down */
 
63
                sys_RemoteEvent(sd, 0, 5, 200, 0, 0, 0, 0);     /* And then lift it up */
 
64
        } else if (strcmp(line, "reboot") == 0) {
 
65
                RPC(sd, 1, 0xA08C, 2, RPC_End);
 
66
        } else if (strcmp(line, "coldboot") == 0) {
 
67
                RPC(sd, 1, 0xA08B, 2, RPC_Long(0), RPC_Long(0),
 
68
                    RPC_Long(0), RPC_Long(0), RPC_End);
 
69
        } else if (strcmp(line, "numdb") == 0) {
 
70
                printf("Number of databases on card 0: %d\n",
 
71
                       RPC(sd, 1, 0xA043, 0, RPC_Short(0), RPC_End)
 
72
                    );
 
73
        } else if (strcmp(line, "dbinfo") == 0) {
 
74
                long creator, type, appInfo, sortInfo, modnum, backdate,
 
75
                    moddate, crdate, version, attr;
 
76
                char name[32];
 
77
 
 
78
                int id_ =
 
79
                    RPC(sd, 1, 0xA044, 0, RPC_Short(0), RPC_Short(0),
 
80
                        RPC_End);
 
81
 
 
82
                RPC(sd, 1, 0xA046, 0, RPC_Short(0), RPC_Long(id_),
 
83
                    RPC_Ptr(name, 32),
 
84
                    RPC_ShortRef(attr), RPC_ShortRef(version),
 
85
                    RPC_LongRef(crdate), RPC_LongRef(moddate),
 
86
                    RPC_LongRef(backdate), RPC_LongRef(modnum),
 
87
                    RPC_LongRef(appInfo), RPC_LongRef(sortInfo),
 
88
                    RPC_LongRef(type), RPC_LongRef(creator), RPC_End);
 
89
 
 
90
                printf("The name of db 0 (LocalID %x) is %s\n", id_, name);
 
91
 
 
92
        } else if (strcmp(line, "quit") == 0) {
 
93
                done = 1;
 
94
        } else if (l > 1) {
 
95
                printf
 
96
                    ("unknown command '%s' (try 'apps', 'menu', 'coldboot', 'reboot', 'dbinfo', or 'quit')\n",
 
97
                     line);
 
98
        }
 
99
 
 
100
        if (!done) {
 
101
                printf("debugsh>");
 
102
                fflush(stdout);
 
103
        }
 
104
        if (l == 0)
 
105
                done = 1;
 
106
}
 
107
 
 
108
void read_pilot(int sd)
 
109
{
 
110
        pi_buffer_t *buf = pi_buffer_new (4096);
 
111
        int     l = pi_read(sd, buf, 4096);
 
112
        
 
113
        printf("From Palm %d:", l);
 
114
        if (l < 0)
 
115
                exit(EXIT_FAILURE);
 
116
        
 
117
        pi_dumpdata(buf->data, l);
 
118
 
 
119
        if (buf->data[2] == 0) {                        /* SysPkt command       */
 
120
                if (buf->data[0] == 1) {                /* Console              */
 
121
                        if (buf->data[4] == 0x7f) {     /* Message from Palm    */
 
122
                                int i;
 
123
 
 
124
                                for (i = 6; i < l; i++)
 
125
                                        if (buf->data[i] == '\r')
 
126
                                                buf->data[i] = '\n';
 
127
                                printf("%s", buf->data + 6);
 
128
                        }
 
129
                }
 
130
        }
 
131
 
 
132
        pi_buffer_free (buf);
 
133
 
 
134
        if (!done) {
 
135
                printf("debugsh>");
 
136
                fflush(stdout);
 
137
        }
 
138
 
 
139
}
 
140
 
 
141
void sig(int signal)
 
142
{
 
143
        done = 1;
 
144
}
 
145
 
 
146
int main(int argc, char *argv[])
 
147
{
 
148
        int     max,
 
149
                sd = -1;
 
150
        
 
151
        fd_set r, rin;
 
152
 
 
153
        if (argc < 2) {
 
154
                fprintf(stderr, "   Usage: %s %s\n\n", argv[0], TTYPrompt);
 
155
                exit(2);
 
156
        }
 
157
 
 
158
        sd = pi_socket(PI_AF_PILOT, PI_SOCK_STREAM, PI_PF_SYS);
 
159
        if (sd < 0) {
 
160
                fprintf(stderr, "Unable to create socket\n");
 
161
                return -1;
 
162
        }
 
163
 
 
164
        if (pi_connect(sd, argv[1]) < 0) {
 
165
                fprintf(stderr, "Unable to connect\n");
 
166
                return -1;
 
167
        }
 
168
        
 
169
        /* Now we can read and write packets: to get the Palm to send a
 
170
           packet, write a ".2" shortcut, which starts the debugging mode.
 
171
           (Make sure to reset your Palm after finishing this example!) */
 
172
 
 
173
        FD_ZERO(&r);
 
174
        FD_SET(sd, &r);
 
175
        FD_SET(fileno(stdin), &r);
 
176
 
 
177
        max = sd;
 
178
        if (fileno(stdin) > max)
 
179
                max = fileno(stdin);
 
180
 
 
181
        printf("debugsh>");
 
182
        fflush(stdout);
 
183
 
 
184
        signal(SIGINT, sig);
 
185
 
 
186
        while (!done) {
 
187
                rin = r;
 
188
                if (select(max + 1, &rin, 0, 0, 0) >= 0) {
 
189
                        if (FD_ISSET(fileno(stdin), &rin)) {
 
190
                                read_user(sd);
 
191
                        } else if (FD_ISSET(sd, &rin)) {
 
192
                                read_pilot(sd);
 
193
                        }
 
194
                } else {
 
195
                        break;
 
196
                }
 
197
        }
 
198
 
 
199
        printf("\n   Exiting...\n");
 
200
        pi_close(sd);
 
201
 
 
202
        return 0;
 
203
}
 
204
 
 
205
/* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
 
206
/* Local Variables: */
 
207
/* indent-tabs-mode: t */
 
208
/* c-basic-offset: 8 */
 
209
/* End: */