120
122
out['status'] = 'ok'
121
123
out['properties'] = properties
122
124
s.wrapMessageAndSend(out, data)
126
elif(command == 'read_register'):
127
#check required arguments
128
if not 'reg' in data:
129
s.error('message doesnt contains "reg" field, '
130
'which is required for "read_register" command', data)
133
#parse command arguments and convert them to string
134
reg = str(data.get('reg', None))
135
bank = data.get('bank', None)
141
value = pcipywrap.read_register(reg, bank)
142
except Exception as e:
143
s.error(str(e), data)
146
#Success! Create and send reply
148
s.send_header('content-type', 'application/json')
153
s.wrapMessageAndSend(out, data)
155
elif(command == 'write_register'):
156
#check required arguments
157
if not 'reg' in data:
158
s.error('message doesnt contains "reg" field, '
159
'which is required for "write_register" command', data)
162
if not 'value' in data:
163
s.error('message doesnt contains "value" field, '
164
'which is required for "write_register" command', data)
167
#parse command arguments and convert them to string
168
reg = str(data.get('reg', None))
169
value = str(data.get('value', None))
170
bank = data.get('bank', None)
175
pcipywrap.write_register(value, reg, bank)
176
except Exception as e:
177
s.error(str(e), data)
180
#Success! Create and send reply
182
s.send_header('content-type', 'application/json')
186
s.wrapMessageAndSend(out, data)
188
elif(command == 'get_property'):
189
#check required arguments
190
if not 'prop' in data:
191
s.error('message doesnt contains "prop" field, '
192
'which is required for "get_property" command', data)
195
#parse command arguments and convert them to string
196
prop = str(data.get('prop', None))
200
value = pcipywrap.get_property(prop)
201
except Exception as e:
202
s.error(str(e), data)
205
#Success! Create and send reply
207
s.send_header('content-type', 'application/json')
212
s.wrapMessageAndSend(out, data)
214
elif(command == 'set_property'):
215
#check required arguments
216
if not 'prop' in data:
217
s.error('message doesnt contains "prop" field, '
218
'which is required for "set_property" command', data)
221
if not 'value' in data:
222
s.error('message doesnt contains "value" field, '
223
'which is required for "set_property" command', data)
226
#parse command arguments and convert them to string
227
prop = str(data.get('prop', None))
228
value = str(data.get('value', None))
231
pcipywrap.set_property(value, prop)
232
except Exception as e:
233
s.error(str(e), data)
236
#Success! Create and send reply
238
s.send_header('content-type', 'application/json')
242
s.wrapMessageAndSend(out, data)
125
246
s.error('command "' + command + '" undefined', data)
135
256
"""open device context """
136
257
def openPcilibInstance(s, device, model):
137
pcipywrap.closeCurrentPcilibInstance()
258
pcipywrap.close_curr_pcilib_instance()
139
lib = pcipywrap.createPcilibInstance(device, model)
140
pcipywrap.setPcilib(lib)
260
lib = pcipywrap.create_pcilib_instance(device, model)
261
pcipywrap.set_pcilib(lib)
142
263
"""Send help message"""
143
264
def help(s, received_message = None):
144
265
s.send_response(200)
145
266
s.send_header('content-type', 'application/json')
147
out = {'status': 'ok', 'help' : 'under construction'}
268
usage = str('Usage:\n'
269
' Server receive commands via http GET with json packet.\n'
270
' content-type should have value "application/json"\n'
271
' Server could handle only commands. to set command, you\n'
272
' should specify field "command" in packet with command name\n'
273
' List of commands:\n'
275
' command: help - Get help. This will return usage\n'
278
' command: open - Opens context of device. It will be reopened if already open.\n'
280
' device: - path to the device file [/dev/fpga0]\n'
282
' model: - specifies the model of hardware, autodetected if doesnt exists\n'
285
' command: get_registers_list - Returns the list of registers provided by the hardware model.\n'
287
' bank: - if set, only register within the specified bank will be returned\n'
290
' command: get_register_info - Returns the information about the specified register.\n'
292
' reg: - the name of the register\n'
294
' bank: - if set, only register within the specified bank will be returned\n'
297
' command: get_property_info - Returns the list of properties available under the specified path.\n'
299
' branch: - Path. If not set, will return the top-level properties\n'
302
' command: read_register - Reads the specified register.\n'
304
' reg: - the name of the register\n'
306
' bank: - if set, only register within the specified bank will be processed\n'
309
' command: write_register - Writes to specified register.\n'
311
' reg: - the name of the register\n'
312
' value: - the register value to write. Should be int, float or string (with number)\n'
314
' bank: - if set, only register within the specified bank will be processed\n'
317
' command: get_property - Reads / computes the property value.\n'
319
' prop: - full name including path\n'
322
' command: set_property - Writes the property value or executes the code associated with property.\n'
324
' prop: - full name including path\n'
325
' value: - the property value to write. Should be int, float or string (with number)\n'
327
out = {'status': 'ok', 'usage' : usage}
148
328
s.wrapMessageAndSend(out, received_message)
150
330
"""Send error message with text description"""
151
331
def error(s, info, received_message = None):
152
332
s.send_response(400)
164
344
message['received_message'] = received_message
165
345
s.wfile.write(json.dumps(message))
167
HOST_NAME = '' # !!!REMEMBER TO CHANGE THIS!!!
168
PORT_NUMBER = 12412 # Maybe set this to 9000.
170
348
if __name__ == '__main__':
171
#initialize variables test (to remove)
172
os.environ["APP_PATH"] = '/home/vchernov/1215N/pcitool'
173
os.environ["PCILIB_MODEL_DIR"] = os.environ["APP_PATH"] + "/xml"
174
os.environ["LD_LIBRARY_PATH"] = os.environ["APP_PATH"] + "/pcilib"
350
HOST_NAME = '' # !!!REMEMBER TO CHANGE THIS!!!
351
PORT_NUMBER = 12412 # Maybe set this to 9000.
354
opts, args = getopt.getopt(sys.argv[1:], "", [])
355
#opts, args = getopt.getopt(sys.argv[1:], "hop:v", ["help", "output="])
357
except getopt.GetoptError as err:
358
# print help information and exit:
359
print str(err) # will print something like "option -a not recognized"
363
#Set enviroment variables, if it not setted already
364
if not 'APP_PATH' in os.environ:
366
file_dir = os.path.dirname(os.path.abspath(__file__))
367
APP_PATH = str(os.path.abspath(file_dir + '/../..'))
368
os.environ["APP_PATH"] = APP_PATH
370
if not 'PCILIB_MODEL_DIR' in os.environ:
371
os.environ['PCILIB_MODEL_DIR'] = os.environ["APP_PATH"] + "/xml"
373
if not 'LD_LIBRARY_PATH' in os.environ:
374
os.environ['LD_LIBRARY_PATH'] = os.environ["APP_PATH"] + "/pcilib"
176
376
pcilib_server = BaseHTTPServer.HTTPServer
177
377
httpd = pcilib_server((HOST_NAME, PORT_NUMBER), PcilibServerHandler)