1 #include "utils/includes.h"
2 #include "utils/common.h"
3 #include "utils/ucode.h"
4 #include "wpa_supplicant_i.h"
5 #include "wps_supplicant.h"
8 static struct wpa_global
*wpa_global
;
9 static uc_resource_type_t
*global_type
, *iface_type
;
10 static uc_value_t
*global
, *iface_registry
;
14 wpas_ucode_iface_get_uval(struct wpa_supplicant
*wpa_s
)
19 return wpa_ucode_registry_get(iface_registry
, wpa_s
->ucode
.idx
);
21 val
= uc_resource_new(iface_type
, wpa_s
);
22 wpa_ucode_registry_add(iface_registry
, val
, &wpa_s
->ucode
.idx
);
28 wpas_ucode_update_interfaces(void)
30 uc_value_t
*ifs
= ucv_object_new(vm
);
31 struct wpa_supplicant
*wpa_s
;
34 for (wpa_s
= wpa_global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
)
35 ucv_object_add(ifs
, wpa_s
->ifname
, ucv_get(wpas_ucode_iface_get_uval(wpa_s
)));
37 ucv_object_add(ucv_prototype_get(global
), "interfaces", ucv_get(ifs
));
41 void wpas_ucode_add_bss(struct wpa_supplicant
*wpa_s
)
45 if (wpa_ucode_call_prepare("iface_add"))
48 uc_value_push(ucv_get(ucv_string_new(wpa_s
->ifname
)));
49 uc_value_push(ucv_get(wpas_ucode_iface_get_uval(wpa_s
)));
50 ucv_put(wpa_ucode_call(2));
54 void wpas_ucode_free_bss(struct wpa_supplicant
*wpa_s
)
58 val
= wpa_ucode_registry_remove(iface_registry
, wpa_s
->ucode
.idx
);
63 if (wpa_ucode_call_prepare("iface_remove"))
66 uc_value_push(ucv_get(ucv_string_new(wpa_s
->ifname
)));
67 uc_value_push(ucv_get(val
));
68 ucv_put(wpa_ucode_call(2));
72 static const char *obj_stringval(uc_value_t
*obj
, const char *name
)
74 uc_value_t
*val
= ucv_object_get(obj
, name
, NULL
);
76 return ucv_string_get(val
);
80 uc_wpas_add_iface(uc_vm_t
*vm
, size_t nargs
)
82 uc_value_t
*info
= uc_fn_arg(0);
83 uc_value_t
*ifname
= ucv_object_get(info
, "iface", NULL
);
84 uc_value_t
*bridge
= ucv_object_get(info
, "bridge", NULL
);
85 uc_value_t
*config
= ucv_object_get(info
, "config", NULL
);
86 uc_value_t
*ctrl
= ucv_object_get(info
, "ctrl", NULL
);
87 uc_value_t
*hapd_ctrl
= ucv_object_get(info
, "hostapd_ctrl", NULL
);
88 struct wpa_interface iface
;
91 if (ucv_type(info
) != UC_OBJECT
)
94 iface
= (struct wpa_interface
){
96 .ifname
= ucv_string_get(ifname
),
97 .bridge_ifname
= ucv_string_get(bridge
),
98 .confname
= ucv_string_get(config
),
99 .ctrl_interface
= ucv_string_get(ctrl
),
100 .hostapd_ctrl
= ucv_string_get(hapd_ctrl
),
103 if (!iface
.ifname
|| !iface
.confname
)
106 ret
= wpa_supplicant_add_iface(wpa_global
, &iface
, 0) ? 0 : -1;
107 wpas_ucode_update_interfaces();
110 return ucv_int64_new(ret
);
114 uc_wpas_remove_iface(uc_vm_t
*vm
, size_t nargs
)
116 struct wpa_supplicant
*wpa_s
= NULL
;
117 uc_value_t
*ifname_arg
= uc_fn_arg(0);
118 const char *ifname
= ucv_string_get(ifname_arg
);
124 for (wpa_s
= wpa_global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
)
125 if (!strcmp(wpa_s
->ifname
, ifname
))
131 ret
= wpa_supplicant_remove_iface(wpa_global
, wpa_s
, 0);
132 wpas_ucode_update_interfaces();
135 return ucv_int64_new(ret
);
138 int wpas_ucode_init(struct wpa_global
*gl
)
140 static const uc_function_list_t global_fns
[] = {
141 { "printf", uc_wpa_printf
},
142 { "getpid", uc_wpa_getpid
},
143 { "add_iface", uc_wpas_add_iface
},
144 { "remove_iface", uc_wpas_remove_iface
},
146 static const uc_function_list_t iface_fns
[] = {
148 uc_value_t
*data
, *proto
;
151 vm
= wpa_ucode_create_vm();
153 global_type
= uc_type_declare(vm
, "wpas.global", global_fns
, NULL
);
154 iface_type
= uc_type_declare(vm
, "hostapd.iface", iface_fns
, NULL
);
156 iface_registry
= ucv_array_new(vm
);
157 uc_vm_registry_set(vm
, "hostap.iface_registry", iface_registry
);
159 global
= wpa_ucode_global_init("wpas", global_type
);
161 if (wpa_ucode_run(HOSTAPD_UC_PATH
"wpa_supplicant.uc"))
172 void wpas_ucode_free(void)
174 if (wpa_ucode_call_prepare("shutdown") == 0)
175 ucv_put(wpa_ucode_call(0));