Harald Welte | 7780489 | 2011-02-06 18:12:47 +0100 | [diff] [blame] | 1 | % Osmocom Erlang utility functions |
| 2 | |
| 3 | % (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | % |
| 5 | % All Rights Reserved |
| 6 | % |
| 7 | % This program is free software; you can redistribute it and/or modify |
| 8 | % it under the terms of the GNU Affero General Public License as |
| 9 | % published by the Free Software Foundation; either version 3 of the |
| 10 | % License, or (at your option) any later version. |
| 11 | % |
| 12 | % This program is distributed in the hope that it will be useful, |
| 13 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | % GNU General Public License for more details. |
| 16 | % |
| 17 | % You should have received a copy of the GNU Affero General Public License |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 18 | % along with this program. If not, see <https://www.gnu.org/licenses/>. |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 19 | % |
| 20 | % Additional Permission under GNU AGPL version 3 section 7: |
| 21 | % |
| 22 | % If you modify this Program, or any covered work, by linking or |
| 23 | % combining it with runtime libraries of Erlang/OTP as released by |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 24 | % Ericsson on https://www.erlang.org (or a modified version of these |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 25 | % libraries), containing parts covered by the terms of the Erlang Public |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 26 | % License (https://www.erlang.org/EPLICENSE), the licensors of this |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 27 | % Program grant you additional permission to convey the resulting work |
| 28 | % without the need to license the runtime libraries of Erlang/OTP under |
| 29 | % the GNU Affero General Public License. Corresponding Source for a |
| 30 | % non-source form of such a combination shall include the source code |
| 31 | % for the parts of the runtime libraries of Erlang/OTP used as well as |
| 32 | % that of the covered work. |
Harald Welte | 7780489 | 2011-02-06 18:12:47 +0100 | [diff] [blame] | 33 | |
| 34 | -module(osmo_util). |
| 35 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 36 | |
| 37 | -export([digit_list2int/1, int2digit_list/1]). |
| 38 | -export([reload_config/0]). |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 39 | -export([tuple_walk/3, tuple_walk_print_cb/3]). |
Harald Welte | 0f2f596 | 2011-04-04 15:59:49 +0200 | [diff] [blame] | 40 | -export([make_prim/4, make_prim/3]). |
Harald Welte | 8ff5df4 | 2011-04-04 21:52:52 +0200 | [diff] [blame] | 41 | -export([pointcode2int/1, pointcode2int/2, pointcode_fmt/2]). |
Harald Welte | 79e233f | 2012-01-31 22:30:22 +0100 | [diff] [blame] | 42 | -export([asn_val/1]). |
Harald Welte | bd88eb1 | 2013-09-08 21:32:32 +0200 | [diff] [blame] | 43 | -export([get_env/2, get_env/3]). |
Harald Welte | 0f2f596 | 2011-04-04 15:59:49 +0200 | [diff] [blame] | 44 | |
| 45 | -include("osmo_util.hrl"). |
| 46 | |
| 47 | -compile({parse_transform, exprecs}). |
| 48 | -export_records([primitive]). |
Harald Welte | 7780489 | 2011-02-06 18:12:47 +0100 | [diff] [blame] | 49 | |
| 50 | % Convert a list of digits to an integer value |
| 51 | digit_list2int(Int, []) -> |
| 52 | Int; |
| 53 | digit_list2int(Int, [Digit|Tail]) -> |
| 54 | digit_list2int(Int*10 + Digit, Tail). |
| 55 | digit_list2int(Digits) when is_list(Digits) -> |
| 56 | digit_list2int(0, Digits). |
| 57 | |
| 58 | % Convert an integer value into a list of decimal digits |
| 59 | int2digit_list(0, Digits) when is_list(Digits) -> |
| 60 | Digits; |
| 61 | int2digit_list(Int, Digits) when is_integer(Int), is_list(Digits) -> |
| 62 | Digit = Int rem 10, |
| 63 | int2digit_list(Int div 10, [Digit|Digits]). |
| 64 | int2digit_list(Int) when is_integer(Int) -> |
| 65 | int2digit_list(Int, []). |
| 66 | |
| 67 | % reload configuration of an application |
| 68 | reload_config() -> |
| 69 | case init:get_argument(config) of |
| 70 | {ok, [ Files ]} -> |
| 71 | ConfFiles = [begin |
| 72 | S = filename:basename(F,".config"), |
| 73 | filename:join(filename:dirname(F), |
| 74 | S ++ ".config") |
| 75 | end || F <- Files], |
| 76 | % Move sys.config to the head of the list |
| 77 | Config = lists:sort(fun("sys.config", _) -> true; |
| 78 | (_, _) -> false end, ConfFiles), |
| 79 | |
| 80 | OldEnv = application_controller:prep_config_change(), |
| 81 | |
| 82 | Apps = [{application, A, make_appl(A)} |
| 83 | || {A,_,_} <- application:which_applications()], |
| 84 | application_controller:change_application_data(Apps, Config), |
| 85 | application_controller:config_change(OldEnv); |
| 86 | _ -> |
| 87 | {ok, []} |
| 88 | end. |
| 89 | |
| 90 | make_appl(App) when is_atom(App) -> |
| 91 | AppList = element(2,application:get_all_key(App)), |
| 92 | FullName = code:where_is_file(atom_to_list(App) ++ ".app"), |
| 93 | case file:consult(FullName) of |
| 94 | {ok, [{application, _, Opts}]} -> |
| 95 | Env = proplists:get_value(env, Opts, []), |
| 96 | lists:keyreplace(env, 1, AppList, {env, Env}); |
| 97 | {error, _Reason} -> |
| 98 | lists:keyreplace(env, 1, AppList, {env, []}) |
| 99 | end. |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 100 | |
| 101 | |
| 102 | % Walk a named tuple and (recursively) all its fields, call user-supplied |
| 103 | % callback for each of them |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 104 | tuple_walk(Tpl, TupleCb, Args) when is_tuple(Tpl), is_function(TupleCb), |
| 105 | is_list(Args) -> |
| 106 | tuple_walk([], Tpl, TupleCb, Args). |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 107 | |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 108 | tuple_walk(Path, Tpl, TupleCb, Args) when is_list(Path), is_tuple(Tpl), |
| 109 | is_list(Args) -> |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 110 | % call Callback |
Tobias Engel | d49aa21 | 2013-06-13 11:16:57 +0200 | [diff] [blame] | 111 | RetVal = TupleCb(Path, Tpl, Args), |
| 112 | if |
| 113 | is_tuple(RetVal) -> |
| 114 | [TplName|TplList] = tuple_to_list(RetVal), |
| 115 | NewTplList = tuple_fieldlist_walk(Path, TplName, TplList, TupleCb, Args), |
| 116 | list_to_tuple([TplName|NewTplList]); |
| 117 | true -> |
| 118 | RetVal |
| 119 | end; |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 120 | tuple_walk(Path, TplL, TupleCb, Args) when is_list(Path), is_list(TplL), |
| 121 | is_list(Args) -> |
| 122 | tuple_walk_list(Path, TplL, TupleCb, Args, []). |
Harald Welte | 4a0afae | 2011-03-07 23:54:04 +0100 | [diff] [blame] | 123 | |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 124 | tuple_walk_list(_Path, [], _TupleCb, _Args, OutList) -> |
Harald Welte | 4a0afae | 2011-03-07 23:54:04 +0100 | [diff] [blame] | 125 | OutList; |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 126 | tuple_walk_list(Path, [Head|Tail], TupleCb, Args, OutList) -> |
Harald Welte | 4a0afae | 2011-03-07 23:54:04 +0100 | [diff] [blame] | 127 | if |
| 128 | is_tuple(Head) -> |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 129 | NewHead = tuple_walk(Path, Head, TupleCb, Args); |
Harald Welte | 4a0afae | 2011-03-07 23:54:04 +0100 | [diff] [blame] | 130 | is_list(Head) -> |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 131 | NewHead = tuple_walk(Path, Head, TupleCb, Args); |
Harald Welte | 4a0afae | 2011-03-07 23:54:04 +0100 | [diff] [blame] | 132 | true -> |
| 133 | NewHead = Head |
| 134 | end, |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 135 | tuple_walk_list(Path, Tail, TupleCb, Args, OutList++[NewHead]). |
Harald Welte | 4a0afae | 2011-03-07 23:54:04 +0100 | [diff] [blame] | 136 | |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 137 | |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 138 | tuple_fieldlist_walk(Path, TplName, FieldList, TupleCb, Args) -> |
| 139 | tuple_fieldlist_walk(Path, TplName, FieldList, TupleCb, Args, []). |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 140 | |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 141 | tuple_fieldlist_walk(_Path, _TplName, [], _TplCb, _Args, OutList) -> |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 142 | OutList; |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 143 | tuple_fieldlist_walk(Path, TplName, [Head|List], TupleCb, Args, OutList) -> |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 144 | if |
| 145 | is_tuple(Head) -> |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 146 | NewHead = tuple_walk(Path++[TplName], Head, TupleCb, Args); |
Harald Welte | 4a0afae | 2011-03-07 23:54:04 +0100 | [diff] [blame] | 147 | is_list(Head) -> |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 148 | NewHead = tuple_walk(Path++[TplName], Head, TupleCb, Args); |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 149 | true -> |
| 150 | NewHead = Head |
| 151 | end, |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 152 | tuple_fieldlist_walk(Path, TplName, List, TupleCb, Args, OutList++[NewHead]). |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 153 | |
| 154 | |
Harald Welte | 8a6823d | 2011-03-08 16:03:31 +0100 | [diff] [blame] | 155 | tuple_walk_print_cb(Path, Tpl, _Args) when is_list(Path), is_tuple(Tpl) -> |
Harald Welte | cf43601 | 2011-03-07 12:54:15 +0100 | [diff] [blame] | 156 | io:format("~p:~p~n", [Path, Tpl]), |
| 157 | Tpl. |
Harald Welte | 0f2f596 | 2011-04-04 15:59:49 +0200 | [diff] [blame] | 158 | |
| 159 | % helper function to create a #primitive record |
| 160 | make_prim(Subsys, GenName, SpecName) -> |
| 161 | make_prim(Subsys, GenName, SpecName, []). |
| 162 | make_prim(Subsys, GenName, SpecName, Param) -> |
| 163 | #primitive{subsystem = Subsys, gen_name = GenName, |
| 164 | spec_name = SpecName, parameters = Param}. |
Harald Welte | 8ff5df4 | 2011-04-04 21:52:52 +0200 | [diff] [blame] | 165 | |
| 166 | % parse a 3-tuple pointcode into a raw integer |
Harald Welte | 7ad37a2 | 2011-12-08 11:50:00 +0100 | [diff] [blame] | 167 | pointcode2int(Int) when is_integer(Int) -> |
| 168 | Int; |
Harald Welte | 0bd3528 | 2012-01-23 17:35:57 +0100 | [diff] [blame] | 169 | pointcode2int(undefined) -> |
| 170 | undefined; |
Harald Welte | ba0ada7 | 2011-12-08 00:56:30 +0100 | [diff] [blame] | 171 | pointcode2int(#pointcode{repr=Type, value=Value}) -> |
| 172 | pointcode2int(Type, Value); |
Harald Welte | 8ff5df4 | 2011-04-04 21:52:52 +0200 | [diff] [blame] | 173 | pointcode2int({Std, Param}) -> |
| 174 | pointcode2int(Std, Param). |
| 175 | |
| 176 | pointcode2int(itu, {A, B, C}) -> |
| 177 | <<PcInt:14/big>> = <<A:3, B:8, C:3>>, |
| 178 | PcInt; |
| 179 | pointcode2int(ansi, {A, B, C}) -> |
| 180 | <<PcInt:24/big>> = <<A:8, B:8, C:8>>, |
| 181 | PcInt; |
| 182 | pointcode2int(ttc, {A, B, C}) -> |
| 183 | <<PcInt:16/big>> = <<A:5, B:4, C:7>>, |
| 184 | PcInt. |
| 185 | |
| 186 | % format a point-code into a 3-tuple according to the standard used |
| 187 | pointcode_fmt(Std, P) when is_binary(P) -> |
| 188 | <<PcInt/integer>> = P, |
| 189 | pointcode_fmt(Std, PcInt); |
| 190 | pointcode_fmt(itu, PcInt) when is_integer(PcInt) -> |
| 191 | <<A:3, B:8, C:3>> = <<PcInt:14/big>>, |
| 192 | {pointcode, itu, {A, B, C}}; |
| 193 | pointcode_fmt(ansi, PcInt) -> |
| 194 | <<A:8, B:8, C:8>> = <<PcInt:24/big>>, |
| 195 | {pointcode, ansi, {A, B, C}}; |
| 196 | pointcode_fmt(ttc, PcInt) -> |
| 197 | <<A:5, B:4, C:7>> = <<PcInt:16/big>>, |
| 198 | {pointcode, ttc, {A, B, C}}. |
Harald Welte | 79e233f | 2012-01-31 22:30:22 +0100 | [diff] [blame] | 199 | |
| 200 | asn_val(undefined) -> |
| 201 | asn1_NOVALUE; |
| 202 | asn_val([]) -> |
| 203 | asn1_NOVALUE; |
| 204 | asn_val(Foo) -> |
| 205 | Foo. |
Harald Welte | bd88eb1 | 2013-09-08 21:32:32 +0200 | [diff] [blame] | 206 | |
| 207 | % wrapper around application:get_env() thwowing exception on undef |
| 208 | get_env(App, Var) when is_atom(App), is_atom(Var) -> |
| 209 | case application:get_env(App, Var) of |
| 210 | undefined -> |
| 211 | throw(undefined); |
| 212 | {ok, Value} -> |
| 213 | Value |
| 214 | end. |
| 215 | |
| 216 | get_env(App, Var, Default) -> |
| 217 | case application:get_env(App, Var) of |
| 218 | undefined -> |
| 219 | Default; |
| 220 | {ok, Value} -> |
| 221 | Value |
| 222 | end. |