Amxb_Ubus  3.3.1
Ambiorix Ubus API
test_amxb_ubus_subscribe.c File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <signal.h>
#include <amxc/amxc.h>
#include <amxp/amxp.h>
#include <amxd/amxd_dm.h>
#include <amxb/amxb.h>
#include "test_amxb_ubus_subscribe.h"

Go to the source code of this file.

Functions

static const char * test_amxb_ubus_get_socket (void)
 
static void handle_events (void)
 
int test_amxb_ubus_subscribe_setup (UNUSED void **state)
 
int test_amxb_ubus_subscribe_teardown (UNUSED void **state)
 
static void test_event_cb (UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
 
void test_ubus_can_subscribe (UNUSED void **state)
 
void test_ubus_can_unsubscribe (UNUSED void **state)
 
void test_ubus_can_wait_for_object (UNUSED void **state)
 
void test_ubus_can_reactivate_subscriptions (UNUSED void **state)
 

Variables

static amxb_bus_ctx_t * bus_ctx = NULL
 

Function Documentation

◆ handle_events()

static void handle_events ( void  )
static

Definition at line 89 of file test_amxb_ubus_subscribe.c.

89  {
90  printf("Handling events \n");
91  while(amxb_read(bus_ctx) > 0) {
92  printf("R");
93  }
94  while(amxp_signal_read() == 0) {
95  printf(".");
96  }
97  sleep(1);
98  while(amxb_read(bus_ctx) > 0) {
99  printf("R");
100  }
101  while(amxp_signal_read() == 0) {
102  printf(".");
103  }
104  printf("\n");
105  fflush(stdout);
106 }
static amxb_bus_ctx_t * bus_ctx
Here is the caller graph for this function:

◆ test_amxb_ubus_get_socket()

static const char* test_amxb_ubus_get_socket ( void  )
static

Definition at line 78 of file test_amxb_ubus_subscribe.c.

78  {
79  struct stat sb;
80  if(stat("/var/run/ubus.sock", &sb) == 0) {
81  return "ubus:/var/run/ubus.sock";
82  }
83  if(stat("/var/run/ubus/ubus.sock", &sb) == 0) {
84  return "ubus:/var/run/ubus/ubus.sock";
85  }
86  return NULL;
87 }
Here is the caller graph for this function:

◆ test_amxb_ubus_subscribe_setup()

int test_amxb_ubus_subscribe_setup ( UNUSED void **  state)

Definition at line 108 of file test_amxb_ubus_subscribe.c.

108  {
109  amxc_string_t txt;
110  sigset_t mask;
111  const char* ubus_sock = test_amxb_ubus_get_socket();
112  printf("Ubus socket = %s\n", ubus_sock);
113 
114  sigemptyset(&mask);
115  sigaddset(&mask, SIGALRM);
116 
117  sigprocmask(SIG_BLOCK, &mask, NULL);
118 
119 
120  amxc_string_init(&txt, 0);
121 
122  amxp_sigmngr_add_signal(NULL, "config:changed");
123 
124  amxc_string_reset(&txt);
125  amxc_string_setf(&txt, "amxrt -u ubus: -B ../mod-amxb-test-ubus.so -A ../test_data/test_nemo.odl &");
126  system(amxc_string_get(&txt, 0));
127 
128  amxc_string_clean(&txt);
129 
130  assert_int_equal(amxb_be_load("../mod-amxb-test-ubus.so"), 0);
131  assert_int_equal(amxb_connect(&bus_ctx, ubus_sock), 0);
132 
133  sleep(1);
134 
135  return 0;
136 }
static const char * test_amxb_ubus_get_socket(void)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_amxb_ubus_subscribe_teardown()

int test_amxb_ubus_subscribe_teardown ( UNUSED void **  state)

Definition at line 138 of file test_amxb_ubus_subscribe.c.

138  {
139  amxb_disconnect(bus_ctx);
140  amxb_free(&bus_ctx);
141 
142  system("killall amxrt");
143 
144  amxb_be_remove_all();
145 
146  return 0;
147 }
Here is the caller graph for this function:

◆ test_event_cb()

static void test_event_cb ( UNUSED const char *const  sig_name,
UNUSED const amxc_var_t *const  data,
UNUSED void *const  priv 
)
static

Definition at line 149 of file test_amxb_ubus_subscribe.c.

151  {
152  printf("Event recieved %s\n", sig_name);
153 }
Here is the caller graph for this function:

◆ test_ubus_can_reactivate_subscriptions()

void test_ubus_can_reactivate_subscriptions ( UNUSED void **  state)

Definition at line 199 of file test_amxb_ubus_subscribe.c.

199  {
200  amxc_var_t config;
201  amxc_var_t* ubus_config = NULL;
202  amxc_var_t ret;
203 
204  amxc_var_init(&ret);
205 
206  amxc_var_init(&config);
207  amxc_var_set_type(&config, AMXC_VAR_ID_HTABLE);
208  ubus_config = amxc_var_add_key(amxc_htable_t, &config, "ubus", NULL);
209  amxc_var_add_key(bool, ubus_config, "watch-ubus-events", true);
210  amxb_set_config(&config);
211  amxp_sigmngr_trigger_signal(NULL, "config:changed", NULL);
212 
213  handle_events();
214  assert_int_equal(amxb_subscribe(bus_ctx, "NeMo.Intf.", NULL, test_event_cb, NULL), AMXB_STATUS_OK);
215 
216  system("killall amxrt");
217  handle_events();
218  amxp_timers_calculate();
219  amxp_timers_check();
220  sleep(1);
221  system("amxrt -u ubus: -B ../mod-amxb-test-ubus.so -A ../test_data/test_nemo.odl &");
222  sleep(1);
223  handle_events();
224  amxp_timers_calculate();
225  amxp_timers_check();
226  assert_int_equal(amxb_add(bus_ctx, "NeMo.Intf.", 7, NULL, NULL, &ret, 5), AMXB_STATUS_OK);
227 
228  handle_events();
229  amxp_timers_calculate();
230  amxp_timers_check();
231 
232  assert_int_equal(amxb_add(bus_ctx, "NeMo.Intf.", 8, NULL, NULL, &ret, 5), AMXB_STATUS_OK);
233  handle_events();
234 
235  amxb_set_config(NULL);
236  amxc_var_clean(&config);
237  amxc_var_clean(&ret);
238 }
static void test_event_cb(UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
static void handle_events(void)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_ubus_can_subscribe()

void test_ubus_can_subscribe ( UNUSED void **  state)

Definition at line 155 of file test_amxb_ubus_subscribe.c.

155  {
156  amxc_var_t ret;
157 
158  amxc_var_init(&ret);
159 
160  handle_events();
161 
162  assert_int_equal(amxb_subscribe(bus_ctx, "NeMo.Intf.", NULL, test_event_cb, NULL), AMXB_STATUS_OK);
163  assert_int_equal(amxb_add(bus_ctx, "NeMo.Intf.", 5, NULL, NULL, &ret, 5), AMXB_STATUS_OK);
164 
165  handle_events();
166 
167  amxc_var_clean(&ret);
168 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_ubus_can_unsubscribe()

void test_ubus_can_unsubscribe ( UNUSED void **  state)

Definition at line 170 of file test_amxb_ubus_subscribe.c.

170  {
171  amxc_var_t ret;
172 
173  amxc_var_init(&ret);
174 
175  handle_events();
176 
177 
178  assert_int_equal(amxb_unsubscribe(bus_ctx, "NeMo.Intf.", test_event_cb, NULL), AMXB_STATUS_OK);
179  assert_int_equal(amxb_add(bus_ctx, "NeMo.Intf.", 6, NULL, NULL, &ret, 5), AMXB_STATUS_OK);
180  handle_events();
181 
182  amxc_var_clean(&ret);
183 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_ubus_can_wait_for_object()

void test_ubus_can_wait_for_object ( UNUSED void **  state)

Definition at line 185 of file test_amxb_ubus_subscribe.c.

185  {
186  system("killall amxrt");
187 
188  amxp_sigmngr_add_signal(NULL, "wait:done");
189  amxp_slot_connect(NULL, "wait:done", NULL, test_event_cb, NULL);
190  assert_int_equal(amxb_wait_for_object("NeMo."), AMXB_STATUS_OK);
191  system("amxrt -u ubus: -B ../mod-amxb-test-ubus.so -A ../test_data/test_nemo.odl &");
192  sleep(1);
193 
194  handle_events();
195 
196  amxp_slot_disconnect(NULL, "wait:done", test_event_cb);
197 }
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ bus_ctx

amxb_bus_ctx_t* bus_ctx = NULL
static

Definition at line 76 of file test_amxb_ubus_subscribe.c.