set_name.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * * Software License Agreement (BSD License)
00003  * *
00004  * *  Copyright (c) 2009-2010, Willow Garage, Inc.
00005  * *  All rights reserved.
00006  * *
00007  * *  Redistribution and use in source and binary forms, with or without
00008  * *  modification, are permitted provided that the following conditions
00009  * *  are met:
00010  * *
00011  * *   * Redistributions of source code must retain the above copyright
00012  * *     notice, this list of conditions and the following disclaimer.
00013  * *   * Redistributions in binary form must reproduce the above
00014  * *     copyright notice, this list of conditions and the following
00015  * *     disclaimer in the documentation and/or other materials provided
00016  * *     with the distribution.
00017  * *   * Neither the name of the Willow Garage nor the names of its
00018  * *     contributors may be used to endorse or promote products derived
00019  * *     from this software without specific prior written permission.
00020  * *
00021  * *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  * *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  * *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  * *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  * *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * *  POSSIBILITY OF SUCH DAMAGE.
00033  * *********************************************************************/
00034 
00035 // Author: Blaise Gassend
00036 
00037 #include <assert.h>
00038 #include <iostream>
00039 #include <fstream>
00040  
00041 #include <string.h> // for memset(3)
00042 #include <stdlib.h> // for atoi(3)
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   // Find the camera matching the URL
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   // Configure the camera with its IP address, wait up to 500ms for completion
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 }


wge100_camera
Author(s): Blaise Gassend, Patrick Mihelich, Eric MacIntosh, David Palchak
autogenerated on Sat Jun 8 2019 20:51:24