00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
#include <bluetooth/bluetooth.h>
00014
00015
#include "blueobex.h"
00016
00017
extern "C"
00018 {
00019
#include <openobex/obex.h>
00020 }
00021
00022
#include <fcntl.h>
00023
#include <stdlib.h>
00024
#include <stdio.h>
00025
#include <unistd.h>
00026
00027
00028
00029
00030
00031
00032 obex_t* obexInit(
struct BlueObex *myObex,
int method,
const char *address,
int channel)
00033 {
00034
int transport_m;
00035
00036
00037
if (method == 1)
00038 {
00039 transport_m = OBEX_TRANS_IRDA;
00040 }
00041
else if (method == 2)
00042 {
00043 transport_m = OBEX_TRANS_BLUETOOTH;
00044 }
00045
else
00046 {
00047 printf(
"Invalid transport method\n");
00048
return NULL;
00049 }
00050
00051 obex_t *handle;
00052
if (! (handle = OBEX_Init(transport_m, obexEvent, 0)))
00053 {
00054 printf(
"Could not init OBEX\n");
00055
return NULL;
00056 }
00057
00058 OBEX_SetUserData(handle, myObex);
00059
00060
if (method == 1)
00061 {
00062
if((IrOBEX_TransportConnect(handle,
"OBEX")) < 0 )
00063 {
00064 printf(
"OBEX transport connect error\n");
00065
return NULL;
00066 }
00067 }
00068
00069
00070
if (method == 2)
00071 {
00072 bdaddr_t bda;
00073
00074 str2ba(address, &bda);
00075
00076
if(BtOBEX_TransportConnect(handle, BDADDR_ANY, &bda, channel) < 0)
00077 {
00078 printf(
"OBEX transport connect error\n");
00079
return NULL;
00080 }
00081 }
00082
00083
return handle;
00084 }
00085
00086
00087
00088
00089
00090
int obexConnect(obex_t *handle)
00091 {
00092 obex_object_t *object;
00093 obex_headerdata_t header;
00094
00095
if (! (object = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT)))
00096 {
00097 printf(
"Could not create OBEX object\n");
00098
return -1;
00099 }
00100
00101 header.bs = (uint8_t*)
"Linux";
00102
00103
if (OBEX_ObjectAddHeader(handle, object, OBEX_HDR_WHO, header, 6, OBEX_FL_FIT_ONE_PACKET) < 0)
00104 {
00105 printf(
"Could not add OBEX header\n");
00106 OBEX_ObjectDelete(handle, object);
00107
return -1;
00108 }
00109
00110 OBEX_Request(handle, object);
00111 syncwait(handle);
00112
00113
return 0;
00114 }
00115
00116
00117
00118
00119
00120
void obexEvent(obex_t *handle, obex_object_t *object,
int mode,
int event,
int obex_cmd,
int obex_rsp)
00121 {
00122
switch (event)
00123 {
00124
case OBEX_EV_PROGRESS:
00125 printf(
"OBEX made some progress..\n");
00126
break;
00127
00128
case OBEX_EV_ABORT:
00129 printf(
"OBEX request aborted\n");
00130
break;
00131
00132
case OBEX_EV_REQDONE:
00133 obexEventDone(handle, object, obex_cmd, obex_rsp);
00134
break;
00135
00136
case OBEX_EV_REQHINT:
00137 OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);
00138
break;
00139
00140
case OBEX_EV_LINKERR:
00141 OBEX_TransportDisconnect(handle);
00142 printf(
"OBEX link broken\n");
00143
break;
00144
00145
default:
00146 printf(
"Unknown OBEX event\n");
00147
break;
00148 }
00149 }
00150
00151
00152
00153
00154
00155
char* obexPutReadFile(
const char *filename,
int *length)
00156 {
00157
int fd;
00158
char *file_buf = NULL;
00159
struct stat stat_buf;
00160
00161 fd = open(filename, O_RDONLY);
00162
if (fd == -1)
00163 {
00164 printf(
"Error opening file: %s\n", filename);
00165 }
00166
else
00167 {
00168
if (fstat(fd, &stat_buf) == -1)
00169 {
00170 printf(
"Could not stat file: %s\n", filename);
00171 }
00172
else
00173 {
00174 *length = stat_buf.st_size;
00175
00176 file_buf = (
char*)malloc(*length);
00177
00178
if (file_buf == NULL)
00179 {
00180 printf(
"Malloc failed\n");
00181 file_buf = NULL;
00182 *length = 0;
00183 }
00184
else
00185 {
00186
int nb = read(fd, file_buf, *length);
00187
00188
if (nb != *length)
00189 {
00190 printf(
"Error: short read\n");
00191 exit(1);
00192 }
00193 }
00194 }
00195
00196 close(fd);
00197 }
00198
00199
return file_buf;
00200 }
00201
00202
00203
00204
00205
00206
int obexPutfile(obex_t *handle,
struct BlueObex *myObex,
const char *name)
00207 {
00208 obex_object_t *object;
00209 obex_headerdata_t header;
00210
int bodysize;
00211
char *body;
00212 uint8_t *tmp;
00213
int namelen;
00214
00215
if (!(object = OBEX_ObjectNew(handle, OBEX_CMD_PUT)))
00216 {
00217 printf(
"Error creating OBEX object\n");
00218
return -1;
00219 }
00220
00221 body = obexPutReadFile(myObex->
filename, &bodysize);
00222
00223
if (body == NULL)
00224 {
00225 OBEX_ObjectDelete(handle, object);
00226
return -1;
00227 }
00228
00229 namelen = (strlen(name)*2)+2;
00230 tmp = (uint8_t*)malloc(namelen);
00231
00232
if(header.bs)
00233 {
00234 namelen = OBEX_CharToUnicode(tmp, (uint8_t*)name, namelen);
00235 header.bs = tmp;
00236
00237
if (OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME, header, namelen, 0) < 0)
00238 {
00239 printf(
"Error add OBEX name header\n");
00240 OBEX_ObjectDelete(handle, object);
00241 free(body);
00242
return -1;
00243 }
00244
00245 free ((
void*)header.bs);
00246 }
00247
else
00248 {
00249 printf(
"Malloc error\n");
00250
return -1;
00251 }
00252
00253 header.bq4 = bodysize;
00254
00255
if (OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, header, 4, 0) <0)
00256 {
00257 printf(
"Error adding OBEX length header\n");
00258 OBEX_ObjectDelete(handle, object);
00259 free(body);
00260
return -1;
00261 }
00262
00263 header.bs = (uint8_t*)body;
00264
00265
if(OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY, header, bodysize, 0) <0 )
00266 {
00267 printf(
"Error adding OBEX body header\n");
00268 OBEX_ObjectDelete(handle, object);
00269 free(body);
00270
return -1;
00271 }
00272
00273 OBEX_Request(handle, object);
00274 syncwait(handle);
00275
00276 free(body);
00277
return 0;
00278 }
00279
00280
00281
00282
00283
00284
void syncwait(obex_t *handle)
00285 {
00286
struct BlueObex *session;
00287
int ret;
00288
00289 session = (
BlueObex*)OBEX_GetUserData(handle);
00290
00291
while (!session->client_done)
00292 {
00293 ret = OBEX_HandleInput(handle, 10);
00294
00295
if (ret < 0)
00296 {
00297 printf(
"Error on OBEX HandleInput\n");
00298
break;
00299 }
00300
else if (ret == 0)
00301 {
00302 printf(
"OBEX timeout\n");
00303 OBEX_CancelRequest(handle,
false);
00304
break;
00305 }
00306 }
00307
00308 session->client_done =
false;
00309 }
00310
00311
00312
00313
00314
00315
void obexEventDone(obex_t *handle, obex_object_t *object,
int obex_cmd,
int obex_rsp)
00316 {
00317
struct BlueObex *session;
00318
00319 session = (
BlueObex*)OBEX_GetUserData(handle);
00320
00321
switch (obex_cmd)
00322 {
00323
00324
case OBEX_CMD_CONNECT:
00325 {
00326
if (obex_rsp == OBEX_RSP_SUCCESS)
00327 {
00328 printf(
"OBEX connected\n");
00329 }
00330
else
00331 {
00332 printf(
"OBEX connect failed\n");
00333 }
00334
00335
break;
00336 }
00337
00338
case OBEX_CMD_DISCONNECT:
00339 {
00340
if (! (object = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT)))
00341 {
00342 printf(
"Error on OBEX disconnect\n");
00343
return;
00344 }
00345
00346 OBEX_Request(handle, object);
00347 syncwait(handle);
00348
00349
break;
00350 }
00351
00352
case OBEX_CMD_PUT:
00353 {
00354
if (obex_rsp == OBEX_RSP_SUCCESS)
00355 {
00356 printf(
"OBEX PUT succeeded\n");
00357 }
00358
else
00359 {
00360 printf(
"OBEX PUT failed\n");
00361 }
00362
00363
break;
00364 }
00365
00366
default:
00367
break;
00368 }
00369
00370 session->client_done =
true;
00371 }
00372
00373
00374
00375
00376
00377
int obexDisconnect(obex_t *handle)
00378 {
00379 OBEX_TransportDisconnect(handle);
00380
00381 printf(
"OBEX disconnected\n");
00382
00383
return 0;
00384 }
00385
00386