/alps/pcitool

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pywrap/server.py

  • Committer: Vasilii Chernov
  • Date: 2016-02-12 13:43:20 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160212134320-qj1oj1v4g6ixn9c8
1. Cmakelists - move copy xml folder command to root file
2. - Move set python paths code to python module init funtction
     - pci.c move python module init block code after checking model
       to get paths before it runs.
   - Fix set python path code to work with PYTHONPATH
   - Update pci run script to work with PYTHONPATH
   - Fix python finalize code
3. Change pcilib_script_s interacting method. Now it stores in hash.
4. Change names of some fucntions to more unified ones
5. Remove old unused function pcilib_xml_create_script_or_transform_view
6. cli - disable reading register after set if write_verification flag is off
7. Remove uninformative error messages fro Python wrap.
8. - Server.py - add read/write property/register command handling
   - Add help message
   - Correcting paths

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import time
2
 
import os #delete later
 
2
import os
3
3
import pcipywrap
4
4
import json
5
5
import BaseHTTPServer
 
6
import sys
 
7
import getopt
6
8
 
7
9
class PcilibServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
8
10
        
70
72
            out['registers'] = registers
71
73
            s.wrapMessageAndSend(out, data)
72
74
            
73
 
         elif(command == 'get_register_info'):
74
 
            #check required arguments
 
75
         elif(command == 'get_register_info'):
 
76
            #check required arguments
75
77
            if not 'reg' in data:
76
78
               s.error('message doesnt contains "reg" field, '
77
79
                       'which is required for "get_register_info" command', data)
81
83
            reg = str(data.get('reg', None))
82
84
            bank = data.get('bank', None)
83
85
            if not bank is None:
84
 
                                bank = str(bank)
 
86
               bank = str(bank)
85
87
            
86
88
            register = dict()
87
89
            try:
120
122
            out['status'] = 'ok'
121
123
            out['properties'] = properties
122
124
            s.wrapMessageAndSend(out, data)
 
125
            
 
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)
 
131
               return
 
132
               
 
133
            #parse command arguments and convert them to string
 
134
            reg = str(data.get('reg', None))
 
135
            bank = data.get('bank', None)
 
136
            if not bank is None:
 
137
                                   bank = str(bank)
 
138
            
 
139
            value = 0
 
140
            try:
 
141
               value = pcipywrap.read_register(reg, bank)
 
142
            except Exception as e:
 
143
               s.error(str(e), data) 
 
144
               return
 
145
            
 
146
            #Success! Create and send reply
 
147
            s.send_response(200)
 
148
            s.send_header('content-type', 'application/json')
 
149
            s.end_headers()
 
150
            out = dict()
 
151
            out['status'] = 'ok'
 
152
            out['value'] = value
 
153
            s.wrapMessageAndSend(out, data)
 
154
            
 
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)
 
160
               return
 
161
               
 
162
            if not 'value' in data:
 
163
               s.error('message doesnt contains "value" field, '
 
164
                       'which is required for "write_register" command', data)
 
165
               return
 
166
               
 
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)
 
171
            if not bank is None:
 
172
                                   bank = str(bank)
 
173
            
 
174
            try:
 
175
               pcipywrap.write_register(value, reg, bank)
 
176
            except Exception as e:
 
177
               s.error(str(e), data) 
 
178
               return
 
179
            
 
180
            #Success! Create and send reply
 
181
            s.send_response(200)
 
182
            s.send_header('content-type', 'application/json')
 
183
            s.end_headers()
 
184
            out = dict()
 
185
            out['status'] = 'ok'
 
186
            s.wrapMessageAndSend(out, data)
 
187
            
 
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)
 
193
               return
 
194
               
 
195
            #parse command arguments and convert them to string
 
196
            prop = str(data.get('prop', None))
 
197
            
 
198
            value = 0
 
199
            try:
 
200
               value = pcipywrap.get_property(prop)
 
201
            except Exception as e:
 
202
               s.error(str(e), data) 
 
203
               return
 
204
            
 
205
            #Success! Create and send reply
 
206
            s.send_response(200)
 
207
            s.send_header('content-type', 'application/json')
 
208
            s.end_headers()
 
209
            out = dict()
 
210
            out['status'] = 'ok'
 
211
            out['value'] = value
 
212
            s.wrapMessageAndSend(out, data)
 
213
            
 
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)
 
219
               return
 
220
               
 
221
            if not 'value' in data:
 
222
               s.error('message doesnt contains "value" field, '
 
223
                       'which is required for "set_property" command', data)
 
224
               return
 
225
               
 
226
            #parse command arguments and convert them to string
 
227
            prop = str(data.get('prop', None))
 
228
            value = str(data.get('value', None))
 
229
            
 
230
            try:
 
231
               pcipywrap.set_property(value, prop)
 
232
            except Exception as e:
 
233
               s.error(str(e), data) 
 
234
               return
 
235
            
 
236
            #Success! Create and send reply
 
237
            s.send_response(200)
 
238
            s.send_header('content-type', 'application/json')
 
239
            s.end_headers()
 
240
            out = dict()
 
241
            out['status'] = 'ok'
 
242
            s.wrapMessageAndSend(out, data)
 
243
            
123
244
                
124
245
         else:
125
246
                    s.error('command "' + command + '" undefined', data)
134
255
      
135
256
   """open device context """
136
257
   def openPcilibInstance(s, device, model):
137
 
      pcipywrap.closeCurrentPcilibInstance()
 
258
      pcipywrap.close_curr_pcilib_instance()
138
259
      
139
 
      lib = pcipywrap.createPcilibInstance(device, model)
140
 
      pcipywrap.setPcilib(lib)
 
260
      lib = pcipywrap.create_pcilib_instance(device, model)
 
261
      pcipywrap.set_pcilib(lib)
141
262
         
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')
146
267
      s.end_headers()
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'
 
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_info - 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}
148
328
      s.wrapMessageAndSend(out, received_message)
149
 
   
 
329
 
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))
166
346
 
167
 
HOST_NAME = '' # !!!REMEMBER TO CHANGE THIS!!!
168
 
PORT_NUMBER = 12412 # Maybe set this to 9000.
169
347
 
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"
 
349
   
 
350
   HOST_NAME = '' # !!!REMEMBER TO CHANGE THIS!!!
 
351
   PORT_NUMBER = 12412 # Maybe set this to 9000.
 
352
   
 
353
   try:
 
354
      opts, args = getopt.getopt(sys.argv[1:], "", [])
 
355
      #opts, args = getopt.getopt(sys.argv[1:], "hop:v", ["help", "output="])
 
356
      #print opts, args
 
357
   except getopt.GetoptError as err:
 
358
      # print help information and exit:
 
359
      print str(err) # will print something like "option -a not recognized"
 
360
      #usage()
 
361
      sys.exit(2)
 
362
   
 
363
   #Set enviroment variables, if it not setted already
 
364
   if not 'APP_PATH' in os.environ:
 
365
      APP_PATH = ''
 
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
 
369
 
 
370
   if not 'PCILIB_MODEL_DIR' in os.environ:   
 
371
      os.environ['PCILIB_MODEL_DIR'] = os.environ["APP_PATH"] + "/xml"
 
372
      
 
373
   if not 'LD_LIBRARY_PATH' in os.environ: 
 
374
      os.environ['LD_LIBRARY_PATH'] = os.environ["APP_PATH"] + "/pcilib"
175
375
   
176
376
   pcilib_server = BaseHTTPServer.HTTPServer
177
377
   httpd = pcilib_server((HOST_NAME, PORT_NUMBER), PcilibServerHandler)