wmshutdown: Add support for suspend and hibernate.
[dockapps.git] / wmcapshare / obex_put_common.c
blobd050e2fade32cdd10d3dc41774937efb2bb86531
1 /*********************************************************************
3 * Filename: obex_common.c
4 * Version: 0.4
5 * Description: Utility-functions to act as a PUT server and client
6 * Status: Experimental.
7 * Author: Pontus Fuchs <pontus.fuchs@tactel.se>
8 * Created at: Sat Nov 20 10:54:14 1999
9 * Modified at: Sun Aug 13 01:54:05 PM CEST 2000
10 * Modified by: Pontus Fuchs <pontus.fuchs@tactel.se>
11 * Modified at: Sun Oct 06 10:00:00 2001
12 * Modified by: Ben Moore <ben@netjunki.org>
14 * Copyright (c) 1999, 2000 Pontus Fuchs, All Rights Reserved.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
31 ********************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
36 #include <openobex/obex.h>
37 #include "obex_put_common.h"
38 #include "obex_io.h"
39 #include "wmcapshare.h"
41 extern obex_t *handle;
42 extern volatile int finished;
43 extern volatile int state;
44 extern gchar *savedir; /*= "/home/bento/Work/Capture/";*/
47 volatile int last_rsp = OBEX_RSP_BAD_REQUEST;
50 * Function put_done()
52 * Parse what we got from a PUT
55 void put_done(obex_object_t *object)
57 obex_headerdata_t hv;
58 guint8 hi;
59 gint hlen;
61 const guint8 *body = NULL;
62 gint body_len = 0;
63 gchar *name = NULL;
64 gchar *namebuf = NULL;
66 while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) {
67 switch(hi) {
68 case OBEX_HDR_BODY:
69 body = hv.bs;
70 body_len = hlen;
71 break;
72 case OBEX_HDR_NAME:
73 if( (namebuf = g_malloc(hlen / 2))) {
74 OBEX_UnicodeToChar(namebuf, hv.bs, hlen);
75 name = namebuf;
77 break;
79 case OBEX_HDR_LENGTH:
80 g_print("HEADER_LENGTH = %d\n", hv.bq4);
81 break;
83 case HEADER_CREATOR_ID:
84 g_print("CREATORID = %#x\n", hv.bq4);
85 break;
87 default:
88 g_print(G_GNUC_FUNCTION "() Skipped header %02x\n", hi);
91 if(!body) {
92 g_print("Got a PUT without a body\n");
93 return;
95 if(!name) {
96 g_print("Got a PUT without a name. Setting name to %s\n", name);
97 name = "OBEX_PUT_Unknown_object";
100 safe_save_file(name, body, body_len, savedir);
101 g_free(namebuf);
106 * Function server_indication()
108 * Called when a request is about to come or has come.
111 void server_request(obex_object_t *object, gint event, gint cmd)
113 switch(cmd) {
114 case OBEX_CMD_SETPATH:
115 g_print("Received SETPATH command\n");
116 OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);
117 break;
118 case OBEX_CMD_PUT:
119 OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);
120 put_done(object);
121 break;
122 case OBEX_CMD_CONNECT:
123 OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS);
124 break;
125 case OBEX_CMD_DISCONNECT:
126 OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS);
127 break;
128 default:
129 g_print(G_GNUC_FUNCTION "() Denied %02x request\n", cmd);
130 OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, OBEX_RSP_NOT_IMPLEMENTED);
131 break;
133 return;
137 * Function client_done_indication()
139 * Called when a server-operation has finished
142 void server_done(obex_object_t *object, gint obex_cmd)
144 /* Quit if DISCONNECT has finished */
145 if(obex_cmd == OBEX_CMD_DISCONNECT)
146 finished = 1;
151 * Function client_done()
153 * Called when a client-operation has finished
156 void client_done(obex_object_t *object, gint obex_cmd, gint obex_rsp)
158 last_rsp = obex_rsp;
159 finished = TRUE;
163 * Function obex_event ()
165 * Called by the obex-layer when some event occurs.
168 void obex_event(obex_t *handle, obex_object_t *object, gint mode, gint event, gint obex_cmd, gint obex_rsp)
170 switch (event) {
171 case OBEX_EV_PROGRESS:
172 g_print(".");
173 updateDisplay();
174 break;
175 case OBEX_EV_REQDONE:
176 g_print("\n");
177 /* Comes when a command has finished. */
178 if(mode == OBEX_CLIENT)
179 client_done(object, obex_cmd, obex_rsp);
180 else
181 server_done(object, obex_cmd);
182 break;
183 case OBEX_EV_REQHINT:
184 /* Comes BEFORE the lib parses anything. */
185 switch(obex_cmd) {
186 case OBEX_CMD_PUT:
187 g_print("put\n");
188 state=CAP_RECIEVE;
189 updateDisplay();
190 OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);
191 break;
192 case OBEX_CMD_CONNECT:
193 g_print("connect\n");
194 state=CAP_CONNECT;
195 updateDisplay();
196 OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);
197 break;
198 case OBEX_CMD_DISCONNECT:
199 g_print("disconnect\n");
200 state=CAP_WAIT;
201 updateDisplay();
202 OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);
203 break;
204 default:
205 OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, OBEX_RSP_NOT_IMPLEMENTED);
206 break;
208 break;
209 case OBEX_EV_REQ:
210 /* Comes when a server-request has been received. */
211 g_print("server request\n");
212 server_request(object, event, obex_cmd);
213 break;
214 case OBEX_EV_LINKERR:
215 g_print("Link broken (this does not have to be an error)!\n");
216 finished = 1;
217 break;
218 default:
219 g_print("Unknown event!\n");
220 break;
225 * Function wait_for_rsp()
227 * Wait for a request to finish!
230 int wait_for_rsp()
232 int ret;
234 while(!finished) {
235 ret = OBEX_HandleInput(handle, 1);
236 if (ret < 0)
237 return ret;
239 return last_rsp;
243 * Function do_sync_request()
245 * Execute an OBEX-request synchronously.
248 int do_sync_request(obex_t *handle, obex_object_t *object, gint async)
250 gint ret;
251 OBEX_Request(handle, object);
252 ret = wait_for_rsp();
253 finished = FALSE;
254 return ret;