OMAPI  1.8
Open Movement Public API
deploy.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2012, Newcastle University, UK.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23  * POSSIBILITY OF SUCH DAMAGE.
24  */
25 
43 /* Headers - need a sleep function to wait */
44 #ifdef _WIN32
45 #define _CRT_SECURE_NO_WARNINGS
46 #include <windows.h>
47 #else
48 #include <unistd.h>
49 #define Sleep(millis) sleep((millis) / 1000)
50 #endif
51 #include <time.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <string.h>
55 
56 
57 /* API header */
58 #include "omapi.h"
59 
60 
61 /* Deploy function */
62 int deploy(const char *infile, const char *outfile)
63 {
64  int result;
65  unsigned int nextSessionId = 0;
66  FILE *ifp = NULL;
67  FILE *ofp = NULL;
68 
69  /* Open the input and output files */
70  ifp = fopen(infile, "rt");
71  if (outfile != NULL)
72  {
73  ofp = fopen(outfile, "at");
74  }
75 
76  /* Start the API */
77  result = OmStartup(OM_VERSION);
78  if (OM_FAILED(result)) { printf("ERROR: OmStartup() %s\n", OmErrorString(result)); return -1; }
79 
80  /* Loop until we've exhausted the incoming list of session identifiers to issue. */
81  /* NOTE: Production code should not loop in this way, but instead respond to the OmDeviceCallback events. */
82  while (ifp != NULL && !feof(ifp))
83  {
84  int numDevices;
85  int *deviceIds;
86  int i;
87 
88  /* Query the current number of devices attached */
89  result = OmGetDeviceIds(NULL, 0);
90  if (OM_FAILED(result)) { printf("ERROR: OmGetDeviceIds() %s\n", OmErrorString(result)); return -1; }
91  numDevices = result;
92 
93  /* Get the currently-attached devices ids */
94  deviceIds = (int *)malloc(numDevices * sizeof(int));
95  result = OmGetDeviceIds(deviceIds, numDevices);
96  if (OM_FAILED(result)) { printf("ERROR: OmGetDeviceIds() %s\n", OmErrorString(result)); return -1; }
97  /* Cope with fewer devices being returned (if some were just removed). */
98  if (result < numDevices) { numDevices = result; }
99 
100  /* For each device currently connected... */
101  for (i = 0; i < numDevices; i++)
102  {
103  int dataOffsetBlocks, dataNumBlocks;
104  unsigned int sessionId;
105  int deviceId = deviceIds[i];
106  char timeStartString[32], timeStopString[32];
107 
108  /* If we don't have a 'next session id', read the file to get one */
109  while (nextSessionId == 0)
110  {
111  static char line[100];
112  if (fgets(line, 100, ifp) == NULL) { break; }
113  nextSessionId = (unsigned int)atoi(line);
114  }
115  if (nextSessionId == 0) { break; }
116 
117  /* Check battery level */
118  result = OmGetBatteryLevel(deviceId);
119  if (OM_FAILED(result)) { printf("WARNING: OmGetBatteryLevel() %s\n", OmErrorString(result)); continue; }
120 
121  /* Check if still charging */
122  if (result < 100)
123  {
124  /* Set the LED to yellow to indicate charging */
125  OmSetLed(deviceId, OM_LED_YELLOW);
126  printf("%d = CHARGING\n", deviceId);
127  continue;
128  }
129 
130  /* The battery is at 100%, check the data range */
131  result = OmGetDataRange(deviceId, NULL, &dataOffsetBlocks, &dataNumBlocks, NULL, NULL);
132  if (OM_FAILED(result)) { printf("ERROR: OmGetDataRange() %s\n", OmErrorString(result)); continue; }
133 
134  /* See if this device is clear (has a zero session id) */
135  result = OmGetSessionId(deviceId, &sessionId);
136  if (OM_FAILED(result)) { printf("ERROR: OmGetSessionId() %s\n", OmErrorString(result)); continue; }
137 
138  /* Check if there's any data blocks stored (more than just the headers) */
139  if (dataNumBlocks - dataOffsetBlocks > 0)
140  {
141  printf("DEPLOY #%d: Ignoring - has data stored on the device.\n", deviceId);
142  /* Set the LED to red to indicate full but un-copied data */
143  OmSetLed(deviceId, OM_LED_RED);
144  printf("%d = HAS-DATA (run 'clear' example to reset)\n", deviceId);
145  continue;
146  }
147 
148  if (sessionId != 0)
149  {
150  /* The device is already prepared for deployment - set the LED to green. */
151  OmSetLed(deviceId, OM_LED_GREEN);
152  printf("%d = ALREADY-DEPLOYED (run 'clear' example to reset)\n", deviceId);
153  continue;
154  }
155 
156  /* This is a clear device, ready to set up for deployment */
157 
158  /* Synchronize the time */
159  {
160  time_t now;
161  struct tm *tm;
162  OM_DATETIME nowTime;
163 
164  /* Get the current time */
165  now = time(NULL);
166  tm = localtime(&now);
167  nowTime = OM_DATETIME_FROM_YMDHMS(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
168 
169  printf("DEPLOY #%d: Synchronizing the time...\n", deviceId);
170  result = OmSetTime(deviceId, nowTime);
171  }
172  if (OM_FAILED(result)) { printf("ERROR: OmSetTime() %s\n", OmErrorString(result)); continue; }
173 
174  /* Get the time */
175  {
176  OM_DATETIME time;
177  char buffer[OM_DATETIME_BUFFER_SIZE];
178  result = OmGetTime(deviceId, &time);
179  if (OM_FAILED(result)) { printf("WARNING: OmGetTime() %s\n", OmErrorString(result)); }
180  printf("DEPLOY #%d: Device time: %s\n", deviceId, OmDateTimeToString(time, buffer));
181  }
182 
183  /* Set the delayed start/stop times */
184  {
185  time_t now;
186  struct tm *tm;
187  OM_DATETIME startTime, stopTime;
188  char buffer[OM_DATETIME_BUFFER_SIZE];
189 
190  /* Get the current time */
191  now = time(NULL);
192 
193 #if 1
194  printf("DEPLOY #%d: Setting delayed start/stop times (start in 3 days at 9am, stop after 1 week)\n", deviceId);
195 
196  /* Start recording on the day 3 days from now, at 9am */
197  now += 3 * 24 * 60 * 60; /* 3 days in seconds */
198  tm = localtime(&now);
199  tm->tm_hour = 9;
200  tm->tm_min = 0;
201  tm->tm_sec = 0;
202  startTime = OM_DATETIME_FROM_YMDHMS(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
203  sprintf(timeStartString, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
204 
205  /* Stop recording 7 days from that day, at 9am */
206  now += 7 * 24 * 60 * 60; /* 7 days in seconds */
207  tm = localtime(&now);
208  tm->tm_hour = 9;
209  tm->tm_min = 0;
210  tm->tm_sec = 0;
211  stopTime = OM_DATETIME_FROM_YMDHMS(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
212  sprintf(timeStopString, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
213 
214 #else
215  printf("DEPLOY #%d: Setting delayed start/stop times (start in 1 minute, stop after 1 minute)\n", deviceId);
216 
217  /* Start recording in 1 minute */
218  now += 60;
219  tm = localtime(&now);
220  startTime = OM_DATETIME_FROM_YMDHMS(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
221  sprintf(timeStartString, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
222 
223  /* Stop recording 1 minute later */
224  now += 60;
225  tm = localtime(&now);
226  stopTime = OM_DATETIME_FROM_YMDHMS(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
227  sprintf(timeStopString, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
228 
229 #endif
230 
231  /* Set the delayed start/stop times */
232  printf("DEPLOY #%d: Setting start: %s [%s]\n", deviceId, timeStartString, OmDateTimeToString(startTime, buffer));
233  printf("DEPLOY #%d: Setting stop: %s [%s]\n", deviceId, timeStopString, OmDateTimeToString(stopTime, buffer));
234  result = OmSetDelays(deviceId, startTime, stopTime);
235  }
236  if (OM_FAILED(result)) { printf("ERROR: OmSetDelays() %s\n", OmErrorString(result)); continue; }
237 
238  /* Get the delayed start/stop times */
239  {
240  OM_DATETIME start, stop;
241  char buffer[OM_DATETIME_BUFFER_SIZE];
242  result = OmGetDelays(deviceId, &start, &stop);
243  if (OM_FAILED(result)) { printf("WARNING: OmGetTime() %s\n", OmErrorString(result)); }
244  printf("DEPLOY #%d: Device delayed start: %s\n", deviceId, OmDateTimeToString(start, buffer));
245  printf("DEPLOY #%d: Device delayed stop: %s\n", deviceId, OmDateTimeToString(stop, buffer));
246  }
247 
248  /* Set the session id */
249  result = OmSetSessionId(deviceId, nextSessionId);
250  printf("DEPLOY #%d: Setting session id: %u\n", deviceId, nextSessionId);
251  if (OM_FAILED(result)) { printf("ERROR: OmSetSessionId() %s\n", OmErrorString(result)); continue; }
252 
253  /* Get the session id */
254  {
255  unsigned int sessionId;
256  result = OmGetSessionId(deviceId, &sessionId);
257  if (OM_FAILED(result)) { printf("WARNING: OmGetSessionId() %s\n", OmErrorString(result)); }
258  printf("DEPLOY #%d: Device session id: %u\n", deviceId, sessionId);
259  }
260 
261  /* Commit the new settings */
262  printf("DEPLOY #%d: Committing new settings...\n", deviceId);
264 
265  /* The device is ready for deployment */
266  printf("DEPLOYED,%u,%d,%s,%s\n", nextSessionId, deviceId, timeStartString, timeStopString);
267  if (ofp != NULL)
268  {
269  fprintf(ofp, "%u,%u,%s,%s\n", nextSessionId, deviceId, timeStartString, timeStopString);
270  }
271  nextSessionId = 0;
272 
273  /* The device is prepared for deployment - set the LED to green. */
274  OmSetLed(deviceId, OM_LED_GREEN);
275  }
276 
277  /* Free our list of device ids */
278  free(deviceIds);
279 
280  /* Wait 15 seconds before querying devices again */
281  Sleep(15000);
282  printf("---\n");
283  }
284 
285  /* Shutdown the API */
286  result = OmShutdown();
287  if (OM_FAILED(result)) { printf("ERROR: OmShutdown() %s\n", OmErrorString(result)); return -1; }
288 
289  /* Close the input and output files */
290  if (ifp != NULL) { fclose(ifp); }
291  if (ofp != NULL) { fclose(ofp); }
292 
293  return 0;
294 }
295 
296 
297 /* Main function */
298 int deploy_main(int argc, char *argv[])
299 {
300  printf("DEPLOY: batch setup of all fully-charged and clear devices.\n");
301  printf("\n");
302  if (argc > 1)
303  {
304  const char *infile = argv[1];
305  const char *outfile = NULL;
306  if (argc > 2)
307  {
308  outfile = argv[2];
309  }
310  return deploy(infile, outfile);
311  }
312  else
313  {
314  printf("Usage: deploy <input-file> [output-file]\n");
315  printf("\n");
316  printf("Where: input-file: contains one numeric session identifier on each line to issue to each deployed device\n");
317  printf(" output-file: will receive the mapping of device ids to each session identifiers\n");
318  printf("\n");
319  printf("Example: deploy input.txt output.txt\n");
320  printf("\n");
321  }
322  return -1;
323 }
324 
OmSetSessionId
int OmSetSessionId(int deviceId, unsigned int sessionId)
Sets the specified device's session identifier to be used at the next recording session.
OM_DATETIME_FROM_YMDHMS
#define OM_DATETIME_FROM_YMDHMS(year, month, day, hours, minutes, seconds)
Macro to create a packed date/time value from components.
Definition: omapi.h:1073
now
unsigned long long now(void)
Definition: verify.c:180
OmErrorString
const char * OmErrorString(int status)
Returns an error string for the specified API return code.
omapi.h
Open Movement API.
OM_DATETIME
unsigned long OM_DATETIME
Definition: omapi.h:332
OM_LED_RED
rgb(1,0,0) Red
Definition: omapi.h:491
OM_LED_YELLOW
rgb(1,1,0) Yellow
Definition: omapi.h:493
OM_ERASE_QUICKFORMAT
Device file-system is re-created and a new data file is created with the current metadata.
Definition: omapi.h:697
OM_FAILED
#define OM_FAILED(value)
Macro to check the specified return value for failure.
Definition: omapi.h:1033
deploy
int deploy(const char *infile, const char *outfile)
Definition: deploy.c:62
OmSetLed
int OmSetLed(int deviceId, OM_LED_STATE ledState)
Sets the specified device's LED colour.
OmGetBatteryLevel
int OmGetBatteryLevel(int deviceId)
Queries the specified device for the current battery charging level.
OM_VERSION
#define OM_VERSION
A numeric code for current API version defined in this header file.
Definition: omapi.h:225
OmGetSessionId
int OmGetSessionId(int deviceId, unsigned int *sessionId)
Queries the specified device's session identifier.
deploy_main
int deploy_main(int argc, char *argv[])
Definition: deploy.c:298
OmSetTime
int OmSetTime(int deviceId, OM_DATETIME time)
Sets the specified device's internal real time clock.
OmGetDataRange
int OmGetDataRange(int deviceId, int *dataBlockSize, int *dataOffsetBlocks, int *dataNumBlocks, OM_DATETIME *startTime, OM_DATETIME *endTime)
Read the size, time-range, and internal chunking of the data buffer.
printf
#define printf(...)
Definition: download.c:57
OmShutdown
int OmShutdown(void)
Shuts down the Open Movement API.
OmSetDelays
int OmSetDelays(int deviceId, OM_DATETIME startTime, OM_DATETIME stopTime)
Sets the specified device's delayed activation start and stop times to use for a new recording sessio...
OM_DATETIME_BUFFER_SIZE
#define OM_DATETIME_BUFFER_SIZE
The size of a buffer to hold a string representation of an OM_DATETIME.
Definition: omapi.h:1093
OM_LED_GREEN
rgb(0,1,0) Green
Definition: omapi.h:489
OmGetTime
int OmGetTime(int deviceId, OM_DATETIME *time)
Queries the specified device's internal real time clock.
OmGetDelays
int OmGetDelays(int deviceId, OM_DATETIME *startTime, OM_DATETIME *stopTime)
Gets the specified device's delayed activation start and stop times.
OmGetDeviceIds
int OmGetDeviceIds(int *deviceIds, int maxDevices)
Obtains the device IDs of all connected devices.
Sleep
#define Sleep(millis)
Definition: deploy.c:49
OmStartup
int OmStartup(int version)
Initializes the Open Movement API.
OmEraseDataAndCommit
int OmEraseDataAndCommit(int deviceId, OM_ERASE_LEVEL eraseLevel)
Erases the specified device storage and commits the metadata and settings.
outfile
FILE * outfile
Definition: verify.c:154