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
|
/*
*
* put.c - COBEX - Send files to a device.
*
* Copyright (c) 2003,2004,2005,2006 Fredrik Srensson
*
* History:
* v0.5 - fsn - 04-02-22 - First version
* v0.6 - fsn - 05-07-19 - Using general signal handling and general path handling
* v0.7 - fsn - 06-07-29 - Code cleanup
*
* Source:
*
*/
#include <stdio.h>
#include <ezV24/ezV24.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "cobex_core.h"
#include "cobex_tools.h"
#include "cobex_serial.h"
v24_port_t *UsedPort=NULL;
// Send the Buffer
int sendBuffer (char *inBuffer, int inBuffLgt, obex_packet *inPacket) {
int sent=0;
while (sent < inBuffLgt) {
obex_opcode_put ( inPacket );
sent+=obex_hi_body ( inPacket, inBuffer, inBuffLgt, sent );
cobex_packlgt ( inPacket );
if (cobex_packet_send(inPacket, UsedPort) != COBEX_OK) {
cobex_closePort(UsedPort);
return COBEX_ERR;
}
if ( cobex_packet_recieve(inPacket, UsedPort) != COBEX_OK) {
cobex_closePort(UsedPort);
return COBEX_ERR;
}
}
return COBEX_OK;
}
// Send a file
int sendFile (char *fileName, char *nameOnDevice, obex_packet *inPacket ) {
FILE *fh;
char *path;
char *name;
char fileBuffer[4001];
int total=0;
int buffLgt;
int rc;
struct stat buf;
// Open file...
if (stat(fileName,&buf) != 0) { return COBEX_ERR; }
fh=fopen(fileName, "r");
if (fh==NULL) { return COBEX_ERR; }
//Set Path
path=calloc( strlen(nameOnDevice)+1, sizeof(char) );
name=calloc( strlen(nameOnDevice)+1, sizeof(char) );
ctools_buildPath (nameOnDevice, path, name);
rc=ctools_recursePath( inPacket, path, UsedPort, OBEX_SETPATH_DONTCREATE );
// Send put start
obex_opcode_put ( inPacket );
obex_hi_length ( inPacket, buf.st_size );
obex_hi_name ( inPacket, name, strlen(name)+1 );
//obex_hi_name ( inPacket, nameOnDevice, strlen(nameOnDevice)+1 );
cobex_packlgt ( inPacket );
free(name);
free(path);
// dump_packet_headers (inPacket);
if ( cobex_packet_send(inPacket, UsedPort) != COBEX_OK) {
fclose (fh);
return COBEX_ERR;
}
if ( cobex_packet_recieve(inPacket, UsedPort) != COBEX_OK) {
fclose (fh);
return COBEX_ERR;
}
// dump_packet_headers (inPacket);
if ( inPacket->buffer[0] != (OBEX_RESPONSE_CONTINUE | OBEX_FINAL_BIT) ) {
fclose (fh);
return COBEX_ERR;
}
// Send file
while ((buffLgt=fread (fileBuffer,1,4000,fh)) > 0 ) {
rc=sendBuffer ( fileBuffer, buffLgt, inPacket);
if (rc != 0) { fclose(fh); return rc; }
if (cobex_response_code(inPacket) != (OBEX_RESPONSE_CONTINUE | OBEX_FINAL_BIT)) {
fclose(fh);
return COBEX_ERR;
}
total+=buffLgt;
printf("Sent %d bytes.\r",total);
fflush(stdout);
}
printf ("\n");
fclose (fh);
// Send end_of_body
obex_opcode_put ( inPacket );
obex_hi_endofbody ( inPacket );
cobex_packlgt ( inPacket );
cobex_set_final_bit( inPacket );
if (cobex_packet_send(inPacket, UsedPort) != COBEX_OK) {
return COBEX_ERR;
}
if ( cobex_packet_recieve(inPacket, UsedPort) != COBEX_OK) {
return COBEX_ERR;
}
if (inPacket->buffer[0] != (OBEX_RESPONSE_OK | OBEX_FINAL_BIT) ) {
return COBEX_ERR;
}
return COBEX_OK;
}
void help () {
printf ("cobex_put v0.5\n\n");
printf ("Usage: obex_put <device> <infile> <name_on_device>\n\n");
printf ("All arguments are compulsory and order is important. Returncodes are used to\n");
printf ("indicate errors. Use the perl frontend if you want more userfriendlyness.\n\n");
}
// Main routine
int main (int argc, char *argv[]) {
int rc;
int endStatus;
static char fsBrowser[]={ 0xf9, 0xec, 0x7b, 0xc4, 0x95, 0x3c, 0x11, 0xd2, 0x98, 0x4e, 0x52, 0x54, 0x00, 0xdc, 0x9e, 0x09, 0x00};
char outBuffer[513];
obex_packet outPacket;
outPacket.max=512;
//outPacket.buffer=outBuffer;
outPacket.buffer=(u_char *)outBuffer;
if (argc != 4) {
help() ;
return COBEX_ERR;
}
// Set up the port
rc = ctools_commonInit( &UsedPort, &outPacket, argv[1], fsBrowser );
if (rc != COBEX_OK) { exit (COBEX_ERR); }
// Do the specific things
endStatus=sendFile(argv[2],argv[3],&outPacket);
if (cobex_response_code(&outPacket) != (OBEX_RESPONSE_OK|OBEX_FINAL_BIT)) {
fprintf (stderr, "ERR: %s \n",
cobex_respstring(cobex_response_code(&outPacket)) ) ;
rc=COBEX_ERR;
}
// Bye, y'all!
ctools_disconnect( &outPacket, UsedPort );
cobex_closePort(UsedPort);
return rc;
}
|