/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pywrap/server.py

  • Committer: Suren A. Chilingaryan
  • Date: 2016-02-23 06:20:33 UTC
  • mfrom: (346.1.18 pcitool)
  • Revision ID: csa@suren.me-20160223062033-mz8qkpm1a2oioveb
Merge Python scripting support from Vasiliy Chernov

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import time
 
2
import os
 
3
import pcipywrap
 
4
import json
 
5
import BaseHTTPServer
 
6
import sys
 
7
from optparse import OptionParser
 
8
 
 
9
class PcilibServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
10
   def __init__(s, pcilib, *args):
 
11
      s.pcilib = pcilib
 
12
      BaseHTTPServer.BaseHTTPRequestHandler.__init__(s, *args)
 
13
   
 
14
   def do_HEAD(s):
 
15
      s.send_response(200)
 
16
      s.send_header('content-type', 'application/json')
 
17
      s.end_headers()
 
18
      
 
19
   def do_GET(s):
 
20
      length = int(s.headers['Content-Length'])
 
21
      
 
22
      #deserialize input data
 
23
      data = json.loads(s.rfile.read(length).decode('utf-8'))
 
24
      
 
25
      if 'command' in data:
 
26
         command = data['command']
 
27
         if(command == 'help'):
 
28
            s.help(data)
 
29
            
 
30
         #elif(command == 'open'):
 
31
         #   #check required arguments
 
32
         #   if not 'device' in data:
 
33
         #      s.error('message doesnt contains "device" field, '
 
34
         #              'which is required for "open" command', data)
 
35
         #      return
 
36
         #   #parse command arguments and convert them to string
 
37
         #   device = str(data.get('device', None))
 
38
         #   model = data.get('model', None)
 
39
         #   if not model is None:
 
40
                        #       model = str(model)
 
41
         #   
 
42
         #   try:
 
43
         #      s.openPcilibInstance(device, model)
 
44
         #   except Exception as e:
 
45
         #      s.error(str(e), data) 
 
46
         #     return
 
47
                   #
 
48
         #   #Success! Create and send reply
 
49
         #   s.send_response(200)
 
50
         #   s.send_header('content-type', 'application/json')
 
51
         #   s.end_headers()
 
52
         #   out = dict()
 
53
         #   out['status'] = 'ok'
 
54
         #   s.wrapMessageAndSend(out, data)
 
55
            
 
56
         elif(command == 'get_registers_list'):
 
57
            #parse command arguments and convert them to string
 
58
            bank = data.get('bank', None)
 
59
            if not bank is None:
 
60
               bank = str(bank)
 
61
 
 
62
            registers = dict()
 
63
            try:
 
64
               registers = s.pcilib.get_registers_list(bank)
 
65
            except Exception as e:
 
66
               s.error(str(e), data) 
 
67
               return
 
68
               
 
69
            #Success! Create and send reply
 
70
            s.send_response(200)
 
71
            s.send_header('content-type', 'application/json')
 
72
            s.end_headers()
 
73
            out = dict()
 
74
            out['status'] = 'ok'
 
75
            out['registers'] = registers
 
76
            s.wrapMessageAndSend(out, data)
 
77
            
 
78
         elif(command == 'get_register_info'):
 
79
            #check required arguments
 
80
            if not 'reg' in data:
 
81
               s.error('message doesnt contains "reg" field, '
 
82
                       'which is required for "get_register_info" command', data)
 
83
               return
 
84
               
 
85
            #parse command arguments and convert them to string
 
86
            reg = str(data.get('reg', None))
 
87
            bank = data.get('bank', None)
 
88
            if not bank is None:
 
89
               bank = str(bank)
 
90
            
 
91
            register = dict()
 
92
            try:
 
93
               register = s.pcilib.get_register_info(reg, bank)
 
94
            except Exception as e:
 
95
               s.error(str(e), data) 
 
96
               return
 
97
                
 
98
            #Success! Create and send reply
 
99
            s.send_response(200)
 
100
            s.send_header('content-type', 'application/json')
 
101
            s.end_headers()
 
102
            out = dict()
 
103
            out['status'] = 'ok'
 
104
            out['register'] = register
 
105
            s.wrapMessageAndSend(out, data)
 
106
                 
 
107
         elif(command == 'get_property_list'):   
 
108
            #parse command arguments and convert them to string
 
109
            branch = data.get('branch', None)
 
110
            if not branch is None:
 
111
               branch = str(branch)
 
112
            
 
113
            properties = dict()
 
114
            try:
 
115
               properties = s.pcilib.get_property_list(branch)
 
116
            except Exception as e:
 
117
               s.error(str(e), data) 
 
118
               return
 
119
                
 
120
            #Success! Create and send reply
 
121
            s.send_response(200)
 
122
            s.send_header('content-type', 'application/json')
 
123
            s.end_headers()
 
124
            out = dict()
 
125
            out['status'] = 'ok'
 
126
            out['properties'] = properties
 
127
            s.wrapMessageAndSend(out, data)
 
128
            
 
129
         elif(command == 'read_register'):
 
130
            #check required arguments
 
131
            if not 'reg' in data:
 
132
               s.error('message doesnt contains "reg" field, '
 
133
                       'which is required for "read_register" command', data)
 
134
               return
 
135
               
 
136
            #parse command arguments and convert them to string
 
137
            reg = str(data.get('reg', None))
 
138
            bank = data.get('bank', None)
 
139
            if not bank is None:
 
140
                                   bank = str(bank)
 
141
            
 
142
            value = 0
 
143
            try:
 
144
               value = s.pcilib.read_register(reg, bank)
 
145
            except Exception as e:
 
146
               s.error(str(e), data) 
 
147
               return
 
148
            
 
149
            #Success! Create and send reply
 
150
            s.send_response(200)
 
151
            s.send_header('content-type', 'application/json')
 
152
            s.end_headers()
 
153
            out = dict()
 
154
            out['status'] = 'ok'
 
155
            out['value'] = value
 
156
            s.wrapMessageAndSend(out, data)
 
157
            
 
158
         elif(command == 'write_register'):
 
159
            #check required arguments
 
160
            if not 'reg' in data:
 
161
               s.error('message doesnt contains "reg" field, '
 
162
                       'which is required for "write_register" command', data)
 
163
               return
 
164
               
 
165
            if not 'value' in data:
 
166
               s.error('message doesnt contains "value" field, '
 
167
                       'which is required for "write_register" command', data)
 
168
               return
 
169
               
 
170
            #parse command arguments and convert them to string
 
171
            reg = str(data.get('reg', None))
 
172
            value = str(data.get('value', None))
 
173
            bank = data.get('bank', None)
 
174
            if not bank is None:
 
175
                                   bank = str(bank)
 
176
            
 
177
            try:
 
178
               s.pcilib.write_register(value, reg, bank)
 
179
            except Exception as e:
 
180
               s.error(str(e), data) 
 
181
               return
 
182
            
 
183
            #Success! Create and send reply
 
184
            s.send_response(200)
 
185
            s.send_header('content-type', 'application/json')
 
186
            s.end_headers()
 
187
            out = dict()
 
188
            out['status'] = 'ok'
 
189
            s.wrapMessageAndSend(out, data)
 
190
            
 
191
         elif(command == 'get_property'):
 
192
            #check required arguments
 
193
            if not 'prop' in data:
 
194
               s.error('message doesnt contains "prop" field, '
 
195
                       'which is required for "get_property" command', data)
 
196
               return
 
197
               
 
198
            #parse command arguments and convert them to string
 
199
            prop = str(data.get('prop', None))
 
200
            
 
201
            value = 0
 
202
            try:
 
203
               value = s.pcilib.get_property(prop)
 
204
            except Exception as e:
 
205
               s.error(str(e), data) 
 
206
               return
 
207
            
 
208
            #Success! Create and send reply
 
209
            s.send_response(200)
 
210
            s.send_header('content-type', 'application/json')
 
211
            s.end_headers()
 
212
            out = dict()
 
213
            out['status'] = 'ok'
 
214
            out['value'] = value
 
215
            s.wrapMessageAndSend(out, data)
 
216
            
 
217
         elif(command == 'set_property'):
 
218
            #check required arguments
 
219
            if not 'prop' in data:
 
220
               s.error('message doesnt contains "prop" field, '
 
221
                       'which is required for "set_property" command', data)
 
222
               return
 
223
               
 
224
            if not 'value' in data:
 
225
               s.error('message doesnt contains "value" field, '
 
226
                       'which is required for "set_property" command', data)
 
227
               return
 
228
               
 
229
            #parse command arguments and convert them to string
 
230
            prop = str(data.get('prop', None))
 
231
            value = str(data.get('value', None))
 
232
            
 
233
            try:
 
234
               s.pcilib.set_property(value, prop)
 
235
            except Exception as e:
 
236
               s.error(str(e), data) 
 
237
               return
 
238
            
 
239
            #Success! Create and send reply
 
240
            s.send_response(200)
 
241
            s.send_header('content-type', 'application/json')
 
242
            s.end_headers()
 
243
            out = dict()
 
244
            out['status'] = 'ok'
 
245
            s.wrapMessageAndSend(out, data)
 
246
            
 
247
                
 
248
         else:
 
249
                    s.error('command "' + command + '" undefined', data)
 
250
                    return
 
251
      else:
 
252
                  s.error('message doesnt contains "command" field, which is required', data)
 
253
                  return
 
254
                  
 
255
       
 
256
      #print str(s.headers['content-type'])
 
257
      #print post_data['some']
 
258
      
 
259
   """open device context """
 
260
   def openPcilibInstance(s, device, model):
 
261
      s.pcilib = pcipywrap.create_pcilib_instance(device, model)
 
262
         
 
263
   """Send help message"""
 
264
   def help(s, received_message = None):
 
265
      s.send_response(200)
 
266
      s.send_header('content-type', 'application/json')
 
267
      s.end_headers()
 
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'
 
274
      '\n'
 
275
      '  command: help - Get help. This will return usage\n'
 
276
      '\n'
 
277
      
 
278
      '  command: open - Opens context of device. It will be reopened if already open.\n'
 
279
      '    required fields\n'
 
280
      '      device:       - path to the device file [/dev/fpga0]\n'
 
281
      '    optional fields\n'
 
282
      '      model:       - specifies the model of hardware, autodetected if doesnt exists\n'
 
283
      '\n'
 
284
      
 
285
      '  command: get_registers_list - Returns the list of registers provided by the hardware model.\n'
 
286
      '    optional fields\n'
 
287
      '      bank:        - if set, only register within the specified bank will be returned\n'
 
288
      '\n'
 
289
      
 
290
      '  command: get_register_info - Returns the information about the specified register.\n'
 
291
      '    required fields\n'
 
292
      '      reg:         - the name of the register\n'
 
293
      '    optional fields\n'
 
294
      '      bank:        - if set, only register within the specified bank will be returned\n'
 
295
      '\n'
 
296
      
 
297
      '  command: get_property_list - Returns the list of properties available under the specified path.\n'
 
298
      '    optional fields\n'
 
299
      '     branch:        - Path. If not set, will return the top-level properties\n'
 
300
      '\n'
 
301
      
 
302
      '  command: read_register - Reads the specified register.\n'
 
303
      '    required fields\n'
 
304
      '      reg:         - the name of the register\n'
 
305
      '    optional fields\n'
 
306
      '      bank:        - if set, only register within the specified bank will be processed\n'
 
307
      '\n'
 
308
      
 
309
      '  command: write_register - Writes to specified register.\n'
 
310
      '    required fields\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'
 
313
      '    optional fields\n'
 
314
      '      bank:        - if set, only register within the specified bank will be processed\n'
 
315
      '\n'
 
316
      
 
317
      '  command: get_property - Reads / computes the property value.\n'
 
318
      '    required fields\n'
 
319
      '      prop:         - full name including path\n'
 
320
      '\n'
 
321
      
 
322
      '  command: set_property - Writes the property value or executes the code associated with property.\n'
 
323
      '    required fields\n'
 
324
      '      prop:        - full name including path\n'
 
325
      '      value:       - the property value to write. Should be int, float or string (with number)\n'
 
326
      '\n')
 
327
      out = {'status': 'ok', 'usage' : usage}
 
328
      s.wrapMessageAndSend(out, received_message)
 
329
 
 
330
   """Send error message with text description"""     
 
331
   def error(s, info, received_message = None):
 
332
      s.send_response(400)
 
333
      s.send_header('content-type', 'application/json')
 
334
      s.end_headers()
 
335
      out = dict()
 
336
      
 
337
      out['status'] = 'error'
 
338
      out['description'] = info
 
339
      out['note'] = 'send {"command" : "help"} to get help'
 
340
      s.wrapMessageAndSend(out, received_message)
 
341
        
 
342
   def wrapMessageAndSend(s, message, received_message = None):
 
343
      if not received_message is None:
 
344
         message['received_message'] = received_message
 
345
      s.wfile.write(json.dumps(message))
 
346
 
 
347
if __name__ == '__main__':
 
348
   
 
349
   #parce command line options
 
350
   parser = OptionParser()
 
351
   parser.add_option("-p", "--port",  action="store",
 
352
                     type="int", dest="port", default=9000,
 
353
                     help="Set server port (9000)")
 
354
   parser.add_option("-d", "--device",  action="store",
 
355
                     type="string", dest="device", default=str('/dev/fpga0'),
 
356
                     help="FPGA device (/dev/fpga0)")                     
 
357
   parser.add_option("-m", "--model",  action="store",
 
358
                     type="string", dest="model", default=None,
 
359
                     help="Memory model (autodetected)")
 
360
   opts = parser.parse_args()[0]
 
361
   
 
362
   HOST_NAME = ''
 
363
   PORT_NUMBER = opts.port
 
364
   MODEL = opts.model
 
365
   DEVICE = opts.device
 
366
   
 
367
   #Set enviroment variables, if it not setted already
 
368
   if not 'APP_PATH' in os.environ:
 
369
      APP_PATH = ''
 
370
      file_dir = os.path.dirname(os.path.abspath(__file__))
 
371
      APP_PATH = str(os.path.abspath(file_dir + '/../..'))
 
372
      os.environ["APP_PATH"] = APP_PATH
 
373
 
 
374
   if not 'PCILIB_MODEL_DIR' in os.environ:   
 
375
      os.environ['PCILIB_MODEL_DIR'] = os.environ["APP_PATH"] + "/xml"
 
376
      
 
377
   if not 'LD_LIBRARY_PATH' in os.environ: 
 
378
      os.environ['LD_LIBRARY_PATH'] = os.environ["APP_PATH"] + "/pcilib"
 
379
   
 
380
   #redirect logs to exeption
 
381
   pcipywrap.__redirect_logs_to_exeption()
 
382
   
 
383
   #start server
 
384
   pcilib_server = BaseHTTPServer.HTTPServer
 
385
   
 
386
   #pass Pcipywrap to to server handler
 
387
   lib = pcipywrap.Pcipywrap(DEVICE, MODEL)
 
388
   def handler(*args):
 
389
      PcilibServerHandler(lib, *args)
 
390
   
 
391
   httpd = pcilib_server((HOST_NAME, PORT_NUMBER), handler)
 
392
   
 
393
   print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
 
394
   try:
 
395
      httpd.serve_forever()
 
396
   except KeyboardInterrupt:
 
397
      pass
 
398
   httpd.server_close()
 
399
   print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)