OMAPI  1.8
Open Movement Public API
test.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 
42 /* Headers */
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 
47 /* API header */
48 #include "omapi.h"
49 
50 
51 static int testDevice(int deviceId)
52 {
53  int firmwareVersion = -1, hardwareVersion = -1;
54  int accelerometer[3];
55  int errors = 0;
56  int result;
57 
58  /* Set the LED to blue to indicate testing */
59  result = OmSetLed(deviceId, OM_LED_BLUE);
60  if (OM_FAILED(result)) { printf("WARNING: OmSetLed() %s\n", OmErrorString(result)); }
61 
62  /* Check hardware and firmware versions */
63  result = OmGetVersion(deviceId, &firmwareVersion, &hardwareVersion);
64  if (OM_FAILED(result)) { printf("WARNING: OmGetVersion() %s\n", OmErrorString(result)); errors++; }
65  printf("CHECK #%d: Firmware %d, Hardware %d\n", deviceId, firmwareVersion, hardwareVersion);
66 
67  /* Check battery level */
68  result = OmGetBatteryLevel(deviceId);
69  if (OM_FAILED(result)) { printf("WARNING: OmGetBatteryLevel() %s\n", OmErrorString(result)); errors++; }
70  else
71  {
72  printf("CHECK #%d: Battery at %d%% (%s)\n", deviceId, result, (result >= 100) ? "charged" : "charging");
73  }
74 
75  /* Get accelerometer readings */
76  result = OmGetAccelerometer(deviceId, &accelerometer[0], &accelerometer[1], &accelerometer[2]);
77  if (OM_FAILED(result)) { printf("WARNING: OmGetAccelerometer() %s\n", OmErrorString(result)); errors++; }
78  else
79  {
80  printf("CHECK #%d: Accelerometer at (x = %d, y = %d, z = %d)\n", deviceId, accelerometer[0], accelerometer[1], accelerometer[2]);
81  }
82 
83  /* Check self-test */
84  result = OmSelfTest(deviceId);
85  if (OM_FAILED(result)) { printf("WARNING: OmSelfTest() %s\n", OmErrorString(result)); errors++; }
86  else
87  {
88  if (result == 0)
89  {
90  printf("CHECK #%d: Self-test: OK\n", deviceId);
91  }
92  else
93  {
94  errors++;
95  printf("CHECK #%d: Self-test: FAILED (diagnostic 0x%04x)\n", deviceId, result);
96  }
97  }
98 
99  /* Check memory health */
100  result = OmGetMemoryHealth(deviceId);
101  if (OM_FAILED(result)) { printf("WARNING: OmGetMemoryHealth() %s\n", OmErrorString(result)); errors++; }
102  else
103  {
104  if (result <= OM_MEMORY_HEALTH_ERROR)
105  {
106  errors++;
107  printf("CHECK #%d: Memory health: FAILED (at least one plane has (or is near to having) no free blocks)\n", deviceId);
108  }
109  else if (result <= OM_MEMORY_HEALTH_WARNING)
110  {
111  printf("CHECK #%d: Memory health: WARNING (only %d free blocks on worst plane)\n", deviceId, result);
112  }
113  else
114  {
115  printf("CHECK #%d: Memory health: OK (at least %d free blocks on each plane)\n", deviceId, result);
116  }
117  }
118 
119  /* Check battery health */
120  result = OmGetBatteryHealth(deviceId);
121  if (OM_FAILED(result)) { printf("WARNING: OmGetBatteryHealth() %s\n", OmErrorString(result)); errors++; }
122  else
123  {
124  if (result > 500)
125  {
126  printf("CHECK #%d: Battery health: NOTICE (%d cycles)\n", deviceId, result);
127  }
128  else
129  {
130  printf("CHECK #%d: Battery health: OK (%d cycles)\n", deviceId, result);
131  }
132  }
133 
134  /* Set the LED to WHITE if successful, or RED otherwise */
135  if (errors > 0)
136  {
137  result = OmSetLed(deviceId, OM_LED_RED);
138  }
139  else
140  {
141  result = OmSetLed(deviceId, OM_LED_WHITE);
142  }
143  if (OM_FAILED(result)) { printf("WARNING: OmSetLed() %s\n", OmErrorString(result)); }
144 
145  return 0;
146 }
147 
148 
149 /* Device updated */
150 static void deviceCallback(void *reference, int deviceId, OM_DEVICE_STATUS status)
151 {
152  if (status == OM_DEVICE_CONNECTED)
153  {
154  printf("TEST #%d: Device CONNECTED\n", deviceId);
155  testDevice(deviceId);
156  }
157  else if (status == OM_DEVICE_REMOVED)
158  {
159  printf("TEST #%d: Device REMOVED\n", deviceId);
160  }
161  else
162  {
163  printf("TEST #%d: Error, unexpected status %d\n", deviceId, status);
164  }
165  return;
166 }
167 
168 
169 /* Device test function */
170 int test(char wait)
171 {
172  int numDevices;
173  int *deviceIds;
174  int result;
175  int i;
176 
177  if (wait)
178  {
179  /* Set device callback before API startup to get initially-connected devices through the callback */
180  OmSetDeviceCallback(deviceCallback, NULL);
181 
182  printf("Waiting for devices...\n");
183  }
184 
185  /* Start the API */
186  result = OmStartup(OM_VERSION);
187  if (OM_FAILED(result)) { printf("ERROR: OmStartup() %s\n", OmErrorString(result)); return -1; }
188 
189  if (!wait)
190  {
191  /* Query the current number of devices attached */
192  result = OmGetDeviceIds(NULL, 0);
193  if (OM_FAILED(result)) { printf("ERROR: OmGetDeviceIds() %s\n", OmErrorString(result)); return -1; }
194  numDevices = result;
195 
196  /* Get the currently-attached devices ids */
197  deviceIds = (int *)malloc(numDevices * sizeof(int));
198  result = OmGetDeviceIds(deviceIds, numDevices);
199 
200  if (OM_FAILED(result)) { printf("ERROR: OmGetDeviceIds() %s\n", OmErrorString(result)); return -1; }
201  /* Cope with fewer devices being returned (if some were just removed).
202  * If more devices were just attached, we'll ignore them for now.
203  * (Production code wouldn't typically use OmGetDeviceIds but would instead work asynchronously with OmDeviceCallback) */
204  if (result < numDevices) { numDevices = result; }
205 
206  /* For each device currently connected... */
207  for (i = 0; i < numDevices; i++)
208  {
209  printf("TEST #%d: Device already CONNECTED\n", deviceIds[i]);
210  testDevice(deviceIds[i]);
211  }
212 
213  /* Free our list of device ids */
214  free(deviceIds);
215  }
216  else
217  {
218  /* Block this thread waiting for a console key-press to terminate the downloader.
219  * The callbacks will allow any existing and future devices connected to have their
220  * data downloaded simultaneously. */
221  getchar();
222  printf("Key pressed, shutting down...\n");
223  }
224 
225  /* Shutdown the API */
226  result = OmShutdown();
227  if (OM_FAILED(result)) { printf("ERROR: OmShutdown() %s\n", OmErrorString(result)); return -1; }
228 
229  return 0;
230 }
231 
232 
233 /* Main function */
234 int test_main(int argc, char *argv[])
235 {
236  char wait = 0;
237  printf("TEST: self-test all connected devices.\n");
238  printf("\n");
239  if (argc > 1 && !strcmp(argv[1], "-wait")) { wait = 1; }
240  return test(wait);
241 }
242 
OM_DEVICE_REMOVED
Device is being removed, or is already removed.
Definition: omapi.h:289
OM_DEVICE_CONNECTED
Device has been connected.
Definition: omapi.h:290
OmSetDeviceCallback
int OmSetDeviceCallback(OmDeviceCallback deviceCallback, void *reference)
Sets the callback function that is called whenever a device is added or removed.
OmErrorString
const char * OmErrorString(int status)
Returns an error string for the specified API return code.
omapi.h
Open Movement API.
OmGetMemoryHealth
int OmGetMemoryHealth(int deviceId)
Determine the health of the NAND flash memory on the specified device.
OM_LED_RED
rgb(1,0,0) Red
Definition: omapi.h:491
OmGetAccelerometer
int OmGetAccelerometer(int deviceId, int *x, int *y, int *z)
Gets the specified device's current accelerometer values.
OM_FAILED
#define OM_FAILED(value)
Macro to check the specified return value for failure.
Definition: omapi.h:1033
OmSetLed
int OmSetLed(int deviceId, OM_LED_STATE ledState)
Sets the specified device's LED colour.
test_main
int test_main(int argc, char *argv[])
Definition: test.c:234
OmGetVersion
int OmGetVersion(int deviceId, int *firmwareVersion, int *hardwareVersion)
Returns the firmware and hardware versions of the specified device.
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
OM_MEMORY_HEALTH_ERROR
#define OM_MEMORY_HEALTH_ERROR
Threshold at or below which the OmGetMemoryHealth() result should be treated as a failure.
Definition: omapi.h:431
test
int test(char wait)
Definition: test.c:170
printf
#define printf(...)
Definition: download.c:57
OmShutdown
int OmShutdown(void)
Shuts down the Open Movement API.
OM_LED_WHITE
rgb(1,1,1) White
Definition: omapi.h:494
OmSelfTest
int OmSelfTest(int deviceId)
Performs a firmware-specific self-test on the specified device (e.g.
OM_MEMORY_HEALTH_WARNING
#define OM_MEMORY_HEALTH_WARNING
Threshold at or below which the OmGetMemoryHealth() result should be treated as a warning.
Definition: omapi.h:432
OmGetDeviceIds
int OmGetDeviceIds(int *deviceIds, int maxDevices)
Obtains the device IDs of all connected devices.
OmGetBatteryHealth
int OmGetBatteryHealth(int deviceId)
Determine the health of the battery on the specified device.
OM_LED_BLUE
rgb(0,0,1) Blue
Definition: omapi.h:488
OmStartup
int OmStartup(int version)
Initializes the Open Movement API.
OM_DEVICE_STATUS
OM_DEVICE_STATUS
Device states used in the OmDeviceCallback handler.
Definition: omapi.h:287