1 | %------------------------------------------------------------------------- |
---|
2 | % WARPLab Framework |
---|
3 | % |
---|
4 | % Copyright 2013, Mango Communications. All rights reserved. |
---|
5 | % Distributed under the WARP license (http://warpproject.org/license) |
---|
6 | % |
---|
7 | % Chris Hunter (chunter [at] mangocomm.com) |
---|
8 | % Patrick Murphy (murphpo [at] mangocomm.com) |
---|
9 | % Erik Welsh (welsh [at] mangocomm.com) |
---|
10 | %------------------------------------------------------------------------- |
---|
11 | |
---|
12 | classdef wl_cmd < handle_light |
---|
13 | |
---|
14 | properties(SetAccess = public) |
---|
15 | cmd; |
---|
16 | end |
---|
17 | |
---|
18 | properties(SetAccess = public, Hidden = true) |
---|
19 | len; |
---|
20 | numArgs; |
---|
21 | end |
---|
22 | |
---|
23 | properties(SetAccess = protected, Hidden=true) |
---|
24 | args; |
---|
25 | end |
---|
26 | |
---|
27 | properties(SetAccess = public, Hidden=true) |
---|
28 | % These properties correspond to the WARPLab Command Defines in wl_common.h |
---|
29 | CMD_PARAM_WRITE_VAL = 0; % 0x00000000 |
---|
30 | CMD_PARAM_READ_VAL = 1; % 0x00000001 |
---|
31 | CMD_PARAM_RSVD = hex2dec('FFFFFFFF'); % 0xFFFFFFFF |
---|
32 | |
---|
33 | CMD_PARAM_SUCCESS = 0; % 0x00000000 |
---|
34 | CMD_PARAM_ERROR = hex2dec('FF000000'); % 0xFF000000 |
---|
35 | end |
---|
36 | |
---|
37 | methods |
---|
38 | function obj = wl_cmd(varargin) |
---|
39 | obj.cmd = uint32(0); |
---|
40 | obj.len = uint16(0); |
---|
41 | obj.numArgs = uint16(0); |
---|
42 | |
---|
43 | if(nargin > 0) |
---|
44 | obj.cmd = uint32(varargin{1}); |
---|
45 | end |
---|
46 | |
---|
47 | if(nargin > 1) |
---|
48 | obj.setArgs(varargin{2:end}); |
---|
49 | end |
---|
50 | end |
---|
51 | |
---|
52 | function print(obj,varargin) |
---|
53 | fprintf('WL Command: 0x%8x length = 0x%x num_args = 0x%d \n', obj.cmd, obj.len, obj.numArgs ); |
---|
54 | tmp_args = obj.getArgs(); |
---|
55 | for index = 1:length(tmp_args) |
---|
56 | fprintf(' Arg[%d] = 0x%8x \n', index, tmp_args(index)); |
---|
57 | end |
---|
58 | end |
---|
59 | |
---|
60 | function setArgs(obj,varargin) |
---|
61 | % Delete old args |
---|
62 | obj.len = uint16(0); |
---|
63 | obj.numArgs = uint16(0); |
---|
64 | obj.addArgs(varargin{:}); |
---|
65 | end |
---|
66 | |
---|
67 | function addArgs(obj,varargin) |
---|
68 | for index = length(varargin):-1:1 |
---|
69 | if(min(size(varargin{index}))~=1) |
---|
70 | error('argument %d must be a scalar or 1-dimensional vector',index) |
---|
71 | end |
---|
72 | newArg = uint32(varargin{index}(:).'); |
---|
73 | obj.args{index + (obj.numArgs)} = newArg; |
---|
74 | obj.len = obj.len + (length(newArg)*4); |
---|
75 | end |
---|
76 | obj.numArgs = obj.numArgs + length(varargin); |
---|
77 | end |
---|
78 | |
---|
79 | function output = getArgs(obj) |
---|
80 | if(isempty(obj.args) == 0) |
---|
81 | output = cat(2, obj.args{:}); |
---|
82 | else |
---|
83 | output = []; |
---|
84 | end |
---|
85 | end |
---|
86 | |
---|
87 | function output = sizeof(obj) |
---|
88 | output = length(obj.serialize)*4; |
---|
89 | end |
---|
90 | |
---|
91 | function output = serialize(obj) |
---|
92 | % Serializes the command header and all arguments to a vector of uint32 items |
---|
93 | % The structure of the command header is: |
---|
94 | % typedef struct { |
---|
95 | % u32 cmd; |
---|
96 | % u16 length; |
---|
97 | % u16 numArgs; |
---|
98 | % } wl_cmd_hdr; |
---|
99 | % |
---|
100 | % NOTE: Calling cast(x,'uint32') is much slower than uint32(2) |
---|
101 | % Bitshift/bitor are slower than explicit mults/adds |
---|
102 | % |
---|
103 | output(1) = obj.cmd; |
---|
104 | output(2) = (2^16 * uint32(obj.len)) + uint32(obj.numArgs); |
---|
105 | |
---|
106 | if (~isempty(obj.args)) |
---|
107 | output = cat(2, output, obj.args{:}); |
---|
108 | end |
---|
109 | end |
---|
110 | end |
---|
111 | end |
---|