00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 # include <dtn-config.h>
00029 #endif
00030
00031 #include <sys/types.h>
00032 #include <signal.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <unistd.h>
00036 #include <getopt.h>
00037 #include <fcntl.h>
00038 #include <string.h>
00039 #include "serialsource.h"
00040
00041 #include <strings.h>
00042 #include <errno.h>
00043 #include <sys/stat.h>
00044 #include <sys/time.h>
00045 #include <time.h>
00046
00047 #include <oasys/compat/inttypes.h>
00048
00049 #include "dtn_api.h"
00050
00051 #define dout stderr
00052
00053 #include <ctype.h>
00054
00055 char *progname;
00056
00057 static char *msgs[] = {
00058 "unknown_packet_type",
00059 "ack_timeout" ,
00060 "sync" ,
00061 "too_long" ,
00062 "too_short" ,
00063 "bad_sync" ,
00064 "bad_crc" ,
00065 "closed" ,
00066 "no_memory" ,
00067 "unix_error"
00068 };
00069
00070 typedef struct data_packet
00071 {
00072
00073 u_int16_t source_mote_id;
00074 u_int16_t origin_mote_id;
00075 u_int16_t seq_no;
00076 u_int8_t hop_cnt;
00077
00078
00079 u_int8_t surge_pkt_type;
00080 u_int16_t surge_reading;
00081 u_int16_t surge_parent_addr;
00082 u_int32_t surge_seq_no;
00083 u_int8_t light;
00084 u_int8_t temp;
00085 u_int8_t magx;
00086 u_int8_t magy;
00087 u_int8_t accelx;
00088 u_int8_t accely;
00089
00090 }DATAPACKET;
00091
00092 #define DATAPACKET_SIZE 22
00093 #define SURGE_PKT 0x11
00094 #define DEBUG_PKT 0x03
00095
00096 void parse_options(int, char**);
00097 dtn_endpoint_id_t * parse_eid(dtn_handle_t handle, dtn_endpoint_id_t * eid,
00098 char * str);
00099 void print_usage();
00100 void print_eid(char * label, dtn_endpoint_id_t * eid);
00101 void init_motes();
00102 void stderr_msg(serial_source_msg problem);
00103 void usage(char *str1, char *str2);
00104 void readCommandLineArgs(int argc, char **argv);
00105 void hexdump();
00106 void read_packet_file(char* filename);
00107
00108
00109 char arg_dest[128];
00110 char arg_target[128];
00111
00112 char devicename[128] = "/dev/ttyS0";
00113 char baud[128] = "57600";
00114 char directory[128]="send";
00115 u_int32_t debug = 0;
00116 serial_source src;
00117
00118 int g_argc;
00119 char **g_argv;
00120
00121 int
00122 main(int argc, char **argv)
00123 {
00124
00125 g_argc = argc;
00126 g_argv = argv;
00127
00128 readCommandLineArgs(argc, argv);
00129 init_motes();
00130
00131
00132 return (EXIT_FAILURE);
00133 }
00134
00135 void stderr_msg(serial_source_msg problem)
00136 {
00137 fprintf(stderr, "Note: %s\n", msgs[problem]);
00138 }
00139
00140 int read_packet(char *buf, int *n)
00141 {
00142 const char *buff;
00143 int i;
00144
00145 if (debug > 0) fprintf(stdout, "Reading packet:\n");
00146
00147 if(!(buff = read_serial_packet(src, n)))
00148 return 0;
00149
00150 if (debug > 0) fprintf(stdout, " ==> : ");
00151 for (i = 0; i < buff[4]; i++)
00152 printf(" %02x", buff[i]);
00153 putchar('\n');
00154
00155
00156
00157 memset(buf,0,BUFSIZ);
00158 memcpy(buf,buff,buff[4]+5);
00159 *n=buff[4] + 5;
00160 if(buff[2]==SURGE_PKT || buff[2]==DEBUG_PKT) return buff[2];
00161 return -1;
00162 }
00163
00164 int
00165 reader_thread(void *p)
00166 {
00167
00168
00169
00170 static int tcnt=0;
00171 DATAPACKET *dataPacket;
00172
00173
00174 int ret;
00175 dtn_handle_t handle;
00176 dtn_reg_info_t reginfo;
00177 dtn_reg_id_t regid = DTN_REGID_NONE;
00178 dtn_bundle_spec_t bundle_spec;
00179 dtn_bundle_payload_t send_payload;
00180 dtn_bundle_id_t bundle_id;
00181 char demux[4096];
00182
00183 p = NULL;
00184
00185
00186 if (debug > 0) fprintf(stdout, "Opening connection to local DTN daemon\n");
00187
00188 int err = dtn_open(&handle);
00189 if (err != DTN_SUCCESS) {
00190 fprintf(stderr, "fatal error opening dtn handle: %s\n",
00191 dtn_strerror(err));
00192 exit(1);
00193 }
00194
00195
00196
00197
00198
00199
00200 memset(&bundle_spec, 0, sizeof(bundle_spec));
00201
00202
00203 sprintf(demux, "%s/dtnmoteproxy/recv", arg_dest);
00204 parse_eid(handle, &bundle_spec.dest, demux);
00205
00206
00207 sprintf(demux, "/dtnmoteproxy/send");
00208 parse_eid(handle, &bundle_spec.source, demux);
00209
00210
00211 dtn_copy_eid(&bundle_spec.replyto, &bundle_spec.source);
00212
00213
00214 if (debug > 2)
00215 {
00216 print_eid("source_eid", &bundle_spec.source);
00217 print_eid("replyto_eid", &bundle_spec.replyto);
00218 print_eid("dest_eid", &bundle_spec.dest);
00219 }
00220
00221
00222 bundle_spec.dopts |= DOPTS_DELIVERY_RCPT;
00223
00224
00225
00226
00227 memset(®info, 0, sizeof(reginfo));
00228 dtn_copy_eid(®info.endpoint, &bundle_spec.replyto);
00229 reginfo.flags = DTN_REG_DEFER;
00230 reginfo.regid = regid;
00231 reginfo.expiration = 0;
00232 if ((ret = dtn_register(handle, ®info, ®id)) != 0) {
00233 fprintf(stderr, "error creating registration (id=%d): %d (%s)\n",
00234 regid, ret, dtn_strerror(dtn_errno(handle)));
00235 exit(1);
00236 }
00237
00238 if (debug > 3) printf("dtn_register succeeded, regid 0x%x\n", regid);
00239
00240 while (1) {
00241 static unsigned char motedata[BUFSIZ];
00242 int length;
00243 int ret;
00244
00245 if (debug > 1) fprintf(dout, "about to read from motes...\n");
00246
00247 while((ret=read_packet((char *) motedata, (int *) &length))){
00248 if(ret==DEBUG_PKT)
00249 continue;
00250 if (debug > 0) {
00251 fprintf(dout, "\nreader loop... got [%d] bytes from motes\n",
00252 length);
00253 if (debug > 1) hexdump(motedata, length);
00254 }
00255
00256
00257
00258 dataPacket=(DATAPACKET *)((void*)motedata);
00259
00260
00261 if(dataPacket->origin_mote_id == 0) continue;
00262
00263
00264 bundle_spec.expiration = 3600;
00265
00266
00267 memset(&send_payload, 0, sizeof(send_payload));
00268
00269 dtn_set_payload(&send_payload, DTN_PAYLOAD_MEM,
00270 (char *) motedata, length);
00271
00272 memset(&bundle_id, 0, sizeof(bundle_id));
00273
00274 if ((ret = dtn_send(handle, regid, &bundle_spec, &send_payload,
00275 &bundle_id)) != 0)
00276 {
00277 fprintf(stderr, "error sending bundle: %d (%s)\n",
00278 ret, dtn_strerror(dtn_errno(handle)));
00279 }
00280 else fprintf(stderr, "motedata bundle sent");
00281
00282 printf("Mote ID = %u\n",dataPacket->origin_mote_id);
00283 printf("Source Mote ID = %u\n",dataPacket->source_mote_id);
00284 printf("Hop Count = %u\n",dataPacket->hop_cnt);
00285 printf("Packet Type = %u\n",dataPacket->surge_pkt_type);
00286 printf("Parent Address = %u\n",dataPacket->surge_parent_addr);
00287 printf("Sequence Number = %u\n", (u_int)dataPacket->surge_seq_no);
00288 printf("Light = %u\n",dataPacket->light);
00289 printf("Temperature = %u\n\n",dataPacket->temp);
00290
00291 tcnt=(tcnt+1)%10000;
00292
00293 }
00294 if (debug > 0)
00295 fprintf(dout, "reader loop.... nothing to do? [shouldn't happen]\n");
00296 }
00297
00298
00299 dtn_close(handle);
00300
00301 return (1);
00302
00303 }
00304
00305
00306 void
00307 readCommandLineArgs(int argc, char **argv)
00308 {
00309 int c;
00310
00311 while ((c = getopt(argc, argv, "hr:d:b:D:t:")) != EOF) {
00312 switch (c) {
00313 case 'h':
00314 usage("moteproxy", "");
00315 exit(0);
00316 break;
00317 case 'r':
00318 read_packet_file(optarg);
00319 exit(0);
00320 case 'd':
00321 debug = atoi(optarg);
00322 break;
00323 case 'b':
00324 strcpy(baud, optarg);
00325 break;
00326 case 't':
00327 strcpy(devicename, optarg);
00328 break;
00329 case 'D':
00330 strcpy(arg_dest,optarg);
00331 break;
00332 default:
00333 fprintf(stderr, "mfproxy: unknown option: '%c'\n", (char) c);
00334 usage("moteproxy", "");
00335 exit(EXIT_FAILURE);
00336 }
00337 }
00338 }
00339
00340 void
00341 usage(char *str1, char *str2)
00342 {
00343 (void)str2;
00344
00345 fprintf(stderr, "usage: %s\n", str1);
00346 fprintf(stderr, " [-b baudrate] - baud rate\n");
00347 fprintf(stderr, " [-t devicename] - name of mote network dev tty\n");
00348 fprintf(stderr, " [-d debugValue]\n");
00349 fprintf(stderr, " [-D directory]\n");
00350 fprintf(stderr, " [-h] - print this message.\n");
00351 fprintf(stderr, "\n");
00352 }
00353
00354
00355
00356 void
00357 init_motes()
00358 {
00359 src = open_serial_source(devicename, atoi(baud), 0, stderr_msg);
00360
00361 if(reader_thread(NULL) == 1) {
00362 fprintf(stderr, "couldn't start reader on mote network\n");
00363 exit(EXIT_FAILURE);
00364 }
00365 return;
00366 }
00367
00368 void read_packet_file(char* filename)
00369 {
00370 int fd = open(filename, O_RDONLY);
00371 static unsigned char buf[BUFSIZ];
00372 int n = read(fd, buf, BUFSIZ);
00373 hexdump(buf, n);
00374 }
00375
00376 void
00377 hexdump(unsigned char *buf, int n)
00378 {
00379 int i;
00380 unsigned char *p = buf;
00381
00382 fprintf(dout,"Packet contains %d:\n",n);
00383 for (i = 0; i < n; i++) {
00384 fprintf(dout,"%02x ", *p++);
00385 if ((i & 0x7) == 0x7)
00386 fprintf(dout,"\n");
00387 }
00388 printf("\n\n");
00389 fflush(stdout);
00390 }
00391
00392 dtn_endpoint_id_t * parse_eid(dtn_handle_t handle,
00393 dtn_endpoint_id_t * eid, char * str)
00394 {
00395
00396
00397 if (!dtn_parse_eid_string(eid, str))
00398 {
00399 return eid;
00400 }
00401
00402
00403 else if (!dtn_build_local_eid(handle, eid, str))
00404 {
00405 return eid;
00406 }
00407 else
00408 {
00409 fprintf(stderr, "invalid eid string '%s'\n", str);
00410 exit(1);
00411 }
00412 }
00413
00414 void print_eid(char* label, dtn_endpoint_id_t* eid)
00415 {
00416 printf("%s [%s]\n", label, eid->uri);
00417 }
00418