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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <assert.h>
00038 #include <iostream>
00039 #include <fstream>
00040
00041 #include <string.h>
00042 #include <stdlib.h>
00043
00044 #include <ros/console.h>
00045 #include <ros/time.h>
00046
00047 #include <wge100_camera/ipcam_packet.h>
00048 #include <wge100_camera/host_netutil.h>
00049 #include <wge100_camera/wge100lib.h>
00050
00051 uint16_t checksum(uint16_t *data)
00052 {
00053 uint16_t sum = 0;
00054 for (int i = 0; i < FLASH_PAGE_SIZE / 2; i++)
00055 sum += htons(data[i]);
00056 return htons(0xFFFF - sum);
00057 }
00058
00059 int read_name(IpCamList *camera)
00060 {
00061 uint8_t namebuff[FLASH_PAGE_SIZE];
00062 IdentityFlashPage *id = (IdentityFlashPage *) &namebuff;
00063
00064 if(wge100ReliableFlashRead(camera, FLASH_NAME_PAGENO, (uint8_t *) namebuff, NULL) != 0)
00065 {
00066 fprintf(stderr, "Flash read error. Aborting.\n");
00067 return -2;
00068 }
00069
00070 uint16_t chk = checksum((uint16_t *) namebuff);
00071 if (chk)
00072 {
00073 fprintf(stderr, "Previous camera name had bad checksum.\n");
00074 }
00075
00076 id->cam_name[sizeof(id->cam_name) - 1] = 0;
00077 printf("Previous camera name was: %s\n", id->cam_name);
00078 uint8_t *oldip = (uint8_t *) &id->cam_addr;
00079 printf("Previous camera IP: %i.%i.%i.%i\n", oldip[0], oldip[1], oldip[2], oldip[3]);
00080
00081 return 0;
00082 }
00083
00084 int write_name(IpCamList *camera, char *name, char *new_ip)
00085 {
00086 uint8_t namebuff[FLASH_PAGE_SIZE];
00087 IdentityFlashPage *id = (IdentityFlashPage *) &namebuff;
00088
00089 if (strlen(name) > sizeof(id->cam_name) - 1)
00090 {
00091 fprintf(stderr, "Name is too long, the maximum number of characters is %zu.\n", sizeof(id->cam_name) - 1);
00092 return -2;
00093 }
00094 bzero(namebuff, FLASH_PAGE_SIZE);
00095 strncpy(id->cam_name, name, sizeof(id->cam_name) - 1);
00096 id->cam_name[sizeof(id->cam_name) - 1] = 0;
00097 struct in_addr cam_ip;
00098 if (!inet_aton(new_ip, &cam_ip))
00099 {
00100 fprintf(stderr, "This is not a valid IP address: %s\n", new_ip);
00101 return -2;
00102 }
00103 id->cam_addr = cam_ip.s_addr;
00104 id->checksum = checksum((uint16_t *) namebuff);
00105
00106 if (wge100ReliableFlashWrite(camera, FLASH_NAME_PAGENO, (uint8_t *) namebuff, NULL) != 0)
00107 {
00108 fprintf(stderr, "Flash write error. The camera name is an undetermined state.\n");
00109 return -2;
00110 }
00111
00112 fprintf(stderr, "Success! Restarting camera, should take about 10 seconds to come back up after this.\n");
00113
00114 wge100Reset(camera);
00115
00116 return 0;
00117 }
00118
00119 int main(int argc, char **argv)
00120 {
00121 if ((argc != 4 && argc != 2) || !strcmp(argv[1], "--help")) {
00122 fprintf(stderr, "Usage: %s <camera_url> <new_name> <new_default_ip> # Sets the camera name and default IP\n", argv[0]);
00123 fprintf(stderr, " %s <camera_url> # Reads the camera name and default IP\n", argv[0]);
00124 fprintf(stderr, "\nReads or writes the camera name and default IP address stored on the camera's flash.\n");
00125 return -1;
00126 }
00127
00128 char *camera_url = argv[1];
00129
00130
00131 IpCamList camera;
00132 const char *errmsg;
00133 int outval = wge100FindByUrl(camera_url, &camera, SEC_TO_USEC(0.1), &errmsg);
00134 if (outval)
00135 {
00136 fprintf(stderr, "Matching URL %s : %s\n", camera_url, errmsg);
00137 return -1;
00138 }
00139
00140
00141 outval = wge100Configure(&camera, camera.ip_str, SEC_TO_USEC(0.5));
00142 if (outval != 0) {
00143 if (outval == ERR_CONFIG_ARPFAIL) {
00144 fprintf(stderr, "Unable to create ARP entry (are you root?), continuing anyway\n");
00145 } else {
00146 fprintf(stderr, "IP address configuration failed\n");
00147 return -1;
00148 }
00149 }
00150
00151 outval = read_name(&camera);
00152 if (outval)
00153 return outval;
00154
00155 if (argc != 4)
00156 return 0;
00157
00158 char *name = argv[2];
00159 char *new_ip = argv[3];
00160
00161 return write_name(&camera, name, new_ip);
00162 }