Skip to content

Commit

Permalink
Convert integer macros to arity 1
Browse files Browse the repository at this point in the history
Manually convert the macros extracting integers from binaries to an
arity-1 macro. Compiles and a quick review of the diff but probably some
copy/paste errors lurking here.
  • Loading branch information
msantos committed Apr 6, 2013
1 parent f5d9bf9 commit 479ebc5
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 174 deletions.
20 changes: 10 additions & 10 deletions include/wierl.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.

-define(UINT16, 2/native-unsigned-integer-unit:8).
-define(UINT32, 4/native-unsigned-integer-unit:8).
-define(UINT64, 8/native-unsigned-integer-unit:8).
-define(UINT16(N), N:2/native-unsigned-integer-unit:8).
-define(UINT32(N), N:4/native-unsigned-integer-unit:8).
-define(UINT64(N), N:8/native-unsigned-integer-unit:8).

-define(UINT16LE, 2/little-unsigned-integer-unit:8).
-define(UINT32LE, 4/little-unsigned-integer-unit:8).
-define(UINT64LE, 8/little-unsigned-integer-unit:8).
-define(UINT16LE(N), N:2/little-unsigned-integer-unit:8).
-define(UINT32LE(N), N:4/little-unsigned-integer-unit:8).
-define(UINT64LE(N), N:8/little-unsigned-integer-unit:8).

-define(INT16, 2/native-signed-integer-unit:8).
-define(INT32, 4/native-signed-integer-unit:8).
-define(INT16(N), N:2/native-signed-integer-unit:8).
-define(INT32(N), N:4/native-signed-integer-unit:8).

-define(INT16LE, 2/little-signed-integer-unit:8).
-define(INT32LE, 4/little-signed-integer-unit:8).
-define(INT16LE(N), N:2/little-signed-integer-unit:8).
-define(INT32LE(N), N:4/little-signed-integer-unit:8).

-define(IFNAMSIZ, 16).
-define(SIOCSIWSCAN, 16#8B18). % Trigger a scan
Expand Down
4 changes: 2 additions & 2 deletions src/rfkill.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ block(on) -> block(?BLOCK);
block(off) -> block(?UNBLOCK);

block(Block) when is_integer(Block) ->
<<0:?UINT32, % idx
<<?UINT32(0), % idx
0:8, % type
?RFKILL_OP_CHANGE_ALL:8, % op
Block:8, % soft
0:8 % hard
>>.

event(<<Idx:?UINT32,
event(<<?UINT32(Idx),
Type:8,
Op:8,
Soft:8,
Expand Down
74 changes: 37 additions & 37 deletions src/wierl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ param(Param) ->
decode({Key, List}) when is_list(List) ->
{Key, [ decode({Key, N}) || N <- List ]};

decode({essid, <<Len:?UINT16, _Cmd:?UINT16, Rest/binary>>}) ->
decode({essid, <<?UINT16(Len), ?UINT16(_Cmd), Rest/binary>>}) ->
Pad = procket:wordalign(2+2) * 8,
<<0:Pad, ESSID:Len/bytes>> = Rest,
{essid, ESSID};

% struct sockadddr
decode({bssid, <<
?ARPHRD_ETHER:?UINT16, % sa_family_t
?UINT16(?ARPHRD_ETHER), % sa_family_t
Bytes:6/bytes, 0:64 % sa_data: 14 bytes
>>}) ->
{bssid, lists:flatten(string:join([ io_lib:format("~.16b", [N]) || <<N:8>> <= Bytes ], ":"))};

decode({ap, AP}) ->
decode({bssid, AP});

decode({mode, <<Mode:?UINT32>>}) ->
decode({mode, <<?UINT32(Mode)>>}) ->
{mode, mode(Mode)};

decode({qual, Qual}) ->
Expand All @@ -125,66 +125,66 @@ decode({updated, Status}) when Status band ?IW_QUAL_QUAL_UPDATED == 1 ->
decode({updated, _Status}) ->
{updated, false};

decode({freq, <<Channel:?UINT64, _/binary>>}) when Channel < 1000 ->
decode({freq, <<?UINT64(Channel), _/binary>>}) when Channel < 1000 ->
{channel, Channel};
decode({freq, <<M:?INT32, E:?INT16, _I:8, _Flags:8, _/binary>>}) ->
decode({freq, <<?INT32(M), ?INT16(E), _I:8, _Flags:8, _/binary>>}) ->
{frequency, M*math:pow(10, E)};

decode({power, Power}) ->
{power, decode({param, Power})};

decode({param, <<Value:?INT32, Fixed:8, Disabled:8, Flags:?UINT32, _/binary>>}) ->
decode({param, <<?INT32(Value), Fixed:8, Disabled:8, ?UINT32(Flags), _/binary>>}) ->
[{value, Value}, {fixed, Fixed}, {disabled, Disabled}, {flags, Flags}];

decode({range, <<
Throughput:?UINT32,
Min_nwid:?UINT32,
Max_nwid:?UINT32,
Old_num_channels:?UINT16,
?UINT32(Throughput),
?UINT32(Min_nwid),
?UINT32(Max_nwid),
?UINT16(Old_num_channels),
Old_num_frequency:8,
Scan_capa:8,
Event_capa:(6*4)/bytes,
Sensitivity:?INT32,
?INT32(Sensitivity),
Max_qual:4/bytes,
Avg_qual:4/bytes,
Num_bitrates:8,
Bitrate:(?IW_MAX_BITRATES*4)/bytes,
Min_rts:?INT32,
Max_rts:?INT32,
Min_frag:?INT32,
Max_frag:?INT32,
Min_pmp:?INT32,
Max_pmp:?INT32,
Min_pmt:?INT32,
Max_pmt:?INT32,
Pmp_flags:?UINT16,
Pmt_flags:?UINT16,
Pm_capa:?UINT16,
?INT32(Min_rts),
?INT32(Max_rts),
?INT32(Min_frag),
?INT32(Max_frag),
?INT32(Min_pmp),
?INT32(Max_pmp),
?INT32(Min_pmt),
?INT32(Max_pmt),
?UINT16(Pmp_flags),
?UINT16(Pmt_flags),
?UINT16(Pm_capa),
Encoding_size:(?IW_MAX_ENCODING_SIZES*2)/bytes,
Num_encoding_sizes:8,
Max_encoding_tokens:8,
Encoding_login_index:8,
Txpower_capa:?UINT16,
?UINT16(Txpower_capa),
Num_txpower:8,
Txpower:(?IW_MAX_TXPOWER*4)/bytes,
We_version_compiled:8,
We_version_source:8,
Retry_capa:?UINT16,
Retry_flags:?UINT16,
R_time_flags:?UINT16,
Min_retry:?INT32,
Max_retry:?INT32,
Min_r_time:?INT32,
Max_r_time:?INT32,
Num_channels:?UINT16,
?UINT16(Retry_capa),
?UINT16(Retry_flags),
?UINT16(R_time_flags),
?INT32(Min_retry),
?INT32(Max_retry),
?INT32(Min_r_time),
?INT32(Max_r_time),
?UINT16(Num_channels),
Num_frequency:8,
Freq:(?IW_MAX_FREQUENCIES*8)/bytes,
Enc_capa:?UINT32,
Min_pms:?INT32,
Max_pms:?INT32,
% Pms_flags:?UINT16,
% Modul_capa:?INT32,
% Bitrate_capa:?UINT32,
?UINT32(Enc_capa),
?INT32(Min_pms),
?INT32(Max_pms),
% ?UINT16(Pms_flags),
% ?INT32(Modul_capa),
% ?UINT32(Bitrate_capa:?UINT32)
_/binary
>>}) ->
{range, [
Expand Down
10 changes: 5 additions & 5 deletions src/wierl_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ param(Socket, Dev, Key) when is_atom(Key) ->
%% Change wireless setting
%%
param(Socket, Dev, {Key, Val}) when is_atom(Key), is_integer(Val) ->
Struct = <<Val:?UINT32, 0:(12*8)>>,
Struct = <<?UINT32(Val), 0:(12*8)>>,
ioctl(Socket, Dev, set(Key), Struct);
param(Socket, Dev, {essid, Val}) when is_binary(Val) ->
ioctl_point(Socket, Dev, set(essid), Val, 1);
Expand Down Expand Up @@ -146,13 +146,13 @@ ioctl_point(Socket, Dev, Req, Alloc, Flags) ->
{ok, Struct, [Res]} = procket:alloc([
<<Dev/bytes, 0:Bits, 0>>,
{ptr, Alloc},
<<ReqLen:?UINT16, Flags:?UINT16>>
<<?UINT16(ReqLen), ?UINT16(Flags)>>
]),

Pointer = erlang:system_info({wordsize, external}) * 8,

case procket:ioctl(Socket, Req, Struct) of
{ok, <<Dev:Len/bytes, 0:Bits, 0, _Ptr:Pointer, ValLen:?UINT16, _Flag:?UINT16>>} ->
{ok, <<Dev:Len/bytes, 0:Bits, 0, _Ptr:Pointer, ?UINT16(ValLen), ?UINT16(_Flag)>>} ->
{ok, <<Val:ValLen/bytes, _/binary>>} = procket:buf(Res),
{ok, Val};
{error, _} = Error ->
Expand Down Expand Up @@ -182,7 +182,7 @@ down(Dev) when byte_size(Dev) < ?IFNAMSIZ ->

set_flag(FD, Dev, Flag) ->
Res = procket:ioctl(FD, ?SIOCSIFFLAGS,
<<Dev/bytes, 0:((15-byte_size(Dev))*8), 0:8, Flag:?UINT16, 0:(14*8)>>),
<<Dev/bytes, 0:((15-byte_size(Dev))*8), 0:8, ?UINT16(Flag), 0:(14*8)>>),
case Res of
{ok, _} -> ok;
Error -> Error
Expand All @@ -192,7 +192,7 @@ get_flag(FD, Dev) ->
Res = procket:ioctl(FD, ?SIOCGIFFLAGS,
<<Dev/bytes, 0:((15-byte_size(Dev))*8), 0:(16*8)>>),
case Res of
{ok, <<_:(16*8), Flag:?UINT16, _/binary>>} ->
{ok, <<_:(16*8), ?UINT16(Flag), _/binary>>} ->
{ok, Flag};
Error ->
Error
Expand Down
Loading

0 comments on commit 479ebc5

Please sign in to comment.