main.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * lws-api-test-dhcpc
  3. *
  4. * Written in 2019 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. */
  9. #include <libwebsockets.h>
  10. #include <signal.h>
  11. static int interrupted, ok, fail, exp = 1;
  12. struct lws_context *context;
  13. const char *nif;
  14. static int
  15. lws_dhcpc_cb(void *opaque, int af, uint8_t *ip, int ip_len)
  16. {
  17. lwsl_user("%s: dhcp set OK\n", __func__);
  18. ok = 1;
  19. interrupted = 1;
  20. return 0;
  21. }
  22. void sigint_handler(int sig)
  23. {
  24. interrupted = 1;
  25. }
  26. int
  27. main(int argc, const char **argv)
  28. {
  29. struct lws_context_creation_info info;
  30. const char *p;
  31. int n = 1;
  32. signal(SIGINT, sigint_handler);
  33. memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
  34. lws_cmdline_option_handle_builtin(argc, argv, &info);
  35. lwsl_user("LWS API selftest: DHCP Client\n");
  36. info.port = CONTEXT_PORT_NO_LISTEN;
  37. info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  38. if ((p = lws_cmdline_option(argc, argv, "-i")))
  39. nif = p;
  40. context = lws_create_context(&info);
  41. if (!context) {
  42. lwsl_err("lws init failed\n");
  43. return 1;
  44. }
  45. if (nif) {
  46. lwsl_user("%s: requesting DHCP for %s\n", __func__, nif);
  47. lws_dhcpc_request(context, nif, AF_INET, lws_dhcpc_cb, NULL);
  48. } else {
  49. lwsl_err("%s: use -i <network-interface> to select if\n", __func__);
  50. interrupted = 1;
  51. }
  52. /* the usual lws event loop */
  53. n = 1;
  54. while (n >= 0 && !interrupted)
  55. n = lws_service(context, 0);
  56. lws_context_destroy(context);
  57. if (fail || ok != exp)
  58. lwsl_user("Completed: PASS: %d / %d, FAIL: %d\n", ok, exp,
  59. fail);
  60. else
  61. lwsl_user("Completed: ALL PASS: %d / %d\n", ok, exp);
  62. return !(ok == exp && !fail);
  63. }