LCOV - code coverage report
Current view: top level - examples - notificationPolling.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 63 0.0 %
Date: 2019-09-12 12:28:41 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Example for notification library which repeatedly reads some keys and
       5             :  * reacts to them; see "notificationAsync.c" for an example without polling
       6             :  *
       7             :  * Relevant keys for this example:
       8             :  *   - /sw/example/notification/#0/current/value: Set to any integer value
       9             :  *   - /sw/example/notification/#0/current/color: Set the text color. Possible
      10             :  *     values are "red", "green" and "blue".
      11             :  *
      12             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
      13             :  *
      14             :  */
      15             : 
      16             : #include <kdb.h>
      17             : #include <kdbhelper.h> // ELEKTRA_UNUSED
      18             : #include <kdbnotification.h>
      19             : 
      20             : #include <signal.h>
      21             : #include <stdio.h>
      22             : #include <string.h>
      23             : #include <unistd.h>
      24             : 
      25             : static volatile int keepRunning = 0;
      26             : 
      27             : // from https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
      28             : #define ANSI_COLOR_RESET "\x1b[0m"
      29             : #define ANSI_COLOR_RED "\x1b[31m"
      30             : #define ANSI_COLOR_GREEN "\x1b[32m"
      31             : #define ANSI_COLOR_BLUE "\x1b[34m"
      32             : 
      33           0 : static void setTerminalColor (Key * color, void * context ELEKTRA_UNUSED)
      34             : {
      35           0 :         const char * value = keyString (color);
      36           0 :         printf ("Callback called. Changing color to %s\n", value);
      37             : 
      38           0 :         if (strcmp (value, "red") == 0)
      39             :         {
      40           0 :                 printf (ANSI_COLOR_RED);
      41             :         }
      42           0 :         else if (strcmp (value, "green") == 0)
      43             :         {
      44           0 :                 printf (ANSI_COLOR_GREEN);
      45             :         }
      46           0 :         else if (strcmp (value, "blue") == 0)
      47             :         {
      48           0 :                 printf (ANSI_COLOR_BLUE);
      49             :         }
      50             :         else
      51             :         {
      52           0 :                 printf ("Specified color (%s) did not match \"red\", \"green\" or \"blue\". Using default color.\n", value);
      53           0 :                 printf (ANSI_COLOR_RESET);
      54             :         }
      55           0 : }
      56             : 
      57             : static void resetTerminalColor (void)
      58             : {
      59           0 :         printf (ANSI_COLOR_RESET "\n");
      60             : }
      61             : 
      62           0 : static void printKeyValue (KeySet * ks, Key * search, char * messageNotSet)
      63             : {
      64           0 :         Key * found = ksLookup (ks, search, 0);
      65           0 :         printf ("Key \"%s\"", keyName (search));
      66           0 :         if (!found)
      67             :         {
      68           0 :                 printf (" not set. %s", messageNotSet);
      69             :         }
      70             :         else
      71             :         {
      72           0 :                 printf (" has value \"%s\"", keyString (found));
      73             :         }
      74           0 :         printf ("\n");
      75           0 : }
      76             : 
      77           0 : void onSIGINT (int signal)
      78             : {
      79           0 :         if (signal == SIGINT)
      80             :         {
      81           0 :                 keepRunning = 1;
      82             :         }
      83           0 : }
      84             : 
      85           0 : int main (void)
      86             : {
      87             :         // Cleanup on SIGINT
      88           0 :         signal (SIGINT, onSIGINT);
      89             : 
      90           0 :         KeySet * config = ksNew (20, KS_END);
      91             : 
      92           0 :         Key * key = keyNew ("/sw/example/notification/#0/current", KEY_END);
      93           0 :         KDB * kdb = kdbOpen (key);
      94           0 :         if (kdb == NULL)
      95             :         {
      96           0 :                 printf ("could not open KDB, aborting\n");
      97           0 :                 return -1;
      98             :         }
      99             : 
     100           0 :         int result = elektraNotificationOpen (kdb);
     101           0 :         if (!result)
     102             :         {
     103           0 :                 printf ("could not init notification, aborting\n");
     104           0 :                 return -1;
     105             :         }
     106             : 
     107           0 :         int value = 0;
     108           0 :         Key * intKeyToWatch = keyNew ("/sw/example/notification/#0/current/value", KEY_END);
     109           0 :         result = elektraNotificationRegisterInt (kdb, intKeyToWatch, &value);
     110           0 :         if (!result)
     111             :         {
     112           0 :                 printf ("could not register variable, aborting\n");
     113           0 :                 return -1;
     114             :         }
     115             : 
     116           0 :         Key * callbackKeyToWatch = keyNew ("/sw/example/notification/#0/current/color", KEY_END);
     117           0 :         result = elektraNotificationRegisterCallback (kdb, callbackKeyToWatch, &setTerminalColor, NULL);
     118           0 :         if (!result)
     119             :         {
     120           0 :                 printf ("could not register callback, aborting!");
     121           0 :                 return -1;
     122             :         }
     123             : 
     124           0 :         printf ("Press Ctl+C to exit.\n\n");
     125             : 
     126             :         // repeatedly call kdbGet and print variables
     127           0 :         while (!keepRunning)
     128             :         {
     129             :                 // After this kdbGet the integer variable is updated and the callback was called.
     130             :                 // see "notificationAsync" for an example without polling
     131           0 :                 kdbGet (kdb, config, key);
     132             : 
     133             :                 // Print values
     134           0 :                 printf ("My integer value is %d\n", value);
     135           0 :                 printKeyValue (config, intKeyToWatch, "Try setting it to any integer value!");
     136           0 :                 printKeyValue (config, callbackKeyToWatch, "Try setting it to \"red\", \"green\" or \"blue\"!");
     137           0 :                 printf ("\n");
     138             : 
     139           0 :                 sleep (2);
     140             :         }
     141             : 
     142             :         // Cleanup
     143             :         resetTerminalColor ();
     144           0 :         elektraNotificationClose (kdb);
     145           0 :         kdbClose (kdb, key);
     146           0 :         ksDel (config);
     147           0 :         keyDel (intKeyToWatch);
     148           0 :         keyDel (callbackKeyToWatch);
     149           0 :         keyDel (key);
     150           0 :         printf ("cleanup done!\n");
     151             : }

Generated by: LCOV version 1.13