set_name.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * * Software License Agreement (BSD License)
3  * *
4  * * Copyright (c) 2009-2010, Willow Garage, Inc.
5  * * All rights reserved.
6  * *
7  * * Redistribution and use in source and binary forms, with or without
8  * * modification, are permitted provided that the following conditions
9  * * are met:
10  * *
11  * * * Redistributions of source code must retain the above copyright
12  * * notice, this list of conditions and the following disclaimer.
13  * * * Redistributions in binary form must reproduce the above
14  * * copyright notice, this list of conditions and the following
15  * * disclaimer in the documentation and/or other materials provided
16  * * with the distribution.
17  * * * Neither the name of the Willow Garage nor the names of its
18  * * contributors may be used to endorse or promote products derived
19  * * from this software without specific prior written permission.
20  * *
21  * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * * POSSIBILITY OF SUCH DAMAGE.
33  * *********************************************************************/
34 
35 // Author: Blaise Gassend
36 
37 #include <assert.h>
38 #include <iostream>
39 #include <fstream>
40 
41 #include <string.h> // for memset(3)
42 #include <stdlib.h> // for atoi(3)
43 
44 #include <ros/console.h>
45 #include <ros/time.h>
46 
50 
51 uint16_t checksum(uint16_t *data)
52 {
53  uint16_t sum = 0;
54  for (int i = 0; i < FLASH_PAGE_SIZE / 2; i++)
55  sum += htons(data[i]);
56  return htons(0xFFFF - sum);
57 }
58 
59 int read_name(IpCamList *camera)
60 {
61  uint8_t namebuff[FLASH_PAGE_SIZE];
62  IdentityFlashPage *id = (IdentityFlashPage *) &namebuff;
63 
64  if(wge100ReliableFlashRead(camera, FLASH_NAME_PAGENO, (uint8_t *) namebuff, NULL) != 0)
65  {
66  fprintf(stderr, "Flash read error. Aborting.\n");
67  return -2;
68  }
69 
70  uint16_t chk = checksum((uint16_t *) namebuff);
71  if (chk)
72  {
73  fprintf(stderr, "Previous camera name had bad checksum.\n");
74  }
75 
76  id->cam_name[sizeof(id->cam_name) - 1] = 0;
77  printf("Previous camera name was: %s\n", id->cam_name);
78  uint8_t *oldip = (uint8_t *) &id->cam_addr;
79  printf("Previous camera IP: %i.%i.%i.%i\n", oldip[0], oldip[1], oldip[2], oldip[3]);
80 
81  return 0;
82 }
83 
84 int write_name(IpCamList *camera, char *name, char *new_ip)
85 {
86  uint8_t namebuff[FLASH_PAGE_SIZE];
87  IdentityFlashPage *id = (IdentityFlashPage *) &namebuff;
88 
89  if (strlen(name) > sizeof(id->cam_name) - 1)
90  {
91  fprintf(stderr, "Name is too long, the maximum number of characters is %zu.\n", sizeof(id->cam_name) - 1);
92  return -2;
93  }
94  bzero(namebuff, FLASH_PAGE_SIZE);
95  strncpy(id->cam_name, name, sizeof(id->cam_name) - 1);
96  id->cam_name[sizeof(id->cam_name) - 1] = 0;
97  struct in_addr cam_ip;
98  if (!inet_aton(new_ip, &cam_ip))
99  {
100  fprintf(stderr, "This is not a valid IP address: %s\n", new_ip);
101  return -2;
102  }
103  id->cam_addr = cam_ip.s_addr;
104  id->checksum = checksum((uint16_t *) namebuff);
105 
106  if (wge100ReliableFlashWrite(camera, FLASH_NAME_PAGENO, (uint8_t *) namebuff, NULL) != 0)
107  {
108  fprintf(stderr, "Flash write error. The camera name is an undetermined state.\n");
109  return -2;
110  }
111 
112  fprintf(stderr, "Success! Restarting camera, should take about 10 seconds to come back up after this.\n");
113 
114  wge100Reset(camera);
115 
116  return 0;
117 }
118 
119 int main(int argc, char **argv)
120 {
121  if ((argc != 4 && argc != 2) || !strcmp(argv[1], "--help")) {
122  fprintf(stderr, "Usage: %s <camera_url> <new_name> <new_default_ip> # Sets the camera name and default IP\n", argv[0]);
123  fprintf(stderr, " %s <camera_url> # Reads the camera name and default IP\n", argv[0]);
124  fprintf(stderr, "\nReads or writes the camera name and default IP address stored on the camera's flash.\n");
125  return -1;
126  }
127 
128  char *camera_url = argv[1];
129 
130  // Find the camera matching the URL
131  IpCamList camera;
132  const char *errmsg;
133  int outval = wge100FindByUrl(camera_url, &camera, SEC_TO_USEC(0.1), &errmsg);
134  if (outval)
135  {
136  fprintf(stderr, "Matching URL %s : %s\n", camera_url, errmsg);
137  return -1;
138  }
139 
140  // Configure the camera with its IP address, wait up to 500ms for completion
141  outval = wge100Configure(&camera, camera.ip_str, SEC_TO_USEC(0.5));
142  if (outval != 0) {
143  if (outval == ERR_CONFIG_ARPFAIL) {
144  fprintf(stderr, "Unable to create ARP entry (are you root?), continuing anyway\n");
145  } else {
146  fprintf(stderr, "IP address configuration failed\n");
147  return -1;
148  }
149  }
150 
151  outval = read_name(&camera);
152  if (outval)
153  return outval;
154 
155  if (argc != 4)
156  return 0;
157 
158  char *name = argv[2];
159  char *new_ip = argv[3];
160 
161  return write_name(&camera, name, new_ip);
162 }
#define FLASH_PAGE_SIZE
Definition: ipcam_packet.h:60
char cam_name[CAMERA_NAME_LEN]
Should be zero terminated. Will be forcibly zero terminated otherwise.
Definition: ipcam_packet.h:144
#define FLASH_NAME_PAGENO
Definition: ipcam_packet.h:62
int wge100ReliableFlashRead(const IpCamList *camInfo, uint32_t address, uint8_t *pageDataOut, int *retries)
Definition: wge100lib.c:906
int wge100ReliableFlashWrite(const IpCamList *camInfo, uint32_t address, const uint8_t *pageDataIn, int *retries)
Definition: wge100lib.c:1014
int wge100Reset(IpCamList *camInfo)
Definition: wge100lib.c:788
int read_name(IpCamList *camera)
Definition: set_name.cpp:59
int wge100Configure(IpCamList *camInfo, const char *ipAddress, unsigned wait_us)
Definition: wge100lib.c:532
#define ERR_CONFIG_ARPFAIL
Definition: wge100lib.h:116
#define SEC_TO_USEC(sec)
Definition: host_netutil.h:65
int wge100FindByUrl(const char *url, IpCamList *camera, unsigned wait_us, const char **errmsg)
Definition: wge100lib.c:94
char ip_str[16]
Definition: list.h:296
int main(int argc, char **argv)
Definition: set_name.cpp:119
IPAddress cam_addr
Camera address at power-on.
Definition: ipcam_packet.h:145
int write_name(IpCamList *camera, char *name, char *new_ip)
Definition: set_name.cpp:84
uint16_t checksum(uint16_t *data)
Definition: set_name.cpp:51


wge100_camera
Author(s): Blaise Gassend, Patrick Mihelich, Eric MacIntosh, David Palchak
autogenerated on Mon Jun 10 2019 15:44:15