Vuo  2.3.2
VuoSerialDevice.c
Go to the documentation of this file.
1 
10 #include "type.h"
11 #include "VuoSerialDevice.h"
12 #include "VuoSerial.h"
13 
15 #ifdef VUO_COMPILER
17  "title" : "Serial Device",
18  "description" : "Information about a serial I/O device.",
19  "keywords" : [ ],
20  "version" : "1.0.0",
21  "dependencies" : [
22  "VuoSerialDevices",
23  "VuoText"
24  ]
25  });
26 #endif
28 
33 {
34  if (strcmp(json_object_get_string(js), "path") == 0)
35  return VuoSerialDevice_MatchPath;
36 
37  return VuoSerialDevice_MatchName;
38 }
39 
44 {
45  if (type == VuoSerialDevice_MatchPath)
46  return "path";
47 
48  return "name";
49 }
50 
63 {
64  return (VuoSerialDevice){
65  VuoJson_getObjectValue(VuoSerialDevice_MatchType, js, "matchType", VuoSerialDevice_MatchName),
66  VuoJson_getObjectValue(VuoText, js, "name", NULL),
67  VuoJson_getObjectValue(VuoText, js, "path", NULL)
68  };
69 }
70 
75 {
76  json_object *js = json_object_new_object();
77 
78  json_object *matchTypeObject = json_object_new_string(VuoSerialDevice_getStringForMatchType(value.matchType));
79  json_object_object_add(js, "matchType", matchTypeObject);
80 
81  if (value.name)
82  {
83  json_object *o = VuoText_getJson(value.name);
84  json_object_object_add(js, "name", o);
85  }
86 
87  if (value.path)
88  {
89  json_object *o = VuoText_getJson(value.path);
90  json_object_object_add(js, "path", o);
91  }
92 
93  return js;
94 }
95 
100 {
101  if (valueA.matchType != valueB.matchType)
102  return false;
103 
104  if (!VuoText_areEqual(valueA.name, valueB.name))
105  return false;
106 
107  if (!VuoText_areEqual(valueA.path, valueB.path))
108  return false;
109 
110  return true;
111 }
112 
117 {
118  return VuoText_isLessThan(valueA.name, valueB.name);
119 }
120 
131 {
132  // Already have a path; nothing to do.
133  if (device.matchType == VuoSerialDevice_MatchPath)
134  {
135  realizedDevice->matchType = device.matchType;
136  realizedDevice->name = VuoText_make(device.name);
137  realizedDevice->path = VuoText_make(device.path);
138  return true;
139  }
140 
141  // Otherwise, try to find a matching name.
142 
144  VuoRetain(devices);
145 
146  unsigned long deviceCount = VuoListGetCount_VuoSerialDevice(devices);
147  if (deviceCount == 0)
148  {
149  VUserLog("Warning: No serial devices found.");
150  VuoRelease(devices);
151  return false;
152  }
153 
154  VuoText nameToMatch = "";
155  if (device.name)
156  nameToMatch = device.name;
157 
158  VuoText foundName = NULL;
159  VuoText foundPath = NULL;
160  for (unsigned long i = 1; i <= deviceCount; ++i)
161  {
163  if (strstr(d.name, nameToMatch))
164  {
165  foundName = d.name;
166  foundPath = d.path;
167  break;
168  }
169  }
170 
171  if (foundPath)
172  {
173  realizedDevice->matchType = VuoSerialDevice_MatchPath;
174  realizedDevice->name = VuoText_make(foundName);
175  realizedDevice->path = VuoText_make(foundPath);
176  VuoRelease(devices);
177  return true;
178  }
179 
180  VUserLog("Warning: Didn't find a serial device matching '%s'.", device.name);
181  VuoRelease(devices);
182  return false;
183 }
184 
189 {
190  VuoSerialDevice realizedDevice;
191  if (VuoSerialDevice_realize(value, &realizedDevice))
192  {
193  VuoSerialDevice_retain(realizedDevice);
194  char *outputText = VuoText_format("%s<br>%s", realizedDevice.name, realizedDevice.path);
195  VuoSerialDevice_release(realizedDevice);
196  return outputText;
197  }
198  return strdup("Unknown device");
199 }